diff --git a/actix-http/src/client/connection.rs b/actix-http/src/client/connection.rs index c5d720efd..9354fca4a 100644 --- a/actix-http/src/client/connection.rs +++ b/actix-http/src/client/connection.rs @@ -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 { @@ -24,6 +24,8 @@ pub trait Connection { type Io: AsyncRead + AsyncWrite; type Future: Future; + fn protocol(&self) -> Protocol; + /// Send request and body fn send_request( self, @@ -94,6 +96,14 @@ where type Io = T; type Future = Box>; + fn protocol(&self) -> Protocol { + match self.io { + Some(ConnectionType::H1(_)) => Protocol::Http1, + Some(ConnectionType::H2(_)) => Protocol::Http2, + None => Protocol::Http1, + } + } + fn send_request( mut self, head: RequestHead, @@ -161,6 +171,13 @@ where type Io = EitherIo; type Future = Box>; + fn protocol(&self) -> Protocol { + match self { + EitherConnection::A(con) => con.protocol(), + EitherConnection::B(con) => con.protocol(), + } + } + fn send_request( self, head: RequestHead, diff --git a/actix-http/src/client/h2proto.rs b/actix-http/src/client/h2proto.rs index da70a878e..ec5beee6b 100644 --- a/actix-http/src/client/h2proto.rs +++ b/actix-http/src/client/h2proto.rs @@ -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() diff --git a/actix-http/src/client/mod.rs b/actix-http/src/client/mod.rs index 87c374742..cf526e25e 100644 --- a/actix-http/src/client/mod.rs +++ b/actix-http/src/client/mod.rs @@ -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; diff --git a/actix-http/src/client/pool.rs b/actix-http/src/client/pool.rs index 2d1785381..68ac6fbc8 100644 --- a/actix-http/src/client/pool.rs +++ b/actix-http/src/client/pool.rs @@ -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, diff --git a/awc/CHANGES.md b/awc/CHANGES.md index fc32cd7eb..767761cef 100644 --- a/awc/CHANGES.md +++ b/awc/CHANGES.md @@ -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 diff --git a/awc/src/request.rs b/awc/src/request.rs index 09a7eecc4..b21c101c4 100644 --- a/awc/src/request.rs +++ b/awc/src/request.rs @@ -61,7 +61,6 @@ pub struct ClientRequest { pub(crate) head: RequestHead, err: Option, cookies: Option, - default_headers: bool, response_decompress: bool, timeout: Option, config: Rc, @@ -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();