HttpServer.h 3.9 KB

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