fix: add port in host header

This commit is contained in:
Arnaud-Marie Gallardo 2021-12-02 19:00:01 +01:00 committed by arnaud-mari.gallardo
parent 9f876c4851
commit 3372ad9dff
6 changed files with 59 additions and 3 deletions

View File

@ -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));

View File

@ -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();

View File

@ -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));

View File

@ -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))

View File

@ -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)

View File

@ -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)
};