Skip to content

Commit

Permalink
feat(uri): redesign RequestUri type into Uri
Browse files Browse the repository at this point in the history
Closes #1000

BREAKING CHANGE: The name of `RequestUri` has changed to `Uri`. It is no
  longer an `enum`, but an opaque struct with getter methods.
  • Loading branch information
GuillaumeGomez authored and seanmonstar committed Jan 18, 2017
1 parent 1868f85 commit 9036443
Show file tree
Hide file tree
Showing 10 changed files with 294 additions and 133 deletions.
4 changes: 2 additions & 2 deletions examples/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ impl Service for Echo {

fn call(&self, req: Request) -> Self::Future {
::futures::finished(match (req.method(), req.path()) {
(&Get, Some("/")) | (&Get, Some("/echo")) => {
(&Get, "/") | (&Get, "/echo") => {
Response::new()
.with_header(ContentLength(INDEX.len() as u64))
.with_body(INDEX)
},
(&Post, Some("/echo")) => {
(&Post, "/echo") => {
let mut res = Response::new();
if let Some(len) = req.headers().get::<ContentLength>() {
res.headers_mut().set(len.clone());
Expand Down
6 changes: 0 additions & 6 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use header::{Headers, Host};
use http::{self, TokioBody};
use method::Method;
use self::pool::{Pool, Pooled};
use uri::RequestUri;
use {Url};

pub use self::connect::{HttpConnector, Connect};
Expand Down Expand Up @@ -120,15 +119,10 @@ impl<C: Connect> Service for Client<C> {

fn call(&self, req: Request) -> Self::Future {
let url = req.url().clone();

let (mut head, body) = request::split(req);
let mut headers = Headers::new();
headers.set(Host::new(url.host_str().unwrap().to_owned(), url.port()));
headers.extend(head.headers.iter());
head.subject.1 = RequestUri::AbsolutePath {
path: url.path().to_owned(),
query: url.query().map(ToOwned::to_owned),
};
head.headers = headers;

let checkout = self.pool.checkout(&url[..::url::Position::BeforePath]);
Expand Down
5 changes: 3 additions & 2 deletions src/client/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use Url;
use header::Headers;
use http::{Body, RequestHead};
use method::Method;
use uri::RequestUri;
use uri::Uri;
use version::HttpVersion;

/// A client request to a remote server.
Expand Down Expand Up @@ -79,8 +79,9 @@ impl fmt::Debug for Request {
}

pub fn split(req: Request) -> (RequestHead, Option<Body>) {
let uri = Uri::new(&req.url[::url::Position::BeforePath..::url::Position::AfterQuery]).expect("url is uri");
let head = RequestHead {
subject: ::http::RequestLine(req.method, RequestUri::AbsoluteUri(req.url)),
subject: ::http::RequestLine(req.method, uri),
headers: req.headers,
version: req.version,
};
Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub type Result<T> = ::std::result::Result<T, Error>;
pub enum Error {
/// An invalid `Method`, such as `GE,T`.
Method,
/// An invalid `RequestUri`, such as `exam ple.domain`.
/// An invalid `Uri`, such as `exam ple.domain`.
Uri(url::ParseError),
/// An invalid `HttpVersion`, such as `HTP/1.1`
Version,
Expand Down
6 changes: 2 additions & 4 deletions src/http/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@ mod tests {
use mock::AsyncIo;

use super::{Conn, Writing};
use ::uri::Uri;

#[test]
fn test_conn_init_read() {
Expand All @@ -585,10 +586,7 @@ mod tests {
match conn.poll().unwrap() {
Async::Ready(Some(Frame::Message { message, body: false })) => {
assert_eq!(message, MessageHead {
subject: ::http::RequestLine(::Get, ::RequestUri::AbsolutePath {
path: "/".to_string(),
query: None,
}),
subject: ::http::RequestLine(::Get, Uri::new("/").unwrap()),
.. MessageHead::default()
})
},
Expand Down
4 changes: 2 additions & 2 deletions src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use header::{Connection, ConnectionOption};
use header::Headers;
use method::Method;
use status::StatusCode;
use uri::RequestUri;
use uri::Uri;
use version::HttpVersion;
use version::HttpVersion::{Http10, Http11};

Expand Down Expand Up @@ -51,7 +51,7 @@ pub struct MessageHead<S> {
pub type RequestHead = MessageHead<RequestLine>;

#[derive(Debug, Default, PartialEq)]
pub struct RequestLine(pub Method, pub RequestUri);
pub struct RequestLine(pub Method, pub Uri);

impl fmt::Display for RequestLine {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ pub use http::{Body, Chunk};
pub use method::Method::{self, Get, Head, Post, Delete};
pub use status::StatusCode::{self, Ok, BadRequest, NotFound};
pub use server::Server;
pub use uri::RequestUri;
pub use version::HttpVersion;

macro_rules! unimplemented {
Expand Down
22 changes: 7 additions & 15 deletions src/server/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ use version::HttpVersion;
use method::Method;
use header::Headers;
use http::{RequestHead, MessageHead, RequestLine, Body};
use uri::RequestUri;
use uri::Uri;

/// A request bundles several parts of an incoming `NetworkStream`, given to a `Handler`.
pub struct Request {
method: Method,
uri: RequestUri,
uri: Uri,
version: HttpVersion,
headers: Headers,
remote_addr: SocketAddr,
Expand All @@ -33,7 +33,7 @@ impl Request {

/// The target request-uri for this request.
#[inline]
pub fn uri(&self) -> &RequestUri { &self.uri }
pub fn uri(&self) -> &Uri { &self.uri }

/// The version of HTTP for this request.
#[inline]
Expand All @@ -45,22 +45,14 @@ impl Request {

/// The target path of this Request.
#[inline]
pub fn path(&self) -> Option<&str> {
match self.uri {
RequestUri::AbsolutePath { path: ref p, .. } => Some(p.as_str()),
RequestUri::AbsoluteUri(ref url) => Some(url.path()),
_ => None,
}
pub fn path(&self) -> &str {
self.uri.path()
}

/// The query string of this Request.
#[inline]
pub fn query(&self) -> Option<&str> {
match self.uri {
RequestUri::AbsolutePath { query: ref q, .. } => q.as_ref().map(|x| x.as_str()),
RequestUri::AbsoluteUri(ref url) => url.query(),
_ => None,
}
self.uri.query()
}

/// Take the `Body` of this `Request`.
Expand All @@ -73,7 +65,7 @@ impl Request {
///
/// Modifying these pieces will have no effect on how hyper behaves.
#[inline]
pub fn deconstruct(self) -> (Method, RequestUri, HttpVersion, Headers, Body) {
pub fn deconstruct(self) -> (Method, Uri, HttpVersion, Headers, Body) {
(self.method, self.uri, self.version, self.headers, self.body)
}
}
Expand Down
Loading

0 comments on commit 9036443

Please sign in to comment.