1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 08:22:59 +01:00

use RequestHead for Request

This commit is contained in:
Nikolay Kim 2018-11-15 22:34:29 -08:00
parent 6d9733cdf7
commit 3b7bc41418
10 changed files with 68 additions and 67 deletions

View File

@ -12,5 +12,5 @@ pub use self::connect::Connect;
pub use self::connection::Connection; pub use self::connection::Connection;
pub use self::connector::Connector; pub use self::connector::Connector;
pub use self::error::{ConnectorError, InvalidUrlKind, SendRequestError}; 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; pub use self::response::ClientResponse;

View File

@ -8,12 +8,12 @@ use futures::{Async, Future, Poll, Sink, Stream};
use tokio_io::{AsyncRead, AsyncWrite}; use tokio_io::{AsyncRead, AsyncWrite};
use super::error::{ConnectorError, SendRequestError}; use super::error::{ConnectorError, SendRequestError};
use super::request::RequestHead;
use super::response::ClientResponse; use super::response::ClientResponse;
use super::{Connect, Connection}; use super::{Connect, Connection};
use body::{BodyType, MessageBody, PayloadStream}; use body::{BodyType, MessageBody, PayloadStream};
use error::PayloadError; use error::PayloadError;
use h1; use h1;
use request::RequestHead;
pub(crate) fn send_request<T, I, B>( pub(crate) fn send_request<T, I, B>(
head: RequestHead, head: RequestHead,

View File

@ -16,6 +16,7 @@ use http::{
uri, Error as HttpError, HeaderMap, HeaderName, HeaderValue, HttpTryFrom, Method, uri, Error as HttpError, HeaderMap, HeaderName, HeaderValue, HttpTryFrom, Method,
Uri, Version, Uri, Version,
}; };
use request::RequestHead;
use super::response::ClientResponse; use super::response::ClientResponse;
use super::{pipeline, Connect, Connection, ConnectorError, SendRequestError}; use super::{pipeline, Connect, Connection, ConnectorError, SendRequestError};
@ -50,21 +51,9 @@ pub struct ClientRequest<B: MessageBody = ()> {
body: B, body: B,
} }
pub struct RequestHead { impl RequestHead {
pub uri: Uri, pub fn clear(&mut self) {
pub method: Method, self.headers.clear()
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),
}
} }
} }

View File

