mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-28 01:52:57 +01:00
optimize encode headers and body split
This commit is contained in:
parent
68d2203dd6
commit
ec09d6fbe6
@ -153,7 +153,7 @@ impl MessageBody for Body {
|
|||||||
if len == 0 {
|
if len == 0 {
|
||||||
Ok(Async::Ready(None))
|
Ok(Async::Ready(None))
|
||||||
} else {
|
} else {
|
||||||
Ok(Async::Ready(Some(bin.split_to(len))))
|
Ok(Async::Ready(Some(mem::replace(bin, Bytes::new()))))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Body::Message(ref mut body) => body.poll_next(),
|
Body::Message(ref mut body) => body.poll_next(),
|
||||||
|
@ -9,8 +9,7 @@ use bytes::{BufMut, Bytes, BytesMut};
|
|||||||
|
|
||||||
use crate::body::BodySize;
|
use crate::body::BodySize;
|
||||||
use crate::config::ServiceConfig;
|
use crate::config::ServiceConfig;
|
||||||
use crate::header::map;
|
use crate::header::{map, ContentEncoding};
|
||||||
use crate::header::ContentEncoding;
|
|
||||||
use crate::helpers;
|
use crate::helpers;
|
||||||
use crate::http::header::{
|
use crate::http::header::{
|
||||||
HeaderValue, ACCEPT_ENCODING, CONNECTION, CONTENT_LENGTH, DATE, TRANSFER_ENCODING,
|
HeaderValue, ACCEPT_ENCODING, CONNECTION, CONTENT_LENGTH, DATE, TRANSFER_ENCODING,
|
||||||
@ -75,32 +74,31 @@ pub(crate) trait MessageType: Sized {
|
|||||||
match length {
|
match length {
|
||||||
BodySize::Stream => {
|
BodySize::Stream => {
|
||||||
if chunked {
|
if chunked {
|
||||||
dst.extend_from_slice(b"\r\ntransfer-encoding: chunked\r\n")
|
dst.put_slice(b"\r\ntransfer-encoding: chunked\r\n")
|
||||||
} else {
|
} else {
|
||||||
skip_len = false;
|
skip_len = false;
|
||||||
dst.extend_from_slice(b"\r\n");
|
dst.put_slice(b"\r\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
BodySize::Empty => {
|
BodySize::Empty => {
|
||||||
dst.extend_from_slice(b"\r\ncontent-length: 0\r\n");
|
dst.put_slice(b"\r\ncontent-length: 0\r\n");
|
||||||
}
|
}
|
||||||
BodySize::Sized(len) => helpers::write_content_length(len, dst),
|
BodySize::Sized(len) => helpers::write_content_length(len, dst),
|
||||||
BodySize::Sized64(len) => {
|
BodySize::Sized64(len) => {
|
||||||
dst.extend_from_slice(b"\r\ncontent-length: ");
|
dst.put_slice(b"\r\ncontent-length: ");
|
||||||
write!(dst.writer(), "{}", len)?;
|
write!(dst.writer(), "{}\r\n", len)?;
|
||||||
dst.extend_from_slice(b"\r\n");
|
|
||||||
}
|
}
|
||||||
BodySize::None => dst.extend_from_slice(b"\r\n"),
|
BodySize::None => dst.put_slice(b"\r\n"),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Connection
|
// Connection
|
||||||
match ctype {
|
match ctype {
|
||||||
ConnectionType::Upgrade => dst.extend_from_slice(b"connection: upgrade\r\n"),
|
ConnectionType::Upgrade => dst.put_slice(b"connection: upgrade\r\n"),
|
||||||
ConnectionType::KeepAlive if version < Version::HTTP_11 => {
|
ConnectionType::KeepAlive if version < Version::HTTP_11 => {
|
||||||
dst.extend_from_slice(b"connection: keep-alive\r\n")
|
dst.put_slice(b"connection: keep-alive\r\n")
|
||||||
}
|
}
|
||||||
ConnectionType::Close if version >= Version::HTTP_11 => {
|
ConnectionType::Close if version >= Version::HTTP_11 => {
|
||||||
dst.extend_from_slice(b"connection: close\r\n")
|
dst.put_slice(b"connection: close\r\n")
|
||||||
}
|
}
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
@ -129,7 +127,7 @@ pub(crate) trait MessageType: Sized {
|
|||||||
dst.advance_mut(pos);
|
dst.advance_mut(pos);
|
||||||
}
|
}
|
||||||
pos = 0;
|
pos = 0;
|
||||||
dst.reserve(len);
|
dst.reserve(len * 2);
|
||||||
remaining = dst.remaining_mut();
|
remaining = dst.remaining_mut();
|
||||||
unsafe {
|
unsafe {
|
||||||
buf = &mut *(dst.bytes_mut() as *mut _);
|
buf = &mut *(dst.bytes_mut() as *mut _);
|
||||||
@ -154,7 +152,7 @@ pub(crate) trait MessageType: Sized {
|
|||||||
dst.advance_mut(pos);
|
dst.advance_mut(pos);
|
||||||
}
|
}
|
||||||
pos = 0;
|
pos = 0;
|
||||||
dst.reserve(len);
|
dst.reserve(len * 2);
|
||||||
remaining = dst.remaining_mut();
|
remaining = dst.remaining_mut();
|
||||||
unsafe {
|
unsafe {
|
||||||
buf = &mut *(dst.bytes_mut() as *mut _);
|
buf = &mut *(dst.bytes_mut() as *mut _);
|
||||||
@ -209,7 +207,7 @@ impl MessageType for Response<()> {
|
|||||||
|
|
||||||
// status line
|
// status line
|
||||||
helpers::write_status_line(head.version, head.status.as_u16(), dst);
|
helpers::write_status_line(head.version, head.status.as_u16(), dst);
|
||||||
dst.extend_from_slice(reason);
|
dst.put_slice(reason);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -228,6 +226,7 @@ impl MessageType for RequestHead {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn encode_status(&mut self, dst: &mut BytesMut) -> io::Result<()> {
|
fn encode_status(&mut self, dst: &mut BytesMut) -> io::Result<()> {
|
||||||
|
dst.reserve(256 + self.headers.len() * AVERAGE_HEADER_SIZE);
|
||||||
write!(
|
write!(
|
||||||
Writer(dst),
|
Writer(dst),
|
||||||
"{} {} {}",
|
"{} {} {}",
|
||||||
|
Loading…
Reference in New Issue
Block a user