2018-10-05 01:22:00 +02:00
|
|
|
#![allow(unused_imports, unused_variables, dead_code)]
|
|
|
|
use std::io::{self, Write};
|
|
|
|
|
|
|
|
use bytes::{BufMut, Bytes, BytesMut};
|
|
|
|
use tokio_codec::{Decoder, Encoder};
|
|
|
|
|
2018-10-23 03:18:05 +02:00
|
|
|
use super::decoder::{PayloadDecoder, PayloadItem, PayloadType, RequestDecoder};
|
2018-10-05 19:03:10 +02:00
|
|
|
use super::encoder::{ResponseEncoder, ResponseLength};
|
2018-10-23 03:18:05 +02:00
|
|
|
use super::{Message, MessageType};
|
2018-10-09 00:24:51 +02:00
|
|
|
use body::{Binary, Body};
|
2018-10-08 19:14:29 +02:00
|
|
|
use config::ServiceConfig;
|
2018-10-05 01:22:00 +02:00
|
|
|
use error::ParseError;
|
2018-10-05 05:02:10 +02:00
|
|
|
use helpers;
|
2018-10-05 01:22:00 +02:00
|
|
|
use http::header::{HeaderValue, CONNECTION, CONTENT_LENGTH, DATE, TRANSFER_ENCODING};
|
2018-10-05 08:39:11 +02:00
|
|
|
use http::{Method, Version};
|
2018-10-23 03:18:05 +02:00
|
|
|
use request::{MessagePool, Request};
|
2018-10-05 20:04:59 +02:00
|
|
|
use response::Response;
|
2018-10-05 01:22:00 +02:00
|
|
|
|
2018-10-05 19:03:10 +02:00
|
|
|
bitflags! {
|
|
|
|
struct Flags: u8 {
|
2018-10-14 08:57:31 +02:00
|
|
|
const HEAD = 0b0000_0001;
|
|
|
|
const UPGRADE = 0b0000_0010;
|
|
|
|
const KEEPALIVE = 0b0000_0100;
|
|
|
|
const KEEPALIVE_ENABLED = 0b0000_1000;
|
|
|
|
const UNHANDLED = 0b0001_0000;
|
2018-10-05 19:03:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const AVERAGE_HEADER_SIZE: usize = 30;
|
|
|
|
|
2018-10-05 05:02:10 +02:00
|
|
|
/// HTTP/1 Codec
|
|
|
|
pub struct Codec {
|
2018-10-08 19:14:29 +02:00
|
|
|
config: ServiceConfig,
|
2018-10-07 05:31:22 +02:00
|
|
|
decoder: RequestDecoder,
|
|
|
|
payload: Option<PayloadDecoder>,
|
2018-10-05 08:39:11 +02:00
|
|
|
version: Version,
|
2018-10-05 19:03:10 +02:00
|
|
|
|
|
|
|
// encoder part
|
|
|
|
flags: Flags,
|
|
|
|
headers_size: u32,
|
|
|
|
te: ResponseEncoder,
|
2018-10-05 01:22:00 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 05:02:10 +02:00
|
|
|
impl Codec {
|
2018-10-05 19:03:10 +02:00
|
|
|
/// Create HTTP/1 codec.
|
|
|
|
///
|
|
|
|
/// `keepalive_enabled` how response `connection` header get generated.
|
2018-10-08 19:14:29 +02:00
|
|
|
pub fn new(config: ServiceConfig) -> Self {
|
2018-10-23 03:18:05 +02:00
|
|
|
Codec::with_pool(MessagePool::pool(), config)
|
2018-10-05 05:02:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create HTTP/1 codec with request's pool
|
2018-10-23 03:18:05 +02:00
|
|
|
pub(crate) fn with_pool(pool: &'static MessagePool, config: ServiceConfig) -> Self {
|
2018-10-08 19:14:29 +02:00
|
|
|
let flags = if config.keep_alive_enabled() {
|
2018-10-05 19:03:10 +02:00
|
|
|
Flags::KEEPALIVE_ENABLED
|
|
|
|
} else {
|
|
|
|
Flags::empty()
|
|
|
|
};
|
2018-10-05 05:02:10 +02:00
|
|
|
Codec {
|
2018-10-08 19:14:29 +02:00
|
|
|
config,
|
2018-10-07 05:31:22 +02:00
|
|
|
decoder: RequestDecoder::with_pool(pool),
|
|
|
|
payload: None,
|
2018-10-05 08:39:11 +02:00
|
|
|
version: Version::HTTP_11,
|
2018-10-05 01:22:00 +02:00
|
|
|
|
2018-10-05 19:03:10 +02:00
|
|
|
flags,
|
2018-10-05 01:22:00 +02:00
|
|
|
headers_size: 0,
|
2018-10-05 19:03:10 +02:00
|
|
|
te: ResponseEncoder::default(),
|
2018-10-05 01:22:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-07 07:36:57 +02:00
|
|
|
/// Check if request is upgrade
|
2018-10-05 01:22:00 +02:00
|
|
|
pub fn upgrade(&self) -> bool {
|
|
|
|
self.flags.contains(Flags::UPGRADE)
|
|
|
|
}
|
|
|
|
|
2018-10-07 07:36:57 +02:00
|
|
|
/// Check if last response is keep-alive
|
2018-10-05 01:22:00 +02:00
|
|
|
pub fn keepalive(&self) -> bool {
|
2018-10-05 19:03:10 +02:00
|
|
|
self.flags.contains(Flags::KEEPALIVE)
|
2018-10-05 01:22:00 +02:00
|
|
|
}
|
|
|
|
|
2018-10-16 00:56:47 +02:00
|
|
|
/// Check last request's message type
|
2018-10-23 03:18:05 +02:00
|
|
|
pub fn message_type(&self) -> MessageType {
|
2018-10-16 00:56:47 +02:00
|
|
|
if self.flags.contains(Flags::UNHANDLED) {
|
2018-10-23 03:18:05 +02:00
|
|
|
MessageType::Unhandled
|
2018-10-16 00:56:47 +02:00
|
|
|
} else if self.payload.is_none() {
|
2018-10-23 03:18:05 +02:00
|
|
|
MessageType::None
|
2018-10-16 00:56:47 +02:00
|
|
|
} else {
|
2018-10-23 03:18:05 +02:00
|
|
|
MessageType::Payload
|
2018-10-16 00:56:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-07 07:36:57 +02:00
|
|
|
/// prepare transfer encoding
|
|
|
|
pub fn prepare_te(&mut self, res: &mut Response) {
|
|
|
|
self.te
|
|
|
|
.update(res, self.flags.contains(Flags::HEAD), self.version);
|
|
|
|
}
|
|
|
|
|
2018-10-05 19:03:10 +02:00
|
|
|
fn encode_response(
|
2018-10-05 20:04:59 +02:00
|
|
|
&mut self, mut msg: Response, buffer: &mut BytesMut,
|
2018-10-05 01:22:00 +02:00
|
|
|
) -> io::Result<()> {
|
2018-10-05 19:03:10 +02:00
|
|
|
let ka = self.flags.contains(Flags::KEEPALIVE_ENABLED) && msg
|
|
|
|
.keep_alive()
|
|
|
|
.unwrap_or_else(|| self.flags.contains(Flags::KEEPALIVE));
|
2018-10-05 01:22:00 +02:00
|
|
|
|
|
|
|
// Connection upgrade
|
2018-10-05 19:03:10 +02:00
|
|
|
let version = msg.version().unwrap_or_else(|| self.version);
|
2018-10-05 01:22:00 +02:00
|
|
|
if msg.upgrade() {
|
|
|
|
self.flags.insert(Flags::UPGRADE);
|
2018-10-05 19:03:10 +02:00
|
|
|
self.flags.remove(Flags::KEEPALIVE);
|
2018-10-05 01:22:00 +02:00
|
|
|
msg.headers_mut()
|
|
|
|
.insert(CONNECTION, HeaderValue::from_static("upgrade"));
|
|
|
|
}
|
|
|
|
// keep-alive
|
2018-10-05 19:03:10 +02:00
|
|
|
else if ka {
|
|
|
|
self.flags.insert(Flags::KEEPALIVE);
|
2018-10-05 01:22:00 +02:00
|
|
|
if version < Version::HTTP_11 {
|
|
|
|
msg.headers_mut()
|
|
|
|
.insert(CONNECTION, HeaderValue::from_static("keep-alive"));
|
|
|
|
}
|
|
|
|
} else if version >= Version::HTTP_11 {
|
2018-10-05 19:03:10 +02:00
|
|
|
self.flags.remove(Flags::KEEPALIVE);
|
2018-10-05 01:22:00 +02:00
|
|
|
msg.headers_mut()
|
|
|
|
.insert(CONNECTION, HeaderValue::from_static("close"));
|
|
|
|
}
|
|
|
|
|
|
|
|
// render message
|
|
|
|
{
|
|
|
|
let reason = msg.reason().as_bytes();
|
2018-10-07 07:36:57 +02:00
|
|
|
if let Body::Binary(ref bytes) = msg.body() {
|
2018-10-05 01:22:00 +02:00
|
|
|
buffer.reserve(
|
|
|
|
256 + msg.headers().len() * AVERAGE_HEADER_SIZE
|
|
|
|
+ bytes.len()
|
|
|
|
+ reason.len(),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
buffer.reserve(
|
|
|
|
256 + msg.headers().len() * AVERAGE_HEADER_SIZE + reason.len(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// status line
|
|
|
|
helpers::write_status_line(version, msg.status().as_u16(), buffer);
|
|
|
|
buffer.extend_from_slice(reason);
|
|
|
|
|
|
|
|
// content length
|
2018-10-09 00:24:51 +02:00
|
|
|
let mut len_is_set = true;
|
2018-10-05 19:03:10 +02:00
|
|
|
match self.te.length {
|
2018-10-05 01:22:00 +02:00
|
|
|
ResponseLength::Chunked => {
|
|
|
|
buffer.extend_from_slice(b"\r\ntransfer-encoding: chunked\r\n")
|
|
|
|
}
|
|
|
|
ResponseLength::Zero => {
|
2018-10-09 00:33:38 +02:00
|
|
|
len_is_set = false;
|
|
|
|
buffer.extend_from_slice(b"\r\n")
|
2018-10-05 01:22:00 +02:00
|
|
|
}
|
|
|
|
ResponseLength::Length(len) => {
|
|
|
|
helpers::write_content_length(len, buffer)
|
|
|
|
}
|
|
|
|
ResponseLength::Length64(len) => {
|
|
|
|
buffer.extend_from_slice(b"\r\ncontent-length: ");
|
|
|
|
write!(buffer.writer(), "{}", len)?;
|
|
|
|
buffer.extend_from_slice(b"\r\n");
|
|
|
|
}
|
|
|
|
ResponseLength::None => buffer.extend_from_slice(b"\r\n"),
|
|
|
|
}
|
|
|
|
|
|
|
|
// write headers
|
|
|
|
let mut pos = 0;
|
|
|
|
let mut has_date = false;
|
|
|
|
let mut remaining = buffer.remaining_mut();
|
|
|
|
let mut buf = unsafe { &mut *(buffer.bytes_mut() as *mut [u8]) };
|
|
|
|
for (key, value) in msg.headers() {
|
|
|
|
match *key {
|
|
|
|
TRANSFER_ENCODING => continue,
|
2018-10-05 19:03:10 +02:00
|
|
|
CONTENT_LENGTH => match self.te.length {
|
2018-10-05 01:22:00 +02:00
|
|
|
ResponseLength::None => (),
|
2018-10-09 00:33:38 +02:00
|
|
|
ResponseLength::Zero => {
|
2018-10-09 00:24:51 +02:00
|
|
|
len_is_set = true;
|
|
|
|
}
|
2018-10-05 01:22:00 +02:00
|
|
|
_ => continue,
|
|
|
|
},
|
|
|
|
DATE => {
|
|
|
|
has_date = true;
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
|
|
|
let v = value.as_ref();
|
|
|
|
let k = key.as_str().as_bytes();
|
|
|
|
let len = k.len() + v.len() + 4;
|
|
|
|
if len > remaining {
|
|
|
|
unsafe {
|
|
|
|
buffer.advance_mut(pos);
|
|
|
|
}
|
|
|
|
pos = 0;
|
|
|
|
buffer.reserve(len);
|
|
|
|
remaining = buffer.remaining_mut();
|
|
|
|
unsafe {
|
|
|
|
buf = &mut *(buffer.bytes_mut() as *mut _);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[pos..pos + k.len()].copy_from_slice(k);
|
|
|
|
pos += k.len();
|
|
|
|
buf[pos..pos + 2].copy_from_slice(b": ");
|
|
|
|
pos += 2;
|
|
|
|
buf[pos..pos + v.len()].copy_from_slice(v);
|
|
|
|
pos += v.len();
|
|
|
|
buf[pos..pos + 2].copy_from_slice(b"\r\n");
|
|
|
|
pos += 2;
|
|
|
|
remaining -= len;
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
buffer.advance_mut(pos);
|
|
|
|
}
|
2018-10-09 00:24:51 +02:00
|
|
|
if !len_is_set {
|
|
|
|
buffer.extend_from_slice(b"content-length: 0\r\n")
|
|
|
|
}
|
2018-10-05 01:22:00 +02:00
|
|
|
|
|
|
|
// optimized date header, set_date writes \r\n
|
|
|
|
if !has_date {
|
2018-10-08 19:14:29 +02:00
|
|
|
self.config.set_date(buffer);
|
2018-10-05 01:22:00 +02:00
|
|
|
} else {
|
|
|
|
// msg eof
|
|
|
|
buffer.extend_from_slice(b"\r\n");
|
|
|
|
}
|
|
|
|
self.headers_size = buffer.len() as u32;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2018-10-05 19:03:10 +02:00
|
|
|
|
|
|
|
impl Decoder for Codec {
|
2018-10-23 03:18:05 +02:00
|
|
|
type Item = Message<Request>;
|
2018-10-05 19:03:10 +02:00
|
|
|
type Error = ParseError;
|
|
|
|
|
|
|
|
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
|
2018-10-07 05:31:22 +02:00
|
|
|
if self.payload.is_some() {
|
|
|
|
Ok(match self.payload.as_mut().unwrap().decode(src)? {
|
2018-10-23 03:18:05 +02:00
|
|
|
Some(PayloadItem::Chunk(chunk)) => Some(Message::Chunk(Some(chunk))),
|
|
|
|
Some(PayloadItem::Eof) => Some(Message::Chunk(None)),
|
2018-10-07 05:31:22 +02:00
|
|
|
None => None,
|
|
|
|
})
|
2018-10-14 08:57:31 +02:00
|
|
|
} else if self.flags.contains(Flags::UNHANDLED) {
|
|
|
|
Ok(None)
|
2018-10-07 05:31:22 +02:00
|
|
|
} else if let Some((req, payload)) = self.decoder.decode(src)? {
|
|
|
|
self.flags
|
|
|
|
.set(Flags::HEAD, req.inner.method == Method::HEAD);
|
|
|
|
self.version = req.inner.version;
|
|
|
|
if self.flags.contains(Flags::KEEPALIVE_ENABLED) {
|
|
|
|
self.flags.set(Flags::KEEPALIVE, req.keep_alive());
|
|
|
|
}
|
2018-10-23 03:18:05 +02:00
|
|
|
match payload {
|
|
|
|
PayloadType::None => self.payload = None,
|
|
|
|
PayloadType::Payload(pl) => self.payload = Some(pl),
|
|
|
|
PayloadType::Unhandled => {
|
2018-10-14 08:57:31 +02:00
|
|
|
self.payload = None;
|
2018-10-16 00:56:47 +02:00
|
|
|
self.flags.insert(Flags::UNHANDLED);
|
2018-10-14 08:57:31 +02:00
|
|
|
}
|
2018-10-23 03:18:05 +02:00
|
|
|
}
|
|
|
|
Ok(Some(Message::Item(req)))
|
2018-10-07 05:31:22 +02:00
|
|
|
} else {
|
|
|
|
Ok(None)
|
2018-10-05 19:03:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Encoder for Codec {
|
2018-10-23 03:18:05 +02:00
|
|
|
type Item = Message<Response>;
|
2018-10-05 19:03:10 +02:00
|
|
|
type Error = io::Error;
|
|
|
|
|
|
|
|
fn encode(
|
|
|
|
&mut self, item: Self::Item, dst: &mut BytesMut,
|
|
|
|
) -> Result<(), Self::Error> {
|
|
|
|
match item {
|
2018-10-23 03:18:05 +02:00
|
|
|
Message::Item(res) => {
|
2018-10-05 19:03:10 +02:00
|
|
|
self.encode_response(res, dst)?;
|
|
|
|
}
|
2018-10-23 03:18:05 +02:00
|
|
|
Message::Chunk(Some(bytes)) => {
|
2018-10-09 00:24:51 +02:00
|
|
|
self.te.encode(bytes.as_ref(), dst)?;
|
|
|
|
}
|
2018-10-23 03:18:05 +02:00
|
|
|
Message::Chunk(None) => {
|
2018-10-09 00:24:51 +02:00
|
|
|
self.te.encode_eof(dst)?;
|
2018-10-05 19:03:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|