2018-10-23 03:18:05 +02:00
|
|
|
#![allow(unused_imports, unused_variables, dead_code)]
|
|
|
|
use std::io::{self, Write};
|
|
|
|
|
2018-12-06 23:32:52 +01:00
|
|
|
use bitflags::bitflags;
|
2018-10-23 03:18:05 +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, UPGRADE,
|
|
|
|
};
|
|
|
|
use http::{Method, Version};
|
2018-10-23 03:18:05 +02:00
|
|
|
use tokio_codec::{Decoder, Encoder};
|
|
|
|
|
2018-11-19 23:57:12 +01:00
|
|
|
use super::decoder::{PayloadDecoder, PayloadItem, PayloadType};
|
|
|
|
use super::{decoder, encoder};
|
2018-10-23 03:18:05 +02:00
|
|
|
use super::{Message, MessageType};
|
2018-12-06 23:32:52 +01:00
|
|
|
use crate::body::BodyLength;
|
|
|
|
use crate::client::ClientResponse;
|
|
|
|
use crate::config::ServiceConfig;
|
|
|
|
use crate::error::{ParseError, PayloadError};
|
|
|
|
use crate::helpers;
|
|
|
|
use crate::message::{ConnectionType, Head, MessagePool, RequestHead};
|
2018-10-23 03:18:05 +02:00
|
|
|
|
|
|
|
bitflags! {
|
|
|
|
struct Flags: u8 {
|
|
|
|
const HEAD = 0b0000_0001;
|
|
|
|
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-19 23:57:12 +01:00
|
|
|
decoder: decoder::MessageDecoder<ClientResponse>,
|
2018-10-23 03:18:05 +02:00
|
|
|
payload: Option<PayloadDecoder>,
|
|
|
|
version: Version,
|
2018-11-19 23:57:12 +01:00
|
|
|
ctype: ConnectionType,
|
2018-10-23 03:18:05 +02:00
|
|
|
|
|
|
|
// encoder part
|
|
|
|
flags: Flags,
|
|
|
|
headers_size: u32,
|
2018-11-19 23:57:12 +01:00
|
|
|
encoder: encoder::MessageEncoder<RequestHead>,
|
2018-10-23 03:18:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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-19 23:57:12 +01:00
|
|
|
decoder: decoder::MessageDecoder::default(),
|
2018-11-14 18:38:16 +01:00
|
|
|
payload: None,
|
|
|
|
version: Version::HTTP_11,
|
2018-11-19 23:57:12 +01:00
|
|
|
ctype: ConnectionType::Close,
|
2018-11-14 18:38:16 +01:00
|
|
|
|
|
|
|
flags,
|
|
|
|
headers_size: 0,
|
2018-11-19 23:57:12 +01:00
|
|
|
encoder: encoder::MessageEncoder::default(),
|
2018-11-14 18:38:16 +01:00
|
|
|
},
|
2018-10-23 03:18:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if request is upgrade
|
|
|
|
pub fn upgrade(&self) -> bool {
|
2018-11-19 23:57:12 +01:00
|
|
|
self.inner.ctype == ConnectionType::Upgrade
|
2018-10-23 03:18:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if last response is keep-alive
|
|
|
|
pub fn keepalive(&self) -> bool {
|
2018-11-19 23:57:12 +01:00
|
|
|
self.inner.ctype == ConnectionType::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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 18:38:16 +01:00
|
|
|
/// Convert message codec to a payload codec
|
|
|
|
pub fn into_payload_codec(self) -> ClientPayloadCodec {
|
|
|
|
ClientPayloadCodec { inner: self.inner }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ClientPayloadCodec {
|
2018-11-19 23:57:12 +01:00
|
|
|
/// Check if last response is keep-alive
|
|
|
|
pub fn keepalive(&self) -> bool {
|
|
|
|
self.inner.ctype == ConnectionType::KeepAlive
|
|
|
|
}
|
|
|
|
|
2018-11-14 18:38:16 +01:00
|
|
|
/// 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
|
|
|
|
|
|
|
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-19 23:57:12 +01:00
|
|
|
if let Some(ctype) = req.head().ctype {
|
|
|
|
// do not use peer's keep-alive
|
|
|
|
self.inner.ctype = if ctype == ConnectionType::KeepAlive {
|
|
|
|
self.inner.ctype
|
|
|
|
} else {
|
|
|
|
ctype
|
|
|
|
};
|
2018-10-23 03:18:05 +02:00
|
|
|
}
|
2018-11-19 23:57:12 +01:00
|
|
|
|
|
|
|
if !self.inner.flags.contains(Flags::HEAD) {
|
|
|
|
match payload {
|
|
|
|
PayloadType::None => self.inner.payload = None,
|
|
|
|
PayloadType::Payload(pl) => self.inner.payload = Some(pl),
|
|
|
|
PayloadType::Stream(pl) => {
|
|
|
|
self.inner.payload = Some(pl);
|
|
|
|
self.inner.flags.insert(Flags::STREAM);
|
|
|
|
}
|
2018-10-23 03:18:05 +02:00
|
|
|
}
|
2018-11-19 23:57:12 +01:00
|
|
|
} else {
|
|
|
|
self.inner.payload = None;
|
|
|
|
}
|
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-19 23:57:12 +01:00
|
|
|
Message::Item((mut msg, length)) => {
|
|
|
|
let inner = &mut self.inner;
|
|
|
|
inner.version = msg.version;
|
|
|
|
inner.flags.set(Flags::HEAD, msg.method == Method::HEAD);
|
|
|
|
|
|
|
|
// connection status
|
|
|
|
inner.ctype = match msg.connection_type() {
|
|
|
|
ConnectionType::KeepAlive => {
|
|
|
|
if inner.flags.contains(Flags::KEEPALIVE_ENABLED) {
|
|
|
|
ConnectionType::KeepAlive
|
|
|
|
} else {
|
|
|
|
ConnectionType::Close
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ConnectionType::Upgrade => ConnectionType::Upgrade,
|
|
|
|
ConnectionType::Close => ConnectionType::Close,
|
|
|
|
};
|
|
|
|
|
|
|
|
inner.encoder.encode(
|
|
|
|
dst,
|
|
|
|
&mut msg,
|
|
|
|
false,
|
|
|
|
inner.version,
|
|
|
|
length,
|
|
|
|
inner.ctype,
|
|
|
|
&inner.config,
|
|
|
|
)?;
|
2018-10-23 03:18:05 +02:00
|
|
|
}
|
|
|
|
Message::Chunk(Some(bytes)) => {
|
2018-11-19 23:57:12 +01:00
|
|
|
self.inner.encoder.encode_chunk(bytes.as_ref(), dst)?;
|
2018-10-23 03:18:05 +02:00
|
|
|
}
|
|
|
|
Message::Chunk(None) => {
|
2018-11-19 23:57:12 +01:00
|
|
|
self.inner.encoder.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(())
|
|
|
|
}
|
|
|
|
}
|