http2.c 79 KB

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