@ -4,13 +4,13 @@ use std::rc::Rc;
use bytes::Bytes; use bytes::Bytes;
use futures::{Async, Poll, Stream}; use futures::{Async, Poll, Stream};
use http::{HeaderMap, Method, StatusCode, Version}; use http::{HeaderMap, StatusCode, Version};
use body::PayloadStream; use body::PayloadStream;
use error::PayloadError; use error::PayloadError;
use extensions::Extensions; use extensions::Extensions;
use httpmessage::HttpMessage; use httpmessage::HttpMessage;
use request::{Message, MessageFlags, MessagePool}; use request::{Message, MessageFlags, MessagePool, RequestHead};
use uri::Url; use uri::Url;
use super::pipeline::Payload; use super::pipeline::Payload;
@ -25,7 +25,7 @@ impl HttpMessage for ClientResponse {
type Stream = PayloadStream; type Stream = PayloadStream;
fn headers(&self) -> &HeaderMap { fn headers(&self) -> &HeaderMap {
&self.inner.headers &self.inner.head.headers
} }
#[inline] #[inline]
@ -49,11 +49,9 @@ impl ClientResponse {
ClientResponse { ClientResponse {
inner: Rc::new(Message { inner: Rc::new(Message {
pool, pool,
method: Method::GET, head: RequestHead::default(),
status: StatusCode::OK, status: StatusCode::OK,
url: Url::default(), url: Url::default(),
version: Version::HTTP_11,
headers: HeaderMap::with_capacity(16),
flags: Cell::new(MessageFlags::empty()), flags: Cell::new(MessageFlags::empty()),
payload: RefCell::new(None), payload: RefCell::new(None),
extensions: RefCell::new(Extensions::new()), extensions: RefCell::new(Extensions::new()),
@ -75,7 +73,7 @@ impl ClientResponse {
/// Read the Request Version. /// Read the Request Version.
#[inline] #[inline]
pub fn version(&self) -> Version { pub fn version(&self) -> Version {
self.inner().version self.inner().head.version
} }
/// Get the status from the server. /// Get the status from the server.
@ -87,13 +85,13 @@ impl ClientResponse {
#[inline] #[inline]
/// Returns Request's headers. /// Returns Request's headers.
pub fn headers(&self) -> &HeaderMap { pub fn headers(&self) -> &HeaderMap {
&self.inner().headers &self.inner().head.headers
} }
#[inline] #[inline]
/// Returns mutable Request's headers. /// Returns mutable Request's headers.
pub fn headers_mut(&mut self) -> &mut HeaderMap { 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. /// Checks if a connection should be kept alive.

View File

@ -8,7 +8,7 @@ use super::decoder::{PayloadDecoder, PayloadItem, PayloadType, ResponseDecoder};
use super::encoder::{RequestEncoder, ResponseLength}; use super::encoder::{RequestEncoder, ResponseLength};
use super::{Message, MessageType}; use super::{Message, MessageType};
use body::{Binary, Body, BodyType}; use body::{Binary, Body, BodyType};
use client::{ClientResponse, RequestHead}; use client::ClientResponse;
use config::ServiceConfig; use config::ServiceConfig;
use error::{ParseError, PayloadError}; use error::{ParseError, PayloadError};
use helpers; use helpers;
@ -16,7 +16,7 @@ use http::header::{
HeaderValue, CONNECTION, CONTENT_LENGTH, DATE, TRANSFER_ENCODING, UPGRADE, HeaderValue, CONNECTION, CONTENT_LENGTH, DATE, TRANSFER_ENCODING, UPGRADE,
}; };
use http::{Method, Version}; use http::{Method, Version};
use request::MessagePool; use request::{MessagePool, RequestHead};
bitflags! { bitflags! {
struct Flags: u8 { struct Flags: u8 {
@ -187,8 +187,8 @@ impl Decoder for ClientCodec {
if let Some((req, payload)) = self.inner.decoder.decode(src)? { if let Some((req, payload)) = self.inner.decoder.decode(src)? {
self.inner self.inner
.flags .flags
.set(Flags::HEAD, req.inner.method == Method::HEAD); .set(Flags::HEAD, req.inner.head.method == Method::HEAD);
self.inner.version = req.inner.version; self.inner.version = req.inner.head.version;
if self.inner.flags.contains(Flags::KEEPALIVE_ENABLED) { if self.inner.flags.contains(Flags::KEEPALIVE_ENABLED) {
self.inner.flags.set(Flags::KEEPALIVE, req.keep_alive()); self.inner.flags.set(Flags::KEEPALIVE, req.keep_alive());
} }

View File

@ -261,8 +261,8 @@ impl Decoder for Codec {
}) })
} else if let Some((req, payload)) = self.decoder.decode(src)? { } else if let Some((req, payload)) = self.decoder.decode(src)? {
self.flags self.flags
.set(Flags::HEAD, req.inner.method == Method::HEAD); .set(Flags::HEAD, req.inner.head.method == Method::HEAD);
self.version = req.inner.version; self.version = req.inner.head.version;
if self.flags.contains(Flags::KEEPALIVE_ENABLED) { if self.flags.contains(Flags::KEEPALIVE_ENABLED) {
self.flags.set(Flags::KEEPALIVE, req.keep_alive()); self.flags.set(Flags::KEEPALIVE, req.keep_alive());
} }

View File

@ -152,15 +152,15 @@ impl Decoder for RequestDecoder {
_ => (), _ => (),
} }
inner.headers.append(name, value); inner.head.headers.append(name, value);
} else { } else {
return Err(ParseError::Header); return Err(ParseError::Header);
} }
} }
inner.url = path; inner.url = path;
inner.method = method; inner.head.method = method;
inner.version = version; inner.head.version = version;
} }
msg msg
}; };
@ -172,7 +172,7 @@ impl Decoder for RequestDecoder {
} else if let Some(len) = content_length { } else if let Some(len) = content_length {
// Content-Length // Content-Length
PayloadType::Payload(PayloadDecoder::length(len)) 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 // upgrade(websocket) or connect
PayloadType::Stream(PayloadDecoder::eof()) PayloadType::Stream(PayloadDecoder::eof())
} else if src.len() >= MAX_BUFFER_SIZE { } 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 { } else {
return Err(ParseError::Header); return Err(ParseError::Header);
} }
} }
inner.status = status; inner.status = status;
inner.version = version; inner.head.version = version;
} }
msg msg
}; };
@ -318,7 +318,7 @@ impl Decoder for ResponseDecoder {
// Content-Length // Content-Length
PayloadType::Payload(PayloadDecoder::length(len)) PayloadType::Payload(PayloadDecoder::length(len))
} else if msg.inner.status == StatusCode::SWITCHING_PROTOCOLS } else if msg.inner.status == StatusCode::SWITCHING_PROTOCOLS
|| msg.inner.method == Method::CONNECT || msg.inner.head.method == Method::CONNECT
{ {
// switching protocol or connect // switching protocol or connect
PayloadType::Stream(PayloadDecoder::eof()) PayloadType::Stream(PayloadDecoder::eof())

View File

@ -9,10 +9,9 @@ use http::header::{HeaderValue, ACCEPT_ENCODING, CONTENT_LENGTH};
use http::{StatusCode, Version}; use http::{StatusCode, Version};
use body::{Binary, Body}; use body::{Binary, Body};
use client::RequestHead;
use header::ContentEncoding; use header::ContentEncoding;
use http::Method; use http::Method;
use request::Request; use request::{Request, RequestHead};
use response::Response; use response::Response;
#[derive(Debug)] #[derive(Debug)]

View File

@ -23,12 +23,28 @@ pub struct Request {
pub(crate) inner: Rc<Message>, pub(crate) inner: Rc<Message>,
} }
pub struct Message { pub struct RequestHead {
pub version: Version, pub uri: Uri,
pub status: StatusCode,
pub method: Method, pub method: Method,
pub url: Url, pub version: Version,
pub headers: HeaderMap, 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<Extensions>, pub extensions: RefCell<Extensions>,
pub payload: RefCell<Option<Payload>>, pub payload: RefCell<Option<Payload>>,
pub(crate) pool: &'static MessagePool, pub(crate) pool: &'static MessagePool,
@ -39,7 +55,7 @@ impl Message {
#[inline] #[inline]
/// Reset request instance /// Reset request instance
pub fn reset(&mut self) { pub fn reset(&mut self) {
self.headers.clear(); self.head.clear();
self.extensions.borrow_mut().clear(); self.extensions.borrow_mut().clear();
self.flags.set(MessageFlags::empty()); self.flags.set(MessageFlags::empty());
*self.payload.borrow_mut() = None; *self.payload.borrow_mut() = None;
@ -50,7 +66,7 @@ impl HttpMessage for Request {
type Stream = Payload; type Stream = Payload;
fn headers(&self) -> &HeaderMap { fn headers(&self) -> &HeaderMap {
&self.inner.headers &self.inner.head.headers
} }
#[inline] #[inline]
@ -74,11 +90,9 @@ impl Request {
Request { Request {
inner: Rc::new(Message { inner: Rc::new(Message {
pool, pool,
method: Method::GET,
status: StatusCode::OK,
url: Url::default(), url: Url::default(),
version: Version::HTTP_11, head: RequestHead::default(),
headers: HeaderMap::with_capacity(16), status: StatusCode::OK,
flags: Cell::new(MessageFlags::empty()), flags: Cell::new(MessageFlags::empty()),
payload: RefCell::new(None), payload: RefCell::new(None),
extensions: RefCell::new(Extensions::new()), extensions: RefCell::new(Extensions::new()),
@ -98,27 +112,28 @@ impl Request {
Rc::get_mut(&mut self.inner).expect("Multiple copies exist") Rc::get_mut(&mut self.inner).expect("Multiple copies exist")
} }
#[inline] /// Request's uri.
pub fn url(&self) -> &Url {
&self.inner().url
}
/// Read the Request Uri.
#[inline] #[inline]
pub fn uri(&self) -> &Uri { 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. /// Read the Request method.
#[inline] #[inline]
pub fn method(&self) -> &Method { pub fn method(&self) -> &Method {
&self.inner().method &self.inner().head.method
} }
/// Read the Request Version. /// Read the Request Version.
#[inline] #[inline]
pub fn version(&self) -> Version { pub fn version(&self) -> Version {
self.inner().version self.inner().head.version
} }
/// The target path of this Request. /// The target path of this Request.
@ -130,13 +145,13 @@ impl Request {
#[inline] #[inline]
/// Returns Request's headers. /// Returns Request's headers.
pub fn headers(&self) -> &HeaderMap { pub fn headers(&self) -> &HeaderMap {
&self.inner().headers &self.inner().head.headers
} }
#[inline] #[inline]
/// Returns mutable Request's headers. /// Returns mutable Request's headers.
pub fn headers_mut(&mut self) -> &mut HeaderMap { 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. /// Checks if a connection should be kept alive.
@ -159,12 +174,12 @@ impl Request {
/// Check if request requires connection upgrade /// Check if request requires connection upgrade
pub fn upgrade(&self) -> bool { 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() { if let Ok(s) = conn.to_str() {
return s.to_lowercase().contains("upgrade"); return s.to_lowercase().contains("upgrade");
} }
} }
self.inner().method == Method::CONNECT self.inner().head.method == Method::CONNECT
} }
#[doc(hidden)] #[doc(hidden)]

View File

@ -391,10 +391,10 @@ impl TestRequest {
let mut req = Request::new(); let mut req = Request::new();
{ {
let inner = req.inner_mut(); let inner = req.inner_mut();
inner.method = method; inner.head.method = method;
inner.url = InnerUrl::new(uri); inner.url = InnerUrl::new(uri);
inner.version = version; inner.head.version = version;
inner.headers = headers; inner.head.headers = headers;
*inner.payload.borrow_mut() = payload; *inner.payload.borrow_mut() = payload;
} }
// req.set_cookies(cookies); // req.set_cookies(cookies);