From 4dba531bf91fc68aa4a3625e4cc205f2ec266f8a Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Tue, 31 Jul 2018 08:51:24 -0700 Subject: [PATCH] do not override HOST header for client request #428 --- CHANGES.md | 4 ++++ src/client/request.rs | 21 ++++++++++++++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index c5c0499dd..8ba3ef566 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -6,6 +6,10 @@ * Support HTTP/2 with rustls #36 +### Fixed + +* Do not override HOST header for client request #428 + ## [0.7.2] - 2018-07-26 diff --git a/src/client/request.rs b/src/client/request.rs index 72aab259d..4d506c3fa 100644 --- a/src/client/request.rs +++ b/src/client/request.rs @@ -291,10 +291,6 @@ impl ClientRequestBuilder { fn _uri(&mut self, url: &str) -> &mut Self { match Uri::try_from(url) { Ok(uri) => { - // set request host header - if let Some(host) = uri.host() { - self.set_header(header::HOST, host); - } if let Some(parts) = parts(&mut self.request, &self.err) { parts.uri = uri; } @@ -629,9 +625,24 @@ impl ClientRequestBuilder { self.set_header_if_none(header::ACCEPT_ENCODING, "gzip, deflate"); } + // set request host header + if let Some(parts) = parts(&mut self.request, &self.err) { + if let Some(host) = parts.uri.host() { + if !parts.headers.contains_key(header::HOST) { + match host.try_into() { + Ok(value) => { + parts.headers.insert(header::HOST, value); + } + Err(e) => self.err = Some(e.into()), + } + } + } + } + + // user agent self.set_header_if_none( header::USER_AGENT, - concat!("Actix-web/", env!("CARGO_PKG_VERSION")), + concat!("actix-web/", env!("CARGO_PKG_VERSION")), ); }