2
0

http2.c 90 KB

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