1
0

HttpHandler.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 proxy: 1;
  33. unsigned upgrade: 1;
  34. // peeraddr
  35. char ip[64];
  36. int port;
  37. // for log
  38. long pid;
  39. long tid;
  40. // for http
  41. hio_t *io;
  42. HttpService *service;
  43. HttpRequestPtr req;
  44. HttpResponsePtr resp;
  45. HttpResponseWriterPtr writer;
  46. HttpParserPtr parser;
  47. HttpContextPtr ctx;
  48. http_handler* api_handler;
  49. // for sendfile
  50. FileCache *files;
  51. file_cache_ptr fc; // cache small file
  52. struct LargeFile : public HFile {
  53. HBuf buf;
  54. uint64_t timer;
  55. } *file; // for large file
  56. // for GetSendData
  57. std::string header;
  58. // std::string body;
  59. // for websocket
  60. WebSocketService* ws_service;
  61. WebSocketChannelPtr ws_channel;
  62. WebSocketParserPtr ws_parser;
  63. uint64_t last_send_ping_time;
  64. uint64_t last_recv_pong_time;
  65. HttpHandler(hio_t* io = NULL);
  66. ~HttpHandler();
  67. bool Init(int http_version = 1);
  68. void Reset();
  69. void Close();
  70. int FeedRecvData(const char* data, size_t len);
  71. // @workflow: preprocessor -> api -> web -> postprocessor
  72. // @result: HttpRequest -> HttpResponse/file_cache_t
  73. int HandleHttpRequest();
  74. int GetSendData(char** data, size_t* len);
  75. // HTTP2
  76. bool SwitchHTTP2();
  77. // websocket
  78. bool SwitchWebSocket();
  79. void WebSocketOnOpen() {
  80. ws_channel->status = hv::SocketChannel::CONNECTED;
  81. if (ws_service && ws_service->onopen) {
  82. ws_service->onopen(ws_channel, req);
  83. }
  84. }
  85. void WebSocketOnClose() {
  86. ws_channel->status = hv::SocketChannel::DISCONNECTED;
  87. if (ws_service && ws_service->onclose) {
  88. ws_service->onclose(ws_channel);
  89. }
  90. }
  91. private:
  92. const HttpContextPtr& getHttpContext();
  93. // http_cb
  94. void onHeadersComplete();
  95. void onMessageComplete();
  96. // default handlers
  97. int defaultRequestHandler();
  98. int defaultStaticHandler();
  99. int defaultLargeFileHandler();
  100. int defaultErrorHandler();
  101. int customHttpHandler(const http_handler& handler);
  102. int invokeHttpHandler(const http_handler* handler);
  103. // sendfile
  104. int openFile(const char* filepath);
  105. int sendFile();
  106. void closeFile();
  107. bool isFileOpened();
  108. // proxy
  109. int proxyConnect(const std::string& url);
  110. static void onProxyConnect(hio_t* upstream_io);
  111. };
  112. #endif // HV_HTTP_HANDLER_H_