http2.c 78 KB

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