1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2025-04-10 03:44:06 +00:00

Added extra_headers

This commit is contained in:
Dmitry Pypin 2019-07-10 11:56:10 -07:00
parent 5ae6fc261e
commit f276db0693
6 changed files with 55 additions and 36 deletions

View file

@ -32,7 +32,7 @@ pub trait Connection {
fn send_request<B: MessageBody + 'static>(
self,
head: Rc<RequestHead>,
additional_headers: Option<HeaderMap>,
extra_headers: Option<HeaderMap>,
body: B,
) -> Self::Future;
@ -44,7 +44,7 @@ pub trait Connection {
/// Send request, returns Response and Framed
fn open_tunnel(self,
head: Rc<RequestHead>,
additional_headers: Option<HeaderMap>,
extra_headers: Option<HeaderMap>,
) -> Self::TunnelFuture;
}
@ -113,14 +113,14 @@ where
fn send_request<B: MessageBody + 'static>(
mut self,
head: Rc<RequestHead>,
additional_headers: Option<HeaderMap>,
extra_headers: Option<HeaderMap>,
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<RequestHead>, additional_headers: Option<HeaderMap>) -> Self::TunnelFuture {
fn open_tunnel(mut self, head: Rc<RequestHead>, extra_headers: Option<HeaderMap>) -> 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<RB: MessageBody + 'static>(
self,
head: Rc<RequestHead>,
additional_headers: Option<HeaderMap>,
extra_headers: Option<HeaderMap>,
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<RequestHead>, additional_headers: Option<HeaderMap>) -> Self::TunnelFuture {
fn open_tunnel(self, head: Rc<RequestHead>, extra_headers: Option<HeaderMap>) -> 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))),
),
}

View file

@ -22,7 +22,7 @@ use crate::body::{BodySize, MessageBody};
pub(crate) fn send_request<T, B>(
io: T,
head: Rc<RequestHead>,
additional_headers: Option<HeaderMap>,
extra_headers: Option<HeaderMap>,
body: B,
created: time::Instant,
pool: Option<Acquired<T>>,
@ -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<T>(
io: T,
head: Rc<RequestHead>,
additional_headers: Option<HeaderMap>,
extra_headers: Option<HeaderMap>,
) -> impl Future<Item = (ResponseHead, Framed<T, h1::ClientCodec>), 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| {

View file

@ -21,7 +21,7 @@ use super::pool::Acquired;
pub(crate) fn send_request<T, B>(
io: SendRequest<Bytes>,
head: Rc<RequestHead>,
additional_headers: Option<HeaderMap>,
extra_headers: Option<HeaderMap>,
body: B,
created: time::Instant,
pool: Option<Acquired<T>>,
@ -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 {

View file

@ -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,

View file

@ -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<RequestHead>, Option<HeaderMap>) {
&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);

View file

@ -20,7 +20,7 @@ pub(crate) trait Connect {
fn send_request(
&mut self,
head: Rc<RequestHead>,
additional_headers: Option<HeaderMap>,
extra_headers: Option<HeaderMap>,
body: Body,
addr: Option<net::SocketAddr>,
) -> Box<Future<Item = ClientResponse, Error = SendRequestError>>;
@ -29,7 +29,7 @@ pub(crate) trait Connect {
fn open_tunnel(
&mut self,
head: Rc<RequestHead>,
additional_headers: Option<HeaderMap>,
extra_headers: Option<HeaderMap>,
addr: Option<net::SocketAddr>,
) -> Box<
Future<
@ -51,7 +51,7 @@ where
fn send_request(
&mut self,
head: Rc<RequestHead>,
additional_headers: Option<HeaderMap>,
extra_headers: Option<HeaderMap>,
body: Body,
addr: Option<net::SocketAddr>,
) -> Box<Future<Item = ClientResponse, Error = SendRequestError>> {
@ -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<RequestHead>,
additional_headers: Option<HeaderMap>,
extra_headers: Option<HeaderMap>,
addr: Option<net::SocketAddr>,
) -> 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)