2018-10-05 01:22:00 +02:00
|
|
|
#![allow(unused_imports, unused_variables, dead_code)]
|
2018-10-25 01:48:45 +02:00
|
|
|
use std::fmt;
|
2018-10-05 01:22:00 +02:00
|
|
|
use std::io::{self, Write};
|
|
|
|
|
2018-12-11 03:08:33 +01:00
|
|
|
use actix_codec::{Decoder, Encoder};
|
2018-12-06 23:32:52 +01:00
|
|
|
use bitflags::bitflags;
|
2018-10-05 01:22:00 +02:00
|
|
|
use bytes::{BufMut, Bytes, BytesMut};
|
2018-12-06 23:32:52 +01:00
|
|
|
use http::header::{HeaderValue, CONNECTION, CONTENT_LENGTH, DATE, TRANSFER_ENCODING};
|
|
|
|
use http::{Method, StatusCode, Version};
|
2018-10-05 01:22:00 +02:00
|
|
|
|
2018-11-19 23:57:12 +01:00
|
|
|
use super::decoder::{PayloadDecoder, PayloadItem, PayloadType};
|
2019-04-02 20:11:32 +02:00
|
|
|
use super::{decoder, encoder, reserve_readbuf};
|
2018-10-23 03:18:05 +02:00
|
|
|
use super::{Message, MessageType};
|
2019-03-27 17:24:55 +01:00
|
|
|
use crate::body::BodySize;
|
2018-12-06 23:32:52 +01:00
|
|
|
use crate::config::ServiceConfig;
|
|
|
|
use crate::error::ParseError;
|
|
|
|
use crate::helpers;
|
|
|
|
use crate::message::{ConnectionType, Head, ResponseHead};
|
|
|
|
use crate::request::Request;
|
|
|
|
use crate::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 KEEPALIVE_ENABLED = 0b0000_1000;
|
2018-11-14 07:53:30 +01:00
|
|
|
const STREAM = 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-11-19 23:57:12 +01:00
|
|
|
decoder: decoder::MessageDecoder<Request>,
|
2018-10-07 05:31:22 +02:00
|
|
|
payload: Option<PayloadDecoder>,
|
2018-10-05 08:39:11 +02:00
|
|
|
version: Version,
|
2018-11-19 23:57:12 +01:00
|
|
|
ctype: ConnectionType,
|
2018-10-05 19:03:10 +02:00
|
|
|
|
|
|
|
// encoder part
|
|
|
|
flags: Flags,
|
|
|
|
headers_size: u32,
|
2018-11-19 23:57:12 +01:00
|
|
|
encoder: encoder::MessageEncoder<Response<()>>,
|
2018-10-05 01:22:00 +02:00
|
|
|
}
|
|
|
|
|
2018-11-06 04:32:03 +01:00
|
|
|
impl Default for Codec {
|
|
|
|
fn default() -> Self {
|
|
|
|
Codec::new(ServiceConfig::default())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-25 01:48:45 +02:00
|
|
|
impl fmt::Debug for Codec {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "h1::Codec({:?})", self.flags)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
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-11-19 23:57:12 +01:00
|
|
|
decoder: decoder::MessageDecoder::default(),
|
2018-10-07 05:31:22 +02:00
|
|
|
payload: None,
|
2018-10-05 08:39:11 +02:00
|
|
|
version: Version::HTTP_11,
|
2018-11-19 23:57:12 +01:00
|
|
|
ctype: ConnectionType::Close,
|
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-11-19 23:57:12 +01:00
|
|
|
encoder: encoder::MessageEncoder::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 {
|
2018-11-19 23:57:12 +01:00
|
|
|
self.ctype == ConnectionType::Upgrade
|
2018-10-05 01:22:00 +02:00
|
|
|
}
|
|
|
|
|
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-11-19 23:57:12 +01:00
|
|
|
self.ctype == ConnectionType::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-11-14 07:53:30 +01:00
|
|
|
if self.flags.contains(Flags::STREAM) {
|
|
|
|
MessageType::Stream
|
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-05 01:22:00 +02:00
|
|
|
}
|
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)? {
|
2019-04-02 20:11:32 +02:00
|
|
|
Some(PayloadItem::Chunk(chunk)) => {
|
|
|
|
reserve_readbuf(src);
|
|
|
|
Some(Message::Chunk(Some(chunk)))
|
|
|
|
}
|
2018-11-06 04:32:03 +01:00
|
|
|
Some(PayloadItem::Eof) => {
|
|
|
|
self.payload.take();
|
|
|
|
Some(Message::Chunk(None))
|
|
|
|
}
|
2018-10-07 05:31:22 +02:00
|
|
|
None => None,
|
|
|
|
})
|
|
|
|
} else if let Some((req, payload)) = self.decoder.decode(src)? {
|
2019-02-08 06:16:46 +01:00
|
|
|
let head = req.head();
|
|
|
|
self.flags.set(Flags::HEAD, head.method == Method::HEAD);
|
|
|
|
self.version = head.version;
|
|
|
|
self.ctype = head.connection_type();
|
2018-11-19 23:57:12 +01:00
|
|
|
if self.ctype == ConnectionType::KeepAlive
|
|
|
|
&& !self.flags.contains(Flags::KEEPALIVE_ENABLED)
|
|
|
|
{
|
|
|
|
self.ctype = ConnectionType::Close
|
2018-10-07 05:31:22 +02:00
|
|
|
}
|
2018-10-23 03:18:05 +02:00
|
|
|
match payload {
|
|
|
|
PayloadType::None => self.payload = None,
|
|
|
|
PayloadType::Payload(pl) => self.payload = Some(pl),
|
2018-11-14 07:53:30 +01:00
|
|
|
PayloadType::Stream(pl) => {
|
|
|
|
self.payload = Some(pl);
|
|
|
|
self.flags.insert(Flags::STREAM);
|
2018-10-14 08:57:31 +02:00
|
|
|
}
|
2018-10-23 03:18:05 +02:00
|
|
|
}
|
2019-04-02 20:11:32 +02:00
|
|
|
reserve_readbuf(src);
|
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 {
|
2019-03-27 17:24:55 +01:00
|
|
|
type Item = Message<(Response<()>, BodySize)>;
|
2018-10-05 19:03:10 +02:00
|
|
|
type Error = io::Error;
|
|
|
|
|
|
|
|
fn encode(
|
2018-10-30 00:39:46 +01:00
|
|
|
&mut self,
|
|
|
|
item: Self::Item,
|
|
|
|
dst: &mut BytesMut,
|
2018-10-05 19:03:10 +02:00
|
|
|
) -> Result<(), Self::Error> {
|
|
|
|
match item {
|
2018-11-19 02:52:56 +01:00
|
|
|
Message::Item((mut res, length)) => {
|
2019-04-06 01:46:44 +02:00
|
|
|
if res.head().status == StatusCode::CONTINUE {
|
|
|
|
dst.extend_from_slice(b"HTTP/1.1 100 Continue\r\n\r\n");
|
2018-11-19 23:57:12 +01:00
|
|
|
} else {
|
2019-04-06 01:46:44 +02:00
|
|
|
// set response version
|
|
|
|
res.head_mut().version = self.version;
|
|
|
|
|
|
|
|
// connection status
|
|
|
|
self.ctype = if let Some(ct) = res.head().ctype() {
|
|
|
|
if ct == ConnectionType::KeepAlive {
|
|
|
|
self.ctype
|
|
|
|
} else {
|
|
|
|
ct
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.ctype
|
|
|
|
};
|
|
|
|
|
|
|
|
// encode message
|
|
|
|
let len = dst.len();
|
|
|
|
self.encoder.encode(
|
|
|
|
dst,
|
|
|
|
&mut res,
|
|
|
|
self.flags.contains(Flags::HEAD),
|
|
|
|
self.flags.contains(Flags::STREAM),
|
|
|
|
self.version,
|
|
|
|
length,
|
|
|
|
self.ctype,
|
|
|
|
&self.config,
|
|
|
|
)?;
|
|
|
|
self.headers_size = (dst.len() - len) as u32;
|
|
|
|
}
|
2018-10-05 19:03:10 +02:00
|
|
|
}
|
2018-10-23 03:18:05 +02:00
|
|
|
Message::Chunk(Some(bytes)) => {
|
2018-11-19 23:57:12 +01:00
|
|
|
self.encoder.encode_chunk(bytes.as_ref(), dst)?;
|
2018-10-09 00:24:51 +02:00
|
|
|
}
|
2018-10-23 03:18:05 +02:00
|
|
|
Message::Chunk(None) => {
|
2018-11-19 23:57:12 +01:00
|
|
|
self.encoder.encode_eof(dst)?;
|
2018-10-05 19:03:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2018-11-06 04:32:03 +01:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use std::{cmp, io};
|
|
|
|
|
2018-12-11 03:08:33 +01:00
|
|
|
use actix_codec::{AsyncRead, AsyncWrite};
|
2018-11-06 04:32:03 +01:00
|
|
|
use bytes::{Buf, Bytes, BytesMut};
|
|
|
|
use http::{Method, Version};
|
|
|
|
|
|
|
|
use super::*;
|
2018-12-07 00:03:01 +01:00
|
|
|
use crate::error::ParseError;
|
|
|
|
use crate::h1::Message;
|
|
|
|
use crate::httpmessage::HttpMessage;
|
|
|
|
use crate::request::Request;
|
2018-11-06 04:32:03 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_http_request_chunked_payload_and_next_message() {
|
|
|
|
let mut codec = Codec::default();
|
|
|
|
|
|
|
|
let mut buf = BytesMut::from(
|
|
|
|
"GET /test HTTP/1.1\r\n\
|
|
|
|
transfer-encoding: chunked\r\n\r\n",
|
|
|
|
);
|
|
|
|
let item = codec.decode(&mut buf).unwrap().unwrap();
|
|
|
|
let req = item.message();
|
|
|
|
|
|
|
|
assert_eq!(req.method(), Method::GET);
|
|
|
|
assert!(req.chunked().unwrap());
|
|
|
|
|
|
|
|
buf.extend(
|
|
|
|
b"4\r\ndata\r\n4\r\nline\r\n0\r\n\r\n\
|
|
|
|
POST /test2 HTTP/1.1\r\n\
|
|
|
|
transfer-encoding: chunked\r\n\r\n"
|
|
|
|
.iter(),
|
|
|
|
);
|
|
|
|
|
|
|
|
let msg = codec.decode(&mut buf).unwrap().unwrap();
|
|
|
|
assert_eq!(msg.chunk().as_ref(), b"data");
|
|
|
|
|
|
|
|
let msg = codec.decode(&mut buf).unwrap().unwrap();
|
|
|
|
assert_eq!(msg.chunk().as_ref(), b"line");
|
|
|
|
|
|
|
|
let msg = codec.decode(&mut buf).unwrap().unwrap();
|
|
|
|
assert!(msg.eof());
|
|
|
|
|
|
|
|
// decode next message
|
|
|
|
let item = codec.decode(&mut buf).unwrap().unwrap();
|
|
|
|
let req = item.message();
|
|
|
|
assert_eq!(*req.method(), Method::POST);
|
|
|
|
assert!(req.chunked().unwrap());
|
|
|
|
}
|
|
|
|
}
|