http2.c 93 KB

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