HttpHandler.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #ifndef HTTP_HANDLER_H_
  2. #define HTTP_HANDLER_H_
  3. #include "HttpService.h"
  4. #include "HttpParser.h"
  5. #include "FileCache.h"
  6. #include "WebSocketServer.h"
  7. #include "WebSocketParser.h"
  8. #include "hlog.h"
  9. class WebSocketHandler {
  10. public:
  11. WebSocketChannelPtr channel;
  12. WebSocketParserPtr parser;
  13. uint64_t last_send_ping_time;
  14. uint64_t last_recv_pong_time;
  15. WebSocketHandler() {
  16. parser.reset(new WebSocketParser);
  17. // channel.reset(new WebSocketChannel);
  18. last_send_ping_time = 0;
  19. last_recv_pong_time = 0;
  20. }
  21. void onopen() {
  22. channel->status = hv::SocketChannel::CONNECTED;
  23. /*
  24. channel->onread = [this](hv::Buffer* buf) {
  25. const char* data = (const char*)buf->data();
  26. int size= buf->size();
  27. int nfeed = parser->FeedRecvData(data, size);
  28. if (nfeed != size) {
  29. hloge("websocket parse error!");
  30. channel->close();
  31. }
  32. };
  33. */
  34. }
  35. void onclose() {
  36. channel->status = hv::SocketChannel::DISCONNECTED;
  37. }
  38. };
  39. typedef std::shared_ptr<WebSocketHandler> WebSocketHandlerPtr;
  40. class HttpHandler {
  41. public:
  42. enum ProtocolType {
  43. UNKNOWN,
  44. HTTP_V1,
  45. HTTP_V2,
  46. WEBSOCKET,
  47. } protocol;
  48. enum State {
  49. WANT_RECV,
  50. WANT_SEND,
  51. SEND_HEADER,
  52. SEND_BODY,
  53. SEND_DONE,
  54. } state;
  55. // peeraddr
  56. char ip[64];
  57. int port;
  58. // for http
  59. HttpService *service;
  60. FileCache *files;
  61. HttpRequest req;
  62. HttpResponse res;
  63. HttpParserPtr parser;
  64. // for GetSendData
  65. file_cache_ptr fc;
  66. std::string header;
  67. std::string body;
  68. // for websocket
  69. WebSocketHandlerPtr ws;
  70. WebSocketServerCallbacks* ws_cbs;
  71. HttpHandler() {
  72. protocol = UNKNOWN;
  73. state = WANT_RECV;
  74. service = NULL;
  75. files = NULL;
  76. ws_cbs = NULL;
  77. }
  78. void Reset() {
  79. state = WANT_RECV;
  80. req.Reset();
  81. res.Reset();
  82. }
  83. // @workflow: preprocessor -> api -> web -> postprocessor
  84. // @result: HttpRequest -> HttpResponse/file_cache_t
  85. int HandleHttpRequest();
  86. int GetSendData(char** data, size_t* len);
  87. // websocket
  88. WebSocketHandler* SwitchWebSocket() {
  89. ws.reset(new WebSocketHandler);
  90. return ws.get();
  91. }
  92. void WebSocketOnOpen() {
  93. ws->onopen();
  94. if (ws_cbs && ws_cbs->onopen) {
  95. ws_cbs->onopen(ws->channel, req.url);
  96. }
  97. }
  98. void WebSocketOnClose() {
  99. ws->onclose();
  100. if (ws_cbs && ws_cbs->onclose) {
  101. ws_cbs->onclose(ws->channel);
  102. }
  103. }
  104. void WebSocketOnMessage(const std::string& msg) {
  105. if (ws_cbs && ws_cbs->onmessage) {
  106. ws_cbs->onmessage(ws->channel, msg);
  107. }
  108. }
  109. };
  110. #endif // HTTP_HANDLER_H_