2017-10-13 23:43:17 +02:00
|
|
|
use std::{self, io, ptr};
|
2017-10-07 06:48:14 +02:00
|
|
|
|
|
|
|
use httparse;
|
2017-10-10 08:07:32 +02:00
|
|
|
use http::{Method, Version, Uri, HttpTryFrom, HeaderMap};
|
|
|
|
use http::header::{self, HeaderName, HeaderValue};
|
|
|
|
use bytes::{BytesMut, BufMut};
|
2017-10-09 05:16:48 +02:00
|
|
|
use futures::{Async, Poll};
|
2017-10-07 06:48:14 +02:00
|
|
|
use tokio_io::AsyncRead;
|
|
|
|
|
2017-10-13 23:43:17 +02:00
|
|
|
use error::ParseError;
|
2017-10-07 06:48:14 +02:00
|
|
|
use decode::Decoder;
|
2017-10-11 02:14:30 +02:00
|
|
|
use httpmessage::HttpRequest;
|
2017-10-09 05:16:48 +02:00
|
|
|
use payload::{Payload, PayloadSender};
|
2017-10-07 06:48:14 +02:00
|
|
|
|
|
|
|
const MAX_HEADERS: usize = 100;
|
|
|
|
const INIT_BUFFER_SIZE: usize = 8192;
|
2017-10-09 05:16:48 +02:00
|
|
|
const MAX_BUFFER_SIZE: usize = 131_072;
|
2017-10-07 06:48:14 +02:00
|
|
|
|
|
|
|
struct PayloadInfo {
|
2017-10-09 05:16:48 +02:00
|
|
|
tx: PayloadSender,
|
2017-10-07 06:48:14 +02:00
|
|
|
decoder: Decoder,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Reader {
|
|
|
|
read_buf: BytesMut,
|
|
|
|
payload: Option<PayloadInfo>,
|
|
|
|
}
|
|
|
|
|
|
|
|
enum Decoding {
|
|
|
|
Paused,
|
|
|
|
Ready,
|
|
|
|
NotReady,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Reader {
|
|
|
|
pub fn new() -> Reader {
|
|
|
|
Reader {
|
|
|
|
read_buf: BytesMut::new(),
|
|
|
|
payload: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub fn consume_leading_lines(&mut self) {
|
|
|
|
if !self.read_buf.is_empty() {
|
|
|
|
let mut i = 0;
|
|
|
|
while i < self.read_buf.len() {
|
|
|
|
match self.read_buf[i] {
|
|
|
|
b'\r' | b'\n' => i += 1,
|
|
|
|
_ => break,
|
|
|
|
}
|
2017-10-10 08:07:32 +02:00
|
|
|
} self.read_buf.split_to(i);
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-13 23:43:17 +02:00
|
|
|
fn decode(&mut self) -> std::result::Result<Decoding, ParseError>
|
2017-10-07 06:48:14 +02:00
|
|
|
{
|
|
|
|
if let Some(ref mut payload) = self.payload {
|
2017-10-09 05:16:48 +02:00
|
|
|
if payload.tx.maybe_paused() {
|
|
|
|
return Ok(Decoding::Paused)
|
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
loop {
|
|
|
|
match payload.decoder.decode(&mut self.read_buf) {
|
|
|
|
Ok(Async::Ready(Some(bytes))) => {
|
2017-10-09 05:16:48 +02:00
|
|
|
payload.tx.feed_data(bytes)
|
2017-10-07 06:48:14 +02:00
|
|
|
},
|
|
|
|
Ok(Async::Ready(None)) => {
|
2017-10-09 05:16:48 +02:00
|
|
|
payload.tx.feed_eof();
|
|
|
|
return Ok(Decoding::Ready)
|
2017-10-07 06:48:14 +02:00
|
|
|
},
|
|
|
|
Ok(Async::NotReady) => return Ok(Decoding::NotReady),
|
2017-10-13 23:43:17 +02:00
|
|
|
Err(_) => return Err(ParseError::Incomplete),
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Ok(Decoding::Ready)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-13 23:43:17 +02:00
|
|
|
pub fn parse<T>(&mut self, io: &mut T) -> Poll<(HttpRequest, Payload), ParseError>
|
2017-10-07 06:48:14 +02:00
|
|
|
where T: AsyncRead
|
|
|
|
{
|
|
|
|
loop {
|
|
|
|
match self.decode()? {
|
|
|
|
Decoding::Paused => return Ok(Async::NotReady),
|
|
|
|
Decoding::Ready => {
|
|
|
|
self.payload = None;
|
|
|
|
break
|
|
|
|
},
|
|
|
|
Decoding::NotReady => {
|
|
|
|
if 0 == try_ready!(self.read_from_io(io)) {
|
2017-10-13 23:43:17 +02:00
|
|
|
return Err(ParseError::Eof)
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
loop {
|
|
|
|
match try!(parse(&mut self.read_buf)) {
|
|
|
|
Some((msg, decoder)) => {
|
|
|
|
let payload = if let Some(decoder) = decoder {
|
2017-10-09 05:16:48 +02:00
|
|
|
let (tx, rx) = Payload::new(false);
|
2017-10-07 06:48:14 +02:00
|
|
|
let payload = PayloadInfo {
|
|
|
|
tx: tx,
|
|
|
|
decoder: decoder,
|
|
|
|
};
|
|
|
|
self.payload = Some(payload);
|
|
|
|
|
|
|
|
loop {
|
|
|
|
match self.decode()? {
|
|
|
|
Decoding::Paused =>
|
|
|
|
break,
|
|
|
|
Decoding::Ready => {
|
|
|
|
self.payload = None;
|
|
|
|
break
|
|
|
|
},
|
|
|
|
Decoding::NotReady => {
|
|
|
|
match self.read_from_io(io) {
|
|
|
|
Ok(Async::Ready(0)) => {
|
|
|
|
trace!("parse eof");
|
2017-10-13 23:43:17 +02:00
|
|
|
return Err(ParseError::Eof);
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
Ok(Async::Ready(_)) => {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
Ok(Async::NotReady) => break,
|
|
|
|
Err(err) => return Err(err.into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-09 05:16:48 +02:00
|
|
|
rx
|
2017-10-07 06:48:14 +02:00
|
|
|
} else {
|
2017-10-09 05:16:48 +02:00
|
|
|
let (_, rx) = Payload::new(true);
|
|
|
|
rx
|
2017-10-07 06:48:14 +02:00
|
|
|
};
|
|
|
|
return Ok(Async::Ready((msg, payload)));
|
|
|
|
},
|
|
|
|
None => {
|
|
|
|
if self.read_buf.capacity() >= MAX_BUFFER_SIZE {
|
|
|
|
debug!("MAX_BUFFER_SIZE reached, closing");
|
2017-10-13 23:43:17 +02:00
|
|
|
return Err(ParseError::TooLarge);
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if 0 == try_ready!(self.read_from_io(io)) {
|
|
|
|
trace!("parse eof");
|
2017-10-13 23:43:17 +02:00
|
|
|
return Err(ParseError::Eof);
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_from_io<T: AsyncRead>(&mut self, io: &mut T) -> Poll<usize, io::Error> {
|
|
|
|
if self.read_buf.remaining_mut() < INIT_BUFFER_SIZE {
|
|
|
|
self.read_buf.reserve(INIT_BUFFER_SIZE);
|
|
|
|
unsafe { // Zero out unused memory
|
|
|
|
let buf = self.read_buf.bytes_mut();
|
|
|
|
let len = buf.len();
|
|
|
|
ptr::write_bytes(buf.as_mut_ptr(), 0, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
let n = match io.read(self.read_buf.bytes_mut()) {
|
|
|
|
Ok(n) => n,
|
|
|
|
Err(e) => {
|
|
|
|
if e.kind() == io::ErrorKind::WouldBlock {
|
|
|
|
return Ok(Async::NotReady);
|
|
|
|
}
|
|
|
|
return Err(e)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
self.read_buf.advance_mut(n);
|
|
|
|
Ok(Async::Ready(n))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-13 23:43:17 +02:00
|
|
|
pub fn parse(buf: &mut BytesMut) -> Result<Option<(HttpRequest, Option<Decoder>)>, ParseError>
|
|
|
|
{
|
2017-10-07 06:48:14 +02:00
|
|
|
if buf.is_empty() {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse http message
|
|
|
|
let mut headers_indices = [HeaderIndices {
|
|
|
|
name: (0, 0),
|
|
|
|
value: (0, 0)
|
|
|
|
}; MAX_HEADERS];
|
|
|
|
|
|
|
|
let (len, method, path, version, headers_len) = {
|
|
|
|
let mut headers = [httparse::EMPTY_HEADER; MAX_HEADERS];
|
|
|
|
trace!("Request.parse([Header; {}], [u8; {}])", headers.len(), buf.len());
|
|
|
|
let mut req = httparse::Request::new(&mut headers);
|
|
|
|
match try!(req.parse(buf)) {
|
|
|
|
httparse::Status::Complete(len) => {
|
|
|
|
trace!("Request.parse Complete({})", len);
|
2017-10-13 23:43:17 +02:00
|
|
|
let method = Method::try_from(req.method.unwrap())
|
|
|
|
.map_err(|_| ParseError::Method)?;
|
2017-10-07 06:48:14 +02:00
|
|
|
let path = req.path.unwrap();
|
|
|
|
let bytes_ptr = buf.as_ref().as_ptr() as usize;
|
|
|
|
let path_start = path.as_ptr() as usize - bytes_ptr;
|
|
|
|
let path_end = path_start + path.len();
|
|
|
|
let path = (path_start, path_end);
|
|
|
|
|
|
|
|
let version = if req.version.unwrap() == 1 {
|
|
|
|
Version::HTTP_11
|
|
|
|
} else {
|
|
|
|
Version::HTTP_10
|
|
|
|
};
|
|
|
|
|
|
|
|
record_header_indices(buf.as_ref(), req.headers, &mut headers_indices);
|
|
|
|
let headers_len = req.headers.len();
|
|
|
|
(len, method, path, version, headers_len)
|
|
|
|
}
|
|
|
|
httparse::Status::Partial => return Ok(None),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let slice = buf.split_to(len).freeze();
|
|
|
|
let path = slice.slice(path.0, path.1);
|
|
|
|
// path was found to be utf8 by httparse
|
2017-10-13 23:43:17 +02:00
|
|
|
let uri = Uri::from_shared(path).map_err(|_| ParseError::Uri)?;
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2017-10-10 08:07:32 +02:00
|
|
|
// convert headers
|
|
|
|
let mut headers = HeaderMap::with_capacity(headers_len);
|
|
|
|
for header in headers_indices[..headers_len].iter() {
|
|
|
|
if let Ok(name) = HeaderName::try_from(slice.slice(header.name.0, header.name.1)) {
|
|
|
|
if let Ok(value) = HeaderValue::try_from(
|
|
|
|
slice.slice(header.value.0, header.value.1))
|
|
|
|
{
|
|
|
|
headers.insert(name, value);
|
|
|
|
} else {
|
2017-10-13 23:43:17 +02:00
|
|
|
return Err(ParseError::Header)
|
2017-10-10 08:07:32 +02:00
|
|
|
}
|
|
|
|
} else {
|
2017-10-13 23:43:17 +02:00
|
|
|
return Err(ParseError::Header)
|
2017-10-10 08:07:32 +02:00
|
|
|
}
|
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
|
|
|
|
let msg = HttpRequest::new(method, uri, version, headers);
|
2017-10-08 06:48:00 +02:00
|
|
|
let upgrade = msg.is_upgrade() || *msg.method() == Method::CONNECT;
|
2017-10-07 06:48:14 +02:00
|
|
|
let chunked = msg.is_chunked()?;
|
|
|
|
|
2017-10-10 08:07:32 +02:00
|
|
|
let decoder = if upgrade {
|
|
|
|
Some(Decoder::eof())
|
2017-10-08 06:48:00 +02:00
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
// Content-Length
|
2017-10-10 08:10:15 +02:00
|
|
|
else if let Some(len) = msg.headers().get(header::CONTENT_LENGTH) {
|
2017-10-07 06:48:14 +02:00
|
|
|
if chunked {
|
2017-10-13 23:43:17 +02:00
|
|
|
return Err(ParseError::Header)
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
2017-10-10 08:07:32 +02:00
|
|
|
if let Ok(s) = len.to_str() {
|
|
|
|
if let Ok(len) = s.parse::<u64>() {
|
|
|
|
Some(Decoder::length(len))
|
|
|
|
} else {
|
|
|
|
debug!("illegal Content-Length: {:?}", len);
|
2017-10-13 23:43:17 +02:00
|
|
|
return Err(ParseError::Header)
|
2017-10-10 08:07:32 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
debug!("illegal Content-Length: {:?}", len);
|
2017-10-13 23:43:17 +02:00
|
|
|
return Err(ParseError::Header)
|
2017-10-10 08:07:32 +02:00
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
} else if chunked {
|
2017-10-10 08:07:32 +02:00
|
|
|
Some(Decoder::chunked())
|
2017-10-07 06:48:14 +02:00
|
|
|
} else {
|
2017-10-10 08:07:32 +02:00
|
|
|
None
|
|
|
|
};
|
|
|
|
Ok(Some((msg, decoder)))
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
struct HeaderIndices {
|
|
|
|
name: (usize, usize),
|
|
|
|
value: (usize, usize),
|
|
|
|
}
|
|
|
|
|
|
|
|
fn record_header_indices(bytes: &[u8],
|
|
|
|
headers: &[httparse::Header],
|
|
|
|
indices: &mut [HeaderIndices])
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|