1
0

HttpHandler.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #ifndef HV_HTTP_HANDLER_H_
  2. #define HV_HTTP_HANDLER_H_
  3. #include "HttpService.h"
  4. #include "HttpParser.h"
  5. #include "FileCache.h"
  6. #include "WebSocketServer.h"
  7. #include "WebSocketParser.h"
  8. class HttpHandler {
  9. public:
  10. enum ProtocolType {
  11. UNKNOWN,
  12. HTTP_V1,
  13. HTTP_V2,
  14. WEBSOCKET,
  15. } protocol;
  16. enum State {
  17. WANT_RECV,
  18. HANDLE_BEGIN,
  19. HANDLE_CONTINUE,
  20. HANDLE_END,
  21. WANT_SEND,
  22. SEND_HEADER,
  23. SEND_BODY,
  24. SEND_DONE,
  25. WANT_CLOSE,
  26. } state;
  27. // errno
  28. int error;
  29. // flags
  30. unsigned ssl :1;
  31. unsigned keepalive :1;
  32. unsigned upgrade :1;
  33. unsigned proxy :1;
  34. unsigned proxy_connected :1;
  35. unsigned forward_proxy :1;
  36. unsigned reverse_proxy :1;
  37. // peeraddr
  38. char ip[64];
  39. int port;
  40. // for log
  41. long pid;
  42. long tid;
  43. // for http
  44. hio_t *io;
  45. HttpService *service;
  46. HttpRequestPtr req;
  47. HttpResponsePtr resp;
  48. HttpResponseWriterPtr writer;
  49. HttpParserPtr parser;
  50. HttpContextPtr ctx;
  51. http_handler* api_handler;
  52. // for GetSendData
  53. std::string header;
  54. // std::string body;
  55. // for websocket
  56. WebSocketService* ws_service;
  57. WebSocketChannelPtr ws_channel;
  58. WebSocketParserPtr ws_parser;
  59. uint64_t last_send_ping_time;
  60. uint64_t last_recv_pong_time;
  61. // for sendfile
  62. FileCache *files;
  63. file_cache_ptr fc; // cache small file
  64. struct LargeFile : public HFile {
  65. HBuf buf;
  66. uint64_t timer;
  67. } *file; // for large file
  68. // for proxy
  69. std::string proxy_host;
  70. int proxy_port;
  71. HttpHandler(hio_t* io = NULL);
  72. ~HttpHandler();
  73. bool Init(int http_version = 1);
  74. void Reset();
  75. void Close();
  76. /* @workflow:
  77. * HttpServer::on_recv -> HttpHandler::FeedRecvData -> Init -> HttpParser::InitRequest -> HttpRequest::http_cb ->
  78. * onHeadersComplete -> proxy ? handleProxy -> connectProxy :
  79. * onMessageComplete -> upgrade ? handleUpgrade : HandleHttpRequest -> HttpParser::SubmitResponse ->
  80. * SendHttpResponse -> while(GetSendData) hio_write ->
  81. * keepalive ? Reset : Close -> hio_close
  82. *
  83. * @return
  84. * == len: ok
  85. * == 0: WANT_CLOSE
  86. * < 0: error
  87. */
  88. int FeedRecvData(const char* data, size_t len);
  89. /* @workflow:
  90. * preprocessor -> middleware -> processor -> postprocessor
  91. *
  92. * @return status_code
  93. * == 0: HANDLE_CONTINUE
  94. * != 0: HANDLE_END
  95. */
  96. int HandleHttpRequest();
  97. int GetSendData(char** data, size_t* len);
  98. int SendHttpResponse(bool submit = true);
  99. int SendHttpStatusResponse(http_status status_code);
  100. // HTTP2
  101. bool SwitchHTTP2();
  102. // websocket
  103. bool SwitchWebSocket();
  104. void WebSocketOnOpen() {
  105. ws_channel->status = hv::SocketChannel::CONNECTED;
  106. if (ws_service && ws_service->onopen) {
  107. ws_service->onopen(ws_channel, req);
  108. }
  109. }
  110. void WebSocketOnClose() {
  111. ws_channel->status = hv::SocketChannel::DISCONNECTED;
  112. if (ws_service && ws_service->onclose) {
  113. ws_service->onclose(ws_channel);
  114. }
  115. }
  116. int SetError(int error_code, http_status status_code = HTTP_STATUS_BAD_REQUEST) {
  117. error = error_code;
  118. if (resp) resp->status_code = status_code;
  119. return error;
  120. }
  121. private:
  122. const HttpContextPtr& context();
  123. void handleRequestHeaders();
  124. // Expect: 100-continue
  125. void handleExpect100();
  126. void addResponseHeaders();
  127. // http_cb
  128. void onHeadersComplete();
  129. void onBody(const char* data, size_t size);
  130. void onMessageComplete();
  131. // default handlers
  132. int defaultRequestHandler();
  133. int defaultStaticHandler();
  134. int defaultLargeFileHandler(const std::string &filepath);
  135. int defaultErrorHandler();
  136. int customHttpHandler(const http_handler& handler);
  137. int invokeHttpHandler(const http_handler* handler);
  138. // sendfile
  139. int openFile(const char* filepath);
  140. int sendFile();
  141. void closeFile();
  142. bool isFileOpened();
  143. // upgrade
  144. int handleUpgrade(const char* upgrade_protocol);
  145. int upgradeWebSocket();
  146. int upgradeHTTP2();
  147. // proxy
  148. int handleProxy();
  149. int handleForwardProxy();
  150. int handleReverseProxy();
  151. int connectProxy(const std::string& url);
  152. int closeProxy();
  153. int sendProxyRequest();
  154. static void onProxyConnect(hio_t* upstream_io);
  155. static void onProxyClose(hio_t* upstream_io);
  156. };
  157. #endif // HV_HTTP_HANDLER_H_