1
0

HttpServer.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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;
  68. server.registerHttpService(&service);
  69. server.setPort(8080);
  70. server.setThreadNum(4);
  71. server.run();
  72. return 0;
  73. }
  74. */
  75. namespace hv {
  76. class 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. void setHost(const char* host = "0.0.0.0") {
  88. if (host) strcpy(this->host, host);
  89. }
  90. void setPort(int port = 0, int ssl_port = 0) {
  91. if (port >= 0) this->port = port;
  92. if (ssl_port >= 0) this->https_port = ssl_port;
  93. }
  94. void setListenFD(int fd = -1, int ssl_fd = -1) {
  95. if (fd >= 0) this->listenfd[0] = fd;
  96. if (ssl_fd >= 0) this->listenfd[1] = ssl_fd;
  97. }
  98. void setProcessNum(int num) {
  99. this->worker_processes = num;
  100. }
  101. void setThreadNum(int num) {
  102. this->worker_threads = num;
  103. }
  104. // SSL/TLS
  105. int setSslCtx(hssl_ctx_t ssl_ctx) {
  106. this->ssl_ctx = ssl_ctx;
  107. return 0;
  108. }
  109. int newSslCtx(hssl_ctx_opt_t* opt) {
  110. // NOTE: hssl_ctx_free in http_server_stop
  111. hssl_ctx_t ssl_ctx = hssl_ctx_new(opt);
  112. if (ssl_ctx == NULL) return -1;
  113. this->alloced_ssl_ctx = 1;
  114. return setSslCtx(ssl_ctx);
  115. }
  116. int run(bool wait = true) {
  117. return http_server_run(this, wait);
  118. }
  119. int start() {
  120. return run(false);
  121. }
  122. int stop() {
  123. return http_server_stop(this);
  124. }
  125. };
  126. }
  127. #endif // HV_HTTP_SERVER_H_