Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overwrite HTTP Host header #40

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Release/include/cpprest/details/http_client_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ static utility::string_t flatten_http_headers(const http_headers &headers)
utility::string_t flattened_headers;
for(auto iter = headers.begin(); iter != headers.end(); ++iter)
{
utility::string_t header_name = iter->first;
if (utility::details::str_icmp(header_name, header_names::host)) {
continue;
}
flattened_headers.append(iter->first);
flattened_headers.push_back(':');
flattened_headers.append(iter->second);
Expand Down
11 changes: 9 additions & 2 deletions Release/src/http/client/http_client_asio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,14 +394,21 @@ class asio_context : public request_context, public std::enable_shared_from_this
std::ostream request_stream(&m_body_buf);
request_stream.imbue(std::locale::classic());

request_stream << method << " " << encoded_resource << " " << "HTTP/1.1" << CRLF << "Host: " << host;
request_stream << method << " " << encoded_resource << " " << "HTTP/1.1" << CRLF;
request_stream << "Host: ";

int port = base_uri.port();
if (base_uri.is_port_default())
{
port = (m_connection->is_ssl() ? 443 : 80);
}
request_stream << ":" << port << CRLF;

std::string specified_host_header;
if (m_request.headers().match(header_names::host, specified_host_header)) {
request_stream << specified_host_header << CRLF;
} else {
request_stream << host << ":" << port << CRLF;
}

// Extra request headers are constructed here.
utility::string_t extra_headers;
Expand Down
33 changes: 32 additions & 1 deletion Release/tests/functional/http/client/header_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,37 @@ TEST_FIXTURE(uri_address, parsing_content_type_redundantsemicolon_string)
auto resp = client.request(methods::GET).get();
VERIFY_ARE_EQUAL(resp.extract_string().get(), utility::conversions::to_string_t(body));
}

TEST_FIXTURE(uri_address, overwrite_http_header)
{
test_http_server::scoped_server scoped(m_uri);
http_client client(m_uri);

// Test default case of cpprestsdk setting host header as host:port
auto& host = m_uri.host();
int port = m_uri.port();
utility::string_t expected_default_header = host + ":" + std::to_string(port);
http_request default_host_headers_request(methods::GET);
scoped.server()->next_request().then([&](test_request *p_request)
{
auto headers = p_request->m_headers;
VERIFY_ARE_EQUAL(expected_default_header, headers[header_names::host]);
p_request->reply(200);
});

client.request(default_host_headers_request).get();

// Test case where we overwrite the host header
http_request overwritten_host_headers_request(methods::GET);
overwritten_host_headers_request.headers().add("Host", host);
scoped.server()->next_request().then([&](test_request *p_request)
{
auto headers = p_request->m_headers;
VERIFY_ARE_EQUAL(host, headers[header_names::host]);
p_request->reply(200);
});
client.request(overwritten_host_headers_request).get();
}
} // SUITE(header_tests)

}}}}
}}}}