1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-25 00:12:59 +01:00
actix-extras/src/h1/client.rs

303 lines
8.6 KiB
Rust
Raw Normal View History

#![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};
use super::encoder::RequestEncoder;
use super::{Message, MessageType};
2018-11-18 05:21:28 +01:00
use body::{Binary, BodyLength};
2018-11-16 07:34:29 +01:00
use client::ClientResponse;
use config::ServiceConfig;
use error::{ParseError, PayloadError};
use helpers;
2018-11-14 07:53:30 +01:00
use http::header::{
HeaderValue, CONNECTION, CONTENT_LENGTH, DATE, TRANSFER_ENCODING, UPGRADE,
};
use http::{Method, Version};
2018-11-17 04:28:07 +01:00
use message::{MessagePool, RequestHead};
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;
}
}
const AVERAGE_HEADER_SIZE: usize = 30;
/// HTTP/1 Codec
pub struct ClientCodec {
inner: ClientCodecInner,
}
/// HTTP/1 Payload Codec
pub struct ClientPayloadCodec {
inner: ClientCodecInner,
}
struct ClientCodecInner {
config: ServiceConfig,
2018-11-17 04:28:07 +01:00
decoder: MessageDecoder<ClientResponse>,
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 {
inner: ClientCodecInner {
config,
2018-11-17 04:28:07 +01:00
decoder: MessageDecoder::default(),
payload: None,
version: Version::HTTP_11,
flags,
headers_size: 0,
te: RequestEncoder::default(),
},
}
}
/// Check if request is upgrade
pub fn upgrade(&self) -> bool {
self.inner.flags.contains(Flags::UPGRADE)
}
/// Check if last response is keep-alive
pub fn keepalive(&self) -> bool {
self.inner.flags.contains(Flags::KEEPALIVE)
}
/// Check last request's message type
pub fn message_type(&self) -> MessageType {
if self.inner.flags.contains(Flags::STREAM) {
2018-11-14 07:53:30 +01:00
MessageType::Stream
} else if self.inner.payload.is_none() {
MessageType::None
} else {
MessageType::Payload
}
}
/// prepare transfer encoding
pub fn prepare_te(&mut self, head: &mut RequestHead, length: BodyLength) {
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-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",
}
}
impl ClientCodecInner {
fn encode_response(
2018-10-30 00:39:46 +01:00
&mut self,
2018-11-14 07:53:30 +01:00
msg: RequestHead,
length: BodyLength,
2018-10-30 00:39:46 +01:00
buffer: &mut BytesMut,
) -> io::Result<()> {
// render message
{
// status line
2018-11-17 06:09:33 +01:00
write!(
Writer(buffer),
2018-11-17 06:09:33 +01:00
"{} {} {}",
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)
).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
let mut len_is_set = true;
match length {
BodyLength::Sized(len) => helpers::write_content_length(len, buffer),
BodyLength::Sized64(len) => {
2018-11-17 06:09:33 +01:00
buffer.extend_from_slice(b"\r\ncontent-length: ");
write!(buffer.writer(), "{}", len)?;
buffer.extend_from_slice(b"\r\n");
}
2018-11-18 05:21:28 +01:00
BodyLength::Chunked => {
2018-11-17 06:09:33 +01:00
buffer.extend_from_slice(b"\r\ntransfer-encoding: chunked\r\n")
}
BodyLength::Zero => {
2018-11-17 06:09:33 +01:00
len_is_set = false;
buffer.extend_from_slice(b"\r\n")
}
2018-11-18 05:21:28 +01:00
BodyLength::None | BodyLength::Stream => {
buffer.extend_from_slice(b"\r\n")
}
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 {
TRANSFER_ENCODING => continue,
CONTENT_LENGTH => match length {
BodyLength::None => (),
BodyLength::Zero => len_is_set = true,
2018-11-17 06:09:33 +01:00
_ => continue,
},
DATE => has_date = true,
UPGRADE => self.flags.insert(Flags::UPGRADE),
_ => (),
}
buffer.put_slice(key.as_ref());
buffer.put_slice(b": ");
2018-11-17 06:09:33 +01:00
buffer.put_slice(value.as_ref());
buffer.put_slice(b"\r\n");
2018-11-17 06:09:33 +01:00
}
2018-11-14 07:53:30 +01:00
2018-11-17 06:09:33 +01:00
// set content length
if !len_is_set {
buffer.extend_from_slice(b"content-length: 0\r\n")
}
// set date header
2018-11-17 06:09:33 +01:00
if !has_date {
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;
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;
if self.inner.flags.contains(Flags::KEEPALIVE_ENABLED) {
self.inner.flags.set(Flags::KEEPALIVE, req.keep_alive());
}
match payload {
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) => {
self.inner.payload = Some(pl);
self.inner.flags.insert(Flags::STREAM);
}
};
2018-11-14 19:52:40 +01:00
Ok(Some(req))
} else {
Ok(None)
}
}
}
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!(
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,
})
}
}
impl Encoder for ClientCodec {
type Item = Message<(RequestHead, BodyLength)>;
type Error = io::Error;
fn encode(
2018-10-30 00:39:46 +01:00
&mut self,
item: Self::Item,
dst: &mut BytesMut,
) -> Result<(), Self::Error> {
match item {
2018-11-14 07:53:30 +01:00
Message::Item((msg, btype)) => {
self.inner.encode_response(msg, btype, dst)?;
}
Message::Chunk(Some(bytes)) => {
self.inner.te.encode(bytes.as_ref(), dst)?;
}
Message::Chunk(None) => {
self.inner.te.encode_eof(dst)?;
}
}
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(())
}
}