diff options
author | Jack Lloyd <[email protected]> | 2020-10-04 13:51:05 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2020-10-04 13:51:05 -0400 |
commit | 45133ccc344ded464d6554fd4890e8addcb0b609 (patch) | |
tree | c2e83a8683a57601f505767a0e4aefedc85ee551 /src/lib | |
parent | cc6aa8a57cf320dd3da7c5c558b5cd0175014399 (diff) | |
parent | af65b8e98ebb3a19e8a0aaf90236a50ef4e1f732 (diff) |
Merge GH #2406 Don't include the port number in HTTP Host header
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/utils/http_util/http_util.cpp | 38 | ||||
-rw-r--r-- | src/lib/utils/http_util/http_util.h | 2 |
2 files changed, 20 insertions, 20 deletions
diff --git a/src/lib/utils/http_util/http_util.cpp b/src/lib/utils/http_util/http_util.cpp index 8cc8e4932..e18303866 100644 --- a/src/lib/utils/http_util/http_util.cpp +++ b/src/lib/utils/http_util/http_util.cpp @@ -25,35 +25,23 @@ namespace { * closes the socket. */ std::string http_transact(const std::string& hostname, + const std::string& service, const std::string& message, std::chrono::milliseconds timeout) { std::unique_ptr<OS::Socket> socket; const std::chrono::system_clock::time_point start_time = std::chrono::system_clock::now(); - std::string service, host; - - const auto port_sep = hostname.find(":"); - if(port_sep == std::string::npos) - { - service = "http"; - host = hostname; - } - else - { - service = hostname.substr(port_sep + 1, std::string::npos); - host = hostname.substr(0, port_sep); - } try { - socket = OS::open_socket(host, service, timeout); + socket = OS::open_socket(hostname, service, timeout); if(!socket) throw Not_Implemented("No socket support enabled in build"); } catch(std::exception& e) { - throw HTTP_Error("HTTP connection to " + host + " failed: " + e.what()); + throw HTTP_Error("HTTP connection to " + hostname + " failed: " + e.what()); } // Blocks until entire message has been written @@ -130,7 +118,7 @@ Response http_sync(http_exch_fn http_transact, const auto host_loc_sep = url.find('/', protocol_host_sep + 3); - std::string hostname, loc; + std::string hostname, loc, service; if(host_loc_sep == std::string::npos) { @@ -143,6 +131,18 @@ Response http_sync(http_exch_fn http_transact, loc = url.substr(host_loc_sep, std::string::npos); } + const auto port_sep = hostname.find(":"); + if(port_sep == std::string::npos) + { + service = "http"; + hostname = hostname; + } + else + { + service = hostname.substr(port_sep + 1, std::string::npos); + hostname = hostname.substr(0, port_sep); + } + std::ostringstream outbuf; outbuf << verb << " " << loc << " HTTP/1.0\r\n"; @@ -161,7 +161,7 @@ Response http_sync(http_exch_fn http_transact, outbuf << "Connection: close\r\n\r\n"; outbuf.write(cast_uint8_ptr_to_char(body.data()), body.size()); - std::istringstream io(http_transact(hostname, outbuf.str())); + std::istringstream io(http_transact(hostname, service, outbuf.str())); std::string line1; std::getline(io, line1); @@ -232,9 +232,9 @@ Response http_sync(const std::string& verb, std::chrono::milliseconds timeout) { auto transact_with_timeout = - [timeout](const std::string& hostname, const std::string& service) + [timeout](const std::string& hostname, const std::string& service, const std::string& message) { - return http_transact(hostname, service, timeout); + return http_transact(hostname, service, message, timeout); }; return http_sync( diff --git a/src/lib/utils/http_util/http_util.h b/src/lib/utils/http_util/http_util.h index 0eeda60f1..7ad7c5828 100644 --- a/src/lib/utils/http_util/http_util.h +++ b/src/lib/utils/http_util/http_util.h @@ -72,7 +72,7 @@ class Response final BOTAN_PUBLIC_API(2,0) std::ostream& operator<<(std::ostream& o, const Response& resp); -typedef std::function<std::string (const std::string&, const std::string&)> http_exch_fn; +typedef std::function<std::string (const std::string&, const std::string&, const std::string&)> http_exch_fn; BOTAN_PUBLIC_API(2,0) Response http_sync(http_exch_fn fn, const std::string& verb, |