mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-24 00:21:08 +01:00
do not set default headers
This commit is contained in:
parent
a7fdac1043
commit
b1547bbbb6
@ -12,7 +12,7 @@ use crate::message::{RequestHead, ResponseHead};
|
||||
use crate::payload::Payload;
|
||||
|
||||
use super::error::SendRequestError;
|
||||
use super::pool::Acquired;
|
||||
use super::pool::{Acquired, Protocol};
|
||||
use super::{h1proto, h2proto};
|
||||
|
||||
pub(crate) enum ConnectionType<Io> {
|
||||
@ -24,6 +24,8 @@ pub trait Connection {
|
||||
type Io: AsyncRead + AsyncWrite;
|
||||
type Future: Future<Item = (ResponseHead, Payload), Error = SendRequestError>;
|
||||
|
||||
fn protocol(&self) -> Protocol;
|
||||
|
||||
/// Send request and body
|
||||
fn send_request<B: MessageBody + 'static>(
|
||||
self,
|
||||
@ -94,6 +96,14 @@ where
|
||||
type Io = T;
|
||||
type Future = Box<Future<Item = (ResponseHead, Payload), Error = SendRequestError>>;
|
||||
|
||||
fn protocol(&self) -> Protocol {
|
||||
match self.io {
|
||||
Some(ConnectionType::H1(_)) => Protocol::Http1,
|
||||
Some(ConnectionType::H2(_)) => Protocol::Http2,
|
||||
None => Protocol::Http1,
|
||||
}
|
||||
}
|
||||
|
||||
fn send_request<B: MessageBody + 'static>(
|
||||
mut self,
|
||||
head: RequestHead,
|
||||
@ -161,6 +171,13 @@ where
|
||||
type Io = EitherIo<A, B>;
|
||||
type Future = Box<Future<Item = (ResponseHead, Payload), Error = SendRequestError>>;
|
||||
|
||||
fn protocol(&self) -> Protocol {
|
||||
match self {
|
||||
EitherConnection::A(con) => con.protocol(),
|
||||
EitherConnection::B(con) => con.protocol(),
|
||||
}
|
||||
}
|
||||
|
||||
fn send_request<RB: MessageBody + 'static>(
|
||||
self,
|
||||
head: RequestHead,
|
||||
|
@ -107,7 +107,6 @@ where
|
||||
let mut head = ResponseHead::new(parts.status);
|
||||
head.version = parts.version;
|
||||
head.headers = parts.headers.into();
|
||||
|
||||
Ok((head, payload))
|
||||
})
|
||||
.from_err()
|
||||
|
@ -9,3 +9,4 @@ mod pool;
|
||||
pub use self::connection::Connection;
|
||||
pub use self::connector::Connector;
|
||||
pub use self::error::{ConnectError, InvalidUrl, SendRequestError};
|
||||
pub use self::pool::Protocol;
|
||||
|
@ -21,8 +21,8 @@ use tokio_timer::{sleep, Delay};
|
||||
use super::connection::{ConnectionType, IoConnection};
|
||||
use super::error::ConnectError;
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Clone, Copy, PartialEq)]
|
||||
/// Protocol version
|
||||
pub enum Protocol {
|
||||
Http1,
|
||||
Http2,
|
||||
|
@ -1,5 +1,12 @@
|
||||
# Changes
|
||||
|
||||
## [0.1.0-alpha.5] - 2019-04-xx
|
||||
|
||||
### Changed
|
||||
|
||||
* Do not set any default headers
|
||||
|
||||
|
||||
## [0.1.0-alpha.4] - 2019-04-08
|
||||
|
||||
### Changed
|
||||
|
@ -61,7 +61,6 @@ pub struct ClientRequest {
|
||||
pub(crate) head: RequestHead,
|
||||
err: Option<HttpError>,
|
||||
cookies: Option<CookieJar>,
|
||||
default_headers: bool,
|
||||
response_decompress: bool,
|
||||
timeout: Option<Duration>,
|
||||
config: Rc<ClientConfig>,
|
||||
@ -79,7 +78,6 @@ impl ClientRequest {
|
||||
err: None,
|
||||
cookies: None,
|
||||
timeout: None,
|
||||
default_headers: true,
|
||||
response_decompress: true,
|
||||
}
|
||||
.method(method)
|
||||
@ -316,13 +314,6 @@ impl ClientRequest {
|
||||
self
|
||||
}
|
||||
|
||||
/// Do not add default request headers.
|
||||
/// By default `Date` and `User-Agent` headers are set.
|
||||
pub fn no_default_headers(mut self) -> Self {
|
||||
self.default_headers = false;
|
||||
self
|
||||
}
|
||||
|
||||
/// Disable automatic decompress of response's body
|
||||
pub fn no_decompress(mut self) -> Self {
|
||||
self.response_decompress = false;
|
||||
@ -392,36 +383,6 @@ impl ClientRequest {
|
||||
return Either::A(err(InvalidUrl::UnknownScheme.into()));
|
||||
}
|
||||
|
||||
// set default headers
|
||||
if self.default_headers {
|
||||
// set request host header
|
||||
if let Some(host) = self.head.uri.host() {
|
||||
if !self.head.headers.contains_key(&header::HOST) {
|
||||
let mut wrt = BytesMut::with_capacity(host.len() + 5).writer();
|
||||
|
||||
let _ = match self.head.uri.port_u16() {
|
||||
None | Some(80) | Some(443) => write!(wrt, "{}", host),
|
||||
Some(port) => write!(wrt, "{}:{}", host, port),
|
||||
};
|
||||
|
||||
match wrt.get_mut().take().freeze().try_into() {
|
||||
Ok(value) => {
|
||||
self.head.headers.insert(header::HOST, value);
|
||||
}
|
||||
Err(e) => return Either::A(err(HttpError::from(e).into())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// user agent
|
||||
if !self.head.headers.contains_key(&header::USER_AGENT) {
|
||||
self.head.headers.insert(
|
||||
header::USER_AGENT,
|
||||
HeaderValue::from_static(concat!("awc/", env!("CARGO_PKG_VERSION"))),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// set cookies
|
||||
if let Some(ref mut jar) = self.cookies {
|
||||
let mut cookie = String::new();
|
||||
@ -436,7 +397,7 @@ impl ClientRequest {
|
||||
);
|
||||
}
|
||||
|
||||
let slf = self;
|
||||
let mut slf = self;
|
||||
|
||||
// enable br only for https
|
||||
#[cfg(any(
|
||||
@ -444,7 +405,8 @@ impl ClientRequest {
|
||||
feature = "flate2-zlib",
|
||||
feature = "flate2-rust"
|
||||
))]
|
||||
let slf = {
|
||||
{
|
||||
if slf.response_decompress {
|
||||
let https = slf
|
||||
.head
|
||||
.uri
|
||||
@ -453,16 +415,16 @@ impl ClientRequest {
|
||||
.unwrap_or(true);
|
||||
|
||||
if https {
|
||||
slf.set_header_if_none(header::ACCEPT_ENCODING, HTTPS_ENCODING)
|
||||
slf = slf.set_header_if_none(header::ACCEPT_ENCODING, HTTPS_ENCODING)
|
||||
} else {
|
||||
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
|
||||
{
|
||||
slf.set_header_if_none(header::ACCEPT_ENCODING, "gzip, deflate")
|
||||
}
|
||||
#[cfg(not(any(feature = "flate2-zlib", feature = "flate2-rust")))]
|
||||
slf
|
||||
slf = slf
|
||||
.set_header_if_none(header::ACCEPT_ENCODING, "gzip, deflate")
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
let head = slf.head;
|
||||
let config = slf.config.as_ref();
|
||||
|
Loading…
Reference in New Issue
Block a user