From b921abf18ff7608296b7c5df6512cbad53f26811 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Mon, 8 Apr 2019 12:48:26 -0700 Subject: [PATCH] set host header for http1 connections --- actix-http/src/client/h1proto.rs | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/actix-http/src/client/h1proto.rs b/actix-http/src/client/h1proto.rs index 5fec9c4f..3a8b119d 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,