diff --git a/actix-http/src/client/h1proto.rs b/actix-http/src/client/h1proto.rs index 5fec9c4f1..3a8b119d3 100644 --- a/actix-http/src/client/h1proto.rs +++ b/actix-http/src/client/h1proto.rs @@ -1,12 +1,14 @@ +use std::io::Write; use std::{io, time}; use actix_codec::{AsyncRead, AsyncWrite, Framed}; -use bytes::Bytes; +use bytes::{BufMut, Bytes, BytesMut}; use futures::future::{ok, Either}; use futures::{Async, Future, Poll, Sink, Stream}; use crate::error::PayloadError; use crate::h1; +use crate::http::header::{IntoHeaderValue, HOST}; use crate::message::{RequestHead, ResponseHead}; use crate::payload::{Payload, PayloadStream}; @@ -17,7 +19,7 @@ use crate::body::{BodySize, MessageBody}; pub(crate) fn send_request( io: T, - head: RequestHead, + mut head: RequestHead, body: B, created: time::Instant, pool: Option>, @@ -26,6 +28,27 @@ where T: AsyncRead + AsyncWrite + 'static, B: MessageBody, { + // set request host header + if !head.headers.contains_key(HOST) { + if let Some(host) = head.uri.host() { + let mut wrt = BytesMut::with_capacity(host.len() + 5).writer(); + + let _ = match 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) => { + head.headers.insert(HOST, value); + } + Err(e) => { + log::error!("Can not set HOST header {}", e); + } + } + } + } + let io = H1Connection { created, pool,