2
0

cf-h2-proxy.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636
  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. #if defined(USE_NGHTTP2) && !defined(CURL_DISABLE_PROXY)
  26. #include <nghttp2/nghttp2.h>
  27. #include "urldata.h"
  28. #include "cfilters.h"
  29. #include "connect.h"
  30. #include "curl_trc.h"
  31. #include "bufq.h"
  32. #include "dynbuf.h"
  33. #include "dynhds.h"
  34. #include "http1.h"
  35. #include "http2.h"
  36. #include "http_proxy.h"
  37. #include "multiif.h"
  38. #include "sendf.h"
  39. #include "cf-h2-proxy.h"
  40. /* The last 3 #include files should be in this order */
  41. #include "curl_printf.h"
  42. #include "curl_memory.h"
  43. #include "memdebug.h"
  44. #define PROXY_H2_CHUNK_SIZE (16*1024)
  45. #define PROXY_HTTP2_HUGE_WINDOW_SIZE (100 * 1024 * 1024)
  46. #define H2_TUNNEL_WINDOW_SIZE (10 * 1024 * 1024)
  47. #define PROXY_H2_NW_RECV_CHUNKS (H2_TUNNEL_WINDOW_SIZE / PROXY_H2_CHUNK_SIZE)
  48. #define PROXY_H2_NW_SEND_CHUNKS 1
  49. #define H2_TUNNEL_RECV_CHUNKS (H2_TUNNEL_WINDOW_SIZE / PROXY_H2_CHUNK_SIZE)
  50. #define H2_TUNNEL_SEND_CHUNKS ((128 * 1024) / PROXY_H2_CHUNK_SIZE)
  51. typedef enum {
  52. H2_TUNNEL_INIT, /* init/default/no tunnel state */
  53. H2_TUNNEL_CONNECT, /* CONNECT request is being send */
  54. H2_TUNNEL_RESPONSE, /* CONNECT response received completely */
  55. H2_TUNNEL_ESTABLISHED,
  56. H2_TUNNEL_FAILED
  57. } h2_tunnel_state;
  58. struct tunnel_stream {
  59. struct http_resp *resp;
  60. struct bufq recvbuf;
  61. struct bufq sendbuf;
  62. char *authority;
  63. int32_t stream_id;
  64. uint32_t error;
  65. size_t upload_blocked_len;
  66. h2_tunnel_state state;
  67. BIT(has_final_response);
  68. BIT(closed);
  69. BIT(reset);
  70. };
  71. static CURLcode tunnel_stream_init(struct Curl_cfilter *cf,
  72. struct tunnel_stream *ts)
  73. {
  74. const char *hostname;
  75. int port;
  76. bool ipv6_ip;
  77. CURLcode result;
  78. ts->state = H2_TUNNEL_INIT;
  79. ts->stream_id = -1;
  80. Curl_bufq_init2(&ts->recvbuf, PROXY_H2_CHUNK_SIZE, H2_TUNNEL_RECV_CHUNKS,
  81. BUFQ_OPT_SOFT_LIMIT);
  82. Curl_bufq_init(&ts->sendbuf, PROXY_H2_CHUNK_SIZE, H2_TUNNEL_SEND_CHUNKS);
  83. result = Curl_http_proxy_get_destination(cf, &hostname, &port, &ipv6_ip);
  84. if(result)
  85. return result;
  86. ts->authority = /* host:port with IPv6 support */
  87. aprintf("%s%s%s:%d", ipv6_ip?"[":"", hostname, ipv6_ip?"]":"", port);
  88. if(!ts->authority)
  89. return CURLE_OUT_OF_MEMORY;
  90. return CURLE_OK;
  91. }
  92. static void tunnel_stream_clear(struct tunnel_stream *ts)
  93. {
  94. Curl_http_resp_free(ts->resp);
  95. Curl_bufq_free(&ts->recvbuf);
  96. Curl_bufq_free(&ts->sendbuf);
  97. Curl_safefree(ts->authority);
  98. memset(ts, 0, sizeof(*ts));
  99. ts->state = H2_TUNNEL_INIT;
  100. }
  101. static void h2_tunnel_go_state(struct Curl_cfilter *cf,
  102. struct tunnel_stream *ts,
  103. h2_tunnel_state new_state,
  104. struct Curl_easy *data)
  105. {
  106. (void)cf;
  107. if(ts->state == new_state)
  108. return;
  109. /* leaving this one */
  110. switch(ts->state) {
  111. case H2_TUNNEL_CONNECT:
  112. data->req.ignorebody = FALSE;
  113. break;
  114. default:
  115. break;
  116. }
  117. /* entering this one */
  118. switch(new_state) {
  119. case H2_TUNNEL_INIT:
  120. CURL_TRC_CF(data, cf, "[%d] new tunnel state 'init'", ts->stream_id);
  121. tunnel_stream_clear(ts);
  122. break;
  123. case H2_TUNNEL_CONNECT:
  124. CURL_TRC_CF(data, cf, "[%d] new tunnel state 'connect'", ts->stream_id);
  125. ts->state = H2_TUNNEL_CONNECT;
  126. break;
  127. case H2_TUNNEL_RESPONSE:
  128. CURL_TRC_CF(data, cf, "[%d] new tunnel state 'response'", ts->stream_id);
  129. ts->state = H2_TUNNEL_RESPONSE;
  130. break;
  131. case H2_TUNNEL_ESTABLISHED:
  132. CURL_TRC_CF(data, cf, "[%d] new tunnel state 'established'",
  133. ts->stream_id);
  134. infof(data, "CONNECT phase completed");
  135. data->state.authproxy.done = TRUE;
  136. data->state.authproxy.multipass = FALSE;
  137. FALLTHROUGH();
  138. case H2_TUNNEL_FAILED:
  139. if(new_state == H2_TUNNEL_FAILED)
  140. CURL_TRC_CF(data, cf, "[%d] new tunnel state 'failed'", ts->stream_id);
  141. ts->state = new_state;
  142. /* If a proxy-authorization header was used for the proxy, then we should
  143. make sure that it is not accidentally used for the document request
  144. after we have connected. So let's free and clear it here. */
  145. Curl_safefree(data->state.aptr.proxyuserpwd);
  146. break;
  147. }
  148. }
  149. struct cf_h2_proxy_ctx {
  150. nghttp2_session *h2;
  151. /* The easy handle used in the current filter call, cleared at return */
  152. struct cf_call_data call_data;
  153. struct bufq inbufq; /* network receive buffer */
  154. struct bufq outbufq; /* network send buffer */
  155. struct tunnel_stream tunnel; /* our tunnel CONNECT stream */
  156. int32_t goaway_error;
  157. int32_t last_stream_id;
  158. BIT(conn_closed);
  159. BIT(rcvd_goaway);
  160. BIT(sent_goaway);
  161. BIT(nw_out_blocked);
  162. };
  163. /* How to access `call_data` from a cf_h2 filter */
  164. #undef CF_CTX_CALL_DATA
  165. #define CF_CTX_CALL_DATA(cf) \
  166. ((struct cf_h2_proxy_ctx *)(cf)->ctx)->call_data
  167. static void cf_h2_proxy_ctx_clear(struct cf_h2_proxy_ctx *ctx)
  168. {
  169. struct cf_call_data save = ctx->call_data;
  170. if(ctx->h2) {
  171. nghttp2_session_del(ctx->h2);
  172. }
  173. Curl_bufq_free(&ctx->inbufq);
  174. Curl_bufq_free(&ctx->outbufq);
  175. tunnel_stream_clear(&ctx->tunnel);
  176. memset(ctx, 0, sizeof(*ctx));
  177. ctx->call_data = save;
  178. }
  179. static void cf_h2_proxy_ctx_free(struct cf_h2_proxy_ctx *ctx)
  180. {
  181. if(ctx) {
  182. cf_h2_proxy_ctx_clear(ctx);
  183. free(ctx);
  184. }
  185. }
  186. static void drain_tunnel(struct Curl_cfilter *cf,
  187. struct Curl_easy *data,
  188. struct tunnel_stream *tunnel)
  189. {
  190. unsigned char bits;
  191. (void)cf;
  192. bits = CURL_CSELECT_IN;
  193. if(!tunnel->closed && !tunnel->reset && tunnel->upload_blocked_len)
  194. bits |= CURL_CSELECT_OUT;
  195. if(data->state.select_bits != bits) {
  196. CURL_TRC_CF(data, cf, "[%d] DRAIN select_bits=%x",
  197. tunnel->stream_id, bits);
  198. data->state.select_bits = bits;
  199. Curl_expire(data, 0, EXPIRE_RUN_NOW);
  200. }
  201. }
  202. static ssize_t proxy_nw_in_reader(void *reader_ctx,
  203. unsigned char *buf, size_t buflen,
  204. CURLcode *err)
  205. {
  206. struct Curl_cfilter *cf = reader_ctx;
  207. ssize_t nread;
  208. if(cf) {
  209. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  210. nread = Curl_conn_cf_recv(cf->next, data, (char *)buf, buflen, err);
  211. CURL_TRC_CF(data, cf, "[0] nw_in_reader(len=%zu) -> %zd, %d",
  212. buflen, nread, *err);
  213. }
  214. else {
  215. nread = 0;
  216. }
  217. return nread;
  218. }
  219. static ssize_t proxy_h2_nw_out_writer(void *writer_ctx,
  220. const unsigned char *buf, size_t buflen,
  221. CURLcode *err)
  222. {
  223. struct Curl_cfilter *cf = writer_ctx;
  224. ssize_t nwritten;
  225. if(cf) {
  226. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  227. nwritten = Curl_conn_cf_send(cf->next, data, (const char *)buf, buflen,
  228. err);
  229. CURL_TRC_CF(data, cf, "[0] nw_out_writer(len=%zu) -> %zd, %d",
  230. buflen, nwritten, *err);
  231. }
  232. else {
  233. nwritten = 0;
  234. }
  235. return nwritten;
  236. }
  237. static int proxy_h2_client_new(struct Curl_cfilter *cf,
  238. nghttp2_session_callbacks *cbs)
  239. {
  240. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  241. nghttp2_option *o;
  242. int rc = nghttp2_option_new(&o);
  243. if(rc)
  244. return rc;
  245. /* We handle window updates ourself to enforce buffer limits */
  246. nghttp2_option_set_no_auto_window_update(o, 1);
  247. #if NGHTTP2_VERSION_NUM >= 0x013200
  248. /* with 1.50.0 */
  249. /* turn off RFC 9113 leading and trailing white spaces validation against
  250. HTTP field value. */
  251. nghttp2_option_set_no_rfc9113_leading_and_trailing_ws_validation(o, 1);
  252. #endif
  253. rc = nghttp2_session_client_new2(&ctx->h2, cbs, cf, o);
  254. nghttp2_option_del(o);
  255. return rc;
  256. }
  257. static ssize_t on_session_send(nghttp2_session *h2,
  258. const uint8_t *buf, size_t blen,
  259. int flags, void *userp);
  260. static int proxy_h2_on_frame_recv(nghttp2_session *session,
  261. const nghttp2_frame *frame,
  262. void *userp);
  263. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  264. static int proxy_h2_on_frame_send(nghttp2_session *session,
  265. const nghttp2_frame *frame,
  266. void *userp);
  267. #endif
  268. static int proxy_h2_on_stream_close(nghttp2_session *session,
  269. int32_t stream_id,
  270. uint32_t error_code, void *userp);
  271. static int proxy_h2_on_header(nghttp2_session *session,
  272. const nghttp2_frame *frame,
  273. const uint8_t *name, size_t namelen,
  274. const uint8_t *value, size_t valuelen,
  275. uint8_t flags,
  276. void *userp);
  277. static int tunnel_recv_callback(nghttp2_session *session, uint8_t flags,
  278. int32_t stream_id,
  279. const uint8_t *mem, size_t len, void *userp);
  280. /*
  281. * Initialize the cfilter context
  282. */
  283. static CURLcode cf_h2_proxy_ctx_init(struct Curl_cfilter *cf,
  284. struct Curl_easy *data)
  285. {
  286. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  287. CURLcode result = CURLE_OUT_OF_MEMORY;
  288. nghttp2_session_callbacks *cbs = NULL;
  289. int rc;
  290. DEBUGASSERT(!ctx->h2);
  291. memset(&ctx->tunnel, 0, sizeof(ctx->tunnel));
  292. Curl_bufq_init(&ctx->inbufq, PROXY_H2_CHUNK_SIZE, PROXY_H2_NW_RECV_CHUNKS);
  293. Curl_bufq_init(&ctx->outbufq, PROXY_H2_CHUNK_SIZE, PROXY_H2_NW_SEND_CHUNKS);
  294. if(tunnel_stream_init(cf, &ctx->tunnel))
  295. goto out;
  296. rc = nghttp2_session_callbacks_new(&cbs);
  297. if(rc) {
  298. failf(data, "Couldn't initialize nghttp2 callbacks");
  299. goto out;
  300. }
  301. nghttp2_session_callbacks_set_send_callback(cbs, on_session_send);
  302. nghttp2_session_callbacks_set_on_frame_recv_callback(
  303. cbs, proxy_h2_on_frame_recv);
  304. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  305. nghttp2_session_callbacks_set_on_frame_send_callback(cbs,
  306. proxy_h2_on_frame_send);
  307. #endif
  308. nghttp2_session_callbacks_set_on_data_chunk_recv_callback(
  309. cbs, tunnel_recv_callback);
  310. nghttp2_session_callbacks_set_on_stream_close_callback(
  311. cbs, proxy_h2_on_stream_close);
  312. nghttp2_session_callbacks_set_on_header_callback(cbs, proxy_h2_on_header);
  313. /* The nghttp2 session is not yet setup, do it */
  314. rc = proxy_h2_client_new(cf, cbs);
  315. if(rc) {
  316. failf(data, "Couldn't initialize nghttp2");
  317. goto out;
  318. }
  319. {
  320. nghttp2_settings_entry iv[3];
  321. iv[0].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS;
  322. iv[0].value = Curl_multi_max_concurrent_streams(data->multi);
  323. iv[1].settings_id = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE;
  324. iv[1].value = H2_TUNNEL_WINDOW_SIZE;
  325. iv[2].settings_id = NGHTTP2_SETTINGS_ENABLE_PUSH;
  326. iv[2].value = 0;
  327. rc = nghttp2_submit_settings(ctx->h2, NGHTTP2_FLAG_NONE, iv, 3);
  328. if(rc) {
  329. failf(data, "nghttp2_submit_settings() failed: %s(%d)",
  330. nghttp2_strerror(rc), rc);
  331. result = CURLE_HTTP2;
  332. goto out;
  333. }
  334. }
  335. rc = nghttp2_session_set_local_window_size(ctx->h2, NGHTTP2_FLAG_NONE, 0,
  336. PROXY_HTTP2_HUGE_WINDOW_SIZE);
  337. if(rc) {
  338. failf(data, "nghttp2_session_set_local_window_size() failed: %s(%d)",
  339. nghttp2_strerror(rc), rc);
  340. result = CURLE_HTTP2;
  341. goto out;
  342. }
  343. /* all set, traffic will be send on connect */
  344. result = CURLE_OK;
  345. out:
  346. if(cbs)
  347. nghttp2_session_callbacks_del(cbs);
  348. CURL_TRC_CF(data, cf, "[0] init proxy ctx -> %d", result);
  349. return result;
  350. }
  351. static int proxy_h2_should_close_session(struct cf_h2_proxy_ctx *ctx)
  352. {
  353. return !nghttp2_session_want_read(ctx->h2) &&
  354. !nghttp2_session_want_write(ctx->h2);
  355. }
  356. static CURLcode proxy_h2_nw_out_flush(struct Curl_cfilter *cf,
  357. struct Curl_easy *data)
  358. {
  359. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  360. ssize_t nwritten;
  361. CURLcode result;
  362. (void)data;
  363. if(Curl_bufq_is_empty(&ctx->outbufq))
  364. return CURLE_OK;
  365. nwritten = Curl_bufq_pass(&ctx->outbufq, proxy_h2_nw_out_writer, cf,
  366. &result);
  367. if(nwritten < 0) {
  368. if(result == CURLE_AGAIN) {
  369. CURL_TRC_CF(data, cf, "[0] flush nw send buffer(%zu) -> EAGAIN",
  370. Curl_bufq_len(&ctx->outbufq));
  371. ctx->nw_out_blocked = 1;
  372. }
  373. return result;
  374. }
  375. CURL_TRC_CF(data, cf, "[0] nw send buffer flushed");
  376. return Curl_bufq_is_empty(&ctx->outbufq)? CURLE_OK: CURLE_AGAIN;
  377. }
  378. /*
  379. * Processes pending input left in network input buffer.
  380. * This function returns 0 if it succeeds, or -1 and error code will
  381. * be assigned to *err.
  382. */
  383. static int proxy_h2_process_pending_input(struct Curl_cfilter *cf,
  384. struct Curl_easy *data,
  385. CURLcode *err)
  386. {
  387. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  388. const unsigned char *buf;
  389. size_t blen;
  390. ssize_t rv;
  391. while(Curl_bufq_peek(&ctx->inbufq, &buf, &blen)) {
  392. rv = nghttp2_session_mem_recv(ctx->h2, (const uint8_t *)buf, blen);
  393. CURL_TRC_CF(data, cf, "[0] %zu bytes to nghttp2 -> %zd", blen, rv);
  394. if(rv < 0) {
  395. failf(data,
  396. "process_pending_input: nghttp2_session_mem_recv() returned "
  397. "%zd:%s", rv, nghttp2_strerror((int)rv));
  398. *err = CURLE_RECV_ERROR;
  399. return -1;
  400. }
  401. Curl_bufq_skip(&ctx->inbufq, (size_t)rv);
  402. if(Curl_bufq_is_empty(&ctx->inbufq)) {
  403. CURL_TRC_CF(data, cf, "[0] all data in connection buffer processed");
  404. break;
  405. }
  406. else {
  407. CURL_TRC_CF(data, cf, "[0] process_pending_input: %zu bytes left "
  408. "in connection buffer", Curl_bufq_len(&ctx->inbufq));
  409. }
  410. }
  411. return 0;
  412. }
  413. static CURLcode proxy_h2_progress_ingress(struct Curl_cfilter *cf,
  414. struct Curl_easy *data)
  415. {
  416. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  417. CURLcode result = CURLE_OK;
  418. ssize_t nread;
  419. /* Process network input buffer fist */
  420. if(!Curl_bufq_is_empty(&ctx->inbufq)) {
  421. CURL_TRC_CF(data, cf, "[0] process %zu bytes in connection buffer",
  422. Curl_bufq_len(&ctx->inbufq));
  423. if(proxy_h2_process_pending_input(cf, data, &result) < 0)
  424. return result;
  425. }
  426. /* Receive data from the "lower" filters, e.g. network until
  427. * it is time to stop or we have enough data for this stream */
  428. while(!ctx->conn_closed && /* not closed the connection */
  429. !ctx->tunnel.closed && /* nor the tunnel */
  430. Curl_bufq_is_empty(&ctx->inbufq) && /* and we consumed our input */
  431. !Curl_bufq_is_full(&ctx->tunnel.recvbuf)) {
  432. nread = Curl_bufq_slurp(&ctx->inbufq, proxy_nw_in_reader, cf, &result);
  433. CURL_TRC_CF(data, cf, "[0] read %zu bytes nw data -> %zd, %d",
  434. Curl_bufq_len(&ctx->inbufq), nread, result);
  435. if(nread < 0) {
  436. if(result != CURLE_AGAIN) {
  437. failf(data, "Failed receiving HTTP2 data");
  438. return result;
  439. }
  440. break;
  441. }
  442. else if(nread == 0) {
  443. ctx->conn_closed = TRUE;
  444. break;
  445. }
  446. if(proxy_h2_process_pending_input(cf, data, &result))
  447. return result;
  448. }
  449. if(ctx->conn_closed && Curl_bufq_is_empty(&ctx->inbufq)) {
  450. connclose(cf->conn, "GOAWAY received");
  451. }
  452. return CURLE_OK;
  453. }
  454. static CURLcode proxy_h2_progress_egress(struct Curl_cfilter *cf,
  455. struct Curl_easy *data)
  456. {
  457. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  458. int rv = 0;
  459. ctx->nw_out_blocked = 0;
  460. while(!rv && !ctx->nw_out_blocked && nghttp2_session_want_write(ctx->h2))
  461. rv = nghttp2_session_send(ctx->h2);
  462. if(nghttp2_is_fatal(rv)) {
  463. CURL_TRC_CF(data, cf, "[0] nghttp2_session_send error (%s)%d",
  464. nghttp2_strerror(rv), rv);
  465. return CURLE_SEND_ERROR;
  466. }
  467. return proxy_h2_nw_out_flush(cf, data);
  468. }
  469. static ssize_t on_session_send(nghttp2_session *h2,
  470. const uint8_t *buf, size_t blen, int flags,
  471. void *userp)
  472. {
  473. struct Curl_cfilter *cf = userp;
  474. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  475. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  476. ssize_t nwritten;
  477. CURLcode result = CURLE_OK;
  478. (void)h2;
  479. (void)flags;
  480. DEBUGASSERT(data);
  481. nwritten = Curl_bufq_write_pass(&ctx->outbufq, buf, blen,
  482. proxy_h2_nw_out_writer, cf, &result);
  483. if(nwritten < 0) {
  484. if(result == CURLE_AGAIN) {
  485. return NGHTTP2_ERR_WOULDBLOCK;
  486. }
  487. failf(data, "Failed sending HTTP2 data");
  488. return NGHTTP2_ERR_CALLBACK_FAILURE;
  489. }
  490. if(!nwritten)
  491. return NGHTTP2_ERR_WOULDBLOCK;
  492. return nwritten;
  493. }
  494. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  495. static int proxy_h2_fr_print(const nghttp2_frame *frame,
  496. char *buffer, size_t blen)
  497. {
  498. switch(frame->hd.type) {
  499. case NGHTTP2_DATA: {
  500. return msnprintf(buffer, blen,
  501. "FRAME[DATA, len=%d, eos=%d, padlen=%d]",
  502. (int)frame->hd.length,
  503. !!(frame->hd.flags & NGHTTP2_FLAG_END_STREAM),
  504. (int)frame->data.padlen);
  505. }
  506. case NGHTTP2_HEADERS: {
  507. return msnprintf(buffer, blen,
  508. "FRAME[HEADERS, len=%d, hend=%d, eos=%d]",
  509. (int)frame->hd.length,
  510. !!(frame->hd.flags & NGHTTP2_FLAG_END_HEADERS),
  511. !!(frame->hd.flags & NGHTTP2_FLAG_END_STREAM));
  512. }
  513. case NGHTTP2_PRIORITY: {
  514. return msnprintf(buffer, blen,
  515. "FRAME[PRIORITY, len=%d, flags=%d]",
  516. (int)frame->hd.length, frame->hd.flags);
  517. }
  518. case NGHTTP2_RST_STREAM: {
  519. return msnprintf(buffer, blen,
  520. "FRAME[RST_STREAM, len=%d, flags=%d, error=%u]",
  521. (int)frame->hd.length, frame->hd.flags,
  522. frame->rst_stream.error_code);
  523. }
  524. case NGHTTP2_SETTINGS: {
  525. if(frame->hd.flags & NGHTTP2_FLAG_ACK) {
  526. return msnprintf(buffer, blen, "FRAME[SETTINGS, ack=1]");
  527. }
  528. return msnprintf(buffer, blen,
  529. "FRAME[SETTINGS, len=%d]", (int)frame->hd.length);
  530. }
  531. case NGHTTP2_PUSH_PROMISE: {
  532. return msnprintf(buffer, blen,
  533. "FRAME[PUSH_PROMISE, len=%d, hend=%d]",
  534. (int)frame->hd.length,
  535. !!(frame->hd.flags & NGHTTP2_FLAG_END_HEADERS));
  536. }
  537. case NGHTTP2_PING: {
  538. return msnprintf(buffer, blen,
  539. "FRAME[PING, len=%d, ack=%d]",
  540. (int)frame->hd.length,
  541. frame->hd.flags&NGHTTP2_FLAG_ACK);
  542. }
  543. case NGHTTP2_GOAWAY: {
  544. char scratch[128];
  545. size_t s_len = sizeof(scratch)/sizeof(scratch[0]);
  546. size_t len = (frame->goaway.opaque_data_len < s_len)?
  547. frame->goaway.opaque_data_len : s_len-1;
  548. if(len)
  549. memcpy(scratch, frame->goaway.opaque_data, len);
  550. scratch[len] = '\0';
  551. return msnprintf(buffer, blen, "FRAME[GOAWAY, error=%d, reason='%s', "
  552. "last_stream=%d]", frame->goaway.error_code,
  553. scratch, frame->goaway.last_stream_id);
  554. }
  555. case NGHTTP2_WINDOW_UPDATE: {
  556. return msnprintf(buffer, blen,
  557. "FRAME[WINDOW_UPDATE, incr=%d]",
  558. frame->window_update.window_size_increment);
  559. }
  560. default:
  561. return msnprintf(buffer, blen, "FRAME[%d, len=%d, flags=%d]",
  562. frame->hd.type, (int)frame->hd.length,
  563. frame->hd.flags);
  564. }
  565. }
  566. static int proxy_h2_on_frame_send(nghttp2_session *session,
  567. const nghttp2_frame *frame,
  568. void *userp)
  569. {
  570. struct Curl_cfilter *cf = userp;
  571. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  572. (void)session;
  573. DEBUGASSERT(data);
  574. if(data && Curl_trc_cf_is_verbose(cf, data)) {
  575. char buffer[256];
  576. int len;
  577. len = proxy_h2_fr_print(frame, buffer, sizeof(buffer)-1);
  578. buffer[len] = 0;
  579. CURL_TRC_CF(data, cf, "[%d] -> %s", frame->hd.stream_id, buffer);
  580. }
  581. return 0;
  582. }
  583. #endif /* !CURL_DISABLE_VERBOSE_STRINGS */
  584. static int proxy_h2_on_frame_recv(nghttp2_session *session,
  585. const nghttp2_frame *frame,
  586. void *userp)
  587. {
  588. struct Curl_cfilter *cf = userp;
  589. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  590. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  591. int32_t stream_id = frame->hd.stream_id;
  592. (void)session;
  593. DEBUGASSERT(data);
  594. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  595. if(Curl_trc_cf_is_verbose(cf, data)) {
  596. char buffer[256];
  597. int len;
  598. len = proxy_h2_fr_print(frame, buffer, sizeof(buffer)-1);
  599. buffer[len] = 0;
  600. CURL_TRC_CF(data, cf, "[%d] <- %s",frame->hd.stream_id, buffer);
  601. }
  602. #endif /* !CURL_DISABLE_VERBOSE_STRINGS */
  603. if(!stream_id) {
  604. /* stream ID zero is for connection-oriented stuff */
  605. DEBUGASSERT(data);
  606. switch(frame->hd.type) {
  607. case NGHTTP2_SETTINGS:
  608. /* Since the initial stream window is 64K, a request might be on HOLD,
  609. * due to exhaustion. The (initial) SETTINGS may announce a much larger
  610. * window and *assume* that we treat this like a WINDOW_UPDATE. Some
  611. * servers send an explicit WINDOW_UPDATE, but not all seem to do that.
  612. * To be safe, we UNHOLD a stream in order not to stall. */
  613. if(CURL_WANT_SEND(data)) {
  614. drain_tunnel(cf, data, &ctx->tunnel);
  615. }
  616. break;
  617. case NGHTTP2_GOAWAY:
  618. ctx->rcvd_goaway = TRUE;
  619. break;
  620. default:
  621. break;
  622. }
  623. return 0;
  624. }
  625. if(stream_id != ctx->tunnel.stream_id) {
  626. CURL_TRC_CF(data, cf, "[%d] rcvd FRAME not for tunnel", stream_id);
  627. return NGHTTP2_ERR_CALLBACK_FAILURE;
  628. }
  629. switch(frame->hd.type) {
  630. case NGHTTP2_HEADERS:
  631. /* nghttp2 guarantees that :status is received, and we store it to
  632. stream->status_code. Fuzzing has proven this can still be reached
  633. without status code having been set. */
  634. if(!ctx->tunnel.resp)
  635. return NGHTTP2_ERR_CALLBACK_FAILURE;
  636. /* Only final status code signals the end of header */
  637. CURL_TRC_CF(data, cf, "[%d] got http status: %d",
  638. stream_id, ctx->tunnel.resp->status);
  639. if(!ctx->tunnel.has_final_response) {
  640. if(ctx->tunnel.resp->status / 100 != 1) {
  641. ctx->tunnel.has_final_response = TRUE;
  642. }
  643. }
  644. break;
  645. case NGHTTP2_WINDOW_UPDATE:
  646. if(CURL_WANT_SEND(data)) {
  647. drain_tunnel(cf, data, &ctx->tunnel);
  648. }
  649. break;
  650. default:
  651. break;
  652. }
  653. return 0;
  654. }
  655. static int proxy_h2_on_header(nghttp2_session *session,
  656. const nghttp2_frame *frame,
  657. const uint8_t *name, size_t namelen,
  658. const uint8_t *value, size_t valuelen,
  659. uint8_t flags,
  660. void *userp)
  661. {
  662. struct Curl_cfilter *cf = userp;
  663. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  664. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  665. int32_t stream_id = frame->hd.stream_id;
  666. CURLcode result;
  667. (void)flags;
  668. (void)data;
  669. (void)session;
  670. DEBUGASSERT(stream_id); /* should never be a zero stream ID here */
  671. if(stream_id != ctx->tunnel.stream_id) {
  672. CURL_TRC_CF(data, cf, "[%d] header for non-tunnel stream: "
  673. "%.*s: %.*s", stream_id,
  674. (int)namelen, name, (int)valuelen, value);
  675. return NGHTTP2_ERR_CALLBACK_FAILURE;
  676. }
  677. if(frame->hd.type == NGHTTP2_PUSH_PROMISE)
  678. return NGHTTP2_ERR_CALLBACK_FAILURE;
  679. if(ctx->tunnel.has_final_response) {
  680. /* we do not do anything with trailers for tunnel streams */
  681. return 0;
  682. }
  683. if(namelen == sizeof(HTTP_PSEUDO_STATUS) - 1 &&
  684. memcmp(HTTP_PSEUDO_STATUS, name, namelen) == 0) {
  685. int http_status;
  686. struct http_resp *resp;
  687. /* status: always comes first, we might get more than one response,
  688. * link the previous ones for keepers */
  689. result = Curl_http_decode_status(&http_status,
  690. (const char *)value, valuelen);
  691. if(result)
  692. return NGHTTP2_ERR_CALLBACK_FAILURE;
  693. result = Curl_http_resp_make(&resp, http_status, NULL);
  694. if(result)
  695. return NGHTTP2_ERR_CALLBACK_FAILURE;
  696. resp->prev = ctx->tunnel.resp;
  697. ctx->tunnel.resp = resp;
  698. CURL_TRC_CF(data, cf, "[%d] status: HTTP/2 %03d",
  699. stream_id, ctx->tunnel.resp->status);
  700. return 0;
  701. }
  702. if(!ctx->tunnel.resp)
  703. return NGHTTP2_ERR_CALLBACK_FAILURE;
  704. result = Curl_dynhds_add(&ctx->tunnel.resp->headers,
  705. (const char *)name, namelen,
  706. (const char *)value, valuelen);
  707. if(result)
  708. return NGHTTP2_ERR_CALLBACK_FAILURE;
  709. CURL_TRC_CF(data, cf, "[%d] header: %.*s: %.*s",
  710. stream_id, (int)namelen, name, (int)valuelen, value);
  711. return 0; /* 0 is successful */
  712. }
  713. static ssize_t tunnel_send_callback(nghttp2_session *session,
  714. int32_t stream_id,
  715. uint8_t *buf, size_t length,
  716. uint32_t *data_flags,
  717. nghttp2_data_source *source,
  718. void *userp)
  719. {
  720. struct Curl_cfilter *cf = userp;
  721. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  722. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  723. struct tunnel_stream *ts;
  724. CURLcode result;
  725. ssize_t nread;
  726. (void)source;
  727. (void)data;
  728. (void)ctx;
  729. if(!stream_id)
  730. return NGHTTP2_ERR_INVALID_ARGUMENT;
  731. ts = nghttp2_session_get_stream_user_data(session, stream_id);
  732. if(!ts)
  733. return NGHTTP2_ERR_CALLBACK_FAILURE;
  734. DEBUGASSERT(ts == &ctx->tunnel);
  735. nread = Curl_bufq_read(&ts->sendbuf, buf, length, &result);
  736. if(nread < 0) {
  737. if(result != CURLE_AGAIN)
  738. return NGHTTP2_ERR_CALLBACK_FAILURE;
  739. return NGHTTP2_ERR_DEFERRED;
  740. }
  741. if(ts->closed && Curl_bufq_is_empty(&ts->sendbuf))
  742. *data_flags = NGHTTP2_DATA_FLAG_EOF;
  743. CURL_TRC_CF(data, cf, "[%d] tunnel_send_callback -> %zd",
  744. ts->stream_id, nread);
  745. return nread;
  746. }
  747. static int tunnel_recv_callback(nghttp2_session *session, uint8_t flags,
  748. int32_t stream_id,
  749. const uint8_t *mem, size_t len, void *userp)
  750. {
  751. struct Curl_cfilter *cf = userp;
  752. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  753. ssize_t nwritten;
  754. CURLcode result;
  755. (void)flags;
  756. (void)session;
  757. DEBUGASSERT(stream_id); /* should never be a zero stream ID here */
  758. if(stream_id != ctx->tunnel.stream_id)
  759. return NGHTTP2_ERR_CALLBACK_FAILURE;
  760. nwritten = Curl_bufq_write(&ctx->tunnel.recvbuf, mem, len, &result);
  761. if(nwritten < 0) {
  762. if(result != CURLE_AGAIN)
  763. return NGHTTP2_ERR_CALLBACK_FAILURE;
  764. nwritten = 0;
  765. }
  766. DEBUGASSERT((size_t)nwritten == len);
  767. return 0;
  768. }
  769. static int proxy_h2_on_stream_close(nghttp2_session *session,
  770. int32_t stream_id,
  771. uint32_t error_code, void *userp)
  772. {
  773. struct Curl_cfilter *cf = userp;
  774. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  775. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  776. (void)session;
  777. (void)data;
  778. if(stream_id != ctx->tunnel.stream_id)
  779. return 0;
  780. CURL_TRC_CF(data, cf, "[%d] proxy_h2_on_stream_close, %s (err %d)",
  781. stream_id, nghttp2_http2_strerror(error_code), error_code);
  782. ctx->tunnel.closed = TRUE;
  783. ctx->tunnel.error = error_code;
  784. return 0;
  785. }
  786. static CURLcode proxy_h2_submit(int32_t *pstream_id,
  787. struct Curl_cfilter *cf,
  788. struct Curl_easy *data,
  789. nghttp2_session *h2,
  790. struct httpreq *req,
  791. const nghttp2_priority_spec *pri_spec,
  792. void *stream_user_data,
  793. nghttp2_data_source_read_callback read_callback,
  794. void *read_ctx)
  795. {
  796. struct dynhds h2_headers;
  797. nghttp2_nv *nva = NULL;
  798. int32_t stream_id = -1;
  799. size_t nheader;
  800. CURLcode result;
  801. (void)cf;
  802. Curl_dynhds_init(&h2_headers, 0, DYN_HTTP_REQUEST);
  803. result = Curl_http_req_to_h2(&h2_headers, req, data);
  804. if(result)
  805. goto out;
  806. nva = Curl_dynhds_to_nva(&h2_headers, &nheader);
  807. if(!nva) {
  808. result = CURLE_OUT_OF_MEMORY;
  809. goto out;
  810. }
  811. if(read_callback) {
  812. nghttp2_data_provider data_prd;
  813. data_prd.read_callback = read_callback;
  814. data_prd.source.ptr = read_ctx;
  815. stream_id = nghttp2_submit_request(h2, pri_spec, nva, nheader,
  816. &data_prd, stream_user_data);
  817. }
  818. else {
  819. stream_id = nghttp2_submit_request(h2, pri_spec, nva, nheader,
  820. NULL, stream_user_data);
  821. }
  822. if(stream_id < 0) {
  823. failf(data, "nghttp2_session_upgrade2() failed: %s(%d)",
  824. nghttp2_strerror(stream_id), stream_id);
  825. result = CURLE_SEND_ERROR;
  826. goto out;
  827. }
  828. result = CURLE_OK;
  829. out:
  830. free(nva);
  831. Curl_dynhds_free(&h2_headers);
  832. *pstream_id = stream_id;
  833. return result;
  834. }
  835. static CURLcode submit_CONNECT(struct Curl_cfilter *cf,
  836. struct Curl_easy *data,
  837. struct tunnel_stream *ts)
  838. {
  839. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  840. CURLcode result;
  841. struct httpreq *req = NULL;
  842. result = Curl_http_proxy_create_CONNECT(&req, cf, data, 2);
  843. if(result)
  844. goto out;
  845. result = Curl_creader_set_null(data);
  846. if(result)
  847. goto out;
  848. infof(data, "Establish HTTP/2 proxy tunnel to %s", req->authority);
  849. result = proxy_h2_submit(&ts->stream_id, cf, data, ctx->h2, req,
  850. NULL, ts, tunnel_send_callback, cf);
  851. if(result) {
  852. CURL_TRC_CF(data, cf, "[%d] send, nghttp2_submit_request error: %s",
  853. ts->stream_id, nghttp2_strerror(ts->stream_id));
  854. }
  855. out:
  856. if(req)
  857. Curl_http_req_free(req);
  858. if(result)
  859. failf(data, "Failed sending CONNECT to proxy");
  860. return result;
  861. }
  862. static CURLcode inspect_response(struct Curl_cfilter *cf,
  863. struct Curl_easy *data,
  864. struct tunnel_stream *ts)
  865. {
  866. CURLcode result = CURLE_OK;
  867. struct dynhds_entry *auth_reply = NULL;
  868. (void)cf;
  869. DEBUGASSERT(ts->resp);
  870. if(ts->resp->status/100 == 2) {
  871. infof(data, "CONNECT tunnel established, response %d", ts->resp->status);
  872. h2_tunnel_go_state(cf, ts, H2_TUNNEL_ESTABLISHED, data);
  873. return CURLE_OK;
  874. }
  875. if(ts->resp->status == 401) {
  876. auth_reply = Curl_dynhds_cget(&ts->resp->headers, "WWW-Authenticate");
  877. }
  878. else if(ts->resp->status == 407) {
  879. auth_reply = Curl_dynhds_cget(&ts->resp->headers, "Proxy-Authenticate");
  880. }
  881. if(auth_reply) {
  882. CURL_TRC_CF(data, cf, "[0] CONNECT: fwd auth header '%s'",
  883. auth_reply->value);
  884. result = Curl_http_input_auth(data, ts->resp->status == 407,
  885. auth_reply->value);
  886. if(result)
  887. return result;
  888. if(data->req.newurl) {
  889. /* Indicator that we should try again */
  890. Curl_safefree(data->req.newurl);
  891. h2_tunnel_go_state(cf, ts, H2_TUNNEL_INIT, data);
  892. return CURLE_OK;
  893. }
  894. }
  895. /* Seems to have failed */
  896. return CURLE_RECV_ERROR;
  897. }
  898. static CURLcode H2_CONNECT(struct Curl_cfilter *cf,
  899. struct Curl_easy *data,
  900. struct tunnel_stream *ts)
  901. {
  902. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  903. CURLcode result = CURLE_OK;
  904. DEBUGASSERT(ts);
  905. DEBUGASSERT(ts->authority);
  906. do {
  907. switch(ts->state) {
  908. case H2_TUNNEL_INIT:
  909. /* Prepare the CONNECT request and make a first attempt to send. */
  910. CURL_TRC_CF(data, cf, "[0] CONNECT start for %s", ts->authority);
  911. result = submit_CONNECT(cf, data, ts);
  912. if(result)
  913. goto out;
  914. h2_tunnel_go_state(cf, ts, H2_TUNNEL_CONNECT, data);
  915. FALLTHROUGH();
  916. case H2_TUNNEL_CONNECT:
  917. /* see that the request is completely sent */
  918. result = proxy_h2_progress_ingress(cf, data);
  919. if(!result)
  920. result = proxy_h2_progress_egress(cf, data);
  921. if(result && result != CURLE_AGAIN) {
  922. h2_tunnel_go_state(cf, ts, H2_TUNNEL_FAILED, data);
  923. break;
  924. }
  925. if(ts->has_final_response) {
  926. h2_tunnel_go_state(cf, ts, H2_TUNNEL_RESPONSE, data);
  927. }
  928. else {
  929. result = CURLE_OK;
  930. goto out;
  931. }
  932. FALLTHROUGH();
  933. case H2_TUNNEL_RESPONSE:
  934. DEBUGASSERT(ts->has_final_response);
  935. result = inspect_response(cf, data, ts);
  936. if(result)
  937. goto out;
  938. break;
  939. case H2_TUNNEL_ESTABLISHED:
  940. return CURLE_OK;
  941. case H2_TUNNEL_FAILED:
  942. return CURLE_RECV_ERROR;
  943. default:
  944. break;
  945. }
  946. } while(ts->state == H2_TUNNEL_INIT);
  947. out:
  948. if(result || ctx->tunnel.closed)
  949. h2_tunnel_go_state(cf, ts, H2_TUNNEL_FAILED, data);
  950. return result;
  951. }
  952. static CURLcode cf_h2_proxy_connect(struct Curl_cfilter *cf,
  953. struct Curl_easy *data,
  954. bool blocking, bool *done)
  955. {
  956. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  957. CURLcode result = CURLE_OK;
  958. struct cf_call_data save;
  959. timediff_t check;
  960. struct tunnel_stream *ts = &ctx->tunnel;
  961. if(cf->connected) {
  962. *done = TRUE;
  963. return CURLE_OK;
  964. }
  965. /* Connect the lower filters first */
  966. if(!cf->next->connected) {
  967. result = Curl_conn_cf_connect(cf->next, data, blocking, done);
  968. if(result || !*done)
  969. return result;
  970. }
  971. *done = FALSE;
  972. CF_DATA_SAVE(save, cf, data);
  973. if(!ctx->h2) {
  974. result = cf_h2_proxy_ctx_init(cf, data);
  975. if(result)
  976. goto out;
  977. }
  978. DEBUGASSERT(ts->authority);
  979. check = Curl_timeleft(data, NULL, TRUE);
  980. if(check <= 0) {
  981. failf(data, "Proxy CONNECT aborted due to timeout");
  982. result = CURLE_OPERATION_TIMEDOUT;
  983. goto out;
  984. }
  985. /* for the secondary socket (FTP), use the "connect to host"
  986. * but ignore the "connect to port" (use the secondary port)
  987. */
  988. result = H2_CONNECT(cf, data, ts);
  989. out:
  990. *done = (result == CURLE_OK) && (ts->state == H2_TUNNEL_ESTABLISHED);
  991. if(*done) {
  992. cf->connected = TRUE;
  993. /* The real request will follow the CONNECT, reset request partially */
  994. Curl_req_soft_reset(&data->req, data);
  995. Curl_client_reset(data);
  996. }
  997. CF_DATA_RESTORE(cf, save);
  998. return result;
  999. }
  1000. static void cf_h2_proxy_close(struct Curl_cfilter *cf, struct Curl_easy *data)
  1001. {
  1002. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  1003. if(ctx) {
  1004. struct cf_call_data save;
  1005. CF_DATA_SAVE(save, cf, data);
  1006. cf_h2_proxy_ctx_clear(ctx);
  1007. CF_DATA_RESTORE(cf, save);
  1008. }
  1009. if(cf->next)
  1010. cf->next->cft->do_close(cf->next, data);
  1011. }
  1012. static void cf_h2_proxy_destroy(struct Curl_cfilter *cf,
  1013. struct Curl_easy *data)
  1014. {
  1015. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  1016. (void)data;
  1017. if(ctx) {
  1018. cf_h2_proxy_ctx_free(ctx);
  1019. cf->ctx = NULL;
  1020. }
  1021. }
  1022. static CURLcode cf_h2_proxy_shutdown(struct Curl_cfilter *cf,
  1023. struct Curl_easy *data, bool *done)
  1024. {
  1025. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  1026. struct cf_call_data save;
  1027. CURLcode result;
  1028. int rv;
  1029. if(!cf->connected || !ctx->h2 || cf->shutdown || ctx->conn_closed) {
  1030. *done = TRUE;
  1031. return CURLE_OK;
  1032. }
  1033. CF_DATA_SAVE(save, cf, data);
  1034. if(!ctx->sent_goaway) {
  1035. rv = nghttp2_submit_goaway(ctx->h2, NGHTTP2_FLAG_NONE,
  1036. 0, 0,
  1037. (const uint8_t *)"shutown", sizeof("shutown"));
  1038. if(rv) {
  1039. failf(data, "nghttp2_submit_goaway() failed: %s(%d)",
  1040. nghttp2_strerror(rv), rv);
  1041. result = CURLE_SEND_ERROR;
  1042. goto out;
  1043. }
  1044. ctx->sent_goaway = TRUE;
  1045. }
  1046. /* GOAWAY submitted, process egress and ingress until nghttp2 is done. */
  1047. result = CURLE_OK;
  1048. if(nghttp2_session_want_write(ctx->h2))
  1049. result = proxy_h2_progress_egress(cf, data);
  1050. if(!result && nghttp2_session_want_read(ctx->h2))
  1051. result = proxy_h2_progress_ingress(cf, data);
  1052. *done = (ctx->conn_closed ||
  1053. (!result && !nghttp2_session_want_write(ctx->h2) &&
  1054. !nghttp2_session_want_read(ctx->h2)));
  1055. out:
  1056. CF_DATA_RESTORE(cf, save);
  1057. cf->shutdown = (result || *done);
  1058. return result;
  1059. }
  1060. static bool cf_h2_proxy_data_pending(struct Curl_cfilter *cf,
  1061. const struct Curl_easy *data)
  1062. {
  1063. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  1064. if((ctx && !Curl_bufq_is_empty(&ctx->inbufq)) ||
  1065. (ctx && ctx->tunnel.state == H2_TUNNEL_ESTABLISHED &&
  1066. !Curl_bufq_is_empty(&ctx->tunnel.recvbuf)))
  1067. return TRUE;
  1068. return cf->next? cf->next->cft->has_data_pending(cf->next, data) : FALSE;
  1069. }
  1070. static void cf_h2_proxy_adjust_pollset(struct Curl_cfilter *cf,
  1071. struct Curl_easy *data,
  1072. struct easy_pollset *ps)
  1073. {
  1074. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  1075. struct cf_call_data save;
  1076. curl_socket_t sock = Curl_conn_cf_get_socket(cf, data);
  1077. bool want_recv, want_send;
  1078. if(!cf->connected && ctx->h2) {
  1079. want_send = nghttp2_session_want_write(ctx->h2);
  1080. want_recv = nghttp2_session_want_read(ctx->h2);
  1081. }
  1082. else
  1083. Curl_pollset_check(data, ps, sock, &want_recv, &want_send);
  1084. if(ctx->h2 && (want_recv || want_send)) {
  1085. bool c_exhaust, s_exhaust;
  1086. CF_DATA_SAVE(save, cf, data);
  1087. c_exhaust = !nghttp2_session_get_remote_window_size(ctx->h2);
  1088. s_exhaust = ctx->tunnel.stream_id >= 0 &&
  1089. !nghttp2_session_get_stream_remote_window_size(
  1090. ctx->h2, ctx->tunnel.stream_id);
  1091. want_recv = (want_recv || c_exhaust || s_exhaust);
  1092. want_send = (!s_exhaust && want_send) ||
  1093. (!c_exhaust && nghttp2_session_want_write(ctx->h2));
  1094. Curl_pollset_set(data, ps, sock, want_recv, want_send);
  1095. CF_DATA_RESTORE(cf, save);
  1096. }
  1097. else if(ctx->sent_goaway && !cf->shutdown) {
  1098. /* shutdown in progress */
  1099. CF_DATA_SAVE(save, cf, data);
  1100. want_send = nghttp2_session_want_write(ctx->h2);
  1101. want_recv = nghttp2_session_want_read(ctx->h2);
  1102. Curl_pollset_set(data, ps, sock, want_recv, want_send);
  1103. CF_DATA_RESTORE(cf, save);
  1104. }
  1105. }
  1106. static ssize_t h2_handle_tunnel_close(struct Curl_cfilter *cf,
  1107. struct Curl_easy *data,
  1108. CURLcode *err)
  1109. {
  1110. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  1111. ssize_t rv = 0;
  1112. if(ctx->tunnel.error == NGHTTP2_REFUSED_STREAM) {
  1113. CURL_TRC_CF(data, cf, "[%d] REFUSED_STREAM, try again on a new "
  1114. "connection", ctx->tunnel.stream_id);
  1115. connclose(cf->conn, "REFUSED_STREAM"); /* do not use this anymore */
  1116. *err = CURLE_RECV_ERROR; /* trigger Curl_retry_request() later */
  1117. return -1;
  1118. }
  1119. else if(ctx->tunnel.error != NGHTTP2_NO_ERROR) {
  1120. failf(data, "HTTP/2 stream %u was not closed cleanly: %s (err %u)",
  1121. ctx->tunnel.stream_id, nghttp2_http2_strerror(ctx->tunnel.error),
  1122. ctx->tunnel.error);
  1123. *err = CURLE_HTTP2_STREAM;
  1124. return -1;
  1125. }
  1126. else if(ctx->tunnel.reset) {
  1127. failf(data, "HTTP/2 stream %u was reset", ctx->tunnel.stream_id);
  1128. *err = CURLE_RECV_ERROR;
  1129. return -1;
  1130. }
  1131. *err = CURLE_OK;
  1132. rv = 0;
  1133. CURL_TRC_CF(data, cf, "[%d] handle_tunnel_close -> %zd, %d",
  1134. ctx->tunnel.stream_id, rv, *err);
  1135. return rv;
  1136. }
  1137. static ssize_t tunnel_recv(struct Curl_cfilter *cf, struct Curl_easy *data,
  1138. char *buf, size_t len, CURLcode *err)
  1139. {
  1140. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  1141. ssize_t nread = -1;
  1142. *err = CURLE_AGAIN;
  1143. if(!Curl_bufq_is_empty(&ctx->tunnel.recvbuf)) {
  1144. nread = Curl_bufq_read(&ctx->tunnel.recvbuf,
  1145. (unsigned char *)buf, len, err);
  1146. if(nread < 0)
  1147. goto out;
  1148. DEBUGASSERT(nread > 0);
  1149. }
  1150. if(nread < 0) {
  1151. if(ctx->tunnel.closed) {
  1152. nread = h2_handle_tunnel_close(cf, data, err);
  1153. }
  1154. else if(ctx->tunnel.reset ||
  1155. (ctx->conn_closed && Curl_bufq_is_empty(&ctx->inbufq)) ||
  1156. (ctx->rcvd_goaway &&
  1157. ctx->last_stream_id < ctx->tunnel.stream_id)) {
  1158. *err = CURLE_RECV_ERROR;
  1159. nread = -1;
  1160. }
  1161. }
  1162. else if(nread == 0) {
  1163. *err = CURLE_AGAIN;
  1164. nread = -1;
  1165. }
  1166. out:
  1167. CURL_TRC_CF(data, cf, "[%d] tunnel_recv(len=%zu) -> %zd, %d",
  1168. ctx->tunnel.stream_id, len, nread, *err);
  1169. return nread;
  1170. }
  1171. static ssize_t cf_h2_proxy_recv(struct Curl_cfilter *cf,
  1172. struct Curl_easy *data,
  1173. char *buf, size_t len, CURLcode *err)
  1174. {
  1175. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  1176. ssize_t nread = -1;
  1177. struct cf_call_data save;
  1178. CURLcode result;
  1179. if(ctx->tunnel.state != H2_TUNNEL_ESTABLISHED) {
  1180. *err = CURLE_RECV_ERROR;
  1181. return -1;
  1182. }
  1183. CF_DATA_SAVE(save, cf, data);
  1184. if(Curl_bufq_is_empty(&ctx->tunnel.recvbuf)) {
  1185. *err = proxy_h2_progress_ingress(cf, data);
  1186. if(*err)
  1187. goto out;
  1188. }
  1189. nread = tunnel_recv(cf, data, buf, len, err);
  1190. if(nread > 0) {
  1191. CURL_TRC_CF(data, cf, "[%d] increase window by %zd",
  1192. ctx->tunnel.stream_id, nread);
  1193. nghttp2_session_consume(ctx->h2, ctx->tunnel.stream_id, (size_t)nread);
  1194. }
  1195. result = proxy_h2_progress_egress(cf, data);
  1196. if(result == CURLE_AGAIN) {
  1197. /* pending data to send, need to be called again. Ideally, we would
  1198. * monitor the socket for POLLOUT, but we might not be in SENDING
  1199. * transfer state any longer and are unable to make this happen.
  1200. */
  1201. CURL_TRC_CF(data, cf, "[%d] egress blocked, DRAIN",
  1202. ctx->tunnel.stream_id);
  1203. drain_tunnel(cf, data, &ctx->tunnel);
  1204. }
  1205. else if(result) {
  1206. *err = result;
  1207. nread = -1;
  1208. }
  1209. out:
  1210. if(!Curl_bufq_is_empty(&ctx->tunnel.recvbuf) &&
  1211. (nread >= 0 || *err == CURLE_AGAIN)) {
  1212. /* data pending and no fatal error to report. Need to trigger
  1213. * draining to avoid stalling when no socket events happen. */
  1214. drain_tunnel(cf, data, &ctx->tunnel);
  1215. }
  1216. CURL_TRC_CF(data, cf, "[%d] cf_recv(len=%zu) -> %zd %d",
  1217. ctx->tunnel.stream_id, len, nread, *err);
  1218. CF_DATA_RESTORE(cf, save);
  1219. return nread;
  1220. }
  1221. static ssize_t cf_h2_proxy_send(struct Curl_cfilter *cf,
  1222. struct Curl_easy *data,
  1223. const void *buf, size_t len, CURLcode *err)
  1224. {
  1225. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  1226. struct cf_call_data save;
  1227. int rv;
  1228. ssize_t nwritten;
  1229. CURLcode result;
  1230. int blocked = 0;
  1231. if(ctx->tunnel.state != H2_TUNNEL_ESTABLISHED) {
  1232. *err = CURLE_SEND_ERROR;
  1233. return -1;
  1234. }
  1235. CF_DATA_SAVE(save, cf, data);
  1236. if(ctx->tunnel.closed) {
  1237. nwritten = -1;
  1238. *err = CURLE_SEND_ERROR;
  1239. goto out;
  1240. }
  1241. else if(ctx->tunnel.upload_blocked_len) {
  1242. /* the data in `buf` has already been submitted or added to the
  1243. * buffers, but have been EAGAINed on the last invocation. */
  1244. DEBUGASSERT(len >= ctx->tunnel.upload_blocked_len);
  1245. if(len < ctx->tunnel.upload_blocked_len) {
  1246. /* Did we get called again with a smaller `len`? This should not
  1247. * happen. We are not prepared to handle that. */
  1248. failf(data, "HTTP/2 proxy, send again with decreased length");
  1249. *err = CURLE_HTTP2;
  1250. nwritten = -1;
  1251. goto out;
  1252. }
  1253. nwritten = (ssize_t)ctx->tunnel.upload_blocked_len;
  1254. ctx->tunnel.upload_blocked_len = 0;
  1255. *err = CURLE_OK;
  1256. }
  1257. else {
  1258. nwritten = Curl_bufq_write(&ctx->tunnel.sendbuf, buf, len, err);
  1259. if(nwritten < 0) {
  1260. if(*err != CURLE_AGAIN)
  1261. goto out;
  1262. nwritten = 0;
  1263. }
  1264. }
  1265. if(!Curl_bufq_is_empty(&ctx->tunnel.sendbuf)) {
  1266. /* req body data is buffered, resume the potentially suspended stream */
  1267. rv = nghttp2_session_resume_data(ctx->h2, ctx->tunnel.stream_id);
  1268. if(nghttp2_is_fatal(rv)) {
  1269. *err = CURLE_SEND_ERROR;
  1270. nwritten = -1;
  1271. goto out;
  1272. }
  1273. }
  1274. result = proxy_h2_progress_ingress(cf, data);
  1275. if(result) {
  1276. *err = result;
  1277. nwritten = -1;
  1278. goto out;
  1279. }
  1280. /* Call the nghttp2 send loop and flush to write ALL buffered data,
  1281. * headers and/or request body completely out to the network */
  1282. result = proxy_h2_progress_egress(cf, data);
  1283. if(result == CURLE_AGAIN) {
  1284. blocked = 1;
  1285. }
  1286. else if(result) {
  1287. *err = result;
  1288. nwritten = -1;
  1289. goto out;
  1290. }
  1291. else if(!Curl_bufq_is_empty(&ctx->tunnel.sendbuf)) {
  1292. /* although we wrote everything that nghttp2 wants to send now,
  1293. * there is data left in our stream send buffer unwritten. This may
  1294. * be due to the stream's HTTP/2 flow window being exhausted. */
  1295. blocked = 1;
  1296. }
  1297. if(blocked) {
  1298. /* Unable to send all data, due to connection blocked or H2 window
  1299. * exhaustion. Data is left in our stream buffer, or nghttp2's internal
  1300. * frame buffer or our network out buffer. */
  1301. size_t rwin = (size_t)nghttp2_session_get_stream_remote_window_size(
  1302. ctx->h2, ctx->tunnel.stream_id);
  1303. if(rwin == 0) {
  1304. /* H2 flow window exhaustion.
  1305. * FIXME: there is no way to HOLD all transfers that use this
  1306. * proxy connection AND to UNHOLD all of them again when the
  1307. * window increases.
  1308. * We *could* iterate over all data on this conn maybe? */
  1309. CURL_TRC_CF(data, cf, "[%d] remote flow "
  1310. "window is exhausted", ctx->tunnel.stream_id);
  1311. }
  1312. /* Whatever the cause, we need to return CURL_EAGAIN for this call.
  1313. * We have unwritten state that needs us being invoked again and EAGAIN
  1314. * is the only way to ensure that. */
  1315. ctx->tunnel.upload_blocked_len = nwritten;
  1316. CURL_TRC_CF(data, cf, "[%d] cf_send(len=%zu) BLOCK: win %u/%zu "
  1317. "blocked_len=%zu",
  1318. ctx->tunnel.stream_id, len,
  1319. nghttp2_session_get_remote_window_size(ctx->h2), rwin,
  1320. nwritten);
  1321. drain_tunnel(cf, data, &ctx->tunnel);
  1322. *err = CURLE_AGAIN;
  1323. nwritten = -1;
  1324. goto out;
  1325. }
  1326. else if(proxy_h2_should_close_session(ctx)) {
  1327. /* nghttp2 thinks this session is done. If the stream has not been
  1328. * closed, this is an error state for out transfer */
  1329. if(ctx->tunnel.closed) {
  1330. *err = CURLE_SEND_ERROR;
  1331. nwritten = -1;
  1332. }
  1333. else {
  1334. CURL_TRC_CF(data, cf, "[0] send: nothing to do in this session");
  1335. *err = CURLE_HTTP2;
  1336. nwritten = -1;
  1337. }
  1338. }
  1339. out:
  1340. if(!Curl_bufq_is_empty(&ctx->tunnel.recvbuf) &&
  1341. (nwritten >= 0 || *err == CURLE_AGAIN)) {
  1342. /* data pending and no fatal error to report. Need to trigger
  1343. * draining to avoid stalling when no socket events happen. */
  1344. drain_tunnel(cf, data, &ctx->tunnel);
  1345. }
  1346. CURL_TRC_CF(data, cf, "[%d] cf_send(len=%zu) -> %zd, %d, "
  1347. "h2 windows %d-%d (stream-conn), buffers %zu-%zu (stream-conn)",
  1348. ctx->tunnel.stream_id, len, nwritten, *err,
  1349. nghttp2_session_get_stream_remote_window_size(
  1350. ctx->h2, ctx->tunnel.stream_id),
  1351. nghttp2_session_get_remote_window_size(ctx->h2),
  1352. Curl_bufq_len(&ctx->tunnel.sendbuf),
  1353. Curl_bufq_len(&ctx->outbufq));
  1354. CF_DATA_RESTORE(cf, save);
  1355. return nwritten;
  1356. }
  1357. static bool proxy_h2_connisalive(struct Curl_cfilter *cf,
  1358. struct Curl_easy *data,
  1359. bool *input_pending)
  1360. {
  1361. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  1362. bool alive = TRUE;
  1363. *input_pending = FALSE;
  1364. if(!cf->next || !cf->next->cft->is_alive(cf->next, data, input_pending))
  1365. return FALSE;
  1366. if(*input_pending) {
  1367. /* This happens before we have sent off a request and the connection is
  1368. not in use by any other transfer, there should not be any data here,
  1369. only "protocol frames" */
  1370. CURLcode result;
  1371. ssize_t nread = -1;
  1372. *input_pending = FALSE;
  1373. nread = Curl_bufq_slurp(&ctx->inbufq, proxy_nw_in_reader, cf, &result);
  1374. if(nread != -1) {
  1375. if(proxy_h2_process_pending_input(cf, data, &result) < 0)
  1376. /* immediate error, considered dead */
  1377. alive = FALSE;
  1378. else {
  1379. alive = !proxy_h2_should_close_session(ctx);
  1380. }
  1381. }
  1382. else if(result != CURLE_AGAIN) {
  1383. /* the read failed so let's say this is dead anyway */
  1384. alive = FALSE;
  1385. }
  1386. }
  1387. return alive;
  1388. }
  1389. static bool cf_h2_proxy_is_alive(struct Curl_cfilter *cf,
  1390. struct Curl_easy *data,
  1391. bool *input_pending)
  1392. {
  1393. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  1394. CURLcode result;
  1395. struct cf_call_data save;
  1396. CF_DATA_SAVE(save, cf, data);
  1397. result = (ctx && ctx->h2 && proxy_h2_connisalive(cf, data, input_pending));
  1398. CURL_TRC_CF(data, cf, "[0] conn alive -> %d, input_pending=%d",
  1399. result, *input_pending);
  1400. CF_DATA_RESTORE(cf, save);
  1401. return result;
  1402. }
  1403. struct Curl_cftype Curl_cft_h2_proxy = {
  1404. "H2-PROXY",
  1405. CF_TYPE_IP_CONNECT|CF_TYPE_PROXY,
  1406. CURL_LOG_LVL_NONE,
  1407. cf_h2_proxy_destroy,
  1408. cf_h2_proxy_connect,
  1409. cf_h2_proxy_close,
  1410. cf_h2_proxy_shutdown,
  1411. Curl_cf_http_proxy_get_host,
  1412. cf_h2_proxy_adjust_pollset,
  1413. cf_h2_proxy_data_pending,
  1414. cf_h2_proxy_send,
  1415. cf_h2_proxy_recv,
  1416. Curl_cf_def_cntrl,
  1417. cf_h2_proxy_is_alive,
  1418. Curl_cf_def_conn_keep_alive,
  1419. Curl_cf_def_query,
  1420. };
  1421. CURLcode Curl_cf_h2_proxy_insert_after(struct Curl_cfilter *cf,
  1422. struct Curl_easy *data)
  1423. {
  1424. struct Curl_cfilter *cf_h2_proxy = NULL;
  1425. struct cf_h2_proxy_ctx *ctx;
  1426. CURLcode result = CURLE_OUT_OF_MEMORY;
  1427. (void)data;
  1428. ctx = calloc(1, sizeof(*ctx));
  1429. if(!ctx)
  1430. goto out;
  1431. result = Curl_cf_create(&cf_h2_proxy, &Curl_cft_h2_proxy, ctx);
  1432. if(result)
  1433. goto out;
  1434. Curl_conn_cf_insert_after(cf, cf_h2_proxy);
  1435. result = CURLE_OK;
  1436. out:
  1437. if(result)
  1438. cf_h2_proxy_ctx_free(ctx);
  1439. return result;
  1440. }
  1441. #endif /* defined(USE_NGHTTP2) && !defined(CURL_DISABLE_PROXY) */