1
0
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:
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 crate::payload::Payload;
use super::error::SendRequestError; use super::error::SendRequestError;
use super::pool::Acquired; use super::pool::{Acquired, Protocol};
use super::{h1proto, h2proto}; use super::{h1proto, h2proto};
pub(crate) enum ConnectionType<Io> { pub(crate) enum ConnectionType<Io> {
@ -24,6 +24,8 @@ pub trait Connection {
type Io: AsyncRead + AsyncWrite; type Io: AsyncRead + AsyncWrite;
type Future: Future<Item = (ResponseHead, Payload), Error = SendRequestError>; type Future: Future<Item = (ResponseHead, Payload), Error = SendRequestError>;
fn protocol(&self) -> Protocol;
/// Send request and body /// Send request and body
fn send_request<B: MessageBody + 'static>( fn send_request<B: MessageBody + 'static>(
self, self,
@ -94,6 +96,14 @@ where
type Io = T; type Io = T;
type Future = Box<Future<Item = (ResponseHead, Payload), Error = SendRequestError>>; 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>( fn send_request<B: MessageBody + 'static>(
mut self, mut self,
head: RequestHead, head: RequestHead,
@ -161,6 +171,13 @@ where
type Io = EitherIo<A, B>; type Io = EitherIo<A, B>;
type Future = Box<Future<Item = (ResponseHead, Payload), Error = SendRequestError>>; 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>( fn send_request<RB: MessageBody + 'static>(
self, self,
head: RequestHead, head: RequestHead,

View File

@ -107,7 +107,6 @@ where
let mut head = ResponseHead::new(parts.status); let mut head = ResponseHead::new(parts.status);
head.version = parts.version; head.version = parts.version;
head.headers = parts.headers.into(); head.headers = parts.headers.into();
Ok((head, payload)) Ok((head, payload))
}) })
.from_err() .from_err()

View File

@ -9,3 +9,4 @@ mod pool;
pub use self::connection::Connection; pub use self::connection::Connection;
pub use self::connector::Connector; pub use self::connector::Connector;
pub use self::error::{ConnectError, InvalidUrl, SendRequestError}; 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::connection::{ConnectionType, IoConnection};
use super::error::ConnectError; use super::error::ConnectError;
#[allow(dead_code)]
#[derive(Clone, Copy, PartialEq)] #[derive(Clone, Copy, PartialEq)]
/// Protocol version
pub enum Protocol { pub enum Protocol {
Http1, Http1,
Http2, Http2,

View File

@ -1,5 +1,12 @@
# Changes # Changes
## [0.1.0-alpha.5] - 2019-04-xx
### Changed
* Do not set any default headers
## [0.1.0-alpha.4] - 2019-04-08 ## [0.1.0-alpha.4] - 2019-04-08
### Changed ### Changed

View File

@ -61,7 +61,6 @@ pub struct ClientRequest {
pub(crate) head: RequestHead, pub(crate) head: RequestHead,
err: Option<HttpError>, err: Option<HttpError>,
cookies: Option<CookieJar>, cookies: Option<CookieJar>,
default_headers: bool,
response_decompress: bool, response_decompress: bool,
timeout: Option<Duration>, timeout: Option<Duration>,
config: Rc<ClientConfig>, config: Rc<ClientConfig>,
@ -79,7 +78,6 @@ impl ClientRequest {
err: None, err: None,
cookies: None, cookies: None,
timeout: None, timeout: None,
default_headers: true,
response_decompress: true, response_decompress: true,
} }
.method(method) .method(method)
@ -316,13 +314,6 @@ impl ClientRequest {
self 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 /// Disable automatic decompress of response's body
pub fn no_decompress(mut self) -> Self { pub fn no_decompress(mut self) -> Self {
self.response_decompress = false; self.response_decompress = false;
@ -392,36 +383,6 @@ impl ClientRequest {
return Either::A(err(InvalidUrl::UnknownScheme.into())); 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 // set cookies
if let Some(ref mut jar) = self.cookies { if let Some(ref mut jar) = self.cookies {
let mut cookie = String::new(); let mut cookie = String::new();
@ -436,7 +397,7 @@ impl ClientRequest {
); );
} }
let slf = self; let mut slf = self;
// enable br only for https // enable br only for https
#[cfg(any( #[cfg(any(
@ -444,25 +405,26 @@ impl ClientRequest {
feature = "flate2-zlib", feature = "flate2-zlib",
feature = "flate2-rust" feature = "flate2-rust"
))] ))]
let slf = { {
let https = slf if slf.response_decompress {
.head let https = slf
.uri .head
.scheme_part() .uri
.map(|s| s == &uri::Scheme::HTTPS) .scheme_part()
.unwrap_or(true); .map(|s| s == &uri::Scheme::HTTPS)
.unwrap_or(true);
if https { if https {
slf.set_header_if_none(header::ACCEPT_ENCODING, HTTPS_ENCODING) slf = slf.set_header_if_none(header::ACCEPT_ENCODING, HTTPS_ENCODING)
} else { } else {
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))] #[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
{ {
slf.set_header_if_none(header::ACCEPT_ENCODING, "gzip, deflate") slf = slf
} .set_header_if_none(header::ACCEPT_ENCODING, "gzip, deflate")
#[cfg(not(any(feature = "flate2-zlib", feature = "flate2-rust")))] }
slf };
} }
}; }
let head = slf.head; let head = slf.head;
let config = slf.config.as_ref(); let config = slf.config.as_ref();