Browse Source

Merge pull request #527 from ArnaudGallardo/fix/host-header-with-port

fix: add port in host header
Leonid Stryzhevskyi 2 years ago
parent
commit
568c96d40a

+ 15 - 2
src/oatpp/web/client/HttpRequestExecutor.cpp

@@ -29,6 +29,7 @@
 
 #include "oatpp/network/tcp/Connection.hpp"
 
+#include "oatpp/core/data/stream/BufferStream.hpp"
 #include "oatpp/core/data/stream/StreamBufferedProxy.hpp"
 
 #if defined(WIN32) || defined(_WIN32)
@@ -198,7 +199,13 @@ HttpRequestExecutor::executeOnce(const String& method,
   connection->setOutputStreamIOMode(data::stream::IOMode::BLOCKING);
   
   auto request = oatpp::web::protocol::http::outgoing::Request::createShared(method, path, headers, body);
-  request->putHeaderIfNotExists_Unsafe(oatpp::web::protocol::http::Header::HOST, m_connectionProvider->getProperty("host"));
+  oatpp::data::stream::BufferOutputStream hostValue;
+  hostValue << m_connectionProvider->getProperty("host").toString();
+  auto port = m_connectionProvider->getProperty("port");
+  if(port) {
+    hostValue << ":" << port.toString();
+  }
+  request->putHeaderIfNotExists_Unsafe(oatpp::web::protocol::http::Header::HOST, hostValue.toString());
   request->putHeaderIfNotExists_Unsafe(oatpp::web::protocol::http::Header::CONNECTION, oatpp::web::protocol::http::Header::Value::CONNECTION_KEEP_ALIVE);
 
   oatpp::data::share::MemoryLabel buffer(std::make_shared<std::string>(oatpp::data::buffer::IOBuffer::BUFFER_SIZE, 0));
@@ -300,7 +307,13 @@ HttpRequestExecutor::executeOnceAsync(const String& method,
       m_connection->setOutputStreamIOMode(data::stream::IOMode::ASYNCHRONOUS);
 
       auto request = OutgoingRequest::createShared(m_method, m_path, m_headers, m_body);
-      request->putHeaderIfNotExists_Unsafe(Header::HOST, m_this->m_connectionProvider->getProperty("host"));
+      oatpp::data::stream::BufferOutputStream hostValue;
+      hostValue << m_this->m_connectionProvider->getProperty("host").toString();
+      auto port = m_this->m_connectionProvider->getProperty("port");
+      if(port) {
+        hostValue << ":" << port.toString();
+      }
+      request->putHeaderIfNotExists_Unsafe(Header::HOST, hostValue.toString());
       request->putHeaderIfNotExists_Unsafe(Header::CONNECTION, Header::Value::CONNECTION_KEEP_ALIVE);
       m_upstream = oatpp::data::stream::OutputStreamBufferedProxy::createShared(m_connection, m_buffer);
       return OutgoingRequest::sendAsync(request, m_upstream).next(m_upstream->flushAsync()).next(yieldTo(&ExecutorCoroutine::readResponse));

+ 9 - 0
test/oatpp/web/FullAsyncTest.cpp

@@ -269,6 +269,15 @@ void FullAsyncTest::onRun() {
         OATPP_ASSERT(value == "Hello World Async!!!");
       }
 
+      { // test host header
+        auto response = client->getHostHeader(connection);
+        OATPP_ASSERT(response->getStatusCode() == 200);
+        auto value = response->readBodyToString();
+        auto host = clientConnectionProvider->getProperty("host");
+        OATPP_ASSERT(host);
+        OATPP_ASSERT(value == host.toString() + ":" + oatpp::utils::conversion::int32ToStr(m_port));
+      }
+
       if((i + 1) % iterationsStep == 0) {
         auto ticks = oatpp::base::Environment::getMicroTickCount() - lastTick;
         lastTick = oatpp::base::Environment::getMicroTickCount();

+ 9 - 0
test/oatpp/web/FullTest.cpp

@@ -508,6 +508,15 @@ void FullTest::onRun() {
         OATPP_ASSERT(dto->testValueInt == 32000);
       }
 
+      { // test host header
+        auto response = client->getHostHeader(connection);
+        OATPP_ASSERT(response->getStatusCode() == 200);
+        auto value = response->readBodyToString();
+        auto host = clientConnectionProvider->getProperty("host");
+        OATPP_ASSERT(host);
+        OATPP_ASSERT(value == host.toString() + ":" + oatpp::utils::conversion::int32ToStr(m_port));
+      }
+
     }
 
   }, std::chrono::minutes(10));

+ 2 - 0
test/oatpp/web/app/Client.hpp

@@ -88,6 +88,8 @@ public:
 
   API_CALL("GET", "bundle", getBundle)
 
+  API_CALL("GET", "host_header", getHostHeader)
+
   API_CALL_ASYNC("GET", "/", getRootAsync)
   API_CALL_ASYNC("GET", "/", getRootAsyncWithCKA, HEADER(String, connection, "Connection"))
   API_CALL_ASYNC("GET", "params/{param}", getWithParamsAsync, PATH(String, param))

+ 10 - 1
test/oatpp/web/app/Controller.hpp

@@ -401,7 +401,16 @@ public:
     dto->testValueInt = a;
     return createDtoResponse(Status::CODE_200, dto);
   }
-  
+
+  ENDPOINT("GET", "host_header", getHostHeader,
+           REQUEST(std::shared_ptr<IncomingRequest>, request)) {
+    auto hostHeader = request->getHeader("Host");
+    if(hostHeader) {
+      return createResponse(Status::CODE_200, hostHeader);
+    }
+    return createResponse(Status::CODE_400, "");
+  }
+
 };
 
 #include OATPP_CODEGEN_END(ApiController)

+ 14 - 0
test/oatpp/web/app/ControllerAsync.hpp

@@ -361,6 +361,20 @@ public:
 
   };
 
+  ENDPOINT_ASYNC("GET", "host_header", HostHeader) {
+
+    ENDPOINT_ASYNC_INIT(HostHeader)
+
+    Action act() {
+      auto hostHeader = request->getHeader("Host");
+      if(hostHeader) {
+        return _return(controller->createResponse(Status::CODE_200, hostHeader));
+      }
+      return _return(controller->createResponse(Status::CODE_400, ""));
+    }
+
+  };
+
 #include OATPP_CODEGEN_END(ApiController)
   
 };