2
0

http2.c 81 KB

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