Http2Session.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. #ifdef WITH_NGHTTP2
  2. #include "Http2Session.h"
  3. static nghttp2_nv make_nv(const char* name, const char* value) {
  4. nghttp2_nv nv;
  5. nv.name = (uint8_t*)name;
  6. nv.value = (uint8_t*)value;
  7. nv.namelen = strlen(name);
  8. nv.valuelen = strlen(value);
  9. nv.flags = NGHTTP2_NV_FLAG_NONE;
  10. return nv;
  11. }
  12. static nghttp2_nv make_nv2(const char* name, const char* value,
  13. int namelen, int valuelen) {
  14. nghttp2_nv nv;
  15. nv.name = (uint8_t*)name;
  16. nv.value = (uint8_t*)value;
  17. nv.namelen = namelen; nv.valuelen = valuelen;
  18. nv.flags = NGHTTP2_NV_FLAG_NONE;
  19. return nv;
  20. }
  21. static void print_frame_hd(const nghttp2_frame_hd* hd) {
  22. printd("[frame] length=%d type=%x flags=%x stream_id=%d\n",
  23. (int)hd->length, (int)hd->type, (int)hd->flags, hd->stream_id);
  24. }
  25. static int on_header_callback(nghttp2_session *session,
  26. const nghttp2_frame *frame,
  27. const uint8_t *name, size_t namelen,
  28. const uint8_t *value, size_t valuelen,
  29. uint8_t flags, void *userdata);
  30. static int on_data_chunk_recv_callback(nghttp2_session *session,
  31. uint8_t flags, int32_t stream_id, const uint8_t *data,
  32. size_t len, void *userdata);
  33. static int on_frame_recv_callback(nghttp2_session *session,
  34. const nghttp2_frame *frame, void *userdata);
  35. /*
  36. static ssize_t data_source_read_callback(nghttp2_session *session,
  37. int32_t stream_id, uint8_t *buf, size_t length,
  38. uint32_t *data_flags, nghttp2_data_source *source, void *userdata);
  39. */
  40. Http2Session::Http2Session(http_session_type type) {
  41. if (cbs == NULL) {
  42. nghttp2_session_callbacks_new(&cbs);
  43. nghttp2_session_callbacks_set_on_header_callback(cbs, on_header_callback);
  44. nghttp2_session_callbacks_set_on_data_chunk_recv_callback(cbs, on_data_chunk_recv_callback);
  45. nghttp2_session_callbacks_set_on_frame_recv_callback(cbs, on_frame_recv_callback);
  46. }
  47. if (type == HTTP_CLIENT) {
  48. nghttp2_session_client_new(&session, cbs, NULL);
  49. state = HSS_SEND_MAGIC;
  50. }
  51. else if (type == HTTP_SERVER) {
  52. nghttp2_session_server_new(&session, cbs, NULL);
  53. }
  54. nghttp2_session_set_user_data(session, this);
  55. submited = NULL;
  56. parsed = NULL;
  57. stream_id = -1;
  58. stream_closed = 0;
  59. nghttp2_settings_entry settings[] = {
  60. {NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, 100}
  61. };
  62. nghttp2_submit_settings(session, NGHTTP2_FLAG_NONE, settings, ARRAY_SIZE(settings));
  63. state = HSS_SEND_SETTINGS;
  64. //nghttp2_submit_ping(session, NGHTTP2_FLAG_NONE, NULL);
  65. //state = HSS_SEND_PING;
  66. }
  67. Http2Session::~Http2Session() {
  68. if (session) {
  69. nghttp2_session_del(session);
  70. session = NULL;
  71. }
  72. }
  73. int Http2Session::GetSendData(char** data, size_t* len) {
  74. // HTTP2_MAGIC,HTTP2_SETTINGS,HTTP2_HEADERS
  75. *len = nghttp2_session_mem_send(session, (const uint8_t**)data);
  76. printd("nghttp2_session_mem_send %d\n", *len);
  77. if (*len != 0) return *len;
  78. if (submited == NULL) return 0;
  79. // HTTP2_DATA
  80. if (state == HSS_SEND_HEADERS) {
  81. void* content = submited->Content();
  82. int content_length = submited->ContentLength();
  83. // HTTP2 DATA framehd
  84. state = HSS_SEND_DATA_FRAME_HD;
  85. http2_frame_hd framehd;
  86. framehd.length = content_length;
  87. framehd.type = HTTP2_DATA;
  88. framehd.flags = HTTP2_FLAG_END_STREAM;
  89. framehd.stream_id = stream_id;
  90. *data = (char*)frame_hdbuf;
  91. *len = HTTP2_FRAME_HDLEN;
  92. printd("HTTP2 DATA framehd-------------------\n");
  93. if (submited->ContentType() == APPLICATION_GRPC) {
  94. printd("grpc DATA framehd-----------------\n");
  95. grpc_message_hd msghd;
  96. msghd.flags = 0;
  97. msghd.length = content_length;
  98. if (type == HTTP_SERVER) {
  99. // grpc server send grpc-status in HTTP2 header frame
  100. framehd.flags = HTTP2_FLAG_NONE;
  101. #ifdef TEST_PROTOBUF
  102. // @test protobuf
  103. // message StringMessage {
  104. // string str = 1;
  105. // }
  106. int protobuf_taglen = 0;
  107. int tag = PROTOBUF_MAKE_TAG(1, WIRE_TYPE_LENGTH_DELIMITED);
  108. unsigned char* p = frame_hdbuf + HTTP2_FRAME_HDLEN + GRPC_MESSAGE_HDLEN;
  109. int bytes = varint_encode(tag, p);
  110. protobuf_taglen += bytes;
  111. p += bytes;
  112. bytes = varint_encode(content_length, p);
  113. protobuf_taglen += bytes;
  114. msghd.length += protobuf_taglen;
  115. framehd.length += protobuf_taglen;
  116. *len += protobuf_taglen;
  117. #endif
  118. }
  119. grpc_message_hd_pack(&msghd, frame_hdbuf + HTTP2_FRAME_HDLEN);
  120. framehd.length += GRPC_MESSAGE_HDLEN;
  121. *len += GRPC_MESSAGE_HDLEN;
  122. }
  123. http2_frame_hd_pack(&framehd, frame_hdbuf);
  124. }
  125. else if (state == HSS_SEND_DATA_FRAME_HD) {
  126. // HTTP2 DATA
  127. printd("HTTP2 DATA-------------------\n");
  128. void* content = submited->Content();
  129. int content_length = submited->ContentLength();
  130. if (content_length == 0) {
  131. state = HSS_SEND_DONE;
  132. }
  133. else {
  134. state = HSS_SEND_DATA;
  135. *data = (char*)content;
  136. *len = content_length;
  137. }
  138. }
  139. else if (state == HSS_SEND_DATA) {
  140. state = HSS_SEND_DONE;
  141. if (submited->ContentType() == APPLICATION_GRPC) {
  142. if (type == HTTP_SERVER && stream_closed) {
  143. // grpc HEADERS grpc-status
  144. printd("grpc HEADERS grpc-status-----------------\n");
  145. int flags = NGHTTP2_FLAG_END_STREAM | NGHTTP2_FLAG_END_HEADERS;
  146. nghttp2_nv nv = make_nv("grpc-status", "0");
  147. nghttp2_submit_headers(session, flags, stream_id, NULL, &nv, 1, NULL);
  148. *len = nghttp2_session_mem_send(session, (const uint8_t**)data);
  149. }
  150. }
  151. }
  152. printd("GetSendData %d\n", *len);
  153. return *len;
  154. }
  155. int Http2Session::FeedRecvData(const char* data, size_t len) {
  156. printd("nghttp2_session_mem_recv %d\n", len);
  157. state = HSS_RECVING;
  158. size_t ret = nghttp2_session_mem_recv(session, (const uint8_t*)data, len);
  159. if (ret != len) {
  160. error = ret;
  161. }
  162. return (int)ret;
  163. }
  164. bool Http2Session::WantRecv() {
  165. if (stream_id == -1) return true;
  166. if (stream_closed) return false;
  167. if (state == HSS_RECV_DATA ||
  168. state == HSS_RECV_PING) {
  169. return false;
  170. }
  171. return true;
  172. }
  173. int Http2Session::SubmitRequest(HttpRequest* req) {
  174. submited = req;
  175. req->FillContentType();
  176. req->FillContentLength();
  177. if (req->ContentType() == APPLICATION_GRPC) {
  178. req->method = HTTP_POST;
  179. req->headers["te"] = "trailers";
  180. req->headers["user-agent"] = "grpc-c++/1.16.0 grpc-c/6.0.0 (linux; nghttp2; hw)";
  181. req->headers["accept-encoding"] = "identity";
  182. req->headers["grpc-accept-encoding"] = "identity";
  183. }
  184. std::vector<nghttp2_nv> nvs;
  185. char c_str[256] = {0};
  186. req->ParseUrl();
  187. nvs.push_back(make_nv(":method", http_method_str(req->method)));
  188. nvs.push_back(make_nv(":path", req->path.c_str()));
  189. nvs.push_back(make_nv(":scheme", req->https ? "https" : "http"));
  190. if (req->port == 0 ||
  191. req->port == DEFAULT_HTTP_PORT ||
  192. req->port == DEFAULT_HTTPS_PORT) {
  193. nvs.push_back(make_nv(":authority", req->host.c_str()));
  194. }
  195. else {
  196. snprintf(c_str, sizeof(c_str), "%s:%d", req->host.c_str(), req->port);
  197. nvs.push_back(make_nv(":authority", c_str));
  198. }
  199. const char* name;
  200. const char* value;
  201. for (auto& header : req->headers) {
  202. name = header.first.c_str();
  203. value = header.second.c_str();
  204. strlower((char*)name);
  205. if (strcmp(name, "connection") == 0) {
  206. // HTTP2 default keep-alive
  207. continue;
  208. }
  209. if (strcmp(name, "content-length") == 0) {
  210. // HTTP2 have frame_hd.length
  211. continue;
  212. }
  213. nvs.push_back(make_nv2(name, value, header.first.size(), header.second.size()));
  214. }
  215. int flags = NGHTTP2_FLAG_END_HEADERS;
  216. // we set EOS on DATA frame
  217. stream_id = nghttp2_submit_headers(session, flags, -1, NULL, &nvs[0], nvs.size(), NULL);
  218. // avoid DATA_SOURCE_COPY, we do not use nghttp2_submit_data
  219. // nghttp2_data_provider data_prd;
  220. // data_prd.read_callback = data_source_read_callback;
  221. //stream_id = nghttp2_submit_request(session, NULL, &nvs[0], nvs.size(), &data_prd, NULL);
  222. stream_closed = 0;
  223. state = HSS_SEND_HEADERS;
  224. return 0;
  225. }
  226. int Http2Session::SubmitResponse(HttpResponse* res) {
  227. submited = res;
  228. res->FillContentType();
  229. res->FillContentLength();
  230. if (parsed && parsed->ContentType() == APPLICATION_GRPC) {
  231. // correct content_type: application/grpc
  232. if (res->ContentType() != APPLICATION_GRPC) {
  233. res->content_type = APPLICATION_GRPC;
  234. res->headers["content-type"] = http_content_type_str(APPLICATION_GRPC);
  235. }
  236. //res->headers["accept-encoding"] = "identity";
  237. //hss->state = HSS_RECV_PING;
  238. //break;
  239. //res->headers["grpc-accept-encoding"] = "identity";
  240. //res->headers["grpc-status"] = "0";
  241. #ifdef TEST_PROTOBUF
  242. res->status_code = HTTP_STATUS_OK;
  243. #endif
  244. }
  245. std::vector<nghttp2_nv> nvs;
  246. char c_str[256] = {0};
  247. snprintf(c_str, sizeof(c_str), "%d", res->status_code);
  248. nvs.push_back(make_nv(":status", c_str));
  249. const char* name;
  250. const char* value;
  251. for (auto& header : res->headers) {
  252. name = header.first.c_str();
  253. value = header.second.c_str();
  254. strlower((char*)name);
  255. if (strcmp(name, "connection") == 0) {
  256. // HTTP2 default keep-alive
  257. continue;
  258. }
  259. if (strcmp(name, "content-length") == 0) {
  260. // HTTP2 have frame_hd.length
  261. continue;
  262. }
  263. nvs.push_back(make_nv2(name, value, header.first.size(), header.second.size()));
  264. }
  265. int flags = NGHTTP2_FLAG_END_HEADERS;
  266. // we set EOS on DATA frame
  267. if (stream_id == -1) {
  268. // upgrade
  269. nghttp2_session_upgrade(session, NULL, 0, NULL);
  270. stream_id = 1;
  271. }
  272. nghttp2_submit_headers(session, flags, stream_id, NULL, &nvs[0], nvs.size(), NULL);
  273. // avoid DATA_SOURCE_COPY, we do not use nghttp2_submit_data
  274. // data_prd.read_callback = data_source_read_callback;
  275. //nghttp2_submit_response(session, stream_id, &nvs[0], nvs.size(), &data_prd);
  276. stream_closed = 0;
  277. state = HSS_SEND_HEADERS;
  278. return 0;
  279. }
  280. int Http2Session::InitResponse(HttpResponse* res) {
  281. res->Reset();
  282. res->http_major = 2;
  283. res->http_minor = 0;
  284. parsed = res;
  285. return 0;
  286. }
  287. int Http2Session::InitRequest(HttpRequest* req) {
  288. req->Reset();
  289. req->http_major = 2;
  290. req->http_minor = 0;
  291. parsed = req;
  292. return 0;
  293. }
  294. int Http2Session::GetError() {
  295. return error;
  296. }
  297. const char* Http2Session::StrError(int error) {
  298. return nghttp2_http2_strerror(error);
  299. }
  300. nghttp2_session_callbacks* Http2Session::cbs = NULL;
  301. int on_header_callback(nghttp2_session *session,
  302. const nghttp2_frame *frame,
  303. const uint8_t *_name, size_t namelen,
  304. const uint8_t *_value, size_t valuelen,
  305. uint8_t flags, void *userdata) {
  306. printd("on_header_callback\n");
  307. print_frame_hd(&frame->hd);
  308. const char* name = (const char*)_name;
  309. const char* value = (const char*)_value;
  310. printd("%s: %s\n", name, value);
  311. Http2Session* hss = (Http2Session*)userdata;
  312. if (*name == ':') {
  313. if (hss->parsed->type == HTTP_REQUEST) {
  314. // :method :path :scheme :authority
  315. HttpRequest* req = (HttpRequest*)hss->parsed;
  316. if (strcmp(name, ":method") == 0) {
  317. req->method = http_method_enum(value);
  318. }
  319. else if (strcmp(name, ":path") == 0) {
  320. req->url = value;
  321. }
  322. else if (strcmp(name, ":scheme") == 0) {
  323. req->headers["Scheme"] = value;
  324. }
  325. else if (strcmp(name, ":authority") == 0) {
  326. req->headers["Host"] = value;
  327. }
  328. }
  329. else if (hss->parsed->type == HTTP_RESPONSE) {
  330. HttpResponse* res = (HttpResponse*)hss->parsed;
  331. if (strcmp(name, ":status") == 0) {
  332. res->status_code = (http_status)atoi(value);
  333. }
  334. }
  335. }
  336. else {
  337. hss->parsed->headers[name] = value;
  338. if (strcmp(name, "content-type") == 0) {
  339. hss->parsed->content_type = http_content_type_enum(value);
  340. }
  341. }
  342. return 0;
  343. }
  344. int on_data_chunk_recv_callback(nghttp2_session *session,
  345. uint8_t flags, int32_t stream_id, const uint8_t *data,
  346. size_t len, void *userdata) {
  347. printd("on_data_chunk_recv_callback\n");
  348. printd("stream_id=%d length=%d\n", stream_id, (int)len);
  349. //printd("%.*s\n", (int)len, data);
  350. Http2Session* hss = (Http2Session*)userdata;
  351. if (hss->parsed->ContentType() == APPLICATION_GRPC) {
  352. // grpc_message_hd
  353. printd("grpc Length-Prefixed-Message-----------------\n");
  354. if (len >= GRPC_MESSAGE_HDLEN) {
  355. data += GRPC_MESSAGE_HDLEN;
  356. len -= GRPC_MESSAGE_HDLEN;
  357. }
  358. }
  359. hss->parsed->body.insert(hss->parsed->body.size(), (const char*)data, len);
  360. return 0;
  361. }
  362. int on_frame_recv_callback(nghttp2_session *session,
  363. const nghttp2_frame *frame, void *userdata) {
  364. printd("on_frame_recv_callback\n");
  365. print_frame_hd(&frame->hd);
  366. Http2Session* hss = (Http2Session*)userdata;
  367. switch (frame->hd.type) {
  368. case NGHTTP2_DATA:
  369. case NGHTTP2_HEADERS:
  370. hss->stream_id = frame->hd.stream_id;
  371. if (frame->hd.flags & NGHTTP2_FLAG_END_STREAM) {
  372. printd("on_stream_closed stream_id=%d\n", hss->stream_id);
  373. hss->stream_closed = 1;
  374. }
  375. break;
  376. default:
  377. break;
  378. }
  379. switch (frame->hd.type) {
  380. case NGHTTP2_DATA:
  381. hss->state = HSS_RECV_DATA;
  382. break;
  383. case NGHTTP2_HEADERS:
  384. hss->state = HSS_RECV_HEADERS;
  385. break;
  386. case NGHTTP2_SETTINGS:
  387. hss->state = HSS_RECV_SETTINGS;
  388. break;
  389. case NGHTTP2_PING:
  390. hss->state = HSS_RECV_PING;
  391. break;
  392. default:
  393. break;
  394. }
  395. return 0;
  396. }
  397. #endif