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 2 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
5 changes: 5 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,11 @@ 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;
http_headers::_case_insensitive_cmp cmp;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, you could use utility::details::str_icmp() API for the comparison. Seems more straightforward than using the http_headers::_case_insensitive_cmp.

if (!cmp(header_name, header_names::host) && !cmp(header_names::host, header_name)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, our windows http client implementation is in http_client_winhttp.cpp. We should make sure that we dont break anything there when modifying these common files.

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