tc_http2.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #ifndef __TC_HTTP2_H__
  2. #define __TC_HTTP2_H__
  3. #include "util/tc_http.h"
  4. #include "util/tc_spin_lock.h"
  5. #include "util/tc_network_buffer.h"
  6. typedef struct nghttp2_session nghttp2_session;
  7. namespace tars
  8. {
  9. class TC_Http2
  10. {
  11. public:
  12. /**
  13. * constructor
  14. */
  15. TC_Http2();
  16. /**
  17. * deconstructor
  18. */
  19. virtual ~TC_Http2();
  20. /**
  21. * data pack
  22. */
  23. struct DataPack
  24. {
  25. DataPack(const char *data, size_t length) : _dataBuf(data), _length(length) {}
  26. const char* _dataBuf;
  27. size_t _length;
  28. size_t _readPos = 0;
  29. };
  30. /**
  31. * @brief setting
  32. */
  33. int settings(unsigned int maxCurrentStreams = 2000);
  34. /**
  35. * @brief buffer
  36. */
  37. vector<char>& buffer() { return _buff; }
  38. /**
  39. * buffer
  40. * @return
  41. */
  42. const vector<char>& buffer() const { return _buff; }
  43. /**
  44. * swap buff
  45. * @param buff
  46. */
  47. void swap(vector<char> &buff) { _buff.swap(buff); }
  48. /**
  49. * insert buff
  50. * @param buff
  51. */
  52. void insertBuff(const char *buff, size_t length) { _buff.insert(_buff.end(), buff, buff + length); }
  53. /**
  54. * @brief session
  55. */
  56. nghttp2_session* session() const { return _session; }
  57. /**
  58. *
  59. * @return
  60. */
  61. const char *getErrMsg();
  62. protected:
  63. /**
  64. * error code
  65. */
  66. int _err = 0;
  67. /**
  68. * session
  69. */
  70. nghttp2_session* _session;
  71. /**
  72. * data buff
  73. */
  74. vector<char> _buff;
  75. };
  76. class TC_Http2Server : public TC_Http2
  77. {
  78. public:
  79. /**
  80. * constructor
  81. */
  82. TC_Http2Server();
  83. /**
  84. * deconstructor
  85. */
  86. ~TC_Http2Server();
  87. /**
  88. * context
  89. */
  90. struct Http2Context
  91. {
  92. Http2Context(int32_t id) : reqId(id) {}
  93. int32_t reqId;
  94. TC_HttpRequest request;
  95. TC_HttpResponse response;
  96. };
  97. /**
  98. * parse all request
  99. * @param request
  100. * @param vector<std::shared_ptr<TC_HttpRequest>>
  101. * @return
  102. */
  103. vector<shared_ptr<Http2Context>> decodeRequest();
  104. /**
  105. *
  106. * @param reqid
  107. * @param response
  108. * @param out
  109. * @return
  110. */
  111. int encodeResponse(const shared_ptr<Http2Context> &context, vector<char> &out);
  112. /**
  113. * http2
  114. * @param in
  115. * @param out
  116. * @return
  117. */
  118. TC_NetWorkBuffer::PACKET_TYPE parse(TC_NetWorkBuffer&in, vector<char> &out);
  119. void onHeaderCallback(int32_t streamId);
  120. void onHeaderCallback(int32_t streamId, const string &skey, const string &svalue);
  121. void onFrameRecvCallback(int32_t streamId);
  122. void onDataChunkRecvCallback(int32_t streamId, const char *data, size_t len);
  123. void onStreamCloseCallback(int32_t streamId);
  124. protected:
  125. shared_ptr<Http2Context> getContext(int32_t streamId);
  126. void deleteContext(int32_t streamId);
  127. protected:
  128. TC_ThreadMutex _nghttpLock;
  129. unordered_map<int32_t, shared_ptr<Http2Context>> _context;
  130. vector<shared_ptr<Http2Context>> _contextFinished;
  131. vector<char> _reqout;
  132. };
  133. /////////////////////////////////////////////////////////////////////////////////
  134. class TC_Http2Client : public TC_Http2
  135. {
  136. public:
  137. /**
  138. * constructor
  139. */
  140. TC_Http2Client();
  141. /**
  142. * deconstructor
  143. */
  144. ~TC_Http2Client();
  145. /**
  146. * parse response
  147. * @param in
  148. */
  149. TC_NetWorkBuffer::PACKET_TYPE parseResponse(TC_NetWorkBuffer &in, pair<int, shared_ptr<TC_HttpResponse>> &out);
  150. /**
  151. * submit, get net buffer to send
  152. * @param method
  153. * @param path
  154. * @param header
  155. * @param buff
  156. * @return
  157. */
  158. // int submit(const string &method, const string &path, const map<string, string> &header, const vector<char> &buff);
  159. int submit(const TC_HttpRequest &request);
  160. /**
  161. * @brief response
  162. */
  163. std::unordered_map<int, shared_ptr<TC_HttpResponse>> &responses() { return _responses; }
  164. /**
  165. * @brief response finished
  166. */
  167. std::unordered_map<int, shared_ptr<TC_HttpResponse>> &doneResponses() { return _doneResponses; }
  168. private:
  169. /**
  170. * 收到的响应
  171. */
  172. std::unordered_map<int, shared_ptr<TC_HttpResponse>> _responses;
  173. /**
  174. * 收到的完整响应
  175. */
  176. std::unordered_map<int, shared_ptr<TC_HttpResponse>> _doneResponses;
  177. };
  178. }
  179. //#endif
  180. #endif