2019-12-05 18:35:43 +01:00
|
|
|
|
use std::convert::TryFrom;
|
2019-09-16 00:52:23 +02:00
|
|
|
|
use std::io;
|
2018-11-17 04:28:07 +01:00
|
|
|
|
use std::marker::PhantomData;
|
2019-09-16 00:52:23 +02:00
|
|
|
|
use std::mem::MaybeUninit;
|
2019-11-26 06:25:50 +01:00
|
|
|
|
use std::task::Poll;
|
2018-04-29 07:20:32 +02:00
|
|
|
|
|
2018-12-11 03:08:33 +01:00
|
|
|
|
use actix_codec::Decoder;
|
2018-04-29 07:20:32 +02:00
|
|
|
|
use bytes::{Bytes, BytesMut};
|
2018-12-06 23:32:52 +01:00
|
|
|
|
use http::header::{HeaderName, HeaderValue};
|
2019-12-05 18:35:43 +01:00
|
|
|
|
use http::{header, Method, StatusCode, Uri, Version};
|
2018-04-29 07:20:32 +02:00
|
|
|
|
use httparse;
|
2018-12-06 23:32:52 +01:00
|
|
|
|
use log::{debug, error, trace};
|
2018-04-29 07:20:32 +02:00
|
|
|
|
|
2018-12-06 23:32:52 +01:00
|
|
|
|
use crate::error::ParseError;
|
2019-04-07 00:02:02 +02:00
|
|
|
|
use crate::header::HeaderMap;
|
2019-03-26 19:43:22 +01:00
|
|
|
|
use crate::message::{ConnectionType, ResponseHead};
|
2018-12-06 23:32:52 +01:00
|
|
|
|
use crate::request::Request;
|
2018-04-29 07:20:32 +02:00
|
|
|
|
|
|
|
|
|
const MAX_BUFFER_SIZE: usize = 131_072;
|
|
|
|
|
const MAX_HEADERS: usize = 96;
|
|
|
|
|
|
2018-11-17 04:28:07 +01:00
|
|
|
|
/// Incoming messagd decoder
|
2018-11-19 23:57:12 +01:00
|
|
|
|
pub(crate) struct MessageDecoder<T: MessageType>(PhantomData<T>);
|
2018-04-29 07:20:32 +02:00
|
|
|
|
|
2018-11-19 23:57:12 +01:00
|
|
|
|
#[derive(Debug)]
|
2018-10-14 08:57:31 +02:00
|
|
|
|
/// Incoming request type
|
2018-11-17 04:28:07 +01:00
|
|
|
|
pub(crate) enum PayloadType {
|
2018-10-14 08:57:31 +02:00
|
|
|
|
None,
|
|
|
|
|
Payload(PayloadDecoder),
|
2018-11-14 07:53:30 +01:00
|
|
|
|
Stream(PayloadDecoder),
|
2018-10-14 08:57:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-19 23:57:12 +01:00
|
|
|
|
impl<T: MessageType> Default for MessageDecoder<T> {
|
2018-11-17 04:28:07 +01:00
|
|
|
|
fn default() -> Self {
|
|
|
|
|
MessageDecoder(PhantomData)
|
2018-10-07 05:31:22 +02:00
|
|
|
|
}
|
2018-04-29 07:20:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-19 23:57:12 +01:00
|
|
|
|
impl<T: MessageType> Decoder for MessageDecoder<T> {
|
2018-11-17 04:28:07 +01:00
|
|
|
|
type Item = (T, PayloadType);
|
|
|
|
|
type Error = ParseError;
|
|
|
|
|
|
|
|
|
|
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
|
|
|
|
|
T::decode(src)
|
2018-10-05 06:14:18 +02:00
|
|
|
|
}
|
2018-10-07 05:31:22 +02:00
|
|
|
|
}
|
2018-10-05 06:14:18 +02:00
|
|
|
|
|
2018-11-17 04:28:07 +01:00
|
|
|
|
pub(crate) enum PayloadLength {
|
2018-11-17 06:09:33 +01:00
|
|
|
|
Payload(PayloadType),
|
2018-11-17 04:28:07 +01:00
|
|
|
|
Upgrade,
|
2018-11-17 06:09:33 +01:00
|
|
|
|
None,
|
2018-11-17 04:28:07 +01:00
|
|
|
|
}
|
2018-04-29 07:20:32 +02:00
|
|
|
|
|
2018-11-19 23:57:12 +01:00
|
|
|
|
pub(crate) trait MessageType: Sized {
|
|
|
|
|
fn set_connection_type(&mut self, ctype: Option<ConnectionType>);
|
2018-11-19 02:52:56 +01:00
|
|
|
|
|
2019-04-06 01:46:44 +02:00
|
|
|
|
fn set_expect(&mut self);
|
|
|
|
|
|
2018-11-17 04:28:07 +01:00
|
|
|
|
fn headers_mut(&mut self) -> &mut HeaderMap;
|
|
|
|
|
|
|
|
|
|
fn decode(src: &mut BytesMut) -> Result<Option<(Self, PayloadType)>, ParseError>;
|
|
|
|
|
|
2018-11-17 06:09:33 +01:00
|
|
|
|
fn set_headers(
|
2018-11-17 04:28:07 +01:00
|
|
|
|
&mut self,
|
|
|
|
|
slice: &Bytes,
|
|
|
|
|
raw_headers: &[HeaderIndex],
|
|
|
|
|
) -> Result<PayloadLength, ParseError> {
|
2018-11-19 23:57:12 +01:00
|
|
|
|
let mut ka = None;
|
2018-04-29 07:20:32 +02:00
|
|
|
|
let mut has_upgrade = false;
|
2019-04-06 01:46:44 +02:00
|
|
|
|
let mut expect = false;
|
2018-04-29 07:20:32 +02:00
|
|
|
|
let mut chunked = false;
|
|
|
|
|
let mut content_length = None;
|
|
|
|
|
|
2018-11-17 04:28:07 +01:00
|
|
|
|
{
|
|
|
|
|
let headers = self.headers_mut();
|
|
|
|
|
|
|
|
|
|
for idx in raw_headers.iter() {
|
2019-04-08 08:06:21 +02:00
|
|
|
|
let name =
|
|
|
|
|
HeaderName::from_bytes(&slice[idx.name.0..idx.name.1]).unwrap();
|
|
|
|
|
|
|
|
|
|
// Unsafe: httparse check header value for valid utf-8
|
|
|
|
|
let value = unsafe {
|
2019-12-05 18:35:43 +01:00
|
|
|
|
HeaderValue::from_maybe_shared_unchecked(
|
|
|
|
|
slice.slice(idx.value.0..idx.value.1),
|
2019-04-08 08:06:21 +02:00
|
|
|
|
)
|
|
|
|
|
};
|
|
|
|
|
match name {
|
|
|
|
|
header::CONTENT_LENGTH => {
|
|
|
|
|
if let Ok(s) = value.to_str() {
|
|
|
|
|
if let Ok(len) = s.parse::<u64>() {
|
|
|
|
|
if len != 0 {
|
|
|
|
|
content_length = Some(len);
|
2018-04-29 07:20:32 +02:00
|
|
|
|
}
|
2018-11-17 04:28:07 +01:00
|
|
|
|
} else {
|
2019-04-08 08:06:21 +02:00
|
|
|
|
debug!("illegal Content-Length: {:?}", s);
|
2018-11-17 04:28:07 +01:00
|
|
|
|
return Err(ParseError::Header);
|
2018-04-29 07:20:32 +02:00
|
|
|
|
}
|
2019-04-08 08:06:21 +02:00
|
|
|
|
} else {
|
|
|
|
|
debug!("illegal Content-Length: {:?}", value);
|
|
|
|
|
return Err(ParseError::Header);
|
2018-11-17 04:28:07 +01:00
|
|
|
|
}
|
2019-04-08 08:06:21 +02:00
|
|
|
|
}
|
|
|
|
|
// transfer-encoding
|
|
|
|
|
header::TRANSFER_ENCODING => {
|
|
|
|
|
if let Ok(s) = value.to_str().map(|s| s.trim()) {
|
|
|
|
|
chunked = s.eq_ignore_ascii_case("chunked");
|
|
|
|
|
} else {
|
|
|
|
|
return Err(ParseError::Header);
|
2018-11-17 04:28:07 +01:00
|
|
|
|
}
|
2019-04-08 08:06:21 +02:00
|
|
|
|
}
|
|
|
|
|
// connection keep-alive state
|
|
|
|
|
header::CONNECTION => {
|
|
|
|
|
ka = if let Ok(conn) = value.to_str().map(|conn| conn.trim()) {
|
|
|
|
|
if conn.eq_ignore_ascii_case("keep-alive") {
|
|
|
|
|
Some(ConnectionType::KeepAlive)
|
|
|
|
|
} else if conn.eq_ignore_ascii_case("close") {
|
|
|
|
|
Some(ConnectionType::Close)
|
|
|
|
|
} else if conn.eq_ignore_ascii_case("upgrade") {
|
|
|
|
|
Some(ConnectionType::Upgrade)
|
2018-11-17 04:28:07 +01:00
|
|
|
|
} else {
|
2018-11-19 23:57:12 +01:00
|
|
|
|
None
|
2018-07-05 09:16:16 +02:00
|
|
|
|
}
|
2019-04-08 08:06:21 +02:00
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
header::UPGRADE => {
|
|
|
|
|
has_upgrade = true;
|
|
|
|
|
// check content-length, some clients (dart)
|
|
|
|
|
// sends "content-length: 0" with websocket upgrade
|
|
|
|
|
if let Ok(val) = value.to_str().map(|val| val.trim()) {
|
|
|
|
|
if val.eq_ignore_ascii_case("websocket") {
|
|
|
|
|
content_length = None;
|
2019-04-06 01:46:44 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-29 07:20:32 +02:00
|
|
|
|
}
|
2019-04-08 08:06:21 +02:00
|
|
|
|
header::EXPECT => {
|
|
|
|
|
let bytes = value.as_bytes();
|
|
|
|
|
if bytes.len() >= 4 && &bytes[0..4] == b"100-" {
|
|
|
|
|
expect = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_ => (),
|
2018-04-29 07:20:32 +02:00
|
|
|
|
}
|
2019-04-08 08:06:21 +02:00
|
|
|
|
|
|
|
|
|
headers.append(name, value);
|
2018-11-17 04:28:07 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-19 23:57:12 +01:00
|
|
|
|
self.set_connection_type(ka);
|
2019-04-06 01:46:44 +02:00
|
|
|
|
if expect {
|
|
|
|
|
self.set_expect()
|
|
|
|
|
}
|
2018-11-17 04:28:07 +01:00
|
|
|
|
|
2018-11-17 06:09:33 +01:00
|
|
|
|
// https://tools.ietf.org/html/rfc7230#section-3.3.3
|
2018-11-17 04:28:07 +01:00
|
|
|
|
if chunked {
|
2018-11-17 06:09:33 +01:00
|
|
|
|
// Chunked encoding
|
|
|
|
|
Ok(PayloadLength::Payload(PayloadType::Payload(
|
|
|
|
|
PayloadDecoder::chunked(),
|
|
|
|
|
)))
|
2018-11-17 04:28:07 +01:00
|
|
|
|
} else if let Some(len) = content_length {
|
2018-11-17 06:09:33 +01:00
|
|
|
|
// Content-Length
|
|
|
|
|
Ok(PayloadLength::Payload(PayloadType::Payload(
|
|
|
|
|
PayloadDecoder::length(len),
|
|
|
|
|
)))
|
2018-11-17 04:28:07 +01:00
|
|
|
|
} else if has_upgrade {
|
|
|
|
|
Ok(PayloadLength::Upgrade)
|
|
|
|
|
} else {
|
|
|
|
|
Ok(PayloadLength::None)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-19 23:57:12 +01:00
|
|
|
|
impl MessageType for Request {
|
|
|
|
|
fn set_connection_type(&mut self, ctype: Option<ConnectionType>) {
|
2019-03-27 18:38:01 +01:00
|
|
|
|
if let Some(ctype) = ctype {
|
|
|
|
|
self.head_mut().set_connection_type(ctype);
|
|
|
|
|
}
|
2018-11-17 04:28:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-06 01:46:44 +02:00
|
|
|
|
fn set_expect(&mut self) {
|
|
|
|
|
self.head_mut().set_expect();
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-17 04:28:07 +01:00
|
|
|
|
fn headers_mut(&mut self) -> &mut HeaderMap {
|
2019-03-05 22:16:26 +01:00
|
|
|
|
&mut self.head_mut().headers
|
2018-11-17 04:28:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-07 19:46:51 +01:00
|
|
|
|
#[allow(clippy::uninit_assumed_init)]
|
2018-11-17 04:28:07 +01:00
|
|
|
|
fn decode(src: &mut BytesMut) -> Result<Option<(Self, PayloadType)>, ParseError> {
|
|
|
|
|
// Unsafe: we read only this data only after httparse parses headers into.
|
|
|
|
|
// performance bump for pipeline benchmarks.
|
2019-09-16 00:52:23 +02:00
|
|
|
|
let mut headers: [HeaderIndex; MAX_HEADERS] =
|
|
|
|
|
unsafe { MaybeUninit::uninit().assume_init() };
|
2018-11-17 04:28:07 +01:00
|
|
|
|
|
2018-11-17 06:09:33 +01:00
|
|
|
|
let (len, method, uri, ver, h_len) = {
|
2019-12-07 19:46:51 +01:00
|
|
|
|
let mut parsed: [httparse::Header<'_>; MAX_HEADERS] =
|
2019-09-16 00:52:23 +02:00
|
|
|
|
unsafe { MaybeUninit::uninit().assume_init() };
|
2018-11-17 04:28:07 +01:00
|
|
|
|
|
|
|
|
|
let mut req = httparse::Request::new(&mut parsed);
|
|
|
|
|
match req.parse(src)? {
|
|
|
|
|
httparse::Status::Complete(len) => {
|
|
|
|
|
let method = Method::from_bytes(req.method.unwrap().as_bytes())
|
|
|
|
|
.map_err(|_| ParseError::Method)?;
|
|
|
|
|
let uri = Uri::try_from(req.path.unwrap())?;
|
|
|
|
|
let version = if req.version.unwrap() == 1 {
|
|
|
|
|
Version::HTTP_11
|
|
|
|
|
} else {
|
|
|
|
|
Version::HTTP_10
|
|
|
|
|
};
|
|
|
|
|
HeaderIndex::record(src, req.headers, &mut headers);
|
|
|
|
|
|
|
|
|
|
(len, method, uri, version, req.headers.len())
|
|
|
|
|
}
|
|
|
|
|
httparse::Status::Partial => return Ok(None),
|
2018-04-29 07:20:32 +02:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-11-17 04:28:07 +01:00
|
|
|
|
let mut msg = Request::new();
|
|
|
|
|
|
2018-11-17 06:09:33 +01:00
|
|
|
|
// convert headers
|
2019-04-08 08:06:21 +02:00
|
|
|
|
let length = msg.set_headers(&src.split_to(len).freeze(), &headers[..h_len])?;
|
2018-11-17 04:28:07 +01:00
|
|
|
|
|
2018-11-17 06:09:33 +01:00
|
|
|
|
// payload decoder
|
2019-04-08 08:06:21 +02:00
|
|
|
|
let decoder = match length {
|
2018-11-17 06:09:33 +01:00
|
|
|
|
PayloadLength::Payload(pl) => pl,
|
2018-11-17 04:28:07 +01:00
|
|
|
|
PayloadLength::Upgrade => {
|
2018-11-17 06:09:33 +01:00
|
|
|
|
// upgrade(websocket)
|
2018-11-17 04:28:07 +01:00
|
|
|
|
PayloadType::Stream(PayloadDecoder::eof())
|
|
|
|
|
}
|
|
|
|
|
PayloadLength::None => {
|
|
|
|
|
if method == Method::CONNECT {
|
|
|
|
|
PayloadType::Stream(PayloadDecoder::eof())
|
|
|
|
|
} else if src.len() >= MAX_BUFFER_SIZE {
|
2018-11-17 06:09:33 +01:00
|
|
|
|
trace!("MAX_BUFFER_SIZE unprocessed data reached, closing");
|
2018-11-17 04:28:07 +01:00
|
|
|
|
return Err(ParseError::TooLarge);
|
|
|
|
|
} else {
|
|
|
|
|
PayloadType::None
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-23 03:18:05 +02:00
|
|
|
|
};
|
|
|
|
|
|
2019-02-08 06:16:46 +01:00
|
|
|
|
let head = msg.head_mut();
|
|
|
|
|
head.uri = uri;
|
|
|
|
|
head.method = method;
|
|
|
|
|
head.version = ver;
|
2018-11-17 04:28:07 +01:00
|
|
|
|
|
2018-10-23 03:18:05 +02:00
|
|
|
|
Ok(Some((msg, decoder)))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-26 19:43:22 +01:00
|
|
|
|
impl MessageType for ResponseHead {
|
2018-11-19 23:57:12 +01:00
|
|
|
|
fn set_connection_type(&mut self, ctype: Option<ConnectionType>) {
|
2019-03-27 18:38:01 +01:00
|
|
|
|
if let Some(ctype) = ctype {
|
|
|
|
|
ResponseHead::set_connection_type(self, ctype);
|
|
|
|
|
}
|
2018-10-23 03:18:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-06 01:46:44 +02:00
|
|
|
|
fn set_expect(&mut self) {}
|
|
|
|
|
|
2018-11-17 04:28:07 +01:00
|
|
|
|
fn headers_mut(&mut self) -> &mut HeaderMap {
|
2019-03-26 19:43:22 +01:00
|
|
|
|
&mut self.headers
|
2018-10-23 03:18:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-07 19:46:51 +01:00
|
|
|
|
#[allow(clippy::uninit_assumed_init)]
|
2018-11-17 04:28:07 +01:00
|
|
|
|
fn decode(src: &mut BytesMut) -> Result<Option<(Self, PayloadType)>, ParseError> {
|
|
|
|
|
// Unsafe: we read only this data only after httparse parses headers into.
|
|
|
|
|
// performance bump for pipeline benchmarks.
|
2019-09-16 00:52:23 +02:00
|
|
|
|
let mut headers: [HeaderIndex; MAX_HEADERS] =
|
|
|
|
|
unsafe { MaybeUninit::uninit().assume_init() };
|
2018-10-23 03:18:05 +02:00
|
|
|
|
|
2018-11-17 06:09:33 +01:00
|
|
|
|
let (len, ver, status, h_len) = {
|
2019-12-07 19:46:51 +01:00
|
|
|
|
let mut parsed: [httparse::Header<'_>; MAX_HEADERS] =
|
2019-09-16 00:52:23 +02:00
|
|
|
|
unsafe { MaybeUninit::uninit().assume_init() };
|
2018-10-23 03:18:05 +02:00
|
|
|
|
|
2018-11-17 04:28:07 +01:00
|
|
|
|
let mut res = httparse::Response::new(&mut parsed);
|
|
|
|
|
match res.parse(src)? {
|
|
|
|
|
httparse::Status::Complete(len) => {
|
|
|
|
|
let version = if res.version.unwrap() == 1 {
|
|
|
|
|
Version::HTTP_11
|
2018-10-23 03:18:05 +02:00
|
|
|
|
} else {
|
2018-11-17 04:28:07 +01:00
|
|
|
|
Version::HTTP_10
|
|
|
|
|
};
|
|
|
|
|
let status = StatusCode::from_u16(res.code.unwrap())
|
|
|
|
|
.map_err(|_| ParseError::Status)?;
|
|
|
|
|
HeaderIndex::record(src, res.headers, &mut headers);
|
2018-10-23 03:18:05 +02:00
|
|
|
|
|
2018-11-17 04:28:07 +01:00
|
|
|
|
(len, version, status, res.headers.len())
|
|
|
|
|
}
|
|
|
|
|
httparse::Status::Partial => return Ok(None),
|
2018-10-23 03:18:05 +02:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-04-08 08:06:21 +02:00
|
|
|
|
let mut msg = ResponseHead::new(status);
|
|
|
|
|
msg.version = ver;
|
2018-11-17 04:28:07 +01:00
|
|
|
|
|
|
|
|
|
// convert headers
|
2019-04-08 08:06:21 +02:00
|
|
|
|
let length = msg.set_headers(&src.split_to(len).freeze(), &headers[..h_len])?;
|
2018-11-17 04:28:07 +01:00
|
|
|
|
|
2018-11-17 06:09:33 +01:00
|
|
|
|
// message payload
|
2019-04-08 08:06:21 +02:00
|
|
|
|
let decoder = if let PayloadLength::Payload(pl) = length {
|
2018-11-17 06:09:33 +01:00
|
|
|
|
pl
|
2019-01-29 19:14:00 +01:00
|
|
|
|
} else if status == StatusCode::SWITCHING_PROTOCOLS {
|
|
|
|
|
// switching protocol or connect
|
|
|
|
|
PayloadType::Stream(PayloadDecoder::eof())
|
|
|
|
|
} else if src.len() >= MAX_BUFFER_SIZE {
|
|
|
|
|
error!("MAX_BUFFER_SIZE unprocessed data reached, closing");
|
|
|
|
|
return Err(ParseError::TooLarge);
|
2018-11-17 06:09:33 +01:00
|
|
|
|
} else {
|
2019-04-24 20:57:40 +02:00
|
|
|
|
// for HTTP/1.0 read to eof and close connection
|
|
|
|
|
if msg.version == Version::HTTP_10 {
|
|
|
|
|
msg.set_connection_type(ConnectionType::Close);
|
|
|
|
|
PayloadType::Payload(PayloadDecoder::eof())
|
|
|
|
|
} else {
|
|
|
|
|
PayloadType::None
|
|
|
|
|
}
|
2018-04-29 07:20:32 +02:00
|
|
|
|
};
|
|
|
|
|
|
2018-10-07 05:31:22 +02:00
|
|
|
|
Ok(Some((msg, decoder)))
|
2018-04-29 07:20:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-23 08:28:55 +02:00
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
|
pub(crate) struct HeaderIndex {
|
|
|
|
|
pub(crate) name: (usize, usize),
|
|
|
|
|
pub(crate) value: (usize, usize),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl HeaderIndex {
|
|
|
|
|
pub(crate) fn record(
|
2018-10-30 00:39:46 +01:00
|
|
|
|
bytes: &[u8],
|
2019-12-07 19:46:51 +01:00
|
|
|
|
headers: &[httparse::Header<'_>],
|
2018-10-30 00:39:46 +01:00
|
|
|
|
indices: &mut [HeaderIndex],
|
2018-06-23 08:28:55 +02:00
|
|
|
|
) {
|
|
|
|
|
let bytes_ptr = bytes.as_ptr() as usize;
|
|
|
|
|
for (header, indices) in headers.iter().zip(indices.iter_mut()) {
|
|
|
|
|
let name_start = header.name.as_ptr() as usize - bytes_ptr;
|
|
|
|
|
let name_end = name_start + header.name.len();
|
|
|
|
|
indices.name = (name_start, name_end);
|
|
|
|
|
let value_start = header.value.as_ptr() as usize - bytes_ptr;
|
|
|
|
|
let value_end = value_start + header.value.len();
|
|
|
|
|
indices.value = (value_start, value_end);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-24 20:57:40 +02:00
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
2018-10-07 05:31:22 +02:00
|
|
|
|
/// Http payload item
|
|
|
|
|
pub enum PayloadItem {
|
|
|
|
|
Chunk(Bytes),
|
|
|
|
|
Eof,
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-29 07:20:32 +02:00
|
|
|
|
/// Decoders to handle different Transfer-Encodings.
|
|
|
|
|
///
|
|
|
|
|
/// If a message body does not include a Transfer-Encoding, it *should*
|
|
|
|
|
/// include a Content-Length header.
|
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
2018-10-07 05:31:22 +02:00
|
|
|
|
pub struct PayloadDecoder {
|
2018-04-29 07:20:32 +02:00
|
|
|
|
kind: Kind,
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-07 05:31:22 +02:00
|
|
|
|
impl PayloadDecoder {
|
|
|
|
|
pub fn length(x: u64) -> PayloadDecoder {
|
|
|
|
|
PayloadDecoder {
|
2018-04-29 07:20:32 +02:00
|
|
|
|
kind: Kind::Length(x),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-07 05:31:22 +02:00
|
|
|
|
pub fn chunked() -> PayloadDecoder {
|
|
|
|
|
PayloadDecoder {
|
2018-04-29 07:20:32 +02:00
|
|
|
|
kind: Kind::Chunked(ChunkedState::Size, 0),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-07 05:31:22 +02:00
|
|
|
|
pub fn eof() -> PayloadDecoder {
|
2018-10-07 18:48:53 +02:00
|
|
|
|
PayloadDecoder { kind: Kind::Eof }
|
2018-04-29 07:20:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
|
enum Kind {
|
|
|
|
|
/// A Reader used when a Content-Length header is passed with a positive
|
|
|
|
|
/// integer.
|
|
|
|
|
Length(u64),
|
|
|
|
|
/// A Reader used when Transfer-Encoding is `chunked`.
|
|
|
|
|
Chunked(ChunkedState, u64),
|
|
|
|
|
/// A Reader used for responses that don't indicate a length or chunked.
|
|
|
|
|
///
|
|
|
|
|
/// Note: This should only used for `Response`s. It is illegal for a
|
|
|
|
|
/// `Request` to be made with both `Content-Length` and
|
|
|
|
|
/// `Transfer-Encoding: chunked` missing, as explained from the spec:
|
|
|
|
|
///
|
|
|
|
|
/// > If a Transfer-Encoding header field is present in a response and
|
|
|
|
|
/// > the chunked transfer coding is not the final encoding, the
|
|
|
|
|
/// > message body length is determined by reading the connection until
|
|
|
|
|
/// > it is closed by the server. If a Transfer-Encoding header field
|
|
|
|
|
/// > is present in a request and the chunked transfer coding is not
|
|
|
|
|
/// > the final encoding, the message body length cannot be determined
|
|
|
|
|
/// > reliably; the server MUST respond with the 400 (Bad Request)
|
|
|
|
|
/// > status code and then close the connection.
|
2018-10-07 18:48:53 +02:00
|
|
|
|
Eof,
|
2018-04-29 07:20:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
|
|
|
|
enum ChunkedState {
|
|
|
|
|
Size,
|
|
|
|
|
SizeLws,
|
|
|
|
|
Extension,
|
|
|
|
|
SizeLf,
|
|
|
|
|
Body,
|
|
|
|
|
BodyCr,
|
|
|
|
|
BodyLf,
|
|
|
|
|
EndCr,
|
|
|
|
|
EndLf,
|
|
|
|
|
End,
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-07 05:31:22 +02:00
|
|
|
|
impl Decoder for PayloadDecoder {
|
|
|
|
|
type Item = PayloadItem;
|
|
|
|
|
type Error = io::Error;
|
|
|
|
|
|
|
|
|
|
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
|
2018-04-29 07:20:32 +02:00
|
|
|
|
match self.kind {
|
|
|
|
|
Kind::Length(ref mut remaining) => {
|
|
|
|
|
if *remaining == 0 {
|
2018-10-07 05:31:22 +02:00
|
|
|
|
Ok(Some(PayloadItem::Eof))
|
2018-04-29 07:20:32 +02:00
|
|
|
|
} else {
|
2018-10-07 05:31:22 +02:00
|
|
|
|
if src.is_empty() {
|
|
|
|
|
return Ok(None);
|
2018-04-29 07:20:32 +02:00
|
|
|
|
}
|
2018-10-07 05:31:22 +02:00
|
|
|
|
let len = src.len() as u64;
|
2018-04-29 07:20:32 +02:00
|
|
|
|
let buf;
|
|
|
|
|
if *remaining > len {
|
2019-12-05 18:35:43 +01:00
|
|
|
|
buf = src.split().freeze();
|
2018-04-29 07:20:32 +02:00
|
|
|
|
*remaining -= len;
|
|
|
|
|
} else {
|
2018-10-07 05:31:22 +02:00
|
|
|
|
buf = src.split_to(*remaining as usize).freeze();
|
2018-04-29 07:20:32 +02:00
|
|
|
|
*remaining = 0;
|
2018-10-07 05:31:22 +02:00
|
|
|
|
};
|
2018-04-29 07:20:32 +02:00
|
|
|
|
trace!("Length read: {}", buf.len());
|
2018-10-07 05:31:22 +02:00
|
|
|
|
Ok(Some(PayloadItem::Chunk(buf)))
|
2018-04-29 07:20:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Kind::Chunked(ref mut state, ref mut size) => {
|
|
|
|
|
loop {
|
|
|
|
|
let mut buf = None;
|
|
|
|
|
// advances the chunked state
|
2019-11-15 10:54:11 +01:00
|
|
|
|
*state = match state.step(src, size, &mut buf) {
|
|
|
|
|
Poll::Pending => return Ok(None),
|
|
|
|
|
Poll::Ready(Ok(state)) => state,
|
|
|
|
|
Poll::Ready(Err(e)) => return Err(e),
|
2018-10-07 05:31:22 +02:00
|
|
|
|
};
|
2018-04-29 07:20:32 +02:00
|
|
|
|
if *state == ChunkedState::End {
|
|
|
|
|
trace!("End of chunked stream");
|
2018-10-07 05:31:22 +02:00
|
|
|
|
return Ok(Some(PayloadItem::Eof));
|
2018-04-29 07:20:32 +02:00
|
|
|
|
}
|
|
|
|
|
if let Some(buf) = buf {
|
2018-10-07 05:31:22 +02:00
|
|
|
|
return Ok(Some(PayloadItem::Chunk(buf)));
|
2018-04-29 07:20:32 +02:00
|
|
|
|
}
|
2018-10-07 05:31:22 +02:00
|
|
|
|
if src.is_empty() {
|
|
|
|
|
return Ok(None);
|
2018-04-29 07:20:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-07 18:48:53 +02:00
|
|
|
|
Kind::Eof => {
|
|
|
|
|
if src.is_empty() {
|
2018-10-07 05:31:22 +02:00
|
|
|
|
Ok(None)
|
2018-10-07 18:48:53 +02:00
|
|
|
|
} else {
|
2019-12-05 18:35:43 +01:00
|
|
|
|
Ok(Some(PayloadItem::Chunk(src.split().freeze())))
|
2018-04-29 07:20:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
macro_rules! byte (
|
|
|
|
|
($rdr:ident) => ({
|
|
|
|
|
if $rdr.len() > 0 {
|
|
|
|
|
let b = $rdr[0];
|
2019-12-13 06:46:02 +01:00
|
|
|
|
let _ = $rdr.split_to(1);
|
2018-04-29 07:20:32 +02:00
|
|
|
|
b
|
|
|
|
|
} else {
|
2019-11-15 10:54:11 +01:00
|
|
|
|
return Poll::Pending
|
2018-04-29 07:20:32 +02:00
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
impl ChunkedState {
|
|
|
|
|
fn step(
|
2018-10-30 00:39:46 +01:00
|
|
|
|
&self,
|
|
|
|
|
body: &mut BytesMut,
|
|
|
|
|
size: &mut u64,
|
|
|
|
|
buf: &mut Option<Bytes>,
|
2019-11-15 10:54:11 +01:00
|
|
|
|
) -> Poll<Result<ChunkedState, io::Error>> {
|
2018-04-29 07:20:32 +02:00
|
|
|
|
use self::ChunkedState::*;
|
|
|
|
|
match *self {
|
|
|
|
|
Size => ChunkedState::read_size(body, size),
|
|
|
|
|
SizeLws => ChunkedState::read_size_lws(body),
|
|
|
|
|
Extension => ChunkedState::read_extension(body),
|
|
|
|
|
SizeLf => ChunkedState::read_size_lf(body, size),
|
|
|
|
|
Body => ChunkedState::read_body(body, size, buf),
|
|
|
|
|
BodyCr => ChunkedState::read_body_cr(body),
|
|
|
|
|
BodyLf => ChunkedState::read_body_lf(body),
|
|
|
|
|
EndCr => ChunkedState::read_end_cr(body),
|
|
|
|
|
EndLf => ChunkedState::read_end_lf(body),
|
2019-11-15 10:54:11 +01:00
|
|
|
|
End => Poll::Ready(Ok(ChunkedState::End)),
|
2018-04-29 07:20:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-15 10:54:11 +01:00
|
|
|
|
|
|
|
|
|
fn read_size(
|
|
|
|
|
rdr: &mut BytesMut,
|
|
|
|
|
size: &mut u64,
|
|
|
|
|
) -> Poll<Result<ChunkedState, io::Error>> {
|
2018-04-29 07:20:32 +02:00
|
|
|
|
let radix = 16;
|
|
|
|
|
match byte!(rdr) {
|
2019-07-17 11:48:37 +02:00
|
|
|
|
b @ b'0'..=b'9' => {
|
2018-04-29 07:20:32 +02:00
|
|
|
|
*size *= radix;
|
|
|
|
|
*size += u64::from(b - b'0');
|
|
|
|
|
}
|
2019-07-17 11:48:37 +02:00
|
|
|
|
b @ b'a'..=b'f' => {
|
2018-04-29 07:20:32 +02:00
|
|
|
|
*size *= radix;
|
|
|
|
|
*size += u64::from(b + 10 - b'a');
|
|
|
|
|
}
|
2019-07-17 11:48:37 +02:00
|
|
|
|
b @ b'A'..=b'F' => {
|
2018-04-29 07:20:32 +02:00
|
|
|
|
*size *= radix;
|
|
|
|
|
*size += u64::from(b + 10 - b'A');
|
|
|
|
|
}
|
2019-11-15 10:54:11 +01:00
|
|
|
|
b'\t' | b' ' => return Poll::Ready(Ok(ChunkedState::SizeLws)),
|
|
|
|
|
b';' => return Poll::Ready(Ok(ChunkedState::Extension)),
|
|
|
|
|
b'\r' => return Poll::Ready(Ok(ChunkedState::SizeLf)),
|
2018-04-29 07:20:32 +02:00
|
|
|
|
_ => {
|
2019-11-15 10:54:11 +01:00
|
|
|
|
return Poll::Ready(Err(io::Error::new(
|
2018-04-29 07:20:32 +02:00
|
|
|
|
io::ErrorKind::InvalidInput,
|
|
|
|
|
"Invalid chunk size line: Invalid Size",
|
2019-11-15 10:54:11 +01:00
|
|
|
|
)));
|
2018-04-29 07:20:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-15 10:54:11 +01:00
|
|
|
|
Poll::Ready(Ok(ChunkedState::Size))
|
2018-04-29 07:20:32 +02:00
|
|
|
|
}
|
2019-11-15 10:54:11 +01:00
|
|
|
|
|
|
|
|
|
fn read_size_lws(rdr: &mut BytesMut) -> Poll<Result<ChunkedState, io::Error>> {
|
2018-04-29 07:20:32 +02:00
|
|
|
|
trace!("read_size_lws");
|
|
|
|
|
match byte!(rdr) {
|
|
|
|
|
// LWS can follow the chunk size, but no more digits can come
|
2019-11-15 10:54:11 +01:00
|
|
|
|
b'\t' | b' ' => Poll::Ready(Ok(ChunkedState::SizeLws)),
|
|
|
|
|
b';' => Poll::Ready(Ok(ChunkedState::Extension)),
|
|
|
|
|
b'\r' => Poll::Ready(Ok(ChunkedState::SizeLf)),
|
|
|
|
|
_ => Poll::Ready(Err(io::Error::new(
|
2018-04-29 07:20:32 +02:00
|
|
|
|
io::ErrorKind::InvalidInput,
|
|
|
|
|
"Invalid chunk size linear white space",
|
2019-11-15 10:54:11 +01:00
|
|
|
|
))),
|
2018-04-29 07:20:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-15 10:54:11 +01:00
|
|
|
|
fn read_extension(rdr: &mut BytesMut) -> Poll<Result<ChunkedState, io::Error>> {
|
2018-04-29 07:20:32 +02:00
|
|
|
|
match byte!(rdr) {
|
2019-11-15 10:54:11 +01:00
|
|
|
|
b'\r' => Poll::Ready(Ok(ChunkedState::SizeLf)),
|
|
|
|
|
_ => Poll::Ready(Ok(ChunkedState::Extension)), // no supported extensions
|
2018-04-29 07:20:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fn read_size_lf(
|
2018-10-30 00:39:46 +01:00
|
|
|
|
rdr: &mut BytesMut,
|
|
|
|
|
size: &mut u64,
|
2019-11-15 10:54:11 +01:00
|
|
|
|
) -> Poll<Result<ChunkedState, io::Error>> {
|
2018-04-29 07:20:32 +02:00
|
|
|
|
match byte!(rdr) {
|
2019-11-15 10:54:11 +01:00
|
|
|
|
b'\n' if *size > 0 => Poll::Ready(Ok(ChunkedState::Body)),
|
|
|
|
|
b'\n' if *size == 0 => Poll::Ready(Ok(ChunkedState::EndCr)),
|
|
|
|
|
_ => Poll::Ready(Err(io::Error::new(
|
2018-04-29 18:09:08 +02:00
|
|
|
|
io::ErrorKind::InvalidInput,
|
|
|
|
|
"Invalid chunk size LF",
|
2019-11-15 10:54:11 +01:00
|
|
|
|
))),
|
2018-04-29 07:20:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn read_body(
|
2018-10-30 00:39:46 +01:00
|
|
|
|
rdr: &mut BytesMut,
|
|
|
|
|
rem: &mut u64,
|
|
|
|
|
buf: &mut Option<Bytes>,
|
2019-11-15 10:54:11 +01:00
|
|
|
|
) -> Poll<Result<ChunkedState, io::Error>> {
|
2018-04-29 07:20:32 +02:00
|
|
|
|
trace!("Chunked read, remaining={:?}", rem);
|
|
|
|
|
|
|
|
|
|
let len = rdr.len() as u64;
|
|
|
|
|
if len == 0 {
|
2019-11-15 10:54:11 +01:00
|
|
|
|
Poll::Ready(Ok(ChunkedState::Body))
|
2018-04-29 07:20:32 +02:00
|
|
|
|
} else {
|
|
|
|
|
let slice;
|
|
|
|
|
if *rem > len {
|
2019-12-05 18:35:43 +01:00
|
|
|
|
slice = rdr.split().freeze();
|
2018-04-29 07:20:32 +02:00
|
|
|
|
*rem -= len;
|
|
|
|
|
} else {
|
|
|
|
|
slice = rdr.split_to(*rem as usize).freeze();
|
|
|
|
|
*rem = 0;
|
|
|
|
|
}
|
|
|
|
|
*buf = Some(slice);
|
|
|
|
|
if *rem > 0 {
|
2019-11-15 10:54:11 +01:00
|
|
|
|
Poll::Ready(Ok(ChunkedState::Body))
|
2018-04-29 07:20:32 +02:00
|
|
|
|
} else {
|
2019-11-15 10:54:11 +01:00
|
|
|
|
Poll::Ready(Ok(ChunkedState::BodyCr))
|
2018-04-29 07:20:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-15 10:54:11 +01:00
|
|
|
|
fn read_body_cr(rdr: &mut BytesMut) -> Poll<Result<ChunkedState, io::Error>> {
|
2018-04-29 07:20:32 +02:00
|
|
|
|
match byte!(rdr) {
|
2019-11-15 10:54:11 +01:00
|
|
|
|
b'\r' => Poll::Ready(Ok(ChunkedState::BodyLf)),
|
|
|
|
|
_ => Poll::Ready(Err(io::Error::new(
|
2018-04-29 18:09:08 +02:00
|
|
|
|
io::ErrorKind::InvalidInput,
|
|
|
|
|
"Invalid chunk body CR",
|
2019-11-15 10:54:11 +01:00
|
|
|
|
))),
|
2018-04-29 07:20:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-15 10:54:11 +01:00
|
|
|
|
fn read_body_lf(rdr: &mut BytesMut) -> Poll<Result<ChunkedState, io::Error>> {
|
2018-04-29 07:20:32 +02:00
|
|
|
|
match byte!(rdr) {
|
2019-11-15 10:54:11 +01:00
|
|
|
|
b'\n' => Poll::Ready(Ok(ChunkedState::Size)),
|
|
|
|
|
_ => Poll::Ready(Err(io::Error::new(
|
2018-04-29 18:09:08 +02:00
|
|
|
|
io::ErrorKind::InvalidInput,
|
|
|
|
|
"Invalid chunk body LF",
|
2019-11-15 10:54:11 +01:00
|
|
|
|
))),
|
2018-04-29 07:20:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-15 10:54:11 +01:00
|
|
|
|
fn read_end_cr(rdr: &mut BytesMut) -> Poll<Result<ChunkedState, io::Error>> {
|
2018-04-29 07:20:32 +02:00
|
|
|
|
match byte!(rdr) {
|
2019-11-15 10:54:11 +01:00
|
|
|
|
b'\r' => Poll::Ready(Ok(ChunkedState::EndLf)),
|
|
|
|
|
_ => Poll::Ready(Err(io::Error::new(
|
2018-04-29 18:09:08 +02:00
|
|
|
|
io::ErrorKind::InvalidInput,
|
|
|
|
|
"Invalid chunk end CR",
|
2019-11-15 10:54:11 +01:00
|
|
|
|
))),
|
2018-04-29 07:20:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-15 10:54:11 +01:00
|
|
|
|
fn read_end_lf(rdr: &mut BytesMut) -> Poll<Result<ChunkedState, io::Error>> {
|
2018-04-29 07:20:32 +02:00
|
|
|
|
match byte!(rdr) {
|
2019-11-15 10:54:11 +01:00
|
|
|
|
b'\n' => Poll::Ready(Ok(ChunkedState::End)),
|
|
|
|
|
_ => Poll::Ready(Err(io::Error::new(
|
2018-04-29 18:09:08 +02:00
|
|
|
|
io::ErrorKind::InvalidInput,
|
|
|
|
|
"Invalid chunk end LF",
|
2019-11-15 10:54:11 +01:00
|
|
|
|
))),
|
2018-04-29 07:20:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-05 06:14:18 +02:00
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
2019-01-29 19:34:27 +01:00
|
|
|
|
use bytes::{Bytes, BytesMut};
|
2018-10-05 06:14:18 +02:00
|
|
|
|
use http::{Method, Version};
|
|
|
|
|
|
|
|
|
|
use super::*;
|
2018-12-07 00:03:01 +01:00
|
|
|
|
use crate::error::ParseError;
|
2019-04-07 00:02:02 +02:00
|
|
|
|
use crate::http::header::{HeaderName, SET_COOKIE};
|
2018-12-07 00:03:01 +01:00
|
|
|
|
use crate::httpmessage::HttpMessage;
|
2018-10-05 06:14:18 +02:00
|
|
|
|
|
2018-10-23 03:18:05 +02:00
|
|
|
|
impl PayloadType {
|
2018-10-14 08:57:31 +02:00
|
|
|
|
fn unwrap(self) -> PayloadDecoder {
|
|
|
|
|
match self {
|
2018-10-23 03:18:05 +02:00
|
|
|
|
PayloadType::Payload(pl) => pl,
|
2018-10-14 08:57:31 +02:00
|
|
|
|
_ => panic!(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn is_unhandled(&self) -> bool {
|
|
|
|
|
match self {
|
2018-11-14 07:53:30 +01:00
|
|
|
|
PayloadType::Stream(_) => true,
|
2018-10-14 08:57:31 +02:00
|
|
|
|
_ => false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-07 05:31:22 +02:00
|
|
|
|
impl PayloadItem {
|
2018-10-05 06:14:18 +02:00
|
|
|
|
fn chunk(self) -> Bytes {
|
|
|
|
|
match self {
|
2018-10-07 05:31:22 +02:00
|
|
|
|
PayloadItem::Chunk(chunk) => chunk,
|
2018-10-05 06:14:18 +02:00
|
|
|
|
_ => panic!("error"),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fn eof(&self) -> bool {
|
|
|
|
|
match *self {
|
2018-10-07 05:31:22 +02:00
|
|
|
|
PayloadItem::Eof => true,
|
2018-10-05 06:14:18 +02:00
|
|
|
|
_ => false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
macro_rules! parse_ready {
|
|
|
|
|
($e:expr) => {{
|
2018-11-17 04:28:07 +01:00
|
|
|
|
match MessageDecoder::<Request>::default().decode($e) {
|
2018-10-07 05:31:22 +02:00
|
|
|
|
Ok(Some((msg, _))) => msg,
|
2018-10-05 06:14:18 +02:00
|
|
|
|
Ok(_) => unreachable!("Eof during parsing http request"),
|
|
|
|
|
Err(err) => unreachable!("Error during parsing http request: {:?}", err),
|
|
|
|
|
}
|
|
|
|
|
}};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
macro_rules! expect_parse_err {
|
|
|
|
|
($e:expr) => {{
|
2018-11-17 04:28:07 +01:00
|
|
|
|
match MessageDecoder::<Request>::default().decode($e) {
|
2018-10-05 06:14:18 +02:00
|
|
|
|
Err(err) => match err {
|
|
|
|
|
ParseError::Io(_) => unreachable!("Parse error expected"),
|
|
|
|
|
_ => (),
|
|
|
|
|
},
|
|
|
|
|
_ => unreachable!("Error expected"),
|
|
|
|
|
}
|
|
|
|
|
}};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_parse() {
|
|
|
|
|
let mut buf = BytesMut::from("GET /test HTTP/1.1\r\n\r\n");
|
|
|
|
|
|
2018-11-17 04:28:07 +01:00
|
|
|
|
let mut reader = MessageDecoder::<Request>::default();
|
2018-10-05 06:14:18 +02:00
|
|
|
|
match reader.decode(&mut buf) {
|
2018-10-07 05:31:22 +02:00
|
|
|
|
Ok(Some((req, _))) => {
|
2018-10-05 06:14:18 +02:00
|
|
|
|
assert_eq!(req.version(), Version::HTTP_11);
|
|
|
|
|
assert_eq!(*req.method(), Method::GET);
|
|
|
|
|
assert_eq!(req.path(), "/test");
|
|
|
|
|
}
|
|
|
|
|
Ok(_) | Err(_) => unreachable!("Error during parsing http request"),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_parse_partial() {
|
|
|
|
|
let mut buf = BytesMut::from("PUT /test HTTP/1");
|
|
|
|
|
|
2018-11-17 04:28:07 +01:00
|
|
|
|
let mut reader = MessageDecoder::<Request>::default();
|
2018-10-07 05:31:22 +02:00
|
|
|
|
assert!(reader.decode(&mut buf).unwrap().is_none());
|
2018-10-05 06:14:18 +02:00
|
|
|
|
|
|
|
|
|
buf.extend(b".1\r\n\r\n");
|
2018-10-07 05:31:22 +02:00
|
|
|
|
let (req, _) = reader.decode(&mut buf).unwrap().unwrap();
|
|
|
|
|
assert_eq!(req.version(), Version::HTTP_11);
|
|
|
|
|
assert_eq!(*req.method(), Method::PUT);
|
|
|
|
|
assert_eq!(req.path(), "/test");
|
2018-10-05 06:14:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_parse_post() {
|
|
|
|
|
let mut buf = BytesMut::from("POST /test2 HTTP/1.0\r\n\r\n");
|
|
|
|
|
|
2018-11-17 04:28:07 +01:00
|
|
|
|
let mut reader = MessageDecoder::<Request>::default();
|
2018-10-07 05:31:22 +02:00
|
|
|
|
let (req, _) = reader.decode(&mut buf).unwrap().unwrap();
|
|
|
|
|
assert_eq!(req.version(), Version::HTTP_10);
|
|
|
|
|
assert_eq!(*req.method(), Method::POST);
|
|
|
|
|
assert_eq!(req.path(), "/test2");
|
2018-10-05 06:14:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_parse_body() {
|
|
|
|
|
let mut buf =
|
|
|
|
|
BytesMut::from("GET /test HTTP/1.1\r\nContent-Length: 4\r\n\r\nbody");
|
|
|
|
|
|
2018-11-17 04:28:07 +01:00
|
|
|
|
let mut reader = MessageDecoder::<Request>::default();
|
2018-10-07 05:31:22 +02:00
|
|
|
|
let (req, pl) = reader.decode(&mut buf).unwrap().unwrap();
|
|
|
|
|
let mut pl = pl.unwrap();
|
|
|
|
|
assert_eq!(req.version(), Version::HTTP_11);
|
|
|
|
|
assert_eq!(*req.method(), Method::GET);
|
|
|
|
|
assert_eq!(req.path(), "/test");
|
|
|
|
|
assert_eq!(
|
|
|
|
|
pl.decode(&mut buf).unwrap().unwrap().chunk().as_ref(),
|
|
|
|
|
b"body"
|
|
|
|
|
);
|
2018-10-05 06:14:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_parse_body_crlf() {
|
|
|
|
|
let mut buf =
|
|
|
|
|
BytesMut::from("\r\nGET /test HTTP/1.1\r\nContent-Length: 4\r\n\r\nbody");
|
|
|
|
|
|
2018-11-17 04:28:07 +01:00
|
|
|
|
let mut reader = MessageDecoder::<Request>::default();
|
2018-10-07 05:31:22 +02:00
|
|
|
|
let (req, pl) = reader.decode(&mut buf).unwrap().unwrap();
|
|
|
|
|
let mut pl = pl.unwrap();
|
|
|
|
|
assert_eq!(req.version(), Version::HTTP_11);
|
|
|
|
|
assert_eq!(*req.method(), Method::GET);
|
|
|
|
|
assert_eq!(req.path(), "/test");
|
|
|
|
|
assert_eq!(
|
|
|
|
|
pl.decode(&mut buf).unwrap().unwrap().chunk().as_ref(),
|
|
|
|
|
b"body"
|
|
|
|
|
);
|
2018-10-05 06:14:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_parse_partial_eof() {
|
|
|
|
|
let mut buf = BytesMut::from("GET /test HTTP/1.1\r\n");
|
2018-11-17 04:28:07 +01:00
|
|
|
|
let mut reader = MessageDecoder::<Request>::default();
|
2018-10-05 06:14:18 +02:00
|
|
|
|
assert!(reader.decode(&mut buf).unwrap().is_none());
|
|
|
|
|
|
|
|
|
|
buf.extend(b"\r\n");
|
2018-10-07 05:31:22 +02:00
|
|
|
|
let (req, _) = reader.decode(&mut buf).unwrap().unwrap();
|
|
|
|
|
assert_eq!(req.version(), Version::HTTP_11);
|
|
|
|
|
assert_eq!(*req.method(), Method::GET);
|
|
|
|
|
assert_eq!(req.path(), "/test");
|
2018-10-05 06:14:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_headers_split_field() {
|
|
|
|
|
let mut buf = BytesMut::from("GET /test HTTP/1.1\r\n");
|
|
|
|
|
|
2018-11-17 04:28:07 +01:00
|
|
|
|
let mut reader = MessageDecoder::<Request>::default();
|
2018-12-06 23:32:52 +01:00
|
|
|
|
assert! { reader.decode(&mut buf).unwrap().is_none() }
|
2018-10-05 06:14:18 +02:00
|
|
|
|
|
|
|
|
|
buf.extend(b"t");
|
2018-12-06 23:32:52 +01:00
|
|
|
|
assert! { reader.decode(&mut buf).unwrap().is_none() }
|
2018-10-05 06:14:18 +02:00
|
|
|
|
|
|
|
|
|
buf.extend(b"es");
|
2018-12-06 23:32:52 +01:00
|
|
|
|
assert! { reader.decode(&mut buf).unwrap().is_none() }
|
2018-10-05 06:14:18 +02:00
|
|
|
|
|
|
|
|
|
buf.extend(b"t: value\r\n\r\n");
|
2018-10-07 05:31:22 +02:00
|
|
|
|
let (req, _) = reader.decode(&mut buf).unwrap().unwrap();
|
|
|
|
|
assert_eq!(req.version(), Version::HTTP_11);
|
|
|
|
|
assert_eq!(*req.method(), Method::GET);
|
|
|
|
|
assert_eq!(req.path(), "/test");
|
2019-04-07 00:02:02 +02:00
|
|
|
|
assert_eq!(
|
|
|
|
|
req.headers()
|
|
|
|
|
.get(HeaderName::try_from("test").unwrap())
|
|
|
|
|
.unwrap()
|
|
|
|
|
.as_bytes(),
|
|
|
|
|
b"value"
|
|
|
|
|
);
|
2018-10-05 06:14:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_headers_multi_value() {
|
|
|
|
|
let mut buf = BytesMut::from(
|
|
|
|
|
"GET /test HTTP/1.1\r\n\
|
|
|
|
|
Set-Cookie: c1=cookie1\r\n\
|
|
|
|
|
Set-Cookie: c2=cookie2\r\n\r\n",
|
|
|
|
|
);
|
2018-11-17 04:28:07 +01:00
|
|
|
|
let mut reader = MessageDecoder::<Request>::default();
|
2018-10-07 05:31:22 +02:00
|
|
|
|
let (req, _) = reader.decode(&mut buf).unwrap().unwrap();
|
2018-10-05 06:14:18 +02:00
|
|
|
|
|
|
|
|
|
let val: Vec<_> = req
|
|
|
|
|
.headers()
|
2019-04-07 00:02:02 +02:00
|
|
|
|
.get_all(SET_COOKIE)
|
2018-10-05 06:14:18 +02:00
|
|
|
|
.map(|v| v.to_str().unwrap().to_owned())
|
|
|
|
|
.collect();
|
2019-04-07 00:02:02 +02:00
|
|
|
|
assert_eq!(val[1], "c1=cookie1");
|
|
|
|
|
assert_eq!(val[0], "c2=cookie2");
|
2018-10-05 06:14:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_conn_default_1_0() {
|
|
|
|
|
let mut buf = BytesMut::from("GET /test HTTP/1.0\r\n\r\n");
|
|
|
|
|
let req = parse_ready!(&mut buf);
|
|
|
|
|
|
2018-11-19 23:57:12 +01:00
|
|
|
|
assert_eq!(req.head().connection_type(), ConnectionType::Close);
|
2018-10-05 06:14:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_conn_default_1_1() {
|
|
|
|
|
let mut buf = BytesMut::from("GET /test HTTP/1.1\r\n\r\n");
|
|
|
|
|
let req = parse_ready!(&mut buf);
|
|
|
|
|
|
2018-11-19 23:57:12 +01:00
|
|
|
|
assert_eq!(req.head().connection_type(), ConnectionType::KeepAlive);
|
2018-10-05 06:14:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_conn_close() {
|
|
|
|
|
let mut buf = BytesMut::from(
|
|
|
|
|
"GET /test HTTP/1.1\r\n\
|
|
|
|
|
connection: close\r\n\r\n",
|
|
|
|
|
);
|
|
|
|
|
let req = parse_ready!(&mut buf);
|
|
|
|
|
|
2019-03-27 18:38:01 +01:00
|
|
|
|
assert_eq!(req.head().connection_type(), ConnectionType::Close);
|
2018-12-16 16:43:11 +01:00
|
|
|
|
|
|
|
|
|
let mut buf = BytesMut::from(
|
|
|
|
|
"GET /test HTTP/1.1\r\n\
|
|
|
|
|
connection: Close\r\n\r\n",
|
|
|
|
|
);
|
|
|
|
|
let req = parse_ready!(&mut buf);
|
|
|
|
|
|
2019-03-27 18:38:01 +01:00
|
|
|
|
assert_eq!(req.head().connection_type(), ConnectionType::Close);
|
2018-10-05 06:14:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_conn_close_1_0() {
|
|
|
|
|
let mut buf = BytesMut::from(
|
|
|
|
|
"GET /test HTTP/1.0\r\n\
|
|
|
|
|
connection: close\r\n\r\n",
|
|
|
|
|
);
|
2019-03-05 22:16:26 +01:00
|
|
|
|
|
2018-10-05 06:14:18 +02:00
|
|
|
|
let req = parse_ready!(&mut buf);
|
|
|
|
|
|
2019-03-27 18:38:01 +01:00
|
|
|
|
assert_eq!(req.head().connection_type(), ConnectionType::Close);
|
2018-10-05 06:14:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_conn_keep_alive_1_0() {
|
|
|
|
|
let mut buf = BytesMut::from(
|
|
|
|
|
"GET /test HTTP/1.0\r\n\
|
|
|
|
|
connection: keep-alive\r\n\r\n",
|
|
|
|
|
);
|
|
|
|
|
let req = parse_ready!(&mut buf);
|
|
|
|
|
|
2019-03-27 18:38:01 +01:00
|
|
|
|
assert_eq!(req.head().connection_type(), ConnectionType::KeepAlive);
|
2018-12-16 16:43:11 +01:00
|
|
|
|
|
|
|
|
|
let mut buf = BytesMut::from(
|
|
|
|
|
"GET /test HTTP/1.0\r\n\
|
|
|
|
|
connection: Keep-Alive\r\n\r\n",
|
|
|
|
|
);
|
|
|
|
|
let req = parse_ready!(&mut buf);
|
|
|
|
|
|
2019-03-27 18:38:01 +01:00
|
|
|
|
assert_eq!(req.head().connection_type(), ConnectionType::KeepAlive);
|
2018-10-05 06:14:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_conn_keep_alive_1_1() {
|
|
|
|
|
let mut buf = BytesMut::from(
|
|
|
|
|
"GET /test HTTP/1.1\r\n\
|
|
|
|
|
connection: keep-alive\r\n\r\n",
|
|
|
|
|
);
|
|
|
|
|
let req = parse_ready!(&mut buf);
|
|
|
|
|
|
2019-03-27 18:38:01 +01:00
|
|
|
|
assert_eq!(req.head().connection_type(), ConnectionType::KeepAlive);
|
2018-10-05 06:14:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_conn_other_1_0() {
|
|
|
|
|
let mut buf = BytesMut::from(
|
|
|
|
|
"GET /test HTTP/1.0\r\n\
|
|
|
|
|
connection: other\r\n\r\n",
|
|
|
|
|
);
|
|
|
|
|
let req = parse_ready!(&mut buf);
|
|
|
|
|
|
2019-02-08 06:16:46 +01:00
|
|
|
|
assert_eq!(req.head().connection_type(), ConnectionType::Close);
|
2018-10-05 06:14:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_conn_other_1_1() {
|
|
|
|
|
let mut buf = BytesMut::from(
|
|
|
|
|
"GET /test HTTP/1.1\r\n\
|
|
|
|
|
connection: other\r\n\r\n",
|
|
|
|
|
);
|
|
|
|
|
let req = parse_ready!(&mut buf);
|
|
|
|
|
|
2019-02-08 06:16:46 +01:00
|
|
|
|
assert_eq!(req.head().connection_type(), ConnectionType::KeepAlive);
|
2018-10-05 06:14:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_conn_upgrade() {
|
|
|
|
|
let mut buf = BytesMut::from(
|
|
|
|
|
"GET /test HTTP/1.1\r\n\
|
|
|
|
|
upgrade: websockets\r\n\
|
|
|
|
|
connection: upgrade\r\n\r\n",
|
|
|
|
|
);
|
|
|
|
|
let req = parse_ready!(&mut buf);
|
|
|
|
|
|
|
|
|
|
assert!(req.upgrade());
|
2019-03-27 18:38:01 +01:00
|
|
|
|
assert_eq!(req.head().connection_type(), ConnectionType::Upgrade);
|
2018-12-16 16:43:11 +01:00
|
|
|
|
|
|
|
|
|
let mut buf = BytesMut::from(
|
|
|
|
|
"GET /test HTTP/1.1\r\n\
|
|
|
|
|
upgrade: Websockets\r\n\
|
|
|
|
|
connection: Upgrade\r\n\r\n",
|
|
|
|
|
);
|
|
|
|
|
let req = parse_ready!(&mut buf);
|
|
|
|
|
|
|
|
|
|
assert!(req.upgrade());
|
2019-03-27 18:38:01 +01:00
|
|
|
|
assert_eq!(req.head().connection_type(), ConnectionType::Upgrade);
|
2018-10-05 06:14:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_conn_upgrade_connect_method() {
|
|
|
|
|
let mut buf = BytesMut::from(
|
|
|
|
|
"CONNECT /test HTTP/1.1\r\n\
|
|
|
|
|
content-type: text/plain\r\n\r\n",
|
|
|
|
|
);
|
|
|
|
|
let req = parse_ready!(&mut buf);
|
|
|
|
|
|
|
|
|
|
assert!(req.upgrade());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_request_chunked() {
|
|
|
|
|
let mut buf = BytesMut::from(
|
|
|
|
|
"GET /test HTTP/1.1\r\n\
|
|
|
|
|
transfer-encoding: chunked\r\n\r\n",
|
|
|
|
|
);
|
|
|
|
|
let req = parse_ready!(&mut buf);
|
|
|
|
|
|
|
|
|
|
if let Ok(val) = req.chunked() {
|
|
|
|
|
assert!(val);
|
|
|
|
|
} else {
|
|
|
|
|
unreachable!("Error");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// type in chunked
|
|
|
|
|
let mut buf = BytesMut::from(
|
|
|
|
|
"GET /test HTTP/1.1\r\n\
|
|
|
|
|
transfer-encoding: chnked\r\n\r\n",
|
|
|
|
|
);
|
|
|
|
|
let req = parse_ready!(&mut buf);
|
|
|
|
|
|
|
|
|
|
if let Ok(val) = req.chunked() {
|
|
|
|
|
assert!(!val);
|
|
|
|
|
} else {
|
|
|
|
|
unreachable!("Error");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_headers_content_length_err_1() {
|
|
|
|
|
let mut buf = BytesMut::from(
|
|
|
|
|
"GET /test HTTP/1.1\r\n\
|
|
|
|
|
content-length: line\r\n\r\n",
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect_parse_err!(&mut buf)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_headers_content_length_err_2() {
|
|
|
|
|
let mut buf = BytesMut::from(
|
|
|
|
|
"GET /test HTTP/1.1\r\n\
|
|
|
|
|
content-length: -1\r\n\r\n",
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect_parse_err!(&mut buf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_invalid_header() {
|
|
|
|
|
let mut buf = BytesMut::from(
|
|
|
|
|
"GET /test HTTP/1.1\r\n\
|
|
|
|
|
test line\r\n\r\n",
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect_parse_err!(&mut buf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_invalid_name() {
|
|
|
|
|
let mut buf = BytesMut::from(
|
|
|
|
|
"GET /test HTTP/1.1\r\n\
|
|
|
|
|
test[]: line\r\n\r\n",
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect_parse_err!(&mut buf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_http_request_bad_status_line() {
|
|
|
|
|
let mut buf = BytesMut::from("getpath \r\n\r\n");
|
|
|
|
|
expect_parse_err!(&mut buf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_http_request_upgrade() {
|
|
|
|
|
let mut buf = BytesMut::from(
|
|
|
|
|
"GET /test HTTP/1.1\r\n\
|
|
|
|
|
connection: upgrade\r\n\
|
|
|
|
|
upgrade: websocket\r\n\r\n\
|
|
|
|
|
some raw data",
|
|
|
|
|
);
|
2018-11-17 04:28:07 +01:00
|
|
|
|
let mut reader = MessageDecoder::<Request>::default();
|
2018-10-07 05:31:22 +02:00
|
|
|
|
let (req, pl) = reader.decode(&mut buf).unwrap().unwrap();
|
2019-03-27 18:38:01 +01:00
|
|
|
|
assert_eq!(req.head().connection_type(), ConnectionType::Upgrade);
|
2018-10-05 06:14:18 +02:00
|
|
|
|
assert!(req.upgrade());
|
2018-10-14 08:57:31 +02:00
|
|
|
|
assert!(pl.is_unhandled());
|
2018-10-05 06:14:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_http_request_parser_utf8() {
|
|
|
|
|
let mut buf = BytesMut::from(
|
|
|
|
|
"GET /test HTTP/1.1\r\n\
|
|
|
|
|
x-test: тест\r\n\r\n",
|
|
|
|
|
);
|
|
|
|
|
let req = parse_ready!(&mut buf);
|
|
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
req.headers().get("x-test").unwrap().as_bytes(),
|
|
|
|
|
"тест".as_bytes()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_http_request_parser_two_slashes() {
|
|
|
|
|
let mut buf = BytesMut::from("GET //path HTTP/1.1\r\n\r\n");
|
|
|
|
|
let req = parse_ready!(&mut buf);
|
|
|
|
|
|
|
|
|
|
assert_eq!(req.path(), "//path");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_http_request_parser_bad_method() {
|
|
|
|
|
let mut buf = BytesMut::from("!12%()+=~$ /get HTTP/1.1\r\n\r\n");
|
|
|
|
|
|
|
|
|
|
expect_parse_err!(&mut buf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_http_request_parser_bad_version() {
|
|
|
|
|
let mut buf = BytesMut::from("GET //get HT/11\r\n\r\n");
|
|
|
|
|
|
|
|
|
|
expect_parse_err!(&mut buf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_http_request_chunked_payload() {
|
|
|
|
|
let mut buf = BytesMut::from(
|
|
|
|
|
"GET /test HTTP/1.1\r\n\
|
|
|
|
|
transfer-encoding: chunked\r\n\r\n",
|
|
|
|
|
);
|
2018-11-17 04:28:07 +01:00
|
|
|
|
let mut reader = MessageDecoder::<Request>::default();
|
2018-10-07 05:31:22 +02:00
|
|
|
|
let (req, pl) = reader.decode(&mut buf).unwrap().unwrap();
|
|
|
|
|
let mut pl = pl.unwrap();
|
2018-10-05 06:14:18 +02:00
|
|
|
|
assert!(req.chunked().unwrap());
|
|
|
|
|
|
|
|
|
|
buf.extend(b"4\r\ndata\r\n4\r\nline\r\n0\r\n\r\n");
|
|
|
|
|
assert_eq!(
|
2018-10-07 05:31:22 +02:00
|
|
|
|
pl.decode(&mut buf).unwrap().unwrap().chunk().as_ref(),
|
2018-10-05 06:14:18 +02:00
|
|
|
|
b"data"
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(
|
2018-10-07 05:31:22 +02:00
|
|
|
|
pl.decode(&mut buf).unwrap().unwrap().chunk().as_ref(),
|
2018-10-05 06:14:18 +02:00
|
|
|
|
b"line"
|
|
|
|
|
);
|
2018-10-07 05:31:22 +02:00
|
|
|
|
assert!(pl.decode(&mut buf).unwrap().unwrap().eof());
|
2018-10-05 06:14:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_http_request_chunked_payload_and_next_message() {
|
|
|
|
|
let mut buf = BytesMut::from(
|
|
|
|
|
"GET /test HTTP/1.1\r\n\
|
|
|
|
|
transfer-encoding: chunked\r\n\r\n",
|
|
|
|
|
);
|
2018-11-17 04:28:07 +01:00
|
|
|
|
let mut reader = MessageDecoder::<Request>::default();
|
2018-10-07 05:31:22 +02:00
|
|
|
|
let (req, pl) = reader.decode(&mut buf).unwrap().unwrap();
|
|
|
|
|
let mut pl = pl.unwrap();
|
2018-10-05 06:14:18 +02:00
|
|
|
|
assert!(req.chunked().unwrap());
|
|
|
|
|
|
|
|
|
|
buf.extend(
|
|
|
|
|
b"4\r\ndata\r\n4\r\nline\r\n0\r\n\r\n\
|
|
|
|
|
POST /test2 HTTP/1.1\r\n\
|
|
|
|
|
transfer-encoding: chunked\r\n\r\n"
|
|
|
|
|
.iter(),
|
|
|
|
|
);
|
2018-10-07 05:31:22 +02:00
|
|
|
|
let msg = pl.decode(&mut buf).unwrap().unwrap();
|
2018-10-05 06:14:18 +02:00
|
|
|
|
assert_eq!(msg.chunk().as_ref(), b"data");
|
2018-10-07 05:31:22 +02:00
|
|
|
|
let msg = pl.decode(&mut buf).unwrap().unwrap();
|
2018-10-05 06:14:18 +02:00
|
|
|
|
assert_eq!(msg.chunk().as_ref(), b"line");
|
2018-10-07 05:31:22 +02:00
|
|
|
|
let msg = pl.decode(&mut buf).unwrap().unwrap();
|
2018-10-05 06:14:18 +02:00
|
|
|
|
assert!(msg.eof());
|
|
|
|
|
|
2018-10-07 05:31:22 +02:00
|
|
|
|
let (req, _) = reader.decode(&mut buf).unwrap().unwrap();
|
|
|
|
|
assert!(req.chunked().unwrap());
|
|
|
|
|
assert_eq!(*req.method(), Method::POST);
|
|
|
|
|
assert!(req.chunked().unwrap());
|
2018-10-05 06:14:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_http_request_chunked_payload_chunks() {
|
|
|
|
|
let mut buf = BytesMut::from(
|
|
|
|
|
"GET /test HTTP/1.1\r\n\
|
|
|
|
|
transfer-encoding: chunked\r\n\r\n",
|
|
|
|
|
);
|
|
|
|
|
|
2018-11-17 04:28:07 +01:00
|
|
|
|
let mut reader = MessageDecoder::<Request>::default();
|
2018-10-07 05:31:22 +02:00
|
|
|
|
let (req, pl) = reader.decode(&mut buf).unwrap().unwrap();
|
|
|
|
|
let mut pl = pl.unwrap();
|
2018-10-05 06:14:18 +02:00
|
|
|
|
assert!(req.chunked().unwrap());
|
|
|
|
|
|
|
|
|
|
buf.extend(b"4\r\n1111\r\n");
|
2018-10-07 05:31:22 +02:00
|
|
|
|
let msg = pl.decode(&mut buf).unwrap().unwrap();
|
2018-10-05 06:14:18 +02:00
|
|
|
|
assert_eq!(msg.chunk().as_ref(), b"1111");
|
|
|
|
|
|
|
|
|
|
buf.extend(b"4\r\ndata\r");
|
2018-10-07 05:31:22 +02:00
|
|
|
|
let msg = pl.decode(&mut buf).unwrap().unwrap();
|
2018-10-05 06:14:18 +02:00
|
|
|
|
assert_eq!(msg.chunk().as_ref(), b"data");
|
|
|
|
|
|
|
|
|
|
buf.extend(b"\n4");
|
2018-10-07 05:31:22 +02:00
|
|
|
|
assert!(pl.decode(&mut buf).unwrap().is_none());
|
2018-10-05 06:14:18 +02:00
|
|
|
|
|
|
|
|
|
buf.extend(b"\r");
|
2018-10-07 05:31:22 +02:00
|
|
|
|
assert!(pl.decode(&mut buf).unwrap().is_none());
|
2018-10-05 06:14:18 +02:00
|
|
|
|
buf.extend(b"\n");
|
2018-10-07 05:31:22 +02:00
|
|
|
|
assert!(pl.decode(&mut buf).unwrap().is_none());
|
2018-10-05 06:14:18 +02:00
|
|
|
|
|
|
|
|
|
buf.extend(b"li");
|
2018-10-07 05:31:22 +02:00
|
|
|
|
let msg = pl.decode(&mut buf).unwrap().unwrap();
|
2018-10-05 06:14:18 +02:00
|
|
|
|
assert_eq!(msg.chunk().as_ref(), b"li");
|
|
|
|
|
|
|
|
|
|
//trailers
|
|
|
|
|
//buf.feed_data("test: test\r\n");
|
|
|
|
|
//not_ready!(reader.parse(&mut buf, &mut readbuf));
|
|
|
|
|
|
|
|
|
|
buf.extend(b"ne\r\n0\r\n");
|
2018-10-07 05:31:22 +02:00
|
|
|
|
let msg = pl.decode(&mut buf).unwrap().unwrap();
|
2018-10-05 06:14:18 +02:00
|
|
|
|
assert_eq!(msg.chunk().as_ref(), b"ne");
|
2018-10-07 05:31:22 +02:00
|
|
|
|
assert!(pl.decode(&mut buf).unwrap().is_none());
|
2018-10-05 06:14:18 +02:00
|
|
|
|
|
|
|
|
|
buf.extend(b"\r\n");
|
2018-10-07 05:31:22 +02:00
|
|
|
|
assert!(pl.decode(&mut buf).unwrap().unwrap().eof());
|
2018-10-05 06:14:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_parse_chunked_payload_chunk_extension() {
|
|
|
|
|
let mut buf = BytesMut::from(
|
|
|
|
|
&"GET /test HTTP/1.1\r\n\
|
|
|
|
|
transfer-encoding: chunked\r\n\r\n"[..],
|
|
|
|
|
);
|
|
|
|
|
|
2018-11-17 04:28:07 +01:00
|
|
|
|
let mut reader = MessageDecoder::<Request>::default();
|
2018-10-07 05:31:22 +02:00
|
|
|
|
let (msg, pl) = reader.decode(&mut buf).unwrap().unwrap();
|
|
|
|
|
let mut pl = pl.unwrap();
|
|
|
|
|
assert!(msg.chunked().unwrap());
|
2018-10-05 06:14:18 +02:00
|
|
|
|
|
|
|
|
|
buf.extend(b"4;test\r\ndata\r\n4\r\nline\r\n0\r\n\r\n"); // test: test\r\n\r\n")
|
2018-10-07 05:31:22 +02:00
|
|
|
|
let chunk = pl.decode(&mut buf).unwrap().unwrap().chunk();
|
2018-10-05 06:14:18 +02:00
|
|
|
|
assert_eq!(chunk, Bytes::from_static(b"data"));
|
2018-10-07 05:31:22 +02:00
|
|
|
|
let chunk = pl.decode(&mut buf).unwrap().unwrap().chunk();
|
2018-10-05 06:14:18 +02:00
|
|
|
|
assert_eq!(chunk, Bytes::from_static(b"line"));
|
2018-10-07 05:31:22 +02:00
|
|
|
|
let msg = pl.decode(&mut buf).unwrap().unwrap();
|
2018-10-05 06:14:18 +02:00
|
|
|
|
assert!(msg.eof());
|
|
|
|
|
}
|
2019-04-24 20:57:40 +02:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_response_http10_read_until_eof() {
|
|
|
|
|
let mut buf = BytesMut::from(&"HTTP/1.0 200 Ok\r\n\r\ntest data"[..]);
|
|
|
|
|
|
|
|
|
|
let mut reader = MessageDecoder::<ResponseHead>::default();
|
|
|
|
|
let (_msg, pl) = reader.decode(&mut buf).unwrap().unwrap();
|
|
|
|
|
let mut pl = pl.unwrap();
|
|
|
|
|
|
|
|
|
|
let chunk = pl.decode(&mut buf).unwrap().unwrap();
|
|
|
|
|
assert_eq!(chunk, PayloadItem::Chunk(Bytes::from_static(b"test data")));
|
|
|
|
|
}
|
2018-10-05 06:14:18 +02:00
|
|
|
|
}
|