http2.c 92 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #ifdef USE_NGHTTP2
  26. #include <stdint.h>
  27. #include <nghttp2/nghttp2.h>
  28. #include "urldata.h"
  29. #include "bufq.h"
  30. #include "hash.h"
  31. #include "http1.h"
  32. #include "http2.h"
  33. #include "http.h"
  34. #include "sendf.h"
  35. #include "select.h"
  36. #include "curl_base64.h"
  37. #include "strcase.h"
  38. #include "multiif.h"
  39. #include "url.h"
  40. #include "urlapi-int.h"
  41. #include "cfilters.h"
  42. #include "connect.h"
  43. #include "rand.h"
  44. #include "strtoofft.h"
  45. #include "strdup.h"
  46. #include "transfer.h"
  47. #include "dynbuf.h"
  48. #include "headers.h"
  49. /* The last 3 #include files should be in this order */
  50. #include "curl_printf.h"
  51. #include "curl_memory.h"
  52. #include "memdebug.h"
  53. #if (NGHTTP2_VERSION_NUM < 0x010c00)
  54. #error too old nghttp2 version, upgrade!
  55. #endif
  56. #ifdef CURL_DISABLE_VERBOSE_STRINGS
  57. #define nghttp2_session_callbacks_set_error_callback(x,y)
  58. #endif
  59. #if (NGHTTP2_VERSION_NUM >= 0x010c00)
  60. #define NGHTTP2_HAS_SET_LOCAL_WINDOW_SIZE 1
  61. #endif
  62. /* buffer dimensioning:
  63. * use 16K as chunk size, as that fits H2 DATA frames well */
  64. #define H2_CHUNK_SIZE (16 * 1024)
  65. /* connection window size */
  66. #define H2_CONN_WINDOW_SIZE (10 * 1024 * 1024)
  67. /* on receiving from TLS, we prep for holding a full stream window */
  68. #define H2_NW_RECV_CHUNKS (H2_CONN_WINDOW_SIZE / H2_CHUNK_SIZE)
  69. /* on send into TLS, we just want to accumulate small frames */
  70. #define H2_NW_SEND_CHUNKS 1
  71. /* this is how much we want "in flight" for a stream, unthrottled */
  72. #define H2_STREAM_WINDOW_SIZE_MAX (10 * 1024 * 1024)
  73. /* this is how much we want "in flight" for a stream, initially, IFF
  74. * nghttp2 allows us to tweak the local window size. */
  75. #if NGHTTP2_HAS_SET_LOCAL_WINDOW_SIZE
  76. #define H2_STREAM_WINDOW_SIZE_INITIAL (64 * 1024)
  77. #else
  78. #define H2_STREAM_WINDOW_SIZE_INITIAL H2_STREAM_WINDOW_SIZE_MAX
  79. #endif
  80. /* keep smaller stream upload buffer (default h2 window size) to have
  81. * our progress bars and "upload done" reporting closer to reality */
  82. #define H2_STREAM_SEND_CHUNKS ((64 * 1024) / H2_CHUNK_SIZE)
  83. /* spare chunks we keep for a full window */
  84. #define H2_STREAM_POOL_SPARES (H2_CONN_WINDOW_SIZE / H2_CHUNK_SIZE)
  85. /* We need to accommodate the max number of streams with their window sizes on
  86. * the overall connection. Streams might become PAUSED which will block their
  87. * received QUOTA in the connection window. If we run out of space, the server
  88. * is blocked from sending us any data. See #10988 for an issue with this. */
  89. #define HTTP2_HUGE_WINDOW_SIZE (100 * H2_STREAM_WINDOW_SIZE_MAX)
  90. #define H2_SETTINGS_IV_LEN 3
  91. #define H2_BINSETTINGS_LEN 80
  92. static size_t populate_settings(nghttp2_settings_entry *iv,
  93. struct Curl_easy *data)
  94. {
  95. iv[0].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS;
  96. iv[0].value = Curl_multi_max_concurrent_streams(data->multi);
  97. iv[1].settings_id = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE;
  98. iv[1].value = H2_STREAM_WINDOW_SIZE_INITIAL;
  99. iv[2].settings_id = NGHTTP2_SETTINGS_ENABLE_PUSH;
  100. iv[2].value = data->multi->push_cb != NULL;
  101. return 3;
  102. }
  103. static ssize_t populate_binsettings(uint8_t *binsettings,
  104. struct Curl_easy *data)
  105. {
  106. nghttp2_settings_entry iv[H2_SETTINGS_IV_LEN];
  107. size_t ivlen;
  108. ivlen = populate_settings(iv, data);
  109. /* this returns number of bytes it wrote or a negative number on error. */
  110. return nghttp2_pack_settings_payload(binsettings, H2_BINSETTINGS_LEN,
  111. iv, ivlen);
  112. }
  113. struct cf_h2_ctx {
  114. nghttp2_session *h2;
  115. /* The easy handle used in the current filter call, cleared at return */
  116. struct cf_call_data call_data;
  117. struct bufq inbufq; /* network input */
  118. struct bufq outbufq; /* network output */
  119. struct bufc_pool stream_bufcp; /* spares for stream buffers */
  120. struct dynbuf scratch; /* scratch buffer for temp use */
  121. struct Curl_hash streams; /* hash of `data->mid` to `h2_stream_ctx` */
  122. size_t drain_total; /* sum of all stream's UrlState drain */
  123. uint32_t max_concurrent_streams;
  124. uint32_t goaway_error; /* goaway error code from server */
  125. int32_t remote_max_sid; /* max id processed by server */
  126. int32_t local_max_sid; /* max id processed by us */
  127. BIT(initialized);
  128. BIT(via_h1_upgrade);
  129. BIT(conn_closed);
  130. BIT(rcvd_goaway);
  131. BIT(sent_goaway);
  132. BIT(enable_push);
  133. BIT(nw_out_blocked);
  134. };
  135. /* How to access `call_data` from a cf_h2 filter */
  136. #undef CF_CTX_CALL_DATA
  137. #define CF_CTX_CALL_DATA(cf) \
  138. ((struct cf_h2_ctx *)(cf)->ctx)->call_data
  139. static void h2_stream_hash_free(void *stream);
  140. static void cf_h2_ctx_init(struct cf_h2_ctx *ctx, bool via_h1_upgrade)
  141. {
  142. Curl_bufcp_init(&ctx->stream_bufcp, H2_CHUNK_SIZE, H2_STREAM_POOL_SPARES);
  143. Curl_bufq_initp(&ctx->inbufq, &ctx->stream_bufcp, H2_NW_RECV_CHUNKS, 0);
  144. Curl_bufq_initp(&ctx->outbufq, &ctx->stream_bufcp, H2_NW_SEND_CHUNKS, 0);
  145. Curl_dyn_init(&ctx->scratch, CURL_MAX_HTTP_HEADER);
  146. Curl_hash_offt_init(&ctx->streams, 63, h2_stream_hash_free);
  147. ctx->remote_max_sid = 2147483647;
  148. ctx->via_h1_upgrade = via_h1_upgrade;
  149. ctx->initialized = TRUE;
  150. }
  151. static void cf_h2_ctx_free(struct cf_h2_ctx *ctx)
  152. {
  153. if(ctx && ctx->initialized) {
  154. Curl_bufq_free(&ctx->inbufq);
  155. Curl_bufq_free(&ctx->outbufq);
  156. Curl_bufcp_free(&ctx->stream_bufcp);
  157. Curl_dyn_free(&ctx->scratch);
  158. Curl_hash_clean(&ctx->streams);
  159. Curl_hash_destroy(&ctx->streams);
  160. memset(ctx, 0, sizeof(*ctx));
  161. }
  162. free(ctx);
  163. }
  164. static void cf_h2_ctx_close(struct cf_h2_ctx *ctx)
  165. {
  166. if(ctx->h2) {
  167. nghttp2_session_del(ctx->h2);
  168. }
  169. }
  170. static CURLcode h2_progress_egress(struct Curl_cfilter *cf,
  171. struct Curl_easy *data);
  172. /**
  173. * All about the H2 internals of a stream
  174. */
  175. struct h2_stream_ctx {
  176. struct bufq recvbuf; /* response buffer */
  177. struct bufq sendbuf; /* request buffer */
  178. struct h1_req_parser h1; /* parsing the request */
  179. struct dynhds resp_trailers; /* response trailer fields */
  180. size_t resp_hds_len; /* amount of response header bytes in recvbuf */
  181. curl_off_t nrcvd_data; /* number of DATA bytes received */
  182. char **push_headers; /* allocated array */
  183. size_t push_headers_used; /* number of entries filled in */
  184. size_t push_headers_alloc; /* number of entries allocated */
  185. int status_code; /* HTTP response status code */
  186. uint32_t error; /* stream error code */
  187. CURLcode xfer_result; /* Result of writing out response */
  188. int32_t local_window_size; /* the local recv window size */
  189. int32_t id; /* HTTP/2 protocol identifier for stream */
  190. BIT(resp_hds_complete); /* we have a complete, final response */
  191. BIT(closed); /* TRUE on stream close */
  192. BIT(reset); /* TRUE on stream reset */
  193. BIT(close_handled); /* TRUE if stream closure is handled by libcurl */
  194. BIT(bodystarted);
  195. BIT(body_eos); /* the complete body has been added to `sendbuf` and
  196. * is being/has been processed from there. */
  197. };
  198. #define H2_STREAM_CTX(ctx,data) ((struct h2_stream_ctx *)(\
  199. data? Curl_hash_offt_get(&(ctx)->streams, (data)->mid) : NULL))
  200. static struct h2_stream_ctx *h2_stream_ctx_create(struct cf_h2_ctx *ctx)
  201. {
  202. struct h2_stream_ctx *stream;
  203. (void)ctx;
  204. stream = calloc(1, sizeof(*stream));
  205. if(!stream)
  206. return NULL;
  207. stream->id = -1;
  208. Curl_bufq_initp(&stream->sendbuf, &ctx->stream_bufcp,
  209. H2_STREAM_SEND_CHUNKS, BUFQ_OPT_NONE);
  210. Curl_h1_req_parse_init(&stream->h1, H1_PARSE_DEFAULT_MAX_LINE_LEN);
  211. Curl_dynhds_init(&stream->resp_trailers, 0, DYN_HTTP_REQUEST);
  212. stream->resp_hds_len = 0;
  213. stream->bodystarted = FALSE;
  214. stream->status_code = -1;
  215. stream->closed = FALSE;
  216. stream->close_handled = FALSE;
  217. stream->error = NGHTTP2_NO_ERROR;
  218. stream->local_window_size = H2_STREAM_WINDOW_SIZE_INITIAL;
  219. stream->nrcvd_data = 0;
  220. return stream;
  221. }
  222. static void free_push_headers(struct h2_stream_ctx *stream)
  223. {
  224. size_t i;
  225. for(i = 0; i < stream->push_headers_used; i++)
  226. free(stream->push_headers[i]);
  227. Curl_safefree(stream->push_headers);
  228. stream->push_headers_used = 0;
  229. }
  230. static void h2_stream_ctx_free(struct h2_stream_ctx *stream)
  231. {
  232. Curl_bufq_free(&stream->sendbuf);
  233. Curl_h1_req_parse_free(&stream->h1);
  234. Curl_dynhds_free(&stream->resp_trailers);
  235. free_push_headers(stream);
  236. free(stream);
  237. }
  238. static void h2_stream_hash_free(void *stream)
  239. {
  240. DEBUGASSERT(stream);
  241. h2_stream_ctx_free((struct h2_stream_ctx *)stream);
  242. }
  243. #ifdef NGHTTP2_HAS_SET_LOCAL_WINDOW_SIZE
  244. static int32_t cf_h2_get_desired_local_win(struct Curl_cfilter *cf,
  245. struct Curl_easy *data)
  246. {
  247. (void)cf;
  248. if(data->set.max_recv_speed && data->set.max_recv_speed < INT32_MAX) {
  249. /* The transfer should only receive `max_recv_speed` bytes per second.
  250. * We restrict the stream's local window size, so that the server cannot
  251. * send us "too much" at a time.
  252. * This gets less precise the higher the latency. */
  253. return (int32_t)data->set.max_recv_speed;
  254. }
  255. return H2_STREAM_WINDOW_SIZE_MAX;
  256. }
  257. static CURLcode cf_h2_update_local_win(struct Curl_cfilter *cf,
  258. struct Curl_easy *data,
  259. struct h2_stream_ctx *stream,
  260. bool paused)
  261. {
  262. struct cf_h2_ctx *ctx = cf->ctx;
  263. int32_t dwsize;
  264. int rv;
  265. dwsize = paused ? 0 : cf_h2_get_desired_local_win(cf, data);
  266. if(dwsize != stream->local_window_size) {
  267. int32_t wsize = nghttp2_session_get_stream_effective_local_window_size(
  268. ctx->h2, stream->id);
  269. if(dwsize > wsize) {
  270. rv = nghttp2_submit_window_update(ctx->h2, NGHTTP2_FLAG_NONE,
  271. stream->id, dwsize - wsize);
  272. if(rv) {
  273. failf(data, "[%d] nghttp2_submit_window_update() failed: "
  274. "%s(%d)", stream->id, nghttp2_strerror(rv), rv);
  275. return CURLE_HTTP2;
  276. }
  277. stream->local_window_size = dwsize;
  278. CURL_TRC_CF(data, cf, "[%d] local window update by %d",
  279. stream->id, dwsize - wsize);
  280. }
  281. else {
  282. rv = nghttp2_session_set_local_window_size(ctx->h2, NGHTTP2_FLAG_NONE,
  283. stream->id, dwsize);
  284. if(rv) {
  285. failf(data, "[%d] nghttp2_session_set_local_window_size() failed: "
  286. "%s(%d)", stream->id, nghttp2_strerror(rv), rv);
  287. return CURLE_HTTP2;
  288. }
  289. stream->local_window_size = dwsize;
  290. CURL_TRC_CF(data, cf, "[%d] local window size now %d",
  291. stream->id, dwsize);
  292. }
  293. }
  294. return CURLE_OK;
  295. }
  296. #else /* NGHTTP2_HAS_SET_LOCAL_WINDOW_SIZE */
  297. static CURLcode cf_h2_update_local_win(struct Curl_cfilter *cf,
  298. struct Curl_easy *data,
  299. struct h2_stream_ctx *stream,
  300. bool paused)
  301. {
  302. (void)cf;
  303. (void)data;
  304. (void)stream;
  305. (void)paused;
  306. return CURLE_OK;
  307. }
  308. #endif /* !NGHTTP2_HAS_SET_LOCAL_WINDOW_SIZE */
  309. /*
  310. * Mark this transfer to get "drained".
  311. */
  312. static void drain_stream(struct Curl_cfilter *cf,
  313. struct Curl_easy *data,
  314. struct h2_stream_ctx *stream)
  315. {
  316. unsigned char bits;
  317. (void)cf;
  318. bits = CURL_CSELECT_IN;
  319. if(!stream->closed &&
  320. (!stream->body_eos || !Curl_bufq_is_empty(&stream->sendbuf)))
  321. bits |= CURL_CSELECT_OUT;
  322. if(stream->closed || (data->state.select_bits != bits)) {
  323. CURL_TRC_CF(data, cf, "[%d] DRAIN select_bits=%x",
  324. stream->id, bits);
  325. data->state.select_bits = bits;
  326. Curl_expire(data, 0, EXPIRE_RUN_NOW);
  327. }
  328. }
  329. static CURLcode http2_data_setup(struct Curl_cfilter *cf,
  330. struct Curl_easy *data,
  331. struct h2_stream_ctx **pstream)
  332. {
  333. struct cf_h2_ctx *ctx = cf->ctx;
  334. struct h2_stream_ctx *stream;
  335. (void)cf;
  336. DEBUGASSERT(data);
  337. stream = H2_STREAM_CTX(ctx, data);
  338. if(stream) {
  339. *pstream = stream;
  340. return CURLE_OK;
  341. }
  342. stream = h2_stream_ctx_create(ctx);
  343. if(!stream)
  344. return CURLE_OUT_OF_MEMORY;
  345. if(!Curl_hash_offt_set(&ctx->streams, data->mid, stream)) {
  346. h2_stream_ctx_free(stream);
  347. return CURLE_OUT_OF_MEMORY;
  348. }
  349. *pstream = stream;
  350. return CURLE_OK;
  351. }
  352. static void http2_data_done(struct Curl_cfilter *cf, struct Curl_easy *data)
  353. {
  354. struct cf_h2_ctx *ctx = cf->ctx;
  355. struct h2_stream_ctx *stream = H2_STREAM_CTX(ctx, data);
  356. DEBUGASSERT(ctx);
  357. if(!stream || !ctx->initialized)
  358. return;
  359. if(ctx->h2) {
  360. bool flush_egress = FALSE;
  361. /* returns error if stream not known, which is fine here */
  362. (void)nghttp2_session_set_stream_user_data(ctx->h2, stream->id, NULL);
  363. if(!stream->closed && stream->id > 0) {
  364. /* RST_STREAM */
  365. CURL_TRC_CF(data, cf, "[%d] premature DATA_DONE, RST stream",
  366. stream->id);
  367. stream->closed = TRUE;
  368. stream->reset = TRUE;
  369. nghttp2_submit_rst_stream(ctx->h2, NGHTTP2_FLAG_NONE,
  370. stream->id, NGHTTP2_STREAM_CLOSED);
  371. flush_egress = TRUE;
  372. }
  373. if(flush_egress)
  374. nghttp2_session_send(ctx->h2);
  375. }
  376. Curl_hash_offt_remove(&ctx->streams, data->mid);
  377. }
  378. static int h2_client_new(struct Curl_cfilter *cf,
  379. nghttp2_session_callbacks *cbs)
  380. {
  381. struct cf_h2_ctx *ctx = cf->ctx;
  382. nghttp2_option *o;
  383. int rc = nghttp2_option_new(&o);
  384. if(rc)
  385. return rc;
  386. /* We handle window updates ourself to enforce buffer limits */
  387. nghttp2_option_set_no_auto_window_update(o, 1);
  388. #if NGHTTP2_VERSION_NUM >= 0x013200
  389. /* with 1.50.0 */
  390. /* turn off RFC 9113 leading and trailing white spaces validation against
  391. HTTP field value. */
  392. nghttp2_option_set_no_rfc9113_leading_and_trailing_ws_validation(o, 1);
  393. #endif
  394. rc = nghttp2_session_client_new2(&ctx->h2, cbs, cf, o);
  395. nghttp2_option_del(o);
  396. return rc;
  397. }
  398. static ssize_t nw_in_reader(void *reader_ctx,
  399. unsigned char *buf, size_t buflen,
  400. CURLcode *err)
  401. {
  402. struct Curl_cfilter *cf = reader_ctx;
  403. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  404. return Curl_conn_cf_recv(cf->next, data, (char *)buf, buflen, err);
  405. }
  406. static ssize_t nw_out_writer(void *writer_ctx,
  407. const unsigned char *buf, size_t buflen,
  408. CURLcode *err)
  409. {
  410. struct Curl_cfilter *cf = writer_ctx;
  411. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  412. if(data) {
  413. ssize_t nwritten = Curl_conn_cf_send(cf->next, data, (const char *)buf,
  414. buflen, FALSE, err);
  415. if(nwritten > 0)
  416. CURL_TRC_CF(data, cf, "[0] egress: wrote %zd bytes", nwritten);
  417. return nwritten;
  418. }
  419. return 0;
  420. }
  421. static ssize_t send_callback(nghttp2_session *h2,
  422. const uint8_t *mem, size_t length, int flags,
  423. void *userp);
  424. static int on_frame_recv(nghttp2_session *session, const nghttp2_frame *frame,
  425. void *userp);
  426. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  427. static int on_frame_send(nghttp2_session *session, const nghttp2_frame *frame,
  428. void *userp);
  429. #endif
  430. static int on_data_chunk_recv(nghttp2_session *session, uint8_t flags,
  431. int32_t stream_id,
  432. const uint8_t *mem, size_t len, void *userp);
  433. static int on_stream_close(nghttp2_session *session, int32_t stream_id,
  434. uint32_t error_code, void *userp);
  435. static int on_begin_headers(nghttp2_session *session,
  436. const nghttp2_frame *frame, void *userp);
  437. static int on_header(nghttp2_session *session, const nghttp2_frame *frame,
  438. const uint8_t *name, size_t namelen,
  439. const uint8_t *value, size_t valuelen,
  440. uint8_t flags,
  441. void *userp);
  442. #if !defined(CURL_DISABLE_VERBOSE_STRINGS)
  443. static int error_callback(nghttp2_session *session, const char *msg,
  444. size_t len, void *userp);
  445. #endif
  446. static CURLcode cf_h2_ctx_open(struct Curl_cfilter *cf,
  447. struct Curl_easy *data)
  448. {
  449. struct cf_h2_ctx *ctx = cf->ctx;
  450. struct h2_stream_ctx *stream;
  451. CURLcode result = CURLE_OUT_OF_MEMORY;
  452. int rc;
  453. nghttp2_session_callbacks *cbs = NULL;
  454. DEBUGASSERT(!ctx->h2);
  455. DEBUGASSERT(ctx->initialized);
  456. rc = nghttp2_session_callbacks_new(&cbs);
  457. if(rc) {
  458. failf(data, "Couldn't initialize nghttp2 callbacks");
  459. goto out;
  460. }
  461. nghttp2_session_callbacks_set_send_callback(cbs, send_callback);
  462. nghttp2_session_callbacks_set_on_frame_recv_callback(cbs, on_frame_recv);
  463. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  464. nghttp2_session_callbacks_set_on_frame_send_callback(cbs, on_frame_send);
  465. #endif
  466. nghttp2_session_callbacks_set_on_data_chunk_recv_callback(
  467. cbs, on_data_chunk_recv);
  468. nghttp2_session_callbacks_set_on_stream_close_callback(cbs, on_stream_close);
  469. nghttp2_session_callbacks_set_on_begin_headers_callback(
  470. cbs, on_begin_headers);
  471. nghttp2_session_callbacks_set_on_header_callback(cbs, on_header);
  472. #if !defined(CURL_DISABLE_VERBOSE_STRINGS)
  473. nghttp2_session_callbacks_set_error_callback(cbs, error_callback);
  474. #endif
  475. /* The nghttp2 session is not yet setup, do it */
  476. rc = h2_client_new(cf, cbs);
  477. if(rc) {
  478. failf(data, "Couldn't initialize nghttp2");
  479. goto out;
  480. }
  481. ctx->max_concurrent_streams = DEFAULT_MAX_CONCURRENT_STREAMS;
  482. if(ctx->via_h1_upgrade) {
  483. /* HTTP/1.1 Upgrade issued. H2 Settings have already been submitted
  484. * in the H1 request and we upgrade from there. This stream
  485. * is opened implicitly as #1. */
  486. uint8_t binsettings[H2_BINSETTINGS_LEN];
  487. ssize_t binlen; /* length of the binsettings data */
  488. binlen = populate_binsettings(binsettings, data);
  489. if(binlen <= 0) {
  490. failf(data, "nghttp2 unexpectedly failed on pack_settings_payload");
  491. result = CURLE_FAILED_INIT;
  492. goto out;
  493. }
  494. result = http2_data_setup(cf, data, &stream);
  495. if(result)
  496. goto out;
  497. DEBUGASSERT(stream);
  498. stream->id = 1;
  499. /* queue SETTINGS frame (again) */
  500. rc = nghttp2_session_upgrade2(ctx->h2, binsettings, (size_t)binlen,
  501. data->state.httpreq == HTTPREQ_HEAD,
  502. NULL);
  503. if(rc) {
  504. failf(data, "nghttp2_session_upgrade2() failed: %s(%d)",
  505. nghttp2_strerror(rc), rc);
  506. result = CURLE_HTTP2;
  507. goto out;
  508. }
  509. rc = nghttp2_session_set_stream_user_data(ctx->h2, stream->id,
  510. data);
  511. if(rc) {
  512. infof(data, "http/2: failed to set user_data for stream %u",
  513. stream->id);
  514. DEBUGASSERT(0);
  515. }
  516. CURL_TRC_CF(data, cf, "created session via Upgrade");
  517. }
  518. else {
  519. nghttp2_settings_entry iv[H2_SETTINGS_IV_LEN];
  520. size_t ivlen;
  521. ivlen = populate_settings(iv, data);
  522. rc = nghttp2_submit_settings(ctx->h2, NGHTTP2_FLAG_NONE,
  523. iv, ivlen);
  524. if(rc) {
  525. failf(data, "nghttp2_submit_settings() failed: %s(%d)",
  526. nghttp2_strerror(rc), rc);
  527. result = CURLE_HTTP2;
  528. goto out;
  529. }
  530. }
  531. rc = nghttp2_session_set_local_window_size(ctx->h2, NGHTTP2_FLAG_NONE, 0,
  532. HTTP2_HUGE_WINDOW_SIZE);
  533. if(rc) {
  534. failf(data, "nghttp2_session_set_local_window_size() failed: %s(%d)",
  535. nghttp2_strerror(rc), rc);
  536. result = CURLE_HTTP2;
  537. goto out;
  538. }
  539. /* all set, traffic will be send on connect */
  540. result = CURLE_OK;
  541. CURL_TRC_CF(data, cf, "[0] created h2 session%s",
  542. ctx->via_h1_upgrade ? " (via h1 upgrade)" : "");
  543. out:
  544. if(cbs)
  545. nghttp2_session_callbacks_del(cbs);
  546. return result;
  547. }
  548. /*
  549. * Returns nonzero if current HTTP/2 session should be closed.
  550. */
  551. static int should_close_session(struct cf_h2_ctx *ctx)
  552. {
  553. return ctx->drain_total == 0 && !nghttp2_session_want_read(ctx->h2) &&
  554. !nghttp2_session_want_write(ctx->h2);
  555. }
  556. /*
  557. * Processes pending input left in network input buffer.
  558. * This function returns 0 if it succeeds, or -1 and error code will
  559. * be assigned to *err.
  560. */
  561. static int h2_process_pending_input(struct Curl_cfilter *cf,
  562. struct Curl_easy *data,
  563. CURLcode *err)
  564. {
  565. struct cf_h2_ctx *ctx = cf->ctx;
  566. const unsigned char *buf;
  567. size_t blen;
  568. ssize_t rv;
  569. while(Curl_bufq_peek(&ctx->inbufq, &buf, &blen)) {
  570. rv = nghttp2_session_mem_recv(ctx->h2, (const uint8_t *)buf, blen);
  571. if(rv < 0) {
  572. failf(data,
  573. "process_pending_input: nghttp2_session_mem_recv() returned "
  574. "%zd:%s", rv, nghttp2_strerror((int)rv));
  575. *err = CURLE_RECV_ERROR;
  576. return -1;
  577. }
  578. Curl_bufq_skip(&ctx->inbufq, (size_t)rv);
  579. if(Curl_bufq_is_empty(&ctx->inbufq)) {
  580. break;
  581. }
  582. else {
  583. CURL_TRC_CF(data, cf, "process_pending_input: %zu bytes left "
  584. "in connection buffer", Curl_bufq_len(&ctx->inbufq));
  585. }
  586. }
  587. if(nghttp2_session_check_request_allowed(ctx->h2) == 0) {
  588. /* No more requests are allowed in the current session, so
  589. the connection may not be reused. This is set when a
  590. GOAWAY frame has been received or when the limit of stream
  591. identifiers has been reached. */
  592. connclose(cf->conn, "http/2: No new requests allowed");
  593. }
  594. return 0;
  595. }
  596. /*
  597. * The server may send us data at any point (e.g. PING frames). Therefore,
  598. * we cannot assume that an HTTP/2 socket is dead just because it is readable.
  599. *
  600. * Check the lower filters first and, if successful, peek at the socket
  601. * and distinguish between closed and data.
  602. */
  603. static bool http2_connisalive(struct Curl_cfilter *cf, struct Curl_easy *data,
  604. bool *input_pending)
  605. {
  606. struct cf_h2_ctx *ctx = cf->ctx;
  607. bool alive = TRUE;
  608. *input_pending = FALSE;
  609. if(!cf->next || !cf->next->cft->is_alive(cf->next, data, input_pending))
  610. return FALSE;
  611. if(*input_pending) {
  612. /* This happens before we have sent off a request and the connection is
  613. not in use by any other transfer, there should not be any data here,
  614. only "protocol frames" */
  615. CURLcode result;
  616. ssize_t nread = -1;
  617. *input_pending = FALSE;
  618. nread = Curl_bufq_slurp(&ctx->inbufq, nw_in_reader, cf, &result);
  619. if(nread != -1) {
  620. CURL_TRC_CF(data, cf, "%zd bytes stray data read before trying "
  621. "h2 connection", nread);
  622. if(h2_process_pending_input(cf, data, &result) < 0)
  623. /* immediate error, considered dead */
  624. alive = FALSE;
  625. else {
  626. alive = !should_close_session(ctx);
  627. }
  628. }
  629. else if(result != CURLE_AGAIN) {
  630. /* the read failed so let's say this is dead anyway */
  631. alive = FALSE;
  632. }
  633. }
  634. return alive;
  635. }
  636. static CURLcode http2_send_ping(struct Curl_cfilter *cf,
  637. struct Curl_easy *data)
  638. {
  639. struct cf_h2_ctx *ctx = cf->ctx;
  640. int rc;
  641. rc = nghttp2_submit_ping(ctx->h2, 0, ZERO_NULL);
  642. if(rc) {
  643. failf(data, "nghttp2_submit_ping() failed: %s(%d)",
  644. nghttp2_strerror(rc), rc);
  645. return CURLE_HTTP2;
  646. }
  647. rc = nghttp2_session_send(ctx->h2);
  648. if(rc) {
  649. failf(data, "nghttp2_session_send() failed: %s(%d)",
  650. nghttp2_strerror(rc), rc);
  651. return CURLE_SEND_ERROR;
  652. }
  653. return CURLE_OK;
  654. }
  655. /*
  656. * Store nghttp2 version info in this buffer.
  657. */
  658. void Curl_http2_ver(char *p, size_t len)
  659. {
  660. nghttp2_info *h2 = nghttp2_version(0);
  661. (void)msnprintf(p, len, "nghttp2/%s", h2->version_str);
  662. }
  663. static CURLcode nw_out_flush(struct Curl_cfilter *cf,
  664. struct Curl_easy *data)
  665. {
  666. struct cf_h2_ctx *ctx = cf->ctx;
  667. ssize_t nwritten;
  668. CURLcode result;
  669. (void)data;
  670. if(Curl_bufq_is_empty(&ctx->outbufq))
  671. return CURLE_OK;
  672. nwritten = Curl_bufq_pass(&ctx->outbufq, nw_out_writer, cf, &result);
  673. if(nwritten < 0) {
  674. if(result == CURLE_AGAIN) {
  675. CURL_TRC_CF(data, cf, "flush nw send buffer(%zu) -> EAGAIN",
  676. Curl_bufq_len(&ctx->outbufq));
  677. ctx->nw_out_blocked = 1;
  678. }
  679. return result;
  680. }
  681. return Curl_bufq_is_empty(&ctx->outbufq) ? CURLE_OK : CURLE_AGAIN;
  682. }
  683. /*
  684. * The implementation of nghttp2_send_callback type. Here we write |data| with
  685. * size |length| to the network and return the number of bytes actually
  686. * written. See the documentation of nghttp2_send_callback for the details.
  687. */
  688. static ssize_t send_callback(nghttp2_session *h2,
  689. const uint8_t *buf, size_t blen, int flags,
  690. void *userp)
  691. {
  692. struct Curl_cfilter *cf = userp;
  693. struct cf_h2_ctx *ctx = cf->ctx;
  694. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  695. ssize_t nwritten;
  696. CURLcode result = CURLE_OK;
  697. (void)h2;
  698. (void)flags;
  699. DEBUGASSERT(data);
  700. if(!cf->connected)
  701. nwritten = Curl_bufq_write(&ctx->outbufq, buf, blen, &result);
  702. else
  703. nwritten = Curl_bufq_write_pass(&ctx->outbufq, buf, blen,
  704. nw_out_writer, cf, &result);
  705. if(nwritten < 0) {
  706. if(result == CURLE_AGAIN) {
  707. ctx->nw_out_blocked = 1;
  708. return NGHTTP2_ERR_WOULDBLOCK;
  709. }
  710. failf(data, "Failed sending HTTP2 data");
  711. return NGHTTP2_ERR_CALLBACK_FAILURE;
  712. }
  713. if(!nwritten) {
  714. ctx->nw_out_blocked = 1;
  715. return NGHTTP2_ERR_WOULDBLOCK;
  716. }
  717. return nwritten;
  718. }
  719. /* We pass a pointer to this struct in the push callback, but the contents of
  720. the struct are hidden from the user. */
  721. struct curl_pushheaders {
  722. struct Curl_easy *data;
  723. struct h2_stream_ctx *stream;
  724. const nghttp2_push_promise *frame;
  725. };
  726. /*
  727. * push header access function. Only to be used from within the push callback
  728. */
  729. char *curl_pushheader_bynum(struct curl_pushheaders *h, size_t num)
  730. {
  731. /* Verify that we got a good easy handle in the push header struct, mostly to
  732. detect rubbish input fast(er). */
  733. if(!h || !GOOD_EASY_HANDLE(h->data))
  734. return NULL;
  735. else {
  736. if(h->stream && num < h->stream->push_headers_used)
  737. return h->stream->push_headers[num];
  738. }
  739. return NULL;
  740. }
  741. /*
  742. * push header access function. Only to be used from within the push callback
  743. */
  744. char *curl_pushheader_byname(struct curl_pushheaders *h, const char *header)
  745. {
  746. struct h2_stream_ctx *stream;
  747. size_t len;
  748. size_t i;
  749. /* Verify that we got a good easy handle in the push header struct,
  750. mostly to detect rubbish input fast(er). Also empty header name
  751. is just a rubbish too. We have to allow ":" at the beginning of
  752. the header, but header == ":" must be rejected. If we have ':' in
  753. the middle of header, it could be matched in middle of the value,
  754. this is because we do prefix match.*/
  755. if(!h || !GOOD_EASY_HANDLE(h->data) || !header || !header[0] ||
  756. !strcmp(header, ":") || strchr(header + 1, ':'))
  757. return NULL;
  758. stream = h->stream;
  759. if(!stream)
  760. return NULL;
  761. len = strlen(header);
  762. for(i = 0; i < stream->push_headers_used; i++) {
  763. if(!strncmp(header, stream->push_headers[i], len)) {
  764. /* sub-match, make sure that it is followed by a colon */
  765. if(stream->push_headers[i][len] != ':')
  766. continue;
  767. return &stream->push_headers[i][len + 1];
  768. }
  769. }
  770. return NULL;
  771. }
  772. static struct Curl_easy *h2_duphandle(struct Curl_cfilter *cf,
  773. struct Curl_easy *data)
  774. {
  775. struct Curl_easy *second = curl_easy_duphandle(data);
  776. if(second) {
  777. struct h2_stream_ctx *second_stream;
  778. http2_data_setup(cf, second, &second_stream);
  779. second->state.priority.weight = data->state.priority.weight;
  780. }
  781. return second;
  782. }
  783. static int set_transfer_url(struct Curl_easy *data,
  784. struct curl_pushheaders *hp)
  785. {
  786. const char *v;
  787. CURLUcode uc;
  788. char *url = NULL;
  789. int rc = 0;
  790. CURLU *u = curl_url();
  791. if(!u)
  792. return 5;
  793. v = curl_pushheader_byname(hp, HTTP_PSEUDO_SCHEME);
  794. if(v) {
  795. uc = curl_url_set(u, CURLUPART_SCHEME, v, 0);
  796. if(uc) {
  797. rc = 1;
  798. goto fail;
  799. }
  800. }
  801. v = curl_pushheader_byname(hp, HTTP_PSEUDO_AUTHORITY);
  802. if(v) {
  803. uc = Curl_url_set_authority(u, v);
  804. if(uc) {
  805. rc = 2;
  806. goto fail;
  807. }
  808. }
  809. v = curl_pushheader_byname(hp, HTTP_PSEUDO_PATH);
  810. if(v) {
  811. uc = curl_url_set(u, CURLUPART_PATH, v, 0);
  812. if(uc) {
  813. rc = 3;
  814. goto fail;
  815. }
  816. }
  817. uc = curl_url_get(u, CURLUPART_URL, &url, 0);
  818. if(uc)
  819. rc = 4;
  820. fail:
  821. curl_url_cleanup(u);
  822. if(rc)
  823. return rc;
  824. if(data->state.url_alloc)
  825. free(data->state.url);
  826. data->state.url_alloc = TRUE;
  827. data->state.url = url;
  828. return 0;
  829. }
  830. static void discard_newhandle(struct Curl_cfilter *cf,
  831. struct Curl_easy *newhandle)
  832. {
  833. http2_data_done(cf, newhandle);
  834. (void)Curl_close(&newhandle);
  835. }
  836. static int push_promise(struct Curl_cfilter *cf,
  837. struct Curl_easy *data,
  838. const nghttp2_push_promise *frame)
  839. {
  840. struct cf_h2_ctx *ctx = cf->ctx;
  841. int rv; /* one of the CURL_PUSH_* defines */
  842. CURL_TRC_CF(data, cf, "[%d] PUSH_PROMISE received",
  843. frame->promised_stream_id);
  844. if(data->multi->push_cb) {
  845. struct h2_stream_ctx *stream;
  846. struct h2_stream_ctx *newstream;
  847. struct curl_pushheaders heads;
  848. CURLMcode rc;
  849. CURLcode result;
  850. /* clone the parent */
  851. struct Curl_easy *newhandle = h2_duphandle(cf, data);
  852. if(!newhandle) {
  853. infof(data, "failed to duplicate handle");
  854. rv = CURL_PUSH_DENY; /* FAIL HARD */
  855. goto fail;
  856. }
  857. /* ask the application */
  858. CURL_TRC_CF(data, cf, "Got PUSH_PROMISE, ask application");
  859. stream = H2_STREAM_CTX(ctx, data);
  860. if(!stream) {
  861. failf(data, "Internal NULL stream");
  862. discard_newhandle(cf, newhandle);
  863. rv = CURL_PUSH_DENY;
  864. goto fail;
  865. }
  866. heads.data = data;
  867. heads.stream = stream;
  868. heads.frame = frame;
  869. rv = set_transfer_url(newhandle, &heads);
  870. if(rv) {
  871. discard_newhandle(cf, newhandle);
  872. rv = CURL_PUSH_DENY;
  873. goto fail;
  874. }
  875. result = http2_data_setup(cf, newhandle, &newstream);
  876. if(result) {
  877. failf(data, "error setting up stream: %d", result);
  878. discard_newhandle(cf, newhandle);
  879. rv = CURL_PUSH_DENY;
  880. goto fail;
  881. }
  882. DEBUGASSERT(stream);
  883. Curl_set_in_callback(data, TRUE);
  884. rv = data->multi->push_cb(data, newhandle,
  885. stream->push_headers_used, &heads,
  886. data->multi->push_userp);
  887. Curl_set_in_callback(data, FALSE);
  888. /* free the headers again */
  889. free_push_headers(stream);
  890. if(rv) {
  891. DEBUGASSERT((rv > CURL_PUSH_OK) && (rv <= CURL_PUSH_ERROROUT));
  892. /* denied, kill off the new handle again */
  893. discard_newhandle(cf, newhandle);
  894. goto fail;
  895. }
  896. newstream->id = frame->promised_stream_id;
  897. newhandle->req.maxdownload = -1;
  898. newhandle->req.size = -1;
  899. /* approved, add to the multi handle and immediately switch to PERFORM
  900. state with the given connection !*/
  901. rc = Curl_multi_add_perform(data->multi, newhandle, cf->conn);
  902. if(rc) {
  903. infof(data, "failed to add handle to multi");
  904. discard_newhandle(cf, newhandle);
  905. rv = CURL_PUSH_DENY;
  906. goto fail;
  907. }
  908. rv = nghttp2_session_set_stream_user_data(ctx->h2,
  909. newstream->id,
  910. newhandle);
  911. if(rv) {
  912. infof(data, "failed to set user_data for stream %u",
  913. newstream->id);
  914. DEBUGASSERT(0);
  915. rv = CURL_PUSH_DENY;
  916. goto fail;
  917. }
  918. /* success, remember max stream id processed */
  919. if(newstream->id > ctx->local_max_sid)
  920. ctx->local_max_sid = newstream->id;
  921. }
  922. else {
  923. CURL_TRC_CF(data, cf, "Got PUSH_PROMISE, ignore it");
  924. rv = CURL_PUSH_DENY;
  925. }
  926. fail:
  927. return rv;
  928. }
  929. static void h2_xfer_write_resp_hd(struct Curl_cfilter *cf,
  930. struct Curl_easy *data,
  931. struct h2_stream_ctx *stream,
  932. const char *buf, size_t blen, bool eos)
  933. {
  934. /* If we already encountered an error, skip further writes */
  935. if(!stream->xfer_result) {
  936. stream->xfer_result = Curl_xfer_write_resp_hd(data, buf, blen, eos);
  937. if(!stream->xfer_result && !eos)
  938. stream->xfer_result = cf_h2_update_local_win(cf, data, stream, FALSE);
  939. if(stream->xfer_result)
  940. CURL_TRC_CF(data, cf, "[%d] error %d writing %zu bytes of headers",
  941. stream->id, stream->xfer_result, blen);
  942. }
  943. }
  944. static void h2_xfer_write_resp(struct Curl_cfilter *cf,
  945. struct Curl_easy *data,
  946. struct h2_stream_ctx *stream,
  947. const char *buf, size_t blen, bool eos)
  948. {
  949. /* If we already encountered an error, skip further writes */
  950. if(!stream->xfer_result)
  951. stream->xfer_result = Curl_xfer_write_resp(data, buf, blen, eos);
  952. if(!stream->xfer_result && !eos)
  953. stream->xfer_result = cf_h2_update_local_win(cf, data, stream, FALSE);
  954. /* If the transfer write is errored, we do not want any more data */
  955. if(stream->xfer_result) {
  956. struct cf_h2_ctx *ctx = cf->ctx;
  957. CURL_TRC_CF(data, cf, "[%d] error %d writing %zu bytes of data, "
  958. "RST-ing stream",
  959. stream->id, stream->xfer_result, blen);
  960. nghttp2_submit_rst_stream(ctx->h2, 0, stream->id,
  961. (uint32_t)NGHTTP2_ERR_CALLBACK_FAILURE);
  962. }
  963. }
  964. static CURLcode on_stream_frame(struct Curl_cfilter *cf,
  965. struct Curl_easy *data,
  966. const nghttp2_frame *frame)
  967. {
  968. struct cf_h2_ctx *ctx = cf->ctx;
  969. struct h2_stream_ctx *stream = H2_STREAM_CTX(ctx, data);
  970. int32_t stream_id = frame->hd.stream_id;
  971. int rv;
  972. if(!stream) {
  973. CURL_TRC_CF(data, cf, "[%d] No stream_ctx set", stream_id);
  974. return CURLE_FAILED_INIT;
  975. }
  976. switch(frame->hd.type) {
  977. case NGHTTP2_DATA:
  978. CURL_TRC_CF(data, cf, "[%d] DATA, window=%d/%d",
  979. stream_id,
  980. nghttp2_session_get_stream_effective_recv_data_length(
  981. ctx->h2, stream->id),
  982. nghttp2_session_get_stream_effective_local_window_size(
  983. ctx->h2, stream->id));
  984. /* If !body started on this stream, then receiving DATA is illegal. */
  985. if(!stream->bodystarted) {
  986. rv = nghttp2_submit_rst_stream(ctx->h2, NGHTTP2_FLAG_NONE,
  987. stream_id, NGHTTP2_PROTOCOL_ERROR);
  988. if(nghttp2_is_fatal(rv)) {
  989. return CURLE_RECV_ERROR;
  990. }
  991. }
  992. break;
  993. case NGHTTP2_HEADERS:
  994. if(stream->bodystarted) {
  995. /* Only valid HEADERS after body started is trailer HEADERS. We
  996. buffer them in on_header callback. */
  997. break;
  998. }
  999. /* nghttp2 guarantees that :status is received, and we store it to
  1000. stream->status_code. Fuzzing has proven this can still be reached
  1001. without status code having been set. */
  1002. if(stream->status_code == -1)
  1003. return CURLE_RECV_ERROR;
  1004. /* Only final status code signals the end of header */
  1005. if(stream->status_code / 100 != 1)
  1006. stream->bodystarted = TRUE;
  1007. else
  1008. stream->status_code = -1;
  1009. h2_xfer_write_resp_hd(cf, data, stream, STRCONST("\r\n"), stream->closed);
  1010. if(stream->status_code / 100 != 1) {
  1011. stream->resp_hds_complete = TRUE;
  1012. }
  1013. drain_stream(cf, data, stream);
  1014. break;
  1015. case NGHTTP2_PUSH_PROMISE:
  1016. rv = push_promise(cf, data, &frame->push_promise);
  1017. if(rv) { /* deny! */
  1018. DEBUGASSERT((rv > CURL_PUSH_OK) && (rv <= CURL_PUSH_ERROROUT));
  1019. rv = nghttp2_submit_rst_stream(ctx->h2, NGHTTP2_FLAG_NONE,
  1020. frame->push_promise.promised_stream_id,
  1021. NGHTTP2_CANCEL);
  1022. if(nghttp2_is_fatal(rv))
  1023. return CURLE_SEND_ERROR;
  1024. else if(rv == CURL_PUSH_ERROROUT) {
  1025. CURL_TRC_CF(data, cf, "[%d] fail in PUSH_PROMISE received",
  1026. stream_id);
  1027. return CURLE_RECV_ERROR;
  1028. }
  1029. }
  1030. break;
  1031. case NGHTTP2_RST_STREAM:
  1032. stream->closed = TRUE;
  1033. if(frame->rst_stream.error_code) {
  1034. stream->reset = TRUE;
  1035. }
  1036. drain_stream(cf, data, stream);
  1037. break;
  1038. case NGHTTP2_WINDOW_UPDATE:
  1039. if(CURL_WANT_SEND(data) && Curl_bufq_is_empty(&stream->sendbuf)) {
  1040. /* need more data, force processing of transfer */
  1041. drain_stream(cf, data, stream);
  1042. }
  1043. else if(!Curl_bufq_is_empty(&stream->sendbuf)) {
  1044. /* resume the potentially suspended stream */
  1045. rv = nghttp2_session_resume_data(ctx->h2, stream->id);
  1046. if(nghttp2_is_fatal(rv))
  1047. return CURLE_SEND_ERROR;
  1048. }
  1049. break;
  1050. default:
  1051. break;
  1052. }
  1053. if(frame->hd.flags & NGHTTP2_FLAG_END_STREAM) {
  1054. if(!stream->closed && !stream->body_eos &&
  1055. ((stream->status_code >= 400) || (stream->status_code < 200))) {
  1056. /* The server did not give us a positive response and we are not
  1057. * done uploading the request body. We need to stop doing that and
  1058. * also inform the server that we aborted our side. */
  1059. CURL_TRC_CF(data, cf, "[%d] EOS frame with unfinished upload and "
  1060. "HTTP status %d, abort upload by RST",
  1061. stream_id, stream->status_code);
  1062. nghttp2_submit_rst_stream(ctx->h2, NGHTTP2_FLAG_NONE,
  1063. stream->id, NGHTTP2_STREAM_CLOSED);
  1064. stream->closed = TRUE;
  1065. }
  1066. drain_stream(cf, data, stream);
  1067. }
  1068. return CURLE_OK;
  1069. }
  1070. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  1071. static int fr_print(const nghttp2_frame *frame, char *buffer, size_t blen)
  1072. {
  1073. switch(frame->hd.type) {
  1074. case NGHTTP2_DATA: {
  1075. return msnprintf(buffer, blen,
  1076. "FRAME[DATA, len=%d, eos=%d, padlen=%d]",
  1077. (int)frame->hd.length,
  1078. !!(frame->hd.flags & NGHTTP2_FLAG_END_STREAM),
  1079. (int)frame->data.padlen);
  1080. }
  1081. case NGHTTP2_HEADERS: {
  1082. return msnprintf(buffer, blen,
  1083. "FRAME[HEADERS, len=%d, hend=%d, eos=%d]",
  1084. (int)frame->hd.length,
  1085. !!(frame->hd.flags & NGHTTP2_FLAG_END_HEADERS),
  1086. !!(frame->hd.flags & NGHTTP2_FLAG_END_STREAM));
  1087. }
  1088. case NGHTTP2_PRIORITY: {
  1089. return msnprintf(buffer, blen,
  1090. "FRAME[PRIORITY, len=%d, flags=%d]",
  1091. (int)frame->hd.length, frame->hd.flags);
  1092. }
  1093. case NGHTTP2_RST_STREAM: {
  1094. return msnprintf(buffer, blen,
  1095. "FRAME[RST_STREAM, len=%d, flags=%d, error=%u]",
  1096. (int)frame->hd.length, frame->hd.flags,
  1097. frame->rst_stream.error_code);
  1098. }
  1099. case NGHTTP2_SETTINGS: {
  1100. if(frame->hd.flags & NGHTTP2_FLAG_ACK) {
  1101. return msnprintf(buffer, blen, "FRAME[SETTINGS, ack=1]");
  1102. }
  1103. return msnprintf(buffer, blen,
  1104. "FRAME[SETTINGS, len=%d]", (int)frame->hd.length);
  1105. }
  1106. case NGHTTP2_PUSH_PROMISE: {
  1107. return msnprintf(buffer, blen,
  1108. "FRAME[PUSH_PROMISE, len=%d, hend=%d]",
  1109. (int)frame->hd.length,
  1110. !!(frame->hd.flags & NGHTTP2_FLAG_END_HEADERS));
  1111. }
  1112. case NGHTTP2_PING: {
  1113. return msnprintf(buffer, blen,
  1114. "FRAME[PING, len=%d, ack=%d]",
  1115. (int)frame->hd.length,
  1116. frame->hd.flags&NGHTTP2_FLAG_ACK);
  1117. }
  1118. case NGHTTP2_GOAWAY: {
  1119. char scratch[128];
  1120. size_t s_len = sizeof(scratch)/sizeof(scratch[0]);
  1121. size_t len = (frame->goaway.opaque_data_len < s_len) ?
  1122. frame->goaway.opaque_data_len : s_len-1;
  1123. if(len)
  1124. memcpy(scratch, frame->goaway.opaque_data, len);
  1125. scratch[len] = '\0';
  1126. return msnprintf(buffer, blen, "FRAME[GOAWAY, error=%d, reason='%s', "
  1127. "last_stream=%d]", frame->goaway.error_code,
  1128. scratch, frame->goaway.last_stream_id);
  1129. }
  1130. case NGHTTP2_WINDOW_UPDATE: {
  1131. return msnprintf(buffer, blen,
  1132. "FRAME[WINDOW_UPDATE, incr=%d]",
  1133. frame->window_update.window_size_increment);
  1134. }
  1135. default:
  1136. return msnprintf(buffer, blen, "FRAME[%d, len=%d, flags=%d]",
  1137. frame->hd.type, (int)frame->hd.length,
  1138. frame->hd.flags);
  1139. }
  1140. }
  1141. static int on_frame_send(nghttp2_session *session, const nghttp2_frame *frame,
  1142. void *userp)
  1143. {
  1144. struct Curl_cfilter *cf = userp;
  1145. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  1146. (void)session;
  1147. DEBUGASSERT(data);
  1148. if(data && Curl_trc_cf_is_verbose(cf, data)) {
  1149. char buffer[256];
  1150. int len;
  1151. len = fr_print(frame, buffer, sizeof(buffer)-1);
  1152. buffer[len] = 0;
  1153. CURL_TRC_CF(data, cf, "[%d] -> %s", frame->hd.stream_id, buffer);
  1154. }
  1155. return 0;
  1156. }
  1157. #endif /* !CURL_DISABLE_VERBOSE_STRINGS */
  1158. static int on_frame_recv(nghttp2_session *session, const nghttp2_frame *frame,
  1159. void *userp)
  1160. {
  1161. struct Curl_cfilter *cf = userp;
  1162. struct cf_h2_ctx *ctx = cf->ctx;
  1163. struct Curl_easy *data = CF_DATA_CURRENT(cf), *data_s;
  1164. int32_t stream_id = frame->hd.stream_id;
  1165. DEBUGASSERT(data);
  1166. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  1167. if(Curl_trc_cf_is_verbose(cf, data)) {
  1168. char buffer[256];
  1169. int len;
  1170. len = fr_print(frame, buffer, sizeof(buffer)-1);
  1171. buffer[len] = 0;
  1172. CURL_TRC_CF(data, cf, "[%d] <- %s",frame->hd.stream_id, buffer);
  1173. }
  1174. #endif /* !CURL_DISABLE_VERBOSE_STRINGS */
  1175. if(!stream_id) {
  1176. /* stream ID zero is for connection-oriented stuff */
  1177. DEBUGASSERT(data);
  1178. switch(frame->hd.type) {
  1179. case NGHTTP2_SETTINGS: {
  1180. if(!(frame->hd.flags & NGHTTP2_FLAG_ACK)) {
  1181. uint32_t max_conn = ctx->max_concurrent_streams;
  1182. ctx->max_concurrent_streams = nghttp2_session_get_remote_settings(
  1183. session, NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS);
  1184. ctx->enable_push = nghttp2_session_get_remote_settings(
  1185. session, NGHTTP2_SETTINGS_ENABLE_PUSH) != 0;
  1186. CURL_TRC_CF(data, cf, "[0] MAX_CONCURRENT_STREAMS: %d",
  1187. ctx->max_concurrent_streams);
  1188. CURL_TRC_CF(data, cf, "[0] ENABLE_PUSH: %s",
  1189. ctx->enable_push ? "TRUE" : "false");
  1190. if(data && max_conn != ctx->max_concurrent_streams) {
  1191. /* only signal change if the value actually changed */
  1192. CURL_TRC_CF(data, cf, "[0] notify MAX_CONCURRENT_STREAMS: %u",
  1193. ctx->max_concurrent_streams);
  1194. Curl_multi_connchanged(data->multi);
  1195. }
  1196. /* Since the initial stream window is 64K, a request might be on HOLD,
  1197. * due to exhaustion. The (initial) SETTINGS may announce a much larger
  1198. * window and *assume* that we treat this like a WINDOW_UPDATE. Some
  1199. * servers send an explicit WINDOW_UPDATE, but not all seem to do that.
  1200. * To be safe, we UNHOLD a stream in order not to stall. */
  1201. if(CURL_WANT_SEND(data)) {
  1202. struct h2_stream_ctx *stream = H2_STREAM_CTX(ctx, data);
  1203. if(stream)
  1204. drain_stream(cf, data, stream);
  1205. }
  1206. }
  1207. break;
  1208. }
  1209. case NGHTTP2_GOAWAY:
  1210. ctx->rcvd_goaway = TRUE;
  1211. ctx->goaway_error = frame->goaway.error_code;
  1212. ctx->remote_max_sid = frame->goaway.last_stream_id;
  1213. if(data) {
  1214. infof(data, "received GOAWAY, error=%u, last_stream=%u",
  1215. ctx->goaway_error, ctx->remote_max_sid);
  1216. Curl_multi_connchanged(data->multi);
  1217. }
  1218. break;
  1219. default:
  1220. break;
  1221. }
  1222. return 0;
  1223. }
  1224. data_s = nghttp2_session_get_stream_user_data(session, stream_id);
  1225. if(!data_s) {
  1226. CURL_TRC_CF(data, cf, "[%d] No Curl_easy associated", stream_id);
  1227. return 0;
  1228. }
  1229. return on_stream_frame(cf, data_s, frame) ? NGHTTP2_ERR_CALLBACK_FAILURE : 0;
  1230. }
  1231. static int on_data_chunk_recv(nghttp2_session *session, uint8_t flags,
  1232. int32_t stream_id,
  1233. const uint8_t *mem, size_t len, void *userp)
  1234. {
  1235. struct Curl_cfilter *cf = userp;
  1236. struct cf_h2_ctx *ctx = cf->ctx;
  1237. struct h2_stream_ctx *stream;
  1238. struct Curl_easy *data_s;
  1239. (void)flags;
  1240. DEBUGASSERT(stream_id); /* should never be a zero stream ID here */
  1241. DEBUGASSERT(CF_DATA_CURRENT(cf));
  1242. /* get the stream from the hash based on Stream ID */
  1243. data_s = nghttp2_session_get_stream_user_data(session, stream_id);
  1244. if(!data_s) {
  1245. /* Receiving a Stream ID not in the hash should not happen - unless
  1246. we have aborted a transfer artificially and there were more data
  1247. in the pipeline. Silently ignore. */
  1248. CURL_TRC_CF(CF_DATA_CURRENT(cf), cf, "[%d] Data for unknown",
  1249. stream_id);
  1250. /* consumed explicitly as no one will read it */
  1251. nghttp2_session_consume(session, stream_id, len);
  1252. return 0;
  1253. }
  1254. stream = H2_STREAM_CTX(ctx, data_s);
  1255. if(!stream)
  1256. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1257. h2_xfer_write_resp(cf, data_s, stream, (char *)mem, len, FALSE);
  1258. nghttp2_session_consume(ctx->h2, stream_id, len);
  1259. stream->nrcvd_data += (curl_off_t)len;
  1260. return 0;
  1261. }
  1262. static int on_stream_close(nghttp2_session *session, int32_t stream_id,
  1263. uint32_t error_code, void *userp)
  1264. {
  1265. struct Curl_cfilter *cf = userp;
  1266. struct cf_h2_ctx *ctx = cf->ctx;
  1267. struct Curl_easy *data_s, *call_data = CF_DATA_CURRENT(cf);
  1268. struct h2_stream_ctx *stream;
  1269. int rv;
  1270. (void)session;
  1271. DEBUGASSERT(call_data);
  1272. /* stream id 0 is the connection, do not look there for streams. */
  1273. data_s = stream_id ?
  1274. nghttp2_session_get_stream_user_data(session, stream_id) : NULL;
  1275. if(!data_s) {
  1276. CURL_TRC_CF(call_data, cf,
  1277. "[%d] on_stream_close, no easy set on stream", stream_id);
  1278. return 0;
  1279. }
  1280. if(!GOOD_EASY_HANDLE(data_s)) {
  1281. /* nghttp2 still has an easy registered for the stream which has
  1282. * been freed be libcurl. This points to a code path that does not
  1283. * trigger DONE or DETACH events as it must. */
  1284. CURL_TRC_CF(call_data, cf,
  1285. "[%d] on_stream_close, not a GOOD easy on stream", stream_id);
  1286. (void)nghttp2_session_set_stream_user_data(session, stream_id, 0);
  1287. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1288. }
  1289. stream = H2_STREAM_CTX(ctx, data_s);
  1290. if(!stream) {
  1291. CURL_TRC_CF(data_s, cf,
  1292. "[%d] on_stream_close, GOOD easy but no stream", stream_id);
  1293. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1294. }
  1295. stream->closed = TRUE;
  1296. stream->error = error_code;
  1297. if(stream->error) {
  1298. stream->reset = TRUE;
  1299. }
  1300. if(stream->error)
  1301. CURL_TRC_CF(data_s, cf, "[%d] RESET: %s (err %d)",
  1302. stream_id, nghttp2_http2_strerror(error_code), error_code);
  1303. else
  1304. CURL_TRC_CF(data_s, cf, "[%d] CLOSED", stream_id);
  1305. drain_stream(cf, data_s, stream);
  1306. /* remove `data_s` from the nghttp2 stream */
  1307. rv = nghttp2_session_set_stream_user_data(session, stream_id, 0);
  1308. if(rv) {
  1309. infof(data_s, "http/2: failed to clear user_data for stream %u",
  1310. stream_id);
  1311. DEBUGASSERT(0);
  1312. }
  1313. return 0;
  1314. }
  1315. static int on_begin_headers(nghttp2_session *session,
  1316. const nghttp2_frame *frame, void *userp)
  1317. {
  1318. struct Curl_cfilter *cf = userp;
  1319. struct cf_h2_ctx *ctx = cf->ctx;
  1320. struct h2_stream_ctx *stream;
  1321. struct Curl_easy *data_s = NULL;
  1322. (void)cf;
  1323. data_s = nghttp2_session_get_stream_user_data(session, frame->hd.stream_id);
  1324. if(!data_s) {
  1325. return 0;
  1326. }
  1327. if(frame->hd.type != NGHTTP2_HEADERS) {
  1328. return 0;
  1329. }
  1330. stream = H2_STREAM_CTX(ctx, data_s);
  1331. if(!stream || !stream->bodystarted) {
  1332. return 0;
  1333. }
  1334. return 0;
  1335. }
  1336. /* frame->hd.type is either NGHTTP2_HEADERS or NGHTTP2_PUSH_PROMISE */
  1337. static int on_header(nghttp2_session *session, const nghttp2_frame *frame,
  1338. const uint8_t *name, size_t namelen,
  1339. const uint8_t *value, size_t valuelen,
  1340. uint8_t flags,
  1341. void *userp)
  1342. {
  1343. struct Curl_cfilter *cf = userp;
  1344. struct cf_h2_ctx *ctx = cf->ctx;
  1345. struct h2_stream_ctx *stream;
  1346. struct Curl_easy *data_s;
  1347. int32_t stream_id = frame->hd.stream_id;
  1348. CURLcode result;
  1349. (void)flags;
  1350. DEBUGASSERT(stream_id); /* should never be a zero stream ID here */
  1351. /* get the stream from the hash based on Stream ID */
  1352. data_s = nghttp2_session_get_stream_user_data(session, stream_id);
  1353. if(!data_s)
  1354. /* Receiving a Stream ID not in the hash should not happen, this is an
  1355. internal error more than anything else! */
  1356. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1357. stream = H2_STREAM_CTX(ctx, data_s);
  1358. if(!stream) {
  1359. failf(data_s, "Internal NULL stream");
  1360. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1361. }
  1362. /* Store received PUSH_PROMISE headers to be used when the subsequent
  1363. PUSH_PROMISE callback comes */
  1364. if(frame->hd.type == NGHTTP2_PUSH_PROMISE) {
  1365. char *h;
  1366. if(!strcmp(HTTP_PSEUDO_AUTHORITY, (const char *)name)) {
  1367. /* pseudo headers are lower case */
  1368. int rc = 0;
  1369. char *check = aprintf("%s:%d", cf->conn->host.name,
  1370. cf->conn->remote_port);
  1371. if(!check)
  1372. /* no memory */
  1373. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1374. if(!strcasecompare(check, (const char *)value) &&
  1375. ((cf->conn->remote_port != cf->conn->given->defport) ||
  1376. !strcasecompare(cf->conn->host.name, (const char *)value))) {
  1377. /* This is push is not for the same authority that was asked for in
  1378. * the URL. RFC 7540 section 8.2 says: "A client MUST treat a
  1379. * PUSH_PROMISE for which the server is not authoritative as a stream
  1380. * error of type PROTOCOL_ERROR."
  1381. */
  1382. (void)nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
  1383. stream_id, NGHTTP2_PROTOCOL_ERROR);
  1384. rc = NGHTTP2_ERR_CALLBACK_FAILURE;
  1385. }
  1386. free(check);
  1387. if(rc)
  1388. return rc;
  1389. }
  1390. if(!stream->push_headers) {
  1391. stream->push_headers_alloc = 10;
  1392. stream->push_headers = malloc(stream->push_headers_alloc *
  1393. sizeof(char *));
  1394. if(!stream->push_headers)
  1395. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1396. stream->push_headers_used = 0;
  1397. }
  1398. else if(stream->push_headers_used ==
  1399. stream->push_headers_alloc) {
  1400. char **headp;
  1401. if(stream->push_headers_alloc > 1000) {
  1402. /* this is beyond crazy many headers, bail out */
  1403. failf(data_s, "Too many PUSH_PROMISE headers");
  1404. free_push_headers(stream);
  1405. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1406. }
  1407. stream->push_headers_alloc *= 2;
  1408. headp = realloc(stream->push_headers,
  1409. stream->push_headers_alloc * sizeof(char *));
  1410. if(!headp) {
  1411. free_push_headers(stream);
  1412. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1413. }
  1414. stream->push_headers = headp;
  1415. }
  1416. h = aprintf("%s:%s", name, value);
  1417. if(h)
  1418. stream->push_headers[stream->push_headers_used++] = h;
  1419. return 0;
  1420. }
  1421. if(stream->bodystarted) {
  1422. /* This is a trailer */
  1423. CURL_TRC_CF(data_s, cf, "[%d] trailer: %.*s: %.*s",
  1424. stream->id, (int)namelen, name, (int)valuelen, value);
  1425. result = Curl_dynhds_add(&stream->resp_trailers,
  1426. (const char *)name, namelen,
  1427. (const char *)value, valuelen);
  1428. if(result)
  1429. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1430. return 0;
  1431. }
  1432. if(namelen == sizeof(HTTP_PSEUDO_STATUS) - 1 &&
  1433. memcmp(HTTP_PSEUDO_STATUS, name, namelen) == 0) {
  1434. /* nghttp2 guarantees :status is received first and only once. */
  1435. char buffer[32];
  1436. result = Curl_http_decode_status(&stream->status_code,
  1437. (const char *)value, valuelen);
  1438. if(result)
  1439. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1440. msnprintf(buffer, sizeof(buffer), HTTP_PSEUDO_STATUS ":%u\r",
  1441. stream->status_code);
  1442. result = Curl_headers_push(data_s, buffer, CURLH_PSEUDO);
  1443. if(result)
  1444. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1445. Curl_dyn_reset(&ctx->scratch);
  1446. result = Curl_dyn_addn(&ctx->scratch, STRCONST("HTTP/2 "));
  1447. if(!result)
  1448. result = Curl_dyn_addn(&ctx->scratch, value, valuelen);
  1449. if(!result)
  1450. result = Curl_dyn_addn(&ctx->scratch, STRCONST(" \r\n"));
  1451. if(!result)
  1452. h2_xfer_write_resp_hd(cf, data_s, stream, Curl_dyn_ptr(&ctx->scratch),
  1453. Curl_dyn_len(&ctx->scratch), FALSE);
  1454. if(result)
  1455. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1456. /* if we receive data for another handle, wake that up */
  1457. if(CF_DATA_CURRENT(cf) != data_s)
  1458. Curl_expire(data_s, 0, EXPIRE_RUN_NOW);
  1459. CURL_TRC_CF(data_s, cf, "[%d] status: HTTP/2 %03d",
  1460. stream->id, stream->status_code);
  1461. return 0;
  1462. }
  1463. /* nghttp2 guarantees that namelen > 0, and :status was already
  1464. received, and this is not pseudo-header field . */
  1465. /* convert to an HTTP1-style header */
  1466. Curl_dyn_reset(&ctx->scratch);
  1467. result = Curl_dyn_addn(&ctx->scratch, (const char *)name, namelen);
  1468. if(!result)
  1469. result = Curl_dyn_addn(&ctx->scratch, STRCONST(": "));
  1470. if(!result)
  1471. result = Curl_dyn_addn(&ctx->scratch, (const char *)value, valuelen);
  1472. if(!result)
  1473. result = Curl_dyn_addn(&ctx->scratch, STRCONST("\r\n"));
  1474. if(!result)
  1475. h2_xfer_write_resp_hd(cf, data_s, stream, Curl_dyn_ptr(&ctx->scratch),
  1476. Curl_dyn_len(&ctx->scratch), FALSE);
  1477. if(result)
  1478. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1479. /* if we receive data for another handle, wake that up */
  1480. if(CF_DATA_CURRENT(cf) != data_s)
  1481. Curl_expire(data_s, 0, EXPIRE_RUN_NOW);
  1482. CURL_TRC_CF(data_s, cf, "[%d] header: %.*s: %.*s",
  1483. stream->id, (int)namelen, name, (int)valuelen, value);
  1484. return 0; /* 0 is successful */
  1485. }
  1486. static ssize_t req_body_read_callback(nghttp2_session *session,
  1487. int32_t stream_id,
  1488. uint8_t *buf, size_t length,
  1489. uint32_t *data_flags,
  1490. nghttp2_data_source *source,
  1491. void *userp)
  1492. {
  1493. struct Curl_cfilter *cf = userp;
  1494. struct cf_h2_ctx *ctx = cf->ctx;
  1495. struct Curl_easy *data_s;
  1496. struct h2_stream_ctx *stream = NULL;
  1497. CURLcode result;
  1498. ssize_t nread;
  1499. (void)source;
  1500. (void)cf;
  1501. if(!stream_id)
  1502. return NGHTTP2_ERR_INVALID_ARGUMENT;
  1503. /* get the stream from the hash based on Stream ID, stream ID zero is for
  1504. connection-oriented stuff */
  1505. data_s = nghttp2_session_get_stream_user_data(session, stream_id);
  1506. if(!data_s)
  1507. /* Receiving a Stream ID not in the hash should not happen, this is an
  1508. internal error more than anything else! */
  1509. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1510. stream = H2_STREAM_CTX(ctx, data_s);
  1511. if(!stream)
  1512. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1513. nread = Curl_bufq_read(&stream->sendbuf, buf, length, &result);
  1514. if(nread < 0) {
  1515. if(result != CURLE_AGAIN)
  1516. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1517. nread = 0;
  1518. }
  1519. CURL_TRC_CF(data_s, cf, "[%d] req_body_read(len=%zu) eos=%d -> %zd, %d",
  1520. stream_id, length, stream->body_eos, nread, result);
  1521. if(stream->body_eos && Curl_bufq_is_empty(&stream->sendbuf)) {
  1522. *data_flags = NGHTTP2_DATA_FLAG_EOF;
  1523. return nread;
  1524. }
  1525. return (nread == 0) ? NGHTTP2_ERR_DEFERRED : nread;
  1526. }
  1527. #if !defined(CURL_DISABLE_VERBOSE_STRINGS)
  1528. static int error_callback(nghttp2_session *session,
  1529. const char *msg,
  1530. size_t len,
  1531. void *userp)
  1532. {
  1533. struct Curl_cfilter *cf = userp;
  1534. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  1535. (void)session;
  1536. failf(data, "%.*s", (int)len, msg);
  1537. return 0;
  1538. }
  1539. #endif
  1540. /*
  1541. * Append headers to ask for an HTTP1.1 to HTTP2 upgrade.
  1542. */
  1543. CURLcode Curl_http2_request_upgrade(struct dynbuf *req,
  1544. struct Curl_easy *data)
  1545. {
  1546. CURLcode result;
  1547. char *base64;
  1548. size_t blen;
  1549. struct SingleRequest *k = &data->req;
  1550. uint8_t binsettings[H2_BINSETTINGS_LEN];
  1551. ssize_t binlen; /* length of the binsettings data */
  1552. binlen = populate_binsettings(binsettings, data);
  1553. if(binlen <= 0) {
  1554. failf(data, "nghttp2 unexpectedly failed on pack_settings_payload");
  1555. Curl_dyn_free(req);
  1556. return CURLE_FAILED_INIT;
  1557. }
  1558. result = Curl_base64url_encode((const char *)binsettings, (size_t)binlen,
  1559. &base64, &blen);
  1560. if(result) {
  1561. Curl_dyn_free(req);
  1562. return result;
  1563. }
  1564. result = Curl_dyn_addf(req,
  1565. "Connection: Upgrade, HTTP2-Settings\r\n"
  1566. "Upgrade: %s\r\n"
  1567. "HTTP2-Settings: %s\r\n",
  1568. NGHTTP2_CLEARTEXT_PROTO_VERSION_ID, base64);
  1569. free(base64);
  1570. k->upgr101 = UPGR101_H2;
  1571. data->conn->bits.asks_multiplex = TRUE;
  1572. return result;
  1573. }
  1574. static ssize_t http2_handle_stream_close(struct Curl_cfilter *cf,
  1575. struct Curl_easy *data,
  1576. struct h2_stream_ctx *stream,
  1577. CURLcode *err)
  1578. {
  1579. ssize_t rv = 0;
  1580. if(stream->error == NGHTTP2_REFUSED_STREAM) {
  1581. CURL_TRC_CF(data, cf, "[%d] REFUSED_STREAM, try again on a new "
  1582. "connection", stream->id);
  1583. connclose(cf->conn, "REFUSED_STREAM"); /* do not use this anymore */
  1584. data->state.refused_stream = TRUE;
  1585. *err = CURLE_RECV_ERROR; /* trigger Curl_retry_request() later */
  1586. return -1;
  1587. }
  1588. else if(stream->error != NGHTTP2_NO_ERROR) {
  1589. if(stream->resp_hds_complete && data->req.no_body) {
  1590. CURL_TRC_CF(data, cf, "[%d] error after response headers, but we did "
  1591. "not want a body anyway, ignore: %s (err %u)",
  1592. stream->id, nghttp2_http2_strerror(stream->error),
  1593. stream->error);
  1594. stream->close_handled = TRUE;
  1595. *err = CURLE_OK;
  1596. goto out;
  1597. }
  1598. failf(data, "HTTP/2 stream %u was not closed cleanly: %s (err %u)",
  1599. stream->id, nghttp2_http2_strerror(stream->error),
  1600. stream->error);
  1601. *err = CURLE_HTTP2_STREAM;
  1602. return -1;
  1603. }
  1604. else if(stream->reset) {
  1605. failf(data, "HTTP/2 stream %u was reset", stream->id);
  1606. *err = data->req.bytecount ? CURLE_PARTIAL_FILE : CURLE_HTTP2;
  1607. return -1;
  1608. }
  1609. if(!stream->bodystarted) {
  1610. failf(data, "HTTP/2 stream %u was closed cleanly, but before getting "
  1611. " all response header fields, treated as error",
  1612. stream->id);
  1613. *err = CURLE_HTTP2_STREAM;
  1614. return -1;
  1615. }
  1616. if(Curl_dynhds_count(&stream->resp_trailers)) {
  1617. struct dynhds_entry *e;
  1618. struct dynbuf dbuf;
  1619. size_t i;
  1620. *err = CURLE_OK;
  1621. Curl_dyn_init(&dbuf, DYN_TRAILERS);
  1622. for(i = 0; i < Curl_dynhds_count(&stream->resp_trailers); ++i) {
  1623. e = Curl_dynhds_getn(&stream->resp_trailers, i);
  1624. if(!e)
  1625. break;
  1626. Curl_dyn_reset(&dbuf);
  1627. *err = Curl_dyn_addf(&dbuf, "%.*s: %.*s\x0d\x0a",
  1628. (int)e->namelen, e->name,
  1629. (int)e->valuelen, e->value);
  1630. if(*err)
  1631. break;
  1632. Curl_debug(data, CURLINFO_HEADER_IN, Curl_dyn_ptr(&dbuf),
  1633. Curl_dyn_len(&dbuf));
  1634. *err = Curl_client_write(data, CLIENTWRITE_HEADER|CLIENTWRITE_TRAILER,
  1635. Curl_dyn_ptr(&dbuf), Curl_dyn_len(&dbuf));
  1636. if(*err)
  1637. break;
  1638. }
  1639. Curl_dyn_free(&dbuf);
  1640. if(*err)
  1641. goto out;
  1642. }
  1643. stream->close_handled = TRUE;
  1644. *err = CURLE_OK;
  1645. rv = 0;
  1646. out:
  1647. CURL_TRC_CF(data, cf, "handle_stream_close -> %zd, %d", rv, *err);
  1648. return rv;
  1649. }
  1650. static int sweight_wanted(const struct Curl_easy *data)
  1651. {
  1652. /* 0 weight is not set by user and we take the nghttp2 default one */
  1653. return data->set.priority.weight ?
  1654. data->set.priority.weight : NGHTTP2_DEFAULT_WEIGHT;
  1655. }
  1656. static int sweight_in_effect(const struct Curl_easy *data)
  1657. {
  1658. /* 0 weight is not set by user and we take the nghttp2 default one */
  1659. return data->state.priority.weight ?
  1660. data->state.priority.weight : NGHTTP2_DEFAULT_WEIGHT;
  1661. }
  1662. /*
  1663. * h2_pri_spec() fills in the pri_spec struct, used by nghttp2 to send weight
  1664. * and dependency to the peer. It also stores the updated values in the state
  1665. * struct.
  1666. */
  1667. static void h2_pri_spec(struct cf_h2_ctx *ctx,
  1668. struct Curl_easy *data,
  1669. nghttp2_priority_spec *pri_spec)
  1670. {
  1671. struct Curl_data_priority *prio = &data->set.priority;
  1672. struct h2_stream_ctx *depstream = H2_STREAM_CTX(ctx, prio->parent);
  1673. int32_t depstream_id = depstream ? depstream->id : 0;
  1674. nghttp2_priority_spec_init(pri_spec, depstream_id,
  1675. sweight_wanted(data),
  1676. data->set.priority.exclusive);
  1677. data->state.priority = *prio;
  1678. }
  1679. /*
  1680. * Check if there is been an update in the priority /
  1681. * dependency settings and if so it submits a PRIORITY frame with the updated
  1682. * info.
  1683. * Flush any out data pending in the network buffer.
  1684. */
  1685. static CURLcode h2_progress_egress(struct Curl_cfilter *cf,
  1686. struct Curl_easy *data)
  1687. {
  1688. struct cf_h2_ctx *ctx = cf->ctx;
  1689. struct h2_stream_ctx *stream = H2_STREAM_CTX(ctx, data);
  1690. int rv = 0;
  1691. if(stream && stream->id > 0 &&
  1692. ((sweight_wanted(data) != sweight_in_effect(data)) ||
  1693. (data->set.priority.exclusive != data->state.priority.exclusive) ||
  1694. (data->set.priority.parent != data->state.priority.parent)) ) {
  1695. /* send new weight and/or dependency */
  1696. nghttp2_priority_spec pri_spec;
  1697. h2_pri_spec(ctx, data, &pri_spec);
  1698. CURL_TRC_CF(data, cf, "[%d] Queuing PRIORITY", stream->id);
  1699. DEBUGASSERT(stream->id != -1);
  1700. rv = nghttp2_submit_priority(ctx->h2, NGHTTP2_FLAG_NONE,
  1701. stream->id, &pri_spec);
  1702. if(rv)
  1703. goto out;
  1704. }
  1705. ctx->nw_out_blocked = 0;
  1706. while(!rv && !ctx->nw_out_blocked && nghttp2_session_want_write(ctx->h2))
  1707. rv = nghttp2_session_send(ctx->h2);
  1708. out:
  1709. if(nghttp2_is_fatal(rv)) {
  1710. CURL_TRC_CF(data, cf, "nghttp2_session_send error (%s)%d",
  1711. nghttp2_strerror(rv), rv);
  1712. return CURLE_SEND_ERROR;
  1713. }
  1714. /* Defer flushing during the connect phase so that the SETTINGS and
  1715. * other initial frames are sent together with the first request.
  1716. * Unless we are 'connect_only' where the request will never come. */
  1717. if(!cf->connected && !cf->conn->connect_only)
  1718. return CURLE_OK;
  1719. return nw_out_flush(cf, data);
  1720. }
  1721. static ssize_t stream_recv(struct Curl_cfilter *cf, struct Curl_easy *data,
  1722. struct h2_stream_ctx *stream,
  1723. char *buf, size_t len, CURLcode *err)
  1724. {
  1725. struct cf_h2_ctx *ctx = cf->ctx;
  1726. ssize_t nread = -1;
  1727. (void)buf;
  1728. *err = CURLE_AGAIN;
  1729. if(stream->xfer_result) {
  1730. CURL_TRC_CF(data, cf, "[%d] xfer write failed", stream->id);
  1731. *err = stream->xfer_result;
  1732. nread = -1;
  1733. }
  1734. else if(stream->closed) {
  1735. CURL_TRC_CF(data, cf, "[%d] returning CLOSE", stream->id);
  1736. nread = http2_handle_stream_close(cf, data, stream, err);
  1737. }
  1738. else if(stream->reset ||
  1739. (ctx->conn_closed && Curl_bufq_is_empty(&ctx->inbufq)) ||
  1740. (ctx->rcvd_goaway && ctx->remote_max_sid < stream->id)) {
  1741. CURL_TRC_CF(data, cf, "[%d] returning ERR", stream->id);
  1742. *err = data->req.bytecount ? CURLE_PARTIAL_FILE : CURLE_HTTP2;
  1743. nread = -1;
  1744. }
  1745. if(nread < 0 && *err != CURLE_AGAIN)
  1746. CURL_TRC_CF(data, cf, "[%d] stream_recv(len=%zu) -> %zd, %d",
  1747. stream->id, len, nread, *err);
  1748. return nread;
  1749. }
  1750. static CURLcode h2_progress_ingress(struct Curl_cfilter *cf,
  1751. struct Curl_easy *data,
  1752. size_t data_max_bytes)
  1753. {
  1754. struct cf_h2_ctx *ctx = cf->ctx;
  1755. struct h2_stream_ctx *stream;
  1756. CURLcode result = CURLE_OK;
  1757. ssize_t nread;
  1758. /* Process network input buffer fist */
  1759. if(!Curl_bufq_is_empty(&ctx->inbufq)) {
  1760. CURL_TRC_CF(data, cf, "Process %zu bytes in connection buffer",
  1761. Curl_bufq_len(&ctx->inbufq));
  1762. if(h2_process_pending_input(cf, data, &result) < 0)
  1763. return result;
  1764. }
  1765. /* Receive data from the "lower" filters, e.g. network until
  1766. * it is time to stop due to connection close or us not processing
  1767. * all network input */
  1768. while(!ctx->conn_closed && Curl_bufq_is_empty(&ctx->inbufq)) {
  1769. stream = H2_STREAM_CTX(ctx, data);
  1770. if(stream && (stream->closed || !data_max_bytes)) {
  1771. /* We would like to abort here and stop processing, so that
  1772. * the transfer loop can handle the data/close here. However,
  1773. * this may leave data in underlying buffers that will not
  1774. * be consumed. */
  1775. if(!cf->next || !cf->next->cft->has_data_pending(cf->next, data))
  1776. drain_stream(cf, data, stream);
  1777. break;
  1778. }
  1779. nread = Curl_bufq_sipn(&ctx->inbufq, 0, nw_in_reader, cf, &result);
  1780. if(nread < 0) {
  1781. if(result != CURLE_AGAIN) {
  1782. failf(data, "Failed receiving HTTP2 data: %d(%s)", result,
  1783. curl_easy_strerror(result));
  1784. return result;
  1785. }
  1786. break;
  1787. }
  1788. else if(nread == 0) {
  1789. CURL_TRC_CF(data, cf, "[0] ingress: connection closed");
  1790. ctx->conn_closed = TRUE;
  1791. break;
  1792. }
  1793. else {
  1794. CURL_TRC_CF(data, cf, "[0] ingress: read %zd bytes", nread);
  1795. data_max_bytes = (data_max_bytes > (size_t)nread) ?
  1796. (data_max_bytes - (size_t)nread) : 0;
  1797. }
  1798. if(h2_process_pending_input(cf, data, &result))
  1799. return result;
  1800. CURL_TRC_CF(data, cf, "[0] progress ingress: inbufg=%zu",
  1801. Curl_bufq_len(&ctx->inbufq));
  1802. }
  1803. if(ctx->conn_closed && Curl_bufq_is_empty(&ctx->inbufq)) {
  1804. connclose(cf->conn, "GOAWAY received");
  1805. }
  1806. CURL_TRC_CF(data, cf, "[0] progress ingress: done");
  1807. return CURLE_OK;
  1808. }
  1809. static ssize_t cf_h2_recv(struct Curl_cfilter *cf, struct Curl_easy *data,
  1810. char *buf, size_t len, CURLcode *err)
  1811. {
  1812. struct cf_h2_ctx *ctx = cf->ctx;
  1813. struct h2_stream_ctx *stream = H2_STREAM_CTX(ctx, data);
  1814. ssize_t nread = -1;
  1815. CURLcode result;
  1816. struct cf_call_data save;
  1817. if(!stream) {
  1818. /* Abnormal call sequence: either this transfer has never opened a stream
  1819. * (unlikely) or the transfer has been done, cleaned up its resources, but
  1820. * a read() is called anyway. It is not clear what the calling sequence
  1821. * is for such a case. */
  1822. failf(data, "http/2 recv on a transfer never opened "
  1823. "or already cleared, mid=%" FMT_OFF_T, data->mid);
  1824. *err = CURLE_HTTP2;
  1825. return -1;
  1826. }
  1827. CF_DATA_SAVE(save, cf, data);
  1828. nread = stream_recv(cf, data, stream, buf, len, err);
  1829. if(nread < 0 && *err != CURLE_AGAIN)
  1830. goto out;
  1831. if(nread < 0) {
  1832. *err = h2_progress_ingress(cf, data, len);
  1833. if(*err)
  1834. goto out;
  1835. nread = stream_recv(cf, data, stream, buf, len, err);
  1836. }
  1837. if(nread > 0) {
  1838. size_t data_consumed = (size_t)nread;
  1839. /* Now that we transferred this to the upper layer, we report
  1840. * the actual amount of DATA consumed to the H2 session, so
  1841. * that it adjusts stream flow control */
  1842. if(stream->resp_hds_len >= data_consumed) {
  1843. stream->resp_hds_len -= data_consumed; /* no DATA */
  1844. }
  1845. else {
  1846. if(stream->resp_hds_len) {
  1847. data_consumed -= stream->resp_hds_len;
  1848. stream->resp_hds_len = 0;
  1849. }
  1850. if(data_consumed) {
  1851. nghttp2_session_consume(ctx->h2, stream->id, data_consumed);
  1852. }
  1853. }
  1854. if(stream->closed) {
  1855. CURL_TRC_CF(data, cf, "[%d] DRAIN closed stream", stream->id);
  1856. drain_stream(cf, data, stream);
  1857. }
  1858. }
  1859. out:
  1860. result = h2_progress_egress(cf, data);
  1861. if(result == CURLE_AGAIN) {
  1862. /* pending data to send, need to be called again. Ideally, we
  1863. * monitor the socket for POLLOUT, but when not SENDING
  1864. * any more, we force processing of the transfer. */
  1865. if(!CURL_WANT_SEND(data))
  1866. drain_stream(cf, data, stream);
  1867. }
  1868. else if(result) {
  1869. *err = result;
  1870. nread = -1;
  1871. }
  1872. CURL_TRC_CF(data, cf, "[%d] cf_recv(len=%zu) -> %zd %d, "
  1873. "window=%d/%d, connection %d/%d",
  1874. stream->id, len, nread, *err,
  1875. nghttp2_session_get_stream_effective_recv_data_length(
  1876. ctx->h2, stream->id),
  1877. nghttp2_session_get_stream_effective_local_window_size(
  1878. ctx->h2, stream->id),
  1879. nghttp2_session_get_local_window_size(ctx->h2),
  1880. HTTP2_HUGE_WINDOW_SIZE);
  1881. CF_DATA_RESTORE(cf, save);
  1882. return nread;
  1883. }
  1884. static ssize_t cf_h2_body_send(struct Curl_cfilter *cf,
  1885. struct Curl_easy *data,
  1886. struct h2_stream_ctx *stream,
  1887. const void *buf, size_t blen, bool eos,
  1888. CURLcode *err)
  1889. {
  1890. struct cf_h2_ctx *ctx = cf->ctx;
  1891. ssize_t nwritten;
  1892. if(stream->closed) {
  1893. if(stream->resp_hds_complete) {
  1894. /* Server decided to close the stream after having sent us a final
  1895. * response. This is valid if it is not interested in the request
  1896. * body. This happens on 30x or 40x responses.
  1897. * We silently discard the data sent, since this is not a transport
  1898. * error situation. */
  1899. CURL_TRC_CF(data, cf, "[%d] discarding data"
  1900. "on closed stream with response", stream->id);
  1901. if(eos)
  1902. stream->body_eos = TRUE;
  1903. *err = CURLE_OK;
  1904. return (ssize_t)blen;
  1905. }
  1906. /* Server closed before we got a response, this is an error */
  1907. infof(data, "stream %u closed", stream->id);
  1908. *err = CURLE_SEND_ERROR;
  1909. return -1;
  1910. }
  1911. nwritten = Curl_bufq_write(&stream->sendbuf, buf, blen, err);
  1912. if(nwritten < 0)
  1913. return -1;
  1914. if(eos && (blen == (size_t)nwritten))
  1915. stream->body_eos = TRUE;
  1916. if(eos || !Curl_bufq_is_empty(&stream->sendbuf)) {
  1917. /* resume the potentially suspended stream */
  1918. int rv = nghttp2_session_resume_data(ctx->h2, stream->id);
  1919. if(nghttp2_is_fatal(rv)) {
  1920. *err = CURLE_SEND_ERROR;
  1921. return -1;
  1922. }
  1923. }
  1924. return nwritten;
  1925. }
  1926. static ssize_t h2_submit(struct h2_stream_ctx **pstream,
  1927. struct Curl_cfilter *cf, struct Curl_easy *data,
  1928. const void *buf, size_t len,
  1929. bool eos, CURLcode *err)
  1930. {
  1931. struct cf_h2_ctx *ctx = cf->ctx;
  1932. struct h2_stream_ctx *stream = NULL;
  1933. struct dynhds h2_headers;
  1934. nghttp2_nv *nva = NULL;
  1935. const void *body = NULL;
  1936. size_t nheader, bodylen, i;
  1937. nghttp2_data_provider data_prd;
  1938. int32_t stream_id;
  1939. nghttp2_priority_spec pri_spec;
  1940. ssize_t nwritten;
  1941. Curl_dynhds_init(&h2_headers, 0, DYN_HTTP_REQUEST);
  1942. *err = http2_data_setup(cf, data, &stream);
  1943. if(*err) {
  1944. nwritten = -1;
  1945. goto out;
  1946. }
  1947. nwritten = Curl_h1_req_parse_read(&stream->h1, buf, len, NULL, 0, err);
  1948. if(nwritten < 0)
  1949. goto out;
  1950. if(!stream->h1.done) {
  1951. /* need more data */
  1952. goto out;
  1953. }
  1954. DEBUGASSERT(stream->h1.req);
  1955. *err = Curl_http_req_to_h2(&h2_headers, stream->h1.req, data);
  1956. if(*err) {
  1957. nwritten = -1;
  1958. goto out;
  1959. }
  1960. /* no longer needed */
  1961. Curl_h1_req_parse_free(&stream->h1);
  1962. nva = Curl_dynhds_to_nva(&h2_headers, &nheader);
  1963. if(!nva) {
  1964. *err = CURLE_OUT_OF_MEMORY;
  1965. nwritten = -1;
  1966. goto out;
  1967. }
  1968. h2_pri_spec(ctx, data, &pri_spec);
  1969. if(!nghttp2_session_check_request_allowed(ctx->h2))
  1970. CURL_TRC_CF(data, cf, "send request NOT allowed (via nghttp2)");
  1971. switch(data->state.httpreq) {
  1972. case HTTPREQ_POST:
  1973. case HTTPREQ_POST_FORM:
  1974. case HTTPREQ_POST_MIME:
  1975. case HTTPREQ_PUT:
  1976. data_prd.read_callback = req_body_read_callback;
  1977. data_prd.source.ptr = NULL;
  1978. stream_id = nghttp2_submit_request(ctx->h2, &pri_spec, nva, nheader,
  1979. &data_prd, data);
  1980. break;
  1981. default:
  1982. stream_id = nghttp2_submit_request(ctx->h2, &pri_spec, nva, nheader,
  1983. NULL, data);
  1984. }
  1985. if(stream_id < 0) {
  1986. CURL_TRC_CF(data, cf, "send: nghttp2_submit_request error (%s)%u",
  1987. nghttp2_strerror(stream_id), stream_id);
  1988. *err = CURLE_SEND_ERROR;
  1989. nwritten = -1;
  1990. goto out;
  1991. }
  1992. #define MAX_ACC 60000 /* <64KB to account for some overhead */
  1993. if(Curl_trc_is_verbose(data)) {
  1994. size_t acc = 0;
  1995. infof(data, "[HTTP/2] [%d] OPENED stream for %s",
  1996. stream_id, data->state.url);
  1997. for(i = 0; i < nheader; ++i) {
  1998. acc += nva[i].namelen + nva[i].valuelen;
  1999. infof(data, "[HTTP/2] [%d] [%.*s: %.*s]", stream_id,
  2000. (int)nva[i].namelen, nva[i].name,
  2001. (int)nva[i].valuelen, nva[i].value);
  2002. }
  2003. if(acc > MAX_ACC) {
  2004. infof(data, "[HTTP/2] Warning: The cumulative length of all "
  2005. "headers exceeds %d bytes and that could cause the "
  2006. "stream to be rejected.", MAX_ACC);
  2007. }
  2008. }
  2009. stream->id = stream_id;
  2010. body = (const char *)buf + nwritten;
  2011. bodylen = len - nwritten;
  2012. if(bodylen || eos) {
  2013. ssize_t n = cf_h2_body_send(cf, data, stream, body, bodylen, eos, err);
  2014. if(n >= 0)
  2015. nwritten += n;
  2016. else if(*err == CURLE_AGAIN)
  2017. *err = CURLE_OK;
  2018. else if(*err != CURLE_AGAIN) {
  2019. *err = CURLE_SEND_ERROR;
  2020. nwritten = -1;
  2021. goto out;
  2022. }
  2023. }
  2024. out:
  2025. CURL_TRC_CF(data, cf, "[%d] submit -> %zd, %d",
  2026. stream ? stream->id : -1, nwritten, *err);
  2027. Curl_safefree(nva);
  2028. *pstream = stream;
  2029. Curl_dynhds_free(&h2_headers);
  2030. return nwritten;
  2031. }
  2032. static ssize_t cf_h2_send(struct Curl_cfilter *cf, struct Curl_easy *data,
  2033. const void *buf, size_t len, bool eos,
  2034. CURLcode *err)
  2035. {
  2036. struct cf_h2_ctx *ctx = cf->ctx;
  2037. struct h2_stream_ctx *stream = H2_STREAM_CTX(ctx, data);
  2038. struct cf_call_data save;
  2039. ssize_t nwritten;
  2040. CURLcode result;
  2041. CF_DATA_SAVE(save, cf, data);
  2042. if(!stream || stream->id == -1) {
  2043. nwritten = h2_submit(&stream, cf, data, buf, len, eos, err);
  2044. if(nwritten < 0) {
  2045. goto out;
  2046. }
  2047. DEBUGASSERT(stream);
  2048. }
  2049. else if(stream->body_eos) {
  2050. /* We already wrote this, but CURLE_AGAINed the call due to not
  2051. * being able to flush stream->sendbuf. Make a 0-length write
  2052. * to trigger flushing again.
  2053. * If this works, we report to have written `len` bytes. */
  2054. DEBUGASSERT(eos);
  2055. nwritten = cf_h2_body_send(cf, data, stream, buf, 0, eos, err);
  2056. CURL_TRC_CF(data, cf, "[%d] cf_body_send last CHUNK -> %zd, %d, eos=%d",
  2057. stream->id, nwritten, *err, eos);
  2058. if(nwritten < 0) {
  2059. goto out;
  2060. }
  2061. nwritten = len;
  2062. }
  2063. else {
  2064. nwritten = cf_h2_body_send(cf, data, stream, buf, len, eos, err);
  2065. CURL_TRC_CF(data, cf, "[%d] cf_body_send(len=%zu) -> %zd, %d, eos=%d",
  2066. stream->id, len, nwritten, *err, eos);
  2067. }
  2068. /* Call the nghttp2 send loop and flush to write ALL buffered data,
  2069. * headers and/or request body completely out to the network */
  2070. result = h2_progress_egress(cf, data);
  2071. /* if the stream has been closed in egress handling (nghttp2 does that
  2072. * when it does not like the headers, for example */
  2073. if(stream && stream->closed) {
  2074. infof(data, "stream %u closed", stream->id);
  2075. *err = CURLE_SEND_ERROR;
  2076. nwritten = -1;
  2077. goto out;
  2078. }
  2079. else if(result && (result != CURLE_AGAIN)) {
  2080. *err = result;
  2081. nwritten = -1;
  2082. goto out;
  2083. }
  2084. if(should_close_session(ctx)) {
  2085. /* nghttp2 thinks this session is done. If the stream has not been
  2086. * closed, this is an error state for out transfer */
  2087. if(stream->closed) {
  2088. nwritten = http2_handle_stream_close(cf, data, stream, err);
  2089. }
  2090. else {
  2091. CURL_TRC_CF(data, cf, "send: nothing to do in this session");
  2092. *err = CURLE_HTTP2;
  2093. nwritten = -1;
  2094. }
  2095. }
  2096. out:
  2097. if(stream) {
  2098. CURL_TRC_CF(data, cf, "[%d] cf_send(len=%zu) -> %zd, %d, "
  2099. "eos=%d, h2 windows %d-%d (stream-conn), "
  2100. "buffers %zu-%zu (stream-conn)",
  2101. stream->id, len, nwritten, *err,
  2102. stream->body_eos,
  2103. nghttp2_session_get_stream_remote_window_size(
  2104. ctx->h2, stream->id),
  2105. nghttp2_session_get_remote_window_size(ctx->h2),
  2106. Curl_bufq_len(&stream->sendbuf),
  2107. Curl_bufq_len(&ctx->outbufq));
  2108. }
  2109. else {
  2110. CURL_TRC_CF(data, cf, "cf_send(len=%zu) -> %zd, %d, "
  2111. "connection-window=%d, nw_send_buffer(%zu)",
  2112. len, nwritten, *err,
  2113. nghttp2_session_get_remote_window_size(ctx->h2),
  2114. Curl_bufq_len(&ctx->outbufq));
  2115. }
  2116. CF_DATA_RESTORE(cf, save);
  2117. return nwritten;
  2118. }
  2119. static CURLcode cf_h2_flush(struct Curl_cfilter *cf,
  2120. struct Curl_easy *data)
  2121. {
  2122. struct cf_h2_ctx *ctx = cf->ctx;
  2123. struct h2_stream_ctx *stream = H2_STREAM_CTX(ctx, data);
  2124. struct cf_call_data save;
  2125. CURLcode result = CURLE_OK;
  2126. CF_DATA_SAVE(save, cf, data);
  2127. if(stream && !Curl_bufq_is_empty(&stream->sendbuf)) {
  2128. /* resume the potentially suspended stream */
  2129. int rv = nghttp2_session_resume_data(ctx->h2, stream->id);
  2130. if(nghttp2_is_fatal(rv)) {
  2131. result = CURLE_SEND_ERROR;
  2132. goto out;
  2133. }
  2134. }
  2135. result = h2_progress_egress(cf, data);
  2136. out:
  2137. if(stream) {
  2138. CURL_TRC_CF(data, cf, "[%d] flush -> %d, "
  2139. "h2 windows %d-%d (stream-conn), "
  2140. "buffers %zu-%zu (stream-conn)",
  2141. stream->id, result,
  2142. nghttp2_session_get_stream_remote_window_size(
  2143. ctx->h2, stream->id),
  2144. nghttp2_session_get_remote_window_size(ctx->h2),
  2145. Curl_bufq_len(&stream->sendbuf),
  2146. Curl_bufq_len(&ctx->outbufq));
  2147. }
  2148. else {
  2149. CURL_TRC_CF(data, cf, "flush -> %d, "
  2150. "connection-window=%d, nw_send_buffer(%zu)",
  2151. result, nghttp2_session_get_remote_window_size(ctx->h2),
  2152. Curl_bufq_len(&ctx->outbufq));
  2153. }
  2154. CF_DATA_RESTORE(cf, save);
  2155. return result;
  2156. }
  2157. static void cf_h2_adjust_pollset(struct Curl_cfilter *cf,
  2158. struct Curl_easy *data,
  2159. struct easy_pollset *ps)
  2160. {
  2161. struct cf_h2_ctx *ctx = cf->ctx;
  2162. struct cf_call_data save;
  2163. curl_socket_t sock;
  2164. bool want_recv, want_send;
  2165. if(!ctx->h2)
  2166. return;
  2167. sock = Curl_conn_cf_get_socket(cf, data);
  2168. Curl_pollset_check(data, ps, sock, &want_recv, &want_send);
  2169. if(want_recv || want_send) {
  2170. struct h2_stream_ctx *stream = H2_STREAM_CTX(ctx, data);
  2171. bool c_exhaust, s_exhaust;
  2172. CF_DATA_SAVE(save, cf, data);
  2173. c_exhaust = want_send && !nghttp2_session_get_remote_window_size(ctx->h2);
  2174. s_exhaust = want_send && stream && stream->id >= 0 &&
  2175. !nghttp2_session_get_stream_remote_window_size(ctx->h2,
  2176. stream->id);
  2177. want_recv = (want_recv || c_exhaust || s_exhaust);
  2178. want_send = (!s_exhaust && want_send) ||
  2179. (!c_exhaust && nghttp2_session_want_write(ctx->h2)) ||
  2180. !Curl_bufq_is_empty(&ctx->outbufq);
  2181. Curl_pollset_set(data, ps, sock, want_recv, want_send);
  2182. CF_DATA_RESTORE(cf, save);
  2183. }
  2184. else if(ctx->sent_goaway && !cf->shutdown) {
  2185. /* shutdown in progress */
  2186. CF_DATA_SAVE(save, cf, data);
  2187. want_send = nghttp2_session_want_write(ctx->h2) ||
  2188. !Curl_bufq_is_empty(&ctx->outbufq);
  2189. want_recv = nghttp2_session_want_read(ctx->h2);
  2190. Curl_pollset_set(data, ps, sock, want_recv, want_send);
  2191. CF_DATA_RESTORE(cf, save);
  2192. }
  2193. }
  2194. static CURLcode cf_h2_connect(struct Curl_cfilter *cf,
  2195. struct Curl_easy *data,
  2196. bool blocking, bool *done)
  2197. {
  2198. struct cf_h2_ctx *ctx = cf->ctx;
  2199. CURLcode result = CURLE_OK;
  2200. struct cf_call_data save;
  2201. bool first_time = FALSE;
  2202. if(cf->connected) {
  2203. *done = TRUE;
  2204. return CURLE_OK;
  2205. }
  2206. /* Connect the lower filters first */
  2207. if(!cf->next->connected) {
  2208. result = Curl_conn_cf_connect(cf->next, data, blocking, done);
  2209. if(result || !*done)
  2210. return result;
  2211. }
  2212. *done = FALSE;
  2213. CF_DATA_SAVE(save, cf, data);
  2214. DEBUGASSERT(ctx->initialized);
  2215. if(!ctx->h2) {
  2216. result = cf_h2_ctx_open(cf, data);
  2217. if(result)
  2218. goto out;
  2219. first_time = TRUE;
  2220. }
  2221. if(!first_time) {
  2222. result = h2_progress_ingress(cf, data, H2_CHUNK_SIZE);
  2223. if(result)
  2224. goto out;
  2225. }
  2226. /* Send out our SETTINGS and ACKs and such. If that blocks, we
  2227. * have it buffered and can count this filter as being connected */
  2228. result = h2_progress_egress(cf, data);
  2229. if(result == CURLE_AGAIN)
  2230. result = CURLE_OK;
  2231. else if(result)
  2232. goto out;
  2233. *done = TRUE;
  2234. cf->connected = TRUE;
  2235. result = CURLE_OK;
  2236. out:
  2237. CURL_TRC_CF(data, cf, "cf_connect() -> %d, %d, ", result, *done);
  2238. CF_DATA_RESTORE(cf, save);
  2239. return result;
  2240. }
  2241. static void cf_h2_close(struct Curl_cfilter *cf, struct Curl_easy *data)
  2242. {
  2243. struct cf_h2_ctx *ctx = cf->ctx;
  2244. if(ctx) {
  2245. struct cf_call_data save;
  2246. CF_DATA_SAVE(save, cf, data);
  2247. cf_h2_ctx_close(ctx);
  2248. CF_DATA_RESTORE(cf, save);
  2249. cf->connected = FALSE;
  2250. }
  2251. if(cf->next)
  2252. cf->next->cft->do_close(cf->next, data);
  2253. }
  2254. static void cf_h2_destroy(struct Curl_cfilter *cf, struct Curl_easy *data)
  2255. {
  2256. struct cf_h2_ctx *ctx = cf->ctx;
  2257. (void)data;
  2258. if(ctx) {
  2259. cf_h2_ctx_free(ctx);
  2260. cf->ctx = NULL;
  2261. }
  2262. }
  2263. static CURLcode cf_h2_shutdown(struct Curl_cfilter *cf,
  2264. struct Curl_easy *data, bool *done)
  2265. {
  2266. struct cf_h2_ctx *ctx = cf->ctx;
  2267. struct cf_call_data save;
  2268. CURLcode result;
  2269. int rv;
  2270. if(!cf->connected || !ctx->h2 || cf->shutdown || ctx->conn_closed) {
  2271. *done = TRUE;
  2272. return CURLE_OK;
  2273. }
  2274. CF_DATA_SAVE(save, cf, data);
  2275. if(!ctx->sent_goaway) {
  2276. rv = nghttp2_submit_goaway(ctx->h2, NGHTTP2_FLAG_NONE,
  2277. ctx->local_max_sid, 0,
  2278. (const uint8_t *)"shutdown",
  2279. sizeof("shutdown"));
  2280. if(rv) {
  2281. failf(data, "nghttp2_submit_goaway() failed: %s(%d)",
  2282. nghttp2_strerror(rv), rv);
  2283. result = CURLE_SEND_ERROR;
  2284. goto out;
  2285. }
  2286. ctx->sent_goaway = TRUE;
  2287. }
  2288. /* GOAWAY submitted, process egress and ingress until nghttp2 is done. */
  2289. result = CURLE_OK;
  2290. if(nghttp2_session_want_write(ctx->h2) ||
  2291. !Curl_bufq_is_empty(&ctx->outbufq))
  2292. result = h2_progress_egress(cf, data);
  2293. if(!result && nghttp2_session_want_read(ctx->h2))
  2294. result = h2_progress_ingress(cf, data, 0);
  2295. if(result == CURLE_AGAIN)
  2296. result = CURLE_OK;
  2297. *done = (ctx->conn_closed ||
  2298. (!result && !nghttp2_session_want_write(ctx->h2) &&
  2299. !nghttp2_session_want_read(ctx->h2) &&
  2300. Curl_bufq_is_empty(&ctx->outbufq)));
  2301. out:
  2302. CF_DATA_RESTORE(cf, save);
  2303. cf->shutdown = (result || *done);
  2304. return result;
  2305. }
  2306. static CURLcode http2_data_pause(struct Curl_cfilter *cf,
  2307. struct Curl_easy *data,
  2308. bool pause)
  2309. {
  2310. struct cf_h2_ctx *ctx = cf->ctx;
  2311. struct h2_stream_ctx *stream = H2_STREAM_CTX(ctx, data);
  2312. DEBUGASSERT(data);
  2313. if(ctx && ctx->h2 && stream) {
  2314. CURLcode result = cf_h2_update_local_win(cf, data, stream, pause);
  2315. if(result)
  2316. return result;
  2317. /* attempt to send the window update */
  2318. (void)h2_progress_egress(cf, data);
  2319. if(!pause) {
  2320. /* Unpausing a h2 transfer, requires it to be run again. The server
  2321. * may send new DATA on us increasing the flow window, and it may
  2322. * not. We may have already buffered and exhausted the new window
  2323. * by operating on things in flight during the handling of other
  2324. * transfers. */
  2325. drain_stream(cf, data, stream);
  2326. Curl_expire(data, 0, EXPIRE_RUN_NOW);
  2327. }
  2328. CURL_TRC_CF(data, cf, "[%d] stream now %spaused", stream->id,
  2329. pause ? "" : "un");
  2330. }
  2331. return CURLE_OK;
  2332. }
  2333. static CURLcode cf_h2_cntrl(struct Curl_cfilter *cf,
  2334. struct Curl_easy *data,
  2335. int event, int arg1, void *arg2)
  2336. {
  2337. CURLcode result = CURLE_OK;
  2338. struct cf_call_data save;
  2339. (void)arg2;
  2340. CF_DATA_SAVE(save, cf, data);
  2341. switch(event) {
  2342. case CF_CTRL_DATA_SETUP:
  2343. break;
  2344. case CF_CTRL_DATA_PAUSE:
  2345. result = http2_data_pause(cf, data, (arg1 != 0));
  2346. break;
  2347. case CF_CTRL_FLUSH:
  2348. result = cf_h2_flush(cf, data);
  2349. break;
  2350. case CF_CTRL_DATA_DETACH:
  2351. http2_data_done(cf, data);
  2352. break;
  2353. case CF_CTRL_DATA_DONE:
  2354. http2_data_done(cf, data);
  2355. break;
  2356. default:
  2357. break;
  2358. }
  2359. CF_DATA_RESTORE(cf, save);
  2360. return result;
  2361. }
  2362. static bool cf_h2_data_pending(struct Curl_cfilter *cf,
  2363. const struct Curl_easy *data)
  2364. {
  2365. struct cf_h2_ctx *ctx = cf->ctx;
  2366. struct h2_stream_ctx *stream = H2_STREAM_CTX(ctx, data);
  2367. if(ctx && (!Curl_bufq_is_empty(&ctx->inbufq)
  2368. || (stream && !Curl_bufq_is_empty(&stream->sendbuf))))
  2369. return TRUE;
  2370. return cf->next ? cf->next->cft->has_data_pending(cf->next, data) : FALSE;
  2371. }
  2372. static bool cf_h2_is_alive(struct Curl_cfilter *cf,
  2373. struct Curl_easy *data,
  2374. bool *input_pending)
  2375. {
  2376. struct cf_h2_ctx *ctx = cf->ctx;
  2377. CURLcode result;
  2378. struct cf_call_data save;
  2379. CF_DATA_SAVE(save, cf, data);
  2380. result = (ctx && ctx->h2 && http2_connisalive(cf, data, input_pending));
  2381. CURL_TRC_CF(data, cf, "conn alive -> %d, input_pending=%d",
  2382. result, *input_pending);
  2383. CF_DATA_RESTORE(cf, save);
  2384. return result;
  2385. }
  2386. static CURLcode cf_h2_keep_alive(struct Curl_cfilter *cf,
  2387. struct Curl_easy *data)
  2388. {
  2389. CURLcode result;
  2390. struct cf_call_data save;
  2391. CF_DATA_SAVE(save, cf, data);
  2392. result = http2_send_ping(cf, data);
  2393. CF_DATA_RESTORE(cf, save);
  2394. return result;
  2395. }
  2396. static CURLcode cf_h2_query(struct Curl_cfilter *cf,
  2397. struct Curl_easy *data,
  2398. int query, int *pres1, void *pres2)
  2399. {
  2400. struct cf_h2_ctx *ctx = cf->ctx;
  2401. struct cf_call_data save;
  2402. size_t effective_max;
  2403. switch(query) {
  2404. case CF_QUERY_MAX_CONCURRENT:
  2405. DEBUGASSERT(pres1);
  2406. CF_DATA_SAVE(save, cf, data);
  2407. if(nghttp2_session_check_request_allowed(ctx->h2) == 0) {
  2408. /* the limit is what we have in use right now */
  2409. effective_max = CONN_INUSE(cf->conn);
  2410. }
  2411. else {
  2412. effective_max = ctx->max_concurrent_streams;
  2413. }
  2414. *pres1 = (effective_max > INT_MAX) ? INT_MAX : (int)effective_max;
  2415. CF_DATA_RESTORE(cf, save);
  2416. return CURLE_OK;
  2417. case CF_QUERY_STREAM_ERROR: {
  2418. struct h2_stream_ctx *stream = H2_STREAM_CTX(ctx, data);
  2419. *pres1 = stream ? (int)stream->error : 0;
  2420. return CURLE_OK;
  2421. }
  2422. case CF_QUERY_NEED_FLUSH: {
  2423. struct h2_stream_ctx *stream = H2_STREAM_CTX(ctx, data);
  2424. if(!Curl_bufq_is_empty(&ctx->outbufq) ||
  2425. (stream && !Curl_bufq_is_empty(&stream->sendbuf))) {
  2426. *pres1 = TRUE;
  2427. return CURLE_OK;
  2428. }
  2429. break;
  2430. }
  2431. default:
  2432. break;
  2433. }
  2434. return cf->next ?
  2435. cf->next->cft->query(cf->next, data, query, pres1, pres2) :
  2436. CURLE_UNKNOWN_OPTION;
  2437. }
  2438. struct Curl_cftype Curl_cft_nghttp2 = {
  2439. "HTTP/2",
  2440. CF_TYPE_MULTIPLEX,
  2441. CURL_LOG_LVL_NONE,
  2442. cf_h2_destroy,
  2443. cf_h2_connect,
  2444. cf_h2_close,
  2445. cf_h2_shutdown,
  2446. Curl_cf_def_get_host,
  2447. cf_h2_adjust_pollset,
  2448. cf_h2_data_pending,
  2449. cf_h2_send,
  2450. cf_h2_recv,
  2451. cf_h2_cntrl,
  2452. cf_h2_is_alive,
  2453. cf_h2_keep_alive,
  2454. cf_h2_query,
  2455. };
  2456. static CURLcode http2_cfilter_add(struct Curl_cfilter **pcf,
  2457. struct Curl_easy *data,
  2458. struct connectdata *conn,
  2459. int sockindex,
  2460. bool via_h1_upgrade)
  2461. {
  2462. struct Curl_cfilter *cf = NULL;
  2463. struct cf_h2_ctx *ctx;
  2464. CURLcode result = CURLE_OUT_OF_MEMORY;
  2465. DEBUGASSERT(data->conn);
  2466. ctx = calloc(1, sizeof(*ctx));
  2467. if(!ctx)
  2468. goto out;
  2469. cf_h2_ctx_init(ctx, via_h1_upgrade);
  2470. result = Curl_cf_create(&cf, &Curl_cft_nghttp2, ctx);
  2471. if(result)
  2472. goto out;
  2473. ctx = NULL;
  2474. Curl_conn_cf_add(data, conn, sockindex, cf);
  2475. out:
  2476. if(result)
  2477. cf_h2_ctx_free(ctx);
  2478. *pcf = result ? NULL : cf;
  2479. return result;
  2480. }
  2481. static CURLcode http2_cfilter_insert_after(struct Curl_cfilter *cf,
  2482. struct Curl_easy *data,
  2483. bool via_h1_upgrade)
  2484. {
  2485. struct Curl_cfilter *cf_h2 = NULL;
  2486. struct cf_h2_ctx *ctx;
  2487. CURLcode result = CURLE_OUT_OF_MEMORY;
  2488. (void)data;
  2489. ctx = calloc(1, sizeof(*ctx));
  2490. if(!ctx)
  2491. goto out;
  2492. cf_h2_ctx_init(ctx, via_h1_upgrade);
  2493. result = Curl_cf_create(&cf_h2, &Curl_cft_nghttp2, ctx);
  2494. if(result)
  2495. goto out;
  2496. ctx = NULL;
  2497. Curl_conn_cf_insert_after(cf, cf_h2);
  2498. out:
  2499. if(result)
  2500. cf_h2_ctx_free(ctx);
  2501. return result;
  2502. }
  2503. static bool Curl_cf_is_http2(struct Curl_cfilter *cf,
  2504. const struct Curl_easy *data)
  2505. {
  2506. (void)data;
  2507. for(; cf; cf = cf->next) {
  2508. if(cf->cft == &Curl_cft_nghttp2)
  2509. return TRUE;
  2510. if(cf->cft->flags & CF_TYPE_IP_CONNECT)
  2511. return FALSE;
  2512. }
  2513. return FALSE;
  2514. }
  2515. bool Curl_conn_is_http2(const struct Curl_easy *data,
  2516. const struct connectdata *conn,
  2517. int sockindex)
  2518. {
  2519. return conn ? Curl_cf_is_http2(conn->cfilter[sockindex], data) : FALSE;
  2520. }
  2521. bool Curl_http2_may_switch(struct Curl_easy *data,
  2522. struct connectdata *conn,
  2523. int sockindex)
  2524. {
  2525. (void)sockindex;
  2526. if(!Curl_conn_is_http2(data, conn, sockindex) &&
  2527. data->state.httpwant == CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE) {
  2528. #ifndef CURL_DISABLE_PROXY
  2529. if(conn->bits.httpproxy && !conn->bits.tunnel_proxy) {
  2530. /* We do not support HTTP/2 proxies yet. Also it is debatable
  2531. whether or not this setting should apply to HTTP/2 proxies. */
  2532. infof(data, "Ignoring HTTP/2 prior knowledge due to proxy");
  2533. return FALSE;
  2534. }
  2535. #endif
  2536. return TRUE;
  2537. }
  2538. return FALSE;
  2539. }
  2540. CURLcode Curl_http2_switch(struct Curl_easy *data,
  2541. struct connectdata *conn, int sockindex)
  2542. {
  2543. struct Curl_cfilter *cf;
  2544. CURLcode result;
  2545. DEBUGASSERT(!Curl_conn_is_http2(data, conn, sockindex));
  2546. result = http2_cfilter_add(&cf, data, conn, sockindex, FALSE);
  2547. if(result)
  2548. return result;
  2549. CURL_TRC_CF(data, cf, "switching connection to HTTP/2");
  2550. conn->httpversion = 20; /* we know we are on HTTP/2 now */
  2551. conn->bits.multiplex = TRUE; /* at least potentially multiplexed */
  2552. Curl_multi_connchanged(data->multi);
  2553. if(cf->next) {
  2554. bool done;
  2555. return Curl_conn_cf_connect(cf, data, FALSE, &done);
  2556. }
  2557. return CURLE_OK;
  2558. }
  2559. CURLcode Curl_http2_switch_at(struct Curl_cfilter *cf, struct Curl_easy *data)
  2560. {
  2561. struct Curl_cfilter *cf_h2;
  2562. CURLcode result;
  2563. DEBUGASSERT(!Curl_cf_is_http2(cf, data));
  2564. result = http2_cfilter_insert_after(cf, data, FALSE);
  2565. if(result)
  2566. return result;
  2567. cf_h2 = cf->next;
  2568. cf->conn->httpversion = 20; /* we know we are on HTTP/2 now */
  2569. cf->conn->bits.multiplex = TRUE; /* at least potentially multiplexed */
  2570. Curl_multi_connchanged(data->multi);
  2571. if(cf_h2->next) {
  2572. bool done;
  2573. return Curl_conn_cf_connect(cf_h2, data, FALSE, &done);
  2574. }
  2575. return CURLE_OK;
  2576. }
  2577. CURLcode Curl_http2_upgrade(struct Curl_easy *data,
  2578. struct connectdata *conn, int sockindex,
  2579. const char *mem, size_t nread)
  2580. {
  2581. struct Curl_cfilter *cf;
  2582. struct cf_h2_ctx *ctx;
  2583. CURLcode result;
  2584. DEBUGASSERT(!Curl_conn_is_http2(data, conn, sockindex));
  2585. DEBUGASSERT(data->req.upgr101 == UPGR101_RECEIVED);
  2586. result = http2_cfilter_add(&cf, data, conn, sockindex, TRUE);
  2587. if(result)
  2588. return result;
  2589. CURL_TRC_CF(data, cf, "upgrading connection to HTTP/2");
  2590. DEBUGASSERT(cf->cft == &Curl_cft_nghttp2);
  2591. ctx = cf->ctx;
  2592. if(nread > 0) {
  2593. /* Remaining data from the protocol switch reply is already using
  2594. * the switched protocol, ie. HTTP/2. We add that to the network
  2595. * inbufq. */
  2596. ssize_t copied;
  2597. copied = Curl_bufq_write(&ctx->inbufq,
  2598. (const unsigned char *)mem, nread, &result);
  2599. if(copied < 0) {
  2600. failf(data, "error on copying HTTP Upgrade response: %d", result);
  2601. return CURLE_RECV_ERROR;
  2602. }
  2603. if((size_t)copied < nread) {
  2604. failf(data, "connection buffer size could not take all data "
  2605. "from HTTP Upgrade response header: copied=%zd, datalen=%zu",
  2606. copied, nread);
  2607. return CURLE_HTTP2;
  2608. }
  2609. infof(data, "Copied HTTP/2 data in stream buffer to connection buffer"
  2610. " after upgrade: len=%zu", nread);
  2611. }
  2612. conn->httpversion = 20; /* we know we are on HTTP/2 now */
  2613. conn->bits.multiplex = TRUE; /* at least potentially multiplexed */
  2614. Curl_multi_connchanged(data->multi);
  2615. if(cf->next) {
  2616. bool done;
  2617. return Curl_conn_cf_connect(cf, data, FALSE, &done);
  2618. }
  2619. return CURLE_OK;
  2620. }
  2621. /* Only call this function for a transfer that already got an HTTP/2
  2622. CURLE_HTTP2_STREAM error! */
  2623. bool Curl_h2_http_1_1_error(struct Curl_easy *data)
  2624. {
  2625. if(Curl_conn_is_http2(data, data->conn, FIRSTSOCKET)) {
  2626. int err = Curl_conn_get_stream_error(data, data->conn, FIRSTSOCKET);
  2627. return (err == NGHTTP2_HTTP_1_1_REQUIRED);
  2628. }
  2629. return FALSE;
  2630. }
  2631. #else /* !USE_NGHTTP2 */
  2632. /* Satisfy external references even if http2 is not compiled in. */
  2633. #include <curl/curl.h>
  2634. char *curl_pushheader_bynum(struct curl_pushheaders *h, size_t num)
  2635. {
  2636. (void) h;
  2637. (void) num;
  2638. return NULL;
  2639. }
  2640. char *curl_pushheader_byname(struct curl_pushheaders *h, const char *header)
  2641. {
  2642. (void) h;
  2643. (void) header;
  2644. return NULL;
  2645. }
  2646. #endif /* USE_NGHTTP2 */