HttpHandler.h 3.0 KB

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