1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-24 16:02: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::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;

View File

@ -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<T, I, B>(
head: RequestHead,

View File

@ -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<B: MessageBody = ()> {
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()
}
}

View File

@ -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.

View File

@ -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());
}

View File

@ -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());
}

View File

@ -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())

View File

@ -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)]

View File

@ -23,12 +23,28 @@ pub struct Request {
pub(crate) inner: Rc<Message>,
}
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<Extensions>,
pub payload: RefCell<Option<Payload>>,
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)]

View File

@ -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);