Skip to content

Commit 1e04ccd

Browse files
authored
Merge pull request #1162 from nickgonzales/master
refactor(http): merge Request and Response from server and client
2 parents df1095d + 864d3e2 commit 1e04ccd

File tree

11 files changed

+264
-308
lines changed

11 files changed

+264
-308
lines changed

src/client/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@ pub use tokio_service::Service;
2222

2323
use header::{Headers, Host};
2424
use http::{self, TokioBody};
25+
use http::response;
26+
use http::request;
2527
use method::Method;
2628
use self::pool::{Pool, Pooled};
2729
use uri::{self, Uri};
2830

31+
pub use http::response::Response;
32+
pub use http::request::Request;
2933
pub use self::connect::{HttpConnector, Connect};
30-
pub use self::request::Request;
31-
pub use self::response::Response;
3234

3335
mod connect;
3436
mod dns;
3537
mod pool;
36-
mod request;
37-
mod response;
3838

3939
/// A Client to make outgoing HTTP requests.
4040
// If the Connector is clone, then the Client can be clone easily.
@@ -198,8 +198,8 @@ where C: Connect,
198198
});
199199
FutureResponse(Box::new(req.map(|msg| {
200200
match msg {
201-
Message::WithoutBody(head) => response::new(head, None),
202-
Message::WithBody(head, body) => response::new(head, Some(body.into())),
201+
Message::WithoutBody(head) => response::from_wire(head, None),
202+
Message::WithBody(head, body) => response::from_wire(head, Some(body.into())),
203203
}
204204
})))
205205
}

src/client/response.rs

-65
This file was deleted.

src/http/body.rs

+7
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@ impl From<&'static str> for Body {
107107
}
108108
}
109109

