http2.c 31 KB

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