http2.c 86 KB

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