http2.c 88 KB

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