http2.c 88 KB

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