http2.c 88 KB

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