diff --git a/src/client/mod.rs b/src/client/mod.rs index da0cbc670..76c3f8b88 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -12,5 +12,5 @@ pub use self::connect::Connect; pub use self::connection::Connection; pub use self::connector::Connector; pub use self::error::{ConnectorError, InvalidUrlKind, SendRequestError}; -pub use self::request::{ClientRequest, ClientRequestBuilder, RequestHead}; +pub use self::request::{ClientRequest, ClientRequestBuilder}; pub use self::response::ClientResponse; diff --git a/src/client/pipeline.rs b/src/client/pipeline.rs index dc6a644dc..26bd1c62a 100644 --- a/src/client/pipeline.rs +++ b/src/client/pipeline.rs @@ -8,12 +8,12 @@ use futures::{Async, Future, Poll, Sink, Stream}; use tokio_io::{AsyncRead, AsyncWrite}; use super::error::{ConnectorError, SendRequestError}; -use super::request::RequestHead; use super::response::ClientResponse; use super::{Connect, Connection}; use body::{BodyType, MessageBody, PayloadStream}; use error::PayloadError; use h1; +use request::RequestHead; pub(crate) fn send_request( head: RequestHead, diff --git a/src/client/request.rs b/src/client/request.rs index 602abed59..c4c7f2f6a 100644 --- a/src/client/request.rs +++ b/src/client/request.rs @@ -16,6 +16,7 @@ use http::{ uri, Error as HttpError, HeaderMap, HeaderName, HeaderValue, HttpTryFrom, Method, Uri, Version, }; +use request::RequestHead; use super::response::ClientResponse; use super::{pipeline, Connect, Connection, ConnectorError, SendRequestError}; @@ -50,21 +51,9 @@ pub struct ClientRequest { body: B, } -pub struct RequestHead { - pub uri: Uri, - pub method: Method, - pub version: Version, - pub headers: HeaderMap, -} - -impl Default for RequestHead { - fn default() -> RequestHead { - RequestHead { - uri: Uri::default(), - method: Method::default(), - version: Version::HTTP_11, - headers: HeaderMap::with_capacity(16), - } +impl RequestHead { + pub fn clear(&mut self) { + self.headers.clear() } } diff --git a/src/client/response.rs b/src/client/response.rs index e8e63e4f7..56e13fa47 100644 --- a/src/client/response.rs +++ b/src/client/response.rs @@ -4,13 +4,13 @@ use std::rc::Rc; use bytes::Bytes; use futures::{Async, Poll, Stream}; -use http::{HeaderMap, Method, StatusCode, Version}; +use http::{HeaderMap, StatusCode, Version}; use body::PayloadStream; use error::PayloadError; use extensions::Extensions; use httpmessage::HttpMessage; -use request::{Message, MessageFlags, MessagePool}; +use request::{Message, MessageFlags, MessagePool, RequestHead}; use uri::Url; use super::pipeline::Payload; @@ -25,7 +25,7 @@ impl HttpMessage for ClientResponse { type Stream = PayloadStream; fn headers(&self) -> &HeaderMap { - &self.inner.headers + &self.inner.head.headers } #[inline] @@ -49,11 +49,9 @@ impl ClientResponse { ClientResponse { inner: Rc::new(Message { pool, - method: Method::GET, + head: RequestHead::default(), status: StatusCode::OK, url: Url::default(), - version: Version::HTTP_11, - headers: HeaderMap::with_capacity(16), flags: Cell::new(MessageFlags::empty()), payload: RefCell::new(None), extensions: RefCell::new(Extensions::new()), @@ -75,7 +73,7 @@ impl ClientResponse { /// Read the Request Version. #[inline] pub fn version(&self) -> Version { - self.inner().version + self.inner().head.version } /// Get the status from the server. @@ -87,13 +85,13 @@ impl ClientResponse { #[inline] /// Returns Request's headers. pub fn headers(&self) -> &HeaderMap { - &self.inner().headers + &self.inner().head.headers } #[inline] /// Returns mutable Request's headers. pub fn headers_mut(&mut self) -> &mut HeaderMap { - &mut self.inner_mut().headers + &mut self.inner_mut().head.headers } /// Checks if a connection should be kept alive. diff --git a/src/h1/client.rs b/src/h1/client.rs index d2ac20348..81a6f5689 100644 --- a/src/h1/client.rs +++ b/src/h1/client.rs @@ -8,7 +8,7 @@ use super::decoder::{PayloadDecoder, PayloadItem, PayloadType, ResponseDecoder}; use super::encoder::{RequestEncoder, ResponseLength}; use super::{Message, MessageType}; use body::{Binary, Body, BodyType}; -use client::{ClientResponse, RequestHead}; +use client::ClientResponse; use config::ServiceConfig; use error::{ParseError, PayloadError}; use helpers; @@ -16,7 +16,7 @@ use http::header::{ HeaderValue, CONNECTION, CONTENT_LENGTH, DATE, TRANSFER_ENCODING, UPGRADE, }; use http::{Method, Version}; -use request::MessagePool; +use request::{MessagePool, RequestHead}; bitflags! { struct Flags: u8 { @@ -187,8 +187,8 @@ impl Decoder for ClientCodec { if let Some((req, payload)) = self.inner.decoder.decode(src)? { self.inner .flags - .set(Flags::HEAD, req.inner.method == Method::HEAD); - self.inner.version = req.inner.version; + .set(Flags::HEAD, req.inner.head.method == Method::HEAD); + self.inner.version = req.inner.head.version; if self.inner.flags.contains(Flags::KEEPALIVE_ENABLED) { self.inner.flags.set(Flags::KEEPALIVE, req.keep_alive()); } diff --git a/src/h1/codec.rs b/src/h1/codec.rs index 44a1b81fc..57e848982 100644 --- a/src/h1/codec.rs +++ b/src/h1/codec.rs @@ -261,8 +261,8 @@ impl Decoder for Codec { }) } else if let Some((req, payload)) = self.decoder.decode(src)? { self.flags - .set(Flags::HEAD, req.inner.method == Method::HEAD); - self.version = req.inner.version; + .set(Flags::HEAD, req.inner.head.method == Method::HEAD); + self.version = req.inner.head.version; if self.flags.contains(Flags::KEEPALIVE_ENABLED) { self.flags.set(Flags::KEEPALIVE, req.keep_alive()); } diff --git a/src/h1/decoder.rs b/src/h1/decoder.rs index f2a3ee3f7..75bd0f7c1 100644 --- a/src/h1/decoder.rs +++ b/src/h1/decoder.rs @@ -152,15 +152,15 @@ impl Decoder for RequestDecoder { _ => (), } - inner.headers.append(name, value); + inner.head.headers.append(name, value); } else { return Err(ParseError::Header); } } inner.url = path; - inner.method = method; - inner.version = version; + inner.head.method = method; + inner.head.version = version; } msg }; @@ -172,7 +172,7 @@ impl Decoder for RequestDecoder { } else if let Some(len) = content_length { // Content-Length PayloadType::Payload(PayloadDecoder::length(len)) - } else if has_upgrade || msg.inner.method == Method::CONNECT { + } else if has_upgrade || msg.inner.head.method == Method::CONNECT { // upgrade(websocket) or connect PayloadType::Stream(PayloadDecoder::eof()) } else if src.len() >= MAX_BUFFER_SIZE { @@ -298,14 +298,14 @@ impl Decoder for ResponseDecoder { _ => (), } - inner.headers.append(name, value); + inner.head.headers.append(name, value); } else { return Err(ParseError::Header); } } inner.status = status; - inner.version = version; + inner.head.version = version; } msg }; @@ -318,7 +318,7 @@ impl Decoder for ResponseDecoder { // Content-Length PayloadType::Payload(PayloadDecoder::length(len)) } else if msg.inner.status == StatusCode::SWITCHING_PROTOCOLS - || msg.inner.method == Method::CONNECT + || msg.inner.head.method == Method::CONNECT { // switching protocol or connect PayloadType::Stream(PayloadDecoder::eof()) diff --git a/src/h1/encoder.rs b/src/h1/encoder.rs index caad113d9..7527eeea3 100644 --- a/src/h1/encoder.rs +++ b/src/h1/encoder.rs @@ -9,10 +9,9 @@ use http::header::{HeaderValue, ACCEPT_ENCODING, CONTENT_LENGTH}; use http::{StatusCode, Version}; use body::{Binary, Body}; -use client::RequestHead; use header::ContentEncoding; use http::Method; -use request::Request; +use request::{Request, RequestHead}; use response::Response; #[derive(Debug)] diff --git a/src/request.rs b/src/request.rs index fb5cb183b..edad4d508 100644 --- a/src/request.rs +++ b/src/request.rs @@ -23,12 +23,28 @@ pub struct Request { pub(crate) inner: Rc, } -pub struct Message { - pub version: Version, - pub status: StatusCode, +pub struct RequestHead { + pub uri: Uri, pub method: Method, - pub url: Url, + pub version: Version, pub headers: HeaderMap, +} + +impl Default for RequestHead { + fn default() -> RequestHead { + RequestHead { + uri: Uri::default(), + method: Method::default(), + version: Version::HTTP_11, + headers: HeaderMap::with_capacity(16), + } + } +} + +pub struct Message { + pub head: RequestHead, + pub url: Url, + pub status: StatusCode, pub extensions: RefCell, pub payload: RefCell>, pub(crate) pool: &'static MessagePool, @@ -39,7 +55,7 @@ impl Message { #[inline] /// Reset request instance pub fn reset(&mut self) { - self.headers.clear(); + self.head.clear(); self.extensions.borrow_mut().clear(); self.flags.set(MessageFlags::empty()); *self.payload.borrow_mut() = None; @@ -50,7 +66,7 @@ impl HttpMessage for Request { type Stream = Payload; fn headers(&self) -> &HeaderMap { - &self.inner.headers + &self.inner.head.headers } #[inline] @@ -74,11 +90,9 @@ impl Request { Request { inner: Rc::new(Message { pool, - method: Method::GET, - status: StatusCode::OK, url: Url::default(), - version: Version::HTTP_11, - headers: HeaderMap::with_capacity(16), + head: RequestHead::default(), + status: StatusCode::OK, flags: Cell::new(MessageFlags::empty()), payload: RefCell::new(None), extensions: RefCell::new(Extensions::new()), @@ -98,27 +112,28 @@ impl Request { Rc::get_mut(&mut self.inner).expect("Multiple copies exist") } - #[inline] - pub fn url(&self) -> &Url { - &self.inner().url - } - - /// Read the Request Uri. + /// Request's uri. #[inline] pub fn uri(&self) -> &Uri { - self.inner().url.uri() + &self.inner().head.uri + } + + /// Mutable reference to the request's uri. + #[inline] + pub fn uri_mut(&mut self) -> &mut Uri { + &mut self.inner_mut().head.uri } /// Read the Request method. #[inline] pub fn method(&self) -> &Method { - &self.inner().method + &self.inner().head.method } /// Read the Request Version. #[inline] pub fn version(&self) -> Version { - self.inner().version + self.inner().head.version } /// The target path of this Request. @@ -130,13 +145,13 @@ impl Request { #[inline] /// Returns Request's headers. pub fn headers(&self) -> &HeaderMap { - &self.inner().headers + &self.inner().head.headers } #[inline] /// Returns mutable Request's headers. pub fn headers_mut(&mut self) -> &mut HeaderMap { - &mut self.inner_mut().headers + &mut self.inner_mut().head.headers } /// Checks if a connection should be kept alive. @@ -159,12 +174,12 @@ impl Request { /// Check if request requires connection upgrade pub fn upgrade(&self) -> bool { - if let Some(conn) = self.inner().headers.get(header::CONNECTION) { + if let Some(conn) = self.inner().head.headers.get(header::CONNECTION) { if let Ok(s) = conn.to_str() { return s.to_lowercase().contains("upgrade"); } } - self.inner().method == Method::CONNECT + self.inner().head.method == Method::CONNECT } #[doc(hidden)] diff --git a/src/test.rs b/src/test.rs index 71145cee8..b0b8dc83d 100644 --- a/src/test.rs +++ b/src/test.rs @@ -391,10 +391,10 @@ impl TestRequest { let mut req = Request::new(); { let inner = req.inner_mut(); - inner.method = method; + inner.head.method = method; inner.url = InnerUrl::new(uri); - inner.version = version; - inner.headers = headers; + inner.head.version = version; + inner.head.headers = headers; *inner.payload.borrow_mut() = payload; } // req.set_cookies(cookies);