Procházet zdrojové kódy

fix: FillHost adapt ipv6 (#683)

ithewei před 9 měsíci
rodič
revize
a1baf8772c
4 změnil soubory, kde provedl 9 přidání a 7 odebrání
  1. 6 5
      cpputil/hstring.cpp
  2. 1 0
      cpputil/hstring.h
  3. 1 1
      cpputil/hurl.h
  4. 1 1
      http/HttpMessage.cpp

+ 6 - 5
cpputil/hstring.cpp

@@ -217,11 +217,12 @@ void NetAddr::from_string(const std::string& ipport) {
 }
 
 std::string NetAddr::to_string() {
-    const char* fmt = "%s:%d";
-    if (ip.find(':') != std::string::npos) {
-        fmt = "[%s]:%d";
-    }
-    return hv::asprintf(fmt, ip.c_str(), port);
+    return NetAddr::to_string(ip.c_str(), port);
+}
+
+std::string NetAddr::to_string(const char* ip, int port) {
+    const char* fmt = strchr(ip, ':') ? "[%s]:%d" : "%s:%d";
+    return hv::asprintf(fmt, ip, port);
 }
 
 } // end namespace hv

+ 1 - 0
cpputil/hstring.h

@@ -85,6 +85,7 @@ struct HV_EXPORT NetAddr {
 
     void from_string(const std::string& ipport);
     std::string to_string();
+    static std::string to_string(const char* ip, int port);
 };
 
 } // end namespace hv

+ 1 - 1
cpputil/hurl.h

@@ -31,7 +31,7 @@ public:
 namespace hv {
 
 HV_INLINE std::string escapeURL(const std::string& url) {
-    return HUrl::escape(url, ":/@?=&#+");
+    return HUrl::escape(url, ":/@[]?=&#+");
 }
 
 HV_EXPORT std::string escapeHTML(const std::string& str);

+ 1 - 1
http/HttpMessage.cpp

@@ -746,7 +746,7 @@ void HttpRequest::FillHost(const char* host, int port) {
             port == DEFAULT_HTTPS_PORT) {
             headers["Host"] = host;
         } else {
-            headers["Host"] = asprintf("%s:%d", host, port);
+            headers["Host"] = hv::NetAddr::to_string(host, port);
         }
     }
 }