cf-h2-proxy.c 48 KB

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