http2.c 95 KB

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