From: Marcin Siodelski Date: Tue, 16 Apr 2019 17:09:42 +0000 (+0200) Subject: [#360,!305] Host header is added before other headers in the HTTP request. X-Git-Tag: Kea-1.6.0-beta~180 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3d723ef845c7428cccdd1b5ab0ad4222c34d6245;p=thirdparty%2Fkea.git [#360,!305] Host header is added before other headers in the HTTP request. --- diff --git a/src/lib/http/request.cc b/src/lib/http/request.cc index a6e846e250..4bc8d31fa9 100644 --- a/src/lib/http/request.cc +++ b/src/lib/http/request.cc @@ -173,10 +173,25 @@ HttpRequest::toString() const { // HTTP method, URI and version number. s << toBriefString() << crlf; + // Host header must go first. + HttpHeaderPtr host_header; + try { + host_header = getHeader("Host"); + if (host_header) { + s << host_header->getName() << ": " << host_header->getValue() << crlf; + } + + } catch (...) { + // impossible condition + } + + // Add all other headers. for (auto header_it = headers_.cbegin(); header_it != headers_.cend(); ++header_it) { - s << header_it->second->getName() << ": " << header_it->second->getValue() - << crlf; + if (header_it->second->getName() != "Host") { + s << header_it->second->getName() << ": " << header_it->second->getValue() + << crlf; + } } s << crlf; diff --git a/src/lib/http/tests/request_unittests.cc b/src/lib/http/tests/request_unittests.cc index 8131a9d482..65febe7990 100644 --- a/src/lib/http/tests/request_unittests.cc +++ b/src/lib/http/tests/request_unittests.cc @@ -296,8 +296,12 @@ TEST_F(HttpRequestTest, isPersistentHttp11Close) { // This test verifies the contents of the HTTP outbound request. TEST_F(HttpRequestTest, clientRequest) { - request_->setDirection(HttpMessage::OUTBOUND); - setContextBasics("POST", "/isc/org", HttpVersion(1, 0)); + ASSERT_NO_THROW( + request_.reset(new HttpRequest(HttpRequest::Method::HTTP_POST, + "/isc/org", + HttpVersion(1, 0), + HostHttpHeader("www.example.org"))); + ); // Capture current date and time. HttpDateTime date_time; @@ -315,6 +319,7 @@ TEST_F(HttpRequestTest, clientRequest) { // it should include "Content-Length", even though we haven't explicitly set // this header. It is dynamically computed from the body size. EXPECT_EQ("POST /isc/org HTTP/1.0\r\n" + "Host: www.example.org\r\n" "Accept: text/html\r\n" "Content-Length: 13\r\n" "Content-Type: text/html\r\n"