Skip to content

Commit

Permalink
Merge pull request #1162 from nickgonzales/master
Browse files Browse the repository at this point in the history
refactor(http): merge Request and Response from server and client
  • Loading branch information
seanmonstar authored May 2, 2017
2 parents df1095d + 864d3e2 commit 1e04ccd
Show file tree
Hide file tree
Showing 11 changed files with 264 additions and 308 deletions.
12 changes: 6 additions & 6 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ pub use tokio_service::Service;

use header::{Headers, Host};
use http::{self, TokioBody};
use http::response;
use http::request;
use method::Method;
use self::pool::{Pool, Pooled};
use uri::{self, Uri};

pub use http::response::Response;
pub use http::request::Request;
pub use self::connect::{HttpConnector, Connect};
pub use self::request::Request;
pub use self::response::Response;

mod connect;
mod dns;
mod pool;
mod request;
mod response;

/// A Client to make outgoing HTTP requests.
// If the Connector is clone, then the Client can be clone easily.
Expand Down Expand Up @@ -198,8 +198,8 @@ where C: Connect,
});
FutureResponse(Box::new(req.map(|msg| {
match msg {
Message::WithoutBody(head) => response::new(head, None),
Message::WithBody(head, body) => response::new(head, Some(body.into())),
Message::WithoutBody(head) => response::from_wire(head, None),
Message::WithBody(head, body) => response::from_wire(head, Some(body.into())),
}
})))
}
Expand Down
65 changes: 0 additions & 65 deletions src/client/response.rs

This file was deleted.

7 changes: 7 additions & 0 deletions src/http/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ impl From<&'static str> for Body {
}
}

impl From<Option<Body>> for Body {
#[inline]
fn from (body: Option<Body>) -> Body {
body.unwrap_or_default()
}
}

fn _assert_send_sync() {
fn _assert_send<T: Send>() {}
fn _assert_sync<T: Sync>() {}
Expand Down
18 changes: 18 additions & 0 deletions src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ mod io;
mod h1;
//mod h2;
mod str;
pub mod request;
pub mod response;

/*
macro_rules! nonblocking {
Expand Down Expand Up @@ -71,10 +73,26 @@ impl<S> MessageHead<S> {
}
}

impl ResponseHead {
/// Converts this head's RawStatus into a StatusCode.
#[inline]
pub fn status(&self) -> StatusCode {
self.subject.status()
}
}

/// The raw status code and reason-phrase.
#[derive(Clone, PartialEq, Debug)]
pub struct RawStatus(pub u16, pub Cow<'static, str>);

impl RawStatus {
/// Converts this into a StatusCode.
#[inline]
pub fn status(&self) -> StatusCode {
StatusCode::from_u16(self.0)
}
}

impl fmt::Display for RawStatus {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} {}", self.0, self.1)
Expand Down
75 changes: 71 additions & 4 deletions src/client/request.rs → src/http/request.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::fmt;

use header::Headers;
use http::{Body, RequestHead};
use http::{Body, MessageHead, RequestHead, RequestLine};
use method::Method;
use uri::{self, Uri};
use version::HttpVersion;
use std::net::SocketAddr;

/// A client request to a remote server.
pub struct Request<B = Body> {
Expand All @@ -14,6 +15,7 @@ pub struct Request<B = Body> {
headers: Headers,
body: Option<B>,
is_proxy: bool,
remote_addr: Option<SocketAddr>,
}

impl<B> Request<B> {
Expand All @@ -27,13 +29,14 @@ impl<B> Request<B> {
headers: Headers::new(),
body: None,
is_proxy: false,
remote_addr: None,
}
}

/// Read the Request Uri.
#[inline]
pub fn uri(&self) -> &Uri { &self.uri }

/// Read the Request Version.
#[inline]
pub fn version(&self) -> HttpVersion { self.version }
Expand All @@ -48,8 +51,29 @@ impl<B> Request<B> {

/// Read the Request body.
#[inline]
pub fn body(&self) -> Option<&B> { self.body.as_ref() }

pub fn body_ref(&self) -> Option<&B> { self.body.as_ref() }

/// The remote socket address of this request
///
/// This is an `Option`, because some underlying transports may not have
/// a socket address, such as Unix Sockets.
///
/// This field is not used for outgoing requests.
#[inline]
pub fn remote_addr(&self) -> Option<SocketAddr> { self.remote_addr }

/// The target path of this Request.
#[inline]
pub fn path(&self) -> &str {
self.uri.path()
}

/// The query string of this Request.
#[inline]
pub fn query(&self) -> Option<&str> {
self.uri.query()
}

/// Set the Method of this request.
#[inline]
pub fn set_method(&mut self, method: Method) { self.method = method; }
Expand Down Expand Up @@ -78,17 +102,60 @@ impl<B> Request<B> {
pub fn set_proxy(&mut self, is_proxy: bool) { self.is_proxy = is_proxy; }
}

impl Request<Body> {
/// Deconstruct this Request into its pieces.
///
/// Modifying these pieces will have no effect on how hyper behaves.
#[inline]
pub fn deconstruct(self) -> (Method, Uri, HttpVersion, Headers, Body) {
(self.method, self.uri, self.version, self.headers, self.body.unwrap_or_default())
}

/// Take the Request body.
#[inline]
pub fn body(self) -> Body { self.body.unwrap_or_default() }
}

impl<B> fmt::Debug for Request<B> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Request")
.field("method", &self.method)
.field("uri", &self.uri)
.field("version", &self.version)
.field("remote_addr", &self.remote_addr)
.field("headers", &self.headers)
.finish()
}
}

struct MaybeAddr<'a>(&'a Option<SocketAddr>);

impl<'a> fmt::Display for MaybeAddr<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self.0 {
Some(ref addr) => fmt::Display::fmt(addr, f),
None => f.write_str("None"),
}
}
}

/// Constructs a request using a received ResponseHead and optional body
pub fn from_wire<B>(addr: Option<SocketAddr>, incoming: RequestHead, body: B) -> Request<B> {
let MessageHead { version, subject: RequestLine(method, uri), headers } = incoming;
debug!("Request::new: addr={}, req=\"{} {} {}\"", MaybeAddr(&addr), method, uri, version);
debug!("Request::new: headers={:?}", headers);

Request::<B> {
method: method,
uri: uri,
headers: headers,
version: version,
remote_addr: addr,
body: Some(body),
is_proxy: false,
}
}

pub fn split<B>(req: Request<B>) -> (RequestHead, Option<B>) {
let uri = if req.is_proxy {
req.uri
Expand Down
Loading

0 comments on commit 1e04ccd

Please sign in to comment.