Browse Source

support HttpServer::run(:port)

ithewei 1 year ago
parent
commit
406c5dbc9d
5 changed files with 56 additions and 20 deletions
  1. 22 0
      cpputil/hstring.cpp
  2. 12 0
      cpputil/hstring.h
  3. 0 13
      http/HttpMessage.h
  4. 13 7
      http/server/HttpServer.h
  5. 9 0
      unittest/hstring_test.cpp

+ 22 - 0
cpputil/hstring.cpp

@@ -202,4 +202,26 @@ std::string replaceAll(const std::string& str, const std::string& find, const st
     return res;
 }
 
+void NetAddr::from_string(const std::string& ipport) {
+    auto pos = ipport.find_last_of(':');
+    if (pos != std::string::npos) {
+        ip = trim_pairs(ipport.substr(0, pos), "[]");
+        std::string strPort = ipport.substr(pos + 1);
+        port = atoi(strPort.c_str());
+    } else if (ipport.find('.') != std::string::npos) {
+        ip = ipport;
+        port = 0;
+    } else {
+        port = atoi(ipport.c_str());
+    }
+}
+
+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);
+}
+
 } // end namespace hv

+ 12 - 0
cpputil/hstring.h

@@ -75,6 +75,18 @@ HV_EXPORT std::string trim_pairs(const std::string& str, const char* pairs = PAI
 HV_EXPORT std::string replace(const std::string& str, const std::string& find, const std::string& rep);
 HV_EXPORT std::string replaceAll(const std::string& str, const std::string& find, const std::string& rep);
 
+struct NetAddr {
+    std::string     ip;
+    int             port;
+
+    NetAddr() : port(0) {}
+    NetAddr(const std::string& _ip, int _port) : ip(_ip), port(_port) {}
+    NetAddr(const std::string& ipport) { from_string(ipport); }
+
+    void from_string(const std::string& ipport);
+    std::string to_string();
+};
+
 } // end namespace hv
 
 #endif // HV_STRING_H_

+ 0 - 13
http/HttpMessage.h

@@ -46,19 +46,6 @@
 #include "httpdef.h"
 #include "http_content.h"
 
-namespace hv {
-
-struct NetAddr {
-    std::string     ip;
-    int             port;
-
-    std::string ipport() {
-        return hv::asprintf("%s:%d", ip.c_str(), port);
-    }
-};
-
-}
-
 // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie
 // Cookie: sessionid=1; domain=.example.com; path=/; max-age=86400; secure; httponly
 struct HV_EXPORT HttpCookie {

+ 13 - 7
http/server/HttpServer.h

@@ -72,11 +72,9 @@ int main() {
         return 200;
     });
 
-    HttpServer server;
-    server.registerHttpService(&service);
-    server.setPort(8080);
+    HttpServer server(&service);
     server.setThreadNum(4);
-    server.run();
+    server.run(":8080");
     return 0;
 }
 */
@@ -130,12 +128,20 @@ public:
         return setSslCtx(ssl_ctx);
     }
 
-    int run(bool wait = true) {
+    // run(":8080")
+    // run("0.0.0.0:8080")
+    // run("[::]:8080")
+    int run(const char* ip_port = NULL, bool wait = true) {
+        if (ip_port) {
+            hv::NetAddr listen_addr(ip_port);
+            if (listen_addr.ip.size() != 0) setHost(listen_addr.ip.c_str());
+            if (listen_addr.port != 0)      setPort(listen_addr.port);
+        }
         return http_server_run(this, wait);
     }
 
-    int start() {
-        return run(false);
+    int start(const char* ip_port = NULL) {
+        return run(ip_port, false);
     }
 
     int stop() {

+ 9 - 0
unittest/hstring_test.cpp

@@ -39,5 +39,14 @@ int main(int argc, char** argv) {
     std::string str11 = replace(str10, "{{title}}", "Home");
     println("replace=" + str11);
 
+    NetAddr addr1("0.0.0.0:8080");
+    println(addr1.to_string());
+
+    NetAddr addr2("[::0]:8080");
+    println(addr2.to_string());
+
+    NetAddr addr3(":8080");
+    println(addr3.to_string());
+
     return 0;
 }