http2.c 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, 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 https://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. #include <nghttp2/nghttp2.h>
  25. #include "urldata.h"
  26. #include "http2.h"
  27. #include "http.h"
  28. #include "sendf.h"
  29. #include "select.h"
  30. #include "curl_base64.h"
  31. #include "strcase.h"
  32. #include "multiif.h"
  33. #include "url.h"
  34. #include "connect.h"
  35. #include "strtoofft.h"
  36. #include "strdup.h"
  37. #include "dynbuf.h"
  38. /* The last 3 #include files should be in this order */
  39. #include "curl_printf.h"
  40. #include "curl_memory.h"
  41. #include "memdebug.h"
  42. #define H2_BUFSIZE 32768
  43. #if (NGHTTP2_VERSION_NUM < 0x010c00)
  44. #error too old nghttp2 version, upgrade!
  45. #endif
  46. #ifdef CURL_DISABLE_VERBOSE_STRINGS
  47. #define nghttp2_session_callbacks_set_error_callback(x,y)
  48. #endif
  49. #if (NGHTTP2_VERSION_NUM >= 0x010c00)
  50. #define NGHTTP2_HAS_SET_LOCAL_WINDOW_SIZE 1
  51. #endif
  52. #define HTTP2_HUGE_WINDOW_SIZE (32 * 1024 * 1024) /* 32 MB */
  53. #ifdef DEBUG_HTTP2
  54. #define H2BUGF(x) x
  55. #else
  56. #define H2BUGF(x) do { } while(0)
  57. #endif
  58. static ssize_t http2_recv(struct connectdata *conn, int sockindex,
  59. char *mem, size_t len, CURLcode *err);
  60. static bool http2_connisdead(struct connectdata *conn);
  61. static int h2_session_send(struct Curl_easy *data,
  62. nghttp2_session *h2);
  63. static int h2_process_pending_input(struct connectdata *conn,
  64. struct http_conn *httpc,
  65. CURLcode *err);
  66. /*
  67. * Curl_http2_init_state() is called when the easy handle is created and
  68. * allows for HTTP/2 specific init of state.
  69. */
  70. void Curl_http2_init_state(struct UrlState *state)
  71. {
  72. state->stream_weight = NGHTTP2_DEFAULT_WEIGHT;
  73. }
  74. /*
  75. * Curl_http2_init_userset() is called when the easy handle is created and
  76. * allows for HTTP/2 specific user-set fields.
  77. */
  78. void Curl_http2_init_userset(struct UserDefined *set)
  79. {
  80. set->stream_weight = NGHTTP2_DEFAULT_WEIGHT;
  81. }
  82. static int http2_perform_getsock(const struct connectdata *conn,
  83. curl_socket_t *sock)
  84. {
  85. const struct http_conn *c = &conn->proto.httpc;
  86. struct SingleRequest *k = &conn->data->req;
  87. int bitmap = GETSOCK_BLANK;
  88. sock[0] = conn->sock[FIRSTSOCKET];
  89. /* in a HTTP/2 connection we can basically always get a frame so we should
  90. always be ready for one */
  91. bitmap |= GETSOCK_READSOCK(FIRSTSOCKET);
  92. /* we're still uploading or the HTTP/2 layer wants to send data */
  93. if(((k->keepon & (KEEP_SEND|KEEP_SEND_PAUSE)) == KEEP_SEND) ||
  94. nghttp2_session_want_write(c->h2))
  95. bitmap |= GETSOCK_WRITESOCK(FIRSTSOCKET);
  96. return bitmap;
  97. }
  98. static int http2_getsock(struct connectdata *conn,
  99. curl_socket_t *socks)
  100. {
  101. return http2_perform_getsock(conn, socks);
  102. }
  103. /*
  104. * http2_stream_free() free HTTP2 stream related data
  105. */
  106. static void http2_stream_free(struct HTTP *http)
  107. {
  108. if(http) {
  109. Curl_dyn_free(&http->header_recvbuf);
  110. for(; http->push_headers_used > 0; --http->push_headers_used) {
  111. free(http->push_headers[http->push_headers_used - 1]);
  112. }
  113. free(http->push_headers);
  114. http->push_headers = NULL;
  115. }
  116. }
  117. /*
  118. * Disconnects *a* connection used for HTTP/2. It might be an old one from the
  119. * connection cache and not the "main" one. Don't touch the easy handle!
  120. */
  121. static CURLcode http2_disconnect(struct connectdata *conn,
  122. bool dead_connection)
  123. {
  124. struct http_conn *c = &conn->proto.httpc;
  125. (void)dead_connection;
  126. H2BUGF(infof(conn->data, "HTTP/2 DISCONNECT starts now\n"));
  127. nghttp2_session_del(c->h2);
  128. Curl_safefree(c->inbuf);
  129. H2BUGF(infof(conn->data, "HTTP/2 DISCONNECT done\n"));
  130. return CURLE_OK;
  131. }
  132. /*
  133. * The server may send us data at any point (e.g. PING frames). Therefore,
  134. * we cannot assume that an HTTP/2 socket is dead just because it is readable.
  135. *
  136. * Instead, if it is readable, run Curl_connalive() to peek at the socket
  137. * and distinguish between closed and data.
  138. */
  139. static bool http2_connisdead(struct connectdata *conn)
  140. {
  141. int sval;
  142. bool dead = TRUE;
  143. if(conn->bits.close)
  144. return TRUE;
  145. sval = SOCKET_READABLE(conn->sock[FIRSTSOCKET], 0);
  146. if(sval == 0) {
  147. /* timeout */
  148. dead = FALSE;
  149. }
  150. else if(sval & CURL_CSELECT_ERR) {
  151. /* socket is in an error state */
  152. dead = TRUE;
  153. }
  154. else if(sval & CURL_CSELECT_IN) {
  155. /* readable with no error. could still be closed */
  156. dead = !Curl_connalive(conn);
  157. if(!dead) {
  158. /* This happens before we've sent off a request and the connection is
  159. not in use by any other transfer, there shouldn't be any data here,
  160. only "protocol frames" */
  161. CURLcode result;
  162. struct http_conn *httpc = &conn->proto.httpc;
  163. ssize_t nread = -1;
  164. if(httpc->recv_underlying)
  165. /* if called "too early", this pointer isn't setup yet! */
  166. nread = ((Curl_recv *)httpc->recv_underlying)(
  167. conn, FIRSTSOCKET, httpc->inbuf, H2_BUFSIZE, &result);
  168. if(nread != -1) {
  169. infof(conn->data,
  170. "%d bytes stray data read before trying h2 connection\n",
  171. (int)nread);
  172. httpc->nread_inbuf = 0;
  173. httpc->inbuflen = nread;
  174. (void)h2_process_pending_input(conn, httpc, &result);
  175. }
  176. else
  177. /* the read failed so let's say this is dead anyway */
  178. dead = TRUE;
  179. }
  180. }
  181. return dead;
  182. }
  183. static unsigned int http2_conncheck(struct connectdata *check,
  184. unsigned int checks_to_perform)
  185. {
  186. unsigned int ret_val = CONNRESULT_NONE;
  187. struct http_conn *c = &check->proto.httpc;
  188. int rc;
  189. bool send_frames = false;
  190. if(checks_to_perform & CONNCHECK_ISDEAD) {
  191. if(http2_connisdead(check))
  192. ret_val |= CONNRESULT_DEAD;
  193. }
  194. if(checks_to_perform & CONNCHECK_KEEPALIVE) {
  195. struct curltime now = Curl_now();
  196. timediff_t elapsed = Curl_timediff(now, check->keepalive);
  197. if(elapsed > check->upkeep_interval_ms) {
  198. /* Perform an HTTP/2 PING */
  199. rc = nghttp2_submit_ping(c->h2, 0, ZERO_NULL);
  200. if(!rc) {
  201. /* Successfully added a PING frame to the session. Need to flag this
  202. so the frame is sent. */
  203. send_frames = true;
  204. }
  205. else {
  206. failf(check->data, "nghttp2_submit_ping() failed: %s(%d)",
  207. nghttp2_strerror(rc), rc);
  208. }
  209. check->keepalive = now;
  210. }
  211. }
  212. if(send_frames) {
  213. rc = nghttp2_session_send(c->h2);
  214. if(rc)
  215. failf(check->data, "nghttp2_session_send() failed: %s(%d)",
  216. nghttp2_strerror(rc), rc);
  217. }
  218. return ret_val;
  219. }
  220. /* called from http_setup_conn */
  221. void Curl_http2_setup_req(struct Curl_easy *data)
  222. {
  223. struct HTTP *http = data->req.protop;
  224. http->bodystarted = FALSE;
  225. http->status_code = -1;
  226. http->pausedata = NULL;
  227. http->pauselen = 0;
  228. http->closed = FALSE;
  229. http->close_handled = FALSE;
  230. http->mem = data->state.buffer;
  231. http->len = data->set.buffer_size;
  232. http->memlen = 0;
  233. }
  234. /* called from http_setup_conn */
  235. void Curl_http2_setup_conn(struct connectdata *conn)
  236. {
  237. conn->proto.httpc.settings.max_concurrent_streams =
  238. DEFAULT_MAX_CONCURRENT_STREAMS;
  239. conn->proto.httpc.error_code = NGHTTP2_NO_ERROR;
  240. }
  241. /*
  242. * HTTP2 handler interface. This isn't added to the general list of protocols
  243. * but will be used at run-time when the protocol is dynamically switched from
  244. * HTTP to HTTP2.
  245. */
  246. static const struct Curl_handler Curl_handler_http2 = {
  247. "HTTP", /* scheme */
  248. ZERO_NULL, /* setup_connection */
  249. Curl_http, /* do_it */
  250. Curl_http_done, /* done */
  251. ZERO_NULL, /* do_more */
  252. ZERO_NULL, /* connect_it */
  253. ZERO_NULL, /* connecting */
  254. ZERO_NULL, /* doing */
  255. http2_getsock, /* proto_getsock */
  256. http2_getsock, /* doing_getsock */
  257. ZERO_NULL, /* domore_getsock */
  258. http2_perform_getsock, /* perform_getsock */
  259. http2_disconnect, /* disconnect */
  260. ZERO_NULL, /* readwrite */
  261. http2_conncheck, /* connection_check */
  262. PORT_HTTP, /* defport */
  263. CURLPROTO_HTTP, /* protocol */
  264. PROTOPT_STREAM /* flags */
  265. };
  266. static const struct Curl_handler Curl_handler_http2_ssl = {
  267. "HTTPS", /* scheme */
  268. ZERO_NULL, /* setup_connection */
  269. Curl_http, /* do_it */
  270. Curl_http_done, /* done */
  271. ZERO_NULL, /* do_more */
  272. ZERO_NULL, /* connect_it */
  273. ZERO_NULL, /* connecting */
  274. ZERO_NULL, /* doing */
  275. http2_getsock, /* proto_getsock */
  276. http2_getsock, /* doing_getsock */
  277. ZERO_NULL, /* domore_getsock */
  278. http2_perform_getsock, /* perform_getsock */
  279. http2_disconnect, /* disconnect */
  280. ZERO_NULL, /* readwrite */
  281. http2_conncheck, /* connection_check */
  282. PORT_HTTP, /* defport */
  283. CURLPROTO_HTTPS, /* protocol */
  284. PROTOPT_SSL | PROTOPT_STREAM /* flags */
  285. };
  286. /*
  287. * Store nghttp2 version info in this buffer, Prefix with a space. Return
  288. * total length written.
  289. */
  290. int Curl_http2_ver(char *p, size_t len)
  291. {
  292. nghttp2_info *h2 = nghttp2_version(0);
  293. return msnprintf(p, len, "nghttp2/%s", h2->version_str);
  294. }
  295. /*
  296. * The implementation of nghttp2_send_callback type. Here we write |data| with
  297. * size |length| to the network and return the number of bytes actually
  298. * written. See the documentation of nghttp2_send_callback for the details.
  299. */
  300. static ssize_t send_callback(nghttp2_session *h2,
  301. const uint8_t *data, size_t length, int flags,
  302. void *userp)
  303. {
  304. struct connectdata *conn = (struct connectdata *)userp;
  305. struct http_conn *c = &conn->proto.httpc;
  306. ssize_t written;
  307. CURLcode result = CURLE_OK;
  308. (void)h2;
  309. (void)flags;
  310. if(!c->send_underlying)
  311. /* called before setup properly! */
  312. return NGHTTP2_ERR_CALLBACK_FAILURE;
  313. written = ((Curl_send*)c->send_underlying)(conn, FIRSTSOCKET,
  314. data, length, &result);
  315. if(result == CURLE_AGAIN) {
  316. return NGHTTP2_ERR_WOULDBLOCK;
  317. }
  318. if(written == -1) {
  319. failf(conn->data, "Failed sending HTTP2 data");
  320. return NGHTTP2_ERR_CALLBACK_FAILURE;
  321. }
  322. if(!written)
  323. return NGHTTP2_ERR_WOULDBLOCK;
  324. return written;
  325. }
  326. /* We pass a pointer to this struct in the push callback, but the contents of
  327. the struct are hidden from the user. */
  328. struct curl_pushheaders {
  329. struct Curl_easy *data;
  330. const nghttp2_push_promise *frame;
  331. };
  332. /*
  333. * push header access function. Only to be used from within the push callback
  334. */
  335. char *curl_pushheader_bynum(struct curl_pushheaders *h, size_t num)
  336. {
  337. /* Verify that we got a good easy handle in the push header struct, mostly to
  338. detect rubbish input fast(er). */
  339. if(!h || !GOOD_EASY_HANDLE(h->data))
  340. return NULL;
  341. else {
  342. struct HTTP *stream = h->data->req.protop;
  343. if(num < stream->push_headers_used)
  344. return stream->push_headers[num];
  345. }
  346. return NULL;
  347. }
  348. /*
  349. * push header access function. Only to be used from within the push callback
  350. */
  351. char *curl_pushheader_byname(struct curl_pushheaders *h, const char *header)
  352. {
  353. /* Verify that we got a good easy handle in the push header struct,
  354. mostly to detect rubbish input fast(er). Also empty header name
  355. is just a rubbish too. We have to allow ":" at the beginning of
  356. the header, but header == ":" must be rejected. If we have ':' in
  357. the middle of header, it could be matched in middle of the value,
  358. this is because we do prefix match.*/
  359. if(!h || !GOOD_EASY_HANDLE(h->data) || !header || !header[0] ||
  360. !strcmp(header, ":") || strchr(header + 1, ':'))
  361. return NULL;
  362. else {
  363. struct HTTP *stream = h->data->req.protop;
  364. size_t len = strlen(header);
  365. size_t i;
  366. for(i = 0; i<stream->push_headers_used; i++) {
  367. if(!strncmp(header, stream->push_headers[i], len)) {
  368. /* sub-match, make sure that it is followed by a colon */
  369. if(stream->push_headers[i][len] != ':')
  370. continue;
  371. return &stream->push_headers[i][len + 1];
  372. }
  373. }
  374. }
  375. return NULL;
  376. }
  377. /*
  378. * This specific transfer on this connection has been "drained".
  379. */
  380. static void drained_transfer(struct Curl_easy *data,
  381. struct http_conn *httpc)
  382. {
  383. DEBUGASSERT(httpc->drain_total >= data->state.drain);
  384. httpc->drain_total -= data->state.drain;
  385. data->state.drain = 0;
  386. }
  387. /*
  388. * Mark this transfer to get "drained".
  389. */
  390. static void drain_this(struct Curl_easy *data,
  391. struct http_conn *httpc)
  392. {
  393. data->state.drain++;
  394. httpc->drain_total++;
  395. DEBUGASSERT(httpc->drain_total >= data->state.drain);
  396. }
  397. static struct Curl_easy *duphandle(struct Curl_easy *data)
  398. {
  399. struct Curl_easy *second = curl_easy_duphandle(data);
  400. if(second) {
  401. /* setup the request struct */
  402. struct HTTP *http = calloc(1, sizeof(struct HTTP));
  403. if(!http) {
  404. (void)Curl_close(&second);
  405. }
  406. else {
  407. second->req.protop = http;
  408. Curl_dyn_init(&http->header_recvbuf, DYN_H2_HEADERS);
  409. Curl_http2_setup_req(second);
  410. second->state.stream_weight = data->state.stream_weight;
  411. }
  412. }
  413. return second;
  414. }
  415. static int push_promise(struct Curl_easy *data,
  416. struct connectdata *conn,
  417. const nghttp2_push_promise *frame)
  418. {
  419. int rv;
  420. H2BUGF(infof(data, "PUSH_PROMISE received, stream %u!\n",
  421. frame->promised_stream_id));
  422. if(data->multi->push_cb) {
  423. struct HTTP *stream;
  424. struct HTTP *newstream;
  425. struct curl_pushheaders heads;
  426. CURLMcode rc;
  427. struct http_conn *httpc;
  428. size_t i;
  429. /* clone the parent */
  430. struct Curl_easy *newhandle = duphandle(data);
  431. if(!newhandle) {
  432. infof(data, "failed to duplicate handle\n");
  433. rv = 1; /* FAIL HARD */
  434. goto fail;
  435. }
  436. heads.data = data;
  437. heads.frame = frame;
  438. /* ask the application */
  439. H2BUGF(infof(data, "Got PUSH_PROMISE, ask application!\n"));
  440. stream = data->req.protop;
  441. if(!stream) {
  442. failf(data, "Internal NULL stream!\n");
  443. (void)Curl_close(&newhandle);
  444. rv = 1;
  445. goto fail;
  446. }
  447. Curl_set_in_callback(data, true);
  448. rv = data->multi->push_cb(data, newhandle,
  449. stream->push_headers_used, &heads,
  450. data->multi->push_userp);
  451. Curl_set_in_callback(data, false);
  452. /* free the headers again */
  453. for(i = 0; i<stream->push_headers_used; i++)
  454. free(stream->push_headers[i]);
  455. free(stream->push_headers);
  456. stream->push_headers = NULL;
  457. stream->push_headers_used = 0;
  458. if(rv) {
  459. /* denied, kill off the new handle again */
  460. http2_stream_free(newhandle->req.protop);
  461. newhandle->req.protop = NULL;
  462. (void)Curl_close(&newhandle);
  463. goto fail;
  464. }
  465. newstream = newhandle->req.protop;
  466. newstream->stream_id = frame->promised_stream_id;
  467. newhandle->req.maxdownload = -1;
  468. newhandle->req.size = -1;
  469. /* approved, add to the multi handle and immediately switch to PERFORM
  470. state with the given connection !*/
  471. rc = Curl_multi_add_perform(data->multi, newhandle, conn);
  472. if(rc) {
  473. infof(data, "failed to add handle to multi\n");
  474. http2_stream_free(newhandle->req.protop);
  475. newhandle->req.protop = NULL;
  476. Curl_close(&newhandle);
  477. rv = 1;
  478. goto fail;
  479. }
  480. httpc = &conn->proto.httpc;
  481. rv = nghttp2_session_set_stream_user_data(httpc->h2,
  482. frame->promised_stream_id,
  483. newhandle);
  484. if(rv) {
  485. infof(data, "failed to set user_data for stream %d\n",
  486. frame->promised_stream_id);
  487. DEBUGASSERT(0);
  488. goto fail;
  489. }
  490. }
  491. else {
  492. H2BUGF(infof(data, "Got PUSH_PROMISE, ignore it!\n"));
  493. rv = 1;
  494. }
  495. fail:
  496. return rv;
  497. }
  498. /*
  499. * multi_connchanged() is called to tell that there is a connection in
  500. * this multi handle that has changed state (multiplexing become possible, the
  501. * number of allowed streams changed or similar), and a subsequent use of this
  502. * multi handle should move CONNECT_PEND handles back to CONNECT to have them
  503. * retry.
  504. */
  505. static void multi_connchanged(struct Curl_multi *multi)
  506. {
  507. multi->recheckstate = TRUE;
  508. }
  509. static int on_frame_recv(nghttp2_session *session, const nghttp2_frame *frame,
  510. void *userp)
  511. {
  512. struct connectdata *conn = (struct connectdata *)userp;
  513. struct http_conn *httpc = &conn->proto.httpc;
  514. struct Curl_easy *data_s = NULL;
  515. struct HTTP *stream = NULL;
  516. int rv;
  517. size_t left, ncopy;
  518. int32_t stream_id = frame->hd.stream_id;
  519. CURLcode result;
  520. if(!stream_id) {
  521. /* stream ID zero is for connection-oriented stuff */
  522. if(frame->hd.type == NGHTTP2_SETTINGS) {
  523. uint32_t max_conn = httpc->settings.max_concurrent_streams;
  524. H2BUGF(infof(conn->data, "Got SETTINGS\n"));
  525. httpc->settings.max_concurrent_streams =
  526. nghttp2_session_get_remote_settings(
  527. session, NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS);
  528. httpc->settings.enable_push =
  529. nghttp2_session_get_remote_settings(
  530. session, NGHTTP2_SETTINGS_ENABLE_PUSH);
  531. H2BUGF(infof(conn->data, "MAX_CONCURRENT_STREAMS == %d\n",
  532. httpc->settings.max_concurrent_streams));
  533. H2BUGF(infof(conn->data, "ENABLE_PUSH == %s\n",
  534. httpc->settings.enable_push?"TRUE":"false"));
  535. if(max_conn != httpc->settings.max_concurrent_streams) {
  536. /* only signal change if the value actually changed */
  537. infof(conn->data,
  538. "Connection state changed (MAX_CONCURRENT_STREAMS == %u)!\n",
  539. httpc->settings.max_concurrent_streams);
  540. multi_connchanged(conn->data->multi);
  541. }
  542. }
  543. return 0;
  544. }
  545. data_s = nghttp2_session_get_stream_user_data(session, stream_id);
  546. if(!data_s) {
  547. H2BUGF(infof(conn->data,
  548. "No Curl_easy associated with stream: %x\n",
  549. stream_id));
  550. return 0;
  551. }
  552. stream = data_s->req.protop;
  553. if(!stream) {
  554. H2BUGF(infof(data_s, "No proto pointer for stream: %x\n",
  555. stream_id));
  556. return NGHTTP2_ERR_CALLBACK_FAILURE;
  557. }
  558. H2BUGF(infof(data_s, "on_frame_recv() header %x stream %x\n",
  559. frame->hd.type, stream_id));
  560. switch(frame->hd.type) {
  561. case NGHTTP2_DATA:
  562. /* If body started on this stream, then receiving DATA is illegal. */
  563. if(!stream->bodystarted) {
  564. rv = nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
  565. stream_id, NGHTTP2_PROTOCOL_ERROR);
  566. if(nghttp2_is_fatal(rv)) {
  567. return NGHTTP2_ERR_CALLBACK_FAILURE;
  568. }
  569. }
  570. break;
  571. case NGHTTP2_HEADERS:
  572. if(stream->bodystarted) {
  573. /* Only valid HEADERS after body started is trailer HEADERS. We
  574. buffer them in on_header callback. */
  575. break;
  576. }
  577. /* nghttp2 guarantees that :status is received, and we store it to
  578. stream->status_code. Fuzzing has proven this can still be reached
  579. without status code having been set. */
  580. if(stream->status_code == -1)
  581. return NGHTTP2_ERR_CALLBACK_FAILURE;
  582. /* Only final status code signals the end of header */
  583. if(stream->status_code / 100 != 1) {
  584. stream->bodystarted = TRUE;
  585. stream->status_code = -1;
  586. }
  587. result = Curl_dyn_add(&stream->header_recvbuf, "\r\n");
  588. if(result)
  589. return NGHTTP2_ERR_CALLBACK_FAILURE;
  590. left = Curl_dyn_len(&stream->header_recvbuf) -
  591. stream->nread_header_recvbuf;
  592. ncopy = CURLMIN(stream->len, left);
  593. memcpy(&stream->mem[stream->memlen],
  594. Curl_dyn_ptr(&stream->header_recvbuf) +
  595. stream->nread_header_recvbuf,
  596. ncopy);
  597. stream->nread_header_recvbuf += ncopy;
  598. H2BUGF(infof(data_s, "Store %zu bytes headers from stream %u at %p\n",
  599. ncopy, stream_id, stream->mem));
  600. stream->len -= ncopy;
  601. stream->memlen += ncopy;
  602. drain_this(data_s, httpc);
  603. {
  604. /* get the pointer from userp again since it was re-assigned above */
  605. struct connectdata *conn_s = (struct connectdata *)userp;
  606. /* if we receive data for another handle, wake that up */
  607. if(conn_s->data != data_s)
  608. Curl_expire(data_s, 0, EXPIRE_RUN_NOW);
  609. }
  610. break;
  611. case NGHTTP2_PUSH_PROMISE:
  612. rv = push_promise(data_s, conn, &frame->push_promise);
  613. if(rv) { /* deny! */
  614. rv = nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
  615. frame->push_promise.promised_stream_id,
  616. NGHTTP2_CANCEL);
  617. if(nghttp2_is_fatal(rv)) {
  618. return rv;
  619. }
  620. }
  621. break;
  622. default:
  623. H2BUGF(infof(data_s, "Got frame type %x for stream %u!\n",
  624. frame->hd.type, stream_id));
  625. break;
  626. }
  627. return 0;
  628. }
  629. static int on_data_chunk_recv(nghttp2_session *session, uint8_t flags,
  630. int32_t stream_id,
  631. const uint8_t *data, size_t len, void *userp)
  632. {
  633. struct HTTP *stream;
  634. struct Curl_easy *data_s;
  635. size_t nread;
  636. struct connectdata *conn = (struct connectdata *)userp;
  637. (void)session;
  638. (void)flags;
  639. (void)data;
  640. DEBUGASSERT(stream_id); /* should never be a zero stream ID here */
  641. /* get the stream from the hash based on Stream ID */
  642. data_s = nghttp2_session_get_stream_user_data(session, stream_id);
  643. if(!data_s)
  644. /* Receiving a Stream ID not in the hash should not happen, this is an
  645. internal error more than anything else! */
  646. return NGHTTP2_ERR_CALLBACK_FAILURE;
  647. stream = data_s->req.protop;
  648. if(!stream)
  649. return NGHTTP2_ERR_CALLBACK_FAILURE;
  650. nread = CURLMIN(stream->len, len);
  651. memcpy(&stream->mem[stream->memlen], data, nread);
  652. stream->len -= nread;
  653. stream->memlen += nread;
  654. drain_this(data_s, &conn->proto.httpc);
  655. /* if we receive data for another handle, wake that up */
  656. if(conn->data != data_s)
  657. Curl_expire(data_s, 0, EXPIRE_RUN_NOW);
  658. H2BUGF(infof(data_s, "%zu data received for stream %u "
  659. "(%zu left in buffer %p, total %zu)\n",
  660. nread, stream_id,
  661. stream->len, stream->mem,
  662. stream->memlen));
  663. if(nread < len) {
  664. stream->pausedata = data + nread;
  665. stream->pauselen = len - nread;
  666. H2BUGF(infof(data_s, "NGHTTP2_ERR_PAUSE - %zu bytes out of buffer"
  667. ", stream %u\n",
  668. len - nread, stream_id));
  669. data_s->conn->proto.httpc.pause_stream_id = stream_id;
  670. return NGHTTP2_ERR_PAUSE;
  671. }
  672. /* pause execution of nghttp2 if we received data for another handle
  673. in order to process them first. */
  674. if(conn->data != data_s) {
  675. data_s->conn->proto.httpc.pause_stream_id = stream_id;
  676. return NGHTTP2_ERR_PAUSE;
  677. }
  678. return 0;
  679. }
  680. static int on_stream_close(nghttp2_session *session, int32_t stream_id,
  681. uint32_t error_code, void *userp)
  682. {
  683. struct Curl_easy *data_s;
  684. struct HTTP *stream;
  685. struct connectdata *conn = (struct connectdata *)userp;
  686. int rv;
  687. (void)session;
  688. (void)stream_id;
  689. if(stream_id) {
  690. struct http_conn *httpc;
  691. /* get the stream from the hash based on Stream ID, stream ID zero is for
  692. connection-oriented stuff */
  693. data_s = nghttp2_session_get_stream_user_data(session, stream_id);
  694. if(!data_s) {
  695. /* We could get stream ID not in the hash. For example, if we
  696. decided to reject stream (e.g., PUSH_PROMISE). */
  697. return 0;
  698. }
  699. H2BUGF(infof(data_s, "on_stream_close(), %s (err %d), stream %u\n",
  700. nghttp2_strerror(error_code), error_code, stream_id));
  701. stream = data_s->req.protop;
  702. if(!stream)
  703. return NGHTTP2_ERR_CALLBACK_FAILURE;
  704. stream->closed = TRUE;
  705. httpc = &conn->proto.httpc;
  706. drain_this(data_s, httpc);
  707. Curl_expire(data_s, 0, EXPIRE_RUN_NOW);
  708. httpc->error_code = error_code;
  709. /* remove the entry from the hash as the stream is now gone */
  710. rv = nghttp2_session_set_stream_user_data(session, stream_id, 0);
  711. if(rv) {
  712. infof(data_s, "http/2: failed to clear user_data for stream %d!\n",
  713. stream_id);
  714. DEBUGASSERT(0);
  715. }
  716. if(stream_id == httpc->pause_stream_id) {
  717. H2BUGF(infof(data_s, "Stopped the pause stream!\n"));
  718. httpc->pause_stream_id = 0;
  719. }
  720. H2BUGF(infof(data_s, "Removed stream %u hash!\n", stream_id));
  721. stream->stream_id = 0; /* cleared */
  722. }
  723. return 0;
  724. }
  725. static int on_begin_headers(nghttp2_session *session,
  726. const nghttp2_frame *frame, void *userp)
  727. {
  728. struct HTTP *stream;
  729. struct Curl_easy *data_s = NULL;
  730. (void)userp;
  731. data_s = nghttp2_session_get_stream_user_data(session, frame->hd.stream_id);
  732. if(!data_s) {
  733. return 0;
  734. }
  735. H2BUGF(infof(data_s, "on_begin_headers() was called\n"));
  736. if(frame->hd.type != NGHTTP2_HEADERS) {
  737. return 0;
  738. }
  739. stream = data_s->req.protop;
  740. if(!stream || !stream->bodystarted) {
  741. return 0;
  742. }
  743. return 0;
  744. }
  745. /* Decode HTTP status code. Returns -1 if no valid status code was
  746. decoded. */
  747. static int decode_status_code(const uint8_t *value, size_t len)
  748. {
  749. int i;
  750. int res;
  751. if(len != 3) {
  752. return -1;
  753. }
  754. res = 0;
  755. for(i = 0; i < 3; ++i) {
  756. char c = value[i];
  757. if(c < '0' || c > '9') {
  758. return -1;
  759. }
  760. res *= 10;
  761. res += c - '0';
  762. }
  763. return res;
  764. }
  765. /* frame->hd.type is either NGHTTP2_HEADERS or NGHTTP2_PUSH_PROMISE */
  766. static int on_header(nghttp2_session *session, const nghttp2_frame *frame,
  767. const uint8_t *name, size_t namelen,
  768. const uint8_t *value, size_t valuelen,
  769. uint8_t flags,
  770. void *userp)
  771. {
  772. struct HTTP *stream;
  773. struct Curl_easy *data_s;
  774. int32_t stream_id = frame->hd.stream_id;
  775. struct connectdata *conn = (struct connectdata *)userp;
  776. CURLcode result;
  777. (void)flags;
  778. DEBUGASSERT(stream_id); /* should never be a zero stream ID here */
  779. /* get the stream from the hash based on Stream ID */
  780. data_s = nghttp2_session_get_stream_user_data(session, stream_id);
  781. if(!data_s)
  782. /* Receiving a Stream ID not in the hash should not happen, this is an
  783. internal error more than anything else! */
  784. return NGHTTP2_ERR_CALLBACK_FAILURE;
  785. stream = data_s->req.protop;
  786. if(!stream) {
  787. failf(data_s, "Internal NULL stream! 5\n");
  788. return NGHTTP2_ERR_CALLBACK_FAILURE;
  789. }
  790. /* Store received PUSH_PROMISE headers to be used when the subsequent
  791. PUSH_PROMISE callback comes */
  792. if(frame->hd.type == NGHTTP2_PUSH_PROMISE) {
  793. char *h;
  794. if(!strcmp(":authority", (const char *)name)) {
  795. /* pseudo headers are lower case */
  796. int rc = 0;
  797. char *check = aprintf("%s:%d", conn->host.name, conn->remote_port);
  798. if(!check)
  799. /* no memory */
  800. return NGHTTP2_ERR_CALLBACK_FAILURE;
  801. if(!Curl_strcasecompare(check, (const char *)value) &&
  802. ((conn->remote_port != conn->given->defport) ||
  803. !Curl_strcasecompare(conn->host.name, (const char *)value))) {
  804. /* This is push is not for the same authority that was asked for in
  805. * the URL. RFC 7540 section 8.2 says: "A client MUST treat a
  806. * PUSH_PROMISE for which the server is not authoritative as a stream
  807. * error of type PROTOCOL_ERROR."
  808. */
  809. (void)nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
  810. stream_id, NGHTTP2_PROTOCOL_ERROR);
  811. rc = NGHTTP2_ERR_CALLBACK_FAILURE;
  812. }
  813. free(check);
  814. if(rc)
  815. return rc;
  816. }
  817. if(!stream->push_headers) {
  818. stream->push_headers_alloc = 10;
  819. stream->push_headers = malloc(stream->push_headers_alloc *
  820. sizeof(char *));
  821. if(!stream->push_headers)
  822. return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
  823. stream->push_headers_used = 0;
  824. }
  825. else if(stream->push_headers_used ==
  826. stream->push_headers_alloc) {
  827. char **headp;
  828. stream->push_headers_alloc *= 2;
  829. headp = Curl_saferealloc(stream->push_headers,
  830. stream->push_headers_alloc * sizeof(char *));
  831. if(!headp) {
  832. stream->push_headers = NULL;
  833. return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
  834. }
  835. stream->push_headers = headp;
  836. }
  837. h = aprintf("%s:%s", name, value);
  838. if(h)
  839. stream->push_headers[stream->push_headers_used++] = h;
  840. return 0;
  841. }
  842. if(stream->bodystarted) {
  843. /* This is a trailer */
  844. struct dynbuf trail;
  845. H2BUGF(infof(data_s, "h2 trailer: %.*s: %.*s\n", namelen, name, valuelen,
  846. value));
  847. Curl_dyn_init(&trail, DYN_H2_TRAILER);
  848. result = Curl_dyn_addf(&trail,
  849. "%.*s: %.*s\r\n", namelen, name,
  850. valuelen, value);
  851. if(!result)
  852. result = Curl_client_write(conn, CLIENTWRITE_HEADER,
  853. Curl_dyn_ptr(&trail),
  854. Curl_dyn_len(&trail));
  855. Curl_dyn_free(&trail);
  856. if(result)
  857. return NGHTTP2_ERR_CALLBACK_FAILURE;
  858. return 0;
  859. }
  860. if(namelen == sizeof(":status") - 1 &&
  861. memcmp(":status", name, namelen) == 0) {
  862. /* nghttp2 guarantees :status is received first and only once, and
  863. value is 3 digits status code, and decode_status_code always
  864. succeeds. */
  865. stream->status_code = decode_status_code(value, valuelen);
  866. DEBUGASSERT(stream->status_code != -1);
  867. result = Curl_dyn_add(&stream->header_recvbuf, "HTTP/2 ");
  868. if(result)
  869. return NGHTTP2_ERR_CALLBACK_FAILURE;
  870. result = Curl_dyn_addn(&stream->header_recvbuf, value, valuelen);
  871. if(result)
  872. return NGHTTP2_ERR_CALLBACK_FAILURE;
  873. /* the space character after the status code is mandatory */
  874. result = Curl_dyn_add(&stream->header_recvbuf, " \r\n");
  875. if(result)
  876. return NGHTTP2_ERR_CALLBACK_FAILURE;
  877. /* if we receive data for another handle, wake that up */
  878. if(conn->data != data_s)
  879. Curl_expire(data_s, 0, EXPIRE_RUN_NOW);
  880. H2BUGF(infof(data_s, "h2 status: HTTP/2 %03d (easy %p)\n",
  881. stream->status_code, data_s));
  882. return 0;
  883. }
  884. /* nghttp2 guarantees that namelen > 0, and :status was already
  885. received, and this is not pseudo-header field . */
  886. /* convert to a HTTP1-style header */
  887. result = Curl_dyn_addn(&stream->header_recvbuf, name, namelen);
  888. if(result)
  889. return NGHTTP2_ERR_CALLBACK_FAILURE;
  890. result = Curl_dyn_add(&stream->header_recvbuf, ": ");
  891. if(result)
  892. return NGHTTP2_ERR_CALLBACK_FAILURE;
  893. result = Curl_dyn_addn(&stream->header_recvbuf, value, valuelen);
  894. if(result)
  895. return NGHTTP2_ERR_CALLBACK_FAILURE;
  896. result = Curl_dyn_add(&stream->header_recvbuf, "\r\n");
  897. if(result)
  898. return NGHTTP2_ERR_CALLBACK_FAILURE;
  899. /* if we receive data for another handle, wake that up */
  900. if(conn->data != data_s)
  901. Curl_expire(data_s, 0, EXPIRE_RUN_NOW);
  902. H2BUGF(infof(data_s, "h2 header: %.*s: %.*s\n", namelen, name, valuelen,
  903. value));
  904. return 0; /* 0 is successful */
  905. }
  906. static ssize_t data_source_read_callback(nghttp2_session *session,
  907. int32_t stream_id,
  908. uint8_t *buf, size_t length,
  909. uint32_t *data_flags,
  910. nghttp2_data_source *source,
  911. void *userp)
  912. {
  913. struct Curl_easy *data_s;
  914. struct HTTP *stream = NULL;
  915. size_t nread;
  916. (void)source;
  917. (void)userp;
  918. if(stream_id) {
  919. /* get the stream from the hash based on Stream ID, stream ID zero is for
  920. connection-oriented stuff */
  921. data_s = nghttp2_session_get_stream_user_data(session, stream_id);
  922. if(!data_s)
  923. /* Receiving a Stream ID not in the hash should not happen, this is an
  924. internal error more than anything else! */
  925. return NGHTTP2_ERR_CALLBACK_FAILURE;
  926. stream = data_s->req.protop;
  927. if(!stream)
  928. return NGHTTP2_ERR_CALLBACK_FAILURE;
  929. }
  930. else
  931. return NGHTTP2_ERR_INVALID_ARGUMENT;
  932. nread = CURLMIN(stream->upload_len, length);
  933. if(nread > 0) {
  934. memcpy(buf, stream->upload_mem, nread);
  935. stream->upload_mem += nread;
  936. stream->upload_len -= nread;
  937. if(data_s->state.infilesize != -1)
  938. stream->upload_left -= nread;
  939. }
  940. if(stream->upload_left == 0)
  941. *data_flags = NGHTTP2_DATA_FLAG_EOF;
  942. else if(nread == 0)
  943. return NGHTTP2_ERR_DEFERRED;
  944. H2BUGF(infof(data_s, "data_source_read_callback: "
  945. "returns %zu bytes stream %u\n",
  946. nread, stream_id));
  947. return nread;
  948. }
  949. #if !defined(CURL_DISABLE_VERBOSE_STRINGS)
  950. static int error_callback(nghttp2_session *session,
  951. const char *msg,
  952. size_t len,
  953. void *userp)
  954. {
  955. struct connectdata *conn = (struct connectdata *)userp;
  956. (void)session;
  957. infof(conn->data, "http2 error: %.*s\n", len, msg);
  958. return 0;
  959. }
  960. #endif
  961. static void populate_settings(struct connectdata *conn,
  962. struct http_conn *httpc)
  963. {
  964. nghttp2_settings_entry *iv = httpc->local_settings;
  965. DEBUGASSERT(conn->data);
  966. iv[0].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS;
  967. iv[0].value = Curl_multi_max_concurrent_streams(conn->data->multi);
  968. iv[1].settings_id = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE;
  969. iv[1].value = HTTP2_HUGE_WINDOW_SIZE;
  970. iv[2].settings_id = NGHTTP2_SETTINGS_ENABLE_PUSH;
  971. iv[2].value = conn->data->multi->push_cb != NULL;
  972. httpc->local_settings_num = 3;
  973. }
  974. void Curl_http2_done(struct Curl_easy *data, bool premature)
  975. {
  976. struct HTTP *http = data->req.protop;
  977. struct http_conn *httpc = &data->conn->proto.httpc;
  978. /* there might be allocated resources done before this got the 'h2' pointer
  979. setup */
  980. Curl_dyn_free(&http->header_recvbuf);
  981. if(http->push_headers) {
  982. /* if they weren't used and then freed before */
  983. for(; http->push_headers_used > 0; --http->push_headers_used) {
  984. free(http->push_headers[http->push_headers_used - 1]);
  985. }
  986. free(http->push_headers);
  987. http->push_headers = NULL;
  988. }
  989. if(!httpc->h2) /* not HTTP/2 ? */
  990. return;
  991. if(premature) {
  992. /* RST_STREAM */
  993. if(!nghttp2_submit_rst_stream(httpc->h2, NGHTTP2_FLAG_NONE,
  994. http->stream_id, NGHTTP2_STREAM_CLOSED))
  995. (void)nghttp2_session_send(httpc->h2);
  996. if(http->stream_id == httpc->pause_stream_id) {
  997. infof(data, "stopped the pause stream!\n");
  998. httpc->pause_stream_id = 0;
  999. }
  1000. }
  1001. if(data->state.drain)
  1002. drained_transfer(data, httpc);
  1003. /* -1 means unassigned and 0 means cleared */
  1004. if(http->stream_id > 0) {
  1005. int rv = nghttp2_session_set_stream_user_data(httpc->h2,
  1006. http->stream_id, 0);
  1007. if(rv) {
  1008. infof(data, "http/2: failed to clear user_data for stream %d!\n",
  1009. http->stream_id);
  1010. DEBUGASSERT(0);
  1011. }
  1012. http->stream_id = 0;
  1013. }
  1014. }
  1015. /*
  1016. * Initialize nghttp2 for a Curl connection
  1017. */
  1018. static CURLcode http2_init(struct connectdata *conn)
  1019. {
  1020. if(!conn->proto.httpc.h2) {
  1021. int rc;
  1022. nghttp2_session_callbacks *callbacks;
  1023. conn->proto.httpc.inbuf = malloc(H2_BUFSIZE);
  1024. if(conn->proto.httpc.inbuf == NULL)
  1025. return CURLE_OUT_OF_MEMORY;
  1026. rc = nghttp2_session_callbacks_new(&callbacks);
  1027. if(rc) {
  1028. failf(conn->data, "Couldn't initialize nghttp2 callbacks!");
  1029. return CURLE_OUT_OF_MEMORY; /* most likely at least */
  1030. }
  1031. /* nghttp2_send_callback */
  1032. nghttp2_session_callbacks_set_send_callback(callbacks, send_callback);
  1033. /* nghttp2_on_frame_recv_callback */
  1034. nghttp2_session_callbacks_set_on_frame_recv_callback
  1035. (callbacks, on_frame_recv);
  1036. /* nghttp2_on_data_chunk_recv_callback */
  1037. nghttp2_session_callbacks_set_on_data_chunk_recv_callback
  1038. (callbacks, on_data_chunk_recv);
  1039. /* nghttp2_on_stream_close_callback */
  1040. nghttp2_session_callbacks_set_on_stream_close_callback
  1041. (callbacks, on_stream_close);
  1042. /* nghttp2_on_begin_headers_callback */
  1043. nghttp2_session_callbacks_set_on_begin_headers_callback
  1044. (callbacks, on_begin_headers);
  1045. /* nghttp2_on_header_callback */
  1046. nghttp2_session_callbacks_set_on_header_callback(callbacks, on_header);
  1047. nghttp2_session_callbacks_set_error_callback(callbacks, error_callback);
  1048. /* The nghttp2 session is not yet setup, do it */
  1049. rc = nghttp2_session_client_new(&conn->proto.httpc.h2, callbacks, conn);
  1050. nghttp2_session_callbacks_del(callbacks);
  1051. if(rc) {
  1052. failf(conn->data, "Couldn't initialize nghttp2!");
  1053. return CURLE_OUT_OF_MEMORY; /* most likely at least */
  1054. }
  1055. }
  1056. return CURLE_OK;
  1057. }
  1058. /*
  1059. * Append headers to ask for a HTTP1.1 to HTTP2 upgrade.
  1060. */
  1061. CURLcode Curl_http2_request_upgrade(struct dynbuf *req,
  1062. struct connectdata *conn)
  1063. {
  1064. CURLcode result;
  1065. ssize_t binlen;
  1066. char *base64;
  1067. size_t blen;
  1068. struct SingleRequest *k = &conn->data->req;
  1069. uint8_t *binsettings = conn->proto.httpc.binsettings;
  1070. struct http_conn *httpc = &conn->proto.httpc;
  1071. populate_settings(conn, httpc);
  1072. /* this returns number of bytes it wrote */
  1073. binlen = nghttp2_pack_settings_payload(binsettings, H2_BINSETTINGS_LEN,
  1074. httpc->local_settings,
  1075. httpc->local_settings_num);
  1076. if(!binlen) {
  1077. failf(conn->data, "nghttp2 unexpectedly failed on pack_settings_payload");
  1078. Curl_dyn_free(req);
  1079. return CURLE_FAILED_INIT;
  1080. }
  1081. conn->proto.httpc.binlen = binlen;
  1082. result = Curl_base64url_encode(conn->data, (const char *)binsettings, binlen,
  1083. &base64, &blen);
  1084. if(result) {
  1085. Curl_dyn_free(req);
  1086. return result;
  1087. }
  1088. result = Curl_dyn_addf(req,
  1089. "Connection: Upgrade, HTTP2-Settings\r\n"
  1090. "Upgrade: %s\r\n"
  1091. "HTTP2-Settings: %s\r\n",
  1092. NGHTTP2_CLEARTEXT_PROTO_VERSION_ID, base64);
  1093. free(base64);
  1094. k->upgr101 = UPGR101_REQUESTED;
  1095. return result;
  1096. }
  1097. /*
  1098. * Returns nonzero if current HTTP/2 session should be closed.
  1099. */
  1100. static int should_close_session(struct http_conn *httpc)
  1101. {
  1102. return httpc->drain_total == 0 && !nghttp2_session_want_read(httpc->h2) &&
  1103. !nghttp2_session_want_write(httpc->h2);
  1104. }
  1105. /*
  1106. * h2_process_pending_input() processes pending input left in
  1107. * httpc->inbuf. Then, call h2_session_send() to send pending data.
  1108. * This function returns 0 if it succeeds, or -1 and error code will
  1109. * be assigned to *err.
  1110. */
  1111. static int h2_process_pending_input(struct connectdata *conn,
  1112. struct http_conn *httpc,
  1113. CURLcode *err)
  1114. {
  1115. ssize_t nread;
  1116. char *inbuf;
  1117. ssize_t rv;
  1118. struct Curl_easy *data = conn->data;
  1119. nread = httpc->inbuflen - httpc->nread_inbuf;
  1120. inbuf = httpc->inbuf + httpc->nread_inbuf;
  1121. rv = nghttp2_session_mem_recv(httpc->h2, (const uint8_t *)inbuf, nread);
  1122. if(rv < 0) {
  1123. failf(data,
  1124. "h2_process_pending_input: nghttp2_session_mem_recv() returned "
  1125. "%zd:%s\n", rv, nghttp2_strerror((int)rv));
  1126. *err = CURLE_RECV_ERROR;
  1127. return -1;
  1128. }
  1129. if(nread == rv) {
  1130. H2BUGF(infof(data,
  1131. "h2_process_pending_input: All data in connection buffer "
  1132. "processed\n"));
  1133. httpc->inbuflen = 0;
  1134. httpc->nread_inbuf = 0;
  1135. }
  1136. else {
  1137. httpc->nread_inbuf += rv;
  1138. H2BUGF(infof(data,
  1139. "h2_process_pending_input: %zu bytes left in connection "
  1140. "buffer\n",
  1141. httpc->inbuflen - httpc->nread_inbuf));
  1142. }
  1143. rv = h2_session_send(data, httpc->h2);
  1144. if(rv != 0) {
  1145. *err = CURLE_SEND_ERROR;
  1146. return -1;
  1147. }
  1148. if(should_close_session(httpc)) {
  1149. H2BUGF(infof(data,
  1150. "h2_process_pending_input: nothing to do in this session\n"));
  1151. if(httpc->error_code)
  1152. *err = CURLE_HTTP2;
  1153. else {
  1154. /* not an error per se, but should still close the connection */
  1155. connclose(conn, "GOAWAY received");
  1156. *err = CURLE_OK;
  1157. }
  1158. return -1;
  1159. }
  1160. return 0;
  1161. }
  1162. /*
  1163. * Called from transfer.c:done_sending when we stop uploading.
  1164. */
  1165. CURLcode Curl_http2_done_sending(struct connectdata *conn)
  1166. {
  1167. CURLcode result = CURLE_OK;
  1168. if((conn->handler == &Curl_handler_http2_ssl) ||
  1169. (conn->handler == &Curl_handler_http2)) {
  1170. /* make sure this is only attempted for HTTP/2 transfers */
  1171. struct HTTP *stream = conn->data->req.protop;
  1172. if(stream->upload_left) {
  1173. /* If the stream still thinks there's data left to upload. */
  1174. struct http_conn *httpc = &conn->proto.httpc;
  1175. nghttp2_session *h2 = httpc->h2;
  1176. stream->upload_left = 0; /* DONE! */
  1177. /* resume sending here to trigger the callback to get called again so
  1178. that it can signal EOF to nghttp2 */
  1179. (void)nghttp2_session_resume_data(h2, stream->stream_id);
  1180. (void)h2_process_pending_input(conn, httpc, &result);
  1181. }
  1182. }
  1183. return result;
  1184. }
  1185. static ssize_t http2_handle_stream_close(struct connectdata *conn,
  1186. struct Curl_easy *data,
  1187. struct HTTP *stream, CURLcode *err)
  1188. {
  1189. struct http_conn *httpc = &conn->proto.httpc;
  1190. if(httpc->pause_stream_id == stream->stream_id) {
  1191. httpc->pause_stream_id = 0;
  1192. }
  1193. drained_transfer(data, httpc);
  1194. if(httpc->pause_stream_id == 0) {
  1195. if(h2_process_pending_input(conn, httpc, err) != 0) {
  1196. return -1;
  1197. }
  1198. }
  1199. DEBUGASSERT(data->state.drain == 0);
  1200. /* Reset to FALSE to prevent infinite loop in readwrite_data function. */
  1201. stream->closed = FALSE;
  1202. if(httpc->error_code == NGHTTP2_REFUSED_STREAM) {
  1203. H2BUGF(infof(data, "REFUSED_STREAM (%d), try again on a new connection!\n",
  1204. stream->stream_id));
  1205. connclose(conn, "REFUSED_STREAM"); /* don't use this anymore */
  1206. data->state.refused_stream = TRUE;
  1207. *err = CURLE_RECV_ERROR; /* trigger Curl_retry_request() later */
  1208. return -1;
  1209. }
  1210. else if(httpc->error_code != NGHTTP2_NO_ERROR) {
  1211. failf(data, "HTTP/2 stream %d was not closed cleanly: %s (err %u)",
  1212. stream->stream_id, nghttp2_strerror(httpc->error_code),
  1213. httpc->error_code);
  1214. *err = CURLE_HTTP2_STREAM;
  1215. return -1;
  1216. }
  1217. if(!stream->bodystarted) {
  1218. failf(data, "HTTP/2 stream %d was closed cleanly, but before getting "
  1219. " all response header fields, treated as error",
  1220. stream->stream_id);
  1221. *err = CURLE_HTTP2_STREAM;
  1222. return -1;
  1223. }
  1224. stream->close_handled = TRUE;
  1225. H2BUGF(infof(data, "http2_recv returns 0, http2_handle_stream_close\n"));
  1226. return 0;
  1227. }
  1228. /*
  1229. * h2_pri_spec() fills in the pri_spec struct, used by nghttp2 to send weight
  1230. * and dependency to the peer. It also stores the updated values in the state
  1231. * struct.
  1232. */
  1233. static void h2_pri_spec(struct Curl_easy *data,
  1234. nghttp2_priority_spec *pri_spec)
  1235. {
  1236. struct HTTP *depstream = (data->set.stream_depends_on?
  1237. data->set.stream_depends_on->req.protop:NULL);
  1238. int32_t depstream_id = depstream? depstream->stream_id:0;
  1239. nghttp2_priority_spec_init(pri_spec, depstream_id, data->set.stream_weight,
  1240. data->set.stream_depends_e);
  1241. data->state.stream_weight = data->set.stream_weight;
  1242. data->state.stream_depends_e = data->set.stream_depends_e;
  1243. data->state.stream_depends_on = data->set.stream_depends_on;
  1244. }
  1245. /*
  1246. * h2_session_send() checks if there's been an update in the priority /
  1247. * dependency settings and if so it submits a PRIORITY frame with the updated
  1248. * info.
  1249. */
  1250. static int h2_session_send(struct Curl_easy *data,
  1251. nghttp2_session *h2)
  1252. {
  1253. struct HTTP *stream = data->req.protop;
  1254. if((data->set.stream_weight != data->state.stream_weight) ||
  1255. (data->set.stream_depends_e != data->state.stream_depends_e) ||
  1256. (data->set.stream_depends_on != data->state.stream_depends_on) ) {
  1257. /* send new weight and/or dependency */
  1258. nghttp2_priority_spec pri_spec;
  1259. int rv;
  1260. h2_pri_spec(data, &pri_spec);
  1261. H2BUGF(infof(data, "Queuing PRIORITY on stream %u (easy %p)\n",
  1262. stream->stream_id, data));
  1263. DEBUGASSERT(stream->stream_id != -1);
  1264. rv = nghttp2_submit_priority(h2, NGHTTP2_FLAG_NONE, stream->stream_id,
  1265. &pri_spec);
  1266. if(rv)
  1267. return rv;
  1268. }
  1269. return nghttp2_session_send(h2);
  1270. }
  1271. static ssize_t http2_recv(struct connectdata *conn, int sockindex,
  1272. char *mem, size_t len, CURLcode *err)
  1273. {
  1274. CURLcode result = CURLE_OK;
  1275. ssize_t rv;
  1276. ssize_t nread;
  1277. struct http_conn *httpc = &conn->proto.httpc;
  1278. struct Curl_easy *data = conn->data;
  1279. struct HTTP *stream = data->req.protop;
  1280. (void)sockindex; /* we always do HTTP2 on sockindex 0 */
  1281. if(should_close_session(httpc)) {
  1282. H2BUGF(infof(data,
  1283. "http2_recv: nothing to do in this session\n"));
  1284. if(conn->bits.close) {
  1285. /* already marked for closure, return OK and we're done */
  1286. *err = CURLE_OK;
  1287. return 0;
  1288. }
  1289. *err = CURLE_HTTP2;
  1290. return -1;
  1291. }
  1292. /* Nullify here because we call nghttp2_session_send() and they
  1293. might refer to the old buffer. */
  1294. stream->upload_mem = NULL;
  1295. stream->upload_len = 0;
  1296. /*
  1297. * At this point 'stream' is just in the Curl_easy the connection
  1298. * identifies as its owner at this time.
  1299. */
  1300. if(stream->bodystarted &&
  1301. stream->nread_header_recvbuf < Curl_dyn_len(&stream->header_recvbuf)) {
  1302. /* If there is header data pending for this stream to return, do that */
  1303. size_t left =
  1304. Curl_dyn_len(&stream->header_recvbuf) - stream->nread_header_recvbuf;
  1305. size_t ncopy = CURLMIN(len, left);
  1306. memcpy(mem, Curl_dyn_ptr(&stream->header_recvbuf) +
  1307. stream->nread_header_recvbuf, ncopy);
  1308. stream->nread_header_recvbuf += ncopy;
  1309. H2BUGF(infof(data, "http2_recv: Got %d bytes from header_recvbuf\n",
  1310. (int)ncopy));
  1311. return ncopy;
  1312. }
  1313. H2BUGF(infof(data, "http2_recv: easy %p (stream %u) win %u/%u\n",
  1314. data, stream->stream_id,
  1315. nghttp2_session_get_local_window_size(httpc->h2),
  1316. nghttp2_session_get_stream_local_window_size(httpc->h2,
  1317. stream->stream_id)
  1318. ));
  1319. if((data->state.drain) && stream->memlen) {
  1320. H2BUGF(infof(data, "http2_recv: DRAIN %zu bytes stream %u!! (%p => %p)\n",
  1321. stream->memlen, stream->stream_id,
  1322. stream->mem, mem));
  1323. if(mem != stream->mem) {
  1324. /* if we didn't get the same buffer this time, we must move the data to
  1325. the beginning */
  1326. memmove(mem, stream->mem, stream->memlen);
  1327. stream->len = len - stream->memlen;
  1328. stream->mem = mem;
  1329. }
  1330. if(httpc->pause_stream_id == stream->stream_id && !stream->pausedata) {
  1331. /* We have paused nghttp2, but we have no pause data (see
  1332. on_data_chunk_recv). */
  1333. httpc->pause_stream_id = 0;
  1334. if(h2_process_pending_input(conn, httpc, &result) != 0) {
  1335. *err = result;
  1336. return -1;
  1337. }
  1338. }
  1339. }
  1340. else if(stream->pausedata) {
  1341. DEBUGASSERT(httpc->pause_stream_id == stream->stream_id);
  1342. nread = CURLMIN(len, stream->pauselen);
  1343. memcpy(mem, stream->pausedata, nread);
  1344. stream->pausedata += nread;
  1345. stream->pauselen -= nread;
  1346. if(stream->pauselen == 0) {
  1347. H2BUGF(infof(data, "Unpaused by stream %u\n", stream->stream_id));
  1348. DEBUGASSERT(httpc->pause_stream_id == stream->stream_id);
  1349. httpc->pause_stream_id = 0;
  1350. stream->pausedata = NULL;
  1351. stream->pauselen = 0;
  1352. /* When NGHTTP2_ERR_PAUSE is returned from
  1353. data_source_read_callback, we might not process DATA frame
  1354. fully. Calling nghttp2_session_mem_recv() again will
  1355. continue to process DATA frame, but if there is no incoming
  1356. frames, then we have to call it again with 0-length data.
  1357. Without this, on_stream_close callback will not be called,
  1358. and stream could be hanged. */
  1359. if(h2_process_pending_input(conn, httpc, &result) != 0) {
  1360. *err = result;
  1361. return -1;
  1362. }
  1363. }
  1364. H2BUGF(infof(data, "http2_recv: returns unpaused %zd bytes on stream %u\n",
  1365. nread, stream->stream_id));
  1366. return nread;
  1367. }
  1368. else if(httpc->pause_stream_id) {
  1369. /* If a stream paused nghttp2_session_mem_recv previously, and has
  1370. not processed all data, it still refers to the buffer in
  1371. nghttp2_session. If we call nghttp2_session_mem_recv(), we may
  1372. overwrite that buffer. To avoid that situation, just return
  1373. here with CURLE_AGAIN. This could be busy loop since data in
  1374. socket is not read. But it seems that usually streams are
  1375. notified with its drain property, and socket is read again
  1376. quickly. */
  1377. if(stream->closed)
  1378. /* closed overrides paused */
  1379. return 0;
  1380. H2BUGF(infof(data, "stream %x is paused, pause id: %x\n",
  1381. stream->stream_id, httpc->pause_stream_id));
  1382. *err = CURLE_AGAIN;
  1383. return -1;
  1384. }
  1385. else {
  1386. char *inbuf;
  1387. /* remember where to store incoming data for this stream and how big the
  1388. buffer is */
  1389. stream->mem = mem;
  1390. stream->len = len;
  1391. stream->memlen = 0;
  1392. if(httpc->inbuflen == 0) {
  1393. nread = ((Curl_recv *)httpc->recv_underlying)(
  1394. conn, FIRSTSOCKET, httpc->inbuf, H2_BUFSIZE, &result);
  1395. if(nread == -1) {
  1396. if(result != CURLE_AGAIN)
  1397. failf(data, "Failed receiving HTTP2 data");
  1398. else if(stream->closed)
  1399. /* received when the stream was already closed! */
  1400. return http2_handle_stream_close(conn, data, stream, err);
  1401. *err = result;
  1402. return -1;
  1403. }
  1404. if(nread == 0) {
  1405. H2BUGF(infof(data, "end of stream\n"));
  1406. *err = CURLE_OK;
  1407. return 0;
  1408. }
  1409. H2BUGF(infof(data, "nread=%zd\n", nread));
  1410. httpc->inbuflen = nread;
  1411. inbuf = httpc->inbuf;
  1412. }
  1413. else {
  1414. nread = httpc->inbuflen - httpc->nread_inbuf;
  1415. inbuf = httpc->inbuf + httpc->nread_inbuf;
  1416. H2BUGF(infof(data, "Use data left in connection buffer, nread=%zd\n",
  1417. nread));
  1418. }
  1419. rv = nghttp2_session_mem_recv(httpc->h2, (const uint8_t *)inbuf, nread);
  1420. if(nghttp2_is_fatal((int)rv)) {
  1421. failf(data, "nghttp2_session_mem_recv() returned %zd:%s\n",
  1422. rv, nghttp2_strerror((int)rv));
  1423. *err = CURLE_RECV_ERROR;
  1424. return -1;
  1425. }
  1426. H2BUGF(infof(data, "nghttp2_session_mem_recv() returns %zd\n", rv));
  1427. if(nread == rv) {
  1428. H2BUGF(infof(data, "All data in connection buffer processed\n"));
  1429. httpc->inbuflen = 0;
  1430. httpc->nread_inbuf = 0;
  1431. }
  1432. else {
  1433. httpc->nread_inbuf += rv;
  1434. H2BUGF(infof(data, "%zu bytes left in connection buffer\n",
  1435. httpc->inbuflen - httpc->nread_inbuf));
  1436. }
  1437. /* Always send pending frames in nghttp2 session, because
  1438. nghttp2_session_mem_recv() may queue new frame */
  1439. rv = h2_session_send(data, httpc->h2);
  1440. if(rv != 0) {
  1441. *err = CURLE_SEND_ERROR;
  1442. return -1;
  1443. }
  1444. if(should_close_session(httpc)) {
  1445. H2BUGF(infof(data, "http2_recv: nothing to do in this session\n"));
  1446. *err = CURLE_HTTP2;
  1447. return -1;
  1448. }
  1449. }
  1450. if(stream->memlen) {
  1451. ssize_t retlen = stream->memlen;
  1452. H2BUGF(infof(data, "http2_recv: returns %zd for stream %u\n",
  1453. retlen, stream->stream_id));
  1454. stream->memlen = 0;
  1455. if(httpc->pause_stream_id == stream->stream_id) {
  1456. /* data for this stream is returned now, but this stream caused a pause
  1457. already so we need it called again asap */
  1458. H2BUGF(infof(data, "Data returned for PAUSED stream %u\n",
  1459. stream->stream_id));
  1460. }
  1461. else if(!stream->closed) {
  1462. drained_transfer(data, httpc);
  1463. }
  1464. else
  1465. /* this stream is closed, trigger a another read ASAP to detect that */
  1466. Curl_expire(data, 0, EXPIRE_RUN_NOW);
  1467. return retlen;
  1468. }
  1469. if(stream->closed)
  1470. return 0;
  1471. *err = CURLE_AGAIN;
  1472. H2BUGF(infof(data, "http2_recv returns AGAIN for stream %u\n",
  1473. stream->stream_id));
  1474. return -1;
  1475. }
  1476. /* Index where :authority header field will appear in request header
  1477. field list. */
  1478. #define AUTHORITY_DST_IDX 3
  1479. /* USHRT_MAX is 65535 == 0xffff */
  1480. #define HEADER_OVERFLOW(x) \
  1481. (x.namelen > 0xffff || x.valuelen > 0xffff - x.namelen)
  1482. /*
  1483. * Check header memory for the token "trailers".
  1484. * Parse the tokens as separated by comma and surrounded by whitespace.
  1485. * Returns TRUE if found or FALSE if not.
  1486. */
  1487. static bool contains_trailers(const char *p, size_t len)
  1488. {
  1489. const char *end = p + len;
  1490. for(;;) {
  1491. for(; p != end && (*p == ' ' || *p == '\t'); ++p)
  1492. ;
  1493. if(p == end || (size_t)(end - p) < sizeof("trailers") - 1)
  1494. return FALSE;
  1495. if(strncasecompare("trailers", p, sizeof("trailers") - 1)) {
  1496. p += sizeof("trailers") - 1;
  1497. for(; p != end && (*p == ' ' || *p == '\t'); ++p)
  1498. ;
  1499. if(p == end || *p == ',')
  1500. return TRUE;
  1501. }
  1502. /* skip to next token */
  1503. for(; p != end && *p != ','; ++p)
  1504. ;
  1505. if(p == end)
  1506. return FALSE;
  1507. ++p;
  1508. }
  1509. }
  1510. typedef enum {
  1511. /* Send header to server */
  1512. HEADERINST_FORWARD,
  1513. /* Don't send header to server */
  1514. HEADERINST_IGNORE,
  1515. /* Discard header, and replace it with "te: trailers" */
  1516. HEADERINST_TE_TRAILERS
  1517. } header_instruction;
  1518. /* Decides how to treat given header field. */
  1519. static header_instruction inspect_header(const char *name, size_t namelen,
  1520. const char *value, size_t valuelen) {
  1521. switch(namelen) {
  1522. case 2:
  1523. if(!strncasecompare("te", name, namelen))
  1524. return HEADERINST_FORWARD;
  1525. return contains_trailers(value, valuelen) ?
  1526. HEADERINST_TE_TRAILERS : HEADERINST_IGNORE;
  1527. case 7:
  1528. return strncasecompare("upgrade", name, namelen) ?
  1529. HEADERINST_IGNORE : HEADERINST_FORWARD;
  1530. case 10:
  1531. return (strncasecompare("connection", name, namelen) ||
  1532. strncasecompare("keep-alive", name, namelen)) ?
  1533. HEADERINST_IGNORE : HEADERINST_FORWARD;
  1534. case 16:
  1535. return strncasecompare("proxy-connection", name, namelen) ?
  1536. HEADERINST_IGNORE : HEADERINST_FORWARD;
  1537. case 17:
  1538. return strncasecompare("transfer-encoding", name, namelen) ?
  1539. HEADERINST_IGNORE : HEADERINST_FORWARD;
  1540. default:
  1541. return HEADERINST_FORWARD;
  1542. }
  1543. }
  1544. static ssize_t http2_send(struct connectdata *conn, int sockindex,
  1545. const void *mem, size_t len, CURLcode *err)
  1546. {
  1547. /*
  1548. * Currently, we send request in this function, but this function is also
  1549. * used to send request body. It would be nice to add dedicated function for
  1550. * request.
  1551. */
  1552. int rv;
  1553. struct http_conn *httpc = &conn->proto.httpc;
  1554. struct HTTP *stream = conn->data->req.protop;
  1555. nghttp2_nv *nva = NULL;
  1556. size_t nheader;
  1557. size_t i;
  1558. size_t authority_idx;
  1559. char *hdbuf = (char *)mem;
  1560. char *end, *line_end;
  1561. nghttp2_data_provider data_prd;
  1562. int32_t stream_id;
  1563. nghttp2_session *h2 = httpc->h2;
  1564. nghttp2_priority_spec pri_spec;
  1565. (void)sockindex;
  1566. H2BUGF(infof(conn->data, "http2_send len=%zu\n", len));
  1567. if(stream->stream_id != -1) {
  1568. if(stream->close_handled) {
  1569. infof(conn->data, "stream %d closed\n", stream->stream_id);
  1570. *err = CURLE_HTTP2_STREAM;
  1571. return -1;
  1572. }
  1573. else if(stream->closed) {
  1574. return http2_handle_stream_close(conn, conn->data, stream, err);
  1575. }
  1576. /* If stream_id != -1, we have dispatched request HEADERS, and now
  1577. are going to send or sending request body in DATA frame */
  1578. stream->upload_mem = mem;
  1579. stream->upload_len = len;
  1580. rv = nghttp2_session_resume_data(h2, stream->stream_id);
  1581. if(nghttp2_is_fatal(rv)) {
  1582. *err = CURLE_SEND_ERROR;
  1583. return -1;
  1584. }
  1585. rv = h2_session_send(conn->data, h2);
  1586. if(nghttp2_is_fatal(rv)) {
  1587. *err = CURLE_SEND_ERROR;
  1588. return -1;
  1589. }
  1590. len -= stream->upload_len;
  1591. /* Nullify here because we call nghttp2_session_send() and they
  1592. might refer to the old buffer. */
  1593. stream->upload_mem = NULL;
  1594. stream->upload_len = 0;
  1595. if(should_close_session(httpc)) {
  1596. H2BUGF(infof(conn->data, "http2_send: nothing to do in this session\n"));
  1597. *err = CURLE_HTTP2;
  1598. return -1;
  1599. }
  1600. if(stream->upload_left) {
  1601. /* we are sure that we have more data to send here. Calling the
  1602. following API will make nghttp2_session_want_write() return
  1603. nonzero if remote window allows it, which then libcurl checks
  1604. socket is writable or not. See http2_perform_getsock(). */
  1605. nghttp2_session_resume_data(h2, stream->stream_id);
  1606. }
  1607. H2BUGF(infof(conn->data, "http2_send returns %zu for stream %u\n", len,
  1608. stream->stream_id));
  1609. return len;
  1610. }
  1611. /* Calculate number of headers contained in [mem, mem + len) */
  1612. /* Here, we assume the curl http code generate *correct* HTTP header
  1613. field block */
  1614. nheader = 0;
  1615. for(i = 1; i < len; ++i) {
  1616. if(hdbuf[i] == '\n' && hdbuf[i - 1] == '\r') {
  1617. ++nheader;
  1618. ++i;
  1619. }
  1620. }
  1621. if(nheader < 2)
  1622. goto fail;
  1623. /* We counted additional 2 \r\n in the first and last line. We need 3
  1624. new headers: :method, :path and :scheme. Therefore we need one
  1625. more space. */
  1626. nheader += 1;
  1627. nva = malloc(sizeof(nghttp2_nv) * nheader);
  1628. if(nva == NULL) {
  1629. *err = CURLE_OUT_OF_MEMORY;
  1630. return -1;
  1631. }
  1632. /* Extract :method, :path from request line
  1633. We do line endings with CRLF so checking for CR is enough */
  1634. line_end = memchr(hdbuf, '\r', len);
  1635. if(!line_end)
  1636. goto fail;
  1637. /* Method does not contain spaces */
  1638. end = memchr(hdbuf, ' ', line_end - hdbuf);
  1639. if(!end || end == hdbuf)
  1640. goto fail;
  1641. nva[0].name = (unsigned char *)":method";
  1642. nva[0].namelen = strlen((char *)nva[0].name);
  1643. nva[0].value = (unsigned char *)hdbuf;
  1644. nva[0].valuelen = (size_t)(end - hdbuf);
  1645. nva[0].flags = NGHTTP2_NV_FLAG_NONE;
  1646. if(HEADER_OVERFLOW(nva[0])) {
  1647. failf(conn->data, "Failed sending HTTP request: Header overflow");
  1648. goto fail;
  1649. }
  1650. hdbuf = end + 1;
  1651. /* Path may contain spaces so scan backwards */
  1652. end = NULL;
  1653. for(i = (size_t)(line_end - hdbuf); i; --i) {
  1654. if(hdbuf[i - 1] == ' ') {
  1655. end = &hdbuf[i - 1];
  1656. break;
  1657. }
  1658. }
  1659. if(!end || end == hdbuf)
  1660. goto fail;
  1661. nva[1].name = (unsigned char *)":path";
  1662. nva[1].namelen = strlen((char *)nva[1].name);
  1663. nva[1].value = (unsigned char *)hdbuf;
  1664. nva[1].valuelen = (size_t)(end - hdbuf);
  1665. nva[1].flags = NGHTTP2_NV_FLAG_NONE;
  1666. if(HEADER_OVERFLOW(nva[1])) {
  1667. failf(conn->data, "Failed sending HTTP request: Header overflow");
  1668. goto fail;
  1669. }
  1670. nva[2].name = (unsigned char *)":scheme";
  1671. nva[2].namelen = strlen((char *)nva[2].name);
  1672. if(conn->handler->flags & PROTOPT_SSL)
  1673. nva[2].value = (unsigned char *)"https";
  1674. else
  1675. nva[2].value = (unsigned char *)"http";
  1676. nva[2].valuelen = strlen((char *)nva[2].value);
  1677. nva[2].flags = NGHTTP2_NV_FLAG_NONE;
  1678. if(HEADER_OVERFLOW(nva[2])) {
  1679. failf(conn->data, "Failed sending HTTP request: Header overflow");
  1680. goto fail;
  1681. }
  1682. authority_idx = 0;
  1683. i = 3;
  1684. while(i < nheader) {
  1685. size_t hlen;
  1686. hdbuf = line_end + 2;
  1687. /* check for next CR, but only within the piece of data left in the given
  1688. buffer */
  1689. line_end = memchr(hdbuf, '\r', len - (hdbuf - (char *)mem));
  1690. if(!line_end || (line_end == hdbuf))
  1691. goto fail;
  1692. /* header continuation lines are not supported */
  1693. if(*hdbuf == ' ' || *hdbuf == '\t')
  1694. goto fail;
  1695. for(end = hdbuf; end < line_end && *end != ':'; ++end)
  1696. ;
  1697. if(end == hdbuf || end == line_end)
  1698. goto fail;
  1699. hlen = end - hdbuf;
  1700. if(hlen == 4 && strncasecompare("host", hdbuf, 4)) {
  1701. authority_idx = i;
  1702. nva[i].name = (unsigned char *)":authority";
  1703. nva[i].namelen = strlen((char *)nva[i].name);
  1704. }
  1705. else {
  1706. nva[i].namelen = (size_t)(end - hdbuf);
  1707. /* Lower case the header name for HTTP/2 */
  1708. Curl_strntolower((char *)hdbuf, hdbuf, nva[i].namelen);
  1709. nva[i].name = (unsigned char *)hdbuf;
  1710. }
  1711. hdbuf = end + 1;
  1712. while(*hdbuf == ' ' || *hdbuf == '\t')
  1713. ++hdbuf;
  1714. end = line_end;
  1715. switch(inspect_header((const char *)nva[i].name, nva[i].namelen, hdbuf,
  1716. end - hdbuf)) {
  1717. case HEADERINST_IGNORE:
  1718. /* skip header fields prohibited by HTTP/2 specification. */
  1719. --nheader;
  1720. continue;
  1721. case HEADERINST_TE_TRAILERS:
  1722. nva[i].value = (uint8_t*)"trailers";
  1723. nva[i].valuelen = sizeof("trailers") - 1;
  1724. break;
  1725. default:
  1726. nva[i].value = (unsigned char *)hdbuf;
  1727. nva[i].valuelen = (size_t)(end - hdbuf);
  1728. }
  1729. nva[i].flags = NGHTTP2_NV_FLAG_NONE;
  1730. if(HEADER_OVERFLOW(nva[i])) {
  1731. failf(conn->data, "Failed sending HTTP request: Header overflow");
  1732. goto fail;
  1733. }
  1734. ++i;
  1735. }
  1736. /* :authority must come before non-pseudo header fields */
  1737. if(authority_idx != 0 && authority_idx != AUTHORITY_DST_IDX) {
  1738. nghttp2_nv authority = nva[authority_idx];
  1739. for(i = authority_idx; i > AUTHORITY_DST_IDX; --i) {
  1740. nva[i] = nva[i - 1];
  1741. }
  1742. nva[i] = authority;
  1743. }
  1744. /* Warn stream may be rejected if cumulative length of headers is too large.
  1745. It appears nghttp2 will not send a header frame larger than 64KB. */
  1746. #define MAX_ACC 60000 /* <64KB to account for some overhead */
  1747. {
  1748. size_t acc = 0;
  1749. for(i = 0; i < nheader; ++i) {
  1750. acc += nva[i].namelen + nva[i].valuelen;
  1751. H2BUGF(infof(conn->data, "h2 header: %.*s:%.*s\n",
  1752. nva[i].namelen, nva[i].name,
  1753. nva[i].valuelen, nva[i].value));
  1754. }
  1755. if(acc > MAX_ACC) {
  1756. infof(conn->data, "http2_send: Warning: The cumulative length of all "
  1757. "headers exceeds %zu bytes and that could cause the "
  1758. "stream to be rejected.\n", MAX_ACC);
  1759. }
  1760. }
  1761. h2_pri_spec(conn->data, &pri_spec);
  1762. switch(conn->data->set.httpreq) {
  1763. case HTTPREQ_POST:
  1764. case HTTPREQ_POST_FORM:
  1765. case HTTPREQ_POST_MIME:
  1766. case HTTPREQ_PUT:
  1767. if(conn->data->state.infilesize != -1)
  1768. stream->upload_left = conn->data->state.infilesize;
  1769. else
  1770. /* data sending without specifying the data amount up front */
  1771. stream->upload_left = -1; /* unknown, but not zero */
  1772. data_prd.read_callback = data_source_read_callback;
  1773. data_prd.source.ptr = NULL;
  1774. stream_id = nghttp2_submit_request(h2, &pri_spec, nva, nheader,
  1775. &data_prd, conn->data);
  1776. break;
  1777. default:
  1778. stream_id = nghttp2_submit_request(h2, &pri_spec, nva, nheader,
  1779. NULL, conn->data);
  1780. }
  1781. Curl_safefree(nva);
  1782. if(stream_id < 0) {
  1783. H2BUGF(infof(conn->data, "http2_send() send error\n"));
  1784. *err = CURLE_SEND_ERROR;
  1785. return -1;
  1786. }
  1787. infof(conn->data, "Using Stream ID: %x (easy handle %p)\n",
  1788. stream_id, (void *)conn->data);
  1789. stream->stream_id = stream_id;
  1790. /* this does not call h2_session_send() since there can not have been any
  1791. * priority upodate since the nghttp2_submit_request() call above */
  1792. rv = nghttp2_session_send(h2);
  1793. if(rv != 0) {
  1794. *err = CURLE_SEND_ERROR;
  1795. return -1;
  1796. }
  1797. if(should_close_session(httpc)) {
  1798. H2BUGF(infof(conn->data, "http2_send: nothing to do in this session\n"));
  1799. *err = CURLE_HTTP2;
  1800. return -1;
  1801. }
  1802. /* If whole HEADERS frame was sent off to the underlying socket, the nghttp2
  1803. library calls data_source_read_callback. But only it found that no data
  1804. available, so it deferred the DATA transmission. Which means that
  1805. nghttp2_session_want_write() returns 0 on http2_perform_getsock(), which
  1806. results that no writable socket check is performed. To workaround this,
  1807. we issue nghttp2_session_resume_data() here to bring back DATA
  1808. transmission from deferred state. */
  1809. nghttp2_session_resume_data(h2, stream->stream_id);
  1810. return len;
  1811. fail:
  1812. free(nva);
  1813. *err = CURLE_SEND_ERROR;
  1814. return -1;
  1815. }
  1816. CURLcode Curl_http2_setup(struct connectdata *conn)
  1817. {
  1818. CURLcode result;
  1819. struct http_conn *httpc = &conn->proto.httpc;
  1820. struct HTTP *stream = conn->data->req.protop;
  1821. stream->stream_id = -1;
  1822. Curl_dyn_init(&stream->header_recvbuf, DYN_H2_HEADERS);
  1823. if((conn->handler == &Curl_handler_http2_ssl) ||
  1824. (conn->handler == &Curl_handler_http2))
  1825. return CURLE_OK; /* already done */
  1826. if(conn->handler->flags & PROTOPT_SSL)
  1827. conn->handler = &Curl_handler_http2_ssl;
  1828. else
  1829. conn->handler = &Curl_handler_http2;
  1830. result = http2_init(conn);
  1831. if(result) {
  1832. Curl_dyn_free(&stream->header_recvbuf);
  1833. return result;
  1834. }
  1835. infof(conn->data, "Using HTTP2, server supports multi-use\n");
  1836. stream->upload_left = 0;
  1837. stream->upload_mem = NULL;
  1838. stream->upload_len = 0;
  1839. httpc->inbuflen = 0;
  1840. httpc->nread_inbuf = 0;
  1841. httpc->pause_stream_id = 0;
  1842. httpc->drain_total = 0;
  1843. conn->bits.multiplex = TRUE; /* at least potentially multiplexed */
  1844. conn->httpversion = 20;
  1845. conn->bundle->multiuse = BUNDLE_MULTIPLEX;
  1846. infof(conn->data, "Connection state changed (HTTP/2 confirmed)\n");
  1847. multi_connchanged(conn->data->multi);
  1848. return CURLE_OK;
  1849. }
  1850. CURLcode Curl_http2_switched(struct connectdata *conn,
  1851. const char *mem, size_t nread)
  1852. {
  1853. CURLcode result;
  1854. struct http_conn *httpc = &conn->proto.httpc;
  1855. int rv;
  1856. ssize_t nproc;
  1857. struct Curl_easy *data = conn->data;
  1858. struct HTTP *stream = conn->data->req.protop;
  1859. result = Curl_http2_setup(conn);
  1860. if(result)
  1861. return result;
  1862. httpc->recv_underlying = conn->recv[FIRSTSOCKET];
  1863. httpc->send_underlying = conn->send[FIRSTSOCKET];
  1864. conn->recv[FIRSTSOCKET] = http2_recv;
  1865. conn->send[FIRSTSOCKET] = http2_send;
  1866. if(conn->data->req.upgr101 == UPGR101_RECEIVED) {
  1867. /* stream 1 is opened implicitly on upgrade */
  1868. stream->stream_id = 1;
  1869. /* queue SETTINGS frame (again) */
  1870. rv = nghttp2_session_upgrade(httpc->h2, httpc->binsettings,
  1871. httpc->binlen, NULL);
  1872. if(rv != 0) {
  1873. failf(data, "nghttp2_session_upgrade() failed: %s(%d)",
  1874. nghttp2_strerror(rv), rv);
  1875. return CURLE_HTTP2;
  1876. }
  1877. rv = nghttp2_session_set_stream_user_data(httpc->h2,
  1878. stream->stream_id,
  1879. data);
  1880. if(rv) {
  1881. infof(data, "http/2: failed to set user_data for stream %d!\n",
  1882. stream->stream_id);
  1883. DEBUGASSERT(0);
  1884. }
  1885. }
  1886. else {
  1887. populate_settings(conn, httpc);
  1888. /* stream ID is unknown at this point */
  1889. stream->stream_id = -1;
  1890. rv = nghttp2_submit_settings(httpc->h2, NGHTTP2_FLAG_NONE,
  1891. httpc->local_settings,
  1892. httpc->local_settings_num);
  1893. if(rv != 0) {
  1894. failf(data, "nghttp2_submit_settings() failed: %s(%d)",
  1895. nghttp2_strerror(rv), rv);
  1896. return CURLE_HTTP2;
  1897. }
  1898. }
  1899. rv = nghttp2_session_set_local_window_size(httpc->h2, NGHTTP2_FLAG_NONE, 0,
  1900. HTTP2_HUGE_WINDOW_SIZE);
  1901. if(rv != 0) {
  1902. failf(data, "nghttp2_session_set_local_window_size() failed: %s(%d)",
  1903. nghttp2_strerror(rv), rv);
  1904. return CURLE_HTTP2;
  1905. }
  1906. /* we are going to copy mem to httpc->inbuf. This is required since
  1907. mem is part of buffer pointed by stream->mem, and callbacks
  1908. called by nghttp2_session_mem_recv() will write stream specific
  1909. data into stream->mem, overwriting data already there. */
  1910. if(H2_BUFSIZE < nread) {
  1911. failf(data, "connection buffer size is too small to store data following "
  1912. "HTTP Upgrade response header: buflen=%zu, datalen=%zu",
  1913. H2_BUFSIZE, nread);
  1914. return CURLE_HTTP2;
  1915. }
  1916. infof(conn->data, "Copying HTTP/2 data in stream buffer to connection buffer"
  1917. " after upgrade: len=%zu\n",
  1918. nread);
  1919. if(nread)
  1920. memcpy(httpc->inbuf, mem, nread);
  1921. httpc->inbuflen = nread;
  1922. nproc = nghttp2_session_mem_recv(httpc->h2, (const uint8_t *)httpc->inbuf,
  1923. httpc->inbuflen);
  1924. if(nghttp2_is_fatal((int)nproc)) {
  1925. failf(data, "nghttp2_session_mem_recv() failed: %s(%d)",
  1926. nghttp2_strerror((int)nproc), (int)nproc);
  1927. return CURLE_HTTP2;
  1928. }
  1929. H2BUGF(infof(data, "nghttp2_session_mem_recv() returns %zd\n", nproc));
  1930. if((ssize_t)nread == nproc) {
  1931. httpc->inbuflen = 0;
  1932. httpc->nread_inbuf = 0;
  1933. }
  1934. else {
  1935. httpc->nread_inbuf += nproc;
  1936. }
  1937. /* Try to send some frames since we may read SETTINGS already. */
  1938. rv = h2_session_send(data, httpc->h2);
  1939. if(rv != 0) {
  1940. failf(data, "nghttp2_session_send() failed: %s(%d)",
  1941. nghttp2_strerror(rv), rv);
  1942. return CURLE_HTTP2;
  1943. }
  1944. if(should_close_session(httpc)) {
  1945. H2BUGF(infof(data,
  1946. "nghttp2_session_send(): nothing to do in this session\n"));
  1947. return CURLE_HTTP2;
  1948. }
  1949. return CURLE_OK;
  1950. }
  1951. CURLcode Curl_http2_stream_pause(struct Curl_easy *data, bool pause)
  1952. {
  1953. DEBUGASSERT(data);
  1954. DEBUGASSERT(data->conn);
  1955. /* if it isn't HTTP/2, we're done */
  1956. if(!data->conn->proto.httpc.h2)
  1957. return CURLE_OK;
  1958. #ifdef NGHTTP2_HAS_SET_LOCAL_WINDOW_SIZE
  1959. else {
  1960. struct HTTP *stream = data->req.protop;
  1961. struct http_conn *httpc = &data->conn->proto.httpc;
  1962. uint32_t window = !pause * HTTP2_HUGE_WINDOW_SIZE;
  1963. int rv = nghttp2_session_set_local_window_size(httpc->h2,
  1964. NGHTTP2_FLAG_NONE,
  1965. stream->stream_id,
  1966. window);
  1967. if(rv) {
  1968. failf(data, "nghttp2_session_set_local_window_size() failed: %s(%d)",
  1969. nghttp2_strerror(rv), rv);
  1970. return CURLE_HTTP2;
  1971. }
  1972. /* make sure the window update gets sent */
  1973. rv = h2_session_send(data, httpc->h2);
  1974. if(rv)
  1975. return CURLE_SEND_ERROR;
  1976. DEBUGF(infof(data, "Set HTTP/2 window size to %u for stream %u\n",
  1977. window, stream->stream_id));
  1978. #ifdef DEBUGBUILD
  1979. {
  1980. /* read out the stream local window again */
  1981. uint32_t window2 =
  1982. nghttp2_session_get_stream_local_window_size(httpc->h2,
  1983. stream->stream_id);
  1984. DEBUGF(infof(data, "HTTP/2 window size is now %u for stream %u\n",
  1985. window2, stream->stream_id));
  1986. }
  1987. #endif
  1988. }
  1989. #endif
  1990. return CURLE_OK;
  1991. }
  1992. CURLcode Curl_http2_add_child(struct Curl_easy *parent,
  1993. struct Curl_easy *child,
  1994. bool exclusive)
  1995. {
  1996. if(parent) {
  1997. struct Curl_http2_dep **tail;
  1998. struct Curl_http2_dep *dep = calloc(1, sizeof(struct Curl_http2_dep));
  1999. if(!dep)
  2000. return CURLE_OUT_OF_MEMORY;
  2001. dep->data = child;
  2002. if(parent->set.stream_dependents && exclusive) {
  2003. struct Curl_http2_dep *node = parent->set.stream_dependents;
  2004. while(node) {
  2005. node->data->set.stream_depends_on = child;
  2006. node = node->next;
  2007. }
  2008. tail = &child->set.stream_dependents;
  2009. while(*tail)
  2010. tail = &(*tail)->next;
  2011. DEBUGASSERT(!*tail);
  2012. *tail = parent->set.stream_dependents;
  2013. parent->set.stream_dependents = 0;
  2014. }
  2015. tail = &parent->set.stream_dependents;
  2016. while(*tail) {
  2017. (*tail)->data->set.stream_depends_e = FALSE;
  2018. tail = &(*tail)->next;
  2019. }
  2020. DEBUGASSERT(!*tail);
  2021. *tail = dep;
  2022. }
  2023. child->set.stream_depends_on = parent;
  2024. child->set.stream_depends_e = exclusive;
  2025. return CURLE_OK;
  2026. }
  2027. void Curl_http2_remove_child(struct Curl_easy *parent, struct Curl_easy *child)
  2028. {
  2029. struct Curl_http2_dep *last = 0;
  2030. struct Curl_http2_dep *data = parent->set.stream_dependents;
  2031. DEBUGASSERT(child->set.stream_depends_on == parent);
  2032. while(data && data->data != child) {
  2033. last = data;
  2034. data = data->next;
  2035. }
  2036. DEBUGASSERT(data);
  2037. if(data) {
  2038. if(last) {
  2039. last->next = data->next;
  2040. }
  2041. else {
  2042. parent->set.stream_dependents = data->next;
  2043. }
  2044. free(data);
  2045. }
  2046. child->set.stream_depends_on = 0;
  2047. child->set.stream_depends_e = FALSE;
  2048. }
  2049. void Curl_http2_cleanup_dependencies(struct Curl_easy *data)
  2050. {
  2051. while(data->set.stream_dependents) {
  2052. struct Curl_easy *tmp = data->set.stream_dependents->data;
  2053. Curl_http2_remove_child(data, tmp);
  2054. if(data->set.stream_depends_on)
  2055. Curl_http2_add_child(data->set.stream_depends_on, tmp, FALSE);
  2056. }
  2057. if(data->set.stream_depends_on)
  2058. Curl_http2_remove_child(data->set.stream_depends_on, data);
  2059. }
  2060. /* Only call this function for a transfer that already got a HTTP/2
  2061. CURLE_HTTP2_STREAM error! */
  2062. bool Curl_h2_http_1_1_error(struct connectdata *conn)
  2063. {
  2064. struct http_conn *httpc = &conn->proto.httpc;
  2065. return (httpc->error_code == NGHTTP2_HTTP_1_1_REQUIRED);
  2066. }
  2067. #else /* !USE_NGHTTP2 */
  2068. /* Satisfy external references even if http2 is not compiled in. */
  2069. #include <curl/curl.h>
  2070. char *curl_pushheader_bynum(struct curl_pushheaders *h, size_t num)
  2071. {
  2072. (void) h;
  2073. (void) num;
  2074. return NULL;
  2075. }
  2076. char *curl_pushheader_byname(struct curl_pushheaders *h, const char *header)
  2077. {
  2078. (void) h;
  2079. (void) header;
  2080. return NULL;
  2081. }
  2082. #endif /* USE_NGHTTP2 */