From f276db069342aa26b0000fa1cf4dc64d3d29c3d7 Mon Sep 17 00:00:00 2001 From: Dmitry Pypin Date: Wed, 10 Jul 2019 11:56:10 -0700 Subject: [PATCH] Added extra_headers --- actix-http/src/client/connection.rs | 26 +++++++++++++------------- actix-http/src/client/h1proto.rs | 18 +++++++++--------- actix-http/src/client/h2proto.rs | 10 +++++----- actix-http/src/h1/client.rs | 4 ++-- actix-http/src/h1/encoder.rs | 21 ++++++++++++++++++++- awc/src/connect.rs | 12 ++++++------ 6 files changed, 55 insertions(+), 36 deletions(-) diff --git a/actix-http/src/client/connection.rs b/actix-http/src/client/connection.rs index 047319470..2bba116b9 100644 --- a/actix-http/src/client/connection.rs +++ b/actix-http/src/client/connection.rs @@ -32,7 +32,7 @@ pub trait Connection { fn send_request( self, head: Rc, - additional_headers: Option, + extra_headers: Option, body: B, ) -> Self::Future; @@ -44,7 +44,7 @@ pub trait Connection { /// Send request, returns Response and Framed fn open_tunnel(self, head: Rc, - additional_headers: Option, + extra_headers: Option, ) -> Self::TunnelFuture; } @@ -113,14 +113,14 @@ where fn send_request( mut self, head: Rc, - additional_headers: Option, + extra_headers: Option, body: B, ) -> Self::Future { match self.io.take().unwrap() { ConnectionType::H1(io) => Box::new(h1proto::send_request( io, head, - additional_headers, + extra_headers, body, self.created, self.pool, @@ -128,7 +128,7 @@ where ConnectionType::H2(io) => Box::new(h2proto::send_request( io, head, - additional_headers, + extra_headers, body, self.created, self.pool, @@ -147,10 +147,10 @@ where >; /// Send request, returns Response and Framed - fn open_tunnel(mut self, head: Rc, additional_headers: Option) -> Self::TunnelFuture { + fn open_tunnel(mut self, head: Rc, extra_headers: Option) -> Self::TunnelFuture { match self.io.take().unwrap() { ConnectionType::H1(io) => { - Either::A(Box::new(h1proto::open_tunnel(io, head, additional_headers))) + Either::A(Box::new(h1proto::open_tunnel(io, head, extra_headers))) } ConnectionType::H2(io) => { if let Some(mut pool) = self.pool.take() { @@ -190,12 +190,12 @@ where fn send_request( self, head: Rc, - additional_headers: Option, + extra_headers: Option, body: RB, ) -> Self::Future { match self { - EitherConnection::A(con) => con.send_request(head, additional_headers, body), - EitherConnection::B(con) => con.send_request(head, additional_headers, body), + EitherConnection::A(con) => con.send_request(head, extra_headers, body), + EitherConnection::B(con) => con.send_request(head, extra_headers, body), } } @@ -207,14 +207,14 @@ where >; /// Send request, returns Response and Framed - fn open_tunnel(self, head: Rc, additional_headers: Option) -> Self::TunnelFuture { + fn open_tunnel(self, head: Rc, extra_headers: Option) -> Self::TunnelFuture { match self { EitherConnection::A(con) => Box::new( - con.open_tunnel(head, additional_headers) + con.open_tunnel(head, extra_headers) .map(|(head, framed)| (head, framed.map_io(EitherIo::A))), ), EitherConnection::B(con) => Box::new( - con.open_tunnel(head, additional_headers) + con.open_tunnel(head, extra_headers) .map(|(head, framed)| (head, framed.map_io(EitherIo::B))), ), } diff --git a/actix-http/src/client/h1proto.rs b/actix-http/src/client/h1proto.rs index e178fe4ac..4163886c6 100644 --- a/actix-http/src/client/h1proto.rs +++ b/actix-http/src/client/h1proto.rs @@ -22,7 +22,7 @@ use crate::body::{BodySize, MessageBody}; pub(crate) fn send_request( io: T, head: Rc, - additional_headers: Option, + extra_headers: Option, body: B, created: time::Instant, pool: Option>, @@ -32,7 +32,7 @@ where B: MessageBody, { // set request host header - let additional_headers = if !head.headers.contains_key(HOST) && !additional_headers.iter().any(|h| h.contains_key(HOST)) { + let extra_headers = if !head.headers.contains_key(HOST) && !extra_headers.iter().any(|h| h.contains_key(HOST)) { if let Some(host) = head.uri.host() { let mut wrt = BytesMut::with_capacity(host.len() + 5).writer(); @@ -43,22 +43,22 @@ where match wrt.get_mut().take().freeze().try_into() { Ok(value) => { - let mut headers = additional_headers.unwrap_or(HeaderMap::new()); + let mut headers = extra_headers.unwrap_or(HeaderMap::new()); headers.insert(HOST, value); Some(headers) } Err(e) => { log::error!("Can not set HOST header {}", e); - additional_headers + extra_headers } } } else { - additional_headers + extra_headers } } else { - additional_headers + extra_headers }; let io = H1Connection { @@ -71,7 +71,7 @@ where // create Framed and send reqest Framed::new(io, h1::ClientCodec::default()) - .send((head, additional_headers, len).into()) + .send((head, extra_headers, len).into()) .from_err() // send request body .and_then(move |framed| match body.size() { @@ -108,14 +108,14 @@ where pub(crate) fn open_tunnel( io: T, head: Rc, - additional_headers: Option, + extra_headers: Option, ) -> impl Future), Error = SendRequestError> where T: AsyncRead + AsyncWrite + 'static, { // create Framed and send reqest Framed::new(io, h1::ClientCodec::default()) - .send((head, additional_headers, BodySize::None).into()) + .send((head, extra_headers, BodySize::None).into()) .from_err() // read response .and_then(|framed| { diff --git a/actix-http/src/client/h2proto.rs b/actix-http/src/client/h2proto.rs index 98ff973cc..844c4cfa2 100644 --- a/actix-http/src/client/h2proto.rs +++ b/actix-http/src/client/h2proto.rs @@ -21,7 +21,7 @@ use super::pool::Acquired; pub(crate) fn send_request( io: SendRequest, head: Rc, - additional_headers: Option, + extra_headers: Option, body: B, created: time::Instant, pool: Option>, @@ -69,13 +69,13 @@ where ), }; - // merging headers from head and additional headers. HeaderMap::new() does not allocate. - let additional_headers = additional_headers.unwrap_or(HeaderMap::new()); + // merging headers from head and extra headers. HeaderMap::new() does not allocate. + let extra_headers = extra_headers.unwrap_or(HeaderMap::new()); let headers = head.headers.iter() .filter(|(name, _)| { - !additional_headers.contains_key(*name) + !extra_headers.contains_key(*name) }) - .chain(additional_headers.iter()); + .chain(extra_headers.iter()); // copy headers for (key, value) in headers { diff --git a/actix-http/src/h1/client.rs b/actix-http/src/h1/client.rs index 59f85e49c..be95b5d96 100644 --- a/actix-http/src/h1/client.rs +++ b/actix-http/src/h1/client.rs @@ -194,7 +194,7 @@ impl Encoder for ClientCodec { dst: &mut BytesMut, ) -> Result<(), Self::Error> { match item { - Message::Item((head, additional_headers, length)) => { + Message::Item((head, extra_headers, length)) => { let inner = &mut self.inner; inner.version = head.version; inner.flags.set(Flags::HEAD, head.method == Method::HEAD); @@ -214,7 +214,7 @@ impl Encoder for ClientCodec { inner.encoder.encode( dst, - &mut (head, additional_headers), + &mut (head, extra_headers), false, false, inner.version, diff --git a/actix-http/src/h1/encoder.rs b/actix-http/src/h1/encoder.rs index 147fa0c5e..f06120031 100644 --- a/actix-http/src/h1/encoder.rs +++ b/actix-http/src/h1/encoder.rs @@ -44,6 +44,8 @@ pub(crate) trait MessageType: Sized { fn headers(&self) -> &HeaderMap; + fn extra_headers(&self) -> Option<&HeaderMap>; + fn camel_case(&self) -> bool { false } @@ -129,12 +131,21 @@ pub(crate) trait MessageType: Sized { _ => (), } + // merging headers from head and extra headers. HeaderMap::new() does not allocate. + let empty_headers = HeaderMap::new(); + let extra_headers = self.extra_headers().unwrap_or(&empty_headers); + let headers = self.headers().inner.iter() + .filter(|(name, _)| { + !extra_headers.contains_key(*name) + }) + .chain(extra_headers.inner.iter()); + // write headers let mut pos = 0; let mut has_date = false; let mut remaining = dst.remaining_mut(); let mut buf = unsafe { &mut *(dst.bytes_mut() as *mut [u8]) }; - for (key, value) in self.headers().inner.iter() { + for (key, value) in headers { match *key { CONNECTION => continue, TRANSFER_ENCODING | CONTENT_LENGTH if skip_len => continue, @@ -236,6 +247,10 @@ impl MessageType for Response<()> { &self.head().headers } + fn extra_headers(&self) -> Option<&HeaderMap> { + None + } + fn encode_status(&mut self, dst: &mut BytesMut) -> io::Result<()> { let head = self.head(); let reason = head.reason().as_bytes(); @@ -265,6 +280,10 @@ impl MessageType for (Rc, Option) { &self.0.headers } + fn extra_headers(&self) -> Option<&HeaderMap> { + self.1.as_ref() + } + fn encode_status(&mut self, dst: &mut BytesMut) -> io::Result<()> { let head = &self.0; dst.reserve(256 + head.headers.len() * AVERAGE_HEADER_SIZE); diff --git a/awc/src/connect.rs b/awc/src/connect.rs index a981b18f9..f9081063f 100644 --- a/awc/src/connect.rs +++ b/awc/src/connect.rs @@ -20,7 +20,7 @@ pub(crate) trait Connect { fn send_request( &mut self, head: Rc, - additional_headers: Option, + extra_headers: Option, body: Body, addr: Option, ) -> Box>; @@ -29,7 +29,7 @@ pub(crate) trait Connect { fn open_tunnel( &mut self, head: Rc, - additional_headers: Option, + extra_headers: Option, addr: Option, ) -> Box< Future< @@ -51,7 +51,7 @@ where fn send_request( &mut self, head: Rc, - additional_headers: Option, + extra_headers: Option, body: Body, addr: Option, ) -> Box> { @@ -64,7 +64,7 @@ where }) .from_err() // send request - .and_then(move |connection| connection.send_request(head, additional_headers, body)) + .and_then(move |connection| connection.send_request(head, extra_headers, body)) .map(|(head, payload)| ClientResponse::new(head, payload)), ) } @@ -72,7 +72,7 @@ where fn open_tunnel( &mut self, head: Rc, - additional_headers: Option, + extra_headers: Option, addr: Option, ) -> Box< Future< @@ -89,7 +89,7 @@ where }) .from_err() // send request - .and_then(move |connection| connection.open_tunnel(head, additional_headers)) + .and_then(move |connection| connection.open_tunnel(head, extra_headers)) .map(|(head, framed)| { let framed = framed.map_io(|io| BoxedSocket(Box::new(Socket(io)))); (head, framed)