refactor(http): merge Request and Response from server and client

Request and Response are now visible from:
- hyper::{Request, Response}
- hyper::server::{Request, Response}
- hyper::client::{Request, Response}
They truly exist in the http module, but are re-exported to reduce the number of breaking changes.

request::new and response::new were renamed to ::from_wire to reduce confusion with Request::new
and Response::new. See issue #1126

Request now has an optional Body, because not all requests have bodies.
Use body_ref() to determine if a body exists.
Use body() to take the body, or construct one if no body exists.

Closes #1155

BREAKING CHANGE: Response::body() now consumes the response
This commit is contained in:
Nick Gonzales
2017-05-01 12:19:55 -06:00
parent 0de295670f
commit 864d3e27a4
11 changed files with 242 additions and 314 deletions

View File

@@ -22,19 +22,19 @@ pub use tokio_service::Service;
use header::{Headers, Host}; use header::{Headers, Host};
use http::{self, TokioBody}; use http::{self, TokioBody};
use http::response;
use http::request;
use method::Method; use method::Method;
use self::pool::{Pool, Pooled}; use self::pool::{Pool, Pooled};
use uri::{self, Uri}; use uri::{self, Uri};
pub use http::response::Response;
pub use http::request::Request;
pub use self::connect::{HttpConnector, Connect}; pub use self::connect::{HttpConnector, Connect};
pub use self::request::Request;
pub use self::response::Response;
mod connect; mod connect;
mod dns; mod dns;
mod pool; mod pool;
mod request;
mod response;
/// A Client to make outgoing HTTP requests. /// A Client to make outgoing HTTP requests.
// If the Connector is clone, then the Client can be clone easily. // If the Connector is clone, then the Client can be clone easily.
@@ -198,8 +198,8 @@ where C: Connect,
}); });
FutureResponse(Box::new(req.map(|msg| { FutureResponse(Box::new(req.map(|msg| {
match msg { match msg {
Message::WithoutBody(head) => response::new(head, None), Message::WithoutBody(head) => response::from_wire(head, None),
Message::WithBody(head, body) => response::new(head, Some(body.into())), Message::WithBody(head, body) => response::from_wire(head, Some(body.into())),
} }
}))) })))
} }

View File

@@ -1,65 +0,0 @@
use std::fmt;
use header;
use http::{self, RawStatus, Body};
use status;
use version;
pub fn new(incoming: http::ResponseHead, body: Option<Body>) -> Response {
trace!("Response::new");
let status = status::StatusCode::from_u16(incoming.subject.0);
debug!("version={:?}, status={:?}", incoming.version, status);
debug!("headers={:?}", incoming.headers);
Response {
status: status,
version: incoming.version,
headers: incoming.headers,
status_raw: incoming.subject,
body: body,
}
}
/// A response for a client request to a remote server.
pub struct Response {
status: status::StatusCode,
headers: header::Headers,
version: version::HttpVersion,
status_raw: RawStatus,
body: Option<Body>,
}
impl Response {
/// Get the headers from the server.
#[inline]
pub fn headers(&self) -> &header::Headers { &self.headers }
/// Get the status from the server.
#[inline]
pub fn status(&self) -> status::StatusCode { self.status }
/// Get the raw status code and reason.
#[inline]
pub fn status_raw(&self) -> &RawStatus { &self.status_raw }
/// Get the HTTP version of this response from the server.
#[inline]
pub fn version(&self) -> version::HttpVersion { self.version }
/// Take the `Body` of this response.
#[inline]
pub fn body(mut self) -> Body {
self.body.take().unwrap_or(Body::empty())
}
}
impl fmt::Debug for Response {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Response")
.field("status", &self.status)
.field("version", &self.version)
.field("headers", &self.headers)
.finish()
}
}

View File

