2
0

http2.c 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006
  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. nghttp2_mem mem = {NULL, Curl_nghttp2_malloc, Curl_nghttp2_free,
  384. Curl_nghttp2_calloc, Curl_nghttp2_realloc};
  385. int rc = nghttp2_option_new(&o);
  386. if(rc)
  387. return rc;
  388. /* We handle window updates ourself to enforce buffer limits */
  389. nghttp2_option_set_no_auto_window_update(o, 1);
  390. #if NGHTTP2_VERSION_NUM >= 0x013200
  391. /* with 1.50.0 */
  392. /* turn off RFC 9113 leading and trailing white spaces validation against
  393. HTTP field value. */
  394. nghttp2_option_set_no_rfc9113_leading_and_trailing_ws_validation(o, 1);
  395. #endif
  396. rc = nghttp2_session_client_new3(&ctx->h2, cbs, cf, o, &mem);
  397. nghttp2_option_del(o);
  398. return rc;
  399. }
  400. static ssize_t nw_in_reader(void *reader_ctx,
  401. unsigned char *buf, size_t buflen,
  402. CURLcode *err)
  403. {
  404. struct Curl_cfilter *cf = reader_ctx;
  405. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  406. return Curl_conn_cf_recv(cf->next, data, (char *)buf, buflen, err);
  407. }
  408. static ssize_t nw_out_writer(void *writer_ctx,
  409. const unsigned char *buf, size_t buflen,
  410. CURLcode *err)
  411. {
  412. struct Curl_cfilter *cf = writer_ctx;
  413. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  414. if(data) {
  415. ssize_t nwritten = Curl_conn_cf_send(cf->next, data, (const char *)buf,
  416. buflen, FALSE, err);
  417. if(nwritten > 0)
  418. CURL_TRC_CF(data, cf, "[0] egress: wrote %zd bytes", nwritten);
  419. return nwritten;
  420. }
  421. return 0;
  422. }
  423. static ssize_t send_callback(nghttp2_session *h2,
  424. const uint8_t *mem, size_t length, int flags,
  425. void *userp);
  426. static int on_frame_recv(nghttp2_session *session, const nghttp2_frame *frame,
  427. void *userp);
  428. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  429. static int on_frame_send(nghttp2_session *session, const nghttp2_frame *frame,
  430. void *userp);
  431. #endif
  432. static int on_data_chunk_recv(nghttp2_session *session, uint8_t flags,
  433. int32_t stream_id,
  434. const uint8_t *mem, size_t len, void *userp);
  435. static int on_stream_close(nghttp2_session *session, int32_t stream_id,
  436. uint32_t error_code, void *userp);
  437. static int on_begin_headers(nghttp2_session *session,
  438. const nghttp2_frame *frame, void *userp);
  439. static int on_header(nghttp2_session *session, const nghttp2_frame *frame,
  440. const uint8_t *name, size_t namelen,
  441. const uint8_t *value, size_t valuelen,
  442. uint8_t flags,
  443. void *userp);
  444. #if !defined(CURL_DISABLE_VERBOSE_STRINGS)
  445. static int error_callback(nghttp2_session *session, const char *msg,
  446. size_t len, void *userp);
  447. #endif
  448. static CURLcode cf_h2_ctx_open(struct Curl_cfilter *cf,
  449. struct Curl_easy *data)
  450. {
  451. struct cf_h2_ctx *ctx = cf->ctx;
  452. struct h2_stream_ctx *stream;
  453. CURLcode result = CURLE_OUT_OF_MEMORY;
  454. int rc;
  455. nghttp2_session_callbacks *cbs = NULL;
  456. DEBUGASSERT(!ctx->h2);
  457. DEBUGASSERT(ctx->initialized);
  458. rc = nghttp2_session_callbacks_new(&cbs);
  459. if(rc) {
  460. failf(data, "Couldn't initialize nghttp2 callbacks");
  461. goto out;
  462. }
  463. nghttp2_session_callbacks_set_send_callback(cbs, send_callback);
  464. nghttp2_session_callbacks_set_on_frame_recv_callback(cbs, on_frame_recv);
  465. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  466. nghttp2_session_callbacks_set_on_frame_send_callback(cbs, on_frame_send);
  467. #endif
  468. nghttp2_session_callbacks_set_on_data_chunk_recv_callback(
  469. cbs, on_data_chunk_recv);
  470. nghttp2_session_callbacks_set_on_stream_close_callback(cbs, on_stream_close);
  471. nghttp2_session_callbacks_set_on_begin_headers_callback(
  472. cbs, on_begin_headers);
  473. nghttp2_session_callbacks_set_on_header_callback(cbs, on_header);
  474. #if !defined(CURL_DISABLE_VERBOSE_STRINGS)
  475. nghttp2_session_callbacks_set_error_callback(cbs, error_callback);
  476. #endif
  477. /* The nghttp2 session is not yet setup, do it */
  478. rc = h2_client_new(cf, cbs);
  479. if(rc) {
  480. failf(data, "Couldn't initialize nghttp2");
  481. goto out;
  482. }
  483. ctx->max_concurrent_streams = DEFAULT_MAX_CONCURRENT_STREAMS;
  484. if(ctx->via_h1_upgrade) {
  485. /* HTTP/1.1 Upgrade issued. H2 Settings have already been submitted
  486. * in the H1 request and we upgrade from there. This stream
  487. * is opened implicitly as #1. */
  488. uint8_t binsettings[H2_BINSETTINGS_LEN];
  489. ssize_t binlen; /* length of the binsettings data */
  490. binlen = populate_binsettings(binsettings, data);
  491. if(binlen <= 0) {
  492. failf(data, "nghttp2 unexpectedly failed on pack_settings_payload");
  493. result = CURLE_FAILED_INIT;
  494. goto out;
  495. }
  496. result = http2_data_setup(cf, data, &stream);
  497. if(result)
  498. goto out;
  499. DEBUGASSERT(stream);
  500. stream->id = 1;
  501. /* queue SETTINGS frame (again) */
  502. rc = nghttp2_session_upgrade2(ctx->h2, binsettings, (size_t)binlen,
  503. data->state.httpreq == HTTPREQ_HEAD,
  504. NULL);
  505. if(rc) {
  506. failf(data, "nghttp2_session_upgrade2() failed: %s(%d)",
  507. nghttp2_strerror(rc), rc);
  508. result = CURLE_HTTP2;
  509. goto out;
  510. }
  511. rc = nghttp2_session_set_stream_user_data(ctx->h2, stream->id,
  512. data);
  513. if(rc) {
  514. infof(data, "http/2: failed to set user_data for stream %u",
  515. stream->id);
  516. DEBUGASSERT(0);
  517. }
  518. CURL_TRC_CF(data, cf, "created session via Upgrade");
  519. }
  520. else {
  521. nghttp2_settings_entry iv[H2_SETTINGS_IV_LEN];
  522. size_t ivlen;
  523. ivlen = populate_settings(iv, data);
  524. rc = nghttp2_submit_settings(ctx->h2, NGHTTP2_FLAG_NONE,
  525. iv, ivlen);
  526. if(rc) {
  527. failf(data, "nghttp2_submit_settings() failed: %s(%d)",
  528. nghttp2_strerror(rc), rc);
  529. result = CURLE_HTTP2;
  530. goto out;
  531. }
  532. }
  533. rc = nghttp2_session_set_local_window_size(ctx->h2, NGHTTP2_FLAG_NONE, 0,
  534. HTTP2_HUGE_WINDOW_SIZE);
  535. if(rc) {
  536. failf(data, "nghttp2_session_set_local_window_size() failed: %s(%d)",
  537. nghttp2_strerror(rc), rc);
  538. result = CURLE_HTTP2;
  539. goto out;
  540. }
  541. /* all set, traffic will be send on connect */
  542. result = CURLE_OK;
  543. CURL_TRC_CF(data, cf, "[0] created h2 session%s",
  544. ctx->via_h1_upgrade ? " (via h1 upgrade)" : "");
  545. out:
  546. if(cbs)
  547. nghttp2_session_callbacks_del(cbs);
  548. return result;
  549. }
  550. /*
  551. * Returns nonzero if current HTTP/2 session should be closed.
  552. */
  553. static int should_close_session(struct cf_h2_ctx *ctx)
  554. {
  555. return ctx->drain_total == 0 && !nghttp2_session_want_read(ctx->h2) &&
  556. !nghttp2_session_want_write(ctx->h2);
  557. }
  558. /*
  559. * Processes pending input left in network input buffer.
  560. * This function returns 0 if it succeeds, or -1 and error code will
  561. * be assigned to *err.
  562. */
  563. static int h2_process_pending_input(struct Curl_cfilter *cf,
  564. struct Curl_easy *data,
  565. CURLcode *err)
  566. {
  567. struct cf_h2_ctx *ctx = cf->ctx;
  568. const unsigned char *buf;
  569. size_t blen;
  570. ssize_t rv;
  571. while(Curl_bufq_peek(&ctx->inbufq, &buf, &blen)) {
  572. rv = nghttp2_session_mem_recv(ctx->h2, (const uint8_t *)buf, blen);
  573. if(rv < 0) {
  574. failf(data,
  575. "process_pending_input: nghttp2_session_mem_recv() returned "
  576. "%zd:%s", rv, nghttp2_strerror((int)rv));
  577. *err = CURLE_RECV_ERROR;
  578. return -1;
  579. }
  580. Curl_bufq_skip(&ctx->inbufq, (size_t)rv);
  581. if(Curl_bufq_is_empty(&ctx->inbufq)) {
  582. break;
  583. }
  584. else {
  585. CURL_TRC_CF(data, cf, "process_pending_input: %zu bytes left "
  586. "in connection buffer", Curl_bufq_len(&ctx->inbufq));
  587. }
  588. }
  589. if(nghttp2_session_check_request_allowed(ctx->h2) == 0) {
  590. /* No more requests are allowed in the current session, so
  591. the connection may not be reused. This is set when a
  592. GOAWAY frame has been received or when the limit of stream
  593. identifiers has been reached. */
  594. connclose(cf->conn, "http/2: No new requests allowed");
  595. }
  596. return 0;
  597. }
  598. /*
  599. * The server may send us data at any point (e.g. PING frames). Therefore,
  600. * we cannot assume that an HTTP/2 socket is dead just because it is readable.
  601. *
  602. * Check the lower filters first and, if successful, peek at the socket
  603. * and distinguish between closed and data.
  604. */
  605. static bool http2_connisalive(struct Curl_cfilter *cf, struct Curl_easy *data,
  606. bool *input_pending)
  607. {
  608. struct cf_h2_ctx *ctx = cf->ctx;
  609. bool alive = TRUE;
  610. *input_pending = FALSE;
  611. if(!cf->next || !cf->next->cft->is_alive(cf->next, data, input_pending))
  612. return FALSE;
  613. if(*input_pending) {
  614. /* This happens before we have sent off a request and the connection is
  615. not in use by any other transfer, there should not be any data here,
  616. only "protocol frames" */
  617. CURLcode result;
  618. ssize_t nread = -1;
  619. *input_pending = FALSE;
  620. nread = Curl_bufq_slurp(&ctx->inbufq, nw_in_reader, cf, &result);
  621. if(nread != -1) {
  622. CURL_TRC_CF(data, cf, "%zd bytes stray data read before trying "
  623. "h2 connection", nread);
  624. if(h2_process_pending_input(cf, data, &result) < 0)
  625. /* immediate error, considered dead */
  626. alive = FALSE;
  627. else {
  628. alive = !should_close_session(ctx);
  629. }
  630. }
  631. else if(result != CURLE_AGAIN) {
  632. /* the read failed so let's say this is dead anyway */
  633. alive = FALSE;
  634. }
  635. }
  636. return alive;
  637. }
  638. static CURLcode http2_send_ping(struct Curl_cfilter *cf,
  639. struct Curl_easy *data)
  640. {
  641. struct cf_h2_ctx *ctx = cf->ctx;
  642. int rc;
  643. rc = nghttp2_submit_ping(ctx->h2, 0, ZERO_NULL);
  644. if(rc) {
  645. failf(data, "nghttp2_submit_ping() failed: %s(%d)",
  646. nghttp2_strerror(rc), rc);
  647. return CURLE_HTTP2;
  648. }
  649. rc = nghttp2_session_send(ctx->h2);
  650. if(rc) {
  651. failf(data, "nghttp2_session_send() failed: %s(%d)",
  652. nghttp2_strerror(rc), rc);
  653. return CURLE_SEND_ERROR;
  654. }
  655. return CURLE_OK;
  656. }
  657. /*
  658. * Store nghttp2 version info in this buffer.
  659. */
  660. void Curl_http2_ver(char *p, size_t len)
  661. {
  662. nghttp2_info *h2 = nghttp2_version(0);
  663. (void)msnprintf(p, len, "nghttp2/%s", h2->version_str);
  664. }
  665. static CURLcode nw_out_flush(struct Curl_cfilter *cf,
  666. struct Curl_easy *data)
  667. {
  668. struct cf_h2_ctx *ctx = cf->ctx;
  669. ssize_t nwritten;
  670. CURLcode result;
  671. (void)data;
  672. if(Curl_bufq_is_empty(&ctx->outbufq))
  673. return CURLE_OK;
  674. nwritten = Curl_bufq_pass(&ctx->outbufq, nw_out_writer, cf, &result);
  675. if(nwritten < 0) {
  676. if(result == CURLE_AGAIN) {
  677. CURL_TRC_CF(data, cf, "flush nw send buffer(%zu) -> EAGAIN",
  678. Curl_bufq_len(&ctx->outbufq));
  679. ctx->nw_out_blocked = 1;
  680. }
  681. return result;
  682. }
  683. return Curl_bufq_is_empty(&ctx->outbufq) ? CURLE_OK : CURLE_AGAIN;
  684. }
  685. /*
  686. * The implementation of nghttp2_send_callback type. Here we write |data| with
  687. * size |length| to the network and return the number of bytes actually
  688. * written. See the documentation of nghttp2_send_callback for the details.
  689. */
  690. static ssize_t send_callback(nghttp2_session *h2,
  691. const uint8_t *buf, size_t blen, int flags,
  692. void *userp)
  693. {
  694. struct Curl_cfilter *cf = userp;
  695. struct cf_h2_ctx *ctx = cf->ctx;
  696. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  697. ssize_t nwritten;
  698. CURLcode result = CURLE_OK;
  699. (void)h2;
  700. (void)flags;
  701. DEBUGASSERT(data);
  702. if(!cf->connected)
  703. nwritten = Curl_bufq_write(&ctx->outbufq, buf, blen, &result);
  704. else
  705. nwritten = Curl_bufq_write_pass(&ctx->outbufq, buf, blen,
  706. nw_out_writer, cf, &result);
  707. if(nwritten < 0) {
  708. if(result == CURLE_AGAIN) {
  709. ctx->nw_out_blocked = 1;
  710. return NGHTTP2_ERR_WOULDBLOCK;
  711. }
  712. failf(data, "Failed sending HTTP2 data");
  713. return NGHTTP2_ERR_CALLBACK_FAILURE;
  714. }
  715. if(!nwritten) {
  716. ctx->nw_out_blocked = 1;
  717. return NGHTTP2_ERR_WOULDBLOCK;
  718. }
  719. return nwritten;
  720. }
  721. /* We pass a pointer to this struct in the push callback, but the contents of
  722. the struct are hidden from the user. */
  723. struct curl_pushheaders {
  724. struct Curl_easy *data;
  725. struct h2_stream_ctx *stream;
  726. const nghttp2_push_promise *frame;
  727. };
  728. /*
  729. * push header access function. Only to be used from within the push callback
  730. */
  731. char *curl_pushheader_bynum(struct curl_pushheaders *h, size_t num)
  732. {
  733. /* Verify that we got a good easy handle in the push header struct, mostly to
  734. detect rubbish input fast(er). */
  735. if(!h || !GOOD_EASY_HANDLE(h->data))
  736. return NULL;
  737. else {
  738. if(h->stream && num < h->stream->push_headers_used)
  739. return h->stream->push_headers[num];
  740. }
  741. return NULL;
  742. }
  743. /*
  744. * push header access function. Only to be used from within the push callback
  745. */
  746. char *curl_pushheader_byname(struct curl_pushheaders *h, const char *header)
  747. {
  748. struct h2_stream_ctx *stream;
  749. size_t len;
  750. size_t i;
  751. /* Verify that we got a good easy handle in the push header struct,
  752. mostly to detect rubbish input fast(er). Also empty header name
  753. is just a rubbish too. We have to allow ":" at the beginning of
  754. the header, but header == ":" must be rejected. If we have ':' in
  755. the middle of header, it could be matched in middle of the value,
  756. this is because we do prefix match.*/
  757. if(!h || !GOOD_EASY_HANDLE(h->data) || !header || !header[0] ||
  758. !strcmp(header, ":") || strchr(header + 1, ':'))
  759. return NULL;
  760. stream = h->stream;
  761. if(!stream)
  762. return NULL;
  763. len = strlen(header);
  764. for(i = 0; i < stream->push_headers_used; i++) {
  765. if(!strncmp(header, stream->push_headers[i], len)) {
  766. /* sub-match, make sure that it is followed by a colon */
  767. if(stream->push_headers[i][len] != ':')
  768. continue;
  769. return &stream->push_headers[i][len + 1];
  770. }
  771. }
  772. return NULL;
  773. }
  774. static struct Curl_easy *h2_duphandle(struct Curl_cfilter *cf,
  775. struct Curl_easy *data)
  776. {
  777. struct Curl_easy *second = curl_easy_duphandle(data);
  778. if(second) {
  779. struct h2_stream_ctx *second_stream;
  780. http2_data_setup(cf, second, &second_stream);
  781. second->state.priority.weight = data->state.priority.weight;
  782. }
  783. return second;
  784. }
  785. static int set_transfer_url(struct Curl_easy *data,
  786. struct curl_pushheaders *hp)
  787. {
  788. const char *v;
  789. CURLUcode uc;
  790. char *url = NULL;
  791. int rc = 0;
  792. CURLU *u = curl_url();
  793. if(!u)
  794. return 5;
  795. v = curl_pushheader_byname(hp, HTTP_PSEUDO_SCHEME);
  796. if(v) {
  797. uc = curl_url_set(u, CURLUPART_SCHEME, v, 0);
  798. if(uc) {
  799. rc = 1;
  800. goto fail;
  801. }
  802. }
  803. v = curl_pushheader_byname(hp, HTTP_PSEUDO_AUTHORITY);
  804. if(v) {
  805. uc = Curl_url_set_authority(u, v);
  806. if(uc) {
  807. rc = 2;
  808. goto fail;
  809. }
  810. }
  811. v = curl_pushheader_byname(hp, HTTP_PSEUDO_PATH);
  812. if(v) {
  813. uc = curl_url_set(u, CURLUPART_PATH, v, 0);
  814. if(uc) {
  815. rc = 3;
  816. goto fail;
  817. }
  818. }
  819. uc = curl_url_get(u, CURLUPART_URL, &url, 0);
  820. if(uc)
  821. rc = 4;
  822. fail:
  823. curl_url_cleanup(u);
  824. if(rc)
  825. return rc;
  826. if(data->state.url_alloc)
  827. free(data->state.url);
  828. data->state.url_alloc = TRUE;
  829. data->state.url = url;
  830. return 0;
  831. }
  832. static void discard_newhandle(struct Curl_cfilter *cf,
  833. struct Curl_easy *newhandle)
  834. {
  835. http2_data_done(cf, newhandle);
  836. (void)Curl_close(&newhandle);
  837. }
  838. static int push_promise(struct Curl_cfilter *cf,
  839. struct Curl_easy *data,
  840. const nghttp2_push_promise *frame)
  841. {
  842. struct cf_h2_ctx *ctx = cf->ctx;
  843. int rv; /* one of the CURL_PUSH_* defines */
  844. CURL_TRC_CF(data, cf, "[%d] PUSH_PROMISE received",
  845. frame->promised_stream_id);
  846. if(data->multi->push_cb) {
  847. struct h2_stream_ctx *stream;
  848. struct h2_stream_ctx *newstream;
  849. struct curl_pushheaders heads;
  850. CURLMcode rc;
  851. CURLcode result;
  852. /* clone the parent */
  853. struct Curl_easy *newhandle = h2_duphandle(cf, data);
  854. if(!newhandle) {
  855. infof(data, "failed to duplicate handle");
  856. rv = CURL_PUSH_DENY; /* FAIL HARD */
  857. goto fail;
  858. }
  859. /* ask the application */
  860. CURL_TRC_CF(data, cf, "Got PUSH_PROMISE, ask application");
  861. stream = H2_STREAM_CTX(ctx, data);
  862. if(!stream) {
  863. failf(data, "Internal NULL stream");
  864. discard_newhandle(cf, newhandle);
  865. rv = CURL_PUSH_DENY;
  866. goto fail;
  867. }
  868. heads.data = data;
  869. heads.stream = stream;
  870. heads.frame = frame;
  871. rv = set_transfer_url(newhandle, &heads);
  872. if(rv) {
  873. discard_newhandle(cf, newhandle);
  874. rv = CURL_PUSH_DENY;
  875. goto fail;
  876. }
  877. result = http2_data_setup(cf, newhandle, &newstream);
  878. if(result) {
  879. failf(data, "error setting up stream: %d", result);
  880. discard_newhandle(cf, newhandle);
  881. rv = CURL_PUSH_DENY;
  882. goto fail;
  883. }
  884. DEBUGASSERT(stream);
  885. Curl_set_in_callback(data, TRUE);
  886. rv = data->multi->push_cb(data, newhandle,
  887. stream->push_headers_used, &heads,
  888. data->multi->push_userp);
  889. Curl_set_in_callback(data, FALSE);
  890. /* free the headers again */
  891. free_push_headers(stream);
  892. if(rv) {
  893. DEBUGASSERT((rv > CURL_PUSH_OK) && (rv <= CURL_PUSH_ERROROUT));
  894. /* denied, kill off the new handle again */
  895. discard_newhandle(cf, newhandle);
  896. goto fail;
  897. }
  898. newstream->id = frame->promised_stream_id;
  899. newhandle->req.maxdownload = -1;
  900. newhandle->req.size = -1;
  901. /* approved, add to the multi handle and immediately switch to PERFORM
  902. state with the given connection !*/
  903. rc = Curl_multi_add_perform(data->multi, newhandle, cf->conn);
  904. if(rc) {
  905. infof(data, "failed to add handle to multi");
  906. discard_newhandle(cf, newhandle);
  907. rv = CURL_PUSH_DENY;
  908. goto fail;
  909. }
  910. rv = nghttp2_session_set_stream_user_data(ctx->h2,
  911. newstream->id,
  912. newhandle);
  913. if(rv) {
  914. infof(data, "failed to set user_data for stream %u",
  915. newstream->id);
  916. DEBUGASSERT(0);
  917. rv = CURL_PUSH_DENY;
  918. goto fail;
  919. }
  920. /* success, remember max stream id processed */
  921. if(newstream->id > ctx->local_max_sid)
  922. ctx->local_max_sid = newstream->id;
  923. }
  924. else {
  925. CURL_TRC_CF(data, cf, "Got PUSH_PROMISE, ignore it");
  926. rv = CURL_PUSH_DENY;
  927. }
  928. fail:
  929. return rv;
  930. }
  931. static void h2_xfer_write_resp_hd(struct Curl_cfilter *cf,
  932. struct Curl_easy *data,
  933. struct h2_stream_ctx *stream,
  934. const char *buf, size_t blen, bool eos)
  935. {
  936. /* If we already encountered an error, skip further writes */
  937. if(!stream->xfer_result) {
  938. stream->xfer_result = Curl_xfer_write_resp_hd(data, buf, blen, eos);
  939. if(!stream->xfer_result && !eos)
  940. stream->xfer_result = cf_h2_update_local_win(cf, data, stream, FALSE);
  941. if(stream->xfer_result)
  942. CURL_TRC_CF(data, cf, "[%d] error %d writing %zu bytes of headers",
  943. stream->id, stream->xfer_result, blen);
  944. }
  945. }
  946. static void h2_xfer_write_resp(struct Curl_cfilter *cf,
  947. struct Curl_easy *data,
  948. struct h2_stream_ctx *stream,
  949. const char *buf, size_t blen, bool eos)
  950. {
  951. /* If we already encountered an error, skip further writes */
  952. if(!stream->xfer_result)
  953. stream->xfer_result = Curl_xfer_write_resp(data, buf, blen, eos);
  954. if(!stream->xfer_result && !eos)
  955. stream->xfer_result = cf_h2_update_local_win(cf, data, stream, FALSE);
  956. /* If the transfer write is errored, we do not want any more data */
  957. if(stream->xfer_result) {
  958. struct cf_h2_ctx *ctx = cf->ctx;
  959. CURL_TRC_CF(data, cf, "[%d] error %d writing %zu bytes of data, "
  960. "RST-ing stream",
  961. stream->id, stream->xfer_result, blen);
  962. nghttp2_submit_rst_stream(ctx->h2, 0, stream->id,
  963. (uint32_t)NGHTTP2_ERR_CALLBACK_FAILURE);
  964. }
  965. }
  966. static CURLcode on_stream_frame(struct Curl_cfilter *cf,
  967. struct Curl_easy *data,
  968. const nghttp2_frame *frame)
  969. {
  970. struct cf_h2_ctx *ctx = cf->ctx;
  971. struct h2_stream_ctx *stream = H2_STREAM_CTX(ctx, data);
  972. int32_t stream_id = frame->hd.stream_id;
  973. int rv;
  974. if(!stream) {
  975. CURL_TRC_CF(data, cf, "[%d] No stream_ctx set", stream_id);
  976. return CURLE_FAILED_INIT;
  977. }
  978. switch(frame->hd.type) {
  979. case NGHTTP2_DATA:
  980. CURL_TRC_CF(data, cf, "[%d] DATA, window=%d/%d",
  981. stream_id,
  982. nghttp2_session_get_stream_effective_recv_data_length(
  983. ctx->h2, stream->id),
  984. nghttp2_session_get_stream_effective_local_window_size(
  985. ctx->h2, stream->id));
  986. /* If !body started on this stream, then receiving DATA is illegal. */
  987. if(!stream->bodystarted) {
  988. rv = nghttp2_submit_rst_stream(ctx->h2, NGHTTP2_FLAG_NONE,
  989. stream_id, NGHTTP2_PROTOCOL_ERROR);
  990. if(nghttp2_is_fatal(rv)) {
  991. return CURLE_RECV_ERROR;
  992. }
  993. }
  994. break;
  995. case NGHTTP2_HEADERS:
  996. if(stream->bodystarted) {
  997. /* Only valid HEADERS after body started is trailer HEADERS. We
  998. buffer them in on_header callback. */
  999. break;
  1000. }
  1001. /* nghttp2 guarantees that :status is received, and we store it to
  1002. stream->status_code. Fuzzing has proven this can still be reached
  1003. without status code having been set. */
  1004. if(stream->status_code == -1)
  1005. return CURLE_RECV_ERROR;
  1006. /* Only final status code signals the end of header */
  1007. if(stream->status_code / 100 != 1)
  1008. stream->bodystarted = TRUE;
  1009. else
  1010. stream->status_code = -1;
  1011. h2_xfer_write_resp_hd(cf, data, stream, STRCONST("\r\n"), stream->closed);
  1012. if(stream->status_code / 100 != 1) {
  1013. stream->resp_hds_complete = TRUE;
  1014. }
  1015. drain_stream(cf, data, stream);
  1016. break;
  1017. case NGHTTP2_PUSH_PROMISE:
  1018. rv = push_promise(cf, data, &frame->push_promise);
  1019. if(rv) { /* deny! */
  1020. DEBUGASSERT((rv > CURL_PUSH_OK) && (rv <= CURL_PUSH_ERROROUT));
  1021. rv = nghttp2_submit_rst_stream(ctx->h2, NGHTTP2_FLAG_NONE,
  1022. frame->push_promise.promised_stream_id,
  1023. NGHTTP2_CANCEL);
  1024. if(nghttp2_is_fatal(rv))
  1025. return CURLE_SEND_ERROR;
  1026. else if(rv == CURL_PUSH_ERROROUT) {
  1027. CURL_TRC_CF(data, cf, "[%d] fail in PUSH_PROMISE received",
  1028. stream_id);
  1029. return CURLE_RECV_ERROR;
  1030. }
  1031. }
  1032. break;
  1033. case NGHTTP2_RST_STREAM:
  1034. stream->closed = TRUE;
  1035. if(frame->rst_stream.error_code) {
  1036. stream->reset = TRUE;
  1037. }
  1038. drain_stream(cf, data, stream);
  1039. break;
  1040. case NGHTTP2_WINDOW_UPDATE:
  1041. if(CURL_WANT_SEND(data) && Curl_bufq_is_empty(&stream->sendbuf)) {
  1042. /* need more data, force processing of transfer */
  1043. drain_stream(cf, data, stream);
  1044. }
  1045. else if(!Curl_bufq_is_empty(&stream->sendbuf)) {
  1046. /* resume the potentially suspended stream */
  1047. rv = nghttp2_session_resume_data(ctx->h2, stream->id);
  1048. if(nghttp2_is_fatal(rv))
  1049. return CURLE_SEND_ERROR;
  1050. }
  1051. break;
  1052. default:
  1053. break;
  1054. }
  1055. if(frame->hd.flags & NGHTTP2_FLAG_END_STREAM) {
  1056. if(!stream->closed && !stream->body_eos &&
  1057. ((stream->status_code >= 400) || (stream->status_code < 200))) {
  1058. /* The server did not give us a positive response and we are not
  1059. * done uploading the request body. We need to stop doing that and
  1060. * also inform the server that we aborted our side. */
  1061. CURL_TRC_CF(data, cf, "[%d] EOS frame with unfinished upload and "
  1062. "HTTP status %d, abort upload by RST",
  1063. stream_id, stream->status_code);
  1064. nghttp2_submit_rst_stream(ctx->h2, NGHTTP2_FLAG_NONE,
  1065. stream->id, NGHTTP2_STREAM_CLOSED);
  1066. stream->closed = TRUE;
  1067. }
  1068. drain_stream(cf, data, stream);
  1069. }
  1070. return CURLE_OK;
  1071. }
  1072. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  1073. static int fr_print(const nghttp2_frame *frame, char *buffer, size_t blen)
  1074. {
  1075. switch(frame->hd.type) {
  1076. case NGHTTP2_DATA: {
  1077. return msnprintf(buffer, blen,
  1078. "FRAME[DATA, len=%d, eos=%d, padlen=%d]",
  1079. (int)frame->hd.length,
  1080. !!(frame->hd.flags & NGHTTP2_FLAG_END_STREAM),
  1081. (int)frame->data.padlen);
  1082. }
  1083. case NGHTTP2_HEADERS: {
  1084. return msnprintf(buffer, blen,
  1085. "FRAME[HEADERS, len=%d, hend=%d, eos=%d]",
  1086. (int)frame->hd.length,
  1087. !!(frame->hd.flags & NGHTTP2_FLAG_END_HEADERS),
  1088. !!(frame->hd.flags & NGHTTP2_FLAG_END_STREAM));
  1089. }
  1090. case NGHTTP2_PRIORITY: {
  1091. return msnprintf(buffer, blen,
  1092. "FRAME[PRIORITY, len=%d, flags=%d]",
  1093. (int)frame->hd.length, frame->hd.flags);
  1094. }
  1095. case NGHTTP2_RST_STREAM: {
  1096. return msnprintf(buffer, blen,
  1097. "FRAME[RST_STREAM, len=%d, flags=%d, error=%u]",
  1098. (int)frame->hd.length, frame->hd.flags,
  1099. frame->rst_stream.error_code);
  1100. }
  1101. case NGHTTP2_SETTINGS: {
  1102. if(frame->hd.flags & NGHTTP2_FLAG_ACK) {
  1103. return msnprintf(buffer, blen, "FRAME[SETTINGS, ack=1]");
  1104. }
  1105. return msnprintf(buffer, blen,
  1106. "FRAME[SETTINGS, len=%d]", (int)frame->hd.length);
  1107. }
  1108. case NGHTTP2_PUSH_PROMISE: {
  1109. return msnprintf(buffer, blen,
  1110. "FRAME[PUSH_PROMISE, len=%d, hend=%d]",
  1111. (int)frame->hd.length,
  1112. !!(frame->hd.flags & NGHTTP2_FLAG_END_HEADERS));
  1113. }
  1114. case NGHTTP2_PING: {
  1115. return msnprintf(buffer, blen,
  1116. "FRAME[PING, len=%d, ack=%d]",
  1117. (int)frame->hd.length,
  1118. frame->hd.flags&NGHTTP2_FLAG_ACK);
  1119. }
  1120. case NGHTTP2_GOAWAY: {
  1121. char scratch[128];
  1122. size_t s_len = sizeof(scratch)/sizeof(scratch[0]);
  1123. size_t len = (frame->goaway.opaque_data_len < s_len) ?
  1124. frame->goaway.opaque_data_len : s_len-1;
  1125. if(len)
  1126. memcpy(scratch, frame->goaway.opaque_data, len);
  1127. scratch[len] = '\0';
  1128. return msnprintf(buffer, blen, "FRAME[GOAWAY, error=%d, reason='%s', "
  1129. "last_stream=%d]", frame->goaway.error_code,
  1130. scratch, frame->goaway.last_stream_id);
  1131. }
  1132. case NGHTTP2_WINDOW_UPDATE: {
  1133. return msnprintf(buffer, blen,
  1134. "FRAME[WINDOW_UPDATE, incr=%d]",
  1135. frame->window_update.window_size_increment);
  1136. }
  1137. default:
  1138. return msnprintf(buffer, blen, "FRAME[%d, len=%d, flags=%d]",
  1139. frame->hd.type, (int)frame->hd.length,
  1140. frame->hd.flags);
  1141. }
  1142. }
  1143. static int on_frame_send(nghttp2_session *session, const nghttp2_frame *frame,
  1144. void *userp)
  1145. {
  1146. struct Curl_cfilter *cf = userp;
  1147. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  1148. (void)session;
  1149. DEBUGASSERT(data);
  1150. if(data && Curl_trc_cf_is_verbose(cf, data)) {
  1151. char buffer[256];
  1152. int len;
  1153. len = fr_print(frame, buffer, sizeof(buffer)-1);
  1154. buffer[len] = 0;
  1155. CURL_TRC_CF(data, cf, "[%d] -> %s", frame->hd.stream_id, buffer);
  1156. }
  1157. return 0;
  1158. }
  1159. #endif /* !CURL_DISABLE_VERBOSE_STRINGS */
  1160. static int on_frame_recv(nghttp2_session *session, const nghttp2_frame *frame,
  1161. void *userp)
  1162. {
  1163. struct Curl_cfilter *cf = userp;
  1164. struct cf_h2_ctx *ctx = cf->ctx;
  1165. struct Curl_easy *data = CF_DATA_CURRENT(cf), *data_s;
  1166. int32_t stream_id = frame->hd.stream_id;
  1167. DEBUGASSERT(data);
  1168. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  1169. if(Curl_trc_cf_is_verbose(cf, data)) {
  1170. char buffer[256];
  1171. int len;
  1172. len = fr_print(frame, buffer, sizeof(buffer)-1);
  1173. buffer[len] = 0;
  1174. CURL_TRC_CF(data, cf, "[%d] <- %s",frame->hd.stream_id, buffer);
  1175. }
  1176. #endif /* !CURL_DISABLE_VERBOSE_STRINGS */
  1177. if(!stream_id) {
  1178. /* stream ID zero is for connection-oriented stuff */
  1179. DEBUGASSERT(data);
  1180. switch(frame->hd.type) {
  1181. case NGHTTP2_SETTINGS: {
  1182. if(!(frame->hd.flags & NGHTTP2_FLAG_ACK)) {
  1183. uint32_t max_conn = ctx->max_concurrent_streams;
  1184. ctx->max_concurrent_streams = nghttp2_session_get_remote_settings(
  1185. session, NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS);
  1186. ctx->enable_push = nghttp2_session_get_remote_settings(
  1187. session, NGHTTP2_SETTINGS_ENABLE_PUSH) != 0;
  1188. CURL_TRC_CF(data, cf, "[0] MAX_CONCURRENT_STREAMS: %d",
  1189. ctx->max_concurrent_streams);
  1190. CURL_TRC_CF(data, cf, "[0] ENABLE_PUSH: %s",
  1191. ctx->enable_push ? "TRUE" : "false");
  1192. if(data && max_conn != ctx->max_concurrent_streams) {
  1193. /* only signal change if the value actually changed */
  1194. CURL_TRC_CF(data, cf, "[0] notify MAX_CONCURRENT_STREAMS: %u",
  1195. ctx->max_concurrent_streams);
  1196. Curl_multi_connchanged(data->multi);
  1197. }
  1198. /* Since the initial stream window is 64K, a request might be on HOLD,
  1199. * due to exhaustion. The (initial) SETTINGS may announce a much larger
  1200. * window and *assume* that we treat this like a WINDOW_UPDATE. Some
  1201. * servers send an explicit WINDOW_UPDATE, but not all seem to do that.
  1202. * To be safe, we UNHOLD a stream in order not to stall. */
  1203. if(CURL_WANT_SEND(data)) {
  1204. struct h2_stream_ctx *stream = H2_STREAM_CTX(ctx, data);
  1205. if(stream)
  1206. drain_stream(cf, data, stream);
  1207. }
  1208. }
  1209. break;
  1210. }
  1211. case NGHTTP2_GOAWAY:
  1212. ctx->rcvd_goaway = TRUE;
  1213. ctx->goaway_error = frame->goaway.error_code;
  1214. ctx->remote_max_sid = frame->goaway.last_stream_id;
  1215. if(data) {
  1216. infof(data, "received GOAWAY, error=%u, last_stream=%u",
  1217. ctx->goaway_error, ctx->remote_max_sid);
  1218. Curl_multi_connchanged(data->multi);
  1219. }
  1220. break;
  1221. default:
  1222. break;
  1223. }
  1224. return 0;
  1225. }
  1226. data_s = nghttp2_session_get_stream_user_data(session, stream_id);
  1227. if(!data_s) {
  1228. CURL_TRC_CF(data, cf, "[%d] No Curl_easy associated", stream_id);
  1229. return 0;
  1230. }
  1231. return on_stream_frame(cf, data_s, frame) ? NGHTTP2_ERR_CALLBACK_FAILURE : 0;
  1232. }
  1233. static int on_data_chunk_recv(nghttp2_session *session, uint8_t flags,
  1234. int32_t stream_id,
  1235. const uint8_t *mem, size_t len, void *userp)
  1236. {
  1237. struct Curl_cfilter *cf = userp;
  1238. struct cf_h2_ctx *ctx = cf->ctx;
  1239. struct h2_stream_ctx *stream;
  1240. struct Curl_easy *data_s;
  1241. (void)flags;
  1242. DEBUGASSERT(stream_id); /* should never be a zero stream ID here */
  1243. DEBUGASSERT(CF_DATA_CURRENT(cf));
  1244. /* get the stream from the hash based on Stream ID */
  1245. data_s = nghttp2_session_get_stream_user_data(session, stream_id);
  1246. if(!data_s) {
  1247. /* Receiving a Stream ID not in the hash should not happen - unless
  1248. we have aborted a transfer artificially and there were more data
  1249. in the pipeline. Silently ignore. */
  1250. CURL_TRC_CF(CF_DATA_CURRENT(cf), cf, "[%d] Data for unknown",
  1251. stream_id);
  1252. /* consumed explicitly as no one will read it */
  1253. nghttp2_session_consume(session, stream_id, len);
  1254. return 0;
  1255. }
  1256. stream = H2_STREAM_CTX(ctx, data_s);
  1257. if(!stream)
  1258. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1259. h2_xfer_write_resp(cf, data_s, stream, (char *)mem, len, FALSE);
  1260. nghttp2_session_consume(ctx->h2, stream_id, len);
  1261. stream->nrcvd_data += (curl_off_t)len;
  1262. return 0;
  1263. }
  1264. static int on_stream_close(nghttp2_session *session, int32_t stream_id,
  1265. uint32_t error_code, void *userp)
  1266. {
  1267. struct Curl_cfilter *cf = userp;
  1268. struct cf_h2_ctx *ctx = cf->ctx;
  1269. struct Curl_easy *data_s, *call_data = CF_DATA_CURRENT(cf);
  1270. struct h2_stream_ctx *stream;
  1271. int rv;
  1272. (void)session;
  1273. DEBUGASSERT(call_data);
  1274. /* stream id 0 is the connection, do not look there for streams. */
  1275. data_s = stream_id ?
  1276. nghttp2_session_get_stream_user_data(session, stream_id) : NULL;
  1277. if(!data_s) {
  1278. CURL_TRC_CF(call_data, cf,
  1279. "[%d] on_stream_close, no easy set on stream", stream_id);
  1280. return 0;
  1281. }
  1282. if(!GOOD_EASY_HANDLE(data_s)) {
  1283. /* nghttp2 still has an easy registered for the stream which has
  1284. * been freed be libcurl. This points to a code path that does not
  1285. * trigger DONE or DETACH events as it must. */
  1286. CURL_TRC_CF(call_data, cf,
  1287. "[%d] on_stream_close, not a GOOD easy on stream", stream_id);
  1288. (void)nghttp2_session_set_stream_user_data(session, stream_id, 0);
  1289. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1290. }
  1291. stream = H2_STREAM_CTX(ctx, data_s);
  1292. if(!stream) {
  1293. CURL_TRC_CF(data_s, cf,
  1294. "[%d] on_stream_close, GOOD easy but no stream", stream_id);
  1295. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1296. }
  1297. stream->closed = TRUE;
  1298. stream->error = error_code;
  1299. if(stream->error) {
  1300. stream->reset = TRUE;
  1301. }
  1302. if(stream->error)
  1303. CURL_TRC_CF(data_s, cf, "[%d] RESET: %s (err %d)",
  1304. stream_id, nghttp2_http2_strerror(error_code), error_code);
  1305. else
  1306. CURL_TRC_CF(data_s, cf, "[%d] CLOSED", stream_id);
  1307. drain_stream(cf, data_s, stream);
  1308. /* remove `data_s` from the nghttp2 stream */
  1309. rv = nghttp2_session_set_stream_user_data(session, stream_id, 0);
  1310. if(rv) {
  1311. infof(data_s, "http/2: failed to clear user_data for stream %u",
  1312. stream_id);
  1313. DEBUGASSERT(0);
  1314. }
  1315. return 0;
  1316. }
  1317. static int on_begin_headers(nghttp2_session *session,
  1318. const nghttp2_frame *frame, void *userp)
  1319. {
  1320. struct Curl_cfilter *cf = userp;
  1321. struct cf_h2_ctx *ctx = cf->ctx;
  1322. struct h2_stream_ctx *stream;
  1323. struct Curl_easy *data_s = NULL;
  1324. (void)cf;
  1325. data_s = nghttp2_session_get_stream_user_data(session, frame->hd.stream_id);
  1326. if(!data_s) {
  1327. return 0;
  1328. }
  1329. if(frame->hd.type != NGHTTP2_HEADERS) {
  1330. return 0;
  1331. }
  1332. stream = H2_STREAM_CTX(ctx, data_s);
  1333. if(!stream || !stream->bodystarted) {
  1334. return 0;
  1335. }
  1336. return 0;
  1337. }
  1338. /* frame->hd.type is either NGHTTP2_HEADERS or NGHTTP2_PUSH_PROMISE */
  1339. static int on_header(nghttp2_session *session, const nghttp2_frame *frame,
  1340. const uint8_t *name, size_t namelen,
  1341. const uint8_t *value, size_t valuelen,
  1342. uint8_t flags,
  1343. void *userp)
  1344. {
  1345. struct Curl_cfilter *cf = userp;
  1346. struct cf_h2_ctx *ctx = cf->ctx;
  1347. struct h2_stream_ctx *stream;
  1348. struct Curl_easy *data_s;
  1349. int32_t stream_id = frame->hd.stream_id;
  1350. CURLcode result;
  1351. (void)flags;
  1352. DEBUGASSERT(stream_id); /* should never be a zero stream ID here */
  1353. /* get the stream from the hash based on Stream ID */
  1354. data_s = nghttp2_session_get_stream_user_data(session, stream_id);
  1355. if(!data_s)
  1356. /* Receiving a Stream ID not in the hash should not happen, this is an
  1357. internal error more than anything else! */
  1358. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1359. stream = H2_STREAM_CTX(ctx, data_s);
  1360. if(!stream) {
  1361. failf(data_s, "Internal NULL stream");
  1362. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1363. }
  1364. /* Store received PUSH_PROMISE headers to be used when the subsequent
  1365. PUSH_PROMISE callback comes */
  1366. if(frame->hd.type == NGHTTP2_PUSH_PROMISE) {
  1367. char *h;
  1368. if(!strcmp(HTTP_PSEUDO_AUTHORITY, (const char *)name)) {
  1369. /* pseudo headers are lower case */
  1370. int rc = 0;
  1371. char *check = aprintf("%s:%d", cf->conn->host.name,
  1372. cf->conn->remote_port);
  1373. if(!check)
  1374. /* no memory */
  1375. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1376. if(!strcasecompare(check, (const char *)value) &&
  1377. ((cf->conn->remote_port != cf->conn->given->defport) ||
  1378. !strcasecompare(cf->conn->host.name, (const char *)value))) {
  1379. /* This is push is not for the same authority that was asked for in
  1380. * the URL. RFC 7540 section 8.2 says: "A client MUST treat a
  1381. * PUSH_PROMISE for which the server is not authoritative as a stream
  1382. * error of type PROTOCOL_ERROR."
  1383. */
  1384. (void)nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
  1385. stream_id, NGHTTP2_PROTOCOL_ERROR);
  1386. rc = NGHTTP2_ERR_CALLBACK_FAILURE;
  1387. }
  1388. free(check);
  1389. if(rc)
  1390. return rc;
  1391. }
  1392. if(!stream->push_headers) {
  1393. stream->push_headers_alloc = 10;
  1394. stream->push_headers = malloc(stream->push_headers_alloc *
  1395. sizeof(char *));
  1396. if(!stream->push_headers)
  1397. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1398. stream->push_headers_used = 0;
  1399. }
  1400. else if(stream->push_headers_used ==
  1401. stream->push_headers_alloc) {
  1402. char **headp;
  1403. if(stream->push_headers_alloc > 1000) {
  1404. /* this is beyond crazy many headers, bail out */
  1405. failf(data_s, "Too many PUSH_PROMISE headers");
  1406. free_push_headers(stream);
  1407. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1408. }
  1409. stream->push_headers_alloc *= 2;
  1410. headp = realloc(stream->push_headers,
  1411. stream->push_headers_alloc * sizeof(char *));
  1412. if(!headp) {
  1413. free_push_headers(stream);
  1414. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1415. }
  1416. stream->push_headers = headp;
  1417. }
  1418. h = aprintf("%s:%s", name, value);
  1419. if(h)
  1420. stream->push_headers[stream->push_headers_used++] = h;
  1421. return 0;
  1422. }
  1423. if(stream->bodystarted) {
  1424. /* This is a trailer */
  1425. CURL_TRC_CF(data_s, cf, "[%d] trailer: %.*s: %.*s",
  1426. stream->id, (int)namelen, name, (int)valuelen, value);
  1427. result = Curl_dynhds_add(&stream->resp_trailers,
  1428. (const char *)name, namelen,
  1429. (const char *)value, valuelen);
  1430. if(result)
  1431. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1432. return 0;
  1433. }
  1434. if(namelen == sizeof(HTTP_PSEUDO_STATUS) - 1 &&
  1435. memcmp(HTTP_PSEUDO_STATUS, name, namelen) == 0) {
  1436. /* nghttp2 guarantees :status is received first and only once. */
  1437. char buffer[32];
  1438. result = Curl_http_decode_status(&stream->status_code,
  1439. (const char *)value, valuelen);
  1440. if(result)
  1441. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1442. msnprintf(buffer, sizeof(buffer), HTTP_PSEUDO_STATUS ":%u\r",
  1443. stream->status_code);
  1444. result = Curl_headers_push(data_s, buffer, CURLH_PSEUDO);
  1445. if(result)
  1446. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1447. Curl_dyn_reset(&ctx->scratch);
  1448. result = Curl_dyn_addn(&ctx->scratch, STRCONST("HTTP/2 "));
  1449. if(!result)
  1450. result = Curl_dyn_addn(&ctx->scratch, value, valuelen);
  1451. if(!result)
  1452. result = Curl_dyn_addn(&ctx->scratch, STRCONST(" \r\n"));
  1453. if(!result)
  1454. h2_xfer_write_resp_hd(cf, data_s, stream, Curl_dyn_ptr(&ctx->scratch),
  1455. Curl_dyn_len(&ctx->scratch), FALSE);
  1456. if(result)
  1457. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1458. /* if we receive data for another handle, wake that up */
  1459. if(CF_DATA_CURRENT(cf) != data_s)
  1460. Curl_expire(data_s, 0, EXPIRE_RUN_NOW);
  1461. CURL_TRC_CF(data_s, cf, "[%d] status: HTTP/2 %03d",
  1462. stream->id, stream->status_code);
  1463. return 0;
  1464. }
  1465. /* nghttp2 guarantees that namelen > 0, and :status was already
  1466. received, and this is not pseudo-header field . */
  1467. /* convert to an HTTP1-style header */
  1468. Curl_dyn_reset(&ctx->scratch);
  1469. result = Curl_dyn_addn(&ctx->scratch, (const char *)name, namelen);
  1470. if(!result)
  1471. result = Curl_dyn_addn(&ctx->scratch, STRCONST(": "));
  1472. if(!result)
  1473. result = Curl_dyn_addn(&ctx->scratch, (const char *)value, valuelen);
  1474. if(!result)
  1475. result = Curl_dyn_addn(&ctx->scratch, STRCONST("\r\n"));
  1476. if(!result)
  1477. h2_xfer_write_resp_hd(cf, data_s, stream, Curl_dyn_ptr(&ctx->scratch),
  1478. Curl_dyn_len(&ctx->scratch), FALSE);
  1479. if(result)
  1480. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1481. /* if we receive data for another handle, wake that up */
  1482. if(CF_DATA_CURRENT(cf) != data_s)
  1483. Curl_expire(data_s, 0, EXPIRE_RUN_NOW);
  1484. CURL_TRC_CF(data_s, cf, "[%d] header: %.*s: %.*s",
  1485. stream->id, (int)namelen, name, (int)valuelen, value);
  1486. return 0; /* 0 is successful */
  1487. }
  1488. static ssize_t req_body_read_callback(nghttp2_session *session,
  1489. int32_t stream_id,
  1490. uint8_t *buf, size_t length,
  1491. uint32_t *data_flags,
  1492. nghttp2_data_source *source,
  1493. void *userp)
  1494. {
  1495. struct Curl_cfilter *cf = userp;
  1496. struct cf_h2_ctx *ctx = cf->ctx;
  1497. struct Curl_easy *data_s;
  1498. struct h2_stream_ctx *stream = NULL;
  1499. CURLcode result;
  1500. ssize_t nread;
  1501. (void)source;
  1502. (void)cf;
  1503. if(!stream_id)
  1504. return NGHTTP2_ERR_INVALID_ARGUMENT;
  1505. /* get the stream from the hash based on Stream ID, stream ID zero is for
  1506. connection-oriented stuff */
  1507. data_s = nghttp2_session_get_stream_user_data(session, stream_id);
  1508. if(!data_s)
  1509. /* Receiving a Stream ID not in the hash should not happen, this is an
  1510. internal error more than anything else! */
  1511. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1512. stream = H2_STREAM_CTX(ctx, data_s);
  1513. if(!stream)
  1514. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1515. nread = Curl_bufq_read(&stream->sendbuf, buf, length, &result);
  1516. if(nread < 0) {
  1517. if(result != CURLE_AGAIN)
  1518. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1519. nread = 0;
  1520. }
  1521. CURL_TRC_CF(data_s, cf, "[%d] req_body_read(len=%zu) eos=%d -> %zd, %d",
  1522. stream_id, length, stream->body_eos, nread, result);
  1523. if(stream->body_eos && Curl_bufq_is_empty(&stream->sendbuf)) {
  1524. *data_flags = NGHTTP2_DATA_FLAG_EOF;
  1525. return nread;
  1526. }
  1527. return (nread == 0) ? NGHTTP2_ERR_DEFERRED : nread;
  1528. }
  1529. #if !defined(CURL_DISABLE_VERBOSE_STRINGS)
  1530. static int error_callback(nghttp2_session *session,
  1531. const char *msg,
  1532. size_t len,
  1533. void *userp)
  1534. {
  1535. struct Curl_cfilter *cf = userp;
  1536. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  1537. (void)session;
  1538. failf(data, "%.*s", (int)len, msg);
  1539. return 0;
  1540. }
  1541. #endif
  1542. /*
  1543. * Append headers to ask for an HTTP1.1 to HTTP2 upgrade.
  1544. */
  1545. CURLcode Curl_http2_request_upgrade(struct dynbuf *req,
  1546. struct Curl_easy *data)
  1547. {
  1548. CURLcode result;
  1549. char *base64;
  1550. size_t blen;
  1551. struct SingleRequest *k = &data->req;
  1552. uint8_t binsettings[H2_BINSETTINGS_LEN];
  1553. ssize_t binlen; /* length of the binsettings data */
  1554. binlen = populate_binsettings(binsettings, data);
  1555. if(binlen <= 0) {
  1556. failf(data, "nghttp2 unexpectedly failed on pack_settings_payload");
  1557. Curl_dyn_free(req);
  1558. return CURLE_FAILED_INIT;
  1559. }
  1560. result = Curl_base64url_encode((const char *)binsettings, (size_t)binlen,
  1561. &base64, &blen);
  1562. if(result) {
  1563. Curl_dyn_free(req);
  1564. return result;
  1565. }
  1566. result = Curl_dyn_addf(req,
  1567. "Connection: Upgrade, HTTP2-Settings\r\n"
  1568. "Upgrade: %s\r\n"
  1569. "HTTP2-Settings: %s\r\n",
  1570. NGHTTP2_CLEARTEXT_PROTO_VERSION_ID, base64);
  1571. free(base64);
  1572. k->upgr101 = UPGR101_H2;
  1573. data->conn->bits.asks_multiplex = TRUE;
  1574. return result;
  1575. }
  1576. static ssize_t http2_handle_stream_close(struct Curl_cfilter *cf,
  1577. struct Curl_easy *data,
  1578. struct h2_stream_ctx *stream,
  1579. CURLcode *err)
  1580. {
  1581. ssize_t rv = 0;
  1582. if(stream->error == NGHTTP2_REFUSED_STREAM) {
  1583. CURL_TRC_CF(data, cf, "[%d] REFUSED_STREAM, try again on a new "
  1584. "connection", stream->id);
  1585. connclose(cf->conn, "REFUSED_STREAM"); /* do not use this anymore */
  1586. data->state.refused_stream = TRUE;
  1587. *err = CURLE_RECV_ERROR; /* trigger Curl_retry_request() later */
  1588. return -1;
  1589. }
  1590. else if(stream->error != NGHTTP2_NO_ERROR) {
  1591. if(stream->resp_hds_complete && data->req.no_body) {
  1592. CURL_TRC_CF(data, cf, "[%d] error after response headers, but we did "
  1593. "not want a body anyway, ignore: %s (err %u)",
  1594. stream->id, nghttp2_http2_strerror(stream->error),
  1595. stream->error);
  1596. stream->close_handled = TRUE;
  1597. *err = CURLE_OK;
  1598. goto out;
  1599. }
  1600. failf(data, "HTTP/2 stream %u was not closed cleanly: %s (err %u)",
  1601. stream->id, nghttp2_http2_strerror(stream->error),
  1602. stream->error);
  1603. *err = CURLE_HTTP2_STREAM;
  1604. return -1;
  1605. }
  1606. else if(stream->reset) {
  1607. failf(data, "HTTP/2 stream %u was reset", stream->id);
  1608. *err = data->req.bytecount ? CURLE_PARTIAL_FILE : CURLE_HTTP2;
  1609. return -1;
  1610. }
  1611. if(!stream->bodystarted) {
  1612. failf(data, "HTTP/2 stream %u was closed cleanly, but before getting "
  1613. " all response header fields, treated as error",
  1614. stream->id);
  1615. *err = CURLE_HTTP2_STREAM;
  1616. return -1;
  1617. }
  1618. if(Curl_dynhds_count(&stream->resp_trailers)) {
  1619. struct dynhds_entry *e;
  1620. struct dynbuf dbuf;
  1621. size_t i;
  1622. *err = CURLE_OK;
  1623. Curl_dyn_init(&dbuf, DYN_TRAILERS);
  1624. for(i = 0; i < Curl_dynhds_count(&stream->resp_trailers); ++i) {
  1625. e = Curl_dynhds_getn(&stream->resp_trailers, i);
  1626. if(!e)
  1627. break;
  1628. Curl_dyn_reset(&dbuf);
  1629. *err = Curl_dyn_addf(&dbuf, "%.*s: %.*s\x0d\x0a",
  1630. (int)e->namelen, e->name,
  1631. (int)e->valuelen, e->value);
  1632. if(*err)
  1633. break;
  1634. Curl_debug(data, CURLINFO_HEADER_IN, Curl_dyn_ptr(&dbuf),
  1635. Curl_dyn_len(&dbuf));
  1636. *err = Curl_client_write(data, CLIENTWRITE_HEADER|CLIENTWRITE_TRAILER,
  1637. Curl_dyn_ptr(&dbuf), Curl_dyn_len(&dbuf));
  1638. if(*err)
  1639. break;
  1640. }
  1641. Curl_dyn_free(&dbuf);
  1642. if(*err)
  1643. goto out;
  1644. }
  1645. stream->close_handled = TRUE;
  1646. *err = CURLE_OK;
  1647. rv = 0;
  1648. out:
  1649. CURL_TRC_CF(data, cf, "handle_stream_close -> %zd, %d", rv, *err);
  1650. return rv;
  1651. }
  1652. static int sweight_wanted(const struct Curl_easy *data)
  1653. {
  1654. /* 0 weight is not set by user and we take the nghttp2 default one */
  1655. return data->set.priority.weight ?
  1656. data->set.priority.weight : NGHTTP2_DEFAULT_WEIGHT;
  1657. }
  1658. static int sweight_in_effect(const struct Curl_easy *data)
  1659. {
  1660. /* 0 weight is not set by user and we take the nghttp2 default one */
  1661. return data->state.priority.weight ?
  1662. data->state.priority.weight : NGHTTP2_DEFAULT_WEIGHT;
  1663. }
  1664. /*
  1665. * h2_pri_spec() fills in the pri_spec struct, used by nghttp2 to send weight
  1666. * and dependency to the peer. It also stores the updated values in the state
  1667. * struct.
  1668. */
  1669. static void h2_pri_spec(struct cf_h2_ctx *ctx,
  1670. struct Curl_easy *data,
  1671. nghttp2_priority_spec *pri_spec)
  1672. {
  1673. struct Curl_data_priority *prio = &data->set.priority;
  1674. struct h2_stream_ctx *depstream = H2_STREAM_CTX(ctx, prio->parent);
  1675. int32_t depstream_id = depstream ? depstream->id : 0;
  1676. nghttp2_priority_spec_init(pri_spec, depstream_id,
  1677. sweight_wanted(data),
  1678. data->set.priority.exclusive);
  1679. data->state.priority = *prio;
  1680. }
  1681. /*
  1682. * Check if there is been an update in the priority /
  1683. * dependency settings and if so it submits a PRIORITY frame with the updated
  1684. * info.
  1685. * Flush any out data pending in the network buffer.
  1686. */
  1687. static CURLcode h2_progress_egress(struct Curl_cfilter *cf,
  1688. struct Curl_easy *data)
  1689. {
  1690. struct cf_h2_ctx *ctx = cf->ctx;
  1691. struct h2_stream_ctx *stream = H2_STREAM_CTX(ctx, data);
  1692. int rv = 0;
  1693. if(stream && stream->id > 0 &&
  1694. ((sweight_wanted(data) != sweight_in_effect(data)) ||
  1695. (data->set.priority.exclusive != data->state.priority.exclusive) ||
  1696. (data->set.priority.parent != data->state.priority.parent)) ) {
  1697. /* send new weight and/or dependency */
  1698. nghttp2_priority_spec pri_spec;
  1699. h2_pri_spec(ctx, data, &pri_spec);
  1700. CURL_TRC_CF(data, cf, "[%d] Queuing PRIORITY", stream->id);
  1701. DEBUGASSERT(stream->id != -1);
  1702. rv = nghttp2_submit_priority(ctx->h2, NGHTTP2_FLAG_NONE,
  1703. stream->id, &pri_spec);
  1704. if(rv)
  1705. goto out;
  1706. }
  1707. ctx->nw_out_blocked = 0;
  1708. while(!rv && !ctx->nw_out_blocked && nghttp2_session_want_write(ctx->h2))
  1709. rv = nghttp2_session_send(ctx->h2);
  1710. out:
  1711. if(nghttp2_is_fatal(rv)) {
  1712. CURL_TRC_CF(data, cf, "nghttp2_session_send error (%s)%d",
  1713. nghttp2_strerror(rv), rv);
  1714. return CURLE_SEND_ERROR;
  1715. }
  1716. /* Defer flushing during the connect phase so that the SETTINGS and
  1717. * other initial frames are sent together with the first request.
  1718. * Unless we are 'connect_only' where the request will never come. */
  1719. if(!cf->connected && !cf->conn->connect_only)
  1720. return CURLE_OK;
  1721. return nw_out_flush(cf, data);
  1722. }
  1723. static ssize_t stream_recv(struct Curl_cfilter *cf, struct Curl_easy *data,
  1724. struct h2_stream_ctx *stream,
  1725. char *buf, size_t len, CURLcode *err)
  1726. {
  1727. struct cf_h2_ctx *ctx = cf->ctx;
  1728. ssize_t nread = -1;
  1729. (void)buf;
  1730. *err = CURLE_AGAIN;
  1731. if(stream->xfer_result) {
  1732. CURL_TRC_CF(data, cf, "[%d] xfer write failed", stream->id);
  1733. *err = stream->xfer_result;
  1734. nread = -1;
  1735. }
  1736. else if(stream->closed) {
  1737. CURL_TRC_CF(data, cf, "[%d] returning CLOSE", stream->id);
  1738. nread = http2_handle_stream_close(cf, data, stream, err);
  1739. }
  1740. else if(stream->reset ||
  1741. (ctx->conn_closed && Curl_bufq_is_empty(&ctx->inbufq)) ||
  1742. (ctx->rcvd_goaway && ctx->remote_max_sid < stream->id)) {
  1743. CURL_TRC_CF(data, cf, "[%d] returning ERR", stream->id);
  1744. *err = data->req.bytecount ? CURLE_PARTIAL_FILE : CURLE_HTTP2;
  1745. nread = -1;
  1746. }
  1747. if(nread < 0 && *err != CURLE_AGAIN)
  1748. CURL_TRC_CF(data, cf, "[%d] stream_recv(len=%zu) -> %zd, %d",
  1749. stream->id, len, nread, *err);
  1750. return nread;
  1751. }
  1752. static CURLcode h2_progress_ingress(struct Curl_cfilter *cf,
  1753. struct Curl_easy *data,
  1754. size_t data_max_bytes)
  1755. {
  1756. struct cf_h2_ctx *ctx = cf->ctx;
  1757. struct h2_stream_ctx *stream;
  1758. CURLcode result = CURLE_OK;
  1759. ssize_t nread;
  1760. /* Process network input buffer fist */
  1761. if(!Curl_bufq_is_empty(&ctx->inbufq)) {
  1762. CURL_TRC_CF(data, cf, "Process %zu bytes in connection buffer",
  1763. Curl_bufq_len(&ctx->inbufq));
  1764. if(h2_process_pending_input(cf, data, &result) < 0)
  1765. return result;
  1766. }
  1767. /* Receive data from the "lower" filters, e.g. network until
  1768. * it is time to stop due to connection close or us not processing
  1769. * all network input */
  1770. while(!ctx->conn_closed && Curl_bufq_is_empty(&ctx->inbufq)) {
  1771. stream = H2_STREAM_CTX(ctx, data);
  1772. if(stream && (stream->closed || !data_max_bytes)) {
  1773. /* We would like to abort here and stop processing, so that
  1774. * the transfer loop can handle the data/close here. However,
  1775. * this may leave data in underlying buffers that will not
  1776. * be consumed. */
  1777. if(!cf->next || !cf->next->cft->has_data_pending(cf->next, data))
  1778. drain_stream(cf, data, stream);
  1779. break;
  1780. }
  1781. nread = Curl_bufq_sipn(&ctx->inbufq, 0, nw_in_reader, cf, &result);
  1782. if(nread < 0) {
  1783. if(result != CURLE_AGAIN) {
  1784. failf(data, "Failed receiving HTTP2 data: %d(%s)", result,
  1785. curl_easy_strerror(result));
  1786. return result;
  1787. }
  1788. break;
  1789. }
  1790. else if(nread == 0) {
  1791. CURL_TRC_CF(data, cf, "[0] ingress: connection closed");
  1792. ctx->conn_closed = TRUE;
  1793. break;
  1794. }
  1795. else {
  1796. CURL_TRC_CF(data, cf, "[0] ingress: read %zd bytes", nread);
  1797. data_max_bytes = (data_max_bytes > (size_t)nread) ?
  1798. (data_max_bytes - (size_t)nread) : 0;
  1799. }
  1800. if(h2_process_pending_input(cf, data, &result))
  1801. return result;
  1802. CURL_TRC_CF(data, cf, "[0] progress ingress: inbufg=%zu",
  1803. Curl_bufq_len(&ctx->inbufq));
  1804. }
  1805. if(ctx->conn_closed && Curl_bufq_is_empty(&ctx->inbufq)) {
  1806. connclose(cf->conn, "GOAWAY received");
  1807. }
  1808. CURL_TRC_CF(data, cf, "[0] progress ingress: done");
  1809. return CURLE_OK;
  1810. }
  1811. static ssize_t cf_h2_recv(struct Curl_cfilter *cf, struct Curl_easy *data,
  1812. char *buf, size_t len, CURLcode *err)
  1813. {
  1814. struct cf_h2_ctx *ctx = cf->ctx;
  1815. struct h2_stream_ctx *stream = H2_STREAM_CTX(ctx, data);
  1816. ssize_t nread = -1;
  1817. CURLcode result;
  1818. struct cf_call_data save;
  1819. if(!stream) {
  1820. /* Abnormal call sequence: either this transfer has never opened a stream
  1821. * (unlikely) or the transfer has been done, cleaned up its resources, but
  1822. * a read() is called anyway. It is not clear what the calling sequence
  1823. * is for such a case. */
  1824. failf(data, "http/2 recv on a transfer never opened "
  1825. "or already cleared, mid=%" FMT_OFF_T, data->mid);
  1826. *err = CURLE_HTTP2;
  1827. return -1;
  1828. }
  1829. CF_DATA_SAVE(save, cf, data);
  1830. nread = stream_recv(cf, data, stream, buf, len, err);
  1831. if(nread < 0 && *err != CURLE_AGAIN)
  1832. goto out;
  1833. if(nread < 0) {
  1834. *err = h2_progress_ingress(cf, data, len);
  1835. if(*err)
  1836. goto out;
  1837. nread = stream_recv(cf, data, stream, buf, len, err);
  1838. }
  1839. if(nread > 0) {
  1840. size_t data_consumed = (size_t)nread;
  1841. /* Now that we transferred this to the upper layer, we report
  1842. * the actual amount of DATA consumed to the H2 session, so
  1843. * that it adjusts stream flow control */
  1844. if(stream->resp_hds_len >= data_consumed) {
  1845. stream->resp_hds_len -= data_consumed; /* no DATA */
  1846. }
  1847. else {
  1848. if(stream->resp_hds_len) {
  1849. data_consumed -= stream->resp_hds_len;
  1850. stream->resp_hds_len = 0;
  1851. }
  1852. if(data_consumed) {
  1853. nghttp2_session_consume(ctx->h2, stream->id, data_consumed);
  1854. }
  1855. }
  1856. if(stream->closed) {
  1857. CURL_TRC_CF(data, cf, "[%d] DRAIN closed stream", stream->id);
  1858. drain_stream(cf, data, stream);
  1859. }
  1860. }
  1861. out:
  1862. result = h2_progress_egress(cf, data);
  1863. if(result == CURLE_AGAIN) {
  1864. /* pending data to send, need to be called again. Ideally, we
  1865. * monitor the socket for POLLOUT, but when not SENDING
  1866. * any more, we force processing of the transfer. */
  1867. if(!CURL_WANT_SEND(data))
  1868. drain_stream(cf, data, stream);
  1869. }
  1870. else if(result) {
  1871. *err = result;
  1872. nread = -1;
  1873. }
  1874. CURL_TRC_CF(data, cf, "[%d] cf_recv(len=%zu) -> %zd %d, "
  1875. "window=%d/%d, connection %d/%d",
  1876. stream->id, len, nread, *err,
  1877. nghttp2_session_get_stream_effective_recv_data_length(
  1878. ctx->h2, stream->id),
  1879. nghttp2_session_get_stream_effective_local_window_size(
  1880. ctx->h2, stream->id),
  1881. nghttp2_session_get_local_window_size(ctx->h2),
  1882. HTTP2_HUGE_WINDOW_SIZE);
  1883. CF_DATA_RESTORE(cf, save);
  1884. return nread;
  1885. }
  1886. static ssize_t cf_h2_body_send(struct Curl_cfilter *cf,
  1887. struct Curl_easy *data,
  1888. struct h2_stream_ctx *stream,
  1889. const void *buf, size_t blen, bool eos,
  1890. CURLcode *err)
  1891. {
  1892. struct cf_h2_ctx *ctx = cf->ctx;
  1893. ssize_t nwritten;
  1894. if(stream->closed) {
  1895. if(stream->resp_hds_complete) {
  1896. /* Server decided to close the stream after having sent us a final
  1897. * response. This is valid if it is not interested in the request
  1898. * body. This happens on 30x or 40x responses.
  1899. * We silently discard the data sent, since this is not a transport
  1900. * error situation. */
  1901. CURL_TRC_CF(data, cf, "[%d] discarding data"
  1902. "on closed stream with response", stream->id);
  1903. if(eos)
  1904. stream->body_eos = TRUE;
  1905. *err = CURLE_OK;
  1906. return (ssize_t)blen;
  1907. }
  1908. /* Server closed before we got a response, this is an error */
  1909. infof(data, "stream %u closed", stream->id);
  1910. *err = CURLE_SEND_ERROR;
  1911. return -1;
  1912. }
  1913. nwritten = Curl_bufq_write(&stream->sendbuf, buf, blen, err);
  1914. if(nwritten < 0)
  1915. return -1;
  1916. if(eos && (blen == (size_t)nwritten))
  1917. stream->body_eos = TRUE;
  1918. if(eos || !Curl_bufq_is_empty(&stream->sendbuf)) {
  1919. /* resume the potentially suspended stream */
  1920. int rv = nghttp2_session_resume_data(ctx->h2, stream->id);
  1921. if(nghttp2_is_fatal(rv)) {
  1922. *err = CURLE_SEND_ERROR;
  1923. return -1;
  1924. }
  1925. }
  1926. return nwritten;
  1927. }
  1928. static ssize_t h2_submit(struct h2_stream_ctx **pstream,
  1929. struct Curl_cfilter *cf, struct Curl_easy *data,
  1930. const void *buf, size_t len,
  1931. bool eos, CURLcode *err)
  1932. {
  1933. struct cf_h2_ctx *ctx = cf->ctx;
  1934. struct h2_stream_ctx *stream = NULL;
  1935. struct dynhds h2_headers;
  1936. nghttp2_nv *nva = NULL;
  1937. const void *body = NULL;
  1938. size_t nheader, bodylen, i;
  1939. nghttp2_data_provider data_prd;
  1940. int32_t stream_id;
  1941. nghttp2_priority_spec pri_spec;
  1942. ssize_t nwritten;
  1943. Curl_dynhds_init(&h2_headers, 0, DYN_HTTP_REQUEST);
  1944. *err = http2_data_setup(cf, data, &stream);
  1945. if(*err) {
  1946. nwritten = -1;
  1947. goto out;
  1948. }
  1949. nwritten = Curl_h1_req_parse_read(&stream->h1, buf, len, NULL, 0, err);
  1950. if(nwritten < 0)
  1951. goto out;
  1952. if(!stream->h1.done) {
  1953. /* need more data */
  1954. goto out;
  1955. }
  1956. DEBUGASSERT(stream->h1.req);
  1957. *err = Curl_http_req_to_h2(&h2_headers, stream->h1.req, data);
  1958. if(*err) {
  1959. nwritten = -1;
  1960. goto out;
  1961. }
  1962. /* no longer needed */
  1963. Curl_h1_req_parse_free(&stream->h1);
  1964. nva = Curl_dynhds_to_nva(&h2_headers, &nheader);
  1965. if(!nva) {
  1966. *err = CURLE_OUT_OF_MEMORY;
  1967. nwritten = -1;
  1968. goto out;
  1969. }
  1970. h2_pri_spec(ctx, data, &pri_spec);
  1971. if(!nghttp2_session_check_request_allowed(ctx->h2))
  1972. CURL_TRC_CF(data, cf, "send request NOT allowed (via nghttp2)");
  1973. switch(data->state.httpreq) {
  1974. case HTTPREQ_POST:
  1975. case HTTPREQ_POST_FORM:
  1976. case HTTPREQ_POST_MIME:
  1977. case HTTPREQ_PUT:
  1978. data_prd.read_callback = req_body_read_callback;
  1979. data_prd.source.ptr = NULL;
  1980. stream_id = nghttp2_submit_request(ctx->h2, &pri_spec, nva, nheader,
  1981. &data_prd, data);
  1982. break;
  1983. default:
  1984. stream_id = nghttp2_submit_request(ctx->h2, &pri_spec, nva, nheader,
  1985. NULL, data);
  1986. }
  1987. if(stream_id < 0) {
  1988. CURL_TRC_CF(data, cf, "send: nghttp2_submit_request error (%s)%u",
  1989. nghttp2_strerror(stream_id), stream_id);
  1990. *err = CURLE_SEND_ERROR;
  1991. nwritten = -1;
  1992. goto out;
  1993. }
  1994. #define MAX_ACC 60000 /* <64KB to account for some overhead */
  1995. if(Curl_trc_is_verbose(data)) {
  1996. size_t acc = 0;
  1997. infof(data, "[HTTP/2] [%d] OPENED stream for %s",
  1998. stream_id, data->state.url);
  1999. for(i = 0; i < nheader; ++i) {
  2000. acc += nva[i].namelen + nva[i].valuelen;
  2001. infof(data, "[HTTP/2] [%d] [%.*s: %.*s]", stream_id,
  2002. (int)nva[i].namelen, nva[i].name,
  2003. (int)nva[i].valuelen, nva[i].value);
  2004. }
  2005. if(acc > MAX_ACC) {
  2006. infof(data, "[HTTP/2] Warning: The cumulative length of all "
  2007. "headers exceeds %d bytes and that could cause the "
  2008. "stream to be rejected.", MAX_ACC);
  2009. }
  2010. }
  2011. stream->id = stream_id;
  2012. body = (const char *)buf + nwritten;
  2013. bodylen = len - nwritten;
  2014. if(bodylen || eos) {
  2015. ssize_t n = cf_h2_body_send(cf, data, stream, body, bodylen, eos, err);
  2016. if(n >= 0)
  2017. nwritten += n;
  2018. else if(*err == CURLE_AGAIN)
  2019. *err = CURLE_OK;
  2020. else if(*err != CURLE_AGAIN) {
  2021. *err = CURLE_SEND_ERROR;
  2022. nwritten = -1;
  2023. goto out;
  2024. }
  2025. }
  2026. out:
  2027. CURL_TRC_CF(data, cf, "[%d] submit -> %zd, %d",
  2028. stream ? stream->id : -1, nwritten, *err);
  2029. Curl_safefree(nva);
  2030. *pstream = stream;
  2031. Curl_dynhds_free(&h2_headers);
  2032. return nwritten;
  2033. }
  2034. static ssize_t cf_h2_send(struct Curl_cfilter *cf, struct Curl_easy *data,
  2035. const void *buf, size_t len, bool eos,
  2036. CURLcode *err)
  2037. {
  2038. struct cf_h2_ctx *ctx = cf->ctx;
  2039. struct h2_stream_ctx *stream = H2_STREAM_CTX(ctx, data);
  2040. struct cf_call_data save;
  2041. ssize_t nwritten;
  2042. CURLcode result;
  2043. CF_DATA_SAVE(save, cf, data);
  2044. if(!stream || stream->id == -1) {
  2045. nwritten = h2_submit(&stream, cf, data, buf, len, eos, err);
  2046. if(nwritten < 0) {
  2047. goto out;
  2048. }
  2049. DEBUGASSERT(stream);
  2050. }
  2051. else if(stream->body_eos) {
  2052. /* We already wrote this, but CURLE_AGAINed the call due to not
  2053. * being able to flush stream->sendbuf. Make a 0-length write
  2054. * to trigger flushing again.
  2055. * If this works, we report to have written `len` bytes. */
  2056. DEBUGASSERT(eos);
  2057. nwritten = cf_h2_body_send(cf, data, stream, buf, 0, eos, err);
  2058. CURL_TRC_CF(data, cf, "[%d] cf_body_send last CHUNK -> %zd, %d, eos=%d",
  2059. stream->id, nwritten, *err, eos);
  2060. if(nwritten < 0) {
  2061. goto out;
  2062. }
  2063. nwritten = len;
  2064. }
  2065. else {
  2066. nwritten = cf_h2_body_send(cf, data, stream, buf, len, eos, err);
  2067. CURL_TRC_CF(data, cf, "[%d] cf_body_send(len=%zu) -> %zd, %d, eos=%d",
  2068. stream->id, len, nwritten, *err, eos);
  2069. }
  2070. /* Call the nghttp2 send loop and flush to write ALL buffered data,
  2071. * headers and/or request body completely out to the network */
  2072. result = h2_progress_egress(cf, data);
  2073. /* if the stream has been closed in egress handling (nghttp2 does that
  2074. * when it does not like the headers, for example */
  2075. if(stream && stream->closed) {
  2076. infof(data, "stream %u closed", stream->id);
  2077. *err = CURLE_SEND_ERROR;
  2078. nwritten = -1;
  2079. goto out;
  2080. }
  2081. else if(result && (result != CURLE_AGAIN)) {
  2082. *err = result;
  2083. nwritten = -1;
  2084. goto out;
  2085. }
  2086. if(should_close_session(ctx)) {
  2087. /* nghttp2 thinks this session is done. If the stream has not been
  2088. * closed, this is an error state for out transfer */
  2089. if(stream->closed) {
  2090. nwritten = http2_handle_stream_close(cf, data, stream, err);
  2091. }
  2092. else {
  2093. CURL_TRC_CF(data, cf, "send: nothing to do in this session");
  2094. *err = CURLE_HTTP2;
  2095. nwritten = -1;
  2096. }
  2097. }
  2098. out:
  2099. if(stream) {
  2100. CURL_TRC_CF(data, cf, "[%d] cf_send(len=%zu) -> %zd, %d, "
  2101. "eos=%d, h2 windows %d-%d (stream-conn), "
  2102. "buffers %zu-%zu (stream-conn)",
  2103. stream->id, len, nwritten, *err,
  2104. stream->body_eos,
  2105. nghttp2_session_get_stream_remote_window_size(
  2106. ctx->h2, stream->id),
  2107. nghttp2_session_get_remote_window_size(ctx->h2),
  2108. Curl_bufq_len(&stream->sendbuf),
  2109. Curl_bufq_len(&ctx->outbufq));
  2110. }
  2111. else {
  2112. CURL_TRC_CF(data, cf, "cf_send(len=%zu) -> %zd, %d, "
  2113. "connection-window=%d, nw_send_buffer(%zu)",
  2114. len, nwritten, *err,
  2115. nghttp2_session_get_remote_window_size(ctx->h2),
  2116. Curl_bufq_len(&ctx->outbufq));
  2117. }
  2118. CF_DATA_RESTORE(cf, save);
  2119. return nwritten;
  2120. }
  2121. static CURLcode cf_h2_flush(struct Curl_cfilter *cf,
  2122. struct Curl_easy *data)
  2123. {
  2124. struct cf_h2_ctx *ctx = cf->ctx;
  2125. struct h2_stream_ctx *stream = H2_STREAM_CTX(ctx, data);
  2126. struct cf_call_data save;
  2127. CURLcode result = CURLE_OK;
  2128. CF_DATA_SAVE(save, cf, data);
  2129. if(stream && !Curl_bufq_is_empty(&stream->sendbuf)) {
  2130. /* resume the potentially suspended stream */
  2131. int rv = nghttp2_session_resume_data(ctx->h2, stream->id);
  2132. if(nghttp2_is_fatal(rv)) {
  2133. result = CURLE_SEND_ERROR;
  2134. goto out;
  2135. }
  2136. }
  2137. result = h2_progress_egress(cf, data);
  2138. out:
  2139. if(stream) {
  2140. CURL_TRC_CF(data, cf, "[%d] flush -> %d, "
  2141. "h2 windows %d-%d (stream-conn), "
  2142. "buffers %zu-%zu (stream-conn)",
  2143. stream->id, result,
  2144. nghttp2_session_get_stream_remote_window_size(
  2145. ctx->h2, stream->id),
  2146. nghttp2_session_get_remote_window_size(ctx->h2),
  2147. Curl_bufq_len(&stream->sendbuf),
  2148. Curl_bufq_len(&ctx->outbufq));
  2149. }
  2150. else {
  2151. CURL_TRC_CF(data, cf, "flush -> %d, "
  2152. "connection-window=%d, nw_send_buffer(%zu)",
  2153. result, nghttp2_session_get_remote_window_size(ctx->h2),
  2154. Curl_bufq_len(&ctx->outbufq));
  2155. }
  2156. CF_DATA_RESTORE(cf, save);
  2157. return result;
  2158. }
  2159. static void cf_h2_adjust_pollset(struct Curl_cfilter *cf,
  2160. struct Curl_easy *data,
  2161. struct easy_pollset *ps)
  2162. {
  2163. struct cf_h2_ctx *ctx = cf->ctx;
  2164. struct cf_call_data save;
  2165. curl_socket_t sock;
  2166. bool want_recv, want_send;
  2167. if(!ctx->h2)
  2168. return;
  2169. sock = Curl_conn_cf_get_socket(cf, data);
  2170. Curl_pollset_check(data, ps, sock, &want_recv, &want_send);
  2171. if(want_recv || want_send) {
  2172. struct h2_stream_ctx *stream = H2_STREAM_CTX(ctx, data);
  2173. bool c_exhaust, s_exhaust;
  2174. CF_DATA_SAVE(save, cf, data);
  2175. c_exhaust = want_send && !nghttp2_session_get_remote_window_size(ctx->h2);
  2176. s_exhaust = want_send && stream && stream->id >= 0 &&
  2177. !nghttp2_session_get_stream_remote_window_size(ctx->h2,
  2178. stream->id);
  2179. want_recv = (want_recv || c_exhaust || s_exhaust);
  2180. want_send = (!s_exhaust && want_send) ||
  2181. (!c_exhaust && nghttp2_session_want_write(ctx->h2)) ||
  2182. !Curl_bufq_is_empty(&ctx->outbufq);
  2183. Curl_pollset_set(data, ps, sock, want_recv, want_send);
  2184. CF_DATA_RESTORE(cf, save);
  2185. }
  2186. else if(ctx->sent_goaway && !cf->shutdown) {
  2187. /* shutdown in progress */
  2188. CF_DATA_SAVE(save, cf, data);
  2189. want_send = nghttp2_session_want_write(ctx->h2) ||
  2190. !Curl_bufq_is_empty(&ctx->outbufq);
  2191. want_recv = nghttp2_session_want_read(ctx->h2);
  2192. Curl_pollset_set(data, ps, sock, want_recv, want_send);
  2193. CF_DATA_RESTORE(cf, save);
  2194. }
  2195. }
  2196. static CURLcode cf_h2_connect(struct Curl_cfilter *cf,
  2197. struct Curl_easy *data,
  2198. bool blocking, bool *done)
  2199. {
  2200. struct cf_h2_ctx *ctx = cf->ctx;
  2201. CURLcode result = CURLE_OK;
  2202. struct cf_call_data save;
  2203. bool first_time = FALSE;
  2204. if(cf->connected) {
  2205. *done = TRUE;
  2206. return CURLE_OK;
  2207. }
  2208. /* Connect the lower filters first */
  2209. if(!cf->next->connected) {
  2210. result = Curl_conn_cf_connect(cf->next, data, blocking, done);
  2211. if(result || !*done)
  2212. return result;
  2213. }
  2214. *done = FALSE;
  2215. CF_DATA_SAVE(save, cf, data);
  2216. DEBUGASSERT(ctx->initialized);
  2217. if(!ctx->h2) {
  2218. result = cf_h2_ctx_open(cf, data);
  2219. if(result)
  2220. goto out;
  2221. first_time = TRUE;
  2222. }
  2223. if(!first_time) {
  2224. result = h2_progress_ingress(cf, data, H2_CHUNK_SIZE);
  2225. if(result)
  2226. goto out;
  2227. }
  2228. /* Send out our SETTINGS and ACKs and such. If that blocks, we
  2229. * have it buffered and can count this filter as being connected */
  2230. result = h2_progress_egress(cf, data);
  2231. if(result && (result != CURLE_AGAIN))
  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 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 ? 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(!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. void *Curl_nghttp2_malloc(size_t size, void *user_data)
  2632. {
  2633. (void)user_data;
  2634. return Curl_cmalloc(size);
  2635. }
  2636. void Curl_nghttp2_free(void *ptr, void *user_data)
  2637. {
  2638. (void)user_data;
  2639. Curl_cfree(ptr);
  2640. }
  2641. void *Curl_nghttp2_calloc(size_t nmemb, size_t size, void *user_data)
  2642. {
  2643. (void)user_data;
  2644. return Curl_ccalloc(nmemb, size);
  2645. }
  2646. void *Curl_nghttp2_realloc(void *ptr, size_t size, void *user_data)
  2647. {
  2648. (void)user_data;
  2649. return Curl_crealloc(ptr, size);
  2650. }
  2651. #else /* !USE_NGHTTP2 */
  2652. /* Satisfy external references even if http2 is not compiled in. */
  2653. #include <curl/curl.h>
  2654. char *curl_pushheader_bynum(struct curl_pushheaders *h, size_t num)
  2655. {
  2656. (void) h;
  2657. (void) num;
  2658. return NULL;
  2659. }
  2660. char *curl_pushheader_byname(struct curl_pushheaders *h, const char *header)
  2661. {
  2662. (void) h;
  2663. (void) header;
  2664. return NULL;
  2665. }
  2666. #endif /* USE_NGHTTP2 */