mirror of https://github.com/fafhrd91/actix-web
Added extra_headers
This commit is contained in:
parent
5ae6fc261e
commit
f276db0693
|
@ -32,7 +32,7 @@ pub trait Connection {
|
||||||
fn send_request<B: MessageBody + 'static>(
|
fn send_request<B: MessageBody + 'static>(
|
||||||
self,
|
self,
|
||||||
head: Rc<RequestHead>,
|
head: Rc<RequestHead>,
|
||||||
additional_headers: Option<HeaderMap>,
|
extra_headers: Option<HeaderMap>,
|
||||||
body: B,
|
body: B,
|
||||||
) -> Self::Future;
|
) -> Self::Future;
|
||||||
|
|
||||||
|
@ -44,7 +44,7 @@ pub trait Connection {
|
||||||
/// Send request, returns Response and Framed
|
/// Send request, returns Response and Framed
|
||||||
fn open_tunnel(self,
|
fn open_tunnel(self,
|
||||||
head: Rc<RequestHead>,
|
head: Rc<RequestHead>,
|
||||||
additional_headers: Option<HeaderMap>,
|
extra_headers: Option<HeaderMap>,
|
||||||
) -> Self::TunnelFuture;
|
) -> Self::TunnelFuture;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -113,14 +113,14 @@ where
|
||||||
fn send_request<B: MessageBody + 'static>(
|
fn send_request<B: MessageBody + 'static>(
|
||||||
mut self,
|
mut self,
|
||||||
head: Rc<RequestHead>,
|
head: Rc<RequestHead>,
|
||||||
additional_headers: Option<HeaderMap>,
|
extra_headers: Option<HeaderMap>,
|
||||||
body: B,
|
body: B,
|
||||||
) -> Self::Future {
|
) -> Self::Future {
|
||||||
match self.io.take().unwrap() {
|
match self.io.take().unwrap() {
|
||||||
ConnectionType::H1(io) => Box::new(h1proto::send_request(
|
ConnectionType::H1(io) => Box::new(h1proto::send_request(
|
||||||
io,
|
io,
|
||||||
head,
|
head,
|
||||||
additional_headers,
|
extra_headers,
|
||||||
body,
|
body,
|
||||||
self.created,
|
self.created,
|
||||||
self.pool,
|
self.pool,
|
||||||
|
@ -128,7 +128,7 @@ where
|
||||||
ConnectionType::H2(io) => Box::new(h2proto::send_request(
|
ConnectionType::H2(io) => Box::new(h2proto::send_request(
|
||||||
io,
|
io,
|
||||||
head,
|
head,
|
||||||
additional_headers,
|
extra_headers,
|
||||||
body,
|
body,
|
||||||
self.created,
|
self.created,
|
||||||
self.pool,
|
self.pool,
|
||||||
|
@ -147,10 +147,10 @@ where
|
||||||
>;
|
>;
|
||||||
|
|
||||||
/// Send request, returns Response and Framed
|
/// 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() {
|
match self.io.take().unwrap() {
|
||||||
ConnectionType::H1(io) => {
|
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) => {
|
ConnectionType::H2(io) => {
|
||||||
if let Some(mut pool) = self.pool.take() {
|
if let Some(mut pool) = self.pool.take() {
|
||||||
|
@ -190,12 +190,12 @@ where
|
||||||
fn send_request<RB: MessageBody + 'static>(
|
fn send_request<RB: MessageBody + 'static>(
|
||||||
self,
|
self,
|
||||||
head: Rc<RequestHead>,
|
head: Rc<RequestHead>,
|
||||||
additional_headers: Option<HeaderMap>,
|
extra_headers: Option<HeaderMap>,
|
||||||
body: RB,
|
body: RB,
|
||||||
) -> Self::Future {
|
) -> Self::Future {
|
||||||
match self {
|
match self {
|
||||||
EitherConnection::A(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, additional_headers, body),
|
EitherConnection::B(con) => con.send_request(head, extra_headers, body),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -207,14 +207,14 @@ where
|
||||||
>;
|
>;
|
||||||
|
|
||||||
/// Send request, returns Response and Framed
|
/// 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 {
|
match self {
|
||||||
EitherConnection::A(con) => Box::new(
|
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))),
|
.map(|(head, framed)| (head, framed.map_io(EitherIo::A))),
|
||||||
),
|
),
|
||||||
EitherConnection::B(con) => Box::new(
|
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))),
|
.map(|(head, framed)| (head, framed.map_io(EitherIo::B))),
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,7 +22,7 @@ use crate::body::{BodySize, MessageBody};
|
||||||
pub(crate) fn send_request<T, B>(
|
pub(crate) fn send_request<T, B>(
|
||||||
io: T,
|
io: T,
|
||||||
head: Rc<RequestHead>,
|
head: Rc<RequestHead>,
|
||||||
additional_headers: Option<HeaderMap>,
|
extra_headers: Option<HeaderMap>,
|
||||||
body: B,
|
body: B,
|
||||||
created: time::Instant,
|
created: time::Instant,
|
||||||
pool: Option<Acquired<T>>,
|
pool: Option<Acquired<T>>,
|
||||||
|
@ -32,7 +32,7 @@ where
|
||||||
B: MessageBody,
|
B: MessageBody,
|
||||||
{
|
{
|
||||||
// set request host header
|
// 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() {
|
if let Some(host) = head.uri.host() {
|
||||||
let mut wrt = BytesMut::with_capacity(host.len() + 5).writer();
|
let mut wrt = BytesMut::with_capacity(host.len() + 5).writer();
|
||||||
|
|
||||||
|
@ -43,22 +43,22 @@ where
|
||||||
|
|
||||||
match wrt.get_mut().take().freeze().try_into() {
|
match wrt.get_mut().take().freeze().try_into() {
|
||||||
Ok(value) => {
|
Ok(value) => {
|
||||||
let mut headers = additional_headers.unwrap_or(HeaderMap::new());
|
let mut headers = extra_headers.unwrap_or(HeaderMap::new());
|
||||||
headers.insert(HOST, value);
|
headers.insert(HOST, value);
|
||||||
Some(headers)
|
Some(headers)
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log::error!("Can not set HOST header {}", e);
|
log::error!("Can not set HOST header {}", e);
|
||||||
additional_headers
|
extra_headers
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
additional_headers
|
extra_headers
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
additional_headers
|
extra_headers
|
||||||
};
|
};
|
||||||
|
|
||||||
let io = H1Connection {
|
let io = H1Connection {
|
||||||
|
@ -71,7 +71,7 @@ where
|
||||||
|
|
||||||
// create Framed and send reqest
|
// create Framed and send reqest
|
||||||
Framed::new(io, h1::ClientCodec::default())
|
Framed::new(io, h1::ClientCodec::default())
|
||||||
.send((head, additional_headers, len).into())
|
.send((head, extra_headers, len).into())
|
||||||
.from_err()
|
.from_err()
|
||||||
// send request body
|
// send request body
|
||||||
.and_then(move |framed| match body.size() {
|
.and_then(move |framed| match body.size() {
|
||||||
|
@ -108,14 +108,14 @@ where
|
||||||
pub(crate) fn open_tunnel<T>(
|
pub(crate) fn open_tunnel<T>(
|
||||||
io: T,
|
io: T,
|
||||||
head: Rc<RequestHead>,
|
head: Rc<RequestHead>,
|
||||||
additional_headers: Option<HeaderMap>,
|
extra_headers: Option<HeaderMap>,
|
||||||
) -> impl Future<Item = (ResponseHead, Framed<T, h1::ClientCodec>), Error = SendRequestError>
|
) -> impl Future<Item = (ResponseHead, Framed<T, h1::ClientCodec>), Error = SendRequestError>
|
||||||
where
|
where
|
||||||
T: AsyncRead + AsyncWrite + 'static,
|
T: AsyncRead + AsyncWrite + 'static,
|
||||||
{
|
{
|
||||||
// create Framed and send reqest
|
// create Framed and send reqest
|
||||||
Framed::new(io, h1::ClientCodec::default())
|
Framed::new(io, h1::ClientCodec::default())
|
||||||
.send((head, additional_headers, BodySize::None).into())
|
.send((head, extra_headers, BodySize::None).into())
|
||||||
.from_err()
|
.from_err()
|
||||||
// read response
|
// read response
|
||||||
.and_then(|framed| {
|
.and_then(|framed| {
|
||||||
|
|
|
@ -21,7 +21,7 @@ use super::pool::Acquired;
|
||||||
pub(crate) fn send_request<T, B>(
|
pub(crate) fn send_request<T, B>(
|
||||||
io: SendRequest<Bytes>,
|
io: SendRequest<Bytes>,
|
||||||
head: Rc<RequestHead>,
|
head: Rc<RequestHead>,
|
||||||
additional_headers: Option<HeaderMap>,
|
extra_headers: Option<HeaderMap>,
|
||||||
body: B,
|
body: B,
|
||||||
created: time::Instant,
|
created: time::Instant,
|
||||||
pool: Option<Acquired<T>>,
|
pool: Option<Acquired<T>>,
|
||||||
|
@ -69,13 +69,13 @@ where
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
|
|
||||||
// merging headers from head and additional headers. HeaderMap::new() does not allocate.
|
// merging headers from head and extra headers. HeaderMap::new() does not allocate.
|
||||||
let additional_headers = additional_headers.unwrap_or(HeaderMap::new());
|
let extra_headers = extra_headers.unwrap_or(HeaderMap::new());
|
||||||
let headers = head.headers.iter()
|
let headers = head.headers.iter()
|
||||||
.filter(|(name, _)| {
|
.filter(|(name, _)| {
|
||||||
!additional_headers.contains_key(*name)
|
!extra_headers.contains_key(*name)
|
||||||
})
|
})
|
||||||
.chain(additional_headers.iter());
|
.chain(extra_headers.iter());
|
||||||
|
|
||||||
// copy headers
|
// copy headers
|
||||||
for (key, value) in headers {
|
for (key, value) in headers {
|
||||||
|
|
|
@ -194,7 +194,7 @@ impl Encoder for ClientCodec {
|
||||||
dst: &mut BytesMut,
|
dst: &mut BytesMut,
|
||||||
) -> Result<(), Self::Error> {
|
) -> Result<(), Self::Error> {
|
||||||
match item {
|
match item {
|
||||||
Message::Item((head, additional_headers, length)) => {
|
Message::Item((head, extra_headers, length)) => {
|
||||||
let inner = &mut self.inner;
|
let inner = &mut self.inner;
|
||||||
inner.version = head.version;
|
inner.version = head.version;
|
||||||
inner.flags.set(Flags::HEAD, head.method == Method::HEAD);
|
inner.flags.set(Flags::HEAD, head.method == Method::HEAD);
|
||||||
|
@ -214,7 +214,7 @@ impl Encoder for ClientCodec {
|
||||||
|
|
||||||
inner.encoder.encode(
|
inner.encoder.encode(
|
||||||
dst,
|
dst,
|
||||||
&mut (head, additional_headers),
|
&mut (head, extra_headers),
|
||||||
false,
|
false,
|
||||||
false,
|
false,
|
||||||
inner.version,
|
inner.version,
|
||||||
|
|
|
@ -44,6 +44,8 @@ pub(crate) trait MessageType: Sized {
|
||||||
|
|
||||||
fn headers(&self) -> &HeaderMap;
|
fn headers(&self) -> &HeaderMap;
|
||||||
|
|
||||||
|
fn extra_headers(&self) -> Option<&HeaderMap>;
|
||||||
|
|
||||||
fn camel_case(&self) -> bool {
|
fn camel_case(&self) -> bool {
|
||||||
false
|
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
|
// write headers
|
||||||
let mut pos = 0;
|
let mut pos = 0;
|
||||||
let mut has_date = false;
|
let mut has_date = false;
|
||||||
let mut remaining = dst.remaining_mut();
|
let mut remaining = dst.remaining_mut();
|
||||||
let mut buf = unsafe { &mut *(dst.bytes_mut() as *mut [u8]) };
|
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 {
|
match *key {
|
||||||
CONNECTION => continue,
|
CONNECTION => continue,
|
||||||
TRANSFER_ENCODING | CONTENT_LENGTH if skip_len => continue,
|
TRANSFER_ENCODING | CONTENT_LENGTH if skip_len => continue,
|
||||||
|
@ -236,6 +247,10 @@ impl MessageType for Response<()> {
|
||||||
&self.head().headers
|
&self.head().headers
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn extra_headers(&self) -> Option<&HeaderMap> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
fn encode_status(&mut self, dst: &mut BytesMut) -> io::Result<()> {
|
fn encode_status(&mut self, dst: &mut BytesMut) -> io::Result<()> {
|
||||||
let head = self.head();
|
let head = self.head();
|
||||||
let reason = head.reason().as_bytes();
|
let reason = head.reason().as_bytes();
|
||||||
|
@ -265,6 +280,10 @@ impl MessageType for (Rc<RequestHead>, Option<HeaderMap>) {
|
||||||
&self.0.headers
|
&self.0.headers
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn extra_headers(&self) -> Option<&HeaderMap> {
|
||||||
|
self.1.as_ref()
|
||||||
|
}
|
||||||
|
|
||||||
fn encode_status(&mut self, dst: &mut BytesMut) -> io::Result<()> {
|
fn encode_status(&mut self, dst: &mut BytesMut) -> io::Result<()> {
|
||||||
let head = &self.0;
|
let head = &self.0;
|
||||||
dst.reserve(256 + head.headers.len() * AVERAGE_HEADER_SIZE);
|
dst.reserve(256 + head.headers.len() * AVERAGE_HEADER_SIZE);
|
||||||
|
|
|
@ -20,7 +20,7 @@ pub(crate) trait Connect {
|
||||||
fn send_request(
|
fn send_request(
|
||||||
&mut self,
|
&mut self,
|
||||||
head: Rc<RequestHead>,
|
head: Rc<RequestHead>,
|
||||||
additional_headers: Option<HeaderMap>,
|
extra_headers: Option<HeaderMap>,
|
||||||
body: Body,
|
body: Body,
|
||||||
addr: Option<net::SocketAddr>,
|
addr: Option<net::SocketAddr>,
|
||||||
) -> Box<Future<Item = ClientResponse, Error = SendRequestError>>;
|
) -> Box<Future<Item = ClientResponse, Error = SendRequestError>>;
|
||||||
|
@ -29,7 +29,7 @@ pub(crate) trait Connect {
|
||||||
fn open_tunnel(
|
fn open_tunnel(
|
||||||
&mut self,
|
&mut self,
|
||||||
head: Rc<RequestHead>,
|
head: Rc<RequestHead>,
|
||||||
additional_headers: Option<HeaderMap>,
|
extra_headers: Option<HeaderMap>,
|
||||||
addr: Option<net::SocketAddr>,
|
addr: Option<net::SocketAddr>,
|
||||||
) -> Box<
|
) -> Box<
|
||||||
Future<
|
Future<
|
||||||
|
@ -51,7 +51,7 @@ where
|
||||||
fn send_request(
|
fn send_request(
|
||||||
&mut self,
|
&mut self,
|
||||||
head: Rc<RequestHead>,
|
head: Rc<RequestHead>,
|
||||||
additional_headers: Option<HeaderMap>,
|
extra_headers: Option<HeaderMap>,
|
||||||
body: Body,
|
body: Body,
|
||||||
addr: Option<net::SocketAddr>,
|
addr: Option<net::SocketAddr>,
|
||||||
) -> Box<Future<Item = ClientResponse, Error = SendRequestError>> {
|
) -> Box<Future<Item = ClientResponse, Error = SendRequestError>> {
|
||||||
|
@ -64,7 +64,7 @@ where
|
||||||
})
|
})
|
||||||
.from_err()
|
.from_err()
|
||||||
// send request
|
// 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)),
|
.map(|(head, payload)| ClientResponse::new(head, payload)),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -72,7 +72,7 @@ where
|
||||||
fn open_tunnel(
|
fn open_tunnel(
|
||||||
&mut self,
|
&mut self,
|
||||||
head: Rc<RequestHead>,
|
head: Rc<RequestHead>,
|
||||||
additional_headers: Option<HeaderMap>,
|
extra_headers: Option<HeaderMap>,
|
||||||
addr: Option<net::SocketAddr>,
|
addr: Option<net::SocketAddr>,
|
||||||
) -> Box<
|
) -> Box<
|
||||||
Future<
|
Future<
|
||||||
|
@ -89,7 +89,7 @@ where
|
||||||
})
|
})
|
||||||
.from_err()
|
.from_err()
|
||||||
// send request
|
// 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)| {
|
.map(|(head, framed)| {
|
||||||
let framed = framed.map_io(|io| BoxedSocket(Box::new(Socket(io))));
|
let framed = framed.map_io(|io| BoxedSocket(Box::new(Socket(io))));
|
||||||
(head, framed)
|
(head, framed)
|
||||||
|
|
Loading…
Reference in New Issue