http2.c 91 KB

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