HttpServer.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #ifndef HV_HTTP_SERVER_H_
  2. #define HV_HTTP_SERVER_H_
  3. #include "hexport.h"
  4. #include "hssl.h"
  5. #include "HttpService.h"
  6. // #include "WebSocketServer.h"
  7. namespace hv {
  8. struct WebSocketService;
  9. }
  10. using hv::HttpService;
  11. using hv::WebSocketService;
  12. typedef struct http_server_s {
  13. char host[64];
  14. int port; // http_port
  15. int https_port;
  16. int http_version;
  17. int worker_processes;
  18. int worker_threads;
  19. uint32_t worker_connections; // max_connections = workers * worker_connections
  20. HttpService* service; // http service
  21. WebSocketService* ws; // websocket service
  22. void* userdata;
  23. int listenfd[2]; // 0: http, 1: https
  24. void* privdata;
  25. // hooks
  26. std::function<void()> onWorkerStart;
  27. std::function<void()> onWorkerStop;
  28. // SSL/TLS
  29. hssl_ctx_t ssl_ctx;
  30. unsigned alloced_ssl_ctx: 1;
  31. #ifdef __cplusplus
  32. http_server_s() {
  33. strcpy(host, "0.0.0.0");
  34. // port = DEFAULT_HTTP_PORT;
  35. // https_port = DEFAULT_HTTPS_PORT;
  36. // port = 8080;
  37. // https_port = 8443;
  38. port = https_port = 0;
  39. http_version = 1;
  40. worker_processes = 0;
  41. worker_threads = 0;
  42. worker_connections = 1024;
  43. service = NULL;
  44. ws = NULL;
  45. listenfd[0] = listenfd[1] = -1;
  46. userdata = NULL;
  47. privdata = NULL;
  48. // SSL/TLS
  49. ssl_ctx = NULL;
  50. alloced_ssl_ctx = 0;
  51. }
  52. #endif
  53. } http_server_t;
  54. // @param wait: Whether to occupy current thread
  55. HV_EXPORT int http_server_run(http_server_t* server, int wait = 1);
  56. // NOTE: stop all loops and join all threads
  57. HV_EXPORT int http_server_stop(http_server_t* server);
  58. /*
  59. #include "HttpServer.h"
  60. using namespace hv;
  61. int main() {
  62. HttpService service;
  63. service.GET("/ping", [](HttpRequest* req, HttpResponse* resp) {
  64. resp->body = "pong";
  65. return 200;
  66. });
  67. HttpServer server(&service);
  68. server.setThreadNum(4);
  69. server.run(":8080");
  70. return 0;
  71. }
  72. */
  73. namespace hv {
  74. class HttpServer : public http_server_t {
  75. public:
  76. HttpServer(HttpService* service = NULL)
  77. : http_server_t()
  78. {
  79. this->service = service;
  80. }
  81. ~HttpServer() { stop(); }
  82. void registerHttpService(HttpService* service) {
  83. this->service = service;
  84. }
  85. void setHost(const char* host = "0.0.0.0") {
  86. if (host) strcpy(this->host, host);
  87. }
  88. void setPort(int port = 0, int ssl_port = 0) {
  89. if (port >= 0) this->port = port;
  90. if (ssl_port >= 0) this->https_port = ssl_port;
  91. }
  92. void setListenFD(int fd = -1, int ssl_fd = -1) {
  93. if (fd >= 0) this->listenfd[0] = fd;
  94. if (ssl_fd >= 0) this->listenfd[1] = ssl_fd;
  95. }
  96. void setProcessNum(int num) {
  97. this->worker_processes = num;
  98. }
  99. void setThreadNum(int num) {
  100. this->worker_threads = num;
  101. }
  102. // SSL/TLS
  103. int setSslCtx(hssl_ctx_t ssl_ctx) {
  104. this->ssl_ctx = ssl_ctx;
  105. return 0;
  106. }
  107. int newSslCtx(hssl_ctx_opt_t* opt) {
  108. // NOTE: hssl_ctx_free in http_server_stop
  109. hssl_ctx_t ssl_ctx = hssl_ctx_new(opt);
  110. if (ssl_ctx == NULL) return -1;
  111. this->alloced_ssl_ctx = 1;
  112. return setSslCtx(ssl_ctx);
  113. }
  114. // run(":8080")
  115. // run("0.0.0.0:8080")
  116. // run("[::]:8080")
  117. int run(const char* ip_port = NULL, bool wait = true) {
  118. if (ip_port) {
  119. hv::NetAddr listen_addr(ip_port);
  120. if (listen_addr.ip.size() != 0) setHost(listen_addr.ip.c_str());
  121. if (listen_addr.port != 0) setPort(listen_addr.port);
  122. }
  123. return http_server_run(this, wait);
  124. }
  125. int start(const char* ip_port = NULL) {
  126. return run(ip_port, false);
  127. }
  128. int stop() {
  129. return http_server_stop(this);
  130. }
  131. };
  132. }
  133. #endif // HV_HTTP_SERVER_H_