http2.c 92 KB

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