1
0

HttpHandler.h 3.2 KB

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