mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-24 16:02:59 +01:00
split request decoder and payload decoder
This commit is contained in:
parent
c0699a070e
commit
ee62814216
@ -4,15 +4,14 @@ use std::io::{self, Write};
|
|||||||
use bytes::{BufMut, Bytes, BytesMut};
|
use bytes::{BufMut, Bytes, BytesMut};
|
||||||
use tokio_codec::{Decoder, Encoder};
|
use tokio_codec::{Decoder, Encoder};
|
||||||
|
|
||||||
use super::decoder::H1Decoder;
|
use super::decoder::{PayloadDecoder, PayloadItem, RequestDecoder};
|
||||||
pub use super::decoder::InMessage;
|
|
||||||
use super::encoder::{ResponseEncoder, ResponseLength};
|
use super::encoder::{ResponseEncoder, ResponseLength};
|
||||||
use body::Body;
|
use body::Body;
|
||||||
use error::ParseError;
|
use error::ParseError;
|
||||||
use helpers;
|
use helpers;
|
||||||
use http::header::{HeaderValue, CONNECTION, CONTENT_LENGTH, DATE, TRANSFER_ENCODING};
|
use http::header::{HeaderValue, CONNECTION, CONTENT_LENGTH, DATE, TRANSFER_ENCODING};
|
||||||
use http::{Method, Version};
|
use http::{Method, Version};
|
||||||
use request::RequestPool;
|
use request::{Request, RequestPool};
|
||||||
use response::Response;
|
use response::Response;
|
||||||
|
|
||||||
bitflags! {
|
bitflags! {
|
||||||
@ -34,9 +33,23 @@ pub enum OutMessage {
|
|||||||
Payload(Bytes),
|
Payload(Bytes),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Incoming http/1 request
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum InMessage {
|
||||||
|
/// Request
|
||||||
|
Message(Request),
|
||||||
|
/// Request with payload
|
||||||
|
MessageWithPayload(Request),
|
||||||
|
/// Payload chunk
|
||||||
|
Chunk(Bytes),
|
||||||
|
/// End of payload
|
||||||
|
Eof,
|
||||||
|
}
|
||||||
|
|
||||||
/// HTTP/1 Codec
|
/// HTTP/1 Codec
|
||||||
pub struct Codec {
|
pub struct Codec {
|
||||||
decoder: H1Decoder,
|
decoder: RequestDecoder,
|
||||||
|
payload: Option<PayloadDecoder>,
|
||||||
version: Version,
|
version: Version,
|
||||||
|
|
||||||
// encoder part
|
// encoder part
|
||||||
@ -64,7 +77,8 @@ impl Codec {
|
|||||||
Flags::empty()
|
Flags::empty()
|
||||||
};
|
};
|
||||||
Codec {
|
Codec {
|
||||||
decoder: H1Decoder::with_pool(pool),
|
decoder: RequestDecoder::with_pool(pool),
|
||||||
|
payload: None,
|
||||||
version: Version::HTTP_11,
|
version: Version::HTTP_11,
|
||||||
|
|
||||||
flags,
|
flags,
|
||||||
@ -234,21 +248,28 @@ impl Decoder for Codec {
|
|||||||
type Error = ParseError;
|
type Error = ParseError;
|
||||||
|
|
||||||
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
|
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
|
||||||
let res = self.decoder.decode(src);
|
if self.payload.is_some() {
|
||||||
|
Ok(match self.payload.as_mut().unwrap().decode(src)? {
|
||||||
match res {
|
Some(PayloadItem::Chunk(chunk)) => Some(InMessage::Chunk(chunk)),
|
||||||
Ok(Some(InMessage::Message(ref req)))
|
Some(PayloadItem::Eof) => Some(InMessage::Eof),
|
||||||
| Ok(Some(InMessage::MessageWithPayload(ref req))) => {
|
None => None,
|
||||||
|
})
|
||||||
|
} else if let Some((req, payload)) = self.decoder.decode(src)? {
|
||||||
self.flags
|
self.flags
|
||||||
.set(Flags::HEAD, req.inner.method == Method::HEAD);
|
.set(Flags::HEAD, req.inner.method == Method::HEAD);
|
||||||
self.version = req.inner.version;
|
self.version = req.inner.version;
|
||||||
if self.flags.contains(Flags::KEEPALIVE_ENABLED) {
|
if self.flags.contains(Flags::KEEPALIVE_ENABLED) {
|
||||||
self.flags.set(Flags::KEEPALIVE, req.keep_alive());
|
self.flags.set(Flags::KEEPALIVE, req.keep_alive());
|
||||||
}
|
}
|
||||||
|
self.payload = payload;
|
||||||
|
if self.payload.is_some() {
|
||||||
|
Ok(Some(InMessage::MessageWithPayload(req)))
|
||||||
|
} else {
|
||||||
|
Ok(Some(InMessage::Message(req)))
|
||||||
}
|
}
|
||||||
_ => (),
|
} else {
|
||||||
|
Ok(None)
|
||||||
}
|
}
|
||||||
res
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ use std::{io, mem};
|
|||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
use futures::{Async, Poll};
|
use futures::{Async, Poll};
|
||||||
use httparse;
|
use httparse;
|
||||||
|
use tokio_codec::Decoder;
|
||||||
|
|
||||||
use error::ParseError;
|
use error::ParseError;
|
||||||
use http::header::{HeaderName, HeaderValue};
|
use http::header::{HeaderName, HeaderValue};
|
||||||
@ -13,75 +14,25 @@ use uri::Url;
|
|||||||
const MAX_BUFFER_SIZE: usize = 131_072;
|
const MAX_BUFFER_SIZE: usize = 131_072;
|
||||||
const MAX_HEADERS: usize = 96;
|
const MAX_HEADERS: usize = 96;
|
||||||
|
|
||||||
pub(crate) struct H1Decoder {
|
pub struct RequestDecoder(&'static RequestPool);
|
||||||
decoder: Option<EncodingDecoder>,
|
|
||||||
pool: &'static RequestPool,
|
impl RequestDecoder {
|
||||||
|
pub(crate) fn with_pool(pool: &'static RequestPool) -> RequestDecoder {
|
||||||
|
RequestDecoder(pool)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Incoming http/1 request
|
impl Default for RequestDecoder {
|
||||||
#[derive(Debug)]
|
fn default() -> RequestDecoder {
|
||||||
pub enum InMessage {
|
RequestDecoder::with_pool(RequestPool::pool())
|
||||||
/// Request
|
}
|
||||||
Message(Request),
|
|
||||||
/// Request with payload
|
|
||||||
MessageWithPayload(Request),
|
|
||||||
/// Payload chunk
|
|
||||||
Chunk(Bytes),
|
|
||||||
/// End of payload
|
|
||||||
Eof,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl H1Decoder {
|
impl Decoder for RequestDecoder {
|
||||||
#[cfg(test)]
|
type Item = (Request, Option<PayloadDecoder>);
|
||||||
pub fn new() -> H1Decoder {
|
type Error = ParseError;
|
||||||
H1Decoder::with_pool(RequestPool::pool())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn with_pool(pool: &'static RequestPool) -> H1Decoder {
|
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
|
||||||
H1Decoder {
|
|
||||||
pool,
|
|
||||||
decoder: None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn decode(
|
|
||||||
&mut self, src: &mut BytesMut,
|
|
||||||
) -> Result<Option<InMessage>, ParseError> {
|
|
||||||
// read payload
|
|
||||||
if self.decoder.is_some() {
|
|
||||||
match self.decoder.as_mut().unwrap().decode(src)? {
|
|
||||||
Async::Ready(Some(bytes)) => return Ok(Some(InMessage::Chunk(bytes))),
|
|
||||||
Async::Ready(None) => {
|
|
||||||
self.decoder.take();
|
|
||||||
return Ok(Some(InMessage::Eof));
|
|
||||||
}
|
|
||||||
Async::NotReady => return Ok(None),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
match self.parse_message(src)? {
|
|
||||||
Async::Ready((msg, decoder)) => {
|
|
||||||
self.decoder = decoder;
|
|
||||||
if self.decoder.is_some() {
|
|
||||||
Ok(Some(InMessage::MessageWithPayload(msg)))
|
|
||||||
} else {
|
|
||||||
Ok(Some(InMessage::Message(msg)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Async::NotReady => {
|
|
||||||
if src.len() >= MAX_BUFFER_SIZE {
|
|
||||||
error!("MAX_BUFFER_SIZE unprocessed data reached, closing");
|
|
||||||
Err(ParseError::TooLarge)
|
|
||||||
} else {
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse_message(
|
|
||||||
&self, buf: &mut BytesMut,
|
|
||||||
) -> Poll<(Request, Option<EncodingDecoder>), ParseError> {
|
|
||||||
// Parse http message
|
// Parse http message
|
||||||
let mut has_upgrade = false;
|
let mut has_upgrade = false;
|
||||||
let mut chunked = false;
|
let mut chunked = false;
|
||||||
@ -98,7 +49,7 @@ impl H1Decoder {
|
|||||||
unsafe { mem::uninitialized() };
|
unsafe { mem::uninitialized() };
|
||||||
|
|
||||||
let mut req = httparse::Request::new(&mut parsed);
|
let mut req = httparse::Request::new(&mut parsed);
|
||||||
match req.parse(buf)? {
|
match req.parse(src)? {
|
||||||
httparse::Status::Complete(len) => {
|
httparse::Status::Complete(len) => {
|
||||||
let method = Method::from_bytes(req.method.unwrap().as_bytes())
|
let method = Method::from_bytes(req.method.unwrap().as_bytes())
|
||||||
.map_err(|_| ParseError::Method)?;
|
.map_err(|_| ParseError::Method)?;
|
||||||
@ -108,18 +59,18 @@ impl H1Decoder {
|
|||||||
} else {
|
} else {
|
||||||
Version::HTTP_10
|
Version::HTTP_10
|
||||||
};
|
};
|
||||||
HeaderIndex::record(buf, req.headers, &mut headers);
|
HeaderIndex::record(src, req.headers, &mut headers);
|
||||||
|
|
||||||
(len, method, path, version, req.headers.len())
|
(len, method, path, version, req.headers.len())
|
||||||
}
|
}
|
||||||
httparse::Status::Partial => return Ok(Async::NotReady),
|
httparse::Status::Partial => return Ok(None),
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let slice = buf.split_to(len).freeze();
|
let slice = src.split_to(len).freeze();
|
||||||
|
|
||||||
// convert headers
|
// convert headers
|
||||||
let mut msg = RequestPool::get(self.pool);
|
let mut msg = RequestPool::get(self.0);
|
||||||
{
|
{
|
||||||
let inner = msg.inner_mut();
|
let inner = msg.inner_mut();
|
||||||
inner
|
inner
|
||||||
@ -198,18 +149,21 @@ impl H1Decoder {
|
|||||||
// https://tools.ietf.org/html/rfc7230#section-3.3.3
|
// https://tools.ietf.org/html/rfc7230#section-3.3.3
|
||||||
let decoder = if chunked {
|
let decoder = if chunked {
|
||||||
// Chunked encoding
|
// Chunked encoding
|
||||||
Some(EncodingDecoder::chunked())
|
Some(PayloadDecoder::chunked())
|
||||||
} else if let Some(len) = content_length {
|
} else if let Some(len) = content_length {
|
||||||
// Content-Length
|
// Content-Length
|
||||||
Some(EncodingDecoder::length(len))
|
Some(PayloadDecoder::length(len))
|
||||||
} else if has_upgrade || msg.inner.method == Method::CONNECT {
|
} else if has_upgrade || msg.inner.method == Method::CONNECT {
|
||||||
// upgrade(websocket) or connect
|
// upgrade(websocket) or connect
|
||||||
Some(EncodingDecoder::eof())
|
Some(PayloadDecoder::eof())
|
||||||
|
} else if src.len() >= MAX_BUFFER_SIZE {
|
||||||
|
error!("MAX_BUFFER_SIZE unprocessed data reached, closing");
|
||||||
|
return Err(ParseError::TooLarge);
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(Async::Ready((msg, decoder)))
|
Ok(Some((msg, decoder)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -235,30 +189,37 @@ impl HeaderIndex {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
/// Http payload item
|
||||||
|
pub enum PayloadItem {
|
||||||
|
Chunk(Bytes),
|
||||||
|
Eof,
|
||||||
|
}
|
||||||
|
|
||||||
/// Decoders to handle different Transfer-Encodings.
|
/// Decoders to handle different Transfer-Encodings.
|
||||||
///
|
///
|
||||||
/// If a message body does not include a Transfer-Encoding, it *should*
|
/// If a message body does not include a Transfer-Encoding, it *should*
|
||||||
/// include a Content-Length header.
|
/// include a Content-Length header.
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub struct EncodingDecoder {
|
pub struct PayloadDecoder {
|
||||||
kind: Kind,
|
kind: Kind,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EncodingDecoder {
|
impl PayloadDecoder {
|
||||||
pub fn length(x: u64) -> EncodingDecoder {
|
pub fn length(x: u64) -> PayloadDecoder {
|
||||||
EncodingDecoder {
|
PayloadDecoder {
|
||||||
kind: Kind::Length(x),
|
kind: Kind::Length(x),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn chunked() -> EncodingDecoder {
|
pub fn chunked() -> PayloadDecoder {
|
||||||
EncodingDecoder {
|
PayloadDecoder {
|
||||||
kind: Kind::Chunked(ChunkedState::Size, 0),
|
kind: Kind::Chunked(ChunkedState::Size, 0),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn eof() -> EncodingDecoder {
|
pub fn eof() -> PayloadDecoder {
|
||||||
EncodingDecoder {
|
PayloadDecoder {
|
||||||
kind: Kind::Eof(false),
|
kind: Kind::Eof(false),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -302,53 +263,59 @@ enum ChunkedState {
|
|||||||
End,
|
End,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EncodingDecoder {
|
impl Decoder for PayloadDecoder {
|
||||||
pub fn decode(&mut self, body: &mut BytesMut) -> Poll<Option<Bytes>, io::Error> {
|
type Item = PayloadItem;
|
||||||
|
type Error = io::Error;
|
||||||
|
|
||||||
|
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
|
||||||
match self.kind {
|
match self.kind {
|
||||||
Kind::Length(ref mut remaining) => {
|
Kind::Length(ref mut remaining) => {
|
||||||
if *remaining == 0 {
|
if *remaining == 0 {
|
||||||
Ok(Async::Ready(None))
|
Ok(Some(PayloadItem::Eof))
|
||||||
} else {
|
} else {
|
||||||
if body.is_empty() {
|
if src.is_empty() {
|
||||||
return Ok(Async::NotReady);
|
return Ok(None);
|
||||||
}
|
}
|
||||||
let len = body.len() as u64;
|
let len = src.len() as u64;
|
||||||
let buf;
|
let buf;
|
||||||
if *remaining > len {
|
if *remaining > len {
|
||||||
buf = body.take().freeze();
|
buf = src.take().freeze();
|
||||||
*remaining -= len;
|
*remaining -= len;
|
||||||
} else {
|
} else {
|
||||||
buf = body.split_to(*remaining as usize).freeze();
|
buf = src.split_to(*remaining as usize).freeze();
|
||||||
*remaining = 0;
|
*remaining = 0;
|
||||||
}
|
};
|
||||||
trace!("Length read: {}", buf.len());
|
trace!("Length read: {}", buf.len());
|
||||||
Ok(Async::Ready(Some(buf)))
|
Ok(Some(PayloadItem::Chunk(buf)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Kind::Chunked(ref mut state, ref mut size) => {
|
Kind::Chunked(ref mut state, ref mut size) => {
|
||||||
loop {
|
loop {
|
||||||
let mut buf = None;
|
let mut buf = None;
|
||||||
// advances the chunked state
|
// advances the chunked state
|
||||||
*state = try_ready!(state.step(body, size, &mut buf));
|
*state = match state.step(src, size, &mut buf)? {
|
||||||
|
Async::NotReady => return Ok(None),
|
||||||
|
Async::Ready(state) => state,
|
||||||
|
};
|
||||||
if *state == ChunkedState::End {
|
if *state == ChunkedState::End {
|
||||||
trace!("End of chunked stream");
|
trace!("End of chunked stream");
|
||||||
return Ok(Async::Ready(None));
|
return Ok(Some(PayloadItem::Eof));
|
||||||
}
|
}
|
||||||
if let Some(buf) = buf {
|
if let Some(buf) = buf {
|
||||||
return Ok(Async::Ready(Some(buf)));
|
return Ok(Some(PayloadItem::Chunk(buf)));
|
||||||
}
|
}
|
||||||
if body.is_empty() {
|
if src.is_empty() {
|
||||||
return Ok(Async::NotReady);
|
return Ok(None);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Kind::Eof(ref mut is_eof) => {
|
Kind::Eof(ref mut is_eof) => {
|
||||||
if *is_eof {
|
if *is_eof {
|
||||||
Ok(Async::Ready(None))
|
Ok(Some(PayloadItem::Eof))
|
||||||
} else if !body.is_empty() {
|
} else if !src.is_empty() {
|
||||||
Ok(Async::Ready(Some(body.take().freeze())))
|
Ok(Some(PayloadItem::Chunk(src.take().freeze())))
|
||||||
} else {
|
} else {
|
||||||
Ok(Async::NotReady)
|
Ok(None)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -536,15 +503,18 @@ mod tests {
|
|||||||
_ => panic!("error"),
|
_ => panic!("error"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PayloadItem {
|
||||||
fn chunk(self) -> Bytes {
|
fn chunk(self) -> Bytes {
|
||||||
match self {
|
match self {
|
||||||
InMessage::Chunk(chunk) => chunk,
|
PayloadItem::Chunk(chunk) => chunk,
|
||||||
_ => panic!("error"),
|
_ => panic!("error"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn eof(&self) -> bool {
|
fn eof(&self) -> bool {
|
||||||
match *self {
|
match *self {
|
||||||
InMessage::Eof => true,
|
PayloadItem::Eof => true,
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -552,8 +522,8 @@ mod tests {
|
|||||||
|
|
||||||
macro_rules! parse_ready {
|
macro_rules! parse_ready {
|
||||||
($e:expr) => {{
|
($e:expr) => {{
|
||||||
match H1Decoder::new().decode($e) {
|
match RequestDecoder::default().decode($e) {
|
||||||
Ok(Some(msg)) => msg.message(),
|
Ok(Some((msg, _))) => msg,
|
||||||
Ok(_) => unreachable!("Eof during parsing http request"),
|
Ok(_) => unreachable!("Eof during parsing http request"),
|
||||||
Err(err) => unreachable!("Error during parsing http request: {:?}", err),
|
Err(err) => unreachable!("Error during parsing http request: {:?}", err),
|
||||||
}
|
}
|
||||||
@ -562,7 +532,7 @@ mod tests {
|
|||||||
|
|
||||||
macro_rules! expect_parse_err {
|
macro_rules! expect_parse_err {
|
||||||
($e:expr) => {{
|
($e:expr) => {{
|
||||||
match H1Decoder::new().decode($e) {
|
match RequestDecoder::default().decode($e) {
|
||||||
Err(err) => match err {
|
Err(err) => match err {
|
||||||
ParseError::Io(_) => unreachable!("Parse error expected"),
|
ParseError::Io(_) => unreachable!("Parse error expected"),
|
||||||
_ => (),
|
_ => (),
|
||||||
@ -641,10 +611,9 @@ mod tests {
|
|||||||
fn test_parse() {
|
fn test_parse() {
|
||||||
let mut buf = BytesMut::from("GET /test HTTP/1.1\r\n\r\n");
|
let mut buf = BytesMut::from("GET /test HTTP/1.1\r\n\r\n");
|
||||||
|
|
||||||
let mut reader = H1Decoder::new();
|
let mut reader = RequestDecoder::default();
|
||||||
match reader.decode(&mut buf) {
|
match reader.decode(&mut buf) {
|
||||||
Ok(Some(msg)) => {
|
Ok(Some((req, _))) => {
|
||||||
let req = msg.message();
|
|
||||||
assert_eq!(req.version(), Version::HTTP_11);
|
assert_eq!(req.version(), Version::HTTP_11);
|
||||||
assert_eq!(*req.method(), Method::GET);
|
assert_eq!(*req.method(), Method::GET);
|
||||||
assert_eq!(req.path(), "/test");
|
assert_eq!(req.path(), "/test");
|
||||||
@ -657,105 +626,79 @@ mod tests {
|
|||||||
fn test_parse_partial() {
|
fn test_parse_partial() {
|
||||||
let mut buf = BytesMut::from("PUT /test HTTP/1");
|
let mut buf = BytesMut::from("PUT /test HTTP/1");
|
||||||
|
|
||||||
let mut reader = H1Decoder::new();
|
let mut reader = RequestDecoder::default();
|
||||||
match reader.decode(&mut buf) {
|
assert!(reader.decode(&mut buf).unwrap().is_none());
|
||||||
Ok(None) => (),
|
|
||||||
_ => unreachable!("Error"),
|
|
||||||
}
|
|
||||||
|
|
||||||
buf.extend(b".1\r\n\r\n");
|
buf.extend(b".1\r\n\r\n");
|
||||||
match reader.decode(&mut buf) {
|
let (req, _) = reader.decode(&mut buf).unwrap().unwrap();
|
||||||
Ok(Some(msg)) => {
|
|
||||||
let mut req = msg.message();
|
|
||||||
assert_eq!(req.version(), Version::HTTP_11);
|
assert_eq!(req.version(), Version::HTTP_11);
|
||||||
assert_eq!(*req.method(), Method::PUT);
|
assert_eq!(*req.method(), Method::PUT);
|
||||||
assert_eq!(req.path(), "/test");
|
assert_eq!(req.path(), "/test");
|
||||||
}
|
}
|
||||||
Ok(_) | Err(_) => unreachable!("Error during parsing http request"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parse_post() {
|
fn test_parse_post() {
|
||||||
let mut buf = BytesMut::from("POST /test2 HTTP/1.0\r\n\r\n");
|
let mut buf = BytesMut::from("POST /test2 HTTP/1.0\r\n\r\n");
|
||||||
|
|
||||||
let mut reader = H1Decoder::new();
|
let mut reader = RequestDecoder::default();
|
||||||
match reader.decode(&mut buf) {
|
let (req, _) = reader.decode(&mut buf).unwrap().unwrap();
|
||||||
Ok(Some(msg)) => {
|
|
||||||
let mut req = msg.message();
|
|
||||||
assert_eq!(req.version(), Version::HTTP_10);
|
assert_eq!(req.version(), Version::HTTP_10);
|
||||||
assert_eq!(*req.method(), Method::POST);
|
assert_eq!(*req.method(), Method::POST);
|
||||||
assert_eq!(req.path(), "/test2");
|
assert_eq!(req.path(), "/test2");
|
||||||
}
|
}
|
||||||
Ok(_) | Err(_) => unreachable!("Error during parsing http request"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parse_body() {
|
fn test_parse_body() {
|
||||||
let mut buf =
|
let mut buf =
|
||||||
BytesMut::from("GET /test HTTP/1.1\r\nContent-Length: 4\r\n\r\nbody");
|
BytesMut::from("GET /test HTTP/1.1\r\nContent-Length: 4\r\n\r\nbody");
|
||||||
|
|
||||||
let mut reader = H1Decoder::new();
|
let mut reader = RequestDecoder::default();
|
||||||
match reader.decode(&mut buf) {
|
let (req, pl) = reader.decode(&mut buf).unwrap().unwrap();
|
||||||
Ok(Some(msg)) => {
|
let mut pl = pl.unwrap();
|
||||||
let mut req = msg.message();
|
|
||||||
assert_eq!(req.version(), Version::HTTP_11);
|
assert_eq!(req.version(), Version::HTTP_11);
|
||||||
assert_eq!(*req.method(), Method::GET);
|
assert_eq!(*req.method(), Method::GET);
|
||||||
assert_eq!(req.path(), "/test");
|
assert_eq!(req.path(), "/test");
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
reader.decode(&mut buf).unwrap().unwrap().chunk().as_ref(),
|
pl.decode(&mut buf).unwrap().unwrap().chunk().as_ref(),
|
||||||
b"body"
|
b"body"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
Ok(_) | Err(_) => unreachable!("Error during parsing http request"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parse_body_crlf() {
|
fn test_parse_body_crlf() {
|
||||||
let mut buf =
|
let mut buf =
|
||||||
BytesMut::from("\r\nGET /test HTTP/1.1\r\nContent-Length: 4\r\n\r\nbody");
|
BytesMut::from("\r\nGET /test HTTP/1.1\r\nContent-Length: 4\r\n\r\nbody");
|
||||||
|
|
||||||
let mut reader = H1Decoder::new();
|
let mut reader = RequestDecoder::default();
|
||||||
match reader.decode(&mut buf) {
|
let (req, pl) = reader.decode(&mut buf).unwrap().unwrap();
|
||||||
Ok(Some(msg)) => {
|
let mut pl = pl.unwrap();
|
||||||
let mut req = msg.message();
|
|
||||||
assert_eq!(req.version(), Version::HTTP_11);
|
assert_eq!(req.version(), Version::HTTP_11);
|
||||||
assert_eq!(*req.method(), Method::GET);
|
assert_eq!(*req.method(), Method::GET);
|
||||||
assert_eq!(req.path(), "/test");
|
assert_eq!(req.path(), "/test");
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
reader.decode(&mut buf).unwrap().unwrap().chunk().as_ref(),
|
pl.decode(&mut buf).unwrap().unwrap().chunk().as_ref(),
|
||||||
b"body"
|
b"body"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
Ok(_) | Err(_) => unreachable!("Error during parsing http request"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parse_partial_eof() {
|
fn test_parse_partial_eof() {
|
||||||
let mut buf = BytesMut::from("GET /test HTTP/1.1\r\n");
|
let mut buf = BytesMut::from("GET /test HTTP/1.1\r\n");
|
||||||
let mut reader = H1Decoder::new();
|
let mut reader = RequestDecoder::default();
|
||||||
assert!(reader.decode(&mut buf).unwrap().is_none());
|
assert!(reader.decode(&mut buf).unwrap().is_none());
|
||||||
|
|
||||||
buf.extend(b"\r\n");
|
buf.extend(b"\r\n");
|
||||||
match reader.decode(&mut buf) {
|
let (req, _) = reader.decode(&mut buf).unwrap().unwrap();
|
||||||
Ok(Some(msg)) => {
|
|
||||||
let req = msg.message();
|
|
||||||
assert_eq!(req.version(), Version::HTTP_11);
|
assert_eq!(req.version(), Version::HTTP_11);
|
||||||
assert_eq!(*req.method(), Method::GET);
|
assert_eq!(*req.method(), Method::GET);
|
||||||
assert_eq!(req.path(), "/test");
|
assert_eq!(req.path(), "/test");
|
||||||
}
|
}
|
||||||
Ok(_) | Err(_) => unreachable!("Error during parsing http request"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_headers_split_field() {
|
fn test_headers_split_field() {
|
||||||
let mut buf = BytesMut::from("GET /test HTTP/1.1\r\n");
|
let mut buf = BytesMut::from("GET /test HTTP/1.1\r\n");
|
||||||
|
|
||||||
let mut reader = H1Decoder::new();
|
let mut reader = RequestDecoder::default();
|
||||||
assert!{ reader.decode(&mut buf).unwrap().is_none() }
|
assert!{ reader.decode(&mut buf).unwrap().is_none() }
|
||||||
|
|
||||||
buf.extend(b"t");
|
buf.extend(b"t");
|
||||||
@ -765,17 +708,12 @@ mod tests {
|
|||||||
assert!{ reader.decode(&mut buf).unwrap().is_none() }
|
assert!{ reader.decode(&mut buf).unwrap().is_none() }
|
||||||
|
|
||||||
buf.extend(b"t: value\r\n\r\n");
|
buf.extend(b"t: value\r\n\r\n");
|
||||||
match reader.decode(&mut buf) {
|
let (req, _) = reader.decode(&mut buf).unwrap().unwrap();
|
||||||
Ok(Some(msg)) => {
|
|
||||||
let req = msg.message();
|
|
||||||
assert_eq!(req.version(), Version::HTTP_11);
|
assert_eq!(req.version(), Version::HTTP_11);
|
||||||
assert_eq!(*req.method(), Method::GET);
|
assert_eq!(*req.method(), Method::GET);
|
||||||
assert_eq!(req.path(), "/test");
|
assert_eq!(req.path(), "/test");
|
||||||
assert_eq!(req.headers().get("test").unwrap().as_bytes(), b"value");
|
assert_eq!(req.headers().get("test").unwrap().as_bytes(), b"value");
|
||||||
}
|
}
|
||||||
Ok(_) | Err(_) => unreachable!("Error during parsing http request"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_headers_multi_value() {
|
fn test_headers_multi_value() {
|
||||||
@ -784,9 +722,8 @@ mod tests {
|
|||||||
Set-Cookie: c1=cookie1\r\n\
|
Set-Cookie: c1=cookie1\r\n\
|
||||||
Set-Cookie: c2=cookie2\r\n\r\n",
|
Set-Cookie: c2=cookie2\r\n\r\n",
|
||||||
);
|
);
|
||||||
let mut reader = H1Decoder::new();
|
let mut reader = RequestDecoder::default();
|
||||||
let msg = reader.decode(&mut buf).unwrap().unwrap();
|
let (req, _) = reader.decode(&mut buf).unwrap().unwrap();
|
||||||
let req = msg.message();
|
|
||||||
|
|
||||||
let val: Vec<_> = req
|
let val: Vec<_> = req
|
||||||
.headers()
|
.headers()
|
||||||
@ -985,14 +922,13 @@ mod tests {
|
|||||||
upgrade: websocket\r\n\r\n\
|
upgrade: websocket\r\n\r\n\
|
||||||
some raw data",
|
some raw data",
|
||||||
);
|
);
|
||||||
let mut reader = H1Decoder::new();
|
let mut reader = RequestDecoder::default();
|
||||||
let msg = reader.decode(&mut buf).unwrap().unwrap();
|
let (req, pl) = reader.decode(&mut buf).unwrap().unwrap();
|
||||||
assert!(msg.is_payload());
|
let mut pl = pl.unwrap();
|
||||||
let req = msg.message();
|
|
||||||
assert!(!req.keep_alive());
|
assert!(!req.keep_alive());
|
||||||
assert!(req.upgrade());
|
assert!(req.upgrade());
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
reader.decode(&mut buf).unwrap().unwrap().chunk().as_ref(),
|
pl.decode(&mut buf).unwrap().unwrap().chunk().as_ref(),
|
||||||
b"some raw data"
|
b"some raw data"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -1039,22 +975,21 @@ mod tests {
|
|||||||
"GET /test HTTP/1.1\r\n\
|
"GET /test HTTP/1.1\r\n\
|
||||||
transfer-encoding: chunked\r\n\r\n",
|
transfer-encoding: chunked\r\n\r\n",
|
||||||
);
|
);
|
||||||
let mut reader = H1Decoder::new();
|
let mut reader = RequestDecoder::default();
|
||||||
let msg = reader.decode(&mut buf).unwrap().unwrap();
|
let (req, pl) = reader.decode(&mut buf).unwrap().unwrap();
|
||||||
assert!(msg.is_payload());
|
let mut pl = pl.unwrap();
|
||||||
let req = msg.message();
|
|
||||||
assert!(req.chunked().unwrap());
|
assert!(req.chunked().unwrap());
|
||||||
|
|
||||||
buf.extend(b"4\r\ndata\r\n4\r\nline\r\n0\r\n\r\n");
|
buf.extend(b"4\r\ndata\r\n4\r\nline\r\n0\r\n\r\n");
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
reader.decode(&mut buf).unwrap().unwrap().chunk().as_ref(),
|
pl.decode(&mut buf).unwrap().unwrap().chunk().as_ref(),
|
||||||
b"data"
|
b"data"
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
reader.decode(&mut buf).unwrap().unwrap().chunk().as_ref(),
|
pl.decode(&mut buf).unwrap().unwrap().chunk().as_ref(),
|
||||||
b"line"
|
b"line"
|
||||||
);
|
);
|
||||||
assert!(reader.decode(&mut buf).unwrap().unwrap().eof());
|
assert!(pl.decode(&mut buf).unwrap().unwrap().eof());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -1063,10 +998,9 @@ mod tests {
|
|||||||
"GET /test HTTP/1.1\r\n\
|
"GET /test HTTP/1.1\r\n\
|
||||||
transfer-encoding: chunked\r\n\r\n",
|
transfer-encoding: chunked\r\n\r\n",
|
||||||
);
|
);
|
||||||
let mut reader = H1Decoder::new();
|
let mut reader = RequestDecoder::default();
|
||||||
let msg = reader.decode(&mut buf).unwrap().unwrap();
|
let (req, pl) = reader.decode(&mut buf).unwrap().unwrap();
|
||||||
assert!(msg.is_payload());
|
let mut pl = pl.unwrap();
|
||||||
let req = msg.message();
|
|
||||||
assert!(req.chunked().unwrap());
|
assert!(req.chunked().unwrap());
|
||||||
|
|
||||||
buf.extend(
|
buf.extend(
|
||||||
@ -1075,19 +1009,17 @@ mod tests {
|
|||||||
transfer-encoding: chunked\r\n\r\n"
|
transfer-encoding: chunked\r\n\r\n"
|
||||||
.iter(),
|
.iter(),
|
||||||
);
|
);
|
||||||
let msg = reader.decode(&mut buf).unwrap().unwrap();
|
let msg = pl.decode(&mut buf).unwrap().unwrap();
|
||||||
assert_eq!(msg.chunk().as_ref(), b"data");
|
assert_eq!(msg.chunk().as_ref(), b"data");
|
||||||
let msg = reader.decode(&mut buf).unwrap().unwrap();
|
let msg = pl.decode(&mut buf).unwrap().unwrap();
|
||||||
assert_eq!(msg.chunk().as_ref(), b"line");
|
assert_eq!(msg.chunk().as_ref(), b"line");
|
||||||
let msg = reader.decode(&mut buf).unwrap().unwrap();
|
let msg = pl.decode(&mut buf).unwrap().unwrap();
|
||||||
assert!(msg.eof());
|
assert!(msg.eof());
|
||||||
|
|
||||||
let msg = reader.decode(&mut buf).unwrap().unwrap();
|
let (req, _) = reader.decode(&mut buf).unwrap().unwrap();
|
||||||
assert!(msg.is_payload());
|
assert!(req.chunked().unwrap());
|
||||||
let req2 = msg.message();
|
assert_eq!(*req.method(), Method::POST);
|
||||||
assert!(req2.chunked().unwrap());
|
assert!(req.chunked().unwrap());
|
||||||
assert_eq!(*req2.method(), Method::POST);
|
|
||||||
assert!(req2.chunked().unwrap());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -1097,30 +1029,29 @@ mod tests {
|
|||||||
transfer-encoding: chunked\r\n\r\n",
|
transfer-encoding: chunked\r\n\r\n",
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut reader = H1Decoder::new();
|
let mut reader = RequestDecoder::default();
|
||||||
let msg = reader.decode(&mut buf).unwrap().unwrap();
|
let (req, pl) = reader.decode(&mut buf).unwrap().unwrap();
|
||||||
assert!(msg.is_payload());
|
let mut pl = pl.unwrap();
|
||||||
let req = msg.message();
|
|
||||||
assert!(req.chunked().unwrap());
|
assert!(req.chunked().unwrap());
|
||||||
|
|
||||||
buf.extend(b"4\r\n1111\r\n");
|
buf.extend(b"4\r\n1111\r\n");
|
||||||
let msg = reader.decode(&mut buf).unwrap().unwrap();
|
let msg = pl.decode(&mut buf).unwrap().unwrap();
|
||||||
assert_eq!(msg.chunk().as_ref(), b"1111");
|
assert_eq!(msg.chunk().as_ref(), b"1111");
|
||||||
|
|
||||||
buf.extend(b"4\r\ndata\r");
|
buf.extend(b"4\r\ndata\r");
|
||||||
let msg = reader.decode(&mut buf).unwrap().unwrap();
|
let msg = pl.decode(&mut buf).unwrap().unwrap();
|
||||||
assert_eq!(msg.chunk().as_ref(), b"data");
|
assert_eq!(msg.chunk().as_ref(), b"data");
|
||||||
|
|
||||||
buf.extend(b"\n4");
|
buf.extend(b"\n4");
|
||||||
assert!(reader.decode(&mut buf).unwrap().is_none());
|
assert!(pl.decode(&mut buf).unwrap().is_none());
|
||||||
|
|
||||||
buf.extend(b"\r");
|
buf.extend(b"\r");
|
||||||
assert!(reader.decode(&mut buf).unwrap().is_none());
|
assert!(pl.decode(&mut buf).unwrap().is_none());
|
||||||
buf.extend(b"\n");
|
buf.extend(b"\n");
|
||||||
assert!(reader.decode(&mut buf).unwrap().is_none());
|
assert!(pl.decode(&mut buf).unwrap().is_none());
|
||||||
|
|
||||||
buf.extend(b"li");
|
buf.extend(b"li");
|
||||||
let msg = reader.decode(&mut buf).unwrap().unwrap();
|
let msg = pl.decode(&mut buf).unwrap().unwrap();
|
||||||
assert_eq!(msg.chunk().as_ref(), b"li");
|
assert_eq!(msg.chunk().as_ref(), b"li");
|
||||||
|
|
||||||
//trailers
|
//trailers
|
||||||
@ -1128,12 +1059,12 @@ mod tests {
|
|||||||
//not_ready!(reader.parse(&mut buf, &mut readbuf));
|
//not_ready!(reader.parse(&mut buf, &mut readbuf));
|
||||||
|
|
||||||
buf.extend(b"ne\r\n0\r\n");
|
buf.extend(b"ne\r\n0\r\n");
|
||||||
let msg = reader.decode(&mut buf).unwrap().unwrap();
|
let msg = pl.decode(&mut buf).unwrap().unwrap();
|
||||||
assert_eq!(msg.chunk().as_ref(), b"ne");
|
assert_eq!(msg.chunk().as_ref(), b"ne");
|
||||||
assert!(reader.decode(&mut buf).unwrap().is_none());
|
assert!(pl.decode(&mut buf).unwrap().is_none());
|
||||||
|
|
||||||
buf.extend(b"\r\n");
|
buf.extend(b"\r\n");
|
||||||
assert!(reader.decode(&mut buf).unwrap().unwrap().eof());
|
assert!(pl.decode(&mut buf).unwrap().unwrap().eof());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -1143,17 +1074,17 @@ mod tests {
|
|||||||
transfer-encoding: chunked\r\n\r\n"[..],
|
transfer-encoding: chunked\r\n\r\n"[..],
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut reader = H1Decoder::new();
|
let mut reader = RequestDecoder::default();
|
||||||
let msg = reader.decode(&mut buf).unwrap().unwrap();
|
let (msg, pl) = reader.decode(&mut buf).unwrap().unwrap();
|
||||||
assert!(msg.is_payload());
|
let mut pl = pl.unwrap();
|
||||||
assert!(msg.message().chunked().unwrap());
|
assert!(msg.chunked().unwrap());
|
||||||
|
|
||||||
buf.extend(b"4;test\r\ndata\r\n4\r\nline\r\n0\r\n\r\n"); // test: test\r\n\r\n")
|
buf.extend(b"4;test\r\ndata\r\n4\r\nline\r\n0\r\n\r\n"); // test: test\r\n\r\n")
|
||||||
let chunk = reader.decode(&mut buf).unwrap().unwrap().chunk();
|
let chunk = pl.decode(&mut buf).unwrap().unwrap().chunk();
|
||||||
assert_eq!(chunk, Bytes::from_static(b"data"));
|
assert_eq!(chunk, Bytes::from_static(b"data"));
|
||||||
let chunk = reader.decode(&mut buf).unwrap().unwrap().chunk();
|
let chunk = pl.decode(&mut buf).unwrap().unwrap().chunk();
|
||||||
assert_eq!(chunk, Bytes::from_static(b"line"));
|
assert_eq!(chunk, Bytes::from_static(b"line"));
|
||||||
let msg = reader.decode(&mut buf).unwrap().unwrap();
|
let msg = pl.decode(&mut buf).unwrap().unwrap();
|
||||||
assert!(msg.eof());
|
assert!(msg.eof());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user