@@ -34,12 +34,6 @@ impl Default for Body {
} }
} }
impl Default for Body {
fn default() -> Body {
Body::empty()
}
}
impl Stream for Body { impl Stream for Body {
type Item = Chunk; type Item = Chunk;
type Error = ::Error; type Error = ::Error;
@@ -114,6 +108,7 @@ impl From<&'static str> for Body {
} }
impl From<Option<Body>> for Body { impl From<Option<Body>> for Body {
#[inline]
fn from (body: Option<Body>) -> Body { fn from (body: Option<Body>) -> Body {
body.unwrap_or_default() body.unwrap_or_default()
} }

View File

@@ -24,6 +24,8 @@ mod io;
mod h1; mod h1;
//mod h2; //mod h2;
mod str; mod str;
pub mod request;
pub mod response;
/* /*
macro_rules! nonblocking { macro_rules! nonblocking {

View File

@@ -1,10 +1,11 @@
use std::fmt; use std::fmt;
use header::Headers; use header::Headers;
use http::{Body, RequestHead}; use http::{Body, MessageHead, RequestHead, RequestLine};
use method::Method; use method::Method;
use uri::{self, Uri}; use uri::{self, Uri};
use version::HttpVersion; use version::HttpVersion;
use std::net::SocketAddr;
/// A client request to a remote server. /// A client request to a remote server.
pub struct Request<B = Body> { pub struct Request<B = Body> {
@@ -14,6 +15,7 @@ pub struct Request<B = Body> {
headers: Headers, headers: Headers,
body: Option<B>, body: Option<B>,
is_proxy: bool, is_proxy: bool,
remote_addr: Option<SocketAddr>,
} }
impl<B> Request<B> { impl<B> Request<B> {
@@ -27,6 +29,7 @@ impl<B> Request<B> {
headers: Headers::new(), headers: Headers::new(),
body: None, body: None,
is_proxy: false, is_proxy: false,
remote_addr: None,
} }
} }
@@ -48,7 +51,28 @@ impl<B> Request<B> {
/// Read the Request body. /// Read the Request body.
#[inline] #[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. /// Set the Method of this request.
#[inline] #[inline]
@@ -78,17 +102,60 @@ impl<B> Request<B> {
pub fn set_proxy(&mut self, is_proxy: bool) { self.is_proxy = is_proxy; } 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> { impl<B> fmt::Debug for Request<B> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Request") f.debug_struct("Request")
.field("method", &self.method) .field("method", &self.method)
.field("uri", &self.uri) .field("uri", &self.uri)
.field("version", &self.version) .field("version", &self.version)
.field("remote_addr", &self.remote_addr)
.field("headers", &self.headers) .field("headers", &self.headers)
.finish() .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>) { pub fn split<B>(req: Request<B>) -> (RequestHead, Option<B>) {
let uri = if req.is_proxy { let uri = if req.is_proxy {
req.uri req.uri

152
src/http/response.rs Normal file
View File

@@ -0,0 +1,152 @@
use std::fmt;
use header::{Header, Headers};
use http::{MessageHead, ResponseHead, Body, RawStatus};
use status::StatusCode;
use version::HttpVersion;
/// A response for a client request to a remote server.
pub struct Response<B = Body> {
version: HttpVersion,
headers: Headers,
status: StatusCode,
raw_status: RawStatus,
body: Option<B>,
}
impl<B> Response<B> {
/// Constructs a default response
#[inline]
pub fn new() -> Response<B> {
Response::default()
}
/// Get the HTTP version of this response.
#[inline]
pub fn version(&self) -> HttpVersion { self.version }
/// Get the headers from the response.
#[inline]
pub fn headers(&self) -> &Headers { &self.headers }
/// Get a mutable reference to the headers.
#[inline]
pub fn headers_mut(&mut self) -> &mut Headers { &mut self.headers }
/// Get the status from the server.
#[inline]
pub fn status(&self) -> StatusCode { self.status }
/// Get the raw status code and reason.
///
/// This method is only useful when inspecting the raw subject line from
/// a received response.
#[inline]
pub fn status_raw(&self) -> &RawStatus { &self.raw_status }
/// Set the `StatusCode` for this response.
#[inline]
pub fn set_status(&mut self, status: StatusCode) {
self.status = status;
}
/// Set the status and move the Response.
///
/// Useful for the "builder-style" pattern.
#[inline]
pub fn with_status(mut self, status: StatusCode) -> Self {
self.set_status(status);
self
}
/// Set a header and move the Response.
///
/// Useful for the "builder-style" pattern.
#[inline]
pub fn with_header<H: Header>(mut self, header: H) -> Self {
self.headers.set(header);
self
}
/// Set the headers and move the Response.
///
/// Useful for the "builder-style" pattern.
#[inline]
pub fn with_headers(mut self, headers: Headers) -> Self {
self.headers = headers;
self
}
/// Set the body.
#[inline]
pub fn set_body<T: Into<B>>(&mut self, body: T) {
self.body = Some(body.into());
}
/// Set the body and move the Response.
///
/// Useful for the "builder-style" pattern.
#[inline]
pub fn with_body<T: Into<B>>(mut self, body: T) -> Self {
self.set_body(body);
self
}
}
impl Response<Body> {
/// Take the `Body` of this response.
#[inline]
pub fn body(self) -> Body {
self.body.unwrap_or_default()
}
}
impl<B> Default for Response<B> {
fn default() -> Response<B> {
Response::<B> {
version: Default::default(),
headers: Default::default(),
status: Default::default(),
raw_status: Default::default(),
body: None,
}
}
}
impl fmt::Debug for Response {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Response")
.field("status", &self.status)
.field("version", &self.version)
.field("headers", &self.headers)
.finish()
}
}
/// Constructs a response using a received ResponseHead and optional body
#[inline]
pub fn from_wire<B>(incoming: ResponseHead, body: Option<B>) -> Response<B> {
let status = incoming.status();
trace!("Response::new");
debug!("version={:?}, status={:?}", incoming.version, status);
debug!("headers={:?}", incoming.headers);
Response::<B> {
status: status,
version: incoming.version,
headers: incoming.headers,
raw_status: incoming.subject,
body: body,
}
}
/// Splits this response into a MessageHead<StatusCode> and its body
#[inline]
pub fn split<B>(res: Response<B>) -> (MessageHead<StatusCode>, Option<B>) {
let head = MessageHead::<StatusCode> {
version: res.version,
headers: res.headers,
subject: res.status
};
(head, res.body)
}

View File

@@ -39,6 +39,8 @@ pub use client::Client;
pub use error::{Result, Error}; pub use error::{Result, Error};
pub use header::Headers; pub use header::Headers;
pub use http::{Body, Chunk}; pub use http::{Body, Chunk};
pub use http::request::Request;
pub use http::response::Response;
pub use method::Method::{self, Get, Head, Post, Put, Delete}; pub use method::Method::{self, Get, Head, Post, Put, Delete};
pub use status::StatusCode::{self, Ok, BadRequest, NotFound}; pub use status::StatusCode::{self, Ok, BadRequest, NotFound};
pub use server::Server; pub use server::Server;

View File

@@ -24,13 +24,12 @@ use tokio_proto::streaming::Message;
use tokio_proto::streaming::pipeline::{Transport, Frame, ServerProto}; use tokio_proto::streaming::pipeline::{Transport, Frame, ServerProto};
pub use tokio_service::{NewService, Service}; pub use tokio_service::{NewService, Service};
pub use self::request::Request;
pub use self::response::Response;
use http; use http;
use http::response;
use http::request;
mod request; pub use http::response::Response;
mod response; pub use http::request::Request;
/// An instance of the HTTP protocol, and implementation of tokio-proto's /// An instance of the HTTP protocol, and implementation of tokio-proto's
/// `ServerProto` trait. /// `ServerProto` trait.
@@ -284,7 +283,7 @@ impl From<Message<__ProtoRequest, http::TokioBody>> for Request {
Message::WithoutBody(head) => (head.0, http::Body::empty()), Message::WithoutBody(head) => (head.0, http::Body::empty()),
Message::WithBody(head, body) => (head.0, body.into()), Message::WithBody(head, body) => (head.0, body.into()),
}; };
request::new(None, head, body) request::from_wire(None, head, body)
} }
} }
@@ -321,7 +320,7 @@ impl<T, B> Service for HttpService<T>
Message::WithoutBody(head) => (head.0, http::Body::empty()), Message::WithoutBody(head) => (head.0, http::Body::empty()),
Message::WithBody(head, body) => (head.0, body.into()), Message::WithBody(head, body) => (head.0, body.into()),
}; };
let req = request::new(Some(self.remote_addr), head, body); let req = request::from_wire(Some(self.remote_addr), head, body);
self.inner.call(req).map(Into::into) self.inner.call(req).map(Into::into)
} }
} }

View File

@@ -1,113 +0,0 @@
//! Server Requests
//!
//! These are requests that a `hyper::Server` receives, and include its method,
//! target URI, headers, and message body.
use std::fmt;
use std::net::SocketAddr;
use version::HttpVersion;
use method::Method;
use header::Headers;
use http::{RequestHead, MessageHead, RequestLine, Body};
use uri::Uri;
/// A request bundles several parts of an incoming `NetworkStream`, given to a `Handler`.
pub struct Request {
method: Method,
uri: Uri,
version: HttpVersion,
headers: Headers,
remote_addr: Option<SocketAddr>,
body: Body,
}
impl Request {
/// The `Method`, such as `Get`, `Post`, etc.
#[inline]
pub fn method(&self) -> &Method { &self.method }
/// The headers of the incoming request.
#[inline]
pub fn headers(&self) -> &Headers { &self.headers }
/// The target request-uri for this request.
#[inline]
pub fn uri(&self) -> &Uri { &self.uri }
/// The version of HTTP for this request.
#[inline]
pub fn version(&self) -> HttpVersion { self.version }
/// 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.
#[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()
}
/// Take the `Body` of this `Request`.
#[inline]
pub fn body(self) -> Body {
self.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)
}
}
impl fmt::Debug for Request {
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()
}
}
pub fn new(addr: Option<SocketAddr>, incoming: RequestHead, body: Body) -> Request {
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 {
method: method,
uri: uri,
headers: headers,
version: version,
remote_addr: addr,
body: body,
}
}
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"),
}
}
}

View File

@@ -1,111 +0,0 @@
use std::fmt;
use header;
use http::{self, Body};
use status::StatusCode;
use version;
/// The Response sent to a client after receiving a Request in a Service.
///
/// The default `StatusCode` for a `Response` is `200 OK`.
pub struct Response<B = Body> {
head: http::MessageHead<StatusCode>,
body: Option<B>,
}
impl<B> Response<B> {
/// Create a new Response.
#[inline]
pub fn new() -> Response<B> {
Response::default()
}
/// The headers of this response.
#[inline]
pub fn headers(&self) -> &header::Headers { &self.head.headers }
/// The status of this response.
#[inline]
pub fn status(&self) -> StatusCode {
self.head.subject
}
/// The HTTP version of this response.
#[inline]
pub fn version(&self) -> version::HttpVersion { self.head.version }
/// Get a mutable reference to the Headers.
#[inline]
pub fn headers_mut(&mut self) -> &mut header::Headers { &mut self.head.headers }
/// Set the `StatusCode` for this response.
#[inline]
pub fn set_status(&mut self, status: StatusCode) {
self.head.subject = status;
}
/// Set the body.
#[inline]
pub fn set_body<T: Into<B>>(&mut self, body: T) {
self.body = Some(body.into());
}
/// Set the status and move the Response.
///
/// Useful for the "builder-style" pattern.
#[inline]
pub fn with_status(mut self, status: StatusCode) -> Self {
self.set_status(status);
self
}
/// Set a header and move the Response.
///
/// Useful for the "builder-style" pattern.
#[inline]
pub fn with_header<H: header::Header>(mut self, header: H) -> Self {
self.head.headers.set(header);
self
}
/// Set the headers and move the Response.
///
/// Useful for the "builder-style" pattern.
#[inline]
pub fn with_headers(mut self, headers: header::Headers) -> Self {
self.head.headers = headers;
self
}
/// Set the body and move the Response.
///
/// Useful for the "builder-style" pattern.
#[inline]
pub fn with_body<T: Into<B>>(mut self, body: T) -> Self {
self.set_body(body);
self
}
}
impl<B> Default for Response<B> {
fn default() -> Response<B> {
Response {
head: Default::default(),
body: None,
}
}
}
impl<B> fmt::Debug for Response<B> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Response")
.field("status", &self.head.subject)
.field("version", &self.head.version)
.field("headers", &self.head.headers)
.finish()
}
}
pub fn split<B>(res: Response<B>) -> (http::MessageHead<StatusCode>, Option<B>) {
(res.head, res.body)
}