http2.c 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506
  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 = NULL;
  231. http->len = 0;
  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 set_transfer_url(struct Curl_easy *data,
  416. struct curl_pushheaders *hp)
  417. {
  418. const char *v;
  419. CURLU *u = curl_url();
  420. CURLUcode uc;
  421. char *url;
  422. v = curl_pushheader_byname(hp, ":scheme");
  423. if(v) {
  424. uc = curl_url_set(u, CURLUPART_SCHEME, v, 0);
  425. if(uc)
  426. return 1;
  427. }
  428. v = curl_pushheader_byname(hp, ":authority");
  429. if(v) {
  430. uc = curl_url_set(u, CURLUPART_HOST, v, 0);
  431. if(uc)
  432. return 2;
  433. }
  434. v = curl_pushheader_byname(hp, ":path");
  435. if(v) {
  436. uc = curl_url_set(u, CURLUPART_PATH, v, 0);
  437. if(uc)
  438. return 3;
  439. }
  440. uc = curl_url_get(u, CURLUPART_URL, &url, 0);
  441. if(uc)
  442. return 4;
  443. curl_url_cleanup(u);
  444. if(data->change.url_alloc)
  445. free(data->change.url);
  446. data->change.url_alloc = TRUE;
  447. data->change.url = url;
  448. return 0;
  449. }
  450. static int push_promise(struct Curl_easy *data,
  451. struct connectdata *conn,
  452. const nghttp2_push_promise *frame)
  453. {
  454. int rv; /* one of the CURL_PUSH_* defines */
  455. H2BUGF(infof(data, "PUSH_PROMISE received, stream %u!\n",
  456. frame->promised_stream_id));
  457. if(data->multi->push_cb) {
  458. struct HTTP *stream;
  459. struct HTTP *newstream;
  460. struct curl_pushheaders heads;
  461. CURLMcode rc;
  462. struct http_conn *httpc;
  463. size_t i;
  464. /* clone the parent */
  465. struct Curl_easy *newhandle = duphandle(data);
  466. if(!newhandle) {
  467. infof(data, "failed to duplicate handle\n");
  468. rv = CURL_PUSH_DENY; /* FAIL HARD */
  469. goto fail;
  470. }
  471. heads.data = data;
  472. heads.frame = frame;
  473. /* ask the application */
  474. H2BUGF(infof(data, "Got PUSH_PROMISE, ask application!\n"));
  475. stream = data->req.protop;
  476. if(!stream) {
  477. failf(data, "Internal NULL stream!\n");
  478. (void)Curl_close(&newhandle);
  479. rv = CURL_PUSH_DENY;
  480. goto fail;
  481. }
  482. rv = set_transfer_url(newhandle, &heads);
  483. if(rv) {
  484. rv = CURL_PUSH_DENY;
  485. goto fail;
  486. }
  487. Curl_set_in_callback(data, true);
  488. rv = data->multi->push_cb(data, newhandle,
  489. stream->push_headers_used, &heads,
  490. data->multi->push_userp);
  491. Curl_set_in_callback(data, false);
  492. /* free the headers again */
  493. for(i = 0; i<stream->push_headers_used; i++)
  494. free(stream->push_headers[i]);
  495. free(stream->push_headers);
  496. stream->push_headers = NULL;
  497. stream->push_headers_used = 0;
  498. if(rv) {
  499. DEBUGASSERT((rv > CURL_PUSH_OK) && (rv <= CURL_PUSH_ERROROUT));
  500. /* denied, kill off the new handle again */
  501. http2_stream_free(newhandle->req.protop);
  502. newhandle->req.protop = NULL;
  503. (void)Curl_close(&newhandle);
  504. goto fail;
  505. }
  506. newstream = newhandle->req.protop;
  507. newstream->stream_id = frame->promised_stream_id;
  508. newhandle->req.maxdownload = -1;
  509. newhandle->req.size = -1;
  510. /* approved, add to the multi handle and immediately switch to PERFORM
  511. state with the given connection !*/
  512. rc = Curl_multi_add_perform(data->multi, newhandle, conn);
  513. if(rc) {
  514. infof(data, "failed to add handle to multi\n");
  515. http2_stream_free(newhandle->req.protop);
  516. newhandle->req.protop = NULL;
  517. Curl_close(&newhandle);
  518. rv = CURL_PUSH_DENY;
  519. goto fail;
  520. }
  521. httpc = &conn->proto.httpc;
  522. rv = nghttp2_session_set_stream_user_data(httpc->h2,
  523. frame->promised_stream_id,
  524. newhandle);
  525. if(rv) {
  526. infof(data, "failed to set user_data for stream %d\n",
  527. frame->promised_stream_id);
  528. DEBUGASSERT(0);
  529. rv = CURL_PUSH_DENY;
  530. goto fail;
  531. }
  532. }
  533. else {
  534. H2BUGF(infof(data, "Got PUSH_PROMISE, ignore it!\n"));
  535. rv = CURL_PUSH_DENY;
  536. }
  537. fail:
  538. return rv;
  539. }
  540. /*
  541. * multi_connchanged() is called to tell that there is a connection in
  542. * this multi handle that has changed state (multiplexing become possible, the
  543. * number of allowed streams changed or similar), and a subsequent use of this
  544. * multi handle should move CONNECT_PEND handles back to CONNECT to have them
  545. * retry.
  546. */
  547. static void multi_connchanged(struct Curl_multi *multi)
  548. {
  549. multi->recheckstate = TRUE;
  550. }
  551. static int on_frame_recv(nghttp2_session *session, const nghttp2_frame *frame,
  552. void *userp)
  553. {
  554. struct connectdata *conn = (struct connectdata *)userp;
  555. struct http_conn *httpc = &conn->proto.httpc;
  556. struct Curl_easy *data_s = NULL;
  557. struct HTTP *stream = NULL;
  558. int rv;
  559. size_t left, ncopy;
  560. int32_t stream_id = frame->hd.stream_id;
  561. CURLcode result;
  562. if(!stream_id) {
  563. /* stream ID zero is for connection-oriented stuff */
  564. if(frame->hd.type == NGHTTP2_SETTINGS) {
  565. uint32_t max_conn = httpc->settings.max_concurrent_streams;
  566. H2BUGF(infof(conn->data, "Got SETTINGS\n"));
  567. httpc->settings.max_concurrent_streams =
  568. nghttp2_session_get_remote_settings(
  569. session, NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS);
  570. httpc->settings.enable_push =
  571. nghttp2_session_get_remote_settings(
  572. session, NGHTTP2_SETTINGS_ENABLE_PUSH);
  573. H2BUGF(infof(conn->data, "MAX_CONCURRENT_STREAMS == %d\n",
  574. httpc->settings.max_concurrent_streams));
  575. H2BUGF(infof(conn->data, "ENABLE_PUSH == %s\n",
  576. httpc->settings.enable_push?"TRUE":"false"));
  577. if(max_conn != httpc->settings.max_concurrent_streams) {
  578. /* only signal change if the value actually changed */
  579. infof(conn->data,
  580. "Connection state changed (MAX_CONCURRENT_STREAMS == %u)!\n",
  581. httpc->settings.max_concurrent_streams);
  582. multi_connchanged(conn->data->multi);
  583. }
  584. }
  585. return 0;
  586. }
  587. data_s = nghttp2_session_get_stream_user_data(session, stream_id);
  588. if(!data_s) {
  589. H2BUGF(infof(conn->data,
  590. "No Curl_easy associated with stream: %x\n",
  591. stream_id));
  592. return 0;
  593. }
  594. stream = data_s->req.protop;
  595. if(!stream) {
  596. H2BUGF(infof(data_s, "No proto pointer for stream: %x\n",
  597. stream_id));
  598. return NGHTTP2_ERR_CALLBACK_FAILURE;
  599. }
  600. H2BUGF(infof(data_s, "on_frame_recv() header %x stream %x\n",
  601. frame->hd.type, stream_id));
  602. switch(frame->hd.type) {
  603. case NGHTTP2_DATA:
  604. /* If body started on this stream, then receiving DATA is illegal. */
  605. if(!stream->bodystarted) {
  606. rv = nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
  607. stream_id, NGHTTP2_PROTOCOL_ERROR);
  608. if(nghttp2_is_fatal(rv)) {
  609. return NGHTTP2_ERR_CALLBACK_FAILURE;
  610. }
  611. }
  612. break;
  613. case NGHTTP2_HEADERS:
  614. if(stream->bodystarted) {
  615. /* Only valid HEADERS after body started is trailer HEADERS. We
  616. buffer them in on_header callback. */
  617. break;
  618. }
  619. /* nghttp2 guarantees that :status is received, and we store it to
  620. stream->status_code. Fuzzing has proven this can still be reached
  621. without status code having been set. */
  622. if(stream->status_code == -1)
  623. return NGHTTP2_ERR_CALLBACK_FAILURE;
  624. /* Only final status code signals the end of header */
  625. if(stream->status_code / 100 != 1) {
  626. stream->bodystarted = TRUE;
  627. stream->status_code = -1;
  628. }
  629. result = Curl_dyn_add(&stream->header_recvbuf, "\r\n");
  630. if(result)
  631. return NGHTTP2_ERR_CALLBACK_FAILURE;
  632. left = Curl_dyn_len(&stream->header_recvbuf) -
  633. stream->nread_header_recvbuf;
  634. ncopy = CURLMIN(stream->len, left);
  635. memcpy(&stream->mem[stream->memlen],
  636. Curl_dyn_ptr(&stream->header_recvbuf) +
  637. stream->nread_header_recvbuf,
  638. ncopy);
  639. stream->nread_header_recvbuf += ncopy;
  640. H2BUGF(infof(data_s, "Store %zu bytes headers from stream %u at %p\n",
  641. ncopy, stream_id, stream->mem));
  642. stream->len -= ncopy;
  643. stream->memlen += ncopy;
  644. drain_this(data_s, httpc);
  645. {
  646. /* get the pointer from userp again since it was re-assigned above */
  647. struct connectdata *conn_s = (struct connectdata *)userp;
  648. /* if we receive data for another handle, wake that up */
  649. if(conn_s->data != data_s)
  650. Curl_expire(data_s, 0, EXPIRE_RUN_NOW);
  651. }
  652. break;
  653. case NGHTTP2_PUSH_PROMISE:
  654. rv = push_promise(data_s, conn, &frame->push_promise);
  655. if(rv) { /* deny! */
  656. int h2;
  657. DEBUGASSERT((rv > CURL_PUSH_OK) && (rv <= CURL_PUSH_ERROROUT));
  658. h2 = nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
  659. frame->push_promise.promised_stream_id,
  660. NGHTTP2_CANCEL);
  661. if(nghttp2_is_fatal(h2))
  662. return NGHTTP2_ERR_CALLBACK_FAILURE;
  663. else if(rv == CURL_PUSH_ERROROUT) {
  664. DEBUGF(infof(data_s, "Fail the parent stream (too)\n"));
  665. return NGHTTP2_ERR_CALLBACK_FAILURE;
  666. }
  667. }
  668. break;
  669. default:
  670. H2BUGF(infof(data_s, "Got frame type %x for stream %u!\n",
  671. frame->hd.type, stream_id));
  672. break;
  673. }
  674. return 0;
  675. }
  676. static int on_data_chunk_recv(nghttp2_session *session, uint8_t flags,
  677. int32_t stream_id,
  678. const uint8_t *data, size_t len, void *userp)
  679. {
  680. struct HTTP *stream;
  681. struct Curl_easy *data_s;
  682. size_t nread;
  683. struct connectdata *conn = (struct connectdata *)userp;
  684. (void)session;
  685. (void)flags;
  686. (void)data;
  687. DEBUGASSERT(stream_id); /* should never be a zero stream ID here */
  688. /* get the stream from the hash based on Stream ID */
  689. data_s = nghttp2_session_get_stream_user_data(session, stream_id);
  690. if(!data_s)
  691. /* Receiving a Stream ID not in the hash should not happen, this is an
  692. internal error more than anything else! */
  693. return NGHTTP2_ERR_CALLBACK_FAILURE;
  694. stream = data_s->req.protop;
  695. if(!stream)
  696. return NGHTTP2_ERR_CALLBACK_FAILURE;
  697. nread = CURLMIN(stream->len, len);
  698. memcpy(&stream->mem[stream->memlen], data, nread);
  699. stream->len -= nread;
  700. stream->memlen += nread;
  701. drain_this(data_s, &conn->proto.httpc);
  702. /* if we receive data for another handle, wake that up */
  703. if(conn->data != data_s)
  704. Curl_expire(data_s, 0, EXPIRE_RUN_NOW);
  705. H2BUGF(infof(data_s, "%zu data received for stream %u "
  706. "(%zu left in buffer %p, total %zu)\n",
  707. nread, stream_id,
  708. stream->len, stream->mem,
  709. stream->memlen));
  710. if(nread < len) {
  711. stream->pausedata = data + nread;
  712. stream->pauselen = len - nread;
  713. H2BUGF(infof(data_s, "NGHTTP2_ERR_PAUSE - %zu bytes out of buffer"
  714. ", stream %u\n",
  715. len - nread, stream_id));
  716. data_s->conn->proto.httpc.pause_stream_id = stream_id;
  717. return NGHTTP2_ERR_PAUSE;
  718. }
  719. /* pause execution of nghttp2 if we received data for another handle
  720. in order to process them first. */
  721. if(conn->data != data_s) {
  722. data_s->conn->proto.httpc.pause_stream_id = stream_id;
  723. return NGHTTP2_ERR_PAUSE;
  724. }
  725. return 0;
  726. }
  727. static int on_stream_close(nghttp2_session *session, int32_t stream_id,
  728. uint32_t error_code, void *userp)
  729. {
  730. struct Curl_easy *data_s;
  731. struct HTTP *stream;
  732. struct connectdata *conn = (struct connectdata *)userp;
  733. int rv;
  734. (void)session;
  735. (void)stream_id;
  736. if(stream_id) {
  737. struct http_conn *httpc;
  738. /* get the stream from the hash based on Stream ID, stream ID zero is for
  739. connection-oriented stuff */
  740. data_s = nghttp2_session_get_stream_user_data(session, stream_id);
  741. if(!data_s) {
  742. /* We could get stream ID not in the hash. For example, if we
  743. decided to reject stream (e.g., PUSH_PROMISE). */
  744. return 0;
  745. }
  746. H2BUGF(infof(data_s, "on_stream_close(), %s (err %d), stream %u\n",
  747. nghttp2_http2_strerror(error_code), error_code, stream_id));
  748. stream = data_s->req.protop;
  749. if(!stream)
  750. return NGHTTP2_ERR_CALLBACK_FAILURE;
  751. stream->closed = TRUE;
  752. httpc = &conn->proto.httpc;
  753. drain_this(data_s, httpc);
  754. Curl_expire(data_s, 0, EXPIRE_RUN_NOW);
  755. httpc->error_code = error_code;
  756. /* remove the entry from the hash as the stream is now gone */
  757. rv = nghttp2_session_set_stream_user_data(session, stream_id, 0);
  758. if(rv) {
  759. infof(data_s, "http/2: failed to clear user_data for stream %d!\n",
  760. stream_id);
  761. DEBUGASSERT(0);
  762. }
  763. if(stream_id == httpc->pause_stream_id) {
  764. H2BUGF(infof(data_s, "Stopped the pause stream!\n"));
  765. httpc->pause_stream_id = 0;
  766. }
  767. H2BUGF(infof(data_s, "Removed stream %u hash!\n", stream_id));
  768. stream->stream_id = 0; /* cleared */
  769. }
  770. return 0;
  771. }
  772. static int on_begin_headers(nghttp2_session *session,
  773. const nghttp2_frame *frame, void *userp)
  774. {
  775. struct HTTP *stream;
  776. struct Curl_easy *data_s = NULL;
  777. (void)userp;
  778. data_s = nghttp2_session_get_stream_user_data(session, frame->hd.stream_id);
  779. if(!data_s) {
  780. return 0;
  781. }
  782. H2BUGF(infof(data_s, "on_begin_headers() was called\n"));
  783. if(frame->hd.type != NGHTTP2_HEADERS) {
  784. return 0;
  785. }
  786. stream = data_s->req.protop;
  787. if(!stream || !stream->bodystarted) {
  788. return 0;
  789. }
  790. return 0;
  791. }
  792. /* Decode HTTP status code. Returns -1 if no valid status code was
  793. decoded. */
  794. static int decode_status_code(const uint8_t *value, size_t len)
  795. {
  796. int i;
  797. int res;
  798. if(len != 3) {
  799. return -1;
  800. }
  801. res = 0;
  802. for(i = 0; i < 3; ++i) {
  803. char c = value[i];
  804. if(c < '0' || c > '9') {
  805. return -1;
  806. }
  807. res *= 10;
  808. res += c - '0';
  809. }
  810. return res;
  811. }
  812. /* frame->hd.type is either NGHTTP2_HEADERS or NGHTTP2_PUSH_PROMISE */
  813. static int on_header(nghttp2_session *session, const nghttp2_frame *frame,
  814. const uint8_t *name, size_t namelen,
  815. const uint8_t *value, size_t valuelen,
  816. uint8_t flags,
  817. void *userp)
  818. {
  819. struct HTTP *stream;
  820. struct Curl_easy *data_s;
  821. int32_t stream_id = frame->hd.stream_id;
  822. struct connectdata *conn = (struct connectdata *)userp;
  823. CURLcode result;
  824. (void)flags;
  825. DEBUGASSERT(stream_id); /* should never be a zero stream ID here */
  826. /* get the stream from the hash based on Stream ID */
  827. data_s = nghttp2_session_get_stream_user_data(session, stream_id);
  828. if(!data_s)
  829. /* Receiving a Stream ID not in the hash should not happen, this is an
  830. internal error more than anything else! */
  831. return NGHTTP2_ERR_CALLBACK_FAILURE;
  832. stream = data_s->req.protop;
  833. if(!stream) {
  834. failf(data_s, "Internal NULL stream! 5\n");
  835. return NGHTTP2_ERR_CALLBACK_FAILURE;
  836. }
  837. /* Store received PUSH_PROMISE headers to be used when the subsequent
  838. PUSH_PROMISE callback comes */
  839. if(frame->hd.type == NGHTTP2_PUSH_PROMISE) {
  840. char *h;
  841. if(!strcmp(":authority", (const char *)name)) {
  842. /* pseudo headers are lower case */
  843. int rc = 0;
  844. char *check = aprintf("%s:%d", conn->host.name, conn->remote_port);
  845. if(!check)
  846. /* no memory */
  847. return NGHTTP2_ERR_CALLBACK_FAILURE;
  848. if(!Curl_strcasecompare(check, (const char *)value) &&
  849. ((conn->remote_port != conn->given->defport) ||
  850. !Curl_strcasecompare(conn->host.name, (const char *)value))) {
  851. /* This is push is not for the same authority that was asked for in
  852. * the URL. RFC 7540 section 8.2 says: "A client MUST treat a
  853. * PUSH_PROMISE for which the server is not authoritative as a stream
  854. * error of type PROTOCOL_ERROR."
  855. */
  856. (void)nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
  857. stream_id, NGHTTP2_PROTOCOL_ERROR);
  858. rc = NGHTTP2_ERR_CALLBACK_FAILURE;
  859. }
  860. free(check);
  861. if(rc)
  862. return rc;
  863. }
  864. if(!stream->push_headers) {
  865. stream->push_headers_alloc = 10;
  866. stream->push_headers = malloc(stream->push_headers_alloc *
  867. sizeof(char *));
  868. if(!stream->push_headers)
  869. return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
  870. stream->push_headers_used = 0;
  871. }
  872. else if(stream->push_headers_used ==
  873. stream->push_headers_alloc) {
  874. char **headp;
  875. stream->push_headers_alloc *= 2;
  876. headp = Curl_saferealloc(stream->push_headers,
  877. stream->push_headers_alloc * sizeof(char *));
  878. if(!headp) {
  879. stream->push_headers = NULL;
  880. return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
  881. }
  882. stream->push_headers = headp;
  883. }
  884. h = aprintf("%s:%s", name, value);
  885. if(h)
  886. stream->push_headers[stream->push_headers_used++] = h;
  887. return 0;
  888. }
  889. if(stream->bodystarted) {
  890. /* This is a trailer */
  891. H2BUGF(infof(data_s, "h2 trailer: %.*s: %.*s\n", namelen, name, valuelen,
  892. value));
  893. result = Curl_dyn_addf(&stream->trailer_recvbuf,
  894. "%.*s: %.*s\r\n", namelen, name,
  895. valuelen, value);
  896. if(result)
  897. return NGHTTP2_ERR_CALLBACK_FAILURE;
  898. return 0;
  899. }
  900. if(namelen == sizeof(":status") - 1 &&
  901. memcmp(":status", name, namelen) == 0) {
  902. /* nghttp2 guarantees :status is received first and only once, and
  903. value is 3 digits status code, and decode_status_code always
  904. succeeds. */
  905. stream->status_code = decode_status_code(value, valuelen);
  906. DEBUGASSERT(stream->status_code != -1);
  907. result = Curl_dyn_add(&stream->header_recvbuf, "HTTP/2 ");
  908. if(result)
  909. return NGHTTP2_ERR_CALLBACK_FAILURE;
  910. result = Curl_dyn_addn(&stream->header_recvbuf, value, valuelen);
  911. if(result)
  912. return NGHTTP2_ERR_CALLBACK_FAILURE;
  913. /* the space character after the status code is mandatory */
  914. result = Curl_dyn_add(&stream->header_recvbuf, " \r\n");
  915. if(result)
  916. return NGHTTP2_ERR_CALLBACK_FAILURE;
  917. /* if we receive data for another handle, wake that up */
  918. if(conn->data != data_s)
  919. Curl_expire(data_s, 0, EXPIRE_RUN_NOW);
  920. H2BUGF(infof(data_s, "h2 status: HTTP/2 %03d (easy %p)\n",
  921. stream->status_code, data_s));
  922. return 0;
  923. }
  924. /* nghttp2 guarantees that namelen > 0, and :status was already
  925. received, and this is not pseudo-header field . */
  926. /* convert to a HTTP1-style header */
  927. result = Curl_dyn_addn(&stream->header_recvbuf, name, namelen);
  928. if(result)
  929. return NGHTTP2_ERR_CALLBACK_FAILURE;
  930. result = Curl_dyn_add(&stream->header_recvbuf, ": ");
  931. if(result)
  932. return NGHTTP2_ERR_CALLBACK_FAILURE;
  933. result = Curl_dyn_addn(&stream->header_recvbuf, value, valuelen);
  934. if(result)
  935. return NGHTTP2_ERR_CALLBACK_FAILURE;
  936. result = Curl_dyn_add(&stream->header_recvbuf, "\r\n");
  937. if(result)
  938. return NGHTTP2_ERR_CALLBACK_FAILURE;
  939. /* if we receive data for another handle, wake that up */
  940. if(conn->data != data_s)
  941. Curl_expire(data_s, 0, EXPIRE_RUN_NOW);
  942. H2BUGF(infof(data_s, "h2 header: %.*s: %.*s\n", namelen, name, valuelen,
  943. value));
  944. return 0; /* 0 is successful */
  945. }
  946. static ssize_t data_source_read_callback(nghttp2_session *session,
  947. int32_t stream_id,
  948. uint8_t *buf, size_t length,
  949. uint32_t *data_flags,
  950. nghttp2_data_source *source,
  951. void *userp)
  952. {
  953. struct Curl_easy *data_s;
  954. struct HTTP *stream = NULL;
  955. size_t nread;
  956. (void)source;
  957. (void)userp;
  958. if(stream_id) {
  959. /* get the stream from the hash based on Stream ID, stream ID zero is for
  960. connection-oriented stuff */
  961. data_s = nghttp2_session_get_stream_user_data(session, stream_id);
  962. if(!data_s)
  963. /* Receiving a Stream ID not in the hash should not happen, this is an
  964. internal error more than anything else! */
  965. return NGHTTP2_ERR_CALLBACK_FAILURE;
  966. stream = data_s->req.protop;
  967. if(!stream)
  968. return NGHTTP2_ERR_CALLBACK_FAILURE;
  969. }
  970. else
  971. return NGHTTP2_ERR_INVALID_ARGUMENT;
  972. nread = CURLMIN(stream->upload_len, length);
  973. if(nread > 0) {
  974. memcpy(buf, stream->upload_mem, nread);
  975. stream->upload_mem += nread;
  976. stream->upload_len -= nread;
  977. if(data_s->state.infilesize != -1)
  978. stream->upload_left -= nread;
  979. }
  980. if(stream->upload_left == 0)
  981. *data_flags = NGHTTP2_DATA_FLAG_EOF;
  982. else if(nread == 0)
  983. return NGHTTP2_ERR_DEFERRED;
  984. H2BUGF(infof(data_s, "data_source_read_callback: "
  985. "returns %zu bytes stream %u\n",
  986. nread, stream_id));
  987. return nread;
  988. }
  989. #if !defined(CURL_DISABLE_VERBOSE_STRINGS)
  990. static int error_callback(nghttp2_session *session,
  991. const char *msg,
  992. size_t len,
  993. void *userp)
  994. {
  995. struct connectdata *conn = (struct connectdata *)userp;
  996. (void)session;
  997. infof(conn->data, "http2 error: %.*s\n", len, msg);
  998. return 0;
  999. }
  1000. #endif
  1001. static void populate_settings(struct connectdata *conn,
  1002. struct http_conn *httpc)
  1003. {
  1004. nghttp2_settings_entry *iv = httpc->local_settings;
  1005. DEBUGASSERT(conn->data);
  1006. iv[0].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS;
  1007. iv[0].value = Curl_multi_max_concurrent_streams(conn->data->multi);
  1008. iv[1].settings_id = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE;
  1009. iv[1].value = HTTP2_HUGE_WINDOW_SIZE;
  1010. iv[2].settings_id = NGHTTP2_SETTINGS_ENABLE_PUSH;
  1011. iv[2].value = conn->data->multi->push_cb != NULL;
  1012. httpc->local_settings_num = 3;
  1013. }
  1014. void Curl_http2_done(struct Curl_easy *data, bool premature)
  1015. {
  1016. struct HTTP *http = data->req.protop;
  1017. struct http_conn *httpc = &data->conn->proto.httpc;
  1018. /* there might be allocated resources done before this got the 'h2' pointer
  1019. setup */
  1020. Curl_dyn_free(&http->header_recvbuf);
  1021. Curl_dyn_free(&http->trailer_recvbuf);
  1022. if(http->push_headers) {
  1023. /* if they weren't used and then freed before */
  1024. for(; http->push_headers_used > 0; --http->push_headers_used) {
  1025. free(http->push_headers[http->push_headers_used - 1]);
  1026. }
  1027. free(http->push_headers);
  1028. http->push_headers = NULL;
  1029. }
  1030. if(!(data->conn->handler->protocol&PROTO_FAMILY_HTTP) ||
  1031. !httpc->h2) /* not HTTP/2 ? */
  1032. return;
  1033. if(premature) {
  1034. /* RST_STREAM */
  1035. if(!nghttp2_submit_rst_stream(httpc->h2, NGHTTP2_FLAG_NONE,
  1036. http->stream_id, NGHTTP2_STREAM_CLOSED))
  1037. (void)nghttp2_session_send(httpc->h2);
  1038. if(http->stream_id == httpc->pause_stream_id) {
  1039. infof(data, "stopped the pause stream!\n");
  1040. httpc->pause_stream_id = 0;
  1041. }
  1042. }
  1043. if(data->state.drain)
  1044. drained_transfer(data, httpc);
  1045. /* -1 means unassigned and 0 means cleared */
  1046. if(http->stream_id > 0) {
  1047. int rv = nghttp2_session_set_stream_user_data(httpc->h2,
  1048. http->stream_id, 0);
  1049. if(rv) {
  1050. infof(data, "http/2: failed to clear user_data for stream %d!\n",
  1051. http->stream_id);
  1052. DEBUGASSERT(0);
  1053. }
  1054. http->stream_id = 0;
  1055. }
  1056. if(0 == nghttp2_session_check_request_allowed(httpc->h2)) {
  1057. /* No more requests are allowed in the current session, so the connection
  1058. may not be reused. This is set when a GOAWAY frame has been received or
  1059. when the limit of stream identifiers has been reached. */
  1060. connclose(data->conn, "http/2: No new requests allowed");
  1061. }
  1062. }
  1063. /*
  1064. * Initialize nghttp2 for a Curl connection
  1065. */
  1066. static CURLcode http2_init(struct connectdata *conn)
  1067. {
  1068. if(!conn->proto.httpc.h2) {
  1069. int rc;
  1070. nghttp2_session_callbacks *callbacks;
  1071. conn->proto.httpc.inbuf = malloc(H2_BUFSIZE);
  1072. if(conn->proto.httpc.inbuf == NULL)
  1073. return CURLE_OUT_OF_MEMORY;
  1074. rc = nghttp2_session_callbacks_new(&callbacks);
  1075. if(rc) {
  1076. failf(conn->data, "Couldn't initialize nghttp2 callbacks!");
  1077. return CURLE_OUT_OF_MEMORY; /* most likely at least */
  1078. }
  1079. /* nghttp2_send_callback */
  1080. nghttp2_session_callbacks_set_send_callback(callbacks, send_callback);
  1081. /* nghttp2_on_frame_recv_callback */
  1082. nghttp2_session_callbacks_set_on_frame_recv_callback
  1083. (callbacks, on_frame_recv);
  1084. /* nghttp2_on_data_chunk_recv_callback */
  1085. nghttp2_session_callbacks_set_on_data_chunk_recv_callback
  1086. (callbacks, on_data_chunk_recv);
  1087. /* nghttp2_on_stream_close_callback */
  1088. nghttp2_session_callbacks_set_on_stream_close_callback
  1089. (callbacks, on_stream_close);
  1090. /* nghttp2_on_begin_headers_callback */
  1091. nghttp2_session_callbacks_set_on_begin_headers_callback
  1092. (callbacks, on_begin_headers);
  1093. /* nghttp2_on_header_callback */
  1094. nghttp2_session_callbacks_set_on_header_callback(callbacks, on_header);
  1095. nghttp2_session_callbacks_set_error_callback(callbacks, error_callback);
  1096. /* The nghttp2 session is not yet setup, do it */
  1097. rc = nghttp2_session_client_new(&conn->proto.httpc.h2, callbacks, conn);
  1098. nghttp2_session_callbacks_del(callbacks);
  1099. if(rc) {
  1100. failf(conn->data, "Couldn't initialize nghttp2!");
  1101. return CURLE_OUT_OF_MEMORY; /* most likely at least */
  1102. }
  1103. }
  1104. return CURLE_OK;
  1105. }
  1106. /*
  1107. * Append headers to ask for a HTTP1.1 to HTTP2 upgrade.
  1108. */
  1109. CURLcode Curl_http2_request_upgrade(struct dynbuf *req,
  1110. struct connectdata *conn)
  1111. {
  1112. CURLcode result;
  1113. ssize_t binlen;
  1114. char *base64;
  1115. size_t blen;
  1116. struct SingleRequest *k = &conn->data->req;
  1117. uint8_t *binsettings = conn->proto.httpc.binsettings;
  1118. struct http_conn *httpc = &conn->proto.httpc;
  1119. populate_settings(conn, httpc);
  1120. /* this returns number of bytes it wrote */
  1121. binlen = nghttp2_pack_settings_payload(binsettings, H2_BINSETTINGS_LEN,
  1122. httpc->local_settings,
  1123. httpc->local_settings_num);
  1124. if(!binlen) {
  1125. failf(conn->data, "nghttp2 unexpectedly failed on pack_settings_payload");
  1126. Curl_dyn_free(req);
  1127. return CURLE_FAILED_INIT;
  1128. }
  1129. conn->proto.httpc.binlen = binlen;
  1130. result = Curl_base64url_encode(conn->data, (const char *)binsettings, binlen,
  1131. &base64, &blen);
  1132. if(result) {
  1133. Curl_dyn_free(req);
  1134. return result;
  1135. }
  1136. result = Curl_dyn_addf(req,
  1137. "Connection: Upgrade, HTTP2-Settings\r\n"
  1138. "Upgrade: %s\r\n"
  1139. "HTTP2-Settings: %s\r\n",
  1140. NGHTTP2_CLEARTEXT_PROTO_VERSION_ID, base64);
  1141. free(base64);
  1142. k->upgr101 = UPGR101_REQUESTED;
  1143. return result;
  1144. }
  1145. /*
  1146. * Returns nonzero if current HTTP/2 session should be closed.
  1147. */
  1148. static int should_close_session(struct http_conn *httpc)
  1149. {
  1150. return httpc->drain_total == 0 && !nghttp2_session_want_read(httpc->h2) &&
  1151. !nghttp2_session_want_write(httpc->h2);
  1152. }
  1153. /*
  1154. * h2_process_pending_input() processes pending input left in
  1155. * httpc->inbuf. Then, call h2_session_send() to send pending data.
  1156. * This function returns 0 if it succeeds, or -1 and error code will
  1157. * be assigned to *err.
  1158. */
  1159. static int h2_process_pending_input(struct connectdata *conn,
  1160. struct http_conn *httpc,
  1161. CURLcode *err)
  1162. {
  1163. ssize_t nread;
  1164. char *inbuf;
  1165. ssize_t rv;
  1166. struct Curl_easy *data = conn->data;
  1167. nread = httpc->inbuflen - httpc->nread_inbuf;
  1168. inbuf = httpc->inbuf + httpc->nread_inbuf;
  1169. rv = nghttp2_session_mem_recv(httpc->h2, (const uint8_t *)inbuf, nread);
  1170. if(rv < 0) {
  1171. failf(data,
  1172. "h2_process_pending_input: nghttp2_session_mem_recv() returned "
  1173. "%zd:%s\n", rv, nghttp2_strerror((int)rv));
  1174. *err = CURLE_RECV_ERROR;
  1175. return -1;
  1176. }
  1177. if(nread == rv) {
  1178. H2BUGF(infof(data,
  1179. "h2_process_pending_input: All data in connection buffer "
  1180. "processed\n"));
  1181. httpc->inbuflen = 0;
  1182. httpc->nread_inbuf = 0;
  1183. }
  1184. else {
  1185. httpc->nread_inbuf += rv;
  1186. H2BUGF(infof(data,
  1187. "h2_process_pending_input: %zu bytes left in connection "
  1188. "buffer\n",
  1189. httpc->inbuflen - httpc->nread_inbuf));
  1190. }
  1191. rv = h2_session_send(data, httpc->h2);
  1192. if(rv != 0) {
  1193. *err = CURLE_SEND_ERROR;
  1194. return -1;
  1195. }
  1196. if(should_close_session(httpc)) {
  1197. H2BUGF(infof(data,
  1198. "h2_process_pending_input: nothing to do in this session\n"));
  1199. if(httpc->error_code)
  1200. *err = CURLE_HTTP2;
  1201. else {
  1202. /* not an error per se, but should still close the connection */
  1203. connclose(conn, "GOAWAY received");
  1204. *err = CURLE_OK;
  1205. }
  1206. return -1;
  1207. }
  1208. return 0;
  1209. }
  1210. /*
  1211. * Called from transfer.c:done_sending when we stop uploading.
  1212. */
  1213. CURLcode Curl_http2_done_sending(struct connectdata *conn)
  1214. {
  1215. CURLcode result = CURLE_OK;
  1216. if((conn->handler == &Curl_handler_http2_ssl) ||
  1217. (conn->handler == &Curl_handler_http2)) {
  1218. /* make sure this is only attempted for HTTP/2 transfers */
  1219. struct HTTP *stream = conn->data->req.protop;
  1220. struct http_conn *httpc = &conn->proto.httpc;
  1221. nghttp2_session *h2 = httpc->h2;
  1222. if(stream->upload_left) {
  1223. /* If the stream still thinks there's data left to upload. */
  1224. stream->upload_left = 0; /* DONE! */
  1225. /* resume sending here to trigger the callback to get called again so
  1226. that it can signal EOF to nghttp2 */
  1227. (void)nghttp2_session_resume_data(h2, stream->stream_id);
  1228. (void)h2_process_pending_input(conn, httpc, &result);
  1229. }
  1230. /* If nghttp2 still has pending frames unsent */
  1231. if(nghttp2_session_want_write(h2)) {
  1232. struct Curl_easy *data = conn->data;
  1233. struct SingleRequest *k = &data->req;
  1234. int rv;
  1235. H2BUGF(infof(data, "HTTP/2 still wants to send data (easy %p)\n", data));
  1236. /* re-set KEEP_SEND to make sure we are called again */
  1237. k->keepon |= KEEP_SEND;
  1238. /* and attempt to send the pending frames */
  1239. rv = h2_session_send(data, h2);
  1240. if(rv != 0)
  1241. result = CURLE_SEND_ERROR;
  1242. }
  1243. }
  1244. return result;
  1245. }
  1246. static ssize_t http2_handle_stream_close(struct connectdata *conn,
  1247. struct Curl_easy *data,
  1248. struct HTTP *stream, CURLcode *err)
  1249. {
  1250. struct http_conn *httpc = &conn->proto.httpc;
  1251. if(httpc->pause_stream_id == stream->stream_id) {
  1252. httpc->pause_stream_id = 0;
  1253. }
  1254. drained_transfer(data, httpc);
  1255. if(httpc->pause_stream_id == 0) {
  1256. if(h2_process_pending_input(conn, httpc, err) != 0) {
  1257. return -1;
  1258. }
  1259. }
  1260. DEBUGASSERT(data->state.drain == 0);
  1261. /* Reset to FALSE to prevent infinite loop in readwrite_data function. */
  1262. stream->closed = FALSE;
  1263. if(httpc->error_code == NGHTTP2_REFUSED_STREAM) {
  1264. H2BUGF(infof(data, "REFUSED_STREAM (%d), try again on a new connection!\n",
  1265. stream->stream_id));
  1266. connclose(conn, "REFUSED_STREAM"); /* don't use this anymore */
  1267. data->state.refused_stream = TRUE;
  1268. *err = CURLE_RECV_ERROR; /* trigger Curl_retry_request() later */
  1269. return -1;
  1270. }
  1271. else if(httpc->error_code != NGHTTP2_NO_ERROR) {
  1272. failf(data, "HTTP/2 stream %d was not closed cleanly: %s (err %u)",
  1273. stream->stream_id, nghttp2_http2_strerror(httpc->error_code),
  1274. httpc->error_code);
  1275. *err = CURLE_HTTP2_STREAM;
  1276. return -1;
  1277. }
  1278. if(!stream->bodystarted) {
  1279. failf(data, "HTTP/2 stream %d was closed cleanly, but before getting "
  1280. " all response header fields, treated as error",
  1281. stream->stream_id);
  1282. *err = CURLE_HTTP2_STREAM;
  1283. return -1;
  1284. }
  1285. if(Curl_dyn_len(&stream->trailer_recvbuf)) {
  1286. char *trailp = Curl_dyn_ptr(&stream->trailer_recvbuf);
  1287. char *lf;
  1288. do {
  1289. size_t len = 0;
  1290. CURLcode result;
  1291. /* each trailer line ends with a newline */
  1292. lf = strchr(trailp, '\n');
  1293. if(!lf)
  1294. break;
  1295. len = lf + 1 - trailp;
  1296. if(data->set.verbose)
  1297. Curl_debug(data, CURLINFO_HEADER_IN, trailp, len);
  1298. /* pass the trailers one by one to the callback */
  1299. result = Curl_client_write(conn, CLIENTWRITE_HEADER, trailp, len);
  1300. if(result) {
  1301. *err = result;
  1302. return -1;
  1303. }
  1304. trailp = ++lf;
  1305. } while(lf);
  1306. }
  1307. stream->close_handled = TRUE;
  1308. H2BUGF(infof(data, "http2_recv returns 0, http2_handle_stream_close\n"));
  1309. return 0;
  1310. }
  1311. /*
  1312. * h2_pri_spec() fills in the pri_spec struct, used by nghttp2 to send weight
  1313. * and dependency to the peer. It also stores the updated values in the state
  1314. * struct.
  1315. */
  1316. static void h2_pri_spec(struct Curl_easy *data,
  1317. nghttp2_priority_spec *pri_spec)
  1318. {
  1319. struct HTTP *depstream = (data->set.stream_depends_on?
  1320. data->set.stream_depends_on->req.protop:NULL);
  1321. int32_t depstream_id = depstream? depstream->stream_id:0;
  1322. nghttp2_priority_spec_init(pri_spec, depstream_id, data->set.stream_weight,
  1323. data->set.stream_depends_e);
  1324. data->state.stream_weight = data->set.stream_weight;
  1325. data->state.stream_depends_e = data->set.stream_depends_e;
  1326. data->state.stream_depends_on = data->set.stream_depends_on;
  1327. }
  1328. /*
  1329. * h2_session_send() checks if there's been an update in the priority /
  1330. * dependency settings and if so it submits a PRIORITY frame with the updated
  1331. * info.
  1332. */
  1333. static int h2_session_send(struct Curl_easy *data,
  1334. nghttp2_session *h2)
  1335. {
  1336. struct HTTP *stream = data->req.protop;
  1337. if((data->set.stream_weight != data->state.stream_weight) ||
  1338. (data->set.stream_depends_e != data->state.stream_depends_e) ||
  1339. (data->set.stream_depends_on != data->state.stream_depends_on) ) {
  1340. /* send new weight and/or dependency */
  1341. nghttp2_priority_spec pri_spec;
  1342. int rv;
  1343. h2_pri_spec(data, &pri_spec);
  1344. H2BUGF(infof(data, "Queuing PRIORITY on stream %u (easy %p)\n",
  1345. stream->stream_id, data));
  1346. DEBUGASSERT(stream->stream_id != -1);
  1347. rv = nghttp2_submit_priority(h2, NGHTTP2_FLAG_NONE, stream->stream_id,
  1348. &pri_spec);
  1349. if(rv)
  1350. return rv;
  1351. }
  1352. return nghttp2_session_send(h2);
  1353. }
  1354. static ssize_t http2_recv(struct connectdata *conn, int sockindex,
  1355. char *mem, size_t len, CURLcode *err)
  1356. {
  1357. CURLcode result = CURLE_OK;
  1358. ssize_t rv;
  1359. ssize_t nread;
  1360. struct http_conn *httpc = &conn->proto.httpc;
  1361. struct Curl_easy *data = conn->data;
  1362. struct HTTP *stream = data->req.protop;
  1363. (void)sockindex; /* we always do HTTP2 on sockindex 0 */
  1364. if(should_close_session(httpc)) {
  1365. H2BUGF(infof(data,
  1366. "http2_recv: nothing to do in this session\n"));
  1367. if(conn->bits.close) {
  1368. /* already marked for closure, return OK and we're done */
  1369. *err = CURLE_OK;
  1370. return 0;
  1371. }
  1372. *err = CURLE_HTTP2;
  1373. return -1;
  1374. }
  1375. /* Nullify here because we call nghttp2_session_send() and they
  1376. might refer to the old buffer. */
  1377. stream->upload_mem = NULL;
  1378. stream->upload_len = 0;
  1379. /*
  1380. * At this point 'stream' is just in the Curl_easy the connection
  1381. * identifies as its owner at this time.
  1382. */
  1383. if(stream->bodystarted &&
  1384. stream->nread_header_recvbuf < Curl_dyn_len(&stream->header_recvbuf)) {
  1385. /* If there is header data pending for this stream to return, do that */
  1386. size_t left =
  1387. Curl_dyn_len(&stream->header_recvbuf) - stream->nread_header_recvbuf;
  1388. size_t ncopy = CURLMIN(len, left);
  1389. memcpy(mem, Curl_dyn_ptr(&stream->header_recvbuf) +
  1390. stream->nread_header_recvbuf, ncopy);
  1391. stream->nread_header_recvbuf += ncopy;
  1392. H2BUGF(infof(data, "http2_recv: Got %d bytes from header_recvbuf\n",
  1393. (int)ncopy));
  1394. return ncopy;
  1395. }
  1396. H2BUGF(infof(data, "http2_recv: easy %p (stream %u) win %u/%u\n",
  1397. data, stream->stream_id,
  1398. nghttp2_session_get_local_window_size(httpc->h2),
  1399. nghttp2_session_get_stream_local_window_size(httpc->h2,
  1400. stream->stream_id)
  1401. ));
  1402. if((data->state.drain) && stream->memlen) {
  1403. H2BUGF(infof(data, "http2_recv: DRAIN %zu bytes stream %u!! (%p => %p)\n",
  1404. stream->memlen, stream->stream_id,
  1405. stream->mem, mem));
  1406. if(mem != stream->mem) {
  1407. /* if we didn't get the same buffer this time, we must move the data to
  1408. the beginning */
  1409. memmove(mem, stream->mem, stream->memlen);
  1410. stream->len = len - stream->memlen;
  1411. stream->mem = mem;
  1412. }
  1413. if(httpc->pause_stream_id == stream->stream_id && !stream->pausedata) {
  1414. /* We have paused nghttp2, but we have no pause data (see
  1415. on_data_chunk_recv). */
  1416. httpc->pause_stream_id = 0;
  1417. if(h2_process_pending_input(conn, httpc, &result) != 0) {
  1418. *err = result;
  1419. return -1;
  1420. }
  1421. }
  1422. }
  1423. else if(stream->pausedata) {
  1424. DEBUGASSERT(httpc->pause_stream_id == stream->stream_id);
  1425. nread = CURLMIN(len, stream->pauselen);
  1426. memcpy(mem, stream->pausedata, nread);
  1427. stream->pausedata += nread;
  1428. stream->pauselen -= nread;
  1429. if(stream->pauselen == 0) {
  1430. H2BUGF(infof(data, "Unpaused by stream %u\n", stream->stream_id));
  1431. DEBUGASSERT(httpc->pause_stream_id == stream->stream_id);
  1432. httpc->pause_stream_id = 0;
  1433. stream->pausedata = NULL;
  1434. stream->pauselen = 0;
  1435. /* When NGHTTP2_ERR_PAUSE is returned from
  1436. data_source_read_callback, we might not process DATA frame
  1437. fully. Calling nghttp2_session_mem_recv() again will
  1438. continue to process DATA frame, but if there is no incoming
  1439. frames, then we have to call it again with 0-length data.
  1440. Without this, on_stream_close callback will not be called,
  1441. and stream could be hanged. */
  1442. if(h2_process_pending_input(conn, httpc, &result) != 0) {
  1443. *err = result;
  1444. return -1;
  1445. }
  1446. }
  1447. H2BUGF(infof(data, "http2_recv: returns unpaused %zd bytes on stream %u\n",
  1448. nread, stream->stream_id));
  1449. return nread;
  1450. }
  1451. else if(httpc->pause_stream_id) {
  1452. /* If a stream paused nghttp2_session_mem_recv previously, and has
  1453. not processed all data, it still refers to the buffer in
  1454. nghttp2_session. If we call nghttp2_session_mem_recv(), we may
  1455. overwrite that buffer. To avoid that situation, just return
  1456. here with CURLE_AGAIN. This could be busy loop since data in
  1457. socket is not read. But it seems that usually streams are
  1458. notified with its drain property, and socket is read again
  1459. quickly. */
  1460. if(stream->closed)
  1461. /* closed overrides paused */
  1462. return 0;
  1463. H2BUGF(infof(data, "stream %x is paused, pause id: %x\n",
  1464. stream->stream_id, httpc->pause_stream_id));
  1465. *err = CURLE_AGAIN;
  1466. return -1;
  1467. }
  1468. else {
  1469. char *inbuf;
  1470. /* remember where to store incoming data for this stream and how big the
  1471. buffer is */
  1472. stream->mem = mem;
  1473. stream->len = len;
  1474. stream->memlen = 0;
  1475. if(httpc->inbuflen == 0) {
  1476. nread = ((Curl_recv *)httpc->recv_underlying)(
  1477. conn, FIRSTSOCKET, httpc->inbuf, H2_BUFSIZE, &result);
  1478. if(nread == -1) {
  1479. if(result != CURLE_AGAIN)
  1480. failf(data, "Failed receiving HTTP2 data");
  1481. else if(stream->closed)
  1482. /* received when the stream was already closed! */
  1483. return http2_handle_stream_close(conn, data, stream, err);
  1484. *err = result;
  1485. return -1;
  1486. }
  1487. if(nread == 0) {
  1488. H2BUGF(infof(data, "end of stream\n"));
  1489. *err = CURLE_OK;
  1490. return 0;
  1491. }
  1492. H2BUGF(infof(data, "nread=%zd\n", nread));
  1493. httpc->inbuflen = nread;
  1494. inbuf = httpc->inbuf;
  1495. }
  1496. else {
  1497. nread = httpc->inbuflen - httpc->nread_inbuf;
  1498. inbuf = httpc->inbuf + httpc->nread_inbuf;
  1499. H2BUGF(infof(data, "Use data left in connection buffer, nread=%zd\n",
  1500. nread));
  1501. }
  1502. rv = nghttp2_session_mem_recv(httpc->h2, (const uint8_t *)inbuf, nread);
  1503. if(nghttp2_is_fatal((int)rv)) {
  1504. failf(data, "nghttp2_session_mem_recv() returned %zd:%s\n",
  1505. rv, nghttp2_strerror((int)rv));
  1506. *err = CURLE_RECV_ERROR;
  1507. return -1;
  1508. }
  1509. H2BUGF(infof(data, "nghttp2_session_mem_recv() returns %zd\n", rv));
  1510. if(nread == rv) {
  1511. H2BUGF(infof(data, "All data in connection buffer processed\n"));
  1512. httpc->inbuflen = 0;
  1513. httpc->nread_inbuf = 0;
  1514. }
  1515. else {
  1516. httpc->nread_inbuf += rv;
  1517. H2BUGF(infof(data, "%zu bytes left in connection buffer\n",
  1518. httpc->inbuflen - httpc->nread_inbuf));
  1519. }
  1520. /* Always send pending frames in nghttp2 session, because
  1521. nghttp2_session_mem_recv() may queue new frame */
  1522. rv = h2_session_send(data, httpc->h2);
  1523. if(rv != 0) {
  1524. *err = CURLE_SEND_ERROR;
  1525. return -1;
  1526. }
  1527. if(should_close_session(httpc)) {
  1528. H2BUGF(infof(data, "http2_recv: nothing to do in this session\n"));
  1529. *err = CURLE_HTTP2;
  1530. return -1;
  1531. }
  1532. }
  1533. if(stream->memlen) {
  1534. ssize_t retlen = stream->memlen;
  1535. H2BUGF(infof(data, "http2_recv: returns %zd for stream %u\n",
  1536. retlen, stream->stream_id));
  1537. stream->memlen = 0;
  1538. if(httpc->pause_stream_id == stream->stream_id) {
  1539. /* data for this stream is returned now, but this stream caused a pause
  1540. already so we need it called again asap */
  1541. H2BUGF(infof(data, "Data returned for PAUSED stream %u\n",
  1542. stream->stream_id));
  1543. }
  1544. else if(!stream->closed) {
  1545. drained_transfer(data, httpc);
  1546. }
  1547. else
  1548. /* this stream is closed, trigger a another read ASAP to detect that */
  1549. Curl_expire(data, 0, EXPIRE_RUN_NOW);
  1550. return retlen;
  1551. }
  1552. if(stream->closed)
  1553. return 0;
  1554. *err = CURLE_AGAIN;
  1555. H2BUGF(infof(data, "http2_recv returns AGAIN for stream %u\n",
  1556. stream->stream_id));
  1557. return -1;
  1558. }
  1559. /* Index where :authority header field will appear in request header
  1560. field list. */
  1561. #define AUTHORITY_DST_IDX 3
  1562. /* USHRT_MAX is 65535 == 0xffff */
  1563. #define HEADER_OVERFLOW(x) \
  1564. (x.namelen > 0xffff || x.valuelen > 0xffff - x.namelen)
  1565. /*
  1566. * Check header memory for the token "trailers".
  1567. * Parse the tokens as separated by comma and surrounded by whitespace.
  1568. * Returns TRUE if found or FALSE if not.
  1569. */
  1570. static bool contains_trailers(const char *p, size_t len)
  1571. {
  1572. const char *end = p + len;
  1573. for(;;) {
  1574. for(; p != end && (*p == ' ' || *p == '\t'); ++p)
  1575. ;
  1576. if(p == end || (size_t)(end - p) < sizeof("trailers") - 1)
  1577. return FALSE;
  1578. if(strncasecompare("trailers", p, sizeof("trailers") - 1)) {
  1579. p += sizeof("trailers") - 1;
  1580. for(; p != end && (*p == ' ' || *p == '\t'); ++p)
  1581. ;
  1582. if(p == end || *p == ',')
  1583. return TRUE;
  1584. }
  1585. /* skip to next token */
  1586. for(; p != end && *p != ','; ++p)
  1587. ;
  1588. if(p == end)
  1589. return FALSE;
  1590. ++p;
  1591. }
  1592. }
  1593. typedef enum {
  1594. /* Send header to server */
  1595. HEADERINST_FORWARD,
  1596. /* Don't send header to server */
  1597. HEADERINST_IGNORE,
  1598. /* Discard header, and replace it with "te: trailers" */
  1599. HEADERINST_TE_TRAILERS
  1600. } header_instruction;
  1601. /* Decides how to treat given header field. */
  1602. static header_instruction inspect_header(const char *name, size_t namelen,
  1603. const char *value, size_t valuelen) {
  1604. switch(namelen) {
  1605. case 2:
  1606. if(!strncasecompare("te", name, namelen))
  1607. return HEADERINST_FORWARD;
  1608. return contains_trailers(value, valuelen) ?
  1609. HEADERINST_TE_TRAILERS : HEADERINST_IGNORE;
  1610. case 7:
  1611. return strncasecompare("upgrade", name, namelen) ?
  1612. HEADERINST_IGNORE : HEADERINST_FORWARD;
  1613. case 10:
  1614. return (strncasecompare("connection", name, namelen) ||
  1615. strncasecompare("keep-alive", name, namelen)) ?
  1616. HEADERINST_IGNORE : HEADERINST_FORWARD;
  1617. case 16:
  1618. return strncasecompare("proxy-connection", name, namelen) ?
  1619. HEADERINST_IGNORE : HEADERINST_FORWARD;
  1620. case 17:
  1621. return strncasecompare("transfer-encoding", name, namelen) ?
  1622. HEADERINST_IGNORE : HEADERINST_FORWARD;
  1623. default:
  1624. return HEADERINST_FORWARD;
  1625. }
  1626. }
  1627. static ssize_t http2_send(struct connectdata *conn, int sockindex,
  1628. const void *mem, size_t len, CURLcode *err)
  1629. {
  1630. /*
  1631. * Currently, we send request in this function, but this function is also
  1632. * used to send request body. It would be nice to add dedicated function for
  1633. * request.
  1634. */
  1635. int rv;
  1636. struct http_conn *httpc = &conn->proto.httpc;
  1637. struct HTTP *stream = conn->data->req.protop;
  1638. nghttp2_nv *nva = NULL;
  1639. size_t nheader;
  1640. size_t i;
  1641. size_t authority_idx;
  1642. char *hdbuf = (char *)mem;
  1643. char *end, *line_end;
  1644. nghttp2_data_provider data_prd;
  1645. int32_t stream_id;
  1646. nghttp2_session *h2 = httpc->h2;
  1647. nghttp2_priority_spec pri_spec;
  1648. (void)sockindex;
  1649. H2BUGF(infof(conn->data, "http2_send len=%zu\n", len));
  1650. if(stream->stream_id != -1) {
  1651. if(stream->close_handled) {
  1652. infof(conn->data, "stream %d closed\n", stream->stream_id);
  1653. *err = CURLE_HTTP2_STREAM;
  1654. return -1;
  1655. }
  1656. else if(stream->closed) {
  1657. return http2_handle_stream_close(conn, conn->data, stream, err);
  1658. }
  1659. /* If stream_id != -1, we have dispatched request HEADERS, and now
  1660. are going to send or sending request body in DATA frame */
  1661. stream->upload_mem = mem;
  1662. stream->upload_len = len;
  1663. rv = nghttp2_session_resume_data(h2, stream->stream_id);
  1664. if(nghttp2_is_fatal(rv)) {
  1665. *err = CURLE_SEND_ERROR;
  1666. return -1;
  1667. }
  1668. rv = h2_session_send(conn->data, h2);
  1669. if(nghttp2_is_fatal(rv)) {
  1670. *err = CURLE_SEND_ERROR;
  1671. return -1;
  1672. }
  1673. len -= stream->upload_len;
  1674. /* Nullify here because we call nghttp2_session_send() and they
  1675. might refer to the old buffer. */
  1676. stream->upload_mem = NULL;
  1677. stream->upload_len = 0;
  1678. if(should_close_session(httpc)) {
  1679. H2BUGF(infof(conn->data, "http2_send: nothing to do in this session\n"));
  1680. *err = CURLE_HTTP2;
  1681. return -1;
  1682. }
  1683. if(stream->upload_left) {
  1684. /* we are sure that we have more data to send here. Calling the
  1685. following API will make nghttp2_session_want_write() return
  1686. nonzero if remote window allows it, which then libcurl checks
  1687. socket is writable or not. See http2_perform_getsock(). */
  1688. nghttp2_session_resume_data(h2, stream->stream_id);
  1689. }
  1690. H2BUGF(infof(conn->data, "http2_send returns %zu for stream %u\n", len,
  1691. stream->stream_id));
  1692. return len;
  1693. }
  1694. /* Calculate number of headers contained in [mem, mem + len) */
  1695. /* Here, we assume the curl http code generate *correct* HTTP header
  1696. field block */
  1697. nheader = 0;
  1698. for(i = 1; i < len; ++i) {
  1699. if(hdbuf[i] == '\n' && hdbuf[i - 1] == '\r') {
  1700. ++nheader;
  1701. ++i;
  1702. }
  1703. }
  1704. if(nheader < 2)
  1705. goto fail;
  1706. /* We counted additional 2 \r\n in the first and last line. We need 3
  1707. new headers: :method, :path and :scheme. Therefore we need one
  1708. more space. */
  1709. nheader += 1;
  1710. nva = malloc(sizeof(nghttp2_nv) * nheader);
  1711. if(nva == NULL) {
  1712. *err = CURLE_OUT_OF_MEMORY;
  1713. return -1;
  1714. }
  1715. /* Extract :method, :path from request line
  1716. We do line endings with CRLF so checking for CR is enough */
  1717. line_end = memchr(hdbuf, '\r', len);
  1718. if(!line_end)
  1719. goto fail;
  1720. /* Method does not contain spaces */
  1721. end = memchr(hdbuf, ' ', line_end - hdbuf);
  1722. if(!end || end == hdbuf)
  1723. goto fail;
  1724. nva[0].name = (unsigned char *)":method";
  1725. nva[0].namelen = strlen((char *)nva[0].name);
  1726. nva[0].value = (unsigned char *)hdbuf;
  1727. nva[0].valuelen = (size_t)(end - hdbuf);
  1728. nva[0].flags = NGHTTP2_NV_FLAG_NONE;
  1729. if(HEADER_OVERFLOW(nva[0])) {
  1730. failf(conn->data, "Failed sending HTTP request: Header overflow");
  1731. goto fail;
  1732. }
  1733. hdbuf = end + 1;
  1734. /* Path may contain spaces so scan backwards */
  1735. end = NULL;
  1736. for(i = (size_t)(line_end - hdbuf); i; --i) {
  1737. if(hdbuf[i - 1] == ' ') {
  1738. end = &hdbuf[i - 1];
  1739. break;
  1740. }
  1741. }
  1742. if(!end || end == hdbuf)
  1743. goto fail;
  1744. nva[1].name = (unsigned char *)":path";
  1745. nva[1].namelen = strlen((char *)nva[1].name);
  1746. nva[1].value = (unsigned char *)hdbuf;
  1747. nva[1].valuelen = (size_t)(end - hdbuf);
  1748. nva[1].flags = NGHTTP2_NV_FLAG_NONE;
  1749. if(HEADER_OVERFLOW(nva[1])) {
  1750. failf(conn->data, "Failed sending HTTP request: Header overflow");
  1751. goto fail;
  1752. }
  1753. nva[2].name = (unsigned char *)":scheme";
  1754. nva[2].namelen = strlen((char *)nva[2].name);
  1755. if(conn->handler->flags & PROTOPT_SSL)
  1756. nva[2].value = (unsigned char *)"https";
  1757. else
  1758. nva[2].value = (unsigned char *)"http";
  1759. nva[2].valuelen = strlen((char *)nva[2].value);
  1760. nva[2].flags = NGHTTP2_NV_FLAG_NONE;
  1761. if(HEADER_OVERFLOW(nva[2])) {
  1762. failf(conn->data, "Failed sending HTTP request: Header overflow");
  1763. goto fail;
  1764. }
  1765. authority_idx = 0;
  1766. i = 3;
  1767. while(i < nheader) {
  1768. size_t hlen;
  1769. hdbuf = line_end + 2;
  1770. /* check for next CR, but only within the piece of data left in the given
  1771. buffer */
  1772. line_end = memchr(hdbuf, '\r', len - (hdbuf - (char *)mem));
  1773. if(!line_end || (line_end == hdbuf))
  1774. goto fail;
  1775. /* header continuation lines are not supported */
  1776. if(*hdbuf == ' ' || *hdbuf == '\t')
  1777. goto fail;
  1778. for(end = hdbuf; end < line_end && *end != ':'; ++end)
  1779. ;
  1780. if(end == hdbuf || end == line_end)
  1781. goto fail;
  1782. hlen = end - hdbuf;
  1783. if(hlen == 4 && strncasecompare("host", hdbuf, 4)) {
  1784. authority_idx = i;
  1785. nva[i].name = (unsigned char *)":authority";
  1786. nva[i].namelen = strlen((char *)nva[i].name);
  1787. }
  1788. else {
  1789. nva[i].namelen = (size_t)(end - hdbuf);
  1790. /* Lower case the header name for HTTP/2 */
  1791. Curl_strntolower((char *)hdbuf, hdbuf, nva[i].namelen);
  1792. nva[i].name = (unsigned char *)hdbuf;
  1793. }
  1794. hdbuf = end + 1;
  1795. while(*hdbuf == ' ' || *hdbuf == '\t')
  1796. ++hdbuf;
  1797. end = line_end;
  1798. switch(inspect_header((const char *)nva[i].name, nva[i].namelen, hdbuf,
  1799. end - hdbuf)) {
  1800. case HEADERINST_IGNORE:
  1801. /* skip header fields prohibited by HTTP/2 specification. */
  1802. --nheader;
  1803. continue;
  1804. case HEADERINST_TE_TRAILERS:
  1805. nva[i].value = (uint8_t*)"trailers";
  1806. nva[i].valuelen = sizeof("trailers") - 1;
  1807. break;
  1808. default:
  1809. nva[i].value = (unsigned char *)hdbuf;
  1810. nva[i].valuelen = (size_t)(end - hdbuf);
  1811. }
  1812. nva[i].flags = NGHTTP2_NV_FLAG_NONE;
  1813. if(HEADER_OVERFLOW(nva[i])) {
  1814. failf(conn->data, "Failed sending HTTP request: Header overflow");
  1815. goto fail;
  1816. }
  1817. ++i;
  1818. }
  1819. /* :authority must come before non-pseudo header fields */
  1820. if(authority_idx != 0 && authority_idx != AUTHORITY_DST_IDX) {
  1821. nghttp2_nv authority = nva[authority_idx];
  1822. for(i = authority_idx; i > AUTHORITY_DST_IDX; --i) {
  1823. nva[i] = nva[i - 1];
  1824. }
  1825. nva[i] = authority;
  1826. }
  1827. /* Warn stream may be rejected if cumulative length of headers is too large.
  1828. It appears nghttp2 will not send a header frame larger than 64KB. */
  1829. #define MAX_ACC 60000 /* <64KB to account for some overhead */
  1830. {
  1831. size_t acc = 0;
  1832. for(i = 0; i < nheader; ++i) {
  1833. acc += nva[i].namelen + nva[i].valuelen;
  1834. H2BUGF(infof(conn->data, "h2 header: %.*s:%.*s\n",
  1835. nva[i].namelen, nva[i].name,
  1836. nva[i].valuelen, nva[i].value));
  1837. }
  1838. if(acc > MAX_ACC) {
  1839. infof(conn->data, "http2_send: Warning: The cumulative length of all "
  1840. "headers exceeds %zu bytes and that could cause the "
  1841. "stream to be rejected.\n", MAX_ACC);
  1842. }
  1843. }
  1844. h2_pri_spec(conn->data, &pri_spec);
  1845. H2BUGF(infof(conn->data, "http2_send request allowed %d (easy handle %p)\n",
  1846. nghttp2_session_check_request_allowed(h2), (void *)conn->data));
  1847. switch(conn->data->state.httpreq) {
  1848. case HTTPREQ_POST:
  1849. case HTTPREQ_POST_FORM:
  1850. case HTTPREQ_POST_MIME:
  1851. case HTTPREQ_PUT:
  1852. if(conn->data->state.infilesize != -1)
  1853. stream->upload_left = conn->data->state.infilesize;
  1854. else
  1855. /* data sending without specifying the data amount up front */
  1856. stream->upload_left = -1; /* unknown, but not zero */
  1857. data_prd.read_callback = data_source_read_callback;
  1858. data_prd.source.ptr = NULL;
  1859. stream_id = nghttp2_submit_request(h2, &pri_spec, nva, nheader,
  1860. &data_prd, conn->data);
  1861. break;
  1862. default:
  1863. stream_id = nghttp2_submit_request(h2, &pri_spec, nva, nheader,
  1864. NULL, conn->data);
  1865. }
  1866. Curl_safefree(nva);
  1867. if(stream_id < 0) {
  1868. H2BUGF(infof(conn->data, "http2_send() send error\n"));
  1869. *err = CURLE_SEND_ERROR;
  1870. return -1;
  1871. }
  1872. infof(conn->data, "Using Stream ID: %x (easy handle %p)\n",
  1873. stream_id, (void *)conn->data);
  1874. stream->stream_id = stream_id;
  1875. /* this does not call h2_session_send() since there can not have been any
  1876. * priority upodate since the nghttp2_submit_request() call above */
  1877. rv = nghttp2_session_send(h2);
  1878. if(rv != 0) {
  1879. *err = CURLE_SEND_ERROR;
  1880. return -1;
  1881. }
  1882. if(should_close_session(httpc)) {
  1883. H2BUGF(infof(conn->data, "http2_send: nothing to do in this session\n"));
  1884. *err = CURLE_HTTP2;
  1885. return -1;
  1886. }
  1887. /* If whole HEADERS frame was sent off to the underlying socket, the nghttp2
  1888. library calls data_source_read_callback. But only it found that no data
  1889. available, so it deferred the DATA transmission. Which means that
  1890. nghttp2_session_want_write() returns 0 on http2_perform_getsock(), which
  1891. results that no writable socket check is performed. To workaround this,
  1892. we issue nghttp2_session_resume_data() here to bring back DATA
  1893. transmission from deferred state. */
  1894. nghttp2_session_resume_data(h2, stream->stream_id);
  1895. return len;
  1896. fail:
  1897. free(nva);
  1898. *err = CURLE_SEND_ERROR;
  1899. return -1;
  1900. }
  1901. CURLcode Curl_http2_setup(struct connectdata *conn)
  1902. {
  1903. CURLcode result;
  1904. struct http_conn *httpc = &conn->proto.httpc;
  1905. struct HTTP *stream = conn->data->req.protop;
  1906. DEBUGASSERT(conn->data->state.buffer);
  1907. stream->stream_id = -1;
  1908. Curl_dyn_init(&stream->header_recvbuf, DYN_H2_HEADERS);
  1909. Curl_dyn_init(&stream->trailer_recvbuf, DYN_H2_TRAILERS);
  1910. if((conn->handler == &Curl_handler_http2_ssl) ||
  1911. (conn->handler == &Curl_handler_http2))
  1912. return CURLE_OK; /* already done */
  1913. if(conn->handler->flags & PROTOPT_SSL)
  1914. conn->handler = &Curl_handler_http2_ssl;
  1915. else
  1916. conn->handler = &Curl_handler_http2;
  1917. result = http2_init(conn);
  1918. if(result) {
  1919. Curl_dyn_free(&stream->header_recvbuf);
  1920. return result;
  1921. }
  1922. infof(conn->data, "Using HTTP2, server supports multi-use\n");
  1923. stream->upload_left = 0;
  1924. stream->upload_mem = NULL;
  1925. stream->upload_len = 0;
  1926. stream->mem = conn->data->state.buffer;
  1927. stream->len = conn->data->set.buffer_size;
  1928. httpc->inbuflen = 0;
  1929. httpc->nread_inbuf = 0;
  1930. httpc->pause_stream_id = 0;
  1931. httpc->drain_total = 0;
  1932. conn->bits.multiplex = TRUE; /* at least potentially multiplexed */
  1933. conn->httpversion = 20;
  1934. conn->bundle->multiuse = BUNDLE_MULTIPLEX;
  1935. infof(conn->data, "Connection state changed (HTTP/2 confirmed)\n");
  1936. multi_connchanged(conn->data->multi);
  1937. return CURLE_OK;
  1938. }
  1939. CURLcode Curl_http2_switched(struct connectdata *conn,
  1940. const char *mem, size_t nread)
  1941. {
  1942. CURLcode result;
  1943. struct http_conn *httpc = &conn->proto.httpc;
  1944. int rv;
  1945. ssize_t nproc;
  1946. struct Curl_easy *data = conn->data;
  1947. struct HTTP *stream = conn->data->req.protop;
  1948. result = Curl_http2_setup(conn);
  1949. if(result)
  1950. return result;
  1951. httpc->recv_underlying = conn->recv[FIRSTSOCKET];
  1952. httpc->send_underlying = conn->send[FIRSTSOCKET];
  1953. conn->recv[FIRSTSOCKET] = http2_recv;
  1954. conn->send[FIRSTSOCKET] = http2_send;
  1955. if(conn->data->req.upgr101 == UPGR101_RECEIVED) {
  1956. /* stream 1 is opened implicitly on upgrade */
  1957. stream->stream_id = 1;
  1958. /* queue SETTINGS frame (again) */
  1959. rv = nghttp2_session_upgrade(httpc->h2, httpc->binsettings,
  1960. httpc->binlen, NULL);
  1961. if(rv != 0) {
  1962. failf(data, "nghttp2_session_upgrade() failed: %s(%d)",
  1963. nghttp2_strerror(rv), rv);
  1964. return CURLE_HTTP2;
  1965. }
  1966. rv = nghttp2_session_set_stream_user_data(httpc->h2,
  1967. stream->stream_id,
  1968. data);
  1969. if(rv) {
  1970. infof(data, "http/2: failed to set user_data for stream %d!\n",
  1971. stream->stream_id);
  1972. DEBUGASSERT(0);
  1973. }
  1974. }
  1975. else {
  1976. populate_settings(conn, httpc);
  1977. /* stream ID is unknown at this point */
  1978. stream->stream_id = -1;
  1979. rv = nghttp2_submit_settings(httpc->h2, NGHTTP2_FLAG_NONE,
  1980. httpc->local_settings,
  1981. httpc->local_settings_num);
  1982. if(rv != 0) {
  1983. failf(data, "nghttp2_submit_settings() failed: %s(%d)",
  1984. nghttp2_strerror(rv), rv);
  1985. return CURLE_HTTP2;
  1986. }
  1987. }
  1988. rv = nghttp2_session_set_local_window_size(httpc->h2, NGHTTP2_FLAG_NONE, 0,
  1989. HTTP2_HUGE_WINDOW_SIZE);
  1990. if(rv != 0) {
  1991. failf(data, "nghttp2_session_set_local_window_size() failed: %s(%d)",
  1992. nghttp2_strerror(rv), rv);
  1993. return CURLE_HTTP2;
  1994. }
  1995. /* we are going to copy mem to httpc->inbuf. This is required since
  1996. mem is part of buffer pointed by stream->mem, and callbacks
  1997. called by nghttp2_session_mem_recv() will write stream specific
  1998. data into stream->mem, overwriting data already there. */
  1999. if(H2_BUFSIZE < nread) {
  2000. failf(data, "connection buffer size is too small to store data following "
  2001. "HTTP Upgrade response header: buflen=%zu, datalen=%zu",
  2002. H2_BUFSIZE, nread);
  2003. return CURLE_HTTP2;
  2004. }
  2005. infof(conn->data, "Copying HTTP/2 data in stream buffer to connection buffer"
  2006. " after upgrade: len=%zu\n",
  2007. nread);
  2008. if(nread)
  2009. memcpy(httpc->inbuf, mem, nread);
  2010. httpc->inbuflen = nread;
  2011. nproc = nghttp2_session_mem_recv(httpc->h2, (const uint8_t *)httpc->inbuf,
  2012. httpc->inbuflen);
  2013. if(nghttp2_is_fatal((int)nproc)) {
  2014. failf(data, "nghttp2_session_mem_recv() failed: %s(%d)",
  2015. nghttp2_strerror((int)nproc), (int)nproc);
  2016. return CURLE_HTTP2;
  2017. }
  2018. H2BUGF(infof(data, "nghttp2_session_mem_recv() returns %zd\n", nproc));
  2019. if((ssize_t)nread == nproc) {
  2020. httpc->inbuflen = 0;
  2021. httpc->nread_inbuf = 0;
  2022. }
  2023. else {
  2024. httpc->nread_inbuf += nproc;
  2025. }
  2026. /* Try to send some frames since we may read SETTINGS already. */
  2027. rv = h2_session_send(data, httpc->h2);
  2028. if(rv != 0) {
  2029. failf(data, "nghttp2_session_send() failed: %s(%d)",
  2030. nghttp2_strerror(rv), rv);
  2031. return CURLE_HTTP2;
  2032. }
  2033. if(should_close_session(httpc)) {
  2034. H2BUGF(infof(data,
  2035. "nghttp2_session_send(): nothing to do in this session\n"));
  2036. return CURLE_HTTP2;
  2037. }
  2038. return CURLE_OK;
  2039. }
  2040. CURLcode Curl_http2_stream_pause(struct Curl_easy *data, bool pause)
  2041. {
  2042. DEBUGASSERT(data);
  2043. DEBUGASSERT(data->conn);
  2044. /* if it isn't HTTP/2, we're done */
  2045. if(!data->conn->proto.httpc.h2)
  2046. return CURLE_OK;
  2047. #ifdef NGHTTP2_HAS_SET_LOCAL_WINDOW_SIZE
  2048. else {
  2049. struct HTTP *stream = data->req.protop;
  2050. struct http_conn *httpc = &data->conn->proto.httpc;
  2051. uint32_t window = !pause * HTTP2_HUGE_WINDOW_SIZE;
  2052. int rv = nghttp2_session_set_local_window_size(httpc->h2,
  2053. NGHTTP2_FLAG_NONE,
  2054. stream->stream_id,
  2055. window);
  2056. if(rv) {
  2057. failf(data, "nghttp2_session_set_local_window_size() failed: %s(%d)",
  2058. nghttp2_strerror(rv), rv);
  2059. return CURLE_HTTP2;
  2060. }
  2061. /* make sure the window update gets sent */
  2062. rv = h2_session_send(data, httpc->h2);
  2063. if(rv)
  2064. return CURLE_SEND_ERROR;
  2065. DEBUGF(infof(data, "Set HTTP/2 window size to %u for stream %u\n",
  2066. window, stream->stream_id));
  2067. #ifdef DEBUGBUILD
  2068. {
  2069. /* read out the stream local window again */
  2070. uint32_t window2 =
  2071. nghttp2_session_get_stream_local_window_size(httpc->h2,
  2072. stream->stream_id);
  2073. DEBUGF(infof(data, "HTTP/2 window size is now %u for stream %u\n",
  2074. window2, stream->stream_id));
  2075. }
  2076. #endif
  2077. }
  2078. #endif
  2079. return CURLE_OK;
  2080. }
  2081. CURLcode Curl_http2_add_child(struct Curl_easy *parent,
  2082. struct Curl_easy *child,
  2083. bool exclusive)
  2084. {
  2085. if(parent) {
  2086. struct Curl_http2_dep **tail;
  2087. struct Curl_http2_dep *dep = calloc(1, sizeof(struct Curl_http2_dep));
  2088. if(!dep)
  2089. return CURLE_OUT_OF_MEMORY;
  2090. dep->data = child;
  2091. if(parent->set.stream_dependents && exclusive) {
  2092. struct Curl_http2_dep *node = parent->set.stream_dependents;
  2093. while(node) {
  2094. node->data->set.stream_depends_on = child;
  2095. node = node->next;
  2096. }
  2097. tail = &child->set.stream_dependents;
  2098. while(*tail)
  2099. tail = &(*tail)->next;
  2100. DEBUGASSERT(!*tail);
  2101. *tail = parent->set.stream_dependents;
  2102. parent->set.stream_dependents = 0;
  2103. }
  2104. tail = &parent->set.stream_dependents;
  2105. while(*tail) {
  2106. (*tail)->data->set.stream_depends_e = FALSE;
  2107. tail = &(*tail)->next;
  2108. }
  2109. DEBUGASSERT(!*tail);
  2110. *tail = dep;
  2111. }
  2112. child->set.stream_depends_on = parent;
  2113. child->set.stream_depends_e = exclusive;
  2114. return CURLE_OK;
  2115. }
  2116. void Curl_http2_remove_child(struct Curl_easy *parent, struct Curl_easy *child)
  2117. {
  2118. struct Curl_http2_dep *last = 0;
  2119. struct Curl_http2_dep *data = parent->set.stream_dependents;
  2120. DEBUGASSERT(child->set.stream_depends_on == parent);
  2121. while(data && data->data != child) {
  2122. last = data;
  2123. data = data->next;
  2124. }
  2125. DEBUGASSERT(data);
  2126. if(data) {
  2127. if(last) {
  2128. last->next = data->next;
  2129. }
  2130. else {
  2131. parent->set.stream_dependents = data->next;
  2132. }
  2133. free(data);
  2134. }
  2135. child->set.stream_depends_on = 0;
  2136. child->set.stream_depends_e = FALSE;
  2137. }
  2138. void Curl_http2_cleanup_dependencies(struct Curl_easy *data)
  2139. {
  2140. while(data->set.stream_dependents) {
  2141. struct Curl_easy *tmp = data->set.stream_dependents->data;
  2142. Curl_http2_remove_child(data, tmp);
  2143. if(data->set.stream_depends_on)
  2144. Curl_http2_add_child(data->set.stream_depends_on, tmp, FALSE);
  2145. }
  2146. if(data->set.stream_depends_on)
  2147. Curl_http2_remove_child(data->set.stream_depends_on, data);
  2148. }
  2149. /* Only call this function for a transfer that already got a HTTP/2
  2150. CURLE_HTTP2_STREAM error! */
  2151. bool Curl_h2_http_1_1_error(struct connectdata *conn)
  2152. {
  2153. struct http_conn *httpc = &conn->proto.httpc;
  2154. return (httpc->error_code == NGHTTP2_HTTP_1_1_REQUIRED);
  2155. }
  2156. #else /* !USE_NGHTTP2 */
  2157. /* Satisfy external references even if http2 is not compiled in. */
  2158. #include <curl/curl.h>
  2159. char *curl_pushheader_bynum(struct curl_pushheaders *h, size_t num)
  2160. {
  2161. (void) h;
  2162. (void) num;
  2163. return NULL;
  2164. }
  2165. char *curl_pushheader_byname(struct curl_pushheaders *h, const char *header)
  2166. {
  2167. (void) h;
  2168. (void) header;
  2169. return NULL;
  2170. }
  2171. #endif /* USE_NGHTTP2 */