http2.c 88 KB

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