cf-h2-proxy.c 44 KB

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