2022-01-31 18:30:34 +01:00
|
|
|
use std::{fmt, io};
|
2018-10-23 03:18:05 +02:00
|
|
|
|
2018-12-11 03:08:33 +01:00
|
|
|
use actix_codec::{Decoder, Encoder};
|
2018-12-06 23:32:52 +01:00
|
|
|
use bitflags::bitflags;
|
2019-12-13 05:59:02 +01:00
|
|
|
use bytes::{Bytes, BytesMut};
|
2018-12-06 23:32:52 +01:00
|
|
|
use http::{Method, Version};
|
2018-10-23 03:18:05 +02:00
|
|
|
|
2021-12-19 18:05:27 +01:00
|
|
|
use super::{
|
|
|
|
decoder::{self, PayloadDecoder, PayloadItem, PayloadType},
|
|
|
|
encoder, reserve_readbuf, Message, MessageType,
|
|
|
|
};
|
|
|
|
use crate::{
|
|
|
|
body::BodySize,
|
|
|
|
error::{ParseError, PayloadError},
|
|
|
|
ConnectionType, RequestHeadType, ResponseHead, ServiceConfig,
|
|
|
|
};
|
2018-10-23 03:18:05 +02:00
|
|
|
|
|
|
|
bitflags! {
|
|
|
|
struct Flags: u8 {
|
2022-01-31 18:30:34 +01:00
|
|
|
const HEAD = 0b0000_0001;
|
|
|
|
const KEEP_ALIVE_ENABLED = 0b0000_1000;
|
|
|
|
const STREAM = 0b0001_0000;
|
2018-10-23 03:18:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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,
|
2019-03-26 19:43:22 +01:00
|
|
|
decoder: decoder::MessageDecoder<ResponseHead>,
|
2018-10-23 03:18:05 +02:00
|
|
|
payload: Option<PayloadDecoder>,
|
|
|
|
version: Version,
|
2022-01-31 18:30:34 +01:00
|
|
|
conn_type: ConnectionType,
|
2018-10-23 03:18:05 +02:00
|
|
|
|
|
|
|
// encoder part
|
|
|
|
flags: Flags,
|
2019-09-10 06:29:32 +02:00
|
|
|
encoder: encoder::MessageEncoder<RequestHeadType>,
|
2018-10-23 03:18:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for ClientCodec {
|
|
|
|
fn default() -> Self {
|
|
|
|
ClientCodec::new(ServiceConfig::default())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-31 18:30:34 +01:00
|
|
|
impl fmt::Debug for ClientCodec {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
f.debug_struct("h1::ClientCodec")
|
|
|
|
.field("flags", &self.inner.flags)
|
|
|
|
.finish_non_exhaustive()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-23 03:18:05 +02:00
|
|
|
impl ClientCodec {
|
|
|
|
/// Create HTTP/1 codec.
|
|
|
|
///
|
|
|
|
/// `keepalive_enabled` how response `connection` header get generated.
|
|
|
|
pub fn new(config: ServiceConfig) -> Self {
|
2022-01-31 18:30:34 +01:00
|
|
|
let flags = if config.keep_alive().enabled() {
|
|
|
|
Flags::KEEP_ALIVE_ENABLED
|
2018-10-23 03:18:05 +02:00
|
|
|
} else {
|
|
|
|
Flags::empty()
|
|
|
|
};
|
2022-01-31 18:30:34 +01:00
|
|
|
|
2018-10-23 03:18:05 +02:00
|
|
|
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,
|
2022-01-31 18:30:34 +01:00
|
|
|
conn_type: ConnectionType::Close,
|
2018-11-14 18:38:16 +01:00
|
|
|
|
|
|
|
flags,
|
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 {
|
2022-01-31 18:30:34 +01:00
|
|
|
self.inner.conn_type == ConnectionType::Upgrade
|
2018-10-23 03:18:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if last response is keep-alive
|
2022-01-31 18:30:34 +01:00
|
|
|
pub fn keep_alive(&self) -> bool {
|
|
|
|
self.inner.conn_type == 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
|
2022-01-31 18:30:34 +01:00
|
|
|
pub fn keep_alive(&self) -> bool {
|
|
|
|
self.inner.conn_type == ConnectionType::KeepAlive
|
2018-11-19 23:57:12 +01:00
|
|
|
}
|
|
|
|
|
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 {
|
2019-03-26 19:43:22 +01:00
|
|
|
type Item = ResponseHead;
|
2018-10-23 03:18:05 +02:00
|
|
|
type Error = ParseError;
|
|
|
|
|
|
|
|
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
|
2022-02-08 09:00:47 +01:00
|
|
|
debug_assert!(
|
|
|
|
self.inner.payload.is_none(),
|
|
|
|
"Payload decoder should not be set"
|
|
|
|
);
|
2018-11-14 19:52:40 +01:00
|
|
|
|
|
|
|
if let Some((req, payload)) = self.inner.decoder.decode(src)? {
|
2022-01-31 18:30:34 +01:00
|
|
|
if let Some(conn_type) = req.conn_type() {
|
2018-11-19 23:57:12 +01:00
|
|
|
// do not use peer's keep-alive
|
2022-01-31 18:30:34 +01:00
|
|
|
self.inner.conn_type = if conn_type == ConnectionType::KeepAlive {
|
|
|
|
self.inner.conn_type
|
2018-11-19 23:57:12 +01:00
|
|
|
} else {
|
2022-01-31 18:30:34 +01:00
|
|
|
conn_type
|
2018-11-19 23:57:12 +01:00
|
|
|
};
|
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;
|
|
|
|
}
|
2019-04-02 20:11:32 +02:00
|
|
|
reserve_readbuf(src);
|
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)? {
|
2019-04-02 20:11:32 +02:00
|
|
|
Some(PayloadItem::Chunk(chunk)) => {
|
|
|
|
reserve_readbuf(src);
|
|
|
|
Some(Some(chunk))
|
|
|
|
}
|
2018-11-14 18:38:16 +01:00
|
|
|
Some(PayloadItem::Eof) => {
|
|
|
|
self.inner.payload.take();
|
|
|
|
Some(None)
|
|
|
|
}
|
|
|
|
None => None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-24 11:13:35 +02:00
|
|
|
impl Encoder<Message<(RequestHeadType, BodySize)>> for ClientCodec {
|
2018-10-23 03:18:05 +02:00
|
|
|
type Error = io::Error;
|
|
|
|
|
|
|
|
fn encode(
|
2018-10-30 00:39:46 +01:00
|
|
|
&mut self,
|
2020-08-24 11:13:35 +02:00
|
|
|
item: Message<(RequestHeadType, BodySize)>,
|
2018-10-30 00:39:46 +01:00
|
|
|
dst: &mut BytesMut,
|
2018-10-23 03:18:05 +02:00
|
|
|
) -> Result<(), Self::Error> {
|
|
|
|
match item {
|
2019-09-10 06:29:32 +02:00
|
|
|
Message::Item((mut head, length)) => {
|
2018-11-19 23:57:12 +01:00
|
|
|
let inner = &mut self.inner;
|
2019-09-10 06:29:32 +02:00
|
|
|
inner.version = head.as_ref().version;
|
2019-09-12 17:52:46 +02:00
|
|
|
inner
|
|
|
|
.flags
|
|
|
|
.set(Flags::HEAD, head.as_ref().method == Method::HEAD);
|
2018-11-19 23:57:12 +01:00
|
|
|
|
|
|
|
// connection status
|
2022-01-31 18:30:34 +01:00
|
|
|
inner.conn_type = match head.as_ref().connection_type() {
|
2018-11-19 23:57:12 +01:00
|
|
|
ConnectionType::KeepAlive => {
|
2022-01-31 18:30:34 +01:00
|
|
|
if inner.flags.contains(Flags::KEEP_ALIVE_ENABLED) {
|
2018-11-19 23:57:12 +01:00
|
|
|
ConnectionType::KeepAlive
|
|
|
|
} else {
|
|
|
|
ConnectionType::Close
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ConnectionType::Upgrade => ConnectionType::Upgrade,
|
|
|
|
ConnectionType::Close => ConnectionType::Close,
|
|
|
|
};
|
|
|
|
|
|
|
|
inner.encoder.encode(
|
|
|
|
dst,
|
2019-09-10 06:29:32 +02:00
|
|
|
&mut head,
|
2018-11-19 23:57:12 +01:00
|
|
|
false,
|
2019-03-18 05:57:53 +01:00
|
|
|
false,
|
2018-11-19 23:57:12 +01:00
|
|
|
inner.version,
|
|
|
|
length,
|
2022-01-31 18:30:34 +01:00
|
|
|
inner.conn_type,
|
2018-11-19 23:57:12 +01:00
|
|
|
&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(())
|
|
|
|
}
|
|
|
|
}
|