http2.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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. Curl_http, /* 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. Curl_http, /* 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_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. (void)session;
  236. (void)frame;
  237. infof(conn->data, "before_frame_send() was called\n");
  238. return 0;
  239. }
  240. static int on_frame_send(nghttp2_session *session,
  241. const nghttp2_frame *frame,
  242. void *userp)
  243. {
  244. struct connectdata *conn = (struct connectdata *)userp;
  245. (void)session;
  246. (void)frame;
  247. infof(conn->data, "on_frame_send() was called\n");
  248. return 0;
  249. }
  250. static int on_frame_not_send(nghttp2_session *session,
  251. const nghttp2_frame *frame,
  252. int lib_error_code, void *userp)
  253. {
  254. struct connectdata *conn = (struct connectdata *)userp;
  255. (void)session;
  256. (void)frame;
  257. infof(conn->data, "on_frame_not_send() was called, lib_error_code = %d\n",
  258. lib_error_code);
  259. return 0;
  260. }
  261. static int on_stream_close(nghttp2_session *session, int32_t stream_id,
  262. nghttp2_error_code error_code, void *userp)
  263. {
  264. struct connectdata *conn = (struct connectdata *)userp;
  265. struct http_conn *c = &conn->proto.httpc;
  266. (void)session;
  267. (void)stream_id;
  268. infof(conn->data, "on_stream_close() was called, error_code = %d\n",
  269. error_code);
  270. if(stream_id != c->stream_id) {
  271. return 0;
  272. }
  273. c->closed = TRUE;
  274. return 0;
  275. }
  276. static int on_unknown_frame_recv(nghttp2_session *session,
  277. const uint8_t *head, size_t headlen,
  278. const uint8_t *payload, size_t payloadlen,
  279. void *userp)
  280. {
  281. struct connectdata *conn = (struct connectdata *)userp;
  282. (void)session;
  283. (void)head;
  284. (void)headlen;
  285. (void)payload;
  286. (void)payloadlen;
  287. infof(conn->data, "on_unknown_frame_recv() was called\n");
  288. return 0;
  289. }
  290. static int on_begin_headers(nghttp2_session *session,
  291. const nghttp2_frame *frame, void *userp)
  292. {
  293. struct connectdata *conn = (struct connectdata *)userp;
  294. (void)session;
  295. (void)frame;
  296. infof(conn->data, "on_begin_headers() was called\n");
  297. return 0;
  298. }
  299. static const char STATUS[] = ":status";
  300. /* frame->hd.type is either NGHTTP2_HEADERS or NGHTTP2_PUSH_PROMISE */
  301. static int on_header(nghttp2_session *session, const nghttp2_frame *frame,
  302. const uint8_t *name, size_t namelen,
  303. const uint8_t *value, size_t valuelen,
  304. uint8_t flags,
  305. void *userp)
  306. {
  307. struct connectdata *conn = (struct connectdata *)userp;
  308. struct http_conn *c = &conn->proto.httpc;
  309. (void)session;
  310. (void)frame;
  311. (void)flags;
  312. if(frame->hd.stream_id != c->stream_id) {
  313. return 0;
  314. }
  315. if(namelen == sizeof(":status") - 1 &&
  316. memcmp(STATUS, name, namelen) == 0) {
  317. snprintf(c->header_recvbuf->buffer, 13, "HTTP/2.0 %s", value);
  318. c->header_recvbuf->buffer[12] = '\r';
  319. return 0;
  320. }
  321. else {
  322. /* convert to a HTTP1-style header */
  323. infof(conn->data, "got header\n");
  324. Curl_add_buffer(c->header_recvbuf, name, namelen);
  325. Curl_add_buffer(c->header_recvbuf, ":", 1);
  326. Curl_add_buffer(c->header_recvbuf, value, valuelen);
  327. Curl_add_buffer(c->header_recvbuf, "\r\n", 2);
  328. }
  329. return 0; /* 0 is successful */
  330. }
  331. /*
  332. * This is all callbacks nghttp2 calls
  333. */
  334. static const nghttp2_session_callbacks callbacks = {
  335. send_callback, /* nghttp2_send_callback */
  336. NULL, /* nghttp2_recv_callback */
  337. on_frame_recv, /* nghttp2_on_frame_recv_callback */
  338. on_invalid_frame_recv, /* nghttp2_on_invalid_frame_recv_callback */
  339. on_data_chunk_recv, /* nghttp2_on_data_chunk_recv_callback */
  340. before_frame_send, /* nghttp2_before_frame_send_callback */
  341. on_frame_send, /* nghttp2_on_frame_send_callback */
  342. on_frame_not_send, /* nghttp2_on_frame_not_send_callback */
  343. on_stream_close, /* nghttp2_on_stream_close_callback */
  344. on_unknown_frame_recv, /* nghttp2_on_unknown_frame_recv_callback */
  345. on_begin_headers, /* nghttp2_on_begin_headers_callback */
  346. on_header /* nghttp2_on_header_callback */
  347. #if NGHTTP2_VERSION_NUM >= 0x000400
  348. , NULL /* nghttp2_select_padding_callback */
  349. #endif
  350. };
  351. static ssize_t data_source_read_callback(nghttp2_session *session,
  352. int32_t stream_id,
  353. uint8_t *buf, size_t length,
  354. uint32_t *data_flags,
  355. nghttp2_data_source *source,
  356. void *userp)
  357. {
  358. struct connectdata *conn = (struct connectdata *)userp;
  359. struct http_conn *c = &conn->proto.httpc;
  360. size_t nread;
  361. (void)session;
  362. (void)stream_id;
  363. (void)source;
  364. nread = c->upload_len < length ? c->upload_len : length;
  365. if(nread > 0) {
  366. memcpy(buf, c->upload_mem, nread);
  367. c->upload_mem += nread;
  368. c->upload_len -= nread;
  369. c->upload_left -= nread;
  370. }
  371. if(c->upload_left == 0)
  372. *data_flags = 1;
  373. else if(nread == 0)
  374. return NGHTTP2_ERR_DEFERRED;
  375. return nread;
  376. }
  377. /*
  378. * The HTTP2 settings we send in the Upgrade request
  379. */
  380. static nghttp2_settings_entry settings[] = {
  381. { NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, 100 },
  382. { NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE, NGHTTP2_INITIAL_WINDOW_SIZE },
  383. };
  384. #define H2_BUFSIZE 4096
  385. /*
  386. * Initialize nghttp2 for a Curl connection
  387. */
  388. CURLcode Curl_http2_init(struct connectdata *conn)
  389. {
  390. if(!conn->proto.httpc.h2) {
  391. int rc;
  392. conn->proto.httpc.inbuf = malloc(H2_BUFSIZE);
  393. if(conn->proto.httpc.inbuf == NULL)
  394. return CURLE_OUT_OF_MEMORY;
  395. /* The nghttp2 session is not yet setup, do it */
  396. rc = nghttp2_session_client_new(&conn->proto.httpc.h2,
  397. &callbacks, conn);
  398. if(rc) {
  399. failf(conn->data, "Couldn't initialize nghttp2!");
  400. return CURLE_OUT_OF_MEMORY; /* most likely at least */
  401. }
  402. }
  403. return CURLE_OK;
  404. }
  405. /*
  406. * Send a request using http2
  407. */
  408. CURLcode Curl_http2_send_request(struct connectdata *conn)
  409. {
  410. (void)conn;
  411. return CURLE_OK;
  412. }
  413. /*
  414. * Append headers to ask for a HTTP1.1 to HTTP2 upgrade.
  415. */
  416. CURLcode Curl_http2_request_upgrade(Curl_send_buffer *req,
  417. struct connectdata *conn)
  418. {
  419. CURLcode result;
  420. ssize_t binlen;
  421. char *base64;
  422. size_t blen;
  423. struct SingleRequest *k = &conn->data->req;
  424. uint8_t *binsettings = conn->proto.httpc.binsettings;
  425. result = Curl_http2_init(conn);
  426. if(result)
  427. return result;
  428. result = Curl_http2_setup(conn);
  429. if(result)
  430. return result;
  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_CLEARTEXT_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. /* return number of received (decrypted) bytes */
  543. static ssize_t http2_send(struct connectdata *conn, int sockindex,
  544. const void *mem, size_t len, CURLcode *err)
  545. {
  546. /*
  547. * BIG TODO: Currently, we send request in this function, but this
  548. * function is also used to send request body. It would be nice to
  549. * add dedicated function for request.
  550. */
  551. int rv;
  552. struct http_conn *httpc = &conn->proto.httpc;
  553. nghttp2_nv *nva;
  554. size_t nheader;
  555. size_t i;
  556. char *hdbuf = (char*)mem;
  557. char *end;
  558. nghttp2_data_provider data_prd;
  559. int32_t stream_id;
  560. (void)sockindex;
  561. infof(conn->data, "http2_send len=%zu\n", len);
  562. if(httpc->stream_id != -1) {
  563. /* If stream_id != -1, we have dispatched request HEADERS, and now
  564. are going to send or sending request body in DATA frame */
  565. httpc->upload_mem = mem;
  566. httpc->upload_len = len;
  567. nghttp2_session_resume_data(httpc->h2, httpc->stream_id);
  568. rv = nghttp2_session_send(httpc->h2);
  569. if(nghttp2_is_fatal(rv)) {
  570. *err = CURLE_SEND_ERROR;
  571. return -1;
  572. }
  573. return len - httpc->upload_len;
  574. }
  575. /* Calculate number of headers contained in [mem, mem + len) */
  576. /* Here, we assume the curl http code generate *correct* HTTP header
  577. field block */
  578. nheader = 0;
  579. for(i = 0; i < len; ++i) {
  580. if(hdbuf[i] == 0x0a) {
  581. ++nheader;
  582. }
  583. }
  584. /* We counted additional 2 \n in the first and last line. We need 3
  585. new headers: :method, :path and :scheme. Therefore we need one
  586. more space. */
  587. nheader += 1;
  588. nva = malloc(sizeof(nghttp2_nv) * nheader);
  589. if(nva == NULL) {
  590. *err = CURLE_OUT_OF_MEMORY;
  591. return -1;
  592. }
  593. /* Extract :method, :path from request line */
  594. end = strchr(hdbuf, ' ');
  595. nva[0].name = (unsigned char *)":method";
  596. nva[0].namelen = (uint16_t)strlen((char *)nva[0].name);
  597. nva[0].value = (unsigned char *)hdbuf;
  598. nva[0].valuelen = (uint16_t)(end - hdbuf);
  599. nva[0].flags = NGHTTP2_NV_FLAG_NONE;
  600. hdbuf = end + 1;
  601. end = strchr(hdbuf, ' ');
  602. nva[1].name = (unsigned char *)":path";
  603. nva[1].namelen = (uint16_t)strlen((char *)nva[1].name);
  604. nva[1].value = (unsigned char *)hdbuf;
  605. nva[1].valuelen = (uint16_t)(end - hdbuf);
  606. nva[1].flags = NGHTTP2_NV_FLAG_NONE;
  607. nva[2].name = (unsigned char *)":scheme";
  608. nva[2].namelen = (uint16_t)strlen((char *)nva[2].name);
  609. if(conn->handler->flags & PROTOPT_SSL)
  610. nva[2].value = (unsigned char *)"https";
  611. else
  612. nva[2].value = (unsigned char *)"http";
  613. nva[2].valuelen = (uint16_t)strlen((char *)nva[2].value);
  614. nva[2].flags = NGHTTP2_NV_FLAG_NONE;
  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. nva[i].flags = NGHTTP2_NV_FLAG_NONE;
  635. hdbuf = end + 2;
  636. /* Inspect Content-Length header field and retrieve the request
  637. entity length so that we can set END_STREAM to the last DATA
  638. frame. */
  639. if(nva[i].namelen == 14 &&
  640. Curl_raw_nequal("content-length", (char*)nva[i].name, 14)) {
  641. size_t j;
  642. for(j = 0; j < nva[i].valuelen; ++j) {
  643. httpc->upload_left *= 10;
  644. httpc->upload_left += nva[i].value[j] - '0';
  645. }
  646. infof(conn->data, "request content-length=%zu\n", httpc->upload_left);
  647. }
  648. }
  649. switch(conn->data->set.httpreq) {
  650. case HTTPREQ_POST:
  651. case HTTPREQ_POST_FORM:
  652. case HTTPREQ_PUT:
  653. data_prd.read_callback = data_source_read_callback;
  654. data_prd.source.ptr = NULL;
  655. stream_id = nghttp2_submit_request(httpc->h2, NULL, nva, nheader,
  656. &data_prd, NULL);
  657. break;
  658. default:
  659. stream_id = nghttp2_submit_request(httpc->h2, NULL, nva, nheader,
  660. NULL, NULL);
  661. }
  662. Curl_safefree(nva);
  663. if(stream_id < 0) {
  664. *err = CURLE_SEND_ERROR;
  665. return -1;
  666. }
  667. httpc->stream_id = stream_id;
  668. rv = nghttp2_session_send(httpc->h2);
  669. if(rv != 0) {
  670. *err = CURLE_SEND_ERROR;
  671. return -1;
  672. }
  673. if(httpc->stream_id != -1) {
  674. /* If whole HEADERS frame was sent off to the underlying socket,
  675. the nghttp2 library calls data_source_read_callback. But only
  676. it found that no data available, so it deferred the DATA
  677. transmission. Which means that nghttp2_session_want_write()
  678. returns 0 on http2_perform_getsock(), which results that no
  679. writable socket check is performed. To workaround this, we
  680. issue nghttp2_session_resume_data() here to bring back DATA
  681. transmission from deferred state. */
  682. nghttp2_session_resume_data(httpc->h2, httpc->stream_id);
  683. }
  684. return len;
  685. }
  686. CURLcode Curl_http2_setup(struct connectdata *conn)
  687. {
  688. struct http_conn *httpc = &conn->proto.httpc;
  689. if(conn->handler->flags & PROTOPT_SSL)
  690. conn->handler = &Curl_handler_http2_ssl;
  691. else
  692. conn->handler = &Curl_handler_http2;
  693. infof(conn->data, "Using HTTP2\n");
  694. httpc->bodystarted = FALSE;
  695. httpc->closed = FALSE;
  696. httpc->header_recvbuf = Curl_add_buffer_init();
  697. httpc->nread_header_recvbuf = 0;
  698. httpc->data = NULL;
  699. httpc->datalen = 0;
  700. httpc->upload_left = 0;
  701. httpc->upload_mem = NULL;
  702. httpc->upload_len = 0;
  703. httpc->stream_id = -1;
  704. conn->httpversion = 20;
  705. /* Put place holder for status line */
  706. return Curl_add_buffer(httpc->header_recvbuf, "HTTP/2.0 200\r\n", 14);
  707. }
  708. int Curl_http2_switched(struct connectdata *conn)
  709. {
  710. /* TODO: May get CURLE_AGAIN */
  711. CURLcode rc;
  712. struct http_conn *httpc = &conn->proto.httpc;
  713. int rv;
  714. httpc->recv_underlying = (recving)conn->recv[FIRSTSOCKET];
  715. httpc->send_underlying = (sending)conn->send[FIRSTSOCKET];
  716. conn->recv[FIRSTSOCKET] = http2_recv;
  717. conn->send[FIRSTSOCKET] = http2_send;
  718. rv = (int) ((Curl_send*)httpc->send_underlying)
  719. (conn, FIRSTSOCKET,
  720. NGHTTP2_CLIENT_CONNECTION_PREFACE,
  721. NGHTTP2_CLIENT_CONNECTION_PREFACE_LEN,
  722. &rc);
  723. assert(rv == 24);
  724. if(conn->data->req.upgr101 == UPGR101_RECEIVED) {
  725. /* stream 1 is opened implicitly on upgrade */
  726. httpc->stream_id = 1;
  727. /* queue SETTINGS frame (again) */
  728. rv = nghttp2_session_upgrade(httpc->h2, httpc->binsettings,
  729. httpc->binlen, NULL);
  730. if(rv != 0) {
  731. failf(conn->data, "nghttp2_session_upgrade() failed: %s(%d)",
  732. nghttp2_strerror(rv), rv);
  733. return -1;
  734. }
  735. }
  736. else {
  737. /* stream ID is unknown at this point */
  738. httpc->stream_id = -1;
  739. rv = nghttp2_submit_settings(httpc->h2, NGHTTP2_FLAG_NONE, NULL, 0);
  740. if(rv != 0) {
  741. failf(conn->data, "nghttp2_submit_settings() failed: %s(%d)",
  742. nghttp2_strerror(rv), rv);
  743. return -1;
  744. }
  745. }
  746. return 0;
  747. }
  748. #endif