http2.c 88 KB

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