http2.c 76 KB

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