yhirose 4 years ago
parent
commit
c1eee3012e
2 changed files with 15 additions and 3 deletions
  1. 10 2
      httplib.h
  2. 5 1
      test/test.cc

+ 10 - 2
httplib.h

@@ -1153,6 +1153,8 @@ private:
       ContentProviderWithoutLength content_provider_without_length,
       ContentProviderWithoutLength content_provider_without_length,
       const char *content_type);
       const char *content_type);
 
 
+  std::string adjust_host_string(const std::string &host) const;
+
   virtual bool process_socket(const Socket &socket,
   virtual bool process_socket(const Socket &socket,
                               std::function<bool(Stream &strm)> callback);
                               std::function<bool(Stream &strm)> callback);
   virtual bool is_ssl() const;
   virtual bool is_ssl() const;
@@ -5301,9 +5303,8 @@ inline ClientImpl::ClientImpl(const std::string &host, int port)
 inline ClientImpl::ClientImpl(const std::string &host, int port,
 inline ClientImpl::ClientImpl(const std::string &host, int port,
                               const std::string &client_cert_path,
                               const std::string &client_cert_path,
                               const std::string &client_key_path)
                               const std::string &client_key_path)
-    // : (Error::Success), host_(host), port_(port),
     : host_(host), port_(port),
     : host_(host), port_(port),
-      host_and_port_(host_ + ":" + std::to_string(port_)),
+      host_and_port_(adjust_host_string(host) + ":" + std::to_string(port)),
       client_cert_path_(client_cert_path), client_key_path_(client_key_path) {}
       client_cert_path_(client_cert_path), client_key_path_(client_key_path) {}
 
 
 inline ClientImpl::~ClientImpl() {
 inline ClientImpl::~ClientImpl() {
@@ -5898,6 +5899,13 @@ inline Result ClientImpl::send_with_content_provider(
   return Result{std::move(res), error, std::move(req.headers)};
   return Result{std::move(res), error, std::move(req.headers)};
 }
 }
 
 
+inline std::string ClientImpl::adjust_host_string(const std::string &host) const {
+  if (host.find(':') != std::string::npos) {
+    return "[" + host + "]";
+  }
+  return host;
+}
+
 inline bool ClientImpl::process_request(Stream &strm, Request &req,
 inline bool ClientImpl::process_request(Stream &strm, Request &req,
                                         Response &res, bool close_connection,
                                         Response &res, bool close_connection,
                                         Error &error) {
                                         Error &error) {

+ 5 - 1
test/test.cc

@@ -954,7 +954,11 @@ TEST(RedirectFromPageWithContentIP6, Redirect) {
     res.set_redirect("http://[::1]:1234/2");
     res.set_redirect("http://[::1]:1234/2");
   });
   });
 
 
-  svr.Get("/2", [&](const Request & /*req*/, Response &res) {
+  svr.Get("/2", [&](const Request &req, Response &res) {
+    auto host_header = req.headers.find("Host");
+    ASSERT_TRUE(host_header != req.headers.end());
+    EXPECT_EQ("[::1]:1234", host_header->second);
+
     res.set_content("Hello World!", "text/plain");
     res.set_content("Hello World!", "text/plain");
   });
   });