2018-10-23 03:18:05 +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-11-17 04:28:07 +01:00
|
|
|
use super::decoder::{MessageDecoder, PayloadDecoder, PayloadItem, PayloadType};
|
2018-11-17 06:30:37 +01:00
|
|
|
use super::encoder::RequestEncoder;
|
2018-10-23 03:18:05 +02:00
|
|
|
use super::{Message, MessageType};
|
2018-11-18 22:48:42 +01:00
|
|
|
use body::BodyLength;
|
2018-11-16 07:34:29 +01:00
|
|
|
use client::ClientResponse;
|
2018-10-23 03:18:05 +02:00
|
|
|
use config::ServiceConfig;
|
2018-11-14 18:38:16 +01:00
|
|
|
use error::{ParseError, PayloadError};
|
2018-10-23 03:18:05 +02:00
|
|
|
use helpers;
|
2018-11-14 07:53:30 +01:00
|
|
|
use http::header::{
|
|
|
|
HeaderValue, CONNECTION, CONTENT_LENGTH, DATE, TRANSFER_ENCODING, UPGRADE,
|
|
|
|
};
|
2018-10-23 03:18:05 +02:00
|
|
|
use http::{Method, Version};
|
2018-11-19 02:52:56 +01:00
|
|
|
use message::{Head, MessagePool, RequestHead};
|
2018-10-23 03:18:05 +02:00
|
|
|
|
|
|
|
bitflags! {
|
|
|
|
struct Flags: u8 {
|
|
|
|
const HEAD = 0b0000_0001;
|
|
|
|
const UPGRADE = 0b0000_0010;
|
|
|
|
const KEEPALIVE = 0b0000_0100;
|
|
|
|
const KEEPALIVE_ENABLED = 0b0000_1000;
|
2018-11-14 07:53:30 +01:00
|
|
|
const STREAM = 0b0001_0000;
|
2018-10-23 03:18:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const AVERAGE_HEADER_SIZE: usize = 30;
|
|
|
|
|
|
|
|
/// HTTP/1 Codec
|
|
|
|
pub struct ClientCodec {
|
2018-11-14 18:38:16 +01:00
|
|
|
inner: ClientCodecInner,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// HTTP/1 Payload Codec
|
|
|
|
pub struct ClientPayloadCodec {
|
|
|
|
inner: ClientCodecInner,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ClientCodecInner {
|
2018-10-23 03:18:05 +02:00
|
|
|
config: ServiceConfig,
|
2018-11-17 04:28:07 +01:00
|
|
|
decoder: MessageDecoder<ClientResponse>,
|
2018-10-23 03:18:05 +02:00
|
|
|
payload: Option<PayloadDecoder>,
|
|
|
|
version: Version,
|
|
|
|
|
|
|
|
// encoder part
|
|
|
|
flags: Flags,
|
|
|
|
headers_size: u32,
|
|
|
|
te: RequestEncoder,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for ClientCodec {
|
|
|
|
fn default() -> Self {
|
|
|
|
ClientCodec::new(ServiceConfig::default())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ClientCodec {
|
|
|
|
/// Create HTTP/1 codec.
|
|
|
|
///
|
|
|
|
/// `keepalive_enabled` how response `connection` header get generated.
|
|
|
|
pub fn new(config: ServiceConfig) -> Self {
|
|
|
|
let flags = if config.keep_alive_enabled() {
|
|
|
|
Flags::KEEPALIVE_ENABLED
|
|
|
|
} else {
|
|
|
|
Flags::empty()
|
|
|
|
};
|
|
|
|
ClientCodec {
|
2018-11-14 18:38:16 +01:00
|
|
|
inner: ClientCodecInner {
|
|
|
|
config,
|
2018-11-17 04:28:07 +01:00
|
|
|
decoder: MessageDecoder::default(),
|
2018-11-14 18:38:16 +01:00
|
|
|
payload: None,
|
|
|
|
version: Version::HTTP_11,
|
|
|
|
|
|
|
|
flags,
|
|
|
|
headers_size: 0,
|
|
|
|
te: RequestEncoder::default(),
|
|
|
|
},
|
2018-10-23 03:18:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if request is upgrade
|
|
|
|
pub fn upgrade(&self) -> bool {
|
2018-11-14 18:38:16 +01:00
|
|
|
self.inner.flags.contains(Flags::UPGRADE)
|
2018-10-23 03:18:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if last response is keep-alive
|
|
|
|
pub fn keepalive(&self) -> bool {
|
2018-11-14 18:38:16 +01:00
|
|
|
self.inner.flags.contains(Flags::KEEPALIVE)
|
2018-10-23 03:18:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Check last request's message type
|
|
|
|
pub fn message_type(&self) -> MessageType {
|
2018-11-14 18:38:16 +01:00
|
|
|
if self.inner.flags.contains(Flags::STREAM) {
|
2018-11-14 07:53:30 +01:00
|
|
|
MessageType::Stream
|
2018-11-14 18:38:16 +01:00
|
|
|
} else if self.inner.payload.is_none() {
|
2018-10-23 03:18:05 +02:00
|
|
|
MessageType::None
|
|
|
|
} else {
|
|
|
|
MessageType::Payload
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// prepare transfer encoding
|
2018-11-17 06:30:37 +01:00
|
|
|
pub fn prepare_te(&mut self, head: &mut RequestHead, length: BodyLength) {
|
2018-11-14 18:38:16 +01:00
|
|
|
self.inner.te.update(
|
|
|
|
head,
|
|
|
|
self.inner.flags.contains(Flags::HEAD),
|
|
|
|
self.inner.version,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convert message codec to a payload codec
|
|
|
|
pub fn into_payload_codec(self) -> ClientPayloadCodec {
|
|
|
|
ClientPayloadCodec { inner: self.inner }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ClientPayloadCodec {
|
|
|
|
/// Transform payload codec to a message codec
|
|
|
|
pub fn into_message_codec(self) -> ClientCodec {
|
|
|
|
ClientCodec { inner: self.inner }
|
2018-10-23 03:18:05 +02:00
|
|
|
}
|
2018-11-14 18:38:16 +01:00
|
|
|
}
|
2018-10-23 03:18:05 +02:00
|
|
|
|
2018-11-17 06:09:33 +01:00
|
|
|
fn prn_version(ver: Version) -> &'static str {
|
|
|
|
match ver {
|
|
|
|
Version::HTTP_09 => "HTTP/0.9",
|
|
|
|
Version::HTTP_10 => "HTTP/1.0",
|
|
|
|
Version::HTTP_11 => "HTTP/1.1",
|
|
|
|
Version::HTTP_2 => "HTTP/2.0",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 18:38:16 +01:00
|
|
|
impl ClientCodecInner {
|
2018-11-19 02:52:56 +01:00
|
|
|
fn encode_request(
|
2018-10-30 00:39:46 +01:00
|
|
|
&mut self,
|
2018-11-14 07:53:30 +01:00
|
|
|
msg: RequestHead,
|
2018-11-17 06:30:37 +01:00
|
|
|
length: BodyLength,
|
2018-10-30 00:39:46 +01:00
|
|
|
buffer: &mut BytesMut,
|
2018-10-23 03:18:05 +02:00
|
|
|
) -> io::Result<()> {
|
|
|
|
// render message
|
|
|
|
{
|
|
|
|
// status line
|
2018-11-17 06:09:33 +01:00
|
|
|
write!(
|
2018-10-23 03:18:05 +02:00
|
|
|
Writer(buffer),
|
2018-11-19 02:52:56 +01:00
|
|
|
"{} {} {}\r\n",
|
2018-11-14 07:53:30 +01:00
|
|
|
msg.method,
|
|
|
|
msg.uri.path_and_query().map(|u| u.as_str()).unwrap_or("/"),
|
2018-11-17 06:09:33 +01:00
|
|
|
prn_version(msg.version)
|
2018-10-23 03:18:05 +02:00
|
|
|
).map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
|
|
|
|
|
|
|
|
// write headers
|
2018-11-14 07:53:30 +01:00
|
|
|
buffer.reserve(msg.headers.len() * AVERAGE_HEADER_SIZE);
|
2018-11-17 06:09:33 +01:00
|
|
|
|
|
|
|
// content length
|
2018-11-17 06:30:37 +01:00
|
|
|
match length {
|
|
|
|
BodyLength::Sized(len) => helpers::write_content_length(len, buffer),
|
|
|
|
BodyLength::Sized64(len) => {
|
2018-11-19 02:52:56 +01:00
|
|
|
buffer.extend_from_slice(b"content-length: ");
|
2018-11-17 06:09:33 +01:00
|
|
|
write!(buffer.writer(), "{}", len)?;
|
|
|
|
buffer.extend_from_slice(b"\r\n");
|
|
|
|
}
|
2018-11-18 05:21:28 +01:00
|
|
|
BodyLength::Chunked => {
|
2018-11-19 02:52:56 +01:00
|
|
|
buffer.extend_from_slice(b"transfer-encoding: chunked\r\n")
|
2018-11-18 05:21:28 +01:00
|
|
|
}
|
2018-11-19 02:52:56 +01:00
|
|
|
BodyLength::Empty => buffer.extend_from_slice(b"content-length: 0\r\n"),
|
|
|
|
BodyLength::None | BodyLength::Stream => (),
|
2018-11-17 06:09:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut has_date = false;
|
|
|
|
|
2018-11-14 07:53:30 +01:00
|
|
|
for (key, value) in &msg.headers {
|
2018-11-17 06:09:33 +01:00
|
|
|
match *key {
|
2018-11-19 02:52:56 +01:00
|
|
|
TRANSFER_ENCODING | CONNECTION | CONTENT_LENGTH => continue,
|
2018-11-17 06:09:33 +01:00
|
|
|
DATE => has_date = true,
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer.put_slice(key.as_ref());
|
2018-10-23 03:18:05 +02:00
|
|
|
buffer.put_slice(b": ");
|
2018-11-17 06:09:33 +01:00
|
|
|
buffer.put_slice(value.as_ref());
|
2018-10-23 03:18:05 +02:00
|
|
|
buffer.put_slice(b"\r\n");
|
2018-11-17 06:09:33 +01:00
|
|
|
}
|
2018-11-14 07:53:30 +01:00
|
|
|
|
2018-11-19 02:52:56 +01:00
|
|
|
// Connection header
|
|
|
|
if msg.upgrade() {
|
|
|
|
self.flags.set(Flags::UPGRADE, msg.upgrade());
|
|
|
|
buffer.extend_from_slice(b"connection: upgrade\r\n");
|
|
|
|
} else if msg.keep_alive() {
|
|
|
|
if self.version < Version::HTTP_11 {
|
|
|
|
buffer.extend_from_slice(b"connection: keep-alive\r\n");
|
|
|
|
}
|
|
|
|
} else if self.version >= Version::HTTP_11 {
|
|
|
|
buffer.extend_from_slice(b"connection: close\r\n");
|
2018-10-23 03:18:05 +02:00
|
|
|
}
|
|
|
|
|
2018-11-19 02:52:56 +01:00
|
|
|
// Date header
|
2018-11-17 06:09:33 +01:00
|
|
|
if !has_date {
|
2018-10-23 03:18:05 +02:00
|
|
|
self.config.set_date(buffer);
|
|
|
|
} else {
|
|
|
|
buffer.extend_from_slice(b"\r\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Decoder for ClientCodec {
|
2018-11-14 19:52:40 +01:00
|
|
|
type Item = ClientResponse;
|
2018-10-23 03:18:05 +02:00
|
|
|
type Error = ParseError;
|
|
|
|
|
|
|
|
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
|
2018-11-14 19:52:40 +01:00
|
|
|
debug_assert!(!self.inner.payload.is_some(), "Payload decoder is set");
|
|
|
|
|
|
|
|
if let Some((req, payload)) = self.inner.decoder.decode(src)? {
|
2018-11-17 04:28:07 +01:00
|
|
|
// self.inner
|
|
|
|
// .flags
|
|
|
|
// .set(Flags::HEAD, req.head.method == Method::HEAD);
|
|
|
|
// self.inner.version = req.head.version;
|
2018-11-14 18:38:16 +01:00
|
|
|
if self.inner.flags.contains(Flags::KEEPALIVE_ENABLED) {
|
|
|
|
self.inner.flags.set(Flags::KEEPALIVE, req.keep_alive());
|
2018-10-23 03:18:05 +02:00
|
|
|
}
|
|
|
|
match payload {
|
2018-11-14 18:38:16 +01:00
|
|
|
PayloadType::None => self.inner.payload = None,
|
|
|
|
PayloadType::Payload(pl) => self.inner.payload = Some(pl),
|
2018-11-14 07:53:30 +01:00
|
|
|
PayloadType::Stream(pl) => {
|
2018-11-14 18:38:16 +01:00
|
|
|
self.inner.payload = Some(pl);
|
|
|
|
self.inner.flags.insert(Flags::STREAM);
|
2018-10-23 03:18:05 +02:00
|
|
|
}
|
|
|
|
};
|
2018-11-14 19:52:40 +01:00
|
|
|
Ok(Some(req))
|
2018-10-23 03:18:05 +02:00
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 18:38:16 +01:00
|
|
|
impl Decoder for ClientPayloadCodec {
|
|
|
|
type Item = Option<Bytes>;
|
|
|
|
type Error = PayloadError;
|
|
|
|
|
|
|
|
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
|
2018-11-14 19:52:40 +01:00
|
|
|
debug_assert!(
|
2018-11-14 18:38:16 +01:00
|
|
|
self.inner.payload.is_some(),
|
|
|
|
"Payload decoder is not specified"
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(match self.inner.payload.as_mut().unwrap().decode(src)? {
|
|
|
|
Some(PayloadItem::Chunk(chunk)) => Some(Some(chunk)),
|
|
|
|
Some(PayloadItem::Eof) => {
|
|
|
|
self.inner.payload.take();
|
|
|
|
Some(None)
|
|
|
|
}
|
|
|
|
None => None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-23 03:18:05 +02:00
|
|
|
impl Encoder for ClientCodec {
|
2018-11-17 06:30:37 +01:00
|
|
|
type Item = Message<(RequestHead, BodyLength)>;
|
2018-10-23 03:18:05 +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-23 03:18:05 +02:00
|
|
|
) -> Result<(), Self::Error> {
|
|
|
|
match item {
|
2018-11-14 07:53:30 +01:00
|
|
|
Message::Item((msg, btype)) => {
|
2018-11-19 02:52:56 +01:00
|
|
|
self.inner.encode_request(msg, btype, dst)?;
|
2018-10-23 03:18:05 +02:00
|
|
|
}
|
|
|
|
Message::Chunk(Some(bytes)) => {
|
2018-11-14 18:38:16 +01:00
|
|
|
self.inner.te.encode(bytes.as_ref(), dst)?;
|
2018-10-23 03:18:05 +02:00
|
|
|
}
|
|
|
|
Message::Chunk(None) => {
|
2018-11-14 18:38:16 +01:00
|
|
|
self.inner.te.encode_eof(dst)?;
|
2018-10-23 03:18:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Writer<'a>(pub &'a mut BytesMut);
|
|
|
|
|
|
|
|
impl<'a> io::Write for Writer<'a> {
|
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
|
|
|
self.0.extend_from_slice(buf);
|
|
|
|
Ok(buf.len())
|
|
|
|
}
|
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|