http2.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  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 < 0x000600)
  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. size_t left, ncopy;
  167. (void)session;
  168. (void)frame;
  169. infof(conn->data, "on_frame_recv() was called with header %x\n",
  170. frame->hd.type);
  171. switch(frame->hd.type) {
  172. case NGHTTP2_DATA:
  173. /* If body started, then receiving DATA is illegal. */
  174. if(!c->bodystarted) {
  175. rv = nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
  176. frame->hd.stream_id,
  177. NGHTTP2_PROTOCOL_ERROR);
  178. if(nghttp2_is_fatal(rv)) {
  179. return NGHTTP2_ERR_CALLBACK_FAILURE;
  180. }
  181. }
  182. break;
  183. case NGHTTP2_HEADERS:
  184. if(frame->headers.cat == NGHTTP2_HCAT_REQUEST)
  185. break;
  186. if(c->bodystarted) {
  187. /* Only valid HEADERS after body started is trailer header,
  188. which is not fully supported in this code. If HEADERS is not
  189. trailer, then it is a PROTOCOL_ERROR. */
  190. if((frame->hd.flags & NGHTTP2_FLAG_END_STREAM) == 0) {
  191. rv = nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
  192. frame->hd.stream_id,
  193. NGHTTP2_PROTOCOL_ERROR);
  194. if(nghttp2_is_fatal(rv)) {
  195. return NGHTTP2_ERR_CALLBACK_FAILURE;
  196. }
  197. }
  198. break;
  199. }
  200. if(c->status_code == -1) {
  201. /* No :status header field means PROTOCOL_ERROR. */
  202. rv = nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
  203. frame->hd.stream_id,
  204. NGHTTP2_PROTOCOL_ERROR);
  205. if(nghttp2_is_fatal(rv)) {
  206. return NGHTTP2_ERR_CALLBACK_FAILURE;
  207. }
  208. break;
  209. }
  210. /* Only final status code signals the end of header */
  211. if(c->status_code / 100 != 1) {
  212. c->bodystarted = TRUE;
  213. }
  214. c->status_code = -1;
  215. Curl_add_buffer(c->header_recvbuf, "\r\n", 2);
  216. left = c->header_recvbuf->size_used - c->nread_header_recvbuf;
  217. ncopy = c->len < left ? c->len : left;
  218. memcpy(c->mem, c->header_recvbuf->buffer + c->nread_header_recvbuf, ncopy);
  219. c->nread_header_recvbuf += ncopy;
  220. c->mem += ncopy;
  221. c->len -= ncopy;
  222. break;
  223. case NGHTTP2_PUSH_PROMISE:
  224. rv = nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
  225. frame->push_promise.promised_stream_id,
  226. NGHTTP2_CANCEL);
  227. if(nghttp2_is_fatal(rv)) {
  228. return rv;
  229. }
  230. break;
  231. }
  232. return 0;
  233. }
  234. static int on_invalid_frame_recv(nghttp2_session *session,
  235. const nghttp2_frame *frame,
  236. uint32_t error_code, void *userp)
  237. {
  238. struct connectdata *conn = (struct connectdata *)userp;
  239. (void)session;
  240. (void)frame;
  241. infof(conn->data, "on_invalid_frame_recv() was called, error_code = %d\n",
  242. error_code);
  243. return 0;
  244. }
  245. static int on_data_chunk_recv(nghttp2_session *session, uint8_t flags,
  246. int32_t stream_id,
  247. const uint8_t *data, size_t len, void *userp)
  248. {
  249. struct connectdata *conn = (struct connectdata *)userp;
  250. struct http_conn *c = &conn->proto.httpc;
  251. size_t nread;
  252. (void)session;
  253. (void)flags;
  254. (void)data;
  255. infof(conn->data, "on_data_chunk_recv() "
  256. "len = %u, stream = %x\n", len, stream_id);
  257. if(stream_id != c->stream_id) {
  258. return 0;
  259. }
  260. nread = c->len < len ? c->len : len;
  261. memcpy(c->mem, data, nread);
  262. c->mem += nread;
  263. c->len -= nread;
  264. infof(conn->data, "%zu data written\n", nread);
  265. if(nread < len) {
  266. c->data = data + nread;
  267. c->datalen = len - nread;
  268. return NGHTTP2_ERR_PAUSE;
  269. }
  270. return 0;
  271. }
  272. static int before_frame_send(nghttp2_session *session,
  273. const nghttp2_frame *frame,
  274. void *userp)
  275. {
  276. struct connectdata *conn = (struct connectdata *)userp;
  277. (void)session;
  278. (void)frame;
  279. infof(conn->data, "before_frame_send() was called\n");
  280. return 0;
  281. }
  282. static int on_frame_send(nghttp2_session *session,
  283. const nghttp2_frame *frame,
  284. void *userp)
  285. {
  286. struct connectdata *conn = (struct connectdata *)userp;
  287. (void)session;
  288. (void)frame;
  289. infof(conn->data, "on_frame_send() was called\n");
  290. return 0;
  291. }
  292. static int on_frame_not_send(nghttp2_session *session,
  293. const nghttp2_frame *frame,
  294. int lib_error_code, void *userp)
  295. {
  296. struct connectdata *conn = (struct connectdata *)userp;
  297. (void)session;
  298. (void)frame;
  299. infof(conn->data, "on_frame_not_send() was called, lib_error_code = %d\n",
  300. lib_error_code);
  301. return 0;
  302. }
  303. static int on_stream_close(nghttp2_session *session, int32_t stream_id,
  304. uint32_t error_code, void *userp)
  305. {
  306. struct connectdata *conn = (struct connectdata *)userp;
  307. struct http_conn *c = &conn->proto.httpc;
  308. (void)session;
  309. (void)stream_id;
  310. infof(conn->data, "on_stream_close() was called, error_code = %d\n",
  311. error_code);
  312. if(stream_id != c->stream_id) {
  313. return 0;
  314. }
  315. c->closed = TRUE;
  316. return 0;
  317. }
  318. static int on_begin_headers(nghttp2_session *session,
  319. const nghttp2_frame *frame, void *userp)
  320. {
  321. struct connectdata *conn = (struct connectdata *)userp;
  322. (void)session;
  323. (void)frame;
  324. infof(conn->data, "on_begin_headers() was called\n");
  325. return 0;
  326. }
  327. /* Decode HTTP status code. Returns -1 if no valid status code was
  328. decoded. */
  329. static int decode_status_code(const uint8_t *value, size_t len)
  330. {
  331. int i;
  332. int res;
  333. if(len != 3) {
  334. return -1;
  335. }
  336. res = 0;
  337. for(i = 0; i < 3; ++i) {
  338. char c = value[i];
  339. if(c < '0' || c > '9') {
  340. return -1;
  341. }
  342. res *= 10;
  343. res += c - '0';
  344. }
  345. return res;
  346. }
  347. static const char STATUS[] = ":status";
  348. /* frame->hd.type is either NGHTTP2_HEADERS or NGHTTP2_PUSH_PROMISE */
  349. static int on_header(nghttp2_session *session, const nghttp2_frame *frame,
  350. const uint8_t *name, size_t namelen,
  351. const uint8_t *value, size_t valuelen,
  352. uint8_t flags,
  353. void *userp)
  354. {
  355. struct connectdata *conn = (struct connectdata *)userp;
  356. struct http_conn *c = &conn->proto.httpc;
  357. int rv;
  358. int goodname;
  359. int goodheader;
  360. (void)session;
  361. (void)frame;
  362. (void)flags;
  363. if(frame->hd.stream_id != c->stream_id) {
  364. return 0;
  365. }
  366. if(c->bodystarted) {
  367. /* Ignore trailer or HEADERS not mapped to HTTP semantics. The
  368. consequence is handled in on_frame_recv(). */
  369. return 0;
  370. }
  371. goodname = nghttp2_check_header_name(name, namelen);
  372. goodheader = nghttp2_check_header_value(value, valuelen);
  373. if(!goodname || !goodheader) {
  374. infof(conn->data, "Detected bad incoming header %s%s, reset stream!\n",
  375. goodname?"":"name",
  376. goodheader?"":"value");
  377. rv = nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
  378. frame->hd.stream_id,
  379. NGHTTP2_PROTOCOL_ERROR);
  380. if(nghttp2_is_fatal(rv)) {
  381. return NGHTTP2_ERR_CALLBACK_FAILURE;
  382. }
  383. return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
  384. }
  385. if(namelen == sizeof(":status") - 1 &&
  386. memcmp(STATUS, name, namelen) == 0) {
  387. /* :status must appear exactly once. */
  388. if(c->status_code != -1 ||
  389. (c->status_code = decode_status_code(value, valuelen)) == -1) {
  390. rv = nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
  391. frame->hd.stream_id,
  392. NGHTTP2_PROTOCOL_ERROR);
  393. if(nghttp2_is_fatal(rv)) {
  394. return NGHTTP2_ERR_CALLBACK_FAILURE;
  395. }
  396. return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
  397. }
  398. Curl_add_buffer(c->header_recvbuf, "HTTP/2.0 ", 9);
  399. Curl_add_buffer(c->header_recvbuf, value, valuelen);
  400. Curl_add_buffer(c->header_recvbuf, "\r\n", 2);
  401. return 0;
  402. }
  403. else {
  404. /* Here we are sure that namelen > 0 because of
  405. nghttp2_check_header_name(). Pseudo header other than :status
  406. is illegal. */
  407. if(c->status_code == -1 || name[0] == ':') {
  408. rv = nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
  409. frame->hd.stream_id,
  410. NGHTTP2_PROTOCOL_ERROR);
  411. if(nghttp2_is_fatal(rv)) {
  412. return NGHTTP2_ERR_CALLBACK_FAILURE;
  413. }
  414. return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
  415. }
  416. /* convert to a HTTP1-style header */
  417. Curl_add_buffer(c->header_recvbuf, name, namelen);
  418. Curl_add_buffer(c->header_recvbuf, ":", 1);
  419. Curl_add_buffer(c->header_recvbuf, value, valuelen);
  420. Curl_add_buffer(c->header_recvbuf, "\r\n", 2);
  421. infof(conn->data, "got http2 header: %.*s: %.*s\n",
  422. namelen, name, valuelen, value);
  423. }
  424. return 0; /* 0 is successful */
  425. }
  426. static ssize_t data_source_read_callback(nghttp2_session *session,
  427. int32_t stream_id,
  428. uint8_t *buf, size_t length,
  429. uint32_t *data_flags,
  430. nghttp2_data_source *source,
  431. void *userp)
  432. {
  433. struct connectdata *conn = (struct connectdata *)userp;
  434. struct http_conn *c = &conn->proto.httpc;
  435. size_t nread;
  436. (void)session;
  437. (void)stream_id;
  438. (void)source;
  439. nread = c->upload_len < length ? c->upload_len : length;
  440. if(nread > 0) {
  441. memcpy(buf, c->upload_mem, nread);
  442. c->upload_mem += nread;
  443. c->upload_len -= nread;
  444. c->upload_left -= nread;
  445. }
  446. if(c->upload_left == 0)
  447. *data_flags = 1;
  448. else if(nread == 0)
  449. return NGHTTP2_ERR_DEFERRED;
  450. return nread;
  451. }
  452. /*
  453. * The HTTP2 settings we send in the Upgrade request
  454. */
  455. static nghttp2_settings_entry settings[] = {
  456. { NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, 100 },
  457. { NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE, NGHTTP2_INITIAL_WINDOW_SIZE },
  458. };
  459. #define H2_BUFSIZE 4096
  460. /*
  461. * Initialize nghttp2 for a Curl connection
  462. */
  463. CURLcode Curl_http2_init(struct connectdata *conn)
  464. {
  465. if(!conn->proto.httpc.h2) {
  466. int rc;
  467. nghttp2_session_callbacks *callbacks;
  468. conn->proto.httpc.inbuf = malloc(H2_BUFSIZE);
  469. if(conn->proto.httpc.inbuf == NULL)
  470. return CURLE_OUT_OF_MEMORY;
  471. rc = nghttp2_session_callbacks_new(&callbacks);
  472. if(rc) {
  473. failf(conn->data, "Couldn't initialize nghttp2 callbacks!");
  474. return CURLE_OUT_OF_MEMORY; /* most likely at least */
  475. }
  476. /* nghttp2_send_callback */
  477. nghttp2_session_callbacks_set_send_callback(callbacks, send_callback);
  478. /* nghttp2_on_frame_recv_callback */
  479. nghttp2_session_callbacks_set_on_frame_recv_callback
  480. (callbacks, on_frame_recv);
  481. /* nghttp2_on_invalid_frame_recv_callback */
  482. nghttp2_session_callbacks_set_on_invalid_frame_recv_callback
  483. (callbacks, on_invalid_frame_recv);
  484. /* nghttp2_on_data_chunk_recv_callback */
  485. nghttp2_session_callbacks_set_on_data_chunk_recv_callback
  486. (callbacks, on_data_chunk_recv);
  487. /* nghttp2_before_frame_send_callback */
  488. nghttp2_session_callbacks_set_before_frame_send_callback
  489. (callbacks, before_frame_send);
  490. /* nghttp2_on_frame_send_callback */
  491. nghttp2_session_callbacks_set_on_frame_send_callback
  492. (callbacks, on_frame_send);
  493. /* nghttp2_on_frame_not_send_callback */
  494. nghttp2_session_callbacks_set_on_frame_not_send_callback
  495. (callbacks, on_frame_not_send);
  496. /* nghttp2_on_stream_close_callback */
  497. nghttp2_session_callbacks_set_on_stream_close_callback
  498. (callbacks, on_stream_close);
  499. /* nghttp2_on_begin_headers_callback */
  500. nghttp2_session_callbacks_set_on_begin_headers_callback
  501. (callbacks, on_begin_headers);
  502. /* nghttp2_on_header_callback */
  503. nghttp2_session_callbacks_set_on_header_callback(callbacks, on_header);
  504. /* The nghttp2 session is not yet setup, do it */
  505. rc = nghttp2_session_client_new(&conn->proto.httpc.h2,
  506. callbacks, conn);
  507. nghttp2_session_callbacks_del(callbacks);
  508. if(rc) {
  509. failf(conn->data, "Couldn't initialize nghttp2!");
  510. return CURLE_OUT_OF_MEMORY; /* most likely at least */
  511. }
  512. }
  513. return CURLE_OK;
  514. }
  515. /*
  516. * Send a request using http2
  517. */
  518. CURLcode Curl_http2_send_request(struct connectdata *conn)
  519. {
  520. (void)conn;
  521. return CURLE_OK;
  522. }
  523. /*
  524. * Append headers to ask for a HTTP1.1 to HTTP2 upgrade.
  525. */
  526. CURLcode Curl_http2_request_upgrade(Curl_send_buffer *req,
  527. struct connectdata *conn)
  528. {
  529. CURLcode result;
  530. ssize_t binlen;
  531. char *base64;
  532. size_t blen;
  533. struct SingleRequest *k = &conn->data->req;
  534. uint8_t *binsettings = conn->proto.httpc.binsettings;
  535. result = Curl_http2_init(conn);
  536. if(result)
  537. return result;
  538. result = Curl_http2_setup(conn);
  539. if(result)
  540. return result;
  541. /* As long as we have a fixed set of settings, we don't have to dynamically
  542. * figure out the base64 strings since it'll always be the same. However,
  543. * the settings will likely not be fixed every time in the future.
  544. */
  545. /* this returns number of bytes it wrote */
  546. binlen = nghttp2_pack_settings_payload(binsettings, H2_BINSETTINGS_LEN,
  547. settings,
  548. sizeof(settings)/sizeof(settings[0]));
  549. if(!binlen) {
  550. failf(conn->data, "nghttp2 unexpectedly failed on pack_settings_payload");
  551. return CURLE_FAILED_INIT;
  552. }
  553. conn->proto.httpc.binlen = binlen;
  554. result = Curl_base64url_encode(conn->data, (const char *)binsettings, binlen,
  555. &base64, &blen);
  556. if(result)
  557. return result;
  558. result = Curl_add_bufferf(req,
  559. "Connection: Upgrade, HTTP2-Settings\r\n"
  560. "Upgrade: %s\r\n"
  561. "HTTP2-Settings: %s\r\n",
  562. NGHTTP2_CLEARTEXT_PROTO_VERSION_ID, base64);
  563. Curl_safefree(base64);
  564. k->upgr101 = UPGR101_REQUESTED;
  565. return result;
  566. }
  567. /*
  568. * If the read would block (EWOULDBLOCK) we return -1. Otherwise we return
  569. * a regular CURLcode value.
  570. */
  571. static ssize_t http2_recv(struct connectdata *conn, int sockindex,
  572. char *mem, size_t len, CURLcode *err)
  573. {
  574. CURLcode rc;
  575. ssize_t rv;
  576. ssize_t nread;
  577. struct http_conn *httpc = &conn->proto.httpc;
  578. (void)sockindex; /* we always do HTTP2 on sockindex 0 */
  579. if(httpc->closed) {
  580. /* Reset to FALSE to prevent infinite loop in readwrite_data
  581. function. */
  582. httpc->closed = FALSE;
  583. return 0;
  584. }
  585. /* Nullify here because we call nghttp2_session_send() and they
  586. might refer to the old buffer. */
  587. httpc->upload_mem = NULL;
  588. httpc->upload_len = 0;
  589. if(httpc->bodystarted &&
  590. httpc->nread_header_recvbuf < httpc->header_recvbuf->size_used) {
  591. size_t left =
  592. httpc->header_recvbuf->size_used - httpc->nread_header_recvbuf;
  593. size_t ncopy = len < left ? len : left;
  594. memcpy(mem, httpc->header_recvbuf->buffer + httpc->nread_header_recvbuf,
  595. ncopy);
  596. httpc->nread_header_recvbuf += ncopy;
  597. return ncopy;
  598. }
  599. if(httpc->data) {
  600. nread = len < httpc->datalen ? len : httpc->datalen;
  601. memcpy(mem, httpc->data, nread);
  602. httpc->data += nread;
  603. httpc->datalen -= nread;
  604. infof(conn->data, "%zu data written\n", nread);
  605. if(httpc->datalen == 0) {
  606. httpc->data = NULL;
  607. httpc->datalen = 0;
  608. }
  609. return nread;
  610. }
  611. conn->proto.httpc.mem = mem;
  612. conn->proto.httpc.len = len;
  613. infof(conn->data, "http2_recv: %d bytes buffer\n",
  614. conn->proto.httpc.len);
  615. rc = 0;
  616. nread = ((Curl_recv*)httpc->recv_underlying)(conn, FIRSTSOCKET,
  617. httpc->inbuf, H2_BUFSIZE, &rc);
  618. if(rc == CURLE_AGAIN) {
  619. *err = rc;
  620. return -1;
  621. }
  622. if(nread == -1) {
  623. failf(conn->data, "Failed receiving HTTP2 data");
  624. *err = rc;
  625. return 0;
  626. }
  627. infof(conn->data, "nread=%zd\n", nread);
  628. if(nread == 0) {
  629. failf(conn->data, "EOF");
  630. return 0;
  631. }
  632. rv = nghttp2_session_mem_recv(httpc->h2,
  633. (const uint8_t *)httpc->inbuf, nread);
  634. if(nghttp2_is_fatal((int)rv)) {
  635. failf(conn->data, "nghttp2_session_mem_recv() returned %d:%s\n",
  636. rv, nghttp2_strerror((int)rv));
  637. *err = CURLE_RECV_ERROR;
  638. return 0;
  639. }
  640. infof(conn->data, "nghttp2_session_mem_recv() returns %zd\n", rv);
  641. /* Always send pending frames in nghttp2 session, because
  642. nghttp2_session_mem_recv() may queue new frame */
  643. rv = nghttp2_session_send(httpc->h2);
  644. if(rv != 0) {
  645. *err = CURLE_SEND_ERROR;
  646. return 0;
  647. }
  648. if(len != httpc->len) {
  649. return len - conn->proto.httpc.len;
  650. }
  651. /* If stream is closed, return 0 to signal the http routine to close
  652. the connection */
  653. if(httpc->closed) {
  654. /* Reset to FALSE to prevent infinite loop in readwrite_data
  655. function. */
  656. httpc->closed = FALSE;
  657. return 0;
  658. }
  659. *err = CURLE_AGAIN;
  660. return -1;
  661. }
  662. /* Index where :authority header field will appear in request header
  663. field list. */
  664. #define AUTHORITY_DST_IDX 3
  665. /* return number of received (decrypted) bytes */
  666. static ssize_t http2_send(struct connectdata *conn, int sockindex,
  667. const void *mem, size_t len, CURLcode *err)
  668. {
  669. /*
  670. * BIG TODO: Currently, we send request in this function, but this
  671. * function is also used to send request body. It would be nice to
  672. * add dedicated function for request.
  673. */
  674. int rv;
  675. struct http_conn *httpc = &conn->proto.httpc;
  676. nghttp2_nv *nva;
  677. size_t nheader;
  678. size_t i;
  679. size_t authority_idx;
  680. char *hdbuf = (char*)mem;
  681. char *end;
  682. nghttp2_data_provider data_prd;
  683. int32_t stream_id;
  684. (void)sockindex;
  685. infof(conn->data, "http2_send len=%zu\n", len);
  686. if(httpc->stream_id != -1) {
  687. /* If stream_id != -1, we have dispatched request HEADERS, and now
  688. are going to send or sending request body in DATA frame */
  689. httpc->upload_mem = mem;
  690. httpc->upload_len = len;
  691. nghttp2_session_resume_data(httpc->h2, httpc->stream_id);
  692. rv = nghttp2_session_send(httpc->h2);
  693. if(nghttp2_is_fatal(rv)) {
  694. *err = CURLE_SEND_ERROR;
  695. return -1;
  696. }
  697. return len - httpc->upload_len;
  698. }
  699. /* Calculate number of headers contained in [mem, mem + len) */
  700. /* Here, we assume the curl http code generate *correct* HTTP header
  701. field block */
  702. nheader = 0;
  703. for(i = 0; i < len; ++i) {
  704. if(hdbuf[i] == 0x0a) {
  705. ++nheader;
  706. }
  707. }
  708. /* We counted additional 2 \n in the first and last line. We need 3
  709. new headers: :method, :path and :scheme. Therefore we need one
  710. more space. */
  711. nheader += 1;
  712. nva = malloc(sizeof(nghttp2_nv) * nheader);
  713. if(nva == NULL) {
  714. *err = CURLE_OUT_OF_MEMORY;
  715. return -1;
  716. }
  717. /* Extract :method, :path from request line */
  718. end = strchr(hdbuf, ' ');
  719. nva[0].name = (unsigned char *)":method";
  720. nva[0].namelen = (uint16_t)strlen((char *)nva[0].name);
  721. nva[0].value = (unsigned char *)hdbuf;
  722. nva[0].valuelen = (uint16_t)(end - hdbuf);
  723. nva[0].flags = NGHTTP2_NV_FLAG_NONE;
  724. hdbuf = end + 1;
  725. end = strchr(hdbuf, ' ');
  726. nva[1].name = (unsigned char *)":path";
  727. nva[1].namelen = (uint16_t)strlen((char *)nva[1].name);
  728. nva[1].value = (unsigned char *)hdbuf;
  729. nva[1].valuelen = (uint16_t)(end - hdbuf);
  730. nva[1].flags = NGHTTP2_NV_FLAG_NONE;
  731. nva[2].name = (unsigned char *)":scheme";
  732. nva[2].namelen = (uint16_t)strlen((char *)nva[2].name);
  733. if(conn->handler->flags & PROTOPT_SSL)
  734. nva[2].value = (unsigned char *)"https";
  735. else
  736. nva[2].value = (unsigned char *)"http";
  737. nva[2].valuelen = (uint16_t)strlen((char *)nva[2].value);
  738. nva[2].flags = NGHTTP2_NV_FLAG_NONE;
  739. hdbuf = strchr(hdbuf, 0x0a);
  740. ++hdbuf;
  741. authority_idx = 0;
  742. for(i = 3; i < nheader; ++i) {
  743. end = strchr(hdbuf, ':');
  744. assert(end);
  745. if(end - hdbuf == 4 && Curl_raw_nequal("host", hdbuf, 4)) {
  746. authority_idx = i;
  747. nva[i].name = (unsigned char *)":authority";
  748. nva[i].namelen = (uint16_t)strlen((char *)nva[i].name);
  749. }
  750. else {
  751. nva[i].name = (unsigned char *)hdbuf;
  752. nva[i].namelen = (uint16_t)(end - hdbuf);
  753. }
  754. hdbuf = end + 1;
  755. for(; *hdbuf == ' '; ++hdbuf);
  756. end = strchr(hdbuf, 0x0d);
  757. assert(end);
  758. nva[i].value = (unsigned char *)hdbuf;
  759. nva[i].valuelen = (uint16_t)(end - hdbuf);
  760. nva[i].flags = NGHTTP2_NV_FLAG_NONE;
  761. hdbuf = end + 2;
  762. /* Inspect Content-Length header field and retrieve the request
  763. entity length so that we can set END_STREAM to the last DATA
  764. frame. */
  765. if(nva[i].namelen == 14 &&
  766. Curl_raw_nequal("content-length", (char*)nva[i].name, 14)) {
  767. size_t j;
  768. for(j = 0; j < nva[i].valuelen; ++j) {
  769. httpc->upload_left *= 10;
  770. httpc->upload_left += nva[i].value[j] - '0';
  771. }
  772. infof(conn->data, "request content-length=%zu\n", httpc->upload_left);
  773. }
  774. }
  775. /* :authority must come before non-pseudo header fields */
  776. if(authority_idx != 0 && authority_idx != AUTHORITY_DST_IDX) {
  777. nghttp2_nv authority = nva[authority_idx];
  778. for(i = authority_idx; i > AUTHORITY_DST_IDX; --i) {
  779. nva[i] = nva[i - 1];
  780. }
  781. nva[i] = authority;
  782. }
  783. switch(conn->data->set.httpreq) {
  784. case HTTPREQ_POST:
  785. case HTTPREQ_POST_FORM:
  786. case HTTPREQ_PUT:
  787. data_prd.read_callback = data_source_read_callback;
  788. data_prd.source.ptr = NULL;
  789. stream_id = nghttp2_submit_request(httpc->h2, NULL, nva, nheader,
  790. &data_prd, NULL);
  791. break;
  792. default:
  793. stream_id = nghttp2_submit_request(httpc->h2, NULL, nva, nheader,
  794. NULL, NULL);
  795. }
  796. Curl_safefree(nva);
  797. if(stream_id < 0) {
  798. *err = CURLE_SEND_ERROR;
  799. return -1;
  800. }
  801. httpc->stream_id = stream_id;
  802. rv = nghttp2_session_send(httpc->h2);
  803. if(rv != 0) {
  804. *err = CURLE_SEND_ERROR;
  805. return -1;
  806. }
  807. if(httpc->stream_id != -1) {
  808. /* If whole HEADERS frame was sent off to the underlying socket,
  809. the nghttp2 library calls data_source_read_callback. But only
  810. it found that no data available, so it deferred the DATA
  811. transmission. Which means that nghttp2_session_want_write()
  812. returns 0 on http2_perform_getsock(), which results that no
  813. writable socket check is performed. To workaround this, we
  814. issue nghttp2_session_resume_data() here to bring back DATA
  815. transmission from deferred state. */
  816. nghttp2_session_resume_data(httpc->h2, httpc->stream_id);
  817. }
  818. return len;
  819. }
  820. CURLcode Curl_http2_setup(struct connectdata *conn)
  821. {
  822. struct http_conn *httpc = &conn->proto.httpc;
  823. if(conn->handler->flags & PROTOPT_SSL)
  824. conn->handler = &Curl_handler_http2_ssl;
  825. else
  826. conn->handler = &Curl_handler_http2;
  827. infof(conn->data, "Using HTTP2\n");
  828. httpc->bodystarted = FALSE;
  829. httpc->closed = FALSE;
  830. httpc->header_recvbuf = Curl_add_buffer_init();
  831. httpc->nread_header_recvbuf = 0;
  832. httpc->data = NULL;
  833. httpc->datalen = 0;
  834. httpc->upload_left = 0;
  835. httpc->upload_mem = NULL;
  836. httpc->upload_len = 0;
  837. httpc->stream_id = -1;
  838. httpc->status_code = -1;
  839. conn->httpversion = 20;
  840. return 0;
  841. }
  842. CURLcode Curl_http2_switched(struct connectdata *conn)
  843. {
  844. CURLcode rc;
  845. struct http_conn *httpc = &conn->proto.httpc;
  846. int rv;
  847. struct SessionHandle *data = conn->data;
  848. httpc->recv_underlying = (recving)conn->recv[FIRSTSOCKET];
  849. httpc->send_underlying = (sending)conn->send[FIRSTSOCKET];
  850. conn->recv[FIRSTSOCKET] = http2_recv;
  851. conn->send[FIRSTSOCKET] = http2_send;
  852. rv = (int) ((Curl_send*)httpc->send_underlying)
  853. (conn, FIRSTSOCKET,
  854. NGHTTP2_CLIENT_CONNECTION_PREFACE,
  855. NGHTTP2_CLIENT_CONNECTION_PREFACE_LEN,
  856. &rc);
  857. if(rc)
  858. /* TODO: This may get CURLE_AGAIN */
  859. return rc;
  860. if(rv != 24) {
  861. failf(data, "Only sent partial HTTP2 packet");
  862. return CURLE_SEND_ERROR;
  863. }
  864. if(conn->data->req.upgr101 == UPGR101_RECEIVED) {
  865. /* stream 1 is opened implicitly on upgrade */
  866. httpc->stream_id = 1;
  867. /* queue SETTINGS frame (again) */
  868. rv = nghttp2_session_upgrade(httpc->h2, httpc->binsettings,
  869. httpc->binlen, NULL);
  870. if(rv != 0) {
  871. failf(data, "nghttp2_session_upgrade() failed: %s(%d)",
  872. nghttp2_strerror(rv), rv);
  873. return CURLE_HTTP2;
  874. }
  875. }
  876. else {
  877. /* stream ID is unknown at this point */
  878. httpc->stream_id = -1;
  879. rv = nghttp2_submit_settings(httpc->h2, NGHTTP2_FLAG_NONE, NULL, 0);
  880. if(rv != 0) {
  881. failf(data, "nghttp2_submit_settings() failed: %s(%d)",
  882. nghttp2_strerror(rv), rv);
  883. return CURLE_HTTP2;
  884. }
  885. }
  886. return CURLE_OK;
  887. }
  888. #endif