http2.c 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969
  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. if(!cf->connected)
  701. nwritten = Curl_bufq_write(&ctx->outbufq, buf, blen, &result);
  702. else
  703. nwritten = Curl_bufq_write_pass(&ctx->outbufq, buf, blen,
  704. nw_out_writer, cf, &result);
  705. if(nwritten < 0) {
  706. if(result == CURLE_AGAIN) {
  707. ctx->nw_out_blocked = 1;
  708. return NGHTTP2_ERR_WOULDBLOCK;
  709. }
  710. failf(data, "Failed sending HTTP2 data");
  711. return NGHTTP2_ERR_CALLBACK_FAILURE;
  712. }
  713. if(!nwritten) {
  714. ctx->nw_out_blocked = 1;
  715. return NGHTTP2_ERR_WOULDBLOCK;
  716. }
  717. return nwritten;
  718. }
  719. /* We pass a pointer to this struct in the push callback, but the contents of
  720. the struct are hidden from the user. */
  721. struct curl_pushheaders {
  722. struct Curl_easy *data;
  723. struct h2_stream_ctx *stream;
  724. const nghttp2_push_promise *frame;
  725. };
  726. /*
  727. * push header access function. Only to be used from within the push callback
  728. */
  729. char *curl_pushheader_bynum(struct curl_pushheaders *h, size_t num)
  730. {
  731. /* Verify that we got a good easy handle in the push header struct, mostly to
  732. detect rubbish input fast(er). */
  733. if(!h || !GOOD_EASY_HANDLE(h->data))
  734. return NULL;
  735. else {
  736. if(h->stream && num < h->stream->push_headers_used)
  737. return h->stream->push_headers[num];
  738. }
  739. return NULL;
  740. }
  741. /*
  742. * push header access function. Only to be used from within the push callback
  743. */
  744. char *curl_pushheader_byname(struct curl_pushheaders *h, const char *header)
  745. {
  746. struct h2_stream_ctx *stream;
  747. size_t len;
  748. size_t i;
  749. /* Verify that we got a good easy handle in the push header struct,
  750. mostly to detect rubbish input fast(er). Also empty header name
  751. is just a rubbish too. We have to allow ":" at the beginning of
  752. the header, but header == ":" must be rejected. If we have ':' in
  753. the middle of header, it could be matched in middle of the value,
  754. this is because we do prefix match.*/
  755. if(!h || !GOOD_EASY_HANDLE(h->data) || !header || !header[0] ||
  756. !strcmp(header, ":") || strchr(header + 1, ':'))
  757. return NULL;
  758. stream = h->stream;
  759. if(!stream)
  760. return NULL;
  761. len = strlen(header);
  762. for(i = 0; i < stream->push_headers_used; i++) {
  763. if(!strncmp(header, stream->push_headers[i], len)) {
  764. /* sub-match, make sure that it is followed by a colon */
  765. if(stream->push_headers[i][len] != ':')
  766. continue;
  767. return &stream->push_headers[i][len + 1];
  768. }
  769. }
  770. return NULL;
  771. }
  772. static struct Curl_easy *h2_duphandle(struct Curl_cfilter *cf,
  773. struct Curl_easy *data)
  774. {
  775. struct Curl_easy *second = curl_easy_duphandle(data);
  776. if(second) {
  777. struct h2_stream_ctx *second_stream;
  778. http2_data_setup(cf, second, &second_stream);
  779. second->state.priority.weight = data->state.priority.weight;
  780. }
  781. return second;
  782. }
  783. static int set_transfer_url(struct Curl_easy *data,
  784. struct curl_pushheaders *hp)
  785. {
  786. const char *v;
  787. CURLUcode uc;
  788. char *url = NULL;
  789. int rc = 0;
  790. CURLU *u = curl_url();
  791. if(!u)
  792. return 5;
  793. v = curl_pushheader_byname(hp, HTTP_PSEUDO_SCHEME);
  794. if(v) {
  795. uc = curl_url_set(u, CURLUPART_SCHEME, v, 0);
  796. if(uc) {
  797. rc = 1;
  798. goto fail;
  799. }
  800. }
  801. v = curl_pushheader_byname(hp, HTTP_PSEUDO_AUTHORITY);
  802. if(v) {
  803. uc = Curl_url_set_authority(u, v);
  804. if(uc) {
  805. rc = 2;
  806. goto fail;
  807. }
  808. }
  809. v = curl_pushheader_byname(hp, HTTP_PSEUDO_PATH);
  810. if(v) {
  811. uc = curl_url_set(u, CURLUPART_PATH, v, 0);
  812. if(uc) {
  813. rc = 3;
  814. goto fail;
  815. }
  816. }
  817. uc = curl_url_get(u, CURLUPART_URL, &url, 0);
  818. if(uc)
  819. rc = 4;
  820. fail:
  821. curl_url_cleanup(u);
  822. if(rc)
  823. return rc;
  824. if(data->state.url_alloc)
  825. free(data->state.url);
  826. data->state.url_alloc = TRUE;
  827. data->state.url = url;
  828. return 0;
  829. }
  830. static void discard_newhandle(struct Curl_cfilter *cf,
  831. struct Curl_easy *newhandle)
  832. {
  833. http2_data_done(cf, newhandle);
  834. (void)Curl_close(&newhandle);
  835. }
  836. static int push_promise(struct Curl_cfilter *cf,
  837. struct Curl_easy *data,
  838. const nghttp2_push_promise *frame)
  839. {
  840. struct cf_h2_ctx *ctx = cf->ctx;
  841. int rv; /* one of the CURL_PUSH_* defines */
  842. CURL_TRC_CF(data, cf, "[%d] PUSH_PROMISE received",
  843. frame->promised_stream_id);
  844. if(data->multi->push_cb) {
  845. struct h2_stream_ctx *stream;
  846. struct h2_stream_ctx *newstream;
  847. struct curl_pushheaders heads;
  848. CURLMcode rc;
  849. CURLcode result;
  850. /* clone the parent */
  851. struct Curl_easy *newhandle = h2_duphandle(cf, data);
  852. if(!newhandle) {
  853. infof(data, "failed to duplicate handle");
  854. rv = CURL_PUSH_DENY; /* FAIL HARD */
  855. goto fail;
  856. }
  857. /* ask the application */
  858. CURL_TRC_CF(data, cf, "Got PUSH_PROMISE, ask application");
  859. stream = H2_STREAM_CTX(ctx, data);
  860. if(!stream) {
  861. failf(data, "Internal NULL stream");
  862. discard_newhandle(cf, newhandle);
  863. rv = CURL_PUSH_DENY;
  864. goto fail;
  865. }
  866. heads.data = data;
  867. heads.stream = stream;
  868. heads.frame = frame;
  869. rv = set_transfer_url(newhandle, &heads);
  870. if(rv) {
  871. discard_newhandle(cf, newhandle);
  872. rv = CURL_PUSH_DENY;
  873. goto fail;
  874. }
  875. result = http2_data_setup(cf, newhandle, &newstream);
  876. if(result) {
  877. failf(data, "error setting up stream: %d", result);
  878. discard_newhandle(cf, newhandle);
  879. rv = CURL_PUSH_DENY;
  880. goto fail;
  881. }
  882. DEBUGASSERT(stream);
  883. Curl_set_in_callback(data, TRUE);
  884. rv = data->multi->push_cb(data, newhandle,
  885. stream->push_headers_used, &heads,
  886. data->multi->push_userp);
  887. Curl_set_in_callback(data, FALSE);
  888. /* free the headers again */
  889. free_push_headers(stream);
  890. if(rv) {
  891. DEBUGASSERT((rv > CURL_PUSH_OK) && (rv <= CURL_PUSH_ERROROUT));
  892. /* denied, kill off the new handle again */
  893. discard_newhandle(cf, newhandle);
  894. goto fail;
  895. }
  896. newstream->id = frame->promised_stream_id;
  897. newhandle->req.maxdownload = -1;
  898. newhandle->req.size = -1;
  899. /* approved, add to the multi handle and immediately switch to PERFORM
  900. state with the given connection !*/
  901. rc = Curl_multi_add_perform(data->multi, newhandle, cf->conn);
  902. if(rc) {
  903. infof(data, "failed to add handle to multi");
  904. discard_newhandle(cf, newhandle);
  905. rv = CURL_PUSH_DENY;
  906. goto fail;
  907. }
  908. rv = nghttp2_session_set_stream_user_data(ctx->h2,
  909. newstream->id,
  910. newhandle);
  911. if(rv) {
  912. infof(data, "failed to set user_data for stream %u",
  913. newstream->id);
  914. DEBUGASSERT(0);
  915. rv = CURL_PUSH_DENY;
  916. goto fail;
  917. }
  918. /* success, remember max stream id processed */
  919. if(newstream->id > ctx->local_max_sid)
  920. ctx->local_max_sid = newstream->id;
  921. }
  922. else {
  923. CURL_TRC_CF(data, cf, "Got PUSH_PROMISE, ignore it");
  924. rv = CURL_PUSH_DENY;
  925. }
  926. fail:
  927. return rv;
  928. }
  929. static void h2_xfer_write_resp_hd(struct Curl_cfilter *cf,
  930. struct Curl_easy *data,
  931. struct h2_stream_ctx *stream,
  932. const char *buf, size_t blen, bool eos)
  933. {
  934. /* If we already encountered an error, skip further writes */
  935. if(!stream->xfer_result) {
  936. stream->xfer_result = Curl_xfer_write_resp_hd(data, buf, blen, eos);
  937. if(!stream->xfer_result && !eos)
  938. stream->xfer_result = cf_h2_update_local_win(cf, data, stream, FALSE);
  939. if(stream->xfer_result)
  940. CURL_TRC_CF(data, cf, "[%d] error %d writing %zu bytes of headers",
  941. stream->id, stream->xfer_result, blen);
  942. }
  943. }
  944. static void h2_xfer_write_resp(struct Curl_cfilter *cf,
  945. struct Curl_easy *data,
  946. struct h2_stream_ctx *stream,
  947. const char *buf, size_t blen, bool eos)
  948. {
  949. /* If we already encountered an error, skip further writes */
  950. if(!stream->xfer_result)
  951. stream->xfer_result = Curl_xfer_write_resp(data, buf, blen, eos);
  952. if(!stream->xfer_result && !eos)
  953. stream->xfer_result = cf_h2_update_local_win(cf, data, stream, FALSE);
  954. /* If the transfer write is errored, we do not want any more data */
  955. if(stream->xfer_result) {
  956. struct cf_h2_ctx *ctx = cf->ctx;
  957. CURL_TRC_CF(data, cf, "[%d] error %d writing %zu bytes of data, "
  958. "RST-ing stream",
  959. stream->id, stream->xfer_result, blen);
  960. nghttp2_submit_rst_stream(ctx->h2, 0, stream->id,
  961. (uint32_t)NGHTTP2_ERR_CALLBACK_FAILURE);
  962. }
  963. }
  964. static CURLcode on_stream_frame(struct Curl_cfilter *cf,
  965. struct Curl_easy *data,
  966. const nghttp2_frame *frame)
  967. {
  968. struct cf_h2_ctx *ctx = cf->ctx;
  969. struct h2_stream_ctx *stream = H2_STREAM_CTX(ctx, data);
  970. int32_t stream_id = frame->hd.stream_id;
  971. int rv;
  972. if(!stream) {
  973. CURL_TRC_CF(data, cf, "[%d] No stream_ctx set", stream_id);
  974. return CURLE_FAILED_INIT;
  975. }
  976. switch(frame->hd.type) {
  977. case NGHTTP2_DATA:
  978. CURL_TRC_CF(data, cf, "[%d] DATA, window=%d/%d",
  979. stream_id,
  980. nghttp2_session_get_stream_effective_recv_data_length(
  981. ctx->h2, stream->id),
  982. nghttp2_session_get_stream_effective_local_window_size(
  983. ctx->h2, stream->id));
  984. /* If !body started on this stream, then receiving DATA is illegal. */
  985. if(!stream->bodystarted) {
  986. rv = nghttp2_submit_rst_stream(ctx->h2, NGHTTP2_FLAG_NONE,
  987. stream_id, NGHTTP2_PROTOCOL_ERROR);
  988. if(nghttp2_is_fatal(rv)) {
  989. return CURLE_RECV_ERROR;
  990. }
  991. }
  992. if(frame->hd.flags & NGHTTP2_FLAG_END_STREAM) {
  993. drain_stream(cf, data, stream);
  994. }
  995. break;
  996. case NGHTTP2_HEADERS:
  997. if(stream->bodystarted) {
  998. /* Only valid HEADERS after body started is trailer HEADERS. We
  999. buffer them in on_header callback. */
  1000. break;
  1001. }
  1002. /* nghttp2 guarantees that :status is received, and we store it to
  1003. stream->status_code. Fuzzing has proven this can still be reached
  1004. without status code having been set. */
  1005. if(stream->status_code == -1)
  1006. return CURLE_RECV_ERROR;
  1007. /* Only final status code signals the end of header */
  1008. if(stream->status_code / 100 != 1) {
  1009. stream->bodystarted = TRUE;
  1010. stream->status_code = -1;
  1011. }
  1012. h2_xfer_write_resp_hd(cf, data, stream, STRCONST("\r\n"), stream->closed);
  1013. if(stream->status_code / 100 != 1) {
  1014. stream->resp_hds_complete = TRUE;
  1015. }
  1016. drain_stream(cf, data, stream);
  1017. break;
  1018. case NGHTTP2_PUSH_PROMISE:
  1019. rv = push_promise(cf, data, &frame->push_promise);
  1020. if(rv) { /* deny! */
  1021. DEBUGASSERT((rv > CURL_PUSH_OK) && (rv <= CURL_PUSH_ERROROUT));
  1022. rv = nghttp2_submit_rst_stream(ctx->h2, NGHTTP2_FLAG_NONE,
  1023. frame->push_promise.promised_stream_id,
  1024. NGHTTP2_CANCEL);
  1025. if(nghttp2_is_fatal(rv))
  1026. return CURLE_SEND_ERROR;
  1027. else if(rv == CURL_PUSH_ERROROUT) {
  1028. CURL_TRC_CF(data, cf, "[%d] fail in PUSH_PROMISE received",
  1029. stream_id);
  1030. return CURLE_RECV_ERROR;
  1031. }
  1032. }
  1033. break;
  1034. case NGHTTP2_RST_STREAM:
  1035. stream->closed = TRUE;
  1036. if(frame->rst_stream.error_code) {
  1037. stream->reset = TRUE;
  1038. }
  1039. drain_stream(cf, data, stream);
  1040. break;
  1041. case NGHTTP2_WINDOW_UPDATE:
  1042. if(CURL_WANT_SEND(data) && Curl_bufq_is_empty(&stream->sendbuf)) {
  1043. /* need more data, force processing of transfer */
  1044. drain_stream(cf, data, stream);
  1045. }
  1046. else if(!Curl_bufq_is_empty(&stream->sendbuf)) {
  1047. /* resume the potentially suspended stream */
  1048. rv = nghttp2_session_resume_data(ctx->h2, stream->id);
  1049. if(nghttp2_is_fatal(rv))
  1050. return CURLE_SEND_ERROR;
  1051. }
  1052. break;
  1053. default:
  1054. break;
  1055. }
  1056. return CURLE_OK;
  1057. }
  1058. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  1059. static int fr_print(const nghttp2_frame *frame, char *buffer, size_t blen)
  1060. {
  1061. switch(frame->hd.type) {
  1062. case NGHTTP2_DATA: {
  1063. return msnprintf(buffer, blen,
  1064. "FRAME[DATA, len=%d, eos=%d, padlen=%d]",
  1065. (int)frame->hd.length,
  1066. !!(frame->hd.flags & NGHTTP2_FLAG_END_STREAM),
  1067. (int)frame->data.padlen);
  1068. }
  1069. case NGHTTP2_HEADERS: {
  1070. return msnprintf(buffer, blen,
  1071. "FRAME[HEADERS, len=%d, hend=%d, eos=%d]",
  1072. (int)frame->hd.length,
  1073. !!(frame->hd.flags & NGHTTP2_FLAG_END_HEADERS),
  1074. !!(frame->hd.flags & NGHTTP2_FLAG_END_STREAM));
  1075. }
  1076. case NGHTTP2_PRIORITY: {
  1077. return msnprintf(buffer, blen,
  1078. "FRAME[PRIORITY, len=%d, flags=%d]",
  1079. (int)frame->hd.length, frame->hd.flags);
  1080. }
  1081. case NGHTTP2_RST_STREAM: {
  1082. return msnprintf(buffer, blen,
  1083. "FRAME[RST_STREAM, len=%d, flags=%d, error=%u]",
  1084. (int)frame->hd.length, frame->hd.flags,
  1085. frame->rst_stream.error_code);
  1086. }
  1087. case NGHTTP2_SETTINGS: {
  1088. if(frame->hd.flags & NGHTTP2_FLAG_ACK) {
  1089. return msnprintf(buffer, blen, "FRAME[SETTINGS, ack=1]");
  1090. }
  1091. return msnprintf(buffer, blen,
  1092. "FRAME[SETTINGS, len=%d]", (int)frame->hd.length);
  1093. }
  1094. case NGHTTP2_PUSH_PROMISE: {
  1095. return msnprintf(buffer, blen,
  1096. "FRAME[PUSH_PROMISE, len=%d, hend=%d]",
  1097. (int)frame->hd.length,
  1098. !!(frame->hd.flags & NGHTTP2_FLAG_END_HEADERS));
  1099. }
  1100. case NGHTTP2_PING: {
  1101. return msnprintf(buffer, blen,
  1102. "FRAME[PING, len=%d, ack=%d]",
  1103. (int)frame->hd.length,
  1104. frame->hd.flags&NGHTTP2_FLAG_ACK);
  1105. }
  1106. case NGHTTP2_GOAWAY: {
  1107. char scratch[128];
  1108. size_t s_len = sizeof(scratch)/sizeof(scratch[0]);
  1109. size_t len = (frame->goaway.opaque_data_len < s_len) ?
  1110. frame->goaway.opaque_data_len : s_len-1;
  1111. if(len)
  1112. memcpy(scratch, frame->goaway.opaque_data, len);
  1113. scratch[len] = '\0';
  1114. return msnprintf(buffer, blen, "FRAME[GOAWAY, error=%d, reason='%s', "
  1115. "last_stream=%d]", frame->goaway.error_code,
  1116. scratch, frame->goaway.last_stream_id);
  1117. }
  1118. case NGHTTP2_WINDOW_UPDATE: {
  1119. return msnprintf(buffer, blen,
  1120. "FRAME[WINDOW_UPDATE, incr=%d]",
  1121. frame->window_update.window_size_increment);
  1122. }
  1123. default:
  1124. return msnprintf(buffer, blen, "FRAME[%d, len=%d, flags=%d]",
  1125. frame->hd.type, (int)frame->hd.length,
  1126. frame->hd.flags);
  1127. }
  1128. }
  1129. static int on_frame_send(nghttp2_session *session, const nghttp2_frame *frame,
  1130. void *userp)
  1131. {
  1132. struct Curl_cfilter *cf = userp;
  1133. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  1134. (void)session;
  1135. DEBUGASSERT(data);
  1136. if(data && Curl_trc_cf_is_verbose(cf, data)) {
  1137. char buffer[256];
  1138. int len;
  1139. len = fr_print(frame, buffer, sizeof(buffer)-1);
  1140. buffer[len] = 0;
  1141. CURL_TRC_CF(data, cf, "[%d] -> %s", frame->hd.stream_id, buffer);
  1142. }
  1143. return 0;
  1144. }
  1145. #endif /* !CURL_DISABLE_VERBOSE_STRINGS */
  1146. static int on_frame_recv(nghttp2_session *session, const nghttp2_frame *frame,
  1147. void *userp)
  1148. {
  1149. struct Curl_cfilter *cf = userp;
  1150. struct cf_h2_ctx *ctx = cf->ctx;
  1151. struct Curl_easy *data = CF_DATA_CURRENT(cf), *data_s;
  1152. int32_t stream_id = frame->hd.stream_id;
  1153. DEBUGASSERT(data);
  1154. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  1155. if(Curl_trc_cf_is_verbose(cf, data)) {
  1156. char buffer[256];
  1157. int len;
  1158. len = fr_print(frame, buffer, sizeof(buffer)-1);
  1159. buffer[len] = 0;
  1160. CURL_TRC_CF(data, cf, "[%d] <- %s",frame->hd.stream_id, buffer);
  1161. }
  1162. #endif /* !CURL_DISABLE_VERBOSE_STRINGS */
  1163. if(!stream_id) {
  1164. /* stream ID zero is for connection-oriented stuff */
  1165. DEBUGASSERT(data);
  1166. switch(frame->hd.type) {
  1167. case NGHTTP2_SETTINGS: {
  1168. if(!(frame->hd.flags & NGHTTP2_FLAG_ACK)) {
  1169. uint32_t max_conn = ctx->max_concurrent_streams;
  1170. ctx->max_concurrent_streams = nghttp2_session_get_remote_settings(
  1171. session, NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS);
  1172. ctx->enable_push = nghttp2_session_get_remote_settings(
  1173. session, NGHTTP2_SETTINGS_ENABLE_PUSH) != 0;
  1174. CURL_TRC_CF(data, cf, "[0] MAX_CONCURRENT_STREAMS: %d",
  1175. ctx->max_concurrent_streams);
  1176. CURL_TRC_CF(data, cf, "[0] ENABLE_PUSH: %s",
  1177. ctx->enable_push ? "TRUE" : "false");
  1178. if(data && max_conn != ctx->max_concurrent_streams) {
  1179. /* only signal change if the value actually changed */
  1180. CURL_TRC_CF(data, cf, "[0] notify MAX_CONCURRENT_STREAMS: %u",
  1181. ctx->max_concurrent_streams);
  1182. Curl_multi_connchanged(data->multi);
  1183. }
  1184. /* Since the initial stream window is 64K, a request might be on HOLD,
  1185. * due to exhaustion. The (initial) SETTINGS may announce a much larger
  1186. * window and *assume* that we treat this like a WINDOW_UPDATE. Some
  1187. * servers send an explicit WINDOW_UPDATE, but not all seem to do that.
  1188. * To be safe, we UNHOLD a stream in order not to stall. */
  1189. if(CURL_WANT_SEND(data)) {
  1190. struct h2_stream_ctx *stream = H2_STREAM_CTX(ctx, data);
  1191. if(stream)
  1192. drain_stream(cf, data, stream);
  1193. }
  1194. }
  1195. break;
  1196. }
  1197. case NGHTTP2_GOAWAY:
  1198. ctx->rcvd_goaway = TRUE;
  1199. ctx->goaway_error = frame->goaway.error_code;
  1200. ctx->remote_max_sid = frame->goaway.last_stream_id;
  1201. if(data) {
  1202. infof(data, "received GOAWAY, error=%u, last_stream=%u",
  1203. ctx->goaway_error, ctx->remote_max_sid);
  1204. Curl_multi_connchanged(data->multi);
  1205. }
  1206. break;
  1207. default:
  1208. break;
  1209. }
  1210. return 0;
  1211. }
  1212. data_s = nghttp2_session_get_stream_user_data(session, stream_id);
  1213. if(!data_s) {
  1214. CURL_TRC_CF(data, cf, "[%d] No Curl_easy associated", stream_id);
  1215. return 0;
  1216. }
  1217. return on_stream_frame(cf, data_s, frame) ? NGHTTP2_ERR_CALLBACK_FAILURE : 0;
  1218. }
  1219. static int on_data_chunk_recv(nghttp2_session *session, uint8_t flags,
  1220. int32_t stream_id,
  1221. const uint8_t *mem, size_t len, void *userp)
  1222. {
  1223. struct Curl_cfilter *cf = userp;
  1224. struct cf_h2_ctx *ctx = cf->ctx;
  1225. struct h2_stream_ctx *stream;
  1226. struct Curl_easy *data_s;
  1227. (void)flags;
  1228. DEBUGASSERT(stream_id); /* should never be a zero stream ID here */
  1229. DEBUGASSERT(CF_DATA_CURRENT(cf));
  1230. /* get the stream from the hash based on Stream ID */
  1231. data_s = nghttp2_session_get_stream_user_data(session, stream_id);
  1232. if(!data_s) {
  1233. /* Receiving a Stream ID not in the hash should not happen - unless
  1234. we have aborted a transfer artificially and there were more data
  1235. in the pipeline. Silently ignore. */
  1236. CURL_TRC_CF(CF_DATA_CURRENT(cf), cf, "[%d] Data for unknown",
  1237. stream_id);
  1238. /* consumed explicitly as no one will read it */
  1239. nghttp2_session_consume(session, stream_id, len);
  1240. return 0;
  1241. }
  1242. stream = H2_STREAM_CTX(ctx, data_s);
  1243. if(!stream)
  1244. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1245. h2_xfer_write_resp(cf, data_s, stream, (char *)mem, len, FALSE);
  1246. nghttp2_session_consume(ctx->h2, stream_id, len);
  1247. stream->nrcvd_data += (curl_off_t)len;
  1248. return 0;
  1249. }
  1250. static int on_stream_close(nghttp2_session *session, int32_t stream_id,
  1251. uint32_t error_code, void *userp)
  1252. {
  1253. struct Curl_cfilter *cf = userp;
  1254. struct cf_h2_ctx *ctx = cf->ctx;
  1255. struct Curl_easy *data_s, *call_data = CF_DATA_CURRENT(cf);
  1256. struct h2_stream_ctx *stream;
  1257. int rv;
  1258. (void)session;
  1259. DEBUGASSERT(call_data);
  1260. /* stream id 0 is the connection, do not look there for streams. */
  1261. data_s = stream_id ?
  1262. nghttp2_session_get_stream_user_data(session, stream_id) : NULL;
  1263. if(!data_s) {
  1264. CURL_TRC_CF(call_data, cf,
  1265. "[%d] on_stream_close, no easy set on stream", stream_id);
  1266. return 0;
  1267. }
  1268. if(!GOOD_EASY_HANDLE(data_s)) {
  1269. /* nghttp2 still has an easy registered for the stream which has
  1270. * been freed be libcurl. This points to a code path that does not
  1271. * trigger DONE or DETACH events as it must. */
  1272. CURL_TRC_CF(call_data, cf,
  1273. "[%d] on_stream_close, not a GOOD easy on stream", stream_id);
  1274. (void)nghttp2_session_set_stream_user_data(session, stream_id, 0);
  1275. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1276. }
  1277. stream = H2_STREAM_CTX(ctx, data_s);
  1278. if(!stream) {
  1279. CURL_TRC_CF(data_s, cf,
  1280. "[%d] on_stream_close, GOOD easy but no stream", stream_id);
  1281. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1282. }
  1283. stream->closed = TRUE;
  1284. stream->error = error_code;
  1285. if(stream->error) {
  1286. stream->reset = TRUE;
  1287. }
  1288. if(stream->error)
  1289. CURL_TRC_CF(data_s, cf, "[%d] RESET: %s (err %d)",
  1290. stream_id, nghttp2_http2_strerror(error_code), error_code);
  1291. else
  1292. CURL_TRC_CF(data_s, cf, "[%d] CLOSED", stream_id);
  1293. drain_stream(cf, data_s, stream);
  1294. /* remove `data_s` from the nghttp2 stream */
  1295. rv = nghttp2_session_set_stream_user_data(session, stream_id, 0);
  1296. if(rv) {
  1297. infof(data_s, "http/2: failed to clear user_data for stream %u",
  1298. stream_id);
  1299. DEBUGASSERT(0);
  1300. }
  1301. return 0;
  1302. }
  1303. static int on_begin_headers(nghttp2_session *session,
  1304. const nghttp2_frame *frame, void *userp)
  1305. {
  1306. struct Curl_cfilter *cf = userp;
  1307. struct cf_h2_ctx *ctx = cf->ctx;
  1308. struct h2_stream_ctx *stream;
  1309. struct Curl_easy *data_s = NULL;
  1310. (void)cf;
  1311. data_s = nghttp2_session_get_stream_user_data(session, frame->hd.stream_id);
  1312. if(!data_s) {
  1313. return 0;
  1314. }
  1315. if(frame->hd.type != NGHTTP2_HEADERS) {
  1316. return 0;
  1317. }
  1318. stream = H2_STREAM_CTX(ctx, data_s);
  1319. if(!stream || !stream->bodystarted) {
  1320. return 0;
  1321. }
  1322. return 0;
  1323. }
  1324. /* frame->hd.type is either NGHTTP2_HEADERS or NGHTTP2_PUSH_PROMISE */
  1325. static int on_header(nghttp2_session *session, const nghttp2_frame *frame,
  1326. const uint8_t *name, size_t namelen,
  1327. const uint8_t *value, size_t valuelen,
  1328. uint8_t flags,
  1329. void *userp)
  1330. {
  1331. struct Curl_cfilter *cf = userp;
  1332. struct cf_h2_ctx *ctx = cf->ctx;
  1333. struct h2_stream_ctx *stream;
  1334. struct Curl_easy *data_s;
  1335. int32_t stream_id = frame->hd.stream_id;
  1336. CURLcode result;
  1337. (void)flags;
  1338. DEBUGASSERT(stream_id); /* should never be a zero stream ID here */
  1339. /* get the stream from the hash based on Stream ID */
  1340. data_s = nghttp2_session_get_stream_user_data(session, stream_id);
  1341. if(!data_s)
  1342. /* Receiving a Stream ID not in the hash should not happen, this is an
  1343. internal error more than anything else! */
  1344. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1345. stream = H2_STREAM_CTX(ctx, data_s);
  1346. if(!stream) {
  1347. failf(data_s, "Internal NULL stream");
  1348. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1349. }
  1350. /* Store received PUSH_PROMISE headers to be used when the subsequent
  1351. PUSH_PROMISE callback comes */
  1352. if(frame->hd.type == NGHTTP2_PUSH_PROMISE) {
  1353. char *h;
  1354. if(!strcmp(HTTP_PSEUDO_AUTHORITY, (const char *)name)) {
  1355. /* pseudo headers are lower case */
  1356. int rc = 0;
  1357. char *check = aprintf("%s:%d", cf->conn->host.name,
  1358. cf->conn->remote_port);
  1359. if(!check)
  1360. /* no memory */
  1361. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1362. if(!strcasecompare(check, (const char *)value) &&
  1363. ((cf->conn->remote_port != cf->conn->given->defport) ||
  1364. !strcasecompare(cf->conn->host.name, (const char *)value))) {
  1365. /* This is push is not for the same authority that was asked for in
  1366. * the URL. RFC 7540 section 8.2 says: "A client MUST treat a
  1367. * PUSH_PROMISE for which the server is not authoritative as a stream
  1368. * error of type PROTOCOL_ERROR."
  1369. */
  1370. (void)nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
  1371. stream_id, NGHTTP2_PROTOCOL_ERROR);
  1372. rc = NGHTTP2_ERR_CALLBACK_FAILURE;
  1373. }
  1374. free(check);
  1375. if(rc)
  1376. return rc;
  1377. }
  1378. if(!stream->push_headers) {
  1379. stream->push_headers_alloc = 10;
  1380. stream->push_headers = malloc(stream->push_headers_alloc *
  1381. sizeof(char *));
  1382. if(!stream->push_headers)
  1383. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1384. stream->push_headers_used = 0;
  1385. }
  1386. else if(stream->push_headers_used ==
  1387. stream->push_headers_alloc) {
  1388. char **headp;
  1389. if(stream->push_headers_alloc > 1000) {
  1390. /* this is beyond crazy many headers, bail out */
  1391. failf(data_s, "Too many PUSH_PROMISE headers");
  1392. free_push_headers(stream);
  1393. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1394. }
  1395. stream->push_headers_alloc *= 2;
  1396. headp = realloc(stream->push_headers,
  1397. stream->push_headers_alloc * sizeof(char *));
  1398. if(!headp) {
  1399. free_push_headers(stream);
  1400. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1401. }
  1402. stream->push_headers = headp;
  1403. }
  1404. h = aprintf("%s:%s", name, value);
  1405. if(h)
  1406. stream->push_headers[stream->push_headers_used++] = h;
  1407. return 0;
  1408. }
  1409. if(stream->bodystarted) {
  1410. /* This is a trailer */
  1411. CURL_TRC_CF(data_s, cf, "[%d] trailer: %.*s: %.*s",
  1412. stream->id, (int)namelen, name, (int)valuelen, value);
  1413. result = Curl_dynhds_add(&stream->resp_trailers,
  1414. (const char *)name, namelen,
  1415. (const char *)value, valuelen);
  1416. if(result)
  1417. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1418. return 0;
  1419. }
  1420. if(namelen == sizeof(HTTP_PSEUDO_STATUS) - 1 &&
  1421. memcmp(HTTP_PSEUDO_STATUS, name, namelen) == 0) {
  1422. /* nghttp2 guarantees :status is received first and only once. */
  1423. char buffer[32];
  1424. result = Curl_http_decode_status(&stream->status_code,
  1425. (const char *)value, valuelen);
  1426. if(result)
  1427. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1428. msnprintf(buffer, sizeof(buffer), HTTP_PSEUDO_STATUS ":%u\r",
  1429. stream->status_code);
  1430. result = Curl_headers_push(data_s, buffer, CURLH_PSEUDO);
  1431. if(result)
  1432. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1433. Curl_dyn_reset(&ctx->scratch);
  1434. result = Curl_dyn_addn(&ctx->scratch, STRCONST("HTTP/2 "));
  1435. if(!result)
  1436. result = Curl_dyn_addn(&ctx->scratch, value, valuelen);
  1437. if(!result)
  1438. result = Curl_dyn_addn(&ctx->scratch, STRCONST(" \r\n"));
  1439. if(!result)
  1440. h2_xfer_write_resp_hd(cf, data_s, stream, Curl_dyn_ptr(&ctx->scratch),
  1441. Curl_dyn_len(&ctx->scratch), FALSE);
  1442. if(result)
  1443. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1444. /* if we receive data for another handle, wake that up */
  1445. if(CF_DATA_CURRENT(cf) != data_s)
  1446. Curl_expire(data_s, 0, EXPIRE_RUN_NOW);
  1447. CURL_TRC_CF(data_s, cf, "[%d] status: HTTP/2 %03d",
  1448. stream->id, stream->status_code);
  1449. return 0;
  1450. }
  1451. /* nghttp2 guarantees that namelen > 0, and :status was already
  1452. received, and this is not pseudo-header field . */
  1453. /* convert to an HTTP1-style header */
  1454. Curl_dyn_reset(&ctx->scratch);
  1455. result = Curl_dyn_addn(&ctx->scratch, (const char *)name, namelen);
  1456. if(!result)
  1457. result = Curl_dyn_addn(&ctx->scratch, STRCONST(": "));
  1458. if(!result)
  1459. result = Curl_dyn_addn(&ctx->scratch, (const char *)value, valuelen);
  1460. if(!result)
  1461. result = Curl_dyn_addn(&ctx->scratch, STRCONST("\r\n"));
  1462. if(!result)
  1463. h2_xfer_write_resp_hd(cf, data_s, stream, Curl_dyn_ptr(&ctx->scratch),
  1464. Curl_dyn_len(&ctx->scratch), FALSE);
  1465. if(result)
  1466. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1467. /* if we receive data for another handle, wake that up */
  1468. if(CF_DATA_CURRENT(cf) != data_s)
  1469. Curl_expire(data_s, 0, EXPIRE_RUN_NOW);
  1470. CURL_TRC_CF(data_s, cf, "[%d] header: %.*s: %.*s",
  1471. stream->id, (int)namelen, name, (int)valuelen, value);
  1472. return 0; /* 0 is successful */
  1473. }
  1474. static ssize_t req_body_read_callback(nghttp2_session *session,
  1475. int32_t stream_id,
  1476. uint8_t *buf, size_t length,
  1477. uint32_t *data_flags,
  1478. nghttp2_data_source *source,
  1479. void *userp)
  1480. {
  1481. struct Curl_cfilter *cf = userp;
  1482. struct cf_h2_ctx *ctx = cf->ctx;
  1483. struct Curl_easy *data_s;
  1484. struct h2_stream_ctx *stream = NULL;
  1485. CURLcode result;
  1486. ssize_t nread;
  1487. (void)source;
  1488. (void)cf;
  1489. if(!stream_id)
  1490. return NGHTTP2_ERR_INVALID_ARGUMENT;
  1491. /* get the stream from the hash based on Stream ID, stream ID zero is for
  1492. connection-oriented stuff */
  1493. data_s = nghttp2_session_get_stream_user_data(session, stream_id);
  1494. if(!data_s)
  1495. /* Receiving a Stream ID not in the hash should not happen, this is an
  1496. internal error more than anything else! */
  1497. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1498. stream = H2_STREAM_CTX(ctx, data_s);
  1499. if(!stream)
  1500. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1501. nread = Curl_bufq_read(&stream->sendbuf, buf, length, &result);
  1502. if(nread < 0) {
  1503. if(result != CURLE_AGAIN)
  1504. return NGHTTP2_ERR_CALLBACK_FAILURE;
  1505. nread = 0;
  1506. }
  1507. CURL_TRC_CF(data_s, cf, "[%d] req_body_read(len=%zu) eos=%d -> %zd, %d",
  1508. stream_id, length, stream->body_eos, nread, result);
  1509. if(stream->body_eos && Curl_bufq_is_empty(&stream->sendbuf)) {
  1510. *data_flags = NGHTTP2_DATA_FLAG_EOF;
  1511. return nread;
  1512. }
  1513. return (nread == 0) ? NGHTTP2_ERR_DEFERRED : nread;
  1514. }
  1515. #if !defined(CURL_DISABLE_VERBOSE_STRINGS)
  1516. static int error_callback(nghttp2_session *session,
  1517. const char *msg,
  1518. size_t len,
  1519. void *userp)
  1520. {
  1521. struct Curl_cfilter *cf = userp;
  1522. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  1523. (void)session;
  1524. failf(data, "%.*s", (int)len, msg);
  1525. return 0;
  1526. }
  1527. #endif
  1528. /*
  1529. * Append headers to ask for an HTTP1.1 to HTTP2 upgrade.
  1530. */
  1531. CURLcode Curl_http2_request_upgrade(struct dynbuf *req,
  1532. struct Curl_easy *data)
  1533. {
  1534. CURLcode result;
  1535. char *base64;
  1536. size_t blen;
  1537. struct SingleRequest *k = &data->req;
  1538. uint8_t binsettings[H2_BINSETTINGS_LEN];
  1539. ssize_t binlen; /* length of the binsettings data */
  1540. binlen = populate_binsettings(binsettings, data);
  1541. if(binlen <= 0) {
  1542. failf(data, "nghttp2 unexpectedly failed on pack_settings_payload");
  1543. Curl_dyn_free(req);
  1544. return CURLE_FAILED_INIT;
  1545. }
  1546. result = Curl_base64url_encode((const char *)binsettings, (size_t)binlen,
  1547. &base64, &blen);
  1548. if(result) {
  1549. Curl_dyn_free(req);
  1550. return result;
  1551. }
  1552. result = Curl_dyn_addf(req,
  1553. "Connection: Upgrade, HTTP2-Settings\r\n"
  1554. "Upgrade: %s\r\n"
  1555. "HTTP2-Settings: %s\r\n",
  1556. NGHTTP2_CLEARTEXT_PROTO_VERSION_ID, base64);
  1557. free(base64);
  1558. k->upgr101 = UPGR101_H2;
  1559. data->conn->bits.asks_multiplex = TRUE;
  1560. return result;
  1561. }
  1562. static ssize_t http2_handle_stream_close(struct Curl_cfilter *cf,
  1563. struct Curl_easy *data,
  1564. struct h2_stream_ctx *stream,
  1565. CURLcode *err)
  1566. {
  1567. ssize_t rv = 0;
  1568. if(stream->error == NGHTTP2_REFUSED_STREAM) {
  1569. CURL_TRC_CF(data, cf, "[%d] REFUSED_STREAM, try again on a new "
  1570. "connection", stream->id);
  1571. connclose(cf->conn, "REFUSED_STREAM"); /* do not use this anymore */
  1572. data->state.refused_stream = TRUE;
  1573. *err = CURLE_RECV_ERROR; /* trigger Curl_retry_request() later */
  1574. return -1;
  1575. }
  1576. else if(stream->error != NGHTTP2_NO_ERROR) {
  1577. if(stream->resp_hds_complete && data->req.no_body) {
  1578. CURL_TRC_CF(data, cf, "[%d] error after response headers, but we did "
  1579. "not want a body anyway, ignore: %s (err %u)",
  1580. stream->id, nghttp2_http2_strerror(stream->error),
  1581. stream->error);
  1582. stream->close_handled = TRUE;
  1583. *err = CURLE_OK;
  1584. goto out;
  1585. }
  1586. failf(data, "HTTP/2 stream %u was not closed cleanly: %s (err %u)",
  1587. stream->id, nghttp2_http2_strerror(stream->error),
  1588. stream->error);
  1589. *err = CURLE_HTTP2_STREAM;
  1590. return -1;
  1591. }
  1592. else if(stream->reset) {
  1593. failf(data, "HTTP/2 stream %u was reset", stream->id);
  1594. *err = data->req.bytecount ? CURLE_PARTIAL_FILE : CURLE_HTTP2;
  1595. return -1;
  1596. }
  1597. if(!stream->bodystarted) {
  1598. failf(data, "HTTP/2 stream %u was closed cleanly, but before getting "
  1599. " all response header fields, treated as error",
  1600. stream->id);
  1601. *err = CURLE_HTTP2_STREAM;
  1602. return -1;
  1603. }
  1604. if(Curl_dynhds_count(&stream->resp_trailers)) {
  1605. struct dynhds_entry *e;
  1606. struct dynbuf dbuf;
  1607. size_t i;
  1608. *err = CURLE_OK;
  1609. Curl_dyn_init(&dbuf, DYN_TRAILERS);
  1610. for(i = 0; i < Curl_dynhds_count(&stream->resp_trailers); ++i) {
  1611. e = Curl_dynhds_getn(&stream->resp_trailers, i);
  1612. if(!e)
  1613. break;
  1614. Curl_dyn_reset(&dbuf);
  1615. *err = Curl_dyn_addf(&dbuf, "%.*s: %.*s\x0d\x0a",
  1616. (int)e->namelen, e->name,
  1617. (int)e->valuelen, e->value);
  1618. if(*err)
  1619. break;
  1620. Curl_debug(data, CURLINFO_HEADER_IN, Curl_dyn_ptr(&dbuf),
  1621. Curl_dyn_len(&dbuf));
  1622. *err = Curl_client_write(data, CLIENTWRITE_HEADER|CLIENTWRITE_TRAILER,
  1623. Curl_dyn_ptr(&dbuf), Curl_dyn_len(&dbuf));
  1624. if(*err)
  1625. break;
  1626. }
  1627. Curl_dyn_free(&dbuf);
  1628. if(*err)
  1629. goto out;
  1630. }
  1631. stream->close_handled = TRUE;
  1632. *err = CURLE_OK;
  1633. rv = 0;
  1634. out:
  1635. CURL_TRC_CF(data, cf, "handle_stream_close -> %zd, %d", rv, *err);
  1636. return rv;
  1637. }
  1638. static int sweight_wanted(const struct Curl_easy *data)
  1639. {
  1640. /* 0 weight is not set by user and we take the nghttp2 default one */
  1641. return data->set.priority.weight ?
  1642. data->set.priority.weight : NGHTTP2_DEFAULT_WEIGHT;
  1643. }
  1644. static int sweight_in_effect(const struct Curl_easy *data)
  1645. {
  1646. /* 0 weight is not set by user and we take the nghttp2 default one */
  1647. return data->state.priority.weight ?
  1648. data->state.priority.weight : NGHTTP2_DEFAULT_WEIGHT;
  1649. }
  1650. /*
  1651. * h2_pri_spec() fills in the pri_spec struct, used by nghttp2 to send weight
  1652. * and dependency to the peer. It also stores the updated values in the state
  1653. * struct.
  1654. */
  1655. static void h2_pri_spec(struct cf_h2_ctx *ctx,
  1656. struct Curl_easy *data,
  1657. nghttp2_priority_spec *pri_spec)
  1658. {
  1659. struct Curl_data_priority *prio = &data->set.priority;
  1660. struct h2_stream_ctx *depstream = H2_STREAM_CTX(ctx, prio->parent);
  1661. int32_t depstream_id = depstream ? depstream->id : 0;
  1662. nghttp2_priority_spec_init(pri_spec, depstream_id,
  1663. sweight_wanted(data),
  1664. data->set.priority.exclusive);
  1665. data->state.priority = *prio;
  1666. }
  1667. /*
  1668. * Check if there is been an update in the priority /
  1669. * dependency settings and if so it submits a PRIORITY frame with the updated
  1670. * info.
  1671. * Flush any out data pending in the network buffer.
  1672. */
  1673. static CURLcode h2_progress_egress(struct Curl_cfilter *cf,
  1674. struct Curl_easy *data)
  1675. {
  1676. struct cf_h2_ctx *ctx = cf->ctx;
  1677. struct h2_stream_ctx *stream = H2_STREAM_CTX(ctx, data);
  1678. int rv = 0;
  1679. if(stream && stream->id > 0 &&
  1680. ((sweight_wanted(data) != sweight_in_effect(data)) ||
  1681. (data->set.priority.exclusive != data->state.priority.exclusive) ||
  1682. (data->set.priority.parent != data->state.priority.parent)) ) {
  1683. /* send new weight and/or dependency */
  1684. nghttp2_priority_spec pri_spec;
  1685. h2_pri_spec(ctx, data, &pri_spec);
  1686. CURL_TRC_CF(data, cf, "[%d] Queuing PRIORITY", stream->id);
  1687. DEBUGASSERT(stream->id != -1);
  1688. rv = nghttp2_submit_priority(ctx->h2, NGHTTP2_FLAG_NONE,
  1689. stream->id, &pri_spec);
  1690. if(rv)
  1691. goto out;
  1692. }
  1693. ctx->nw_out_blocked = 0;
  1694. while(!rv && !ctx->nw_out_blocked && nghttp2_session_want_write(ctx->h2))
  1695. rv = nghttp2_session_send(ctx->h2);
  1696. out:
  1697. if(nghttp2_is_fatal(rv)) {
  1698. CURL_TRC_CF(data, cf, "nghttp2_session_send error (%s)%d",
  1699. nghttp2_strerror(rv), rv);
  1700. return CURLE_SEND_ERROR;
  1701. }
  1702. /* Defer flushing during the connect phase so that the SETTINGS and
  1703. * other initial frames are sent together with the first request.
  1704. * Unless we are 'connect_only' where the request will never come. */
  1705. if(!cf->connected && !cf->conn->connect_only)
  1706. return CURLE_OK;
  1707. return nw_out_flush(cf, data);
  1708. }
  1709. static ssize_t stream_recv(struct Curl_cfilter *cf, struct Curl_easy *data,
  1710. struct h2_stream_ctx *stream,
  1711. char *buf, size_t len, CURLcode *err)
  1712. {
  1713. struct cf_h2_ctx *ctx = cf->ctx;
  1714. ssize_t nread = -1;
  1715. (void)buf;
  1716. *err = CURLE_AGAIN;
  1717. if(stream->xfer_result) {
  1718. CURL_TRC_CF(data, cf, "[%d] xfer write failed", stream->id);
  1719. *err = stream->xfer_result;
  1720. nread = -1;
  1721. }
  1722. else if(stream->closed) {
  1723. CURL_TRC_CF(data, cf, "[%d] returning CLOSE", stream->id);
  1724. nread = http2_handle_stream_close(cf, data, stream, err);
  1725. }
  1726. else if(stream->reset ||
  1727. (ctx->conn_closed && Curl_bufq_is_empty(&ctx->inbufq)) ||
  1728. (ctx->rcvd_goaway && ctx->remote_max_sid < stream->id)) {
  1729. CURL_TRC_CF(data, cf, "[%d] returning ERR", stream->id);
  1730. *err = data->req.bytecount ? CURLE_PARTIAL_FILE : CURLE_HTTP2;
  1731. nread = -1;
  1732. }
  1733. if(nread < 0 && *err != CURLE_AGAIN)
  1734. CURL_TRC_CF(data, cf, "[%d] stream_recv(len=%zu) -> %zd, %d",
  1735. stream->id, len, nread, *err);
  1736. return nread;
  1737. }
  1738. static CURLcode h2_progress_ingress(struct Curl_cfilter *cf,
  1739. struct Curl_easy *data,
  1740. size_t data_max_bytes)
  1741. {
  1742. struct cf_h2_ctx *ctx = cf->ctx;
  1743. struct h2_stream_ctx *stream;
  1744. CURLcode result = CURLE_OK;
  1745. ssize_t nread;
  1746. /* Process network input buffer fist */
  1747. if(!Curl_bufq_is_empty(&ctx->inbufq)) {
  1748. CURL_TRC_CF(data, cf, "Process %zu bytes in connection buffer",
  1749. Curl_bufq_len(&ctx->inbufq));
  1750. if(h2_process_pending_input(cf, data, &result) < 0)
  1751. return result;
  1752. }
  1753. /* Receive data from the "lower" filters, e.g. network until
  1754. * it is time to stop due to connection close or us not processing
  1755. * all network input */
  1756. while(!ctx->conn_closed && Curl_bufq_is_empty(&ctx->inbufq)) {
  1757. stream = H2_STREAM_CTX(ctx, data);
  1758. if(stream && (stream->closed || !data_max_bytes)) {
  1759. /* We would like to abort here and stop processing, so that
  1760. * the transfer loop can handle the data/close here. However,
  1761. * this may leave data in underlying buffers that will not
  1762. * be consumed. */
  1763. if(!cf->next || !cf->next->cft->has_data_pending(cf->next, data))
  1764. drain_stream(cf, data, stream);
  1765. break;
  1766. }
  1767. nread = Curl_bufq_sipn(&ctx->inbufq, 0, nw_in_reader, cf, &result);
  1768. if(nread < 0) {
  1769. if(result != CURLE_AGAIN) {
  1770. failf(data, "Failed receiving HTTP2 data: %d(%s)", result,
  1771. curl_easy_strerror(result));
  1772. return result;
  1773. }
  1774. break;
  1775. }
  1776. else if(nread == 0) {
  1777. CURL_TRC_CF(data, cf, "[0] ingress: connection closed");
  1778. ctx->conn_closed = TRUE;
  1779. break;
  1780. }
  1781. else {
  1782. CURL_TRC_CF(data, cf, "[0] ingress: read %zd bytes", nread);
  1783. data_max_bytes = (data_max_bytes > (size_t)nread) ?
  1784. (data_max_bytes - (size_t)nread) : 0;
  1785. }
  1786. if(h2_process_pending_input(cf, data, &result))
  1787. return result;
  1788. CURL_TRC_CF(data, cf, "[0] progress ingress: inbufg=%zu",
  1789. Curl_bufq_len(&ctx->inbufq));
  1790. }
  1791. if(ctx->conn_closed && Curl_bufq_is_empty(&ctx->inbufq)) {
  1792. connclose(cf->conn, "GOAWAY received");
  1793. }
  1794. CURL_TRC_CF(data, cf, "[0] progress ingress: done");
  1795. return CURLE_OK;
  1796. }
  1797. static ssize_t cf_h2_recv(struct Curl_cfilter *cf, struct Curl_easy *data,
  1798. char *buf, size_t len, CURLcode *err)
  1799. {
  1800. struct cf_h2_ctx *ctx = cf->ctx;
  1801. struct h2_stream_ctx *stream = H2_STREAM_CTX(ctx, data);
  1802. ssize_t nread = -1;
  1803. CURLcode result;
  1804. struct cf_call_data save;
  1805. if(!stream) {
  1806. /* Abnormal call sequence: either this transfer has never opened a stream
  1807. * (unlikely) or the transfer has been done, cleaned up its resources, but
  1808. * a read() is called anyway. It is not clear what the calling sequence
  1809. * is for such a case. */
  1810. failf(data, "http/2 recv on a transfer never opened "
  1811. "or already cleared, mid=%" FMT_OFF_T, data->mid);
  1812. *err = CURLE_HTTP2;
  1813. return -1;
  1814. }
  1815. CF_DATA_SAVE(save, cf, data);
  1816. nread = stream_recv(cf, data, stream, buf, len, err);
  1817. if(nread < 0 && *err != CURLE_AGAIN)
  1818. goto out;
  1819. if(nread < 0) {
  1820. *err = h2_progress_ingress(cf, data, len);
  1821. if(*err)
  1822. goto out;
  1823. nread = stream_recv(cf, data, stream, buf, len, err);
  1824. }
  1825. if(nread > 0) {
  1826. size_t data_consumed = (size_t)nread;
  1827. /* Now that we transferred this to the upper layer, we report
  1828. * the actual amount of DATA consumed to the H2 session, so
  1829. * that it adjusts stream flow control */
  1830. if(stream->resp_hds_len >= data_consumed) {
  1831. stream->resp_hds_len -= data_consumed; /* no DATA */
  1832. }
  1833. else {
  1834. if(stream->resp_hds_len) {
  1835. data_consumed -= stream->resp_hds_len;
  1836. stream->resp_hds_len = 0;
  1837. }
  1838. if(data_consumed) {
  1839. nghttp2_session_consume(ctx->h2, stream->id, data_consumed);
  1840. }
  1841. }
  1842. if(stream->closed) {
  1843. CURL_TRC_CF(data, cf, "[%d] DRAIN closed stream", stream->id);
  1844. drain_stream(cf, data, stream);
  1845. }
  1846. }
  1847. out:
  1848. result = h2_progress_egress(cf, data);
  1849. if(result == CURLE_AGAIN) {
  1850. /* pending data to send, need to be called again. Ideally, we
  1851. * monitor the socket for POLLOUT, but when not SENDING
  1852. * any more, we force processing of the transfer. */
  1853. if(!CURL_WANT_SEND(data))
  1854. drain_stream(cf, data, stream);
  1855. }
  1856. else if(result) {
  1857. *err = result;
  1858. nread = -1;
  1859. }
  1860. CURL_TRC_CF(data, cf, "[%d] cf_recv(len=%zu) -> %zd %d, "
  1861. "window=%d/%d, connection %d/%d",
  1862. stream->id, len, nread, *err,
  1863. nghttp2_session_get_stream_effective_recv_data_length(
  1864. ctx->h2, stream->id),
  1865. nghttp2_session_get_stream_effective_local_window_size(
  1866. ctx->h2, stream->id),
  1867. nghttp2_session_get_local_window_size(ctx->h2),
  1868. HTTP2_HUGE_WINDOW_SIZE);
  1869. CF_DATA_RESTORE(cf, save);
  1870. return nread;
  1871. }
  1872. static ssize_t cf_h2_body_send(struct Curl_cfilter *cf,
  1873. struct Curl_easy *data,
  1874. struct h2_stream_ctx *stream,
  1875. const void *buf, size_t blen, bool eos,
  1876. CURLcode *err)
  1877. {
  1878. struct cf_h2_ctx *ctx = cf->ctx;
  1879. ssize_t nwritten;
  1880. if(stream->closed) {
  1881. if(stream->resp_hds_complete) {
  1882. /* Server decided to close the stream after having sent us a final
  1883. * response. This is valid if it is not interested in the request
  1884. * body. This happens on 30x or 40x responses.
  1885. * We silently discard the data sent, since this is not a transport
  1886. * error situation. */
  1887. CURL_TRC_CF(data, cf, "[%d] discarding data"
  1888. "on closed stream with response", stream->id);
  1889. if(eos)
  1890. stream->body_eos = TRUE;
  1891. *err = CURLE_OK;
  1892. return (ssize_t)blen;
  1893. }
  1894. /* Server closed before we got a response, this is an error */
  1895. infof(data, "stream %u closed", stream->id);
  1896. *err = CURLE_SEND_ERROR;
  1897. return -1;
  1898. }
  1899. nwritten = Curl_bufq_write(&stream->sendbuf, buf, blen, err);
  1900. if(nwritten < 0)
  1901. return -1;
  1902. if(eos && (blen == (size_t)nwritten))
  1903. stream->body_eos = TRUE;
  1904. if(eos || !Curl_bufq_is_empty(&stream->sendbuf)) {
  1905. /* resume the potentially suspended stream */
  1906. int rv = nghttp2_session_resume_data(ctx->h2, stream->id);
  1907. if(nghttp2_is_fatal(rv)) {
  1908. *err = CURLE_SEND_ERROR;
  1909. return -1;
  1910. }
  1911. }
  1912. return nwritten;
  1913. }
  1914. static ssize_t h2_submit(struct h2_stream_ctx **pstream,
  1915. struct Curl_cfilter *cf, struct Curl_easy *data,
  1916. const void *buf, size_t len,
  1917. bool eos, CURLcode *err)
  1918. {
  1919. struct cf_h2_ctx *ctx = cf->ctx;
  1920. struct h2_stream_ctx *stream = NULL;
  1921. struct dynhds h2_headers;
  1922. nghttp2_nv *nva = NULL;
  1923. const void *body = NULL;
  1924. size_t nheader, bodylen, i;
  1925. nghttp2_data_provider data_prd;
  1926. int32_t stream_id;
  1927. nghttp2_priority_spec pri_spec;
  1928. ssize_t nwritten;
  1929. Curl_dynhds_init(&h2_headers, 0, DYN_HTTP_REQUEST);
  1930. *err = http2_data_setup(cf, data, &stream);
  1931. if(*err) {
  1932. nwritten = -1;
  1933. goto out;
  1934. }
  1935. nwritten = Curl_h1_req_parse_read(&stream->h1, buf, len, NULL, 0, err);
  1936. if(nwritten < 0)
  1937. goto out;
  1938. if(!stream->h1.done) {
  1939. /* need more data */
  1940. goto out;
  1941. }
  1942. DEBUGASSERT(stream->h1.req);
  1943. *err = Curl_http_req_to_h2(&h2_headers, stream->h1.req, data);
  1944. if(*err) {
  1945. nwritten = -1;
  1946. goto out;
  1947. }
  1948. /* no longer needed */
  1949. Curl_h1_req_parse_free(&stream->h1);
  1950. nva = Curl_dynhds_to_nva(&h2_headers, &nheader);
  1951. if(!nva) {
  1952. *err = CURLE_OUT_OF_MEMORY;
  1953. nwritten = -1;
  1954. goto out;
  1955. }
  1956. h2_pri_spec(ctx, data, &pri_spec);
  1957. if(!nghttp2_session_check_request_allowed(ctx->h2))
  1958. CURL_TRC_CF(data, cf, "send request NOT allowed (via nghttp2)");
  1959. switch(data->state.httpreq) {
  1960. case HTTPREQ_POST:
  1961. case HTTPREQ_POST_FORM:
  1962. case HTTPREQ_POST_MIME:
  1963. case HTTPREQ_PUT:
  1964. data_prd.read_callback = req_body_read_callback;
  1965. data_prd.source.ptr = NULL;
  1966. stream_id = nghttp2_submit_request(ctx->h2, &pri_spec, nva, nheader,
  1967. &data_prd, data);
  1968. break;
  1969. default:
  1970. stream_id = nghttp2_submit_request(ctx->h2, &pri_spec, nva, nheader,
  1971. NULL, data);
  1972. }
  1973. if(stream_id < 0) {
  1974. CURL_TRC_CF(data, cf, "send: nghttp2_submit_request error (%s)%u",
  1975. nghttp2_strerror(stream_id), stream_id);
  1976. *err = CURLE_SEND_ERROR;
  1977. nwritten = -1;
  1978. goto out;
  1979. }
  1980. #define MAX_ACC 60000 /* <64KB to account for some overhead */
  1981. if(Curl_trc_is_verbose(data)) {
  1982. size_t acc = 0;
  1983. infof(data, "[HTTP/2] [%d] OPENED stream for %s",
  1984. stream_id, data->state.url);
  1985. for(i = 0; i < nheader; ++i) {
  1986. acc += nva[i].namelen + nva[i].valuelen;
  1987. infof(data, "[HTTP/2] [%d] [%.*s: %.*s]", stream_id,
  1988. (int)nva[i].namelen, nva[i].name,
  1989. (int)nva[i].valuelen, nva[i].value);
  1990. }
  1991. if(acc > MAX_ACC) {
  1992. infof(data, "[HTTP/2] Warning: The cumulative length of all "
  1993. "headers exceeds %d bytes and that could cause the "
  1994. "stream to be rejected.", MAX_ACC);
  1995. }
  1996. }
  1997. stream->id = stream_id;
  1998. body = (const char *)buf + nwritten;
  1999. bodylen = len - nwritten;
  2000. if(bodylen || eos) {
  2001. ssize_t n = cf_h2_body_send(cf, data, stream, body, bodylen, eos, err);
  2002. if(n >= 0)
  2003. nwritten += n;
  2004. else if(*err == CURLE_AGAIN)
  2005. *err = CURLE_OK;
  2006. else if(*err != CURLE_AGAIN) {
  2007. *err = CURLE_SEND_ERROR;
  2008. nwritten = -1;
  2009. goto out;
  2010. }
  2011. }
  2012. out:
  2013. CURL_TRC_CF(data, cf, "[%d] submit -> %zd, %d",
  2014. stream ? stream->id : -1, nwritten, *err);
  2015. Curl_safefree(nva);
  2016. *pstream = stream;
  2017. Curl_dynhds_free(&h2_headers);
  2018. return nwritten;
  2019. }
  2020. static ssize_t cf_h2_send(struct Curl_cfilter *cf, struct Curl_easy *data,
  2021. const void *buf, size_t len, bool eos,
  2022. CURLcode *err)
  2023. {
  2024. struct cf_h2_ctx *ctx = cf->ctx;
  2025. struct h2_stream_ctx *stream = H2_STREAM_CTX(ctx, data);
  2026. struct cf_call_data save;
  2027. ssize_t nwritten;
  2028. CURLcode result;
  2029. CF_DATA_SAVE(save, cf, data);
  2030. if(!stream || stream->id == -1) {
  2031. nwritten = h2_submit(&stream, cf, data, buf, len, eos, err);
  2032. if(nwritten < 0) {
  2033. goto out;
  2034. }
  2035. DEBUGASSERT(stream);
  2036. }
  2037. else if(stream->body_eos) {
  2038. /* We already wrote this, but CURLE_AGAINed the call due to not
  2039. * being able to flush stream->sendbuf. Make a 0-length write
  2040. * to trigger flushing again.
  2041. * If this works, we report to have written `len` bytes. */
  2042. DEBUGASSERT(eos);
  2043. nwritten = cf_h2_body_send(cf, data, stream, buf, 0, eos, err);
  2044. CURL_TRC_CF(data, cf, "[%d] cf_body_send last CHUNK -> %zd, %d, eos=%d",
  2045. stream->id, nwritten, *err, eos);
  2046. if(nwritten < 0) {
  2047. goto out;
  2048. }
  2049. nwritten = len;
  2050. }
  2051. else {
  2052. nwritten = cf_h2_body_send(cf, data, stream, buf, len, eos, err);
  2053. CURL_TRC_CF(data, cf, "[%d] cf_body_send(len=%zu) -> %zd, %d, eos=%d",
  2054. stream->id, len, nwritten, *err, eos);
  2055. }
  2056. /* Call the nghttp2 send loop and flush to write ALL buffered data,
  2057. * headers and/or request body completely out to the network */
  2058. result = h2_progress_egress(cf, data);
  2059. /* if the stream has been closed in egress handling (nghttp2 does that
  2060. * when it does not like the headers, for example */
  2061. if(stream && stream->closed) {
  2062. infof(data, "stream %u closed", stream->id);
  2063. *err = CURLE_SEND_ERROR;
  2064. nwritten = -1;
  2065. goto out;
  2066. }
  2067. else if(result && (result != CURLE_AGAIN)) {
  2068. *err = result;
  2069. nwritten = -1;
  2070. goto out;
  2071. }
  2072. if(should_close_session(ctx)) {
  2073. /* nghttp2 thinks this session is done. If the stream has not been
  2074. * closed, this is an error state for out transfer */
  2075. if(stream->closed) {
  2076. nwritten = http2_handle_stream_close(cf, data, stream, err);
  2077. }
  2078. else {
  2079. CURL_TRC_CF(data, cf, "send: nothing to do in this session");
  2080. *err = CURLE_HTTP2;
  2081. nwritten = -1;
  2082. }
  2083. }
  2084. out:
  2085. if(stream) {
  2086. CURL_TRC_CF(data, cf, "[%d] cf_send(len=%zu) -> %zd, %d, "
  2087. "eos=%d, h2 windows %d-%d (stream-conn), "
  2088. "buffers %zu-%zu (stream-conn)",
  2089. stream->id, len, nwritten, *err,
  2090. stream->body_eos,
  2091. nghttp2_session_get_stream_remote_window_size(
  2092. ctx->h2, stream->id),
  2093. nghttp2_session_get_remote_window_size(ctx->h2),
  2094. Curl_bufq_len(&stream->sendbuf),
  2095. Curl_bufq_len(&ctx->outbufq));
  2096. }
  2097. else {
  2098. CURL_TRC_CF(data, cf, "cf_send(len=%zu) -> %zd, %d, "
  2099. "connection-window=%d, nw_send_buffer(%zu)",
  2100. len, nwritten, *err,
  2101. nghttp2_session_get_remote_window_size(ctx->h2),
  2102. Curl_bufq_len(&ctx->outbufq));
  2103. }
  2104. CF_DATA_RESTORE(cf, save);
  2105. return nwritten;
  2106. }
  2107. static CURLcode cf_h2_flush(struct Curl_cfilter *cf,
  2108. struct Curl_easy *data)
  2109. {
  2110. struct cf_h2_ctx *ctx = cf->ctx;
  2111. struct h2_stream_ctx *stream = H2_STREAM_CTX(ctx, data);
  2112. struct cf_call_data save;
  2113. CURLcode result = CURLE_OK;
  2114. CF_DATA_SAVE(save, cf, data);
  2115. if(stream && !Curl_bufq_is_empty(&stream->sendbuf)) {
  2116. /* resume the potentially suspended stream */
  2117. int rv = nghttp2_session_resume_data(ctx->h2, stream->id);
  2118. if(nghttp2_is_fatal(rv)) {
  2119. result = CURLE_SEND_ERROR;
  2120. goto out;
  2121. }
  2122. }
  2123. result = h2_progress_egress(cf, data);
  2124. out:
  2125. if(stream) {
  2126. CURL_TRC_CF(data, cf, "[%d] flush -> %d, "
  2127. "h2 windows %d-%d (stream-conn), "
  2128. "buffers %zu-%zu (stream-conn)",
  2129. stream->id, result,
  2130. nghttp2_session_get_stream_remote_window_size(
  2131. ctx->h2, stream->id),
  2132. nghttp2_session_get_remote_window_size(ctx->h2),
  2133. Curl_bufq_len(&stream->sendbuf),
  2134. Curl_bufq_len(&ctx->outbufq));
  2135. }
  2136. else {
  2137. CURL_TRC_CF(data, cf, "flush -> %d, "
  2138. "connection-window=%d, nw_send_buffer(%zu)",
  2139. result, nghttp2_session_get_remote_window_size(ctx->h2),
  2140. Curl_bufq_len(&ctx->outbufq));
  2141. }
  2142. CF_DATA_RESTORE(cf, save);
  2143. return result;
  2144. }
  2145. static void cf_h2_adjust_pollset(struct Curl_cfilter *cf,
  2146. struct Curl_easy *data,
  2147. struct easy_pollset *ps)
  2148. {
  2149. struct cf_h2_ctx *ctx = cf->ctx;
  2150. struct cf_call_data save;
  2151. curl_socket_t sock;
  2152. bool want_recv, want_send;
  2153. if(!ctx->h2)
  2154. return;
  2155. sock = Curl_conn_cf_get_socket(cf, data);
  2156. Curl_pollset_check(data, ps, sock, &want_recv, &want_send);
  2157. if(want_recv || want_send) {
  2158. struct h2_stream_ctx *stream = H2_STREAM_CTX(ctx, data);
  2159. bool c_exhaust, s_exhaust;
  2160. CF_DATA_SAVE(save, cf, data);
  2161. c_exhaust = want_send && !nghttp2_session_get_remote_window_size(ctx->h2);
  2162. s_exhaust = want_send && stream && stream->id >= 0 &&
  2163. !nghttp2_session_get_stream_remote_window_size(ctx->h2,
  2164. stream->id);
  2165. want_recv = (want_recv || c_exhaust || s_exhaust);
  2166. want_send = (!s_exhaust && want_send) ||
  2167. (!c_exhaust && nghttp2_session_want_write(ctx->h2)) ||
  2168. !Curl_bufq_is_empty(&ctx->outbufq);
  2169. Curl_pollset_set(data, ps, sock, want_recv, want_send);
  2170. CF_DATA_RESTORE(cf, save);
  2171. }
  2172. else if(ctx->sent_goaway && !cf->shutdown) {
  2173. /* shutdown in progress */
  2174. CF_DATA_SAVE(save, cf, data);
  2175. want_send = nghttp2_session_want_write(ctx->h2) ||
  2176. !Curl_bufq_is_empty(&ctx->outbufq);
  2177. want_recv = nghttp2_session_want_read(ctx->h2);
  2178. Curl_pollset_set(data, ps, sock, want_recv, want_send);
  2179. CF_DATA_RESTORE(cf, save);
  2180. }
  2181. }
  2182. static CURLcode cf_h2_connect(struct Curl_cfilter *cf,
  2183. struct Curl_easy *data,
  2184. bool blocking, bool *done)
  2185. {
  2186. struct cf_h2_ctx *ctx = cf->ctx;
  2187. CURLcode result = CURLE_OK;
  2188. struct cf_call_data save;
  2189. bool first_time = FALSE;
  2190. if(cf->connected) {
  2191. *done = TRUE;
  2192. return CURLE_OK;
  2193. }
  2194. /* Connect the lower filters first */
  2195. if(!cf->next->connected) {
  2196. result = Curl_conn_cf_connect(cf->next, data, blocking, done);
  2197. if(result || !*done)
  2198. return result;
  2199. }
  2200. *done = FALSE;
  2201. CF_DATA_SAVE(save, cf, data);
  2202. DEBUGASSERT(ctx->initialized);
  2203. if(!ctx->h2) {
  2204. result = cf_h2_ctx_open(cf, data);
  2205. if(result)
  2206. goto out;
  2207. first_time = TRUE;
  2208. }
  2209. if(!first_time) {
  2210. result = h2_progress_ingress(cf, data, H2_CHUNK_SIZE);
  2211. if(result)
  2212. goto out;
  2213. }
  2214. /* Send out our SETTINGS and ACKs and such. If that blocks, we
  2215. * have it buffered and can count this filter as being connected */
  2216. result = h2_progress_egress(cf, data);
  2217. if(result == CURLE_AGAIN)
  2218. result = CURLE_OK;
  2219. else if(result)
  2220. goto out;
  2221. *done = TRUE;
  2222. cf->connected = TRUE;
  2223. result = CURLE_OK;
  2224. out:
  2225. CURL_TRC_CF(data, cf, "cf_connect() -> %d, %d, ", result, *done);
  2226. CF_DATA_RESTORE(cf, save);
  2227. return result;
  2228. }
  2229. static void cf_h2_close(struct Curl_cfilter *cf, struct Curl_easy *data)
  2230. {
  2231. struct cf_h2_ctx *ctx = cf->ctx;
  2232. if(ctx) {
  2233. struct cf_call_data save;
  2234. CF_DATA_SAVE(save, cf, data);
  2235. cf_h2_ctx_close(ctx);
  2236. CF_DATA_RESTORE(cf, save);
  2237. cf->connected = FALSE;
  2238. }
  2239. if(cf->next)
  2240. cf->next->cft->do_close(cf->next, data);
  2241. }
  2242. static void cf_h2_destroy(struct Curl_cfilter *cf, struct Curl_easy *data)
  2243. {
  2244. struct cf_h2_ctx *ctx = cf->ctx;
  2245. (void)data;
  2246. if(ctx) {
  2247. cf_h2_ctx_free(ctx);
  2248. cf->ctx = NULL;
  2249. }
  2250. }
  2251. static CURLcode cf_h2_shutdown(struct Curl_cfilter *cf,
  2252. struct Curl_easy *data, bool *done)
  2253. {
  2254. struct cf_h2_ctx *ctx = cf->ctx;
  2255. struct cf_call_data save;
  2256. CURLcode result;
  2257. int rv;
  2258. if(!cf->connected || !ctx->h2 || cf->shutdown || ctx->conn_closed) {
  2259. *done = TRUE;
  2260. return CURLE_OK;
  2261. }
  2262. CF_DATA_SAVE(save, cf, data);
  2263. if(!ctx->sent_goaway) {
  2264. rv = nghttp2_submit_goaway(ctx->h2, NGHTTP2_FLAG_NONE,
  2265. ctx->local_max_sid, 0,
  2266. (const uint8_t *)"shutdown",
  2267. sizeof("shutdown"));
  2268. if(rv) {
  2269. failf(data, "nghttp2_submit_goaway() failed: %s(%d)",
  2270. nghttp2_strerror(rv), rv);
  2271. result = CURLE_SEND_ERROR;
  2272. goto out;
  2273. }
  2274. ctx->sent_goaway = TRUE;
  2275. }
  2276. /* GOAWAY submitted, process egress and ingress until nghttp2 is done. */
  2277. result = CURLE_OK;
  2278. if(nghttp2_session_want_write(ctx->h2) ||
  2279. !Curl_bufq_is_empty(&ctx->outbufq))
  2280. result = h2_progress_egress(cf, data);
  2281. if(!result && nghttp2_session_want_read(ctx->h2))
  2282. result = h2_progress_ingress(cf, data, 0);
  2283. if(result == CURLE_AGAIN)
  2284. result = CURLE_OK;
  2285. *done = (ctx->conn_closed ||
  2286. (!result && !nghttp2_session_want_write(ctx->h2) &&
  2287. !nghttp2_session_want_read(ctx->h2) &&
  2288. Curl_bufq_is_empty(&ctx->outbufq)));
  2289. out:
  2290. CF_DATA_RESTORE(cf, save);
  2291. cf->shutdown = (result || *done);
  2292. return result;
  2293. }
  2294. static CURLcode http2_data_pause(struct Curl_cfilter *cf,
  2295. struct Curl_easy *data,
  2296. bool pause)
  2297. {
  2298. struct cf_h2_ctx *ctx = cf->ctx;
  2299. struct h2_stream_ctx *stream = H2_STREAM_CTX(ctx, data);
  2300. DEBUGASSERT(data);
  2301. if(ctx && ctx->h2 && stream) {
  2302. CURLcode result = cf_h2_update_local_win(cf, data, stream, pause);
  2303. if(result)
  2304. return result;
  2305. /* attempt to send the window update */
  2306. (void)h2_progress_egress(cf, data);
  2307. if(!pause) {
  2308. /* Unpausing a h2 transfer, requires it to be run again. The server
  2309. * may send new DATA on us increasing the flow window, and it may
  2310. * not. We may have already buffered and exhausted the new window
  2311. * by operating on things in flight during the handling of other
  2312. * transfers. */
  2313. drain_stream(cf, data, stream);
  2314. Curl_expire(data, 0, EXPIRE_RUN_NOW);
  2315. }
  2316. CURL_TRC_CF(data, cf, "[%d] stream now %spaused", stream->id,
  2317. pause ? "" : "un");
  2318. }
  2319. return CURLE_OK;
  2320. }
  2321. static CURLcode cf_h2_cntrl(struct Curl_cfilter *cf,
  2322. struct Curl_easy *data,
  2323. int event, int arg1, void *arg2)
  2324. {
  2325. CURLcode result = CURLE_OK;
  2326. struct cf_call_data save;
  2327. (void)arg2;
  2328. CF_DATA_SAVE(save, cf, data);
  2329. switch(event) {
  2330. case CF_CTRL_DATA_SETUP:
  2331. break;
  2332. case CF_CTRL_DATA_PAUSE:
  2333. result = http2_data_pause(cf, data, (arg1 != 0));
  2334. break;
  2335. case CF_CTRL_FLUSH:
  2336. result = cf_h2_flush(cf, data);
  2337. break;
  2338. case CF_CTRL_DATA_DETACH:
  2339. http2_data_done(cf, data);
  2340. break;
  2341. case CF_CTRL_DATA_DONE:
  2342. http2_data_done(cf, data);
  2343. break;
  2344. default:
  2345. break;
  2346. }
  2347. CF_DATA_RESTORE(cf, save);
  2348. return result;
  2349. }
  2350. static bool cf_h2_data_pending(struct Curl_cfilter *cf,
  2351. const struct Curl_easy *data)
  2352. {
  2353. struct cf_h2_ctx *ctx = cf->ctx;
  2354. struct h2_stream_ctx *stream = H2_STREAM_CTX(ctx, data);
  2355. if(ctx && (!Curl_bufq_is_empty(&ctx->inbufq)
  2356. || (stream && !Curl_bufq_is_empty(&stream->sendbuf))))
  2357. return TRUE;
  2358. return cf->next ? cf->next->cft->has_data_pending(cf->next, data) : FALSE;
  2359. }
  2360. static bool cf_h2_is_alive(struct Curl_cfilter *cf,
  2361. struct Curl_easy *data,
  2362. bool *input_pending)
  2363. {
  2364. struct cf_h2_ctx *ctx = cf->ctx;
  2365. CURLcode result;
  2366. struct cf_call_data save;
  2367. CF_DATA_SAVE(save, cf, data);
  2368. result = (ctx && ctx->h2 && http2_connisalive(cf, data, input_pending));
  2369. CURL_TRC_CF(data, cf, "conn alive -> %d, input_pending=%d",
  2370. result, *input_pending);
  2371. CF_DATA_RESTORE(cf, save);
  2372. return result;
  2373. }
  2374. static CURLcode cf_h2_keep_alive(struct Curl_cfilter *cf,
  2375. struct Curl_easy *data)
  2376. {
  2377. CURLcode result;
  2378. struct cf_call_data save;
  2379. CF_DATA_SAVE(save, cf, data);
  2380. result = http2_send_ping(cf, data);
  2381. CF_DATA_RESTORE(cf, save);
  2382. return result;
  2383. }
  2384. static CURLcode cf_h2_query(struct Curl_cfilter *cf,
  2385. struct Curl_easy *data,
  2386. int query, int *pres1, void *pres2)
  2387. {
  2388. struct cf_h2_ctx *ctx = cf->ctx;
  2389. struct cf_call_data save;
  2390. size_t effective_max;
  2391. switch(query) {
  2392. case CF_QUERY_MAX_CONCURRENT:
  2393. DEBUGASSERT(pres1);
  2394. CF_DATA_SAVE(save, cf, data);
  2395. if(nghttp2_session_check_request_allowed(ctx->h2) == 0) {
  2396. /* the limit is what we have in use right now */
  2397. effective_max = CONN_INUSE(cf->conn);
  2398. }
  2399. else {
  2400. effective_max = ctx->max_concurrent_streams;
  2401. }
  2402. *pres1 = (effective_max > INT_MAX) ? INT_MAX : (int)effective_max;
  2403. CF_DATA_RESTORE(cf, save);
  2404. return CURLE_OK;
  2405. case CF_QUERY_STREAM_ERROR: {
  2406. struct h2_stream_ctx *stream = H2_STREAM_CTX(ctx, data);
  2407. *pres1 = stream ? (int)stream->error : 0;
  2408. return CURLE_OK;
  2409. }
  2410. case CF_QUERY_NEED_FLUSH: {
  2411. struct h2_stream_ctx *stream = H2_STREAM_CTX(ctx, data);
  2412. if(!Curl_bufq_is_empty(&ctx->outbufq) ||
  2413. (stream && !Curl_bufq_is_empty(&stream->sendbuf))) {
  2414. *pres1 = TRUE;
  2415. return CURLE_OK;
  2416. }
  2417. break;
  2418. }
  2419. default:
  2420. break;
  2421. }
  2422. return cf->next ?
  2423. cf->next->cft->query(cf->next, data, query, pres1, pres2) :
  2424. CURLE_UNKNOWN_OPTION;
  2425. }
  2426. struct Curl_cftype Curl_cft_nghttp2 = {
  2427. "HTTP/2",
  2428. CF_TYPE_MULTIPLEX,
  2429. CURL_LOG_LVL_NONE,
  2430. cf_h2_destroy,
  2431. cf_h2_connect,
  2432. cf_h2_close,
  2433. cf_h2_shutdown,
  2434. Curl_cf_def_get_host,
  2435. cf_h2_adjust_pollset,
  2436. cf_h2_data_pending,
  2437. cf_h2_send,
  2438. cf_h2_recv,
  2439. cf_h2_cntrl,
  2440. cf_h2_is_alive,
  2441. cf_h2_keep_alive,
  2442. cf_h2_query,
  2443. };
  2444. static CURLcode http2_cfilter_add(struct Curl_cfilter **pcf,
  2445. struct Curl_easy *data,
  2446. struct connectdata *conn,
  2447. int sockindex,
  2448. bool via_h1_upgrade)
  2449. {
  2450. struct Curl_cfilter *cf = NULL;
  2451. struct cf_h2_ctx *ctx;
  2452. CURLcode result = CURLE_OUT_OF_MEMORY;
  2453. DEBUGASSERT(data->conn);
  2454. ctx = calloc(1, sizeof(*ctx));
  2455. if(!ctx)
  2456. goto out;
  2457. cf_h2_ctx_init(ctx, via_h1_upgrade);
  2458. result = Curl_cf_create(&cf, &Curl_cft_nghttp2, ctx);
  2459. if(result)
  2460. goto out;
  2461. ctx = NULL;
  2462. Curl_conn_cf_add(data, conn, sockindex, cf);
  2463. out:
  2464. if(result)
  2465. cf_h2_ctx_free(ctx);
  2466. *pcf = result ? NULL : cf;
  2467. return result;
  2468. }
  2469. static CURLcode http2_cfilter_insert_after(struct Curl_cfilter *cf,
  2470. struct Curl_easy *data,
  2471. bool via_h1_upgrade)
  2472. {
  2473. struct Curl_cfilter *cf_h2 = NULL;
  2474. struct cf_h2_ctx *ctx;
  2475. CURLcode result = CURLE_OUT_OF_MEMORY;
  2476. (void)data;
  2477. ctx = calloc(1, sizeof(*ctx));
  2478. if(!ctx)
  2479. goto out;
  2480. cf_h2_ctx_init(ctx, via_h1_upgrade);
  2481. result = Curl_cf_create(&cf_h2, &Curl_cft_nghttp2, ctx);
  2482. if(result)
  2483. goto out;
  2484. ctx = NULL;
  2485. Curl_conn_cf_insert_after(cf, cf_h2);
  2486. out:
  2487. if(result)
  2488. cf_h2_ctx_free(ctx);
  2489. return result;
  2490. }
  2491. static bool Curl_cf_is_http2(struct Curl_cfilter *cf,
  2492. const struct Curl_easy *data)
  2493. {
  2494. (void)data;
  2495. for(; cf; cf = cf->next) {
  2496. if(cf->cft == &Curl_cft_nghttp2)
  2497. return TRUE;
  2498. if(cf->cft->flags & CF_TYPE_IP_CONNECT)
  2499. return FALSE;
  2500. }
  2501. return FALSE;
  2502. }
  2503. bool Curl_conn_is_http2(const struct Curl_easy *data,
  2504. const struct connectdata *conn,
  2505. int sockindex)
  2506. {
  2507. return conn ? Curl_cf_is_http2(conn->cfilter[sockindex], data) : FALSE;
  2508. }
  2509. bool Curl_http2_may_switch(struct Curl_easy *data,
  2510. struct connectdata *conn,
  2511. int sockindex)
  2512. {
  2513. (void)sockindex;
  2514. if(!Curl_conn_is_http2(data, conn, sockindex) &&
  2515. data->state.httpwant == CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE) {
  2516. #ifndef CURL_DISABLE_PROXY
  2517. if(conn->bits.httpproxy && !conn->bits.tunnel_proxy) {
  2518. /* We do not support HTTP/2 proxies yet. Also it is debatable
  2519. whether or not this setting should apply to HTTP/2 proxies. */
  2520. infof(data, "Ignoring HTTP/2 prior knowledge due to proxy");
  2521. return FALSE;
  2522. }
  2523. #endif
  2524. return TRUE;
  2525. }
  2526. return FALSE;
  2527. }
  2528. CURLcode Curl_http2_switch(struct Curl_easy *data,
  2529. struct connectdata *conn, int sockindex)
  2530. {
  2531. struct Curl_cfilter *cf;
  2532. CURLcode result;
  2533. DEBUGASSERT(!Curl_conn_is_http2(data, conn, sockindex));
  2534. result = http2_cfilter_add(&cf, data, conn, sockindex, FALSE);
  2535. if(result)
  2536. return result;
  2537. CURL_TRC_CF(data, cf, "switching connection to HTTP/2");
  2538. conn->httpversion = 20; /* we know we are on HTTP/2 now */
  2539. conn->bits.multiplex = TRUE; /* at least potentially multiplexed */
  2540. Curl_multi_connchanged(data->multi);
  2541. if(cf->next) {
  2542. bool done;
  2543. return Curl_conn_cf_connect(cf, data, FALSE, &done);
  2544. }
  2545. return CURLE_OK;
  2546. }
  2547. CURLcode Curl_http2_switch_at(struct Curl_cfilter *cf, struct Curl_easy *data)
  2548. {
  2549. struct Curl_cfilter *cf_h2;
  2550. CURLcode result;
  2551. DEBUGASSERT(!Curl_cf_is_http2(cf, data));
  2552. result = http2_cfilter_insert_after(cf, data, FALSE);
  2553. if(result)
  2554. return result;
  2555. cf_h2 = cf->next;
  2556. cf->conn->httpversion = 20; /* we know we are on HTTP/2 now */
  2557. cf->conn->bits.multiplex = TRUE; /* at least potentially multiplexed */
  2558. Curl_multi_connchanged(data->multi);
  2559. if(cf_h2->next) {
  2560. bool done;
  2561. return Curl_conn_cf_connect(cf_h2, data, FALSE, &done);
  2562. }
  2563. return CURLE_OK;
  2564. }
  2565. CURLcode Curl_http2_upgrade(struct Curl_easy *data,
  2566. struct connectdata *conn, int sockindex,
  2567. const char *mem, size_t nread)
  2568. {
  2569. struct Curl_cfilter *cf;
  2570. struct cf_h2_ctx *ctx;
  2571. CURLcode result;
  2572. DEBUGASSERT(!Curl_conn_is_http2(data, conn, sockindex));
  2573. DEBUGASSERT(data->req.upgr101 == UPGR101_RECEIVED);
  2574. result = http2_cfilter_add(&cf, data, conn, sockindex, TRUE);
  2575. if(result)
  2576. return result;
  2577. CURL_TRC_CF(data, cf, "upgrading connection to HTTP/2");
  2578. DEBUGASSERT(cf->cft == &Curl_cft_nghttp2);
  2579. ctx = cf->ctx;
  2580. if(nread > 0) {
  2581. /* Remaining data from the protocol switch reply is already using
  2582. * the switched protocol, ie. HTTP/2. We add that to the network
  2583. * inbufq. */
  2584. ssize_t copied;
  2585. copied = Curl_bufq_write(&ctx->inbufq,
  2586. (const unsigned char *)mem, nread, &result);
  2587. if(copied < 0) {
  2588. failf(data, "error on copying HTTP Upgrade response: %d", result);
  2589. return CURLE_RECV_ERROR;
  2590. }
  2591. if((size_t)copied < nread) {
  2592. failf(data, "connection buffer size could not take all data "
  2593. "from HTTP Upgrade response header: copied=%zd, datalen=%zu",
  2594. copied, nread);
  2595. return CURLE_HTTP2;
  2596. }
  2597. infof(data, "Copied HTTP/2 data in stream buffer to connection buffer"
  2598. " after upgrade: len=%zu", nread);
  2599. }
  2600. conn->httpversion = 20; /* we know we are on HTTP/2 now */
  2601. conn->bits.multiplex = TRUE; /* at least potentially multiplexed */
  2602. Curl_multi_connchanged(data->multi);
  2603. if(cf->next) {
  2604. bool done;
  2605. return Curl_conn_cf_connect(cf, data, FALSE, &done);
  2606. }
  2607. return CURLE_OK;
  2608. }
  2609. /* Only call this function for a transfer that already got an HTTP/2
  2610. CURLE_HTTP2_STREAM error! */
  2611. bool Curl_h2_http_1_1_error(struct Curl_easy *data)
  2612. {
  2613. if(Curl_conn_is_http2(data, data->conn, FIRSTSOCKET)) {
  2614. int err = Curl_conn_get_stream_error(data, data->conn, FIRSTSOCKET);
  2615. return (err == NGHTTP2_HTTP_1_1_REQUIRED);
  2616. }
  2617. return FALSE;
  2618. }
  2619. #else /* !USE_NGHTTP2 */
  2620. /* Satisfy external references even if http2 is not compiled in. */
  2621. #include <curl/curl.h>
  2622. char *curl_pushheader_bynum(struct curl_pushheaders *h, size_t num)
  2623. {
  2624. (void) h;
  2625. (void) num;
  2626. return NULL;
  2627. }
  2628. char *curl_pushheader_byname(struct curl_pushheaders *h, const char *header)
  2629. {
  2630. (void) h;
  2631. (void) header;
  2632. return NULL;
  2633. }
  2634. #endif /* USE_NGHTTP2 */