Skip to content

Commit

Permalink
support HttpServer::run(:port)
Browse files Browse the repository at this point in the history
  • Loading branch information
ithewei committed Apr 10, 2024
1 parent 1252567 commit 406c5db
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 20 deletions.
22 changes: 22 additions & 0 deletions cpputil/hstring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 12 additions & 0 deletions cpputil/hstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_
13 changes: 0 additions & 13 deletions http/HttpMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
20 changes: 13 additions & 7 deletions http/server/HttpServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
*/
Expand Down Expand Up @@ -130,12 +128,20 @@ class HttpServer : public http_server_t {
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() {
Expand Down
9 changes: 9 additions & 0 deletions unittest/hstring_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 406c5db

Please sign in to comment.