http2.c 83 KB

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