110+
impl From<Option<Body>> for Body {
111+
#[inline]
112+
fn from (body: Option<Body>) -> Body {
113+
body.unwrap_or_default()
114+
}
115+
}
116+
110117
fn _assert_send_sync() {
111118
fn _assert_send<T: Send>() {}
112119
fn _assert_sync<T: Sync>() {}

src/http/mod.rs

+18
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ mod io;
2424
mod h1;
2525
//mod h2;
2626
mod str;
27+
pub mod request;
28+
pub mod response;
2729

2830
/*
2931
macro_rules! nonblocking {
@@ -71,10 +73,26 @@ impl<S> MessageHead<S> {
7173
}
7274
}
7375

76+
impl ResponseHead {
77+
/// Converts this head's RawStatus into a StatusCode.
78+
#[inline]
79+
pub fn status(&self) -> StatusCode {
80+
self.subject.status()
81+
}
82+
}
83+
7484
/// The raw status code and reason-phrase.
7585
#[derive(Clone, PartialEq, Debug)]
7686
pub struct RawStatus(pub u16, pub Cow<'static, str>);
7787

88+
impl RawStatus {
89+
/// Converts this into a StatusCode.
90+
#[inline]
91+
pub fn status(&self) -> StatusCode {
92+
StatusCode::from_u16(self.0)
93+
}
94+
}
95+
7896
impl fmt::Display for RawStatus {
7997
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
8098
write!(f, "{} {}", self.0, self.1)

src/client/request.rs renamed to src/http/request.rs

+71-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use std::fmt;
22

33
use header::Headers;
4-
use http::{Body, RequestHead};
4+
use http::{Body, MessageHead, RequestHead, RequestLine};
55
use method::Method;
66
use uri::{self, Uri};
77
use version::HttpVersion;
8+
use std::net::SocketAddr;
89

910
/// A client request to a remote server.
1011
pub struct Request<B = Body> {
@@ -14,6 +15,7 @@ pub struct Request<B = Body> {
1415
headers: Headers,
1516
body: Option<B>,
1617
is_proxy: bool,
18+
remote_addr: Option<SocketAddr>,
1719
}
1820

1921
impl<B> Request<B> {
@@ -27,13 +29,14 @@ impl<B> Request<B> {
2729
headers: Headers::new(),
2830
body: None,
2931
is_proxy: false,
32+
remote_addr: None,
3033
}
3134
}
3235

3336
/// Read the Request Uri.
3437
#[inline]
3538
pub fn uri(&self) -> &Uri { &self.uri }
36-
39+
3740
/// Read the Request Version.
3841
#[inline]
3942
pub fn version(&self) -> HttpVersion { self.version }
@@ -48,8 +51,29 @@ impl<B> Request<B> {
4851

4952
/// Read the Request body.
5053
#[inline]
51-
pub fn body(&self) -> Option<&B> { self.body.as_ref() }
52-
54+
pub fn body_ref(&self) -> Option<&B> { self.body.as_ref() }
55+
56+
/// The remote socket address of this request
57+
///
58+
/// This is an `Option`, because some underlying transports may not have
59+
/// a socket address, such as Unix Sockets.
60+
///
61+
/// This field is not used for outgoing requests.
62+
#[inline]
63+
pub fn remote_addr(&self) -> Option<SocketAddr> { self.remote_addr }
64+
65+
/// The target path of this Request.
66+
#[inline]
67+
pub fn path(&self) -> &str {
68+
self.uri.path()
69+
}
70+
71+
/// The query string of this Request.
72+
#[inline]
73+
pub fn query(&self) -> Option<&str> {
74+
self.uri.query()
75+
}
76+
5377
/// Set the Method of this request.
5478
#[inline]
5579
pub fn set_method(&mut self, method: Method) { self.method = method; }
@@ -78,17 +102,60 @@ impl<B> Request<B> {
78102
pub fn set_proxy(&mut self, is_proxy: bool) { self.is_proxy = is_proxy; }
79103
}
80104

105+
impl Request<Body> {
106+
/// Deconstruct this Request into its pieces.
107+
///
108+
/// Modifying these pieces will have no effect on how hyper behaves.
109+
#[inline]
110+
pub fn deconstruct(self) -> (Method, Uri, HttpVersion, Headers, Body) {
111+
(self.method, self.uri, self.version, self.headers, self.body.unwrap_or_default())
112+
}
113+
114+
/// Take the Request body.
115+
#[inline]
116+
pub fn body(self) -> Body { self.body.unwrap_or_default() }
117+
}
118+
81119
impl<B> fmt::Debug for Request<B> {
82120
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
83121
f.debug_struct("Request")
84122
.field("method", &self.method)
85123
.field("uri", &self.uri)
86124
.field("version", &self.version)
125+
.field("remote_addr", &self.remote_addr)
87126
.field("headers", &self.headers)
88127
.finish()
89128
}
90129
}
91130

131+
struct MaybeAddr<'a>(&'a Option<SocketAddr>);
132+
133+
impl<'a> fmt::Display for MaybeAddr<'a> {
134+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
135+
match *self.0 {
136+
Some(ref addr) => fmt::Display::fmt(addr, f),
137+
None => f.write_str("None"),
138+
}
139+
}
140+
}
141+
142+
/// Constructs a request using a received ResponseHead and optional body
143+
pub fn from_wire<B>(addr: Option<SocketAddr>, incoming: RequestHead, body: B) -> Request<B> {
144+
let MessageHead { version, subject: RequestLine(method, uri), headers } = incoming;
145+
debug!("Request::new: addr={}, req=\"{} {} {}\"", MaybeAddr(&addr), method, uri, version);
146+
debug!("Request::new: headers={:?}", headers);
147+
148+
Request::<B> {
149+
method: method,
150+
uri: uri,
151+
headers: headers,
152+
version: version,
153+
remote_addr: addr,
154+
body: Some(body),
155+
is_proxy: false,
156+
}
157+
}
158+
92159
pub fn split<B>(req: Request<B>) -> (RequestHead, Option<B>) {
93160
let uri = if req.is_proxy {
94161
req.uri

0 commit comments

Comments
 (0)