http2.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #ifdef USE_NGHTTP2
  24. #define _MPRINTF_REPLACE
  25. #include <curl/mprintf.h>
  26. #include <nghttp2/nghttp2.h>
  27. #include "urldata.h"
  28. #include "http2.h"
  29. #include "http.h"
  30. #include "sendf.h"
  31. #include "curl_base64.h"
  32. #include "curl_memory.h"
  33. #include "rawstr.h"
  34. #include "multiif.h"
  35. /* include memdebug.h last */
  36. #include "memdebug.h"
  37. #if (NGHTTP2_VERSION_NUM < 0x000300)
  38. #error too old nghttp2 version, upgrade!
  39. #endif
  40. static int http2_perform_getsock(const struct connectdata *conn,
  41. curl_socket_t *sock, /* points to
  42. numsocks
  43. number of
  44. sockets */
  45. int numsocks)
  46. {
  47. const struct http_conn *httpc = &conn->proto.httpc;
  48. int bitmap = GETSOCK_BLANK;
  49. (void)numsocks;
  50. /* TODO We should check underlying socket state if it is SSL socket
  51. because of renegotiation. */
  52. sock[0] = conn->sock[FIRSTSOCKET];
  53. if(nghttp2_session_want_read(httpc->h2))
  54. bitmap |= GETSOCK_READSOCK(FIRSTSOCKET);
  55. if(nghttp2_session_want_write(httpc->h2))
  56. bitmap |= GETSOCK_WRITESOCK(FIRSTSOCKET);
  57. return bitmap;
  58. }
  59. static int http2_getsock(struct connectdata *conn,
  60. curl_socket_t *sock, /* points to numsocks
  61. number of sockets */
  62. int numsocks)
  63. {
  64. return http2_perform_getsock(conn, sock, numsocks);
  65. }
  66. static CURLcode http2_disconnect(struct connectdata *conn,
  67. bool dead_connection)
  68. {
  69. struct http_conn *httpc = &conn->proto.httpc;
  70. (void)dead_connection;
  71. infof(conn->data, "HTTP/2 DISCONNECT starts now\n");
  72. nghttp2_session_del(httpc->h2);
  73. Curl_safefree(httpc->header_recvbuf->buffer);
  74. Curl_safefree(httpc->header_recvbuf);
  75. Curl_safefree(httpc->inbuf);
  76. infof(conn->data, "HTTP/2 DISCONNECT done\n");
  77. return CURLE_OK;
  78. }
  79. /*
  80. * HTTP2 handler interface. This isn't added to the general list of protocols
  81. * but will be used at run-time when the protocol is dynamically switched from
  82. * HTTP to HTTP2.
  83. */
  84. const struct Curl_handler Curl_handler_http2 = {
  85. "HTTP2", /* scheme */
  86. ZERO_NULL, /* setup_connection */
  87. ZERO_NULL, /* do_it */
  88. ZERO_NULL , /* done */
  89. ZERO_NULL, /* do_more */
  90. ZERO_NULL, /* connect_it */
  91. ZERO_NULL, /* connecting */
  92. ZERO_NULL, /* doing */
  93. http2_getsock, /* proto_getsock */
  94. http2_getsock, /* doing_getsock */
  95. ZERO_NULL, /* domore_getsock */
  96. http2_perform_getsock, /* perform_getsock */
  97. http2_disconnect, /* disconnect */
  98. ZERO_NULL, /* readwrite */
  99. PORT_HTTP, /* defport */
  100. CURLPROTO_HTTP, /* protocol */
  101. PROTOPT_NONE /* flags */
  102. };
  103. const struct Curl_handler Curl_handler_http2_ssl = {
  104. "HTTP2", /* scheme */
  105. ZERO_NULL, /* setup_connection */
  106. ZERO_NULL, /* do_it */
  107. ZERO_NULL , /* done */
  108. ZERO_NULL, /* do_more */
  109. ZERO_NULL, /* connect_it */
  110. ZERO_NULL, /* connecting */
  111. ZERO_NULL, /* doing */
  112. http2_getsock, /* proto_getsock */
  113. http2_getsock, /* doing_getsock */
  114. ZERO_NULL, /* domore_getsock */
  115. http2_perform_getsock, /* perform_getsock */
  116. http2_disconnect, /* disconnect */
  117. ZERO_NULL, /* readwrite */
  118. PORT_HTTP, /* defport */
  119. CURLPROTO_HTTP | CURLPROTO_HTTPS, /* protocol */
  120. PROTOPT_SSL /* flags */
  121. };
  122. /*
  123. * Store nghttp2 version info in this buffer, Prefix with a space. Return
  124. * total length written.
  125. */
  126. int Curl_http2_ver(char *p, size_t len)
  127. {
  128. nghttp2_info *h2 = nghttp2_version(0);
  129. return snprintf(p, len, " nghttp2/%s", h2->version_str);
  130. }
  131. /*
  132. * The implementation of nghttp2_send_callback type. Here we write |data| with
  133. * size |length| to the network and return the number of bytes actually
  134. * written. See the documentation of nghttp2_send_callback for the details.
  135. */
  136. static ssize_t send_callback(nghttp2_session *h2,
  137. const uint8_t *data, size_t length, int flags,
  138. void *userp)
  139. {
  140. struct connectdata *conn = (struct connectdata *)userp;
  141. struct http_conn *httpc = &conn->proto.httpc;
  142. ssize_t written;
  143. CURLcode rc;
  144. (void)h2;
  145. (void)flags;
  146. rc = 0;
  147. written = ((Curl_send*)httpc->send_underlying)(conn, FIRSTSOCKET,
  148. data, length, &rc);
  149. if(rc == CURLE_AGAIN) {
  150. return NGHTTP2_ERR_WOULDBLOCK;
  151. }
  152. if(written == -1) {
  153. failf(conn->data, "Failed sending HTTP2 data");
  154. return NGHTTP2_ERR_CALLBACK_FAILURE;
  155. }
  156. if(!written)
  157. return NGHTTP2_ERR_WOULDBLOCK;
  158. return written;
  159. }
  160. static int on_frame_recv(nghttp2_session *session, const nghttp2_frame *frame,
  161. void *userp)
  162. {
  163. struct connectdata *conn = (struct connectdata *)userp;
  164. struct http_conn *c = &conn->proto.httpc;
  165. int rv;
  166. (void)session;
  167. (void)frame;
  168. infof(conn->data, "on_frame_recv() was called with header %x\n",
  169. frame->hd.type);
  170. switch(frame->hd.type) {
  171. case NGHTTP2_HEADERS:
  172. if(frame->headers.cat != NGHTTP2_HCAT_RESPONSE)
  173. break;
  174. c->bodystarted = TRUE;
  175. Curl_add_buffer(c->header_recvbuf, "\r\n", 2);
  176. c->nread_header_recvbuf = c->len < c->header_recvbuf->size_used ?
  177. c->len : c->header_recvbuf->size_used;
  178. memcpy(c->mem, c->header_recvbuf->buffer, c->nread_header_recvbuf);
  179. c->mem += c->nread_header_recvbuf;
  180. c->len -= c->nread_header_recvbuf;
  181. break;
  182. case NGHTTP2_PUSH_PROMISE:
  183. rv = nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
  184. frame->hd.stream_id, NGHTTP2_CANCEL);
  185. if(nghttp2_is_fatal(rv)) {
  186. return rv;
  187. }
  188. break;
  189. }
  190. return 0;
  191. }
  192. static int on_invalid_frame_recv(nghttp2_session *session,
  193. const nghttp2_frame *frame,
  194. nghttp2_error_code error_code, void *userp)
  195. {
  196. struct connectdata *conn = (struct connectdata *)userp;
  197. (void)session;
  198. (void)frame;
  199. infof(conn->data, "on_invalid_frame_recv() was called, error_code = %d\n",
  200. error_code);
  201. return 0;
  202. }
  203. static int on_data_chunk_recv(nghttp2_session *session, uint8_t flags,
  204. int32_t stream_id,
  205. const uint8_t *data, size_t len, void *userp)
  206. {
  207. struct connectdata *conn = (struct connectdata *)userp;
  208. struct http_conn *c = &conn->proto.httpc;
  209. size_t nread;
  210. (void)session;
  211. (void)flags;
  212. (void)data;
  213. infof(conn->data, "on_data_chunk_recv() "
  214. "len = %u, stream = %x\n", len, stream_id);
  215. if(stream_id != c->stream_id) {
  216. return 0;
  217. }
  218. nread = c->len < len ? c->len : len;
  219. memcpy(c->mem, data, nread);
  220. c->mem += nread;
  221. c->len -= nread;
  222. infof(conn->data, "%zu data written\n", nread);
  223. if(nread < len) {
  224. c->data = data + nread;
  225. c->datalen = len - nread;
  226. return NGHTTP2_ERR_PAUSE;
  227. }
  228. return 0;
  229. }
  230. static int before_frame_send(nghttp2_session *session,
  231. const nghttp2_frame *frame,
  232. void *userp)
  233. {
  234. struct connectdata *conn = (struct connectdata *)userp;
  235. struct http_conn *c = &conn->proto.httpc;
  236. (void)session;
  237. (void)frame;
  238. infof(conn->data, "before_frame_send() was called\n");
  239. if(frame->hd.type == NGHTTP2_HEADERS &&
  240. frame->headers.cat == NGHTTP2_HCAT_REQUEST) {
  241. /* Get stream ID of our request */
  242. c->stream_id = frame->hd.stream_id;
  243. }
  244. return 0;
  245. }
  246. static int on_frame_send(nghttp2_session *session,
  247. const nghttp2_frame *frame,
  248. void *userp)
  249. {
  250. struct connectdata *conn = (struct connectdata *)userp;
  251. (void)session;
  252. (void)frame;
  253. infof(conn->data, "on_frame_send() was called\n");
  254. return 0;
  255. }
  256. static int on_frame_not_send(nghttp2_session *session,
  257. const nghttp2_frame *frame,
  258. int lib_error_code, void *userp)
  259. {
  260. struct connectdata *conn = (struct connectdata *)userp;
  261. (void)session;
  262. (void)frame;
  263. infof(conn->data, "on_frame_not_send() was called, lib_error_code = %d\n",
  264. lib_error_code);
  265. return 0;
  266. }
  267. static int on_stream_close(nghttp2_session *session, int32_t stream_id,
  268. nghttp2_error_code error_code, void *userp)
  269. {
  270. struct connectdata *conn = (struct connectdata *)userp;
  271. struct http_conn *c = &conn->proto.httpc;
  272. (void)session;
  273. (void)stream_id;
  274. infof(conn->data, "on_stream_close() was called, error_code = %d\n",
  275. error_code);
  276. if(stream_id != c->stream_id) {
  277. return 0;
  278. }
  279. c->closed = TRUE;
  280. return 0;
  281. }
  282. static int on_unknown_frame_recv(nghttp2_session *session,
  283. const uint8_t *head, size_t headlen,
  284. const uint8_t *payload, size_t payloadlen,
  285. void *userp)
  286. {
  287. struct connectdata *conn = (struct connectdata *)userp;
  288. (void)session;
  289. (void)head;
  290. (void)headlen;
  291. (void)payload;
  292. (void)payloadlen;
  293. infof(conn->data, "on_unknown_frame_recv() was called\n");
  294. return 0;
  295. }
  296. static int on_begin_headers(nghttp2_session *session,
  297. const nghttp2_frame *frame, void *userp)
  298. {
  299. struct connectdata *conn = (struct connectdata *)userp;
  300. (void)session;
  301. (void)frame;
  302. infof(conn->data, "on_begin_headers() was called\n");
  303. return 0;
  304. }
  305. static const char STATUS[] = ":status";
  306. /* frame->hd.type is either NGHTTP2_HEADERS or NGHTTP2_PUSH_PROMISE */
  307. static int on_header(nghttp2_session *session, const nghttp2_frame *frame,
  308. const uint8_t *name, size_t namelen,
  309. const uint8_t *value, size_t valuelen,
  310. void *userp)
  311. {
  312. struct connectdata *conn = (struct connectdata *)userp;
  313. struct http_conn *c = &conn->proto.httpc;
  314. (void)session;
  315. (void)frame;
  316. if(frame->hd.stream_id != c->stream_id) {
  317. return 0;
  318. }
  319. if(namelen == sizeof(":status") - 1 &&
  320. memcmp(STATUS, name, namelen) == 0) {
  321. snprintf(c->header_recvbuf->buffer, 13, "HTTP/2.0 %s", value);
  322. c->header_recvbuf->buffer[12] = '\r';
  323. return 0;
  324. }
  325. else {
  326. /* convert to a HTTP1-style header */
  327. infof(conn->data, "got header\n");
  328. Curl_add_buffer(c->header_recvbuf, name, namelen);
  329. Curl_add_buffer(c->header_recvbuf, ":", 1);
  330. Curl_add_buffer(c->header_recvbuf, value, valuelen);
  331. Curl_add_buffer(c->header_recvbuf, "\r\n", 2);
  332. }
  333. return 0; /* 0 is successful */
  334. }
  335. /*
  336. * This is all callbacks nghttp2 calls
  337. */
  338. static const nghttp2_session_callbacks callbacks = {
  339. send_callback, /* nghttp2_send_callback */
  340. NULL, /* nghttp2_recv_callback */
  341. on_frame_recv, /* nghttp2_on_frame_recv_callback */
  342. on_invalid_frame_recv, /* nghttp2_on_invalid_frame_recv_callback */
  343. on_data_chunk_recv, /* nghttp2_on_data_chunk_recv_callback */
  344. before_frame_send, /* nghttp2_before_frame_send_callback */
  345. on_frame_send, /* nghttp2_on_frame_send_callback */
  346. on_frame_not_send, /* nghttp2_on_frame_not_send_callback */
  347. on_stream_close, /* nghttp2_on_stream_close_callback */
  348. on_unknown_frame_recv, /* nghttp2_on_unknown_frame_recv_callback */
  349. on_begin_headers, /* nghttp2_on_begin_headers_callback */
  350. on_header /* nghttp2_on_header_callback */
  351. #if NGHTTP2_VERSION_NUM >= 0x000400
  352. , NULL /* nghttp2_select_padding_callback */
  353. #endif
  354. };
  355. static ssize_t data_source_read_callback(nghttp2_session *session,
  356. int32_t stream_id,
  357. uint8_t *buf, size_t length,
  358. int *eof,
  359. nghttp2_data_source *source,
  360. void *userp)
  361. {
  362. struct connectdata *conn = (struct connectdata *)userp;
  363. struct http_conn *c = &conn->proto.httpc;
  364. size_t nread;
  365. (void)session;
  366. (void)stream_id;
  367. (void)eof;
  368. (void)source;
  369. nread = c->upload_len < length ? c->upload_len : length;
  370. if(nread > 0) {
  371. memcpy(buf, c->upload_mem, nread);
  372. c->upload_mem += nread;
  373. c->upload_len -= nread;
  374. c->upload_left -= nread;
  375. }
  376. if(c->upload_left == 0)
  377. *eof = 1;
  378. else if(nread == 0)
  379. return NGHTTP2_ERR_DEFERRED;
  380. return nread;
  381. }
  382. /*
  383. * The HTTP2 settings we send in the Upgrade request
  384. */
  385. static nghttp2_settings_entry settings[] = {
  386. { NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, 100 },
  387. { NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE, NGHTTP2_INITIAL_WINDOW_SIZE },
  388. };
  389. #define H2_BUFSIZE 4096
  390. /*
  391. * Initialize nghttp2 for a Curl connection
  392. */
  393. CURLcode Curl_http2_init(struct connectdata *conn)
  394. {
  395. if(!conn->proto.httpc.h2) {
  396. int rc;
  397. conn->proto.httpc.inbuf = malloc(H2_BUFSIZE);
  398. if(conn->proto.httpc.inbuf == NULL)
  399. return CURLE_OUT_OF_MEMORY;
  400. /* The nghttp2 session is not yet setup, do it */
  401. rc = nghttp2_session_client_new(&conn->proto.httpc.h2,
  402. &callbacks, conn);
  403. if(rc) {
  404. failf(conn->data, "Couldn't initialize nghttp2!");
  405. return CURLE_OUT_OF_MEMORY; /* most likely at least */
  406. }
  407. }
  408. return CURLE_OK;
  409. }
  410. /*
  411. * Send a request using http2
  412. */
  413. CURLcode Curl_http2_send_request(struct connectdata *conn)
  414. {
  415. (void)conn;
  416. return CURLE_OK;
  417. }
  418. /*
  419. * Append headers to ask for a HTTP1.1 to HTTP2 upgrade.
  420. */
  421. CURLcode Curl_http2_request_upgrade(Curl_send_buffer *req,
  422. struct connectdata *conn)
  423. {
  424. CURLcode result;
  425. ssize_t binlen;
  426. char *base64;
  427. size_t blen;
  428. struct SingleRequest *k = &conn->data->req;
  429. uint8_t *binsettings = conn->proto.httpc.binsettings;
  430. Curl_http2_init(conn);
  431. /* As long as we have a fixed set of settings, we don't have to dynamically
  432. * figure out the base64 strings since it'll always be the same. However,
  433. * the settings will likely not be fixed every time in the future.
  434. */
  435. /* this returns number of bytes it wrote */
  436. binlen = nghttp2_pack_settings_payload(binsettings, H2_BINSETTINGS_LEN,
  437. settings,
  438. sizeof(settings)/sizeof(settings[0]));
  439. if(!binlen) {
  440. failf(conn->data, "nghttp2 unexpectedly failed on pack_settings_payload");
  441. return CURLE_FAILED_INIT;
  442. }
  443. conn->proto.httpc.binlen = binlen;
  444. result = Curl_base64_encode(conn->data, (const char *)binsettings, binlen,
  445. &base64, &blen);
  446. if(result)
  447. return result;
  448. result = Curl_add_bufferf(req,
  449. "Connection: Upgrade, HTTP2-Settings\r\n"
  450. "Upgrade: %s\r\n"
  451. "HTTP2-Settings: %s\r\n",
  452. NGHTTP2_PROTO_VERSION_ID, base64);
  453. Curl_safefree(base64);
  454. k->upgr101 = UPGR101_REQUESTED;
  455. return result;
  456. }
  457. /*
  458. * If the read would block (EWOULDBLOCK) we return -1. Otherwise we return
  459. * a regular CURLcode value.
  460. */
  461. static ssize_t http2_recv(struct connectdata *conn, int sockindex,
  462. char *mem, size_t len, CURLcode *err)
  463. {
  464. CURLcode rc;
  465. ssize_t rv;
  466. ssize_t nread;
  467. struct http_conn *httpc = &conn->proto.httpc;
  468. (void)sockindex; /* we always do HTTP2 on sockindex 0 */
  469. if(httpc->closed) {
  470. return 0;
  471. }
  472. /* Nullify here because we call nghttp2_session_send() and they
  473. might refer to the old buffer. */
  474. httpc->upload_mem = NULL;
  475. httpc->upload_len = 0;
  476. if(httpc->bodystarted &&
  477. httpc->nread_header_recvbuf < httpc->header_recvbuf->size_used) {
  478. size_t left =
  479. httpc->header_recvbuf->size_used - httpc->nread_header_recvbuf;
  480. size_t ncopy = len < left ? len : left;
  481. memcpy(mem, httpc->header_recvbuf->buffer + httpc->nread_header_recvbuf,
  482. ncopy);
  483. httpc->nread_header_recvbuf += ncopy;
  484. return ncopy;
  485. }
  486. if(httpc->data) {
  487. nread = len < httpc->datalen ? len : httpc->datalen;
  488. memcpy(mem, httpc->data, nread);
  489. httpc->data += nread;
  490. httpc->datalen -= nread;
  491. infof(conn->data, "%zu data written\n", nread);
  492. if(httpc->datalen == 0) {
  493. httpc->data = NULL;
  494. httpc->datalen = 0;
  495. }
  496. return nread;
  497. }
  498. conn->proto.httpc.mem = mem;
  499. conn->proto.httpc.len = len;
  500. infof(conn->data, "http2_recv: %d bytes buffer\n",
  501. conn->proto.httpc.len);
  502. rc = 0;
  503. nread = ((Curl_recv*)httpc->recv_underlying)(conn, FIRSTSOCKET,
  504. httpc->inbuf, H2_BUFSIZE, &rc);
  505. if(rc == CURLE_AGAIN) {
  506. *err = rc;
  507. return -1;
  508. }
  509. if(nread == -1) {
  510. failf(conn->data, "Failed receiving HTTP2 data");
  511. *err = rc;
  512. return 0;
  513. }
  514. infof(conn->data, "nread=%zd\n", nread);
  515. rv = nghttp2_session_mem_recv(httpc->h2,
  516. (const uint8_t *)httpc->inbuf, nread);
  517. if(nghttp2_is_fatal((int)rv)) {
  518. failf(conn->data, "nghttp2_session_mem_recv() returned %d:%s\n",
  519. rv, nghttp2_strerror((int)rv));
  520. *err = CURLE_RECV_ERROR;
  521. return 0;
  522. }
  523. infof(conn->data, "nghttp2_session_mem_recv() returns %zd\n", rv);
  524. /* Always send pending frames in nghttp2 session, because
  525. nghttp2_session_mem_recv() may queue new frame */
  526. rv = nghttp2_session_send(httpc->h2);
  527. if(rv != 0) {
  528. *err = CURLE_SEND_ERROR;
  529. return 0;
  530. }
  531. if(len != httpc->len) {
  532. return len - conn->proto.httpc.len;
  533. }
  534. /* If stream is closed, return 0 to signal the http routine to close
  535. the connection */
  536. if(httpc->closed) {
  537. return 0;
  538. }
  539. *err = CURLE_AGAIN;
  540. return -1;
  541. }
  542. #define MAKE_NV(k, v) \
  543. { (uint8_t*)k, (uint8_t*)v, sizeof(k) - 1, sizeof(v) - 1 }
  544. #define MAKE_NV2(k, v, vlen) \
  545. { (uint8_t*)k, (uint8_t*)v, sizeof(k) - 1, vlen }
  546. /* return number of received (decrypted) bytes */
  547. static ssize_t http2_send(struct connectdata *conn, int sockindex,
  548. const void *mem, size_t len, CURLcode *err)
  549. {
  550. /*
  551. * BIG TODO: Currently, we send request in this function, but this
  552. * function is also used to send request body. It would be nice to
  553. * add dedicated function for request.
  554. */
  555. int rv;
  556. struct http_conn *httpc = &conn->proto.httpc;
  557. nghttp2_nv *nva;
  558. size_t nheader;
  559. size_t i;
  560. char *hdbuf = (char*)mem;
  561. char *end;
  562. nghttp2_data_provider data_prd;
  563. (void)sockindex;
  564. infof(conn->data, "http2_send len=%zu\n", len);
  565. if(httpc->stream_id != -1) {
  566. /* If stream_id != -1, we have dispatched request HEADERS, and now
  567. are going to send or sending request body in DATA frame */
  568. httpc->upload_mem = mem;
  569. httpc->upload_len = len;
  570. nghttp2_session_resume_data(httpc->h2, httpc->stream_id);
  571. rv = nghttp2_session_send(httpc->h2);
  572. if(nghttp2_is_fatal(rv)) {
  573. *err = CURLE_SEND_ERROR;
  574. return -1;
  575. }
  576. return len - httpc->upload_len;
  577. }
  578. /* Calculate number of headers contained in [mem, mem + len) */
  579. /* Here, we assume the curl http code generate *correct* HTTP header
  580. field block */
  581. nheader = 0;
  582. for(i = 0; i < len; ++i) {
  583. if(hdbuf[i] == 0x0a) {
  584. ++nheader;
  585. }
  586. }
  587. /* We counted additional 2 \n in the first and last line. We need 3
  588. new headers: :method, :path and :scheme. Therefore we need one
  589. more space. */
  590. nheader += 1;
  591. nva = malloc(sizeof(nghttp2_nv) * nheader);
  592. if(nva == NULL) {
  593. *err = CURLE_OUT_OF_MEMORY;
  594. return -1;
  595. }
  596. /* Extract :method, :path from request line */
  597. end = strchr(hdbuf, ' ');
  598. nva[0].name = (unsigned char *)":method";
  599. nva[0].namelen = (uint16_t)strlen((char *)nva[0].name);
  600. nva[0].value = (unsigned char *)hdbuf;
  601. nva[0].valuelen = (uint16_t)(end - hdbuf);
  602. hdbuf = end + 1;
  603. end = strchr(hdbuf, ' ');
  604. nva[1].name = (unsigned char *)":path";
  605. nva[1].namelen = (uint16_t)strlen((char *)nva[1].name);
  606. nva[1].value = (unsigned char *)hdbuf;
  607. nva[1].valuelen = (uint16_t)(end - hdbuf);
  608. nva[2].name = (unsigned char *)":scheme";
  609. nva[2].namelen = (uint16_t)strlen((char *)nva[2].name);
  610. if(conn->handler->flags & PROTOPT_SSL)
  611. nva[2].value = (unsigned char *)"https";
  612. else
  613. nva[2].value = (unsigned char *)"http";
  614. nva[2].valuelen = (uint16_t)strlen((char *)nva[2].value);
  615. hdbuf = strchr(hdbuf, 0x0a);
  616. ++hdbuf;
  617. for(i = 3; i < nheader; ++i) {
  618. end = strchr(hdbuf, ':');
  619. assert(end);
  620. if(end - hdbuf == 4 && Curl_raw_nequal("host", hdbuf, 4)) {
  621. nva[i].name = (unsigned char *)":authority";
  622. nva[i].namelen = (uint16_t)strlen((char *)nva[i].name);
  623. }
  624. else {
  625. nva[i].name = (unsigned char *)hdbuf;
  626. nva[i].namelen = (uint16_t)(end - hdbuf);
  627. }
  628. hdbuf = end + 1;
  629. for(; *hdbuf == ' '; ++hdbuf);
  630. end = strchr(hdbuf, 0x0d);
  631. assert(end);
  632. nva[i].value = (unsigned char *)hdbuf;
  633. nva[i].valuelen = (uint16_t)(end - hdbuf);
  634. hdbuf = end + 2;
  635. /* Inspect Content-Length header field and retrieve the request
  636. entity length so that we can set END_STREAM to the last DATA
  637. frame. */
  638. if(nva[i].namelen == 14 &&
  639. Curl_raw_nequal("content-length", (char*)nva[i].name, 14)) {
  640. size_t j;
  641. for(j = 0; j < nva[i].valuelen; ++j) {
  642. httpc->upload_left *= 10;
  643. httpc->upload_left += nva[i].value[j] - '0';
  644. }
  645. infof(conn->data, "request content-length=%zu\n", httpc->upload_left);
  646. }
  647. }
  648. switch(conn->data->set.httpreq) {
  649. case HTTPREQ_POST:
  650. case HTTPREQ_POST_FORM:
  651. case HTTPREQ_PUT:
  652. data_prd.read_callback = data_source_read_callback;
  653. data_prd.source.ptr = NULL;
  654. rv = nghttp2_submit_request(httpc->h2, 0, nva, nheader, &data_prd, NULL);
  655. break;
  656. default:
  657. rv = nghttp2_submit_request(httpc->h2, 0, nva, nheader, NULL, NULL);
  658. }
  659. Curl_safefree(nva);
  660. if(rv != 0) {
  661. *err = CURLE_SEND_ERROR;
  662. return -1;
  663. }
  664. rv = nghttp2_session_send(httpc->h2);
  665. if(rv != 0) {
  666. *err = CURLE_SEND_ERROR;
  667. return -1;
  668. }
  669. if(httpc->stream_id != -1) {
  670. /* If whole HEADERS frame was sent off to the underlying socket,
  671. the nghttp2 library calls data_source_read_callback. But only
  672. it found that no data available, so it deferred the DATA
  673. transmission. Which means that nghttp2_session_want_write()
  674. returns 0 on http2_perform_getsock(), which results that no
  675. writable socket check is performed. To workaround this, we
  676. issue nghttp2_session_resume_data() here to bring back DATA
  677. transmission from deferred state. */
  678. nghttp2_session_resume_data(httpc->h2, httpc->stream_id);
  679. }
  680. return len;
  681. }
  682. int Curl_http2_switched(struct connectdata *conn)
  683. {
  684. int rv;
  685. CURLcode rc;
  686. struct http_conn *httpc = &conn->proto.httpc;
  687. /* we are switched! */
  688. /* Don't know this is needed here at this moment. Original
  689. handler->flags is still useful. */
  690. if(conn->handler->flags & PROTOPT_SSL)
  691. conn->handler = &Curl_handler_http2_ssl;
  692. else
  693. conn->handler = &Curl_handler_http2;
  694. httpc->recv_underlying = (recving)conn->recv[FIRSTSOCKET];
  695. httpc->send_underlying = (sending)conn->send[FIRSTSOCKET];
  696. conn->recv[FIRSTSOCKET] = http2_recv;
  697. conn->send[FIRSTSOCKET] = http2_send;
  698. infof(conn->data, "We have switched to HTTP2\n");
  699. httpc->bodystarted = FALSE;
  700. httpc->closed = FALSE;
  701. httpc->header_recvbuf = Curl_add_buffer_init();
  702. httpc->nread_header_recvbuf = 0;
  703. httpc->data = NULL;
  704. httpc->datalen = 0;
  705. httpc->upload_left = 0;
  706. httpc->upload_mem = NULL;
  707. httpc->upload_len = 0;
  708. conn->httpversion = 20;
  709. /* Put place holder for status line */
  710. Curl_add_buffer(httpc->header_recvbuf, "HTTP/2.0 200\r\n", 14);
  711. /* TODO: May get CURLE_AGAIN */
  712. rv = (int) ((Curl_send*)httpc->send_underlying)
  713. (conn, FIRSTSOCKET,
  714. NGHTTP2_CLIENT_CONNECTION_HEADER,
  715. NGHTTP2_CLIENT_CONNECTION_HEADER_LEN,
  716. &rc);
  717. assert(rv == 24);
  718. if(conn->data->req.upgr101 == UPGR101_RECEIVED) {
  719. /* stream 1 is opened implicitly on upgrade */
  720. httpc->stream_id = 1;
  721. /* queue SETTINGS frame (again) */
  722. rv = nghttp2_session_upgrade(httpc->h2, httpc->binsettings,
  723. httpc->binlen, NULL);
  724. if(rv != 0) {
  725. failf(conn->data, "nghttp2_session_upgrade() failed: %s(%d)",
  726. nghttp2_strerror(rv), rv);
  727. return -1;
  728. }
  729. }
  730. else {
  731. /* stream ID is unknown at this point */
  732. httpc->stream_id = -1;
  733. rv = nghttp2_submit_settings(httpc->h2, NGHTTP2_FLAG_NONE, NULL, 0);
  734. if(rv != 0) {
  735. failf(conn->data, "nghttp2_submit_settings() failed: %s(%d)",
  736. nghttp2_strerror(rv), rv);
  737. return -1;
  738. }
  739. }
  740. return 0;
  741. }
  742. #endif