1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-25 08:52:42 +01:00
actix-web/src/server/h1.rs

1158 lines
37 KiB
Rust
Raw Normal View History

2018-04-14 01:02:01 +02:00
use std::collections::VecDeque;
2018-04-29 18:09:08 +02:00
use std::io;
use std::net::SocketAddr;
2018-04-14 01:02:01 +02:00
use std::rc::Rc;
use std::time::Duration;
2017-10-07 06:48:14 +02:00
use actix::Arbiter;
2018-04-29 07:20:32 +02:00
use bytes::{BufMut, BytesMut};
2018-04-14 01:02:01 +02:00
use futures::{Async, Future, Poll};
use tokio_core::reactor::Timeout;
2017-10-07 06:48:14 +02:00
2018-04-29 07:20:32 +02:00
use error::PayloadError;
use httprequest::HttpRequest;
use httpresponse::HttpResponse;
2018-04-14 01:02:01 +02:00
use payload::{Payload, PayloadStatus, PayloadWriter};
use pipeline::Pipeline;
2017-10-07 06:48:14 +02:00
2018-01-12 03:41:33 +01:00
use super::encoding::PayloadType;
2018-04-29 07:20:32 +02:00
use super::h1decoder::{DecoderError, H1Decoder, Message};
2018-04-14 01:02:01 +02:00
use super::h1writer::H1Writer;
2018-01-12 03:35:05 +01:00
use super::settings::WorkerSettings;
2018-04-29 07:20:32 +02:00
use super::Writer;
2018-01-12 03:35:05 +01:00
use super::{HttpHandler, HttpHandlerTask, IoStream};
const MAX_PIPELINED_MESSAGES: usize = 16;
2018-04-29 07:20:32 +02:00
const LW_BUFFER_SIZE: usize = 4096;
const HW_BUFFER_SIZE: usize = 32_768;
2017-10-07 06:48:14 +02:00
bitflags! {
struct Flags: u8 {
2018-03-13 00:16:17 +01:00
const STARTED = 0b0000_0001;
const ERROR = 0b0000_0010;
const KEEPALIVE = 0b0000_0100;
2018-03-13 00:16:17 +01:00
const SHUTDOWN = 0b0000_1000;
2018-04-29 07:20:32 +02:00
const DISCONNECTED = 0b0001_0000;
}
}
bitflags! {
struct EntryFlags: u8 {
const EOF = 0b0000_0001;
const ERROR = 0b0000_0010;
const FINISHED = 0b0000_0100;
}
}
pub(crate) struct Http1<T: IoStream, H: 'static> {
flags: Flags,
2017-12-14 06:38:47 +01:00
settings: Rc<WorkerSettings<H>>,
addr: Option<SocketAddr>,
2018-03-18 19:05:44 +01:00
stream: H1Writer<T, H>,
2018-04-29 07:20:32 +02:00
decoder: H1Decoder,
payload: Option<PayloadType>,
buf: BytesMut,
tasks: VecDeque<Entry>,
keepalive_timer: Option<Timeout>,
2017-10-07 06:48:14 +02:00
}
struct Entry {
2017-12-09 13:33:40 +01:00
pipe: Box<HttpHandlerTask>,
flags: EntryFlags,
}
impl<T, H> Http1<T, H>
2018-04-14 01:02:01 +02:00
where
T: IoStream,
H: HttpHandler + 'static,
{
2018-04-14 01:02:01 +02:00
pub fn new(
2018-05-15 16:55:36 +02:00
settings: Rc<WorkerSettings<H>>,
stream: T,
addr: Option<SocketAddr>,
2018-04-29 07:20:32 +02:00
buf: BytesMut,
2018-04-14 01:02:01 +02:00
) -> Self {
2018-02-26 23:33:56 +01:00
let bytes = settings.get_shared_bytes();
2018-04-14 01:02:01 +02:00
Http1 {
flags: Flags::KEEPALIVE,
stream: H1Writer::new(stream, bytes, Rc::clone(&settings)),
2018-04-29 07:20:32 +02:00
decoder: H1Decoder::new(),
payload: None,
2018-04-14 01:02:01 +02:00
tasks: VecDeque::new(),
keepalive_timer: None,
addr,
2018-04-29 07:20:32 +02:00
buf,
2018-04-14 01:02:01 +02:00
settings,
2018-02-26 23:33:56 +01:00
}
}
2018-04-29 07:20:32 +02:00
#[inline]
2017-12-29 01:25:47 +01:00
pub fn settings(&self) -> &WorkerSettings<H> {
self.settings.as_ref()
}
2018-04-29 07:20:32 +02:00
#[inline]
2018-01-04 07:43:44 +01:00
pub(crate) fn io(&mut self) -> &mut T {
self.stream.get_mut()
}
2018-04-29 07:20:32 +02:00
#[inline]
fn can_read(&self) -> bool {
if let Some(ref info) = self.payload {
info.need_read() == PayloadStatus::Read
} else {
true
}
}
#[inline]
pub fn poll(&mut self) -> Poll<(), ()> {
// keep-alive timer
2018-01-21 01:12:38 +01:00
if let Some(ref mut timer) = self.keepalive_timer {
match timer.poll() {
2017-11-09 04:31:25 +01:00
Ok(Async::Ready(_)) => {
trace!("Keep-alive timeout, close connection");
2018-03-13 00:16:17 +01:00
self.flags.insert(Flags::SHUTDOWN);
2017-11-09 04:31:25 +01:00
}
Ok(Async::NotReady) => (),
Err(_) => unreachable!(),
}
}
2018-03-13 00:16:17 +01:00
// shutdown
if self.flags.contains(Flags::SHUTDOWN) {
match self.stream.poll_completed(true) {
Ok(Async::NotReady) => return Ok(Async::NotReady),
Ok(Async::Ready(_)) => return Ok(Async::Ready(())),
Err(err) => {
debug!("Error sending data: {}", err);
2018-04-14 01:02:01 +02:00
return Err(());
2018-03-13 00:16:17 +01:00
}
}
}
2018-04-29 07:20:32 +02:00
self.poll_io();
2018-02-27 05:07:22 +01:00
loop {
2018-04-29 07:20:32 +02:00
match self.poll_handler()? {
Async::Ready(true) => {
self.poll_io();
}
2018-03-13 00:16:17 +01:00
Async::Ready(false) => {
self.flags.insert(Flags::SHUTDOWN);
2018-04-14 01:02:01 +02:00
return self.poll();
}
2018-02-27 05:07:22 +01:00
Async::NotReady => return Ok(Async::NotReady),
}
}
}
2018-04-29 07:20:32 +02:00
#[inline]
2018-05-16 01:41:46 +02:00
/// read data from stream
2018-04-29 07:20:32 +02:00
pub fn poll_io(&mut self) {
// read io from socket
if !self.flags.intersects(Flags::ERROR)
2018-05-15 16:55:36 +02:00
&& self.tasks.len() < MAX_PIPELINED_MESSAGES
&& self.can_read()
2018-02-10 07:26:48 +01:00
{
2018-04-29 18:09:08 +02:00
if self.read() {
// notify all tasks
self.stream.disconnected();
for entry in &mut self.tasks {
entry.pipe.disconnected()
}
// kill keepalive
self.keepalive_timer.take();
2018-04-29 18:09:08 +02:00
// on parse error, stop reading stream but tasks need to be
// completed
self.flags.insert(Flags::ERROR);
2018-02-10 07:26:48 +01:00
2018-05-07 05:05:31 +02:00
if let Some(mut payload) = self.payload.take() {
2018-04-29 18:09:08 +02:00
payload.set_error(PayloadError::Incomplete);
2018-04-29 07:20:32 +02:00
}
2018-04-29 18:09:08 +02:00
} else {
self.parse();
2018-02-10 07:26:48 +01:00
}
2018-04-29 07:20:32 +02:00
}
}
2018-02-10 07:26:48 +01:00
2018-04-29 07:20:32 +02:00
pub fn poll_handler(&mut self) -> Poll<bool, ()> {
let retry = self.can_read();
2018-02-27 05:07:22 +01:00
// check in-flight messages
let mut io = false;
let mut idx = 0;
while idx < self.tasks.len() {
2018-04-29 18:09:08 +02:00
let item: &mut Entry = unsafe { &mut *(&mut self.tasks[idx] as *mut _) };
2018-04-29 07:20:32 +02:00
// only one task can do io operation in http/1
if !io && !item.flags.contains(EntryFlags::EOF) {
// io is corrupted, send buffer
if item.flags.contains(EntryFlags::ERROR) {
if let Ok(Async::NotReady) = self.stream.poll_completed(true) {
2018-04-14 01:02:01 +02:00
return Ok(Async::NotReady);
}
2018-04-14 01:02:01 +02:00
return Err(());
}
match item.pipe.poll_io(&mut self.stream) {
Ok(Async::Ready(ready)) => {
// override keep-alive state
if self.stream.keepalive() {
self.flags.insert(Flags::KEEPALIVE);
} else {
self.flags.remove(Flags::KEEPALIVE);
}
// prepare stream for next response
self.stream.reset();
if ready {
2018-05-15 16:55:36 +02:00
item.flags.insert(EntryFlags::EOF | EntryFlags::FINISHED);
} else {
2018-05-16 01:41:46 +02:00
item.flags.insert(EntryFlags::EOF);
2018-02-27 05:07:22 +01:00
}
2018-04-14 01:02:01 +02:00
}
// no more IO for this iteration
Ok(Async::NotReady) => {
2018-04-29 07:20:32 +02:00
// check if previously read backpressure was enabled
if self.can_read() && !retry {
return Ok(Async::Ready(true));
}
io = true;
}
Err(err) => {
// it is not possible to recover from error
// during pipe handling, so just drop connection
error!("Unhandled error: {}", err);
item.flags.insert(EntryFlags::ERROR);
// check stream state, we still can have valid data in buffer
if let Ok(Async::NotReady) = self.stream.poll_completed(true) {
2018-04-14 01:02:01 +02:00
return Ok(Async::NotReady);
2017-11-25 18:28:25 +01:00
}
2018-04-14 01:02:01 +02:00
return Err(());
}
}
} else if !item.flags.contains(EntryFlags::FINISHED) {
match item.pipe.poll() {
Ok(Async::NotReady) => (),
Ok(Async::Ready(_)) => item.flags.insert(EntryFlags::FINISHED),
Err(err) => {
item.flags.insert(EntryFlags::ERROR);
error!("Unhandled error: {}", err);
}
}
}
idx += 1;
}
// cleanup finished tasks
2018-04-29 07:20:32 +02:00
let max = self.tasks.len() >= MAX_PIPELINED_MESSAGES;
while !self.tasks.is_empty() {
2018-04-29 18:09:08 +02:00
if self.tasks[0]
.flags
.contains(EntryFlags::EOF | EntryFlags::FINISHED)
{
self.tasks.pop_front();
} else {
2018-04-14 01:02:01 +02:00
break;
2018-02-10 07:26:48 +01:00
}
}
2018-04-29 07:20:32 +02:00
// read more message
if max && self.tasks.len() >= MAX_PIPELINED_MESSAGES {
return Ok(Async::Ready(true));
}
// check stream state
2018-03-13 00:16:17 +01:00
if self.flags.contains(Flags::STARTED) {
match self.stream.poll_completed(false) {
Ok(Async::NotReady) => return Ok(Async::NotReady),
Err(err) => {
debug!("Error sending data: {}", err);
2018-04-14 01:02:01 +02:00
return Err(());
2018-03-13 00:16:17 +01:00
}
_ => (),
2018-03-11 23:37:33 +01:00
}
}
// deal with keep-alive
if self.tasks.is_empty() {
2018-04-29 18:09:08 +02:00
// no keep-alive
2018-04-14 01:02:01 +02:00
if self.flags.contains(Flags::ERROR)
|| (!self.flags.contains(Flags::KEEPALIVE)
|| !self.settings.keep_alive_enabled())
&& self.flags.contains(Flags::STARTED)
{
2018-04-14 01:02:01 +02:00
return Ok(Async::Ready(false));
}
// start keep-alive timer
let keep_alive = self.settings.keep_alive();
if self.keepalive_timer.is_none() && keep_alive > 0 {
trace!("Start keep-alive timer");
2018-04-14 01:02:01 +02:00
let mut timer =
Timeout::new(Duration::new(keep_alive, 0), Arbiter::handle())
.unwrap();
// register timer
let _ = timer.poll();
self.keepalive_timer = Some(timer);
}
}
Ok(Async::NotReady)
}
2018-04-29 07:20:32 +02:00
pub fn parse(&mut self) {
'outer: loop {
match self.decoder.decode(&mut self.buf, &self.settings) {
2018-04-29 18:09:08 +02:00
Ok(Some(Message::Message { msg, payload })) => {
2018-04-29 07:20:32 +02:00
self.flags.insert(Flags::STARTED);
2018-03-18 19:05:44 +01:00
2018-04-29 07:20:32 +02:00
if payload {
let (ps, pl) = Payload::new(false);
msg.get_mut().payload = Some(pl);
self.payload =
Some(PayloadType::new(&msg.get_ref().headers, ps));
2018-02-27 05:07:22 +01:00
}
2018-02-10 01:20:10 +01:00
2018-04-29 07:20:32 +02:00
let mut req = HttpRequest::from_message(msg);
2018-02-27 05:07:22 +01:00
2018-04-29 07:20:32 +02:00
// set remote addr
req.set_peer_addr(self.addr);
2018-02-10 01:20:10 +01:00
2018-04-29 07:20:32 +02:00
// stop keepalive timer
self.keepalive_timer.take();
2018-04-29 07:20:32 +02:00
// search handler for request
for h in self.settings.handlers().iter_mut() {
req = match h.handle(req) {
2018-05-16 01:41:46 +02:00
Ok(mut pipe) => {
if self.tasks.is_empty() {
match pipe.poll_io(&mut self.stream) {
Ok(Async::Ready(ready)) => {
// override keep-alive state
if self.stream.keepalive() {
self.flags.insert(Flags::KEEPALIVE);
} else {
self.flags.remove(Flags::KEEPALIVE);
}
// prepare stream for next response
self.stream.reset();
if !ready {
let item = Entry {
pipe,
flags: EntryFlags::EOF,
};
self.tasks.push_back(item);
}
continue 'outer;
}
Ok(Async::NotReady) => {}
Err(err) => {
error!("Unhandled error: {}", err);
2018-05-17 19:58:08 +02:00
self.flags.insert(Flags::ERROR);
2018-05-16 01:41:46 +02:00
return;
}
}
}
2018-04-29 07:20:32 +02:00
self.tasks.push_back(Entry {
pipe,
flags: EntryFlags::empty(),
});
continue 'outer;
}
2018-04-29 07:20:32 +02:00
Err(req) => req,
}
2018-02-10 01:20:10 +01:00
}
2017-10-07 06:48:14 +02:00
2018-04-29 07:20:32 +02:00
// handler is not found
self.tasks.push_back(Entry {
pipe: Pipeline::error(HttpResponse::NotFound()),
flags: EntryFlags::empty(),
});
2018-04-14 01:02:01 +02:00
}
2018-04-29 07:20:32 +02:00
Ok(Some(Message::Chunk(chunk))) => {
if let Some(ref mut payload) = self.payload {
payload.feed_data(chunk);
} else {
error!("Internal server error: unexpected payload chunk");
self.flags.insert(Flags::ERROR);
2018-05-15 16:55:36 +02:00
break;
2017-12-15 22:10:12 +01:00
}
2018-04-14 01:02:01 +02:00
}
2018-04-29 07:20:32 +02:00
Ok(Some(Message::Eof)) => {
2018-05-07 05:05:31 +02:00
if let Some(mut payload) = self.payload.take() {
2018-04-29 07:20:32 +02:00
payload.feed_eof();
} else {
error!("Internal server error: unexpected eof");
self.flags.insert(Flags::ERROR);
2018-05-15 16:55:36 +02:00
break;
2017-12-16 07:49:48 +01:00
}
2017-10-14 09:11:12 +02:00
}
2018-04-29 07:20:32 +02:00
Ok(None) => break,
Err(e) => {
self.flags.insert(Flags::ERROR);
2018-05-07 05:05:31 +02:00
if let Some(mut payload) = self.payload.take() {
2018-04-29 07:20:32 +02:00
let e = match e {
DecoderError::Io(e) => PayloadError::Io(e),
DecoderError::Error(_) => PayloadError::EncodingCorrupted,
2018-04-14 01:02:01 +02:00
};
2018-04-29 07:20:32 +02:00
payload.set_error(e);
2018-03-17 04:56:23 +01:00
}
2018-05-15 16:55:36 +02:00
break;
2017-10-14 09:11:12 +02:00
}
2018-03-17 04:56:23 +01:00
}
2018-04-14 01:02:01 +02:00
}
2017-11-02 20:54:09 +01:00
}
2018-04-29 07:20:32 +02:00
#[inline]
2018-04-29 18:09:08 +02:00
fn read(&mut self) -> bool {
2018-04-29 07:20:32 +02:00
loop {
unsafe {
if self.buf.remaining_mut() < LW_BUFFER_SIZE {
self.buf.reserve(HW_BUFFER_SIZE);
2017-11-02 20:54:09 +01:00
}
2018-04-29 07:20:32 +02:00
match self.stream.get_mut().read(self.buf.bytes_mut()) {
Ok(n) => {
if n == 0 {
2018-04-29 18:09:08 +02:00
return true;
2018-04-29 07:20:32 +02:00
} else {
self.buf.advance_mut(n);
}
2017-11-02 20:54:09 +01:00
}
2018-04-29 07:20:32 +02:00
Err(e) => {
2018-04-30 05:34:59 +02:00
return e.kind() != io::ErrorKind::WouldBlock;
2017-11-02 20:54:09 +01:00
}
}
}
}
}
}
2017-10-14 09:11:12 +02:00
#[cfg(test)]
mod tests {
2018-05-15 18:29:59 +02:00
use std::net::Shutdown;
use std::{cmp, time};
use bytes::{Buf, Bytes, BytesMut};
2018-04-14 01:02:01 +02:00
use http::{Method, Version};
2018-05-15 18:29:59 +02:00
use tokio_io::{AsyncRead, AsyncWrite};
use super::*;
2017-12-15 05:29:49 +01:00
use application::HttpApplication;
2018-04-14 01:02:01 +02:00
use httpmessage::HttpMessage;
2018-04-29 07:20:32 +02:00
use server::h1decoder::Message;
use server::helpers::SharedHttpInnerMessage;
2018-01-12 03:35:05 +01:00
use server::settings::WorkerSettings;
2018-04-29 07:20:32 +02:00
use server::KeepAlive;
2017-10-14 09:11:12 +02:00
2018-04-29 07:20:32 +02:00
impl Message {
fn message(self) -> SharedHttpInnerMessage {
match self {
2018-04-29 18:09:08 +02:00
Message::Message { msg, payload: _ } => msg,
2018-04-29 07:20:32 +02:00
_ => panic!("error"),
2017-10-14 09:11:12 +02:00
}
}
2018-04-29 07:20:32 +02:00
fn is_payload(&self) -> bool {
match *self {
2018-04-29 18:09:08 +02:00
Message::Message { msg: _, payload } => payload,
2018-04-29 07:20:32 +02:00
_ => panic!("error"),
2017-10-14 09:11:12 +02:00
}
}
2018-04-29 07:20:32 +02:00
fn chunk(self) -> Bytes {
match self {
Message::Chunk(chunk) => chunk,
_ => panic!("error"),
}
}
2018-04-29 07:20:32 +02:00
fn eof(&self) -> bool {
match *self {
Message::Eof => true,
_ => false,
2018-04-14 01:02:01 +02:00
}
2018-04-29 07:20:32 +02:00
}
2017-10-14 09:11:12 +02:00
}
macro_rules! parse_ready {
2018-04-14 01:02:01 +02:00
($e:expr) => {{
let settings: WorkerSettings<HttpApplication> =
WorkerSettings::new(Vec::new(), KeepAlive::Os);
2018-04-29 07:20:32 +02:00
match H1Decoder::new().decode($e, &settings) {
Ok(Some(msg)) => HttpRequest::from_message(msg.message()),
2018-03-30 00:55:27 +02:00
Ok(_) => unreachable!("Eof during parsing http request"),
Err(err) => unreachable!("Error during parsing http request: {:?}", err),
2017-10-14 09:11:12 +02:00
}
2018-04-14 01:02:01 +02:00
}};
2017-10-14 09:11:12 +02:00
}
macro_rules! expect_parse_err {
2018-04-14 01:02:01 +02:00
($e:expr) => {{
let settings: WorkerSettings<HttpApplication> =
WorkerSettings::new(Vec::new(), KeepAlive::Os);
2017-12-15 05:29:49 +01:00
2018-04-29 07:20:32 +02:00
match H1Decoder::new().decode($e, &settings) {
Err(err) => match err {
2018-04-29 07:20:32 +02:00
DecoderError::Error(_) => (),
2018-03-30 00:55:27 +02:00
_ => unreachable!("Parse error expected"),
},
2018-04-14 01:02:01 +02:00
_ => unreachable!("Error expected"),
}
}};
2017-10-14 09:11:12 +02:00
}
2018-05-15 18:29:59 +02:00
struct Buffer {
buf: Bytes,
err: Option<io::Error>,
}
impl Buffer {
fn new(data: &'static str) -> Buffer {
Buffer {
buf: Bytes::from(data),
err: None,
}
}
fn feed_data(&mut self, data: &'static str) {
let mut b = BytesMut::from(self.buf.as_ref());
b.extend(data.as_bytes());
self.buf = b.take().freeze();
}
}
impl AsyncRead for Buffer {}
impl io::Read for Buffer {
fn read(&mut self, dst: &mut [u8]) -> Result<usize, io::Error> {
if self.buf.is_empty() {
if self.err.is_some() {
Err(self.err.take().unwrap())
} else {
Err(io::Error::new(io::ErrorKind::WouldBlock, ""))
}
} else {
let size = cmp::min(self.buf.len(), dst.len());
let b = self.buf.split_to(size);
dst[..size].copy_from_slice(&b);
Ok(size)
}
}
}
impl IoStream for Buffer {
fn shutdown(&mut self, _: Shutdown) -> io::Result<()> {
Ok(())
}
fn set_nodelay(&mut self, _: bool) -> io::Result<()> {
Ok(())
}
fn set_linger(&mut self, _: Option<time::Duration>) -> io::Result<()> {
Ok(())
}
}
impl io::Write for Buffer {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
Ok(buf.len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
impl AsyncWrite for Buffer {
fn shutdown(&mut self) -> Poll<(), io::Error> {
Ok(Async::Ready(()))
}
fn write_buf<B: Buf>(&mut self, _: &mut B) -> Poll<usize, io::Error> {
Ok(Async::NotReady)
}
}
#[test]
fn test_req_parse() {
let buf = Buffer::new("GET /test HTTP/1.1\r\n\r\n");
let readbuf = BytesMut::new();
let settings = Rc::new(WorkerSettings::<HttpApplication>::new(
Vec::new(),
KeepAlive::Os,
));
let mut h1 = Http1::new(Rc::clone(&settings), buf, None, readbuf);
h1.poll_io();
h1.parse();
assert_eq!(h1.tasks.len(), 1);
}
#[test]
fn test_req_parse_err() {
let buf = Buffer::new("GET /test HTTP/1\r\n\r\n");
let readbuf = BytesMut::new();
let settings = Rc::new(WorkerSettings::<HttpApplication>::new(
Vec::new(),
KeepAlive::Os,
));
let mut h1 = Http1::new(Rc::clone(&settings), buf, None, readbuf);
h1.poll_io();
h1.parse();
assert!(h1.flags.contains(Flags::ERROR));
}
2017-10-14 09:11:12 +02:00
#[test]
fn test_parse() {
2018-04-29 07:20:32 +02:00
let mut buf = BytesMut::from("GET /test HTTP/1.1\r\n\r\n");
2018-04-14 01:02:01 +02:00
let settings = WorkerSettings::<HttpApplication>::new(Vec::new(), KeepAlive::Os);
2017-10-14 09:11:12 +02:00
2018-04-29 07:20:32 +02:00
let mut reader = H1Decoder::new();
match reader.decode(&mut buf, &settings) {
Ok(Some(msg)) => {
let req = HttpRequest::from_message(msg.message());
2017-10-14 09:11:12 +02:00
assert_eq!(req.version(), Version::HTTP_11);
assert_eq!(*req.method(), Method::GET);
assert_eq!(req.path(), "/test");
}
2018-03-30 00:55:27 +02:00
Ok(_) | Err(_) => unreachable!("Error during parsing http request"),
2017-10-14 09:11:12 +02:00
}
}
#[test]
fn test_parse_partial() {
2018-04-29 07:20:32 +02:00
let mut buf = BytesMut::from("PUT /test HTTP/1");
2018-04-14 01:02:01 +02:00
let settings = WorkerSettings::<HttpApplication>::new(Vec::new(), KeepAlive::Os);
2017-10-14 09:11:12 +02:00
2018-04-29 07:20:32 +02:00
let mut reader = H1Decoder::new();
match reader.decode(&mut buf, &settings) {
Ok(None) => (),
2018-03-30 00:55:27 +02:00
_ => unreachable!("Error"),
2017-10-14 09:11:12 +02:00
}
2018-04-29 07:20:32 +02:00
buf.extend(b".1\r\n\r\n");
match reader.decode(&mut buf, &settings) {
Ok(Some(msg)) => {
let mut req = HttpRequest::from_message(msg.message());
2017-10-14 09:11:12 +02:00
assert_eq!(req.version(), Version::HTTP_11);
assert_eq!(*req.method(), Method::PUT);
assert_eq!(req.path(), "/test");
}
2018-03-30 00:55:27 +02:00
Ok(_) | Err(_) => unreachable!("Error during parsing http request"),
2017-10-14 09:11:12 +02:00
}
}
#[test]
fn test_parse_post() {
2018-04-29 07:20:32 +02:00
let mut buf = BytesMut::from("POST /test2 HTTP/1.0\r\n\r\n");
2018-04-14 01:02:01 +02:00
let settings = WorkerSettings::<HttpApplication>::new(Vec::new(), KeepAlive::Os);
2017-10-14 09:11:12 +02:00
2018-04-29 07:20:32 +02:00
let mut reader = H1Decoder::new();
match reader.decode(&mut buf, &settings) {
Ok(Some(msg)) => {
let mut req = HttpRequest::from_message(msg.message());
2017-10-14 09:11:12 +02:00
assert_eq!(req.version(), Version::HTTP_10);
assert_eq!(*req.method(), Method::POST);
assert_eq!(req.path(), "/test2");
}
2018-03-30 00:55:27 +02:00
Ok(_) | Err(_) => unreachable!("Error during parsing http request"),
2017-10-14 09:11:12 +02:00
}
}
#[test]
fn test_parse_body() {
2018-04-29 07:20:32 +02:00
let mut buf =
BytesMut::from("GET /test HTTP/1.1\r\nContent-Length: 4\r\n\r\nbody");
2018-04-14 01:02:01 +02:00
let settings = WorkerSettings::<HttpApplication>::new(Vec::new(), KeepAlive::Os);
2017-10-14 09:11:12 +02:00
2018-04-29 07:20:32 +02:00
let mut reader = H1Decoder::new();
match reader.decode(&mut buf, &settings) {
Ok(Some(msg)) => {
let mut req = HttpRequest::from_message(msg.message());
2017-10-14 09:11:12 +02:00
assert_eq!(req.version(), Version::HTTP_11);
assert_eq!(*req.method(), Method::GET);
assert_eq!(req.path(), "/test");
2018-04-29 07:20:32 +02:00
assert_eq!(
reader
.decode(&mut buf, &settings)
.unwrap()
.unwrap()
.chunk()
.as_ref(),
b"body"
);
2017-10-14 09:11:12 +02:00
}
2018-03-30 00:55:27 +02:00
Ok(_) | Err(_) => unreachable!("Error during parsing http request"),
2017-10-14 09:11:12 +02:00
}
}
#[test]
fn test_parse_body_crlf() {
2018-04-14 01:02:01 +02:00
let mut buf =
2018-04-29 07:20:32 +02:00
BytesMut::from("\r\nGET /test HTTP/1.1\r\nContent-Length: 4\r\n\r\nbody");
2018-04-14 01:02:01 +02:00
let settings = WorkerSettings::<HttpApplication>::new(Vec::new(), KeepAlive::Os);
2017-10-14 09:11:12 +02:00
2018-04-29 07:20:32 +02:00
let mut reader = H1Decoder::new();
match reader.decode(&mut buf, &settings) {
Ok(Some(msg)) => {
let mut req = HttpRequest::from_message(msg.message());
2017-10-14 09:11:12 +02:00
assert_eq!(req.version(), Version::HTTP_11);
assert_eq!(*req.method(), Method::GET);
assert_eq!(req.path(), "/test");
2018-04-29 07:20:32 +02:00
assert_eq!(
reader
.decode(&mut buf, &settings)
.unwrap()
.unwrap()
.chunk()
.as_ref(),
b"body"
);
2017-10-14 09:11:12 +02:00
}
2018-03-30 00:55:27 +02:00
Ok(_) | Err(_) => unreachable!("Error during parsing http request"),
2017-10-14 09:11:12 +02:00
}
}
#[test]
fn test_parse_partial_eof() {
2018-04-29 07:20:32 +02:00
let mut buf = BytesMut::from("GET /test HTTP/1.1\r\n");
2018-04-14 01:02:01 +02:00
let settings = WorkerSettings::<HttpApplication>::new(Vec::new(), KeepAlive::Os);
2018-04-29 07:20:32 +02:00
let mut reader = H1Decoder::new();
assert!(reader.decode(&mut buf, &settings).unwrap().is_none());
2017-10-14 09:11:12 +02:00
2018-04-29 07:20:32 +02:00
buf.extend(b"\r\n");
match reader.decode(&mut buf, &settings) {
Ok(Some(msg)) => {
let req = HttpRequest::from_message(msg.message());
2017-10-14 09:11:12 +02:00
assert_eq!(req.version(), Version::HTTP_11);
assert_eq!(*req.method(), Method::GET);
assert_eq!(req.path(), "/test");
}
2018-03-30 00:55:27 +02:00
Ok(_) | Err(_) => unreachable!("Error during parsing http request"),
2017-10-14 09:11:12 +02:00
}
}
#[test]
fn test_headers_split_field() {
2018-04-29 07:20:32 +02:00
let mut buf = BytesMut::from("GET /test HTTP/1.1\r\n");
2018-04-14 01:02:01 +02:00
let settings = WorkerSettings::<HttpApplication>::new(Vec::new(), KeepAlive::Os);
2017-10-14 09:11:12 +02:00
2018-04-29 07:20:32 +02:00
let mut reader = H1Decoder::new();
assert!{ reader.decode(&mut buf, &settings).unwrap().is_none() }
2017-10-14 09:11:12 +02:00
2018-04-29 07:20:32 +02:00
buf.extend(b"t");
assert!{ reader.decode(&mut buf, &settings).unwrap().is_none() }
2017-10-14 09:11:12 +02:00
2018-04-29 07:20:32 +02:00
buf.extend(b"es");
assert!{ reader.decode(&mut buf, &settings).unwrap().is_none() }
2017-10-14 09:11:12 +02:00
2018-04-29 07:20:32 +02:00
buf.extend(b"t: value\r\n\r\n");
match reader.decode(&mut buf, &settings) {
Ok(Some(msg)) => {
let req = HttpRequest::from_message(msg.message());
2017-10-14 09:11:12 +02:00
assert_eq!(req.version(), Version::HTTP_11);
assert_eq!(*req.method(), Method::GET);
assert_eq!(req.path(), "/test");
2018-05-15 16:55:36 +02:00
assert_eq!(req.headers().get("test").unwrap().as_bytes(), b"value");
2017-10-14 09:11:12 +02:00
}
2018-03-30 00:55:27 +02:00
Ok(_) | Err(_) => unreachable!("Error during parsing http request"),
2017-10-14 09:11:12 +02:00
}
}
#[test]
fn test_headers_multi_value() {
2018-04-29 07:20:32 +02:00
let mut buf = BytesMut::from(
2017-10-14 09:11:12 +02:00
"GET /test HTTP/1.1\r\n\
Set-Cookie: c1=cookie1\r\n\
2018-04-14 01:02:01 +02:00
Set-Cookie: c2=cookie2\r\n\r\n",
);
let settings = WorkerSettings::<HttpApplication>::new(Vec::new(), KeepAlive::Os);
2018-04-29 07:20:32 +02:00
let mut reader = H1Decoder::new();
let msg = reader.decode(&mut buf, &settings).unwrap().unwrap();
let req = HttpRequest::from_message(msg.message());
2017-10-14 09:11:12 +02:00
2018-04-29 07:20:32 +02:00
let val: Vec<_> = req.headers()
.get_all("Set-Cookie")
.iter()
.map(|v| v.to_str().unwrap().to_owned())
.collect();
assert_eq!(val[0], "c1=cookie1");
assert_eq!(val[1], "c2=cookie2");
2017-10-14 09:11:12 +02:00
}
#[test]
fn test_conn_default_1_0() {
2018-04-29 07:20:32 +02:00
let mut buf = BytesMut::from("GET /test HTTP/1.0\r\n\r\n");
2017-11-27 04:00:57 +01:00
let req = parse_ready!(&mut buf);
2017-10-14 09:11:12 +02:00
assert!(!req.keep_alive());
}
#[test]
fn test_conn_default_1_1() {
2018-04-29 07:20:32 +02:00
let mut buf = BytesMut::from("GET /test HTTP/1.1\r\n\r\n");
2017-11-27 04:00:57 +01:00
let req = parse_ready!(&mut buf);
2017-10-14 09:11:12 +02:00
assert!(req.keep_alive());
}
#[test]
fn test_conn_close() {
2018-04-29 07:20:32 +02:00
let mut buf = BytesMut::from(
2017-10-14 09:11:12 +02:00
"GET /test HTTP/1.1\r\n\
2018-04-14 01:02:01 +02:00
connection: close\r\n\r\n",
);
2017-11-27 04:00:57 +01:00
let req = parse_ready!(&mut buf);
2017-10-14 09:11:12 +02:00
assert!(!req.keep_alive());
}
#[test]
fn test_conn_close_1_0() {
2018-04-29 07:20:32 +02:00
let mut buf = BytesMut::from(
2017-10-14 09:11:12 +02:00
"GET /test HTTP/1.0\r\n\
2018-04-14 01:02:01 +02:00
connection: close\r\n\r\n",
);
2017-11-27 04:00:57 +01:00
let req = parse_ready!(&mut buf);
2017-10-14 09:11:12 +02:00
assert!(!req.keep_alive());
}
#[test]
fn test_conn_keep_alive_1_0() {
2018-04-29 07:20:32 +02:00
let mut buf = BytesMut::from(
2017-10-14 09:11:12 +02:00
"GET /test HTTP/1.0\r\n\
2018-04-14 01:02:01 +02:00
connection: keep-alive\r\n\r\n",
);
2017-11-27 04:00:57 +01:00
let req = parse_ready!(&mut buf);
2017-10-14 09:11:12 +02:00
assert!(req.keep_alive());
}
#[test]
fn test_conn_keep_alive_1_1() {
2018-04-29 07:20:32 +02:00
let mut buf = BytesMut::from(
2017-10-14 09:11:12 +02:00
"GET /test HTTP/1.1\r\n\
2018-04-14 01:02:01 +02:00
connection: keep-alive\r\n\r\n",
);
2017-11-27 04:00:57 +01:00
let req = parse_ready!(&mut buf);
2017-10-14 09:11:12 +02:00
assert!(req.keep_alive());
}
#[test]
fn test_conn_other_1_0() {
2018-04-29 07:20:32 +02:00
let mut buf = BytesMut::from(
2017-10-14 09:11:12 +02:00
"GET /test HTTP/1.0\r\n\
2018-04-14 01:02:01 +02:00
connection: other\r\n\r\n",
);
2017-11-27 04:00:57 +01:00
let req = parse_ready!(&mut buf);
2017-10-14 09:11:12 +02:00
assert!(!req.keep_alive());
}
#[test]
fn test_conn_other_1_1() {
2018-04-29 07:20:32 +02:00
let mut buf = BytesMut::from(
2017-10-14 09:11:12 +02:00
"GET /test HTTP/1.1\r\n\
2018-04-14 01:02:01 +02:00
connection: other\r\n\r\n",
);
2017-11-27 04:00:57 +01:00
let req = parse_ready!(&mut buf);
2017-10-14 09:11:12 +02:00
assert!(req.keep_alive());
}
#[test]
fn test_conn_upgrade() {
2018-04-29 07:20:32 +02:00
let mut buf = BytesMut::from(
2017-10-14 09:11:12 +02:00
"GET /test HTTP/1.1\r\n\
2017-12-25 01:15:40 +01:00
upgrade: websockets\r\n\
2018-04-14 01:02:01 +02:00
connection: upgrade\r\n\r\n",
);
2017-11-27 04:00:57 +01:00
let req = parse_ready!(&mut buf);
2017-10-14 09:11:12 +02:00
assert!(req.upgrade());
}
#[test]
fn test_conn_upgrade_connect_method() {
2018-04-29 07:20:32 +02:00
let mut buf = BytesMut::from(
2017-10-14 09:11:12 +02:00
"CONNECT /test HTTP/1.1\r\n\
2018-04-14 01:02:01 +02:00
content-type: text/plain\r\n\r\n",
);
2017-11-27 04:00:57 +01:00
let req = parse_ready!(&mut buf);
2017-10-14 09:11:12 +02:00
assert!(req.upgrade());
}
#[test]
fn test_request_chunked() {
2018-04-29 07:20:32 +02:00
let mut buf = BytesMut::from(
2017-10-14 09:11:12 +02:00
"GET /test HTTP/1.1\r\n\
2018-04-14 01:02:01 +02:00
transfer-encoding: chunked\r\n\r\n",
);
2017-11-27 04:00:57 +01:00
let req = parse_ready!(&mut buf);
2017-10-14 09:11:12 +02:00
if let Ok(val) = req.chunked() {
assert!(val);
} else {
2018-03-30 00:55:27 +02:00
unreachable!("Error");
2017-10-14 09:11:12 +02:00
}
2018-01-21 01:51:18 +01:00
// type in chunked
2018-04-29 07:20:32 +02:00
let mut buf = BytesMut::from(
2018-01-21 01:51:18 +01:00
"GET /test HTTP/1.1\r\n\
2018-04-14 01:02:01 +02:00
transfer-encoding: chnked\r\n\r\n",
);
2018-01-21 01:51:18 +01:00
let req = parse_ready!(&mut buf);
if let Ok(val) = req.chunked() {
assert!(!val);
} else {
2018-03-30 00:55:27 +02:00
unreachable!("Error");
2018-01-21 01:51:18 +01:00
}
2017-10-14 09:11:12 +02:00
}
#[test]
fn test_headers_content_length_err_1() {
2018-04-29 07:20:32 +02:00
let mut buf = BytesMut::from(
2017-10-14 09:11:12 +02:00
"GET /test HTTP/1.1\r\n\
2018-04-14 01:02:01 +02:00
content-length: line\r\n\r\n",
);
2017-10-14 09:11:12 +02:00
expect_parse_err!(&mut buf)
}
#[test]
fn test_headers_content_length_err_2() {
2018-04-29 07:20:32 +02:00
let mut buf = BytesMut::from(
2017-10-14 09:11:12 +02:00
"GET /test HTTP/1.1\r\n\
2018-04-14 01:02:01 +02:00
content-length: -1\r\n\r\n",
);
2017-10-14 09:11:12 +02:00
expect_parse_err!(&mut buf);
}
#[test]
fn test_invalid_header() {
2018-04-29 07:20:32 +02:00
let mut buf = BytesMut::from(
2017-10-14 09:11:12 +02:00
"GET /test HTTP/1.1\r\n\
2018-04-14 01:02:01 +02:00
test line\r\n\r\n",
);
2017-10-14 09:11:12 +02:00
expect_parse_err!(&mut buf);
}
#[test]
fn test_invalid_name() {
2018-04-29 07:20:32 +02:00
let mut buf = BytesMut::from(
2017-10-14 09:11:12 +02:00
"GET /test HTTP/1.1\r\n\
2018-04-14 01:02:01 +02:00
test[]: line\r\n\r\n",
);
2017-10-14 09:11:12 +02:00
expect_parse_err!(&mut buf);
}
#[test]
fn test_http_request_bad_status_line() {
2018-04-29 07:20:32 +02:00
let mut buf = BytesMut::from("getpath \r\n\r\n");
2017-10-14 09:11:12 +02:00
expect_parse_err!(&mut buf);
}
#[test]
fn test_http_request_upgrade() {
2018-04-29 07:20:32 +02:00
let settings = WorkerSettings::<HttpApplication>::new(Vec::new(), KeepAlive::Os);
let mut buf = BytesMut::from(
2017-10-14 09:11:12 +02:00
"GET /test HTTP/1.1\r\n\
connection: upgrade\r\n\
upgrade: websocket\r\n\r\n\
2018-04-14 01:02:01 +02:00
some raw data",
);
2018-04-29 07:20:32 +02:00
let mut reader = H1Decoder::new();
let msg = reader.decode(&mut buf, &settings).unwrap().unwrap();
assert!(msg.is_payload());
let req = HttpRequest::from_message(msg.message());
2017-10-14 09:11:12 +02:00
assert!(!req.keep_alive());
assert!(req.upgrade());
2018-04-14 01:02:01 +02:00
assert_eq!(
2018-04-29 18:09:08 +02:00
reader
.decode(&mut buf, &settings)
.unwrap()
.unwrap()
.chunk()
.as_ref(),
2018-04-14 01:02:01 +02:00
b"some raw data"
);
2017-10-14 09:11:12 +02:00
}
#[test]
fn test_http_request_parser_utf8() {
2018-04-29 07:20:32 +02:00
let mut buf = BytesMut::from(
2017-10-14 09:11:12 +02:00
"GET /test HTTP/1.1\r\n\
2018-04-14 01:02:01 +02:00
x-test: тест\r\n\r\n",
);
2017-11-27 04:00:57 +01:00
let req = parse_ready!(&mut buf);
2017-10-14 09:11:12 +02:00
2018-04-14 01:02:01 +02:00
assert_eq!(
req.headers().get("x-test").unwrap().as_bytes(),
"тест".as_bytes()
);
2017-10-14 09:11:12 +02:00
}
#[test]
fn test_http_request_parser_two_slashes() {
2018-04-29 07:20:32 +02:00
let mut buf = BytesMut::from("GET //path HTTP/1.1\r\n\r\n");
2017-11-27 04:00:57 +01:00
let req = parse_ready!(&mut buf);
2017-10-14 09:11:12 +02:00
assert_eq!(req.path(), "//path");
}
#[test]
fn test_http_request_parser_bad_method() {
2018-04-29 07:20:32 +02:00
let mut buf = BytesMut::from("!12%()+=~$ /get HTTP/1.1\r\n\r\n");
2017-10-14 09:11:12 +02:00
expect_parse_err!(&mut buf);
}
#[test]
fn test_http_request_parser_bad_version() {
2018-04-29 07:20:32 +02:00
let mut buf = BytesMut::from("GET //get HT/11\r\n\r\n");
2017-10-14 09:11:12 +02:00
expect_parse_err!(&mut buf);
}
#[test]
fn test_http_request_chunked_payload() {
2018-04-29 07:20:32 +02:00
let mut buf = BytesMut::from(
2017-10-14 09:11:12 +02:00
"GET /test HTTP/1.1\r\n\
2018-04-14 01:02:01 +02:00
transfer-encoding: chunked\r\n\r\n",
);
let settings = WorkerSettings::<HttpApplication>::new(Vec::new(), KeepAlive::Os);
2017-10-14 09:11:12 +02:00
2018-04-29 07:20:32 +02:00
let mut reader = H1Decoder::new();
let msg = reader.decode(&mut buf, &settings).unwrap().unwrap();
assert!(msg.is_payload());
let req = HttpRequest::from_message(msg.message());
2017-10-14 09:11:12 +02:00
assert!(req.chunked().unwrap());
2018-04-29 07:20:32 +02:00
buf.extend(b"4\r\ndata\r\n4\r\nline\r\n0\r\n\r\n");
assert_eq!(
2018-04-29 18:09:08 +02:00
reader
.decode(&mut buf, &settings)
.unwrap()
.unwrap()
.chunk()
.as_ref(),
2018-04-29 07:20:32 +02:00
b"data"
);
2018-04-14 01:02:01 +02:00
assert_eq!(
2018-04-29 18:09:08 +02:00
reader
.decode(&mut buf, &settings)
.unwrap()
.unwrap()
.chunk()
.as_ref(),
2018-04-29 07:20:32 +02:00
b"line"
);
2018-05-15 16:55:36 +02:00
assert!(reader.decode(&mut buf, &settings).unwrap().unwrap().eof());
2017-10-14 09:11:12 +02:00
}
2017-10-14 09:28:49 +02:00
#[test]
fn test_http_request_chunked_payload_and_next_message() {
2018-04-29 07:20:32 +02:00
let mut buf = BytesMut::from(
2017-10-14 09:28:49 +02:00
"GET /test HTTP/1.1\r\n\
2018-04-14 01:02:01 +02:00
transfer-encoding: chunked\r\n\r\n",
);
let settings = WorkerSettings::<HttpApplication>::new(Vec::new(), KeepAlive::Os);
2018-04-29 07:20:32 +02:00
let mut reader = H1Decoder::new();
let msg = reader.decode(&mut buf, &settings).unwrap().unwrap();
assert!(msg.is_payload());
let req = HttpRequest::from_message(msg.message());
2017-10-14 09:28:49 +02:00
assert!(req.chunked().unwrap());
2018-04-29 07:20:32 +02:00
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-04-14 01:02:01 +02:00
);
2018-04-29 07:20:32 +02:00
let msg = reader.decode(&mut buf, &settings).unwrap().unwrap();
assert_eq!(msg.chunk().as_ref(), b"data");
let msg = reader.decode(&mut buf, &settings).unwrap().unwrap();
assert_eq!(msg.chunk().as_ref(), b"line");
let msg = reader.decode(&mut buf, &settings).unwrap().unwrap();
assert!(msg.eof());
let msg = reader.decode(&mut buf, &settings).unwrap().unwrap();
assert!(msg.is_payload());
let req2 = HttpRequest::from_message(msg.message());
assert!(req2.chunked().unwrap());
2017-10-14 09:28:49 +02:00
assert_eq!(*req2.method(), Method::POST);
assert!(req2.chunked().unwrap());
}
#[test]
fn test_http_request_chunked_payload_chunks() {
2018-04-29 07:20:32 +02:00
let mut buf = BytesMut::from(
2017-10-14 09:28:49 +02:00
"GET /test HTTP/1.1\r\n\
2018-04-14 01:02:01 +02:00
transfer-encoding: chunked\r\n\r\n",
);
let settings = WorkerSettings::<HttpApplication>::new(Vec::new(), KeepAlive::Os);
2017-10-14 09:28:49 +02:00
2018-04-29 07:20:32 +02:00
let mut reader = H1Decoder::new();
let msg = reader.decode(&mut buf, &settings).unwrap().unwrap();
assert!(msg.is_payload());
let req = HttpRequest::from_message(msg.message());
2017-10-14 09:28:49 +02:00
assert!(req.chunked().unwrap());
2018-03-03 07:08:56 +01:00
2018-04-29 07:20:32 +02:00
buf.extend(b"4\r\n1111\r\n");
let msg = reader.decode(&mut buf, &settings).unwrap().unwrap();
assert_eq!(msg.chunk().as_ref(), b"1111");
2017-10-14 09:28:49 +02:00
2018-04-29 07:20:32 +02:00
buf.extend(b"4\r\ndata\r");
let msg = reader.decode(&mut buf, &settings).unwrap().unwrap();
assert_eq!(msg.chunk().as_ref(), b"data");
2017-10-14 09:28:49 +02:00
2018-04-29 07:20:32 +02:00
buf.extend(b"\n4");
assert!(reader.decode(&mut buf, &settings).unwrap().is_none());
2017-10-14 09:28:49 +02:00
2018-04-29 07:20:32 +02:00
buf.extend(b"\r");
assert!(reader.decode(&mut buf, &settings).unwrap().is_none());
buf.extend(b"\n");
assert!(reader.decode(&mut buf, &settings).unwrap().is_none());
2017-10-14 09:28:49 +02:00
2018-04-29 07:20:32 +02:00
buf.extend(b"li");
let msg = reader.decode(&mut buf, &settings).unwrap().unwrap();
assert_eq!(msg.chunk().as_ref(), b"li");
2017-10-14 09:28:49 +02:00
2018-03-03 07:08:56 +01:00
//trailers
2017-10-14 09:28:49 +02:00
//buf.feed_data("test: test\r\n");
//not_ready!(reader.parse(&mut buf, &mut readbuf));
2017-10-14 09:28:49 +02:00
2018-04-29 07:20:32 +02:00
buf.extend(b"ne\r\n0\r\n");
let msg = reader.decode(&mut buf, &settings).unwrap().unwrap();
assert_eq!(msg.chunk().as_ref(), b"ne");
assert!(reader.decode(&mut buf, &settings).unwrap().is_none());
buf.extend(b"\r\n");
2018-05-15 16:55:36 +02:00
assert!(reader.decode(&mut buf, &settings).unwrap().unwrap().eof());
2017-10-14 09:28:49 +02:00
}
#[test]
fn test_parse_chunked_payload_chunk_extension() {
2018-04-29 07:20:32 +02:00
let mut buf = BytesMut::from(
&"GET /test HTTP/1.1\r\n\
transfer-encoding: chunked\r\n\r\n"[..],
2018-04-14 01:02:01 +02:00
);
let settings = WorkerSettings::<HttpApplication>::new(Vec::new(), KeepAlive::Os);
2017-10-14 09:28:49 +02:00
2018-04-29 07:20:32 +02:00
let mut reader = H1Decoder::new();
let msg = reader.decode(&mut buf, &settings).unwrap().unwrap();
assert!(msg.is_payload());
let req = HttpRequest::from_message(msg.message());
2017-10-14 09:28:49 +02:00
assert!(req.chunked().unwrap());
2018-04-29 07:20:32 +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-05-15 16:55:36 +02:00
let chunk = reader.decode(&mut buf, &settings).unwrap().unwrap().chunk();
2018-04-29 07:20:32 +02:00
assert_eq!(chunk, Bytes::from_static(b"data"));
2018-05-15 16:55:36 +02:00
let chunk = reader.decode(&mut buf, &settings).unwrap().unwrap().chunk();
2018-04-29 07:20:32 +02:00
assert_eq!(chunk, Bytes::from_static(b"line"));
let msg = reader.decode(&mut buf, &settings).unwrap().unwrap();
assert!(msg.eof());
2017-10-14 09:28:49 +02:00
}
2017-10-14 09:11:12 +02:00
}