mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-24 16:32:59 +01:00
replace message flags with ConnectionType
This commit is contained in:
parent
adad203314
commit
7d3adaa6a8
@ -16,7 +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 message::{Head, RequestHead};
|
use message::{ConnectionType, Head, RequestHead};
|
||||||
|
|
||||||
use super::response::ClientResponse;
|
use super::response::ClientResponse;
|
||||||
use super::{pipeline, Connect, Connection, ConnectorError, SendRequestError};
|
use super::{pipeline, Connect, Connection, ConnectorError, SendRequestError};
|
||||||
@ -367,7 +367,7 @@ impl ClientRequestBuilder {
|
|||||||
{
|
{
|
||||||
{
|
{
|
||||||
if let Some(parts) = parts(&mut self.head, &self.err) {
|
if let Some(parts) = parts(&mut self.head, &self.err) {
|
||||||
parts.set_upgrade();
|
parts.set_connection_type(ConnectionType::Upgrade);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.set_header(header::UPGRADE, value)
|
self.set_header(header::UPGRADE, value)
|
||||||
@ -377,7 +377,7 @@ impl ClientRequestBuilder {
|
|||||||
#[inline]
|
#[inline]
|
||||||
pub fn close(&mut self) -> &mut Self {
|
pub fn close(&mut self) -> &mut Self {
|
||||||
if let Some(parts) = parts(&mut self.head, &self.err) {
|
if let Some(parts) = parts(&mut self.head, &self.err) {
|
||||||
parts.force_close();
|
parts.set_connection_type(ConnectionType::Close);
|
||||||
}
|
}
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@ use client::ClientResponse;
|
|||||||
use error::ParseError;
|
use error::ParseError;
|
||||||
use http::header::{HeaderName, HeaderValue};
|
use http::header::{HeaderName, HeaderValue};
|
||||||
use http::{header, HeaderMap, HttpTryFrom, Method, StatusCode, Uri, Version};
|
use http::{header, HeaderMap, HttpTryFrom, Method, StatusCode, Uri, Version};
|
||||||
use message::Head;
|
use message::{ConnectionType, Head};
|
||||||
use request::Request;
|
use request::Request;
|
||||||
|
|
||||||
const MAX_BUFFER_SIZE: usize = 131_072;
|
const MAX_BUFFER_SIZE: usize = 131_072;
|
||||||
@ -164,11 +164,15 @@ pub(crate) trait MessageTypeDecoder: Sized {
|
|||||||
|
|
||||||
impl MessageTypeDecoder for Request {
|
impl MessageTypeDecoder for Request {
|
||||||
fn keep_alive(&mut self) {
|
fn keep_alive(&mut self) {
|
||||||
self.inner_mut().head.set_keep_alive()
|
self.inner_mut()
|
||||||
|
.head
|
||||||
|
.set_connection_type(ConnectionType::KeepAlive)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn force_close(&mut self) {
|
fn force_close(&mut self) {
|
||||||
self.inner_mut().head.force_close()
|
self.inner_mut()
|
||||||
|
.head
|
||||||
|
.set_connection_type(ConnectionType::Close)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn headers_mut(&mut self) -> &mut HeaderMap {
|
fn headers_mut(&mut self) -> &mut HeaderMap {
|
||||||
@ -242,11 +246,11 @@ impl MessageTypeDecoder for Request {
|
|||||||
|
|
||||||
impl MessageTypeDecoder for ClientResponse {
|
impl MessageTypeDecoder for ClientResponse {
|
||||||
fn keep_alive(&mut self) {
|
fn keep_alive(&mut self) {
|
||||||
self.head.set_keep_alive();
|
self.head.set_connection_type(ConnectionType::KeepAlive);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn force_close(&mut self) {
|
fn force_close(&mut self) {
|
||||||
self.head.force_close();
|
self.head.set_connection_type(ConnectionType::Close);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn headers_mut(&mut self) -> &mut HeaderMap {
|
fn headers_mut(&mut self) -> &mut HeaderMap {
|
||||||
|
@ -175,4 +175,5 @@ pub mod http {
|
|||||||
pub use header::*;
|
pub use header::*;
|
||||||
}
|
}
|
||||||
pub use header::ContentEncoding;
|
pub use header::ContentEncoding;
|
||||||
|
pub use message::ConnectionType;
|
||||||
}
|
}
|
||||||
|
115
src/message.rs
115
src/message.rs
@ -1,4 +1,4 @@
|
|||||||
use std::cell::{Cell, RefCell};
|
use std::cell::RefCell;
|
||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
@ -8,46 +8,36 @@ use extensions::Extensions;
|
|||||||
use payload::Payload;
|
use payload::Payload;
|
||||||
use uri::Url;
|
use uri::Url;
|
||||||
|
|
||||||
|
/// Represents various types of connection
|
||||||
|
#[derive(Copy, Clone, PartialEq, Debug)]
|
||||||
|
pub enum ConnectionType {
|
||||||
|
/// Close connection after response
|
||||||
|
Close,
|
||||||
|
/// Keep connection alive after response
|
||||||
|
KeepAlive,
|
||||||
|
/// Connection is upgraded to different type
|
||||||
|
Upgrade,
|
||||||
|
}
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub trait Head: Default + 'static {
|
pub trait Head: Default + 'static {
|
||||||
fn clear(&mut self);
|
fn clear(&mut self);
|
||||||
|
|
||||||
fn flags(&self) -> MessageFlags;
|
/// Connection type
|
||||||
|
fn connection_type(&self) -> ConnectionType;
|
||||||
|
|
||||||
fn flags_mut(&mut self) -> &mut MessageFlags;
|
/// Set connection type of the message
|
||||||
|
fn set_connection_type(&mut self, ctype: ConnectionType);
|
||||||
|
|
||||||
|
fn upgrade(&self) -> bool {
|
||||||
|
self.connection_type() == ConnectionType::Upgrade
|
||||||
|
}
|
||||||
|
|
||||||
|
fn keep_alive(&self) -> bool {
|
||||||
|
self.connection_type() == ConnectionType::KeepAlive
|
||||||
|
}
|
||||||
|
|
||||||
fn pool() -> &'static MessagePool<Self>;
|
fn pool() -> &'static MessagePool<Self>;
|
||||||
|
|
||||||
/// Set upgrade
|
|
||||||
fn set_upgrade(&mut self) {
|
|
||||||
*self.flags_mut() = MessageFlags::UPGRADE;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Check if request is upgrade request
|
|
||||||
fn upgrade(&self) -> bool {
|
|
||||||
self.flags().contains(MessageFlags::UPGRADE)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Set keep-alive
|
|
||||||
fn set_keep_alive(&mut self) {
|
|
||||||
*self.flags_mut() = MessageFlags::KEEP_ALIVE;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Check if request is keep-alive
|
|
||||||
fn keep_alive(&self) -> bool;
|
|
||||||
|
|
||||||
/// Set force-close connection
|
|
||||||
fn force_close(&mut self) {
|
|
||||||
*self.flags_mut() = MessageFlags::FORCE_CLOSE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bitflags! {
|
|
||||||
pub struct MessageFlags: u8 {
|
|
||||||
const KEEP_ALIVE = 0b0000_0001;
|
|
||||||
const FORCE_CLOSE = 0b0000_0010;
|
|
||||||
const UPGRADE = 0b0000_0100;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct RequestHead {
|
pub struct RequestHead {
|
||||||
@ -55,7 +45,7 @@ pub struct RequestHead {
|
|||||||
pub method: Method,
|
pub method: Method,
|
||||||
pub version: Version,
|
pub version: Version,
|
||||||
pub headers: HeaderMap,
|
pub headers: HeaderMap,
|
||||||
pub(crate) flags: MessageFlags,
|
ctype: Option<ConnectionType>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for RequestHead {
|
impl Default for RequestHead {
|
||||||
@ -65,33 +55,28 @@ impl Default for RequestHead {
|
|||||||
method: Method::default(),
|
method: Method::default(),
|
||||||
version: Version::HTTP_11,
|
version: Version::HTTP_11,
|
||||||
headers: HeaderMap::with_capacity(16),
|
headers: HeaderMap::with_capacity(16),
|
||||||
flags: MessageFlags::empty(),
|
ctype: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Head for RequestHead {
|
impl Head for RequestHead {
|
||||||
fn clear(&mut self) {
|
fn clear(&mut self) {
|
||||||
|
self.ctype = None;
|
||||||
self.headers.clear();
|
self.headers.clear();
|
||||||
self.flags = MessageFlags::empty();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn flags(&self) -> MessageFlags {
|
fn set_connection_type(&mut self, ctype: ConnectionType) {
|
||||||
self.flags
|
self.ctype = Some(ctype)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn flags_mut(&mut self) -> &mut MessageFlags {
|
fn connection_type(&self) -> ConnectionType {
|
||||||
&mut self.flags
|
if let Some(ct) = self.ctype {
|
||||||
}
|
ct
|
||||||
|
} else if self.version <= Version::HTTP_11 {
|
||||||
/// Check if request is keep-alive
|
ConnectionType::Close
|
||||||
fn keep_alive(&self) -> bool {
|
|
||||||
if self.flags().contains(MessageFlags::FORCE_CLOSE) {
|
|
||||||
false
|
|
||||||
} else if self.flags().contains(MessageFlags::KEEP_ALIVE) {
|
|
||||||
true
|
|
||||||
} else {
|
} else {
|
||||||
self.version <= Version::HTTP_11
|
ConnectionType::KeepAlive
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,7 +90,7 @@ pub struct ResponseHead {
|
|||||||
pub status: StatusCode,
|
pub status: StatusCode,
|
||||||
pub headers: HeaderMap,
|
pub headers: HeaderMap,
|
||||||
pub reason: Option<&'static str>,
|
pub reason: Option<&'static str>,
|
||||||
pub(crate) flags: MessageFlags,
|
pub(crate) ctype: Option<ConnectionType>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for ResponseHead {
|
impl Default for ResponseHead {
|
||||||
@ -115,34 +100,29 @@ impl Default for ResponseHead {
|
|||||||
status: StatusCode::OK,
|
status: StatusCode::OK,
|
||||||
headers: HeaderMap::with_capacity(16),
|
headers: HeaderMap::with_capacity(16),
|
||||||
reason: None,
|
reason: None,
|
||||||
flags: MessageFlags::empty(),
|
ctype: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Head for ResponseHead {
|
impl Head for ResponseHead {
|
||||||
fn clear(&mut self) {
|
fn clear(&mut self) {
|
||||||
|
self.ctype = None;
|
||||||
self.reason = None;
|
self.reason = None;
|
||||||
self.headers.clear();
|
self.headers.clear();
|
||||||
self.flags = MessageFlags::empty();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn flags(&self) -> MessageFlags {
|
fn set_connection_type(&mut self, ctype: ConnectionType) {
|
||||||
self.flags
|
self.ctype = Some(ctype)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn flags_mut(&mut self) -> &mut MessageFlags {
|
fn connection_type(&self) -> ConnectionType {
|
||||||
&mut self.flags
|
if let Some(ct) = self.ctype {
|
||||||
}
|
ct
|
||||||
|
} else if self.version <= Version::HTTP_11 {
|
||||||
/// Check if response is keep-alive
|
ConnectionType::Close
|
||||||
fn keep_alive(&self) -> bool {
|
|
||||||
if self.flags().contains(MessageFlags::FORCE_CLOSE) {
|
|
||||||
false
|
|
||||||
} else if self.flags().contains(MessageFlags::KEEP_ALIVE) {
|
|
||||||
true
|
|
||||||
} else {
|
} else {
|
||||||
self.version <= Version::HTTP_11
|
ConnectionType::KeepAlive
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -172,7 +152,6 @@ pub struct Message<T: Head> {
|
|||||||
pub extensions: RefCell<Extensions>,
|
pub extensions: RefCell<Extensions>,
|
||||||
pub payload: RefCell<Option<Payload>>,
|
pub payload: RefCell<Option<Payload>>,
|
||||||
pub(crate) pool: &'static MessagePool<T>,
|
pub(crate) pool: &'static MessagePool<T>,
|
||||||
pub(crate) flags: Cell<MessageFlags>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Head> Message<T> {
|
impl<T: Head> Message<T> {
|
||||||
@ -181,7 +160,6 @@ impl<T: Head> Message<T> {
|
|||||||
pub fn reset(&mut self) {
|
pub fn reset(&mut self) {
|
||||||
self.head.clear();
|
self.head.clear();
|
||||||
self.extensions.borrow_mut().clear();
|
self.extensions.borrow_mut().clear();
|
||||||
self.flags.set(MessageFlags::empty());
|
|
||||||
*self.payload.borrow_mut() = None;
|
*self.payload.borrow_mut() = None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -193,7 +171,6 @@ impl<T: Head> Default for Message<T> {
|
|||||||
url: Url::default(),
|
url: Url::default(),
|
||||||
head: T::default(),
|
head: T::default(),
|
||||||
status: StatusCode::OK,
|
status: StatusCode::OK,
|
||||||
flags: Cell::new(MessageFlags::empty()),
|
|
||||||
payload: RefCell::new(None),
|
payload: RefCell::new(None),
|
||||||
extensions: RefCell::new(Extensions::new()),
|
extensions: RefCell::new(Extensions::new()),
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ use serde_json;
|
|||||||
use body::{Body, BodyStream, MessageBody};
|
use body::{Body, BodyStream, MessageBody};
|
||||||
use error::Error;
|
use error::Error;
|
||||||
use header::{Header, IntoHeaderValue};
|
use header::{Header, IntoHeaderValue};
|
||||||
use message::{Head, MessageFlags, ResponseHead};
|
use message::{ConnectionType, Head, ResponseHead};
|
||||||
|
|
||||||
/// max write buffer size 64k
|
/// max write buffer size 64k
|
||||||
pub(crate) const MAX_WRITE_BUFFER_SIZE: usize = 65_536;
|
pub(crate) const MAX_WRITE_BUFFER_SIZE: usize = 65_536;
|
||||||
@ -360,7 +360,7 @@ impl ResponseBuilder {
|
|||||||
#[inline]
|
#[inline]
|
||||||
pub fn keep_alive(&mut self) -> &mut Self {
|
pub fn keep_alive(&mut self) -> &mut Self {
|
||||||
if let Some(parts) = parts(&mut self.response, &self.err) {
|
if let Some(parts) = parts(&mut self.response, &self.err) {
|
||||||
parts.head.set_keep_alive();
|
parts.head.set_connection_type(ConnectionType::KeepAlive);
|
||||||
}
|
}
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
@ -369,7 +369,7 @@ impl ResponseBuilder {
|
|||||||
#[inline]
|
#[inline]
|
||||||
pub fn upgrade(&mut self) -> &mut Self {
|
pub fn upgrade(&mut self) -> &mut Self {
|
||||||
if let Some(parts) = parts(&mut self.response, &self.err) {
|
if let Some(parts) = parts(&mut self.response, &self.err) {
|
||||||
parts.head.set_upgrade();
|
parts.head.set_connection_type(ConnectionType::Upgrade);
|
||||||
}
|
}
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
@ -378,7 +378,7 @@ impl ResponseBuilder {
|
|||||||
#[inline]
|
#[inline]
|
||||||
pub fn force_close(&mut self) -> &mut Self {
|
pub fn force_close(&mut self) -> &mut Self {
|
||||||
if let Some(parts) = parts(&mut self.response, &self.err) {
|
if let Some(parts) = parts(&mut self.response, &self.err) {
|
||||||
parts.head.force_close();
|
parts.head.set_connection_type(ConnectionType::Close);
|
||||||
}
|
}
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
@ -698,7 +698,7 @@ impl InnerResponse {
|
|||||||
version: Version::default(),
|
version: Version::default(),
|
||||||
headers: HeaderMap::with_capacity(16),
|
headers: HeaderMap::with_capacity(16),
|
||||||
reason: None,
|
reason: None,
|
||||||
flags: MessageFlags::empty(),
|
ctype: None,
|
||||||
},
|
},
|
||||||
pool,
|
pool,
|
||||||
response_size: 0,
|
response_size: 0,
|
||||||
|
Loading…
Reference in New Issue
Block a user