1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-06-10 17:29:36 +00:00

do not set default headers

This commit is contained in:
Nikolay Kim 2019-04-08 11:09:57 -07:00
parent a7fdac1043
commit b1547bbbb6
6 changed files with 46 additions and 60 deletions

View file

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

View file

@ -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()

View file

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

View file

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

View file

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

View file

@ -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,25 +405,26 @@ impl ClientRequest {
feature = "flate2-zlib",
feature = "flate2-rust"
))]
let slf = {
let https = slf
.head
.uri
.scheme_part()
.map(|s| s == &uri::Scheme::HTTPS)
.unwrap_or(true);
{
if slf.response_decompress {
let https = slf
.head
.uri
.scheme_part()
.map(|s| s == &uri::Scheme::HTTPS)
.unwrap_or(true);
if https {
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
if https {
slf = slf.set_header_if_none(header::ACCEPT_ENCODING, HTTPS_ENCODING)
} else {
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
{
slf = slf
.set_header_if_none(header::ACCEPT_ENCODING, "gzip, deflate")
}
};
}
};
}
let head = slf.head;
let config = slf.config.as_ref();