1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-06-25 22:49:21 +02:00

refactor RequestHead/ResponseHead

This commit is contained in:
Nikolay Kim
2019-03-27 10:38:01 -07:00
parent fb9c94c3e0
commit 3edc515bac
13 changed files with 191 additions and 132 deletions

View File

@ -129,7 +129,7 @@ impl Decoder for ClientCodec {
debug_assert!(!self.inner.payload.is_some(), "Payload decoder is set");
if let Some((req, payload)) = self.inner.decoder.decode(src)? {
if let Some(ctype) = req.ctype {
if let Some(ctype) = req.ctype() {
// do not use peer's keep-alive
self.inner.ctype = if ctype == ConnectionType::KeepAlive {
self.inner.ctype

View File

@ -154,7 +154,7 @@ impl Encoder for Codec {
res.head_mut().version = self.version;
// connection status
self.ctype = if let Some(ct) = res.head().ctype {
self.ctype = if let Some(ct) = res.head().ctype() {
if ct == ConnectionType::KeepAlive {
self.ctype
} else {

View File

@ -158,7 +158,9 @@ pub(crate) trait MessageType: Sized {
impl MessageType for Request {
fn set_connection_type(&mut self, ctype: Option<ConnectionType>) {
self.head_mut().ctype = ctype;
if let Some(ctype) = ctype {
self.head_mut().set_connection_type(ctype);
}
}
fn headers_mut(&mut self) -> &mut HeaderMap {
@ -228,7 +230,9 @@ impl MessageType for Request {
impl MessageType for ResponseHead {
fn set_connection_type(&mut self, ctype: Option<ConnectionType>) {
self.ctype = ctype;
if let Some(ctype) = ctype {
ResponseHead::set_connection_type(self, ctype);
}
}
fn headers_mut(&mut self) -> &mut HeaderMap {
@ -814,7 +818,7 @@ mod tests {
);
let req = parse_ready!(&mut buf);
assert_eq!(req.head().ctype, Some(ConnectionType::Close));
assert_eq!(req.head().connection_type(), ConnectionType::Close);
let mut buf = BytesMut::from(
"GET /test HTTP/1.1\r\n\
@ -822,7 +826,7 @@ mod tests {
);
let req = parse_ready!(&mut buf);
assert_eq!(req.head().ctype, Some(ConnectionType::Close));
assert_eq!(req.head().connection_type(), ConnectionType::Close);
}
#[test]
@ -834,7 +838,7 @@ mod tests {
let req = parse_ready!(&mut buf);
assert_eq!(req.head().ctype, Some(ConnectionType::Close));
assert_eq!(req.head().connection_type(), ConnectionType::Close);
}
#[test]
@ -845,7 +849,7 @@ mod tests {
);
let req = parse_ready!(&mut buf);
assert_eq!(req.head().ctype, Some(ConnectionType::KeepAlive));
assert_eq!(req.head().connection_type(), ConnectionType::KeepAlive);
let mut buf = BytesMut::from(
"GET /test HTTP/1.0\r\n\
@ -853,7 +857,7 @@ mod tests {
);
let req = parse_ready!(&mut buf);
assert_eq!(req.head().ctype, Some(ConnectionType::KeepAlive));
assert_eq!(req.head().connection_type(), ConnectionType::KeepAlive);
}
#[test]
@ -864,7 +868,7 @@ mod tests {
);
let req = parse_ready!(&mut buf);
assert_eq!(req.head().ctype, Some(ConnectionType::KeepAlive));
assert_eq!(req.head().connection_type(), ConnectionType::KeepAlive);
}
#[test]
@ -886,7 +890,6 @@ mod tests {
);
let req = parse_ready!(&mut buf);
assert_eq!(req.head().ctype, None);
assert_eq!(req.head().connection_type(), ConnectionType::KeepAlive);
}
@ -900,7 +903,7 @@ mod tests {
let req = parse_ready!(&mut buf);
assert!(req.upgrade());
assert_eq!(req.head().ctype, Some(ConnectionType::Upgrade));
assert_eq!(req.head().connection_type(), ConnectionType::Upgrade);
let mut buf = BytesMut::from(
"GET /test HTTP/1.1\r\n\
@ -910,7 +913,7 @@ mod tests {
let req = parse_ready!(&mut buf);
assert!(req.upgrade());
assert_eq!(req.head().ctype, Some(ConnectionType::Upgrade));
assert_eq!(req.head().connection_type(), ConnectionType::Upgrade);
}
#[test]
@ -1008,7 +1011,7 @@ mod tests {
);
let mut reader = MessageDecoder::<Request>::default();
let (req, pl) = reader.decode(&mut buf).unwrap().unwrap();
assert_eq!(req.head().ctype, Some(ConnectionType::Upgrade));
assert_eq!(req.head().connection_type(), ConnectionType::Upgrade);
assert!(req.upgrade());
assert!(pl.is_unhandled());
}

View File

@ -15,7 +15,7 @@ use crate::body::BodySize;
use crate::config::ServiceConfig;
use crate::header::ContentEncoding;
use crate::helpers;
use crate::message::{ConnectionType, RequestHead, ResponseHead};
use crate::message::{ConnectionType, Head, RequestHead, ResponseHead};
use crate::request::Request;
use crate::response::Response;
@ -41,7 +41,7 @@ impl<T: MessageType> Default for MessageEncoder<T> {
pub(crate) trait MessageType: Sized {
fn status(&self) -> Option<StatusCode>;
fn connection_type(&self) -> Option<ConnectionType>;
// fn connection_type(&self) -> Option<ConnectionType>;
fn headers(&self) -> &HeaderMap;
@ -168,12 +168,12 @@ impl MessageType for Response<()> {
}
fn chunked(&self) -> bool {
!self.head().no_chunking
self.head().chunked()
}
fn connection_type(&self) -> Option<ConnectionType> {
self.head().ctype
}
//fn connection_type(&self) -> Option<ConnectionType> {
// self.head().ctype
//}
fn headers(&self) -> &HeaderMap {
&self.head().headers
@ -196,12 +196,8 @@ impl MessageType for RequestHead {
None
}
fn connection_type(&self) -> Option<ConnectionType> {
self.ctype
}
fn chunked(&self) -> bool {
!self.no_chunking
self.chunked()
}
fn headers(&self) -> &HeaderMap {