master
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"
26#if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_PROXY) && \
27 defined(USE_NGHTTP2)
29#include <nghttp2/nghttp2.h>
31#include "urldata.h"
32#include "url.h"
33#include "cfilters.h"
34#include "connect.h"
35#include "curl_trc.h"
36#include "bufq.h"
37#include "curlx/dynbuf.h"
38#include "dynhds.h"
39#include "http2.h"
40#include "http_proxy.h"
41#include "multiif.h"
42#include "ngh2_shim.h"
43#include "sendf.h"
44#include "select.h"
45#include "cf-h2-proxy.h"
47#define PROXY_H2_CHUNK_SIZE (16 * 1024)
49#define PROXY_HTTP2_HUGE_WINDOW_SIZE (100 * 1024 * 1024)
50#define H2_TUNNEL_WINDOW_SIZE (10 * 1024 * 1024)
52#define PROXY_H2_NW_RECV_CHUNKS (H2_TUNNEL_WINDOW_SIZE / PROXY_H2_CHUNK_SIZE)
53#define PROXY_H2_NW_SEND_CHUNKS 1
55#define H2_TUNNEL_RECV_CHUNKS (H2_TUNNEL_WINDOW_SIZE / PROXY_H2_CHUNK_SIZE)
56#define H2_TUNNEL_SEND_CHUNKS ((128 * 1024) / PROXY_H2_CHUNK_SIZE)
59typedef enum {
60 H2_TUNNEL_INIT, /* init/default/no tunnel state */
61 H2_TUNNEL_CONNECT, /* CONNECT request is being send */
62 H2_TUNNEL_RESPONSE, /* CONNECT response received completely */
63 H2_TUNNEL_ESTABLISHED,
64 H2_TUNNEL_FAILED
65} h2_tunnel_state;
67struct tunnel_stream {
68 struct http_resp *resp;
69 struct bufq recvbuf;
70 struct bufq sendbuf;
71 char *authority;
72 int32_t stream_id;
73 uint32_t error;
74 h2_tunnel_state state;
75 BIT(has_final_response);
76 BIT(closed);
77 BIT(reset);
78};
80static CURLcode tunnel_stream_init(struct tunnel_stream *ts,
81 struct Curl_peer *dest)
82{
83 ts->state = H2_TUNNEL_INIT;
84 ts->stream_id = -1;
85 Curl_bufq_init2(&ts->recvbuf, PROXY_H2_CHUNK_SIZE, H2_TUNNEL_RECV_CHUNKS,
86 BUFQ_OPT_SOFT_LIMIT);
87 Curl_bufq_init(&ts->sendbuf, PROXY_H2_CHUNK_SIZE, H2_TUNNEL_SEND_CHUNKS);
89 /* host:port with IPv6 support */
90 ts->authority = curl_maprintf("%s%s%s:%u", dest->ipv6 ? "[" : "",
91 dest->hostname,
92 dest->ipv6 ? "]" : "",
93 dest->port);
94 if(!ts->authority)
95 return CURLE_OUT_OF_MEMORY;
97 return CURLE_OK;
98}
100static void tunnel_stream_reset(struct tunnel_stream *ts)
101{
102 Curl_http_resp_free(ts->resp);
103 ts->resp = NULL;
104 Curl_bufq_reset(&ts->recvbuf);
105 Curl_bufq_reset(&ts->sendbuf);
106 ts->stream_id = -1;
107 ts->error = 0;
108 ts->has_final_response = FALSE;
109 ts->closed = FALSE;
110 ts->reset = FALSE;
111 ts->state = H2_TUNNEL_INIT;
112}
114static void tunnel_stream_clear(struct tunnel_stream *ts)
115{
116 Curl_http_resp_free(ts->resp);
117 Curl_bufq_free(&ts->recvbuf);
118 Curl_bufq_free(&ts->sendbuf);
119 curlx_safefree(ts->authority);
120 memset(ts, 0, sizeof(*ts));
121 ts->state = H2_TUNNEL_INIT;
122}
124static void h2_tunnel_go_state(struct Curl_cfilter *cf,
125 struct tunnel_stream *ts,
126 h2_tunnel_state new_state,
127 struct Curl_easy *data,
128 bool udp_tunnel)
129{
130 (void)cf;
131 (void)udp_tunnel;
133 if(ts->state == new_state)
134 return;
135 /* leaving this one */
136 switch(ts->state) {
137 case H2_TUNNEL_CONNECT:
138 data->req.ignorebody = FALSE;
139 break;
140 default:
141 break;
142 }
143 /* entering this one */
144 switch(new_state) {
145 case H2_TUNNEL_INIT:
146 CURL_TRC_CF(data, cf, "[%d] new tunnel state 'init'", ts->stream_id);
147 tunnel_stream_reset(ts);
148 break;
150 case H2_TUNNEL_CONNECT:
151 CURL_TRC_CF(data, cf, "[%d] new tunnel state 'connect'", ts->stream_id);
152 ts->state = H2_TUNNEL_CONNECT;
153 break;
155 case H2_TUNNEL_RESPONSE:
156 CURL_TRC_CF(data, cf, "[%d] new tunnel state 'response'", ts->stream_id);
157 ts->state = H2_TUNNEL_RESPONSE;
158 break;
160 case H2_TUNNEL_ESTABLISHED:
161 CURL_TRC_CF(data, cf, "[%d] new tunnel state 'established'",
162 ts->stream_id);
163 infof(data, "CONNECT%s phase completed for HTTP/2 proxy",
164 udp_tunnel ? "-UDP" : "");
165 data->state.authproxy.done = TRUE;
166 data->state.authproxy.multipass = FALSE;
167 FALLTHROUGH();
168 case H2_TUNNEL_FAILED:
169 if(new_state == H2_TUNNEL_FAILED)
170 CURL_TRC_CF(data, cf, "[%d] new tunnel state 'failed'", ts->stream_id);
171 ts->state = new_state;
172 /* If a proxy-authorization header was used for the proxy, then we should
173 make sure that it is not accidentally used for the document request
174 after we have connected. Let's thus free and clear it here. */
175 curlx_safefree(data->req.hd_proxy_auth);
176 break;
177 }
178}
180struct cf_h2_proxy_ctx {
181 struct curl_ngh2 ngh2;
182 /* The easy handle used in the current filter call, cleared at return */
183 struct cf_call_data call_data;
185 struct bufq inbufq; /* network receive buffer */
186 struct bufq outbufq; /* network send buffer */
188 struct Curl_peer *peer; /* proxy we talk to */
189 struct Curl_peer *dest; /* where to tunnel to */
190 struct tunnel_stream tunnel; /* our tunnel CONNECT stream */
191 int32_t goaway_error;
192 int32_t remote_max_sid;
193 BIT(conn_closed);
194 BIT(rcvd_goaway);
195 BIT(sent_goaway);
196 BIT(nw_out_blocked);
197 BIT(udp_tunnel);
198};
200/* How to access `call_data` from a cf_h2 filter */
201#undef CF_CTX_CALL_DATA
202#define CF_CTX_CALL_DATA(cf) ((struct cf_h2_proxy_ctx *)(cf)->ctx)->call_data
204static void cf_h2_proxy_ctx_clear(struct cf_h2_proxy_ctx *ctx)
205{
206 struct cf_call_data save = ctx->call_data;
208 Curl_ngh2_del(&ctx->ngh2);
209 Curl_bufq_free(&ctx->inbufq);
210 Curl_bufq_free(&ctx->outbufq);
211 Curl_peer_unlink(&ctx->peer);
212 Curl_peer_unlink(&ctx->dest);
213 tunnel_stream_clear(&ctx->tunnel);
214 memset(ctx, 0, sizeof(*ctx));
215 ctx->call_data = save;
216}
218static void cf_h2_proxy_ctx_free(struct cf_h2_proxy_ctx *ctx)
219{
220 if(ctx) {
221 cf_h2_proxy_ctx_clear(ctx);
222 curlx_free(ctx);
223 }
224}
226static void drain_tunnel(struct Curl_cfilter *cf,
227 struct Curl_easy *data,
228 struct tunnel_stream *tunnel)
229{
230 struct cf_h2_proxy_ctx *ctx = cf->ctx;
231 (void)cf;
232 if(!tunnel->closed && !tunnel->reset &&
233 (!Curl_bufq_is_empty(&ctx->tunnel.sendbuf) ||
234 !Curl_bufq_is_empty(&ctx->tunnel.recvbuf)))
235 Curl_multi_mark_dirty(data);
236}
238static CURLcode proxy_h2_nw_out_writer(void *writer_ctx,
239 const uint8_t *buf, size_t buflen,
240 size_t *pnwritten)
241{
242 struct Curl_cfilter *cf = writer_ctx;
243 *pnwritten = 0;
244 if(cf) {
245 struct Curl_easy *data = CF_DATA_CURRENT(cf);
246 CURLcode result;
247 result = Curl_conn_cf_send(cf->next, data, buf, buflen, FALSE, pnwritten);
248 CURL_TRC_CF(data, cf, "[0] nw_out_writer(len=%zu) -> %d, %zu",
249 buflen, (int)result, *pnwritten);
250 return result;
251 }
252 return CURLE_FAILED_INIT;
253}
255static int proxy_h2_client_new(struct Curl_cfilter *cf,
256 nghttp2_session_callbacks *cbs)
257{
258 struct cf_h2_proxy_ctx *ctx = cf->ctx;
259 nghttp2_option *o;
260 nghttp2_mem mem = { NULL, Curl_nghttp2_malloc, Curl_nghttp2_free,
261 Curl_nghttp2_calloc, Curl_nghttp2_realloc };
263 int rc = nghttp2_option_new(&o);
264 if(rc)
265 return rc;
266 /* We handle window updates ourself to enforce buffer limits */
267 nghttp2_option_set_no_auto_window_update(o, 1);
268#if NGHTTP2_VERSION_NUM >= 0x013200 /* with 1.50.0 */
269 /* turn off RFC 9113 leading and trailing white spaces validation against
270 HTTP field value. */
271 nghttp2_option_set_no_rfc9113_leading_and_trailing_ws_validation(o, 1);
272#endif
273 rc = Curl_ngh2_session_client_new3(&ctx->ngh2, cbs, cf, o, &mem);
274 nghttp2_option_del(o);
275 return rc;
276}
278static int proxy_h2_should_close_session(struct cf_h2_proxy_ctx *ctx)
279{
280 return !Curl_ngh2_valid(&ctx->ngh2) ||
281 (!Curl_ngh2_session_want_read(&ctx->ngh2) &&
282 !Curl_ngh2_session_want_write(&ctx->ngh2));
283}
285static CURLcode proxy_h2_nw_out_flush(struct Curl_cfilter *cf,
286 struct Curl_easy *data)
287{
288 struct cf_h2_proxy_ctx *ctx = cf->ctx;
289 size_t nwritten;
290 CURLcode result;
292 if(Curl_bufq_is_empty(&ctx->outbufq))
293 return CURLE_OK;
295 result = Curl_bufq_pass(&ctx->outbufq, proxy_h2_nw_out_writer, cf,
296 &nwritten);
297 if(result) {
298 if(result == CURLE_AGAIN) {
299 CURL_TRC_CF(data, cf, "[0] flush nw send buffer(%zu) -> EAGAIN",
300 Curl_bufq_len(&ctx->outbufq));
301 ctx->nw_out_blocked = 1;
302 }
303 return result;
304 }
305 CURL_TRC_CF(data, cf, "[0] nw send buffer flushed");
306 return Curl_bufq_is_empty(&ctx->outbufq) ? CURLE_OK : CURLE_AGAIN;
307}
309/*
310 * Processes pending input left in network input buffer.
311 * This function returns 0 if it succeeds, or -1 and error code will
312 * be assigned to *err.
313 */
314static CURLcode proxy_h2_process_pending_input(struct Curl_cfilter *cf,
315 struct Curl_easy *data)
316{
317 struct cf_h2_proxy_ctx *ctx = cf->ctx;
318 const unsigned char *buf;
319 size_t blen, nread;
320 ssize_t rv;
322 while(Curl_bufq_peek(&ctx->inbufq, &buf, &blen)) {
324 rv = Curl_ngh2_session_mem_recv(&ctx->ngh2, (const uint8_t *)buf, blen);
325 CURL_TRC_CF(data, cf, "[0] %zu bytes to nghttp2 -> %zd", blen, rv);
326 if(!curlx_sztouz(rv, &nread)) {
327 failf(data,
328 "process_pending_input: nghttp2_session_mem_recv() returned "
329 "%zd:%s", rv, nghttp2_strerror((int)rv));
330 return CURLE_RECV_ERROR;
331 }
332 else if(!nread) {
333 /* nghttp2 does not want to process more, but has no error. This
334 * probably cannot happen, but be safe. */
335 break;
336 }
337 Curl_bufq_skip(&ctx->inbufq, nread);
338 if(Curl_bufq_is_empty(&ctx->inbufq)) {
339 CURL_TRC_CF(data, cf, "[0] all data in connection buffer processed");
340 break;
341 }
342 else {
343 CURL_TRC_CF(data, cf, "[0] process_pending_input: %zu bytes left "
344 "in connection buffer", Curl_bufq_len(&ctx->inbufq));
345 }
346 }
347 return CURLE_OK;
348}
350static CURLcode proxy_h2_progress_ingress(struct Curl_cfilter *cf,
351 struct Curl_easy *data)
352{
353 struct cf_h2_proxy_ctx *ctx = cf->ctx;
354 CURLcode result = CURLE_OK;
355 size_t nread;
357 /* Process network input buffer first */
358 if(!Curl_bufq_is_empty(&ctx->inbufq)) {
359 CURL_TRC_CF(data, cf, "[0] process %zu bytes in connection buffer",
360 Curl_bufq_len(&ctx->inbufq));
361 result = proxy_h2_process_pending_input(cf, data);
362 if(result)
363 return result;
364 }
366 /* Receive data from the "lower" filters, e.g. network until
367 * it is time to stop or we have enough data for this stream */
368 while(!ctx->conn_closed && /* not closed the connection */
369 !ctx->tunnel.closed && /* nor the tunnel */
370 Curl_bufq_is_empty(&ctx->inbufq) && /* and we consumed our input */
371 !Curl_bufq_is_full(&ctx->tunnel.recvbuf)) {
373 result = Curl_cf_recv_bufq(cf->next, data, &ctx->inbufq, 0, &nread);
374 CURL_TRC_CF(data, cf, "[0] read %zu bytes nw data -> %d, %zu",
375 Curl_bufq_len(&ctx->inbufq), (int)result, nread);
376 if(result) {
377 if(result != CURLE_AGAIN) {
378 failf(data, "Failed receiving HTTP2 proxy data");
379 return result;
380 }
381 break;
382 }
383 else if(nread == 0) {
384 CURL_TRC_CF(data, cf, "server closed connection");
385 ctx->conn_closed = TRUE;
386 break;
387 }
389 result = proxy_h2_process_pending_input(cf, data);
390 if(result)
391 return result;
392 }
394 return CURLE_OK;
395}
397static CURLcode proxy_h2_progress_egress(struct Curl_cfilter *cf,
398 struct Curl_easy *data)
399{
400 struct cf_h2_proxy_ctx *ctx = cf->ctx;
401 int rv = 0;
403 ctx->nw_out_blocked = 0;
404 while(!rv && !ctx->nw_out_blocked &&
405 Curl_ngh2_session_want_write(&ctx->ngh2))
406 rv = Curl_ngh2_session_send(&ctx->ngh2);
408 if(Curl_ngh2_is_fatal(&ctx->ngh2, rv)) {
409 VERBOSE(rv = Curl_ngh2_fatal(&ctx->ngh2));
410 CURL_TRC_CF(data, cf, "[0] nghttp2_session_send error (%s)%d",
411 nghttp2_strerror(rv), rv);
412 return CURLE_SEND_ERROR;
413 }
414 return proxy_h2_nw_out_flush(cf, data);
415}
417static ssize_t on_session_send(nghttp2_session *h2,
418 const uint8_t *buf, size_t blen, int flags,
419 void *userp)
420{
421 struct Curl_cfilter *cf = userp;
422 struct cf_h2_proxy_ctx *ctx = cf->ctx;
423 struct Curl_easy *data = CF_DATA_CURRENT(cf);
424 size_t nwritten;
425 CURLcode result = CURLE_OK;
427 (void)h2;
428 (void)flags;
429 DEBUGASSERT(data);
431 result = Curl_bufq_write_pass(&ctx->outbufq, buf, blen,
432 proxy_h2_nw_out_writer, cf, &nwritten);
433 if(result) {
434 if(result == CURLE_AGAIN) {
435 ctx->nw_out_blocked = 1;
436 return NGHTTP2_ERR_WOULDBLOCK;
437 }
438 failf(data, "Failed sending HTTP2 data");
439 return NGHTTP2_ERR_CALLBACK_FAILURE;
440 }
442 if(!nwritten)
443 return NGHTTP2_ERR_WOULDBLOCK;
445 return (nwritten > SSIZE_MAX) ?
446 NGHTTP2_ERR_CALLBACK_FAILURE : (ssize_t)nwritten;
447}
449#ifdef CURLVERBOSE
450static int proxy_h2_on_frame_send(nghttp2_session *session,
451 const nghttp2_frame *frame,
452 void *userp)
453{
454 struct Curl_cfilter *cf = userp;
455 struct Curl_easy *data = CF_DATA_CURRENT(cf);
457 (void)session;
458 DEBUGASSERT(data);
459 if(Curl_trc_cf_is_verbose(cf, data)) {
460 char buffer[256];
461 int len;
462 len = Curl_nghttp2_fr_print(frame, buffer, sizeof(buffer) - 1);
463 buffer[len] = 0;
464 CURL_TRC_CF(data, cf, "[%d] -> %s", frame->hd.stream_id, buffer);
465 }
466 return 0;
467}
468#endif /* CURLVERBOSE */
470static int proxy_h2_on_frame_recv(nghttp2_session *session,
471 const nghttp2_frame *frame,
472 void *userp)
473{
474 struct Curl_cfilter *cf = userp;
475 struct cf_h2_proxy_ctx *ctx = cf->ctx;
476 struct Curl_easy *data = CF_DATA_CURRENT(cf);
477 int32_t stream_id = frame->hd.stream_id;
479 (void)session;
480 DEBUGASSERT(data);
481#ifdef CURLVERBOSE
482 if(Curl_trc_cf_is_verbose(cf, data)) {
483 char buffer[256];
484 int len;
485 len = Curl_nghttp2_fr_print(frame, buffer, sizeof(buffer) - 1);
486 buffer[len] = 0;
487 CURL_TRC_CF(data, cf, "[%d] <- %s", frame->hd.stream_id, buffer);
488 }
489#endif /* CURLVERBOSE */
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 /* Since the initial stream window is 64K, a request might be on HOLD,
497 * due to exhaustion. The (initial) SETTINGS may announce a much larger
498 * window and *assume* that we treat this like a WINDOW_UPDATE. Some
499 * servers send an explicit WINDOW_UPDATE, but not all seem to do that.
500 * To be safe, we UNHOLD a stream in order not to stall. */
501 if(CURL_REQ_WANT_SEND(data)) {
502 drain_tunnel(cf, data, &ctx->tunnel);
503 }
504 break;
505 case NGHTTP2_GOAWAY:
506 ctx->rcvd_goaway = TRUE;
507 ctx->remote_max_sid = frame->goaway.last_stream_id;
508 if(data) {
509 infof(data, "received GOAWAY, error=%u, last_stream=%d",
510 frame->goaway.error_code, ctx->remote_max_sid);
511 }
512 break;
513 default:
514 break;
515 }
516 return 0;
517 }
519 if(stream_id != ctx->tunnel.stream_id) {
520 CURL_TRC_CF(data, cf, "[%d] rcvd FRAME not for tunnel", stream_id);
521 return NGHTTP2_ERR_CALLBACK_FAILURE;
522 }
524 switch(frame->hd.type) {
525 case NGHTTP2_HEADERS:
526 /* nghttp2 guarantees that :status is received, and we store it to
527 stream->status_code. Fuzzing has proven this can still be reached
528 without status code having been set. */
529 if(!ctx->tunnel.resp)
530 return NGHTTP2_ERR_CALLBACK_FAILURE;
531 /* Only final status code signals the end of header */
532 CURL_TRC_CF(data, cf, "[%d] got http status: %d",
533 stream_id, ctx->tunnel.resp->status);
534 if(!ctx->tunnel.has_final_response && ctx->tunnel.resp->status / 100 != 1)
535 ctx->tunnel.has_final_response = TRUE;
536 break;
537 case NGHTTP2_WINDOW_UPDATE:
538 if(CURL_REQ_WANT_SEND(data)) {
539 drain_tunnel(cf, data, &ctx->tunnel);
540 }
541 break;
542 case NGHTTP2_RST_STREAM:
543 if(frame->rst_stream.error_code)
544 ctx->tunnel.reset = TRUE;
545 break;
546 default:
547 break;
548 }
549 return 0;
550}
552static int proxy_h2_on_header(nghttp2_session *session,
553 const nghttp2_frame *frame,
554 const uint8_t *name, size_t namelen,
555 const uint8_t *value, size_t valuelen,
556 uint8_t flags,
557 void *userp)
558{
559 struct Curl_cfilter *cf = userp;
560 struct cf_h2_proxy_ctx *ctx = cf->ctx;
561 struct Curl_easy *data = CF_DATA_CURRENT(cf);
562 int32_t stream_id = frame->hd.stream_id;
563 CURLcode result;
565 (void)flags;
566 (void)session;
567 DEBUGASSERT(stream_id); /* should never be a zero stream ID here */
568 if(stream_id != ctx->tunnel.stream_id) {
569 CURL_TRC_CF(data, cf, "[%d] header for non-tunnel stream: "
570 "%.*s: %.*s", stream_id,
571 (int)namelen, name, (int)valuelen, value);
572 return NGHTTP2_ERR_CALLBACK_FAILURE;
573 }
575 if(frame->hd.type == NGHTTP2_PUSH_PROMISE)
576 return NGHTTP2_ERR_CALLBACK_FAILURE;
578 if(ctx->tunnel.has_final_response) {
579 /* we do not do anything with trailers for tunnel streams */
580 return 0;
581 }
583 if(namelen == CURL_CSTRLEN(HTTP_PSEUDO_STATUS) &&
584 !memcmp(HTTP_PSEUDO_STATUS, name, namelen)) {
585 int32_t http_status;
586 struct http_resp *resp;
588 /* status: always comes first, we might get more than one response,
589 * discard previous, interim responses */
590 result = Curl_http_decode_status(&http_status,
591 (const char *)value, valuelen);
592 if(result)
593 return NGHTTP2_ERR_CALLBACK_FAILURE;
594 result = Curl_http_resp_make(&resp, http_status, NULL);
595 if(result)
596 return NGHTTP2_ERR_CALLBACK_FAILURE;
597 if(ctx->tunnel.resp)
598 Curl_http_resp_free(ctx->tunnel.resp);
599 ctx->tunnel.resp = resp;
600 CURL_TRC_CF(data, cf, "[%d] status: HTTP/2 %03d",
601 stream_id, ctx->tunnel.resp->status);
602 return 0;
603 }
605 if(!ctx->tunnel.resp)
606 return NGHTTP2_ERR_CALLBACK_FAILURE;
608 result = Curl_dynhds_add(&ctx->tunnel.resp->headers,
609 (const char *)name, namelen,
610 (const char *)value, valuelen);
611 if(result)
612 return NGHTTP2_ERR_CALLBACK_FAILURE;
614 CURL_TRC_CF(data, cf, "[%d] header: %.*s: %.*s",
615 stream_id, (int)namelen, name, (int)valuelen, value);
617 return 0; /* 0 is successful */
618}
620static ssize_t tunnel_send_callback(nghttp2_session *session,
621 int32_t stream_id,
622 uint8_t *buf, size_t length,
623 uint32_t *data_flags,
624 nghttp2_data_source *source,
625 void *userp)
626{
627 struct Curl_cfilter *cf = userp;
628 struct cf_h2_proxy_ctx *ctx = cf->ctx;
629 struct Curl_easy *data = CF_DATA_CURRENT(cf);
630 struct tunnel_stream *ts;
631 CURLcode result;
632 size_t nread;
634 (void)session;
635 (void)source;
636 (void)ctx;
638 if(!stream_id)
639 return NGHTTP2_ERR_INVALID_ARGUMENT;
641 ts = Curl_ngh2_session_get_stream_user_data(&ctx->ngh2, stream_id);
642 if(!ts)
643 return NGHTTP2_ERR_CALLBACK_FAILURE;
644 DEBUGASSERT(ts == &ctx->tunnel);
646 result = Curl_bufq_read(&ts->sendbuf, buf, length, &nread);
647 if(result) {
648 if(result != CURLE_AGAIN)
649 return NGHTTP2_ERR_CALLBACK_FAILURE;
650 return NGHTTP2_ERR_DEFERRED;
651 }
652 if(ts->closed && Curl_bufq_is_empty(&ts->sendbuf))
653 *data_flags = NGHTTP2_DATA_FLAG_EOF;
655 CURL_TRC_CF(data, cf, "[%d] tunnel_send_callback -> %zu",
656 ts->stream_id, nread);
657 return (nread > SSIZE_MAX) ?
658 NGHTTP2_ERR_CALLBACK_FAILURE : (ssize_t)nread;
659}
661static int tunnel_recv_callback(nghttp2_session *session, uint8_t flags,
662 int32_t stream_id,
663 const uint8_t *mem, size_t len, void *userp)
664{
665 struct Curl_cfilter *cf = userp;
666 struct cf_h2_proxy_ctx *ctx = cf->ctx;
667 size_t nwritten;
668 CURLcode result;
670 (void)flags;
671 (void)session;
672 DEBUGASSERT(stream_id); /* should never be a zero stream ID here */
674 if(stream_id != ctx->tunnel.stream_id)
675 return NGHTTP2_ERR_CALLBACK_FAILURE;
677 result = Curl_bufq_write(&ctx->tunnel.recvbuf, mem, len, &nwritten);
678 if(result) {
679 if(result != CURLE_AGAIN)
680 return NGHTTP2_ERR_CALLBACK_FAILURE;
681#ifdef DEBUGBUILD
682 nwritten = 0;
683#endif
684 }
685 /* tunnel.recbuf has soft limit, any success MUST add all data */
686 DEBUGASSERT(nwritten == len);
687 return 0;
688}
690static int proxy_h2_on_stream_close(nghttp2_session *session,
691 int32_t stream_id,
692 uint32_t error_code, void *userp)
693{
694 struct Curl_cfilter *cf = userp;
695 struct cf_h2_proxy_ctx *ctx = cf->ctx;
696 struct Curl_easy *data = CF_DATA_CURRENT(cf);
698 (void)session;
700 if(stream_id != ctx->tunnel.stream_id)
701 return 0;
703 CURL_TRC_CF(data, cf, "[%d] proxy_h2_on_stream_close, %s (err %u)",
704 stream_id, nghttp2_http2_strerror(error_code), error_code);
705 ctx->tunnel.closed = TRUE;
706 ctx->tunnel.error = error_code;
707 if(error_code)
708 ctx->tunnel.reset = TRUE;
710 return 0;
711}
713static CURLcode proxy_h2_submit(
714 int32_t *pstream_id,
715 struct Curl_cfilter *cf,
716 struct Curl_easy *data,
717 struct curl_ngh2 *ngh2,
718 struct httpreq *req,
719 const nghttp2_priority_spec *pri_spec,
720 void *stream_user_data,
721 nghttp2_data_source_read_callback read_callback,
722 void *read_ctx)
723{
724 struct dynhds h2_headers;
725 nghttp2_nv *nva = NULL;
726 int32_t stream_id = -1;
727 size_t nheader;
728 CURLcode result;
730 (void)cf;
731 Curl_dynhds_init(&h2_headers, MAX_HTTP_RESP_HEADER_COUNT, DYN_HTTP_REQUEST);
732 result = Curl_http_req_to_h2(&h2_headers, req, data);
733 if(result)
734 goto out;
736 nva = Curl_dynhds_to_nva(&h2_headers, &nheader);
737 if(!nva) {
738 result = CURLE_OUT_OF_MEMORY;
739 goto out;
740 }
742 if(read_callback) {
743 nghttp2_data_provider data_prd;
745 data_prd.read_callback = read_callback;
746 data_prd.source.ptr = read_ctx;
747 stream_id = Curl_ngh2_submit_request(
748 ngh2, pri_spec, nva, nheader, &data_prd, stream_user_data);
749 }
750 else {
751 stream_id = Curl_ngh2_submit_request(
752 ngh2, pri_spec, nva, nheader, NULL, stream_user_data);
753 }
755 if(stream_id < 0) {
756 failf(data, "nghttp2_session_upgrade2() failed: %s(%d)",
757 nghttp2_strerror(stream_id), stream_id);
758 result = CURLE_SEND_ERROR;
759 goto out;
760 }
761 result = CURLE_OK;
763out:
764 curlx_free(nva);
765 Curl_dynhds_free(&h2_headers);
766 *pstream_id = stream_id;
767 return result;
768}
770static CURLcode submit_CONNECT(struct Curl_cfilter *cf,
771 struct Curl_easy *data,
772 struct tunnel_stream *ts)
773{
774 struct cf_h2_proxy_ctx *ctx = cf->ctx;
775 CURLcode result;
776 struct httpreq *req = NULL;
778 result = Curl_http_proxy_create_tunnel_request(&req, cf, data,
779 ctx->peer, ctx->dest,
780 PROXY_HTTP_V2,
781 (bool)ctx->udp_tunnel);
782 if(result)
783 goto out;
784 result = Curl_creader_set_null(data);
785 if(result)
786 goto out;
788 result = proxy_h2_submit(&ts->stream_id, cf, data, &ctx->ngh2, req,
789 NULL, ts, tunnel_send_callback, cf);
790 if(result) {
791 CURL_TRC_CF(data, cf, "[%d] send, nghttp2_submit_request error: %s",
792 ts->stream_id, nghttp2_strerror(ts->stream_id));
793 }
795out:
796 if(req)
797 Curl_http_req_free(req);
798 if(result)
799 failf(data, "Failed sending CONNECT to proxy");
800 return result;
801}
803static CURLcode inspect_response(struct Curl_cfilter *cf,
804 struct Curl_easy *data,
805 struct tunnel_stream *ts)
806{
807 struct cf_h2_proxy_ctx *ctx = cf->ctx;
808 proxy_inspect_result res;
809 CURLcode result;
811 result = Curl_http_proxy_inspect_tunnel_response(
812 cf, data, ts->resp, (bool)ctx->udp_tunnel, &res);
813 if(result)
814 return result;
815 switch(res) {
816 case PROXY_INSPECT_OK:
817 h2_tunnel_go_state(cf, ts, H2_TUNNEL_ESTABLISHED, data,
818 (bool)ctx->udp_tunnel);
819 break;
820 case PROXY_INSPECT_FAILED:
821 h2_tunnel_go_state(cf, ts, H2_TUNNEL_FAILED, data,
822 (bool)ctx->udp_tunnel);
823 result = CURLE_COULDNT_CONNECT;
824 break;
825 case PROXY_INSPECT_AUTH_RETRY:
826 h2_tunnel_go_state(cf, ts, H2_TUNNEL_INIT, data,
827 (bool)ctx->udp_tunnel);
828 break;
829 }
830 return result;
831}
833static CURLcode H2_CONNECT(struct Curl_cfilter *cf,
834 struct Curl_easy *data,
835 struct tunnel_stream *ts)
836{
837 struct cf_h2_proxy_ctx *ctx = cf->ctx;
838 CURLcode result = CURLE_OK;
840 DEBUGASSERT(ts);
841 DEBUGASSERT(ts->authority);
842 if(ctx->conn_closed) {
843 failf(data, "proxy closed connection");
844 return CURLE_COULDNT_CONNECT;
845 }
847 do {
848 switch(ts->state) {
849 case H2_TUNNEL_INIT:
850 /* Prepare the CONNECT request and make a first attempt to send. */
851 CURL_TRC_CF(data, cf, "[0] CONNECT start for %s", ts->authority);
852 result = submit_CONNECT(cf, data, ts);
853 if(result)
854 goto out;
855 h2_tunnel_go_state(cf, ts, H2_TUNNEL_CONNECT, data,
856 (bool)ctx->udp_tunnel);
857 FALLTHROUGH();
859 case H2_TUNNEL_CONNECT:
860 /* see that the request is completely sent */
861 result = proxy_h2_progress_ingress(cf, data);
862 if(!result)
863 result = proxy_h2_progress_egress(cf, data);
864 if(result && result != CURLE_AGAIN) {
865 h2_tunnel_go_state(cf, ts, H2_TUNNEL_FAILED, data,
866 (bool)ctx->udp_tunnel);
867 break;
868 }
870 if(ts->has_final_response) {
871 h2_tunnel_go_state(cf, ts, H2_TUNNEL_RESPONSE, data,
872 (bool)ctx->udp_tunnel);
873 }
874 else {
875 result = CURLE_OK;
876 goto out;
877 }
878 FALLTHROUGH();
880 case H2_TUNNEL_RESPONSE:
881 DEBUGASSERT(ts->has_final_response);
882 result = inspect_response(cf, data, ts);
883 if(result)
884 goto out;
885 break;
887 case H2_TUNNEL_ESTABLISHED:
888 return CURLE_OK;
890 case H2_TUNNEL_FAILED:
891 return CURLE_RECV_ERROR;
893 default:
894 break;
895 }
897 } while(ts->state == H2_TUNNEL_INIT);
899out:
900 if((result && (result != CURLE_AGAIN)) || ctx->tunnel.closed)
901 h2_tunnel_go_state(cf, ts, H2_TUNNEL_FAILED, data,
902 (bool)ctx->udp_tunnel);
903 return result;
904}
906/*
907 * Initialize the cfilter context
908 */
909static CURLcode cf_h2_proxy_ctx_init(struct Curl_cfilter *cf,
910 struct Curl_easy *data)
911{
912 struct cf_h2_proxy_ctx *ctx = cf->ctx;
913 CURLcode result = CURLE_OUT_OF_MEMORY;
914 nghttp2_session_callbacks *cbs = NULL;
915 int rc;
917 DEBUGASSERT(!ctx->ngh2.s);
918 memset(&ctx->tunnel, 0, sizeof(ctx->tunnel));
920 Curl_bufq_init(&ctx->inbufq, PROXY_H2_CHUNK_SIZE, PROXY_H2_NW_RECV_CHUNKS);
921 Curl_bufq_init(&ctx->outbufq, PROXY_H2_CHUNK_SIZE, PROXY_H2_NW_SEND_CHUNKS);
922 ctx->remote_max_sid = INT32_MAX;
924 if(tunnel_stream_init(&ctx->tunnel, ctx->dest))
925 goto out;
927 rc = nghttp2_session_callbacks_new(&cbs);
928 if(rc) {
929 failf(data, "Could not initialize nghttp2 callbacks");
930 goto out;
931 }
933 nghttp2_session_callbacks_set_send_callback(cbs, on_session_send);
934 nghttp2_session_callbacks_set_on_frame_recv_callback(
935 cbs, proxy_h2_on_frame_recv);
936#ifdef CURLVERBOSE
937 nghttp2_session_callbacks_set_on_frame_send_callback(cbs,
938 proxy_h2_on_frame_send);
939#endif
940 nghttp2_session_callbacks_set_on_data_chunk_recv_callback(
941 cbs, tunnel_recv_callback);
942 nghttp2_session_callbacks_set_on_stream_close_callback(
943 cbs, proxy_h2_on_stream_close);
944 nghttp2_session_callbacks_set_on_header_callback(cbs, proxy_h2_on_header);
946 /* The nghttp2 session is not yet setup, do it */
947 rc = proxy_h2_client_new(cf, cbs);
948 if(rc) {
949 failf(data, "Could not initialize nghttp2");
950 goto out;
951 }
953 {
954 nghttp2_settings_entry iv[3];
956 iv[0].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS;
957 iv[0].value = Curl_multi_max_concurrent_streams(data->multi);
958 iv[1].settings_id = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE;
959 iv[1].value = H2_TUNNEL_WINDOW_SIZE;
960 iv[2].settings_id = NGHTTP2_SETTINGS_ENABLE_PUSH;
961 iv[2].value = 0;
962 rc = Curl_ngh2_submit_settings(&ctx->ngh2, NGHTTP2_FLAG_NONE, iv, 3);
963 if(rc) {
964 failf(data, "nghttp2_submit_settings() failed: %s(%d)",
965 nghttp2_strerror(rc), rc);
966 result = CURLE_HTTP2;
967 goto out;
968 }
969 }
971 rc = Curl_ngh2_session_set_local_window_size(
972 &ctx->ngh2, NGHTTP2_FLAG_NONE, 0, PROXY_HTTP2_HUGE_WINDOW_SIZE);
973 if(rc) {
974 failf(data, "nghttp2_session_set_local_window_size() failed: %s(%d)",
975 nghttp2_strerror(rc), rc);
976 result = CURLE_HTTP2;
977 goto out;
978 }
980 /* all set, traffic will be send on connect */
981 result = CURLE_OK;
983out:
984 if(cbs)
985 nghttp2_session_callbacks_del(cbs);
986 CURL_TRC_CF(data, cf, "[0] init proxy ctx -> %d", (int)result);
987 return result;
988}
990static CURLcode cf_h2_proxy_connect(struct Curl_cfilter *cf,
991 struct Curl_easy *data,
992 bool *done)
993{
994 struct cf_h2_proxy_ctx *ctx = cf->ctx;
995 CURLcode result = CURLE_OK;
996 struct cf_call_data save;
997 struct tunnel_stream *ts = &ctx->tunnel;
999 if(cf->connected) {
1000 *done = TRUE;
1001 return CURLE_OK;
1002 }
1004 /* Connect the lower filters first */
1005 if(!cf->next->connected) {
1006 result = Curl_conn_cf_connect(cf->next, data, done);
1007 if(result || !*done)
1008 return result;
1009 }
1011 *done = FALSE;
1013 CF_DATA_SAVE(save, cf, data);
1014 if(!ctx->ngh2.s) {
1015 result = cf_h2_proxy_ctx_init(cf, data);
1016 if(result)
1017 goto out;
1018 }
1019 DEBUGASSERT(ts->authority);
1021 if(Curl_timeleft_ms(data) < 0) {
1022 failf(data, "Proxy CONNECT aborted due to timeout");
1023 result = CURLE_OPERATION_TIMEDOUT;
1024 goto out;
1025 }
1027 /* for the secondary socket (FTP), use the "connect to host"
1028 * but ignore the "connect to port" (use the secondary port)
1029 */
1030 result = H2_CONNECT(cf, data, ts);
1032out:
1033 *done = (result == CURLE_OK) && (ts->state == H2_TUNNEL_ESTABLISHED);
1034 if(*done) {
1035 cf->connected = TRUE;
1036 /* The real request will follow the CONNECT, reset request partially */
1037 Curl_req_soft_reset(&data->req, data);
1038 Curl_client_reset(data);
1039 }
1040 CF_DATA_RESTORE(cf, save);
1041 return result;
1042}
1044static void cf_h2_proxy_destroy(struct Curl_cfilter *cf,
1045 struct Curl_easy *data)
1046{
1047 struct cf_h2_proxy_ctx *ctx = cf->ctx;
1049 (void)data;
1050 if(ctx) {
1051 cf_h2_proxy_ctx_free(ctx);
1052 cf->ctx = NULL;
1053 }
1054}
1056static CURLcode cf_h2_proxy_shutdown(struct Curl_cfilter *cf,
1057 struct Curl_easy *data, bool *done)
1058{
1059 struct cf_h2_proxy_ctx *ctx = cf->ctx;
1060 struct cf_call_data save;
1061 CURLcode result;
1062 int rv;
1064 if(!cf->connected || !Curl_ngh2_valid(&ctx->ngh2) ||
1065 cf->shutdown || ctx->conn_closed) {
1066 *done = TRUE;
1067 return CURLE_OK;
1068 }
1070 CF_DATA_SAVE(save, cf, data);
1072 if(!ctx->sent_goaway) {
1073 rv = Curl_ngh2_submit_goaway(
1074 &ctx->ngh2, NGHTTP2_FLAG_NONE, 0, 0,
1075 (const uint8_t *)"shutdown", sizeof("shutdown"));
1076 if(rv) {
1077 failf(data, "nghttp2_submit_goaway() failed: %s(%d)",
1078 nghttp2_strerror(rv), rv);
1079 result = CURLE_SEND_ERROR;
1080 goto out;
1081 }
1082 ctx->sent_goaway = TRUE;
1083 }
1084 /* GOAWAY submitted, process egress and ingress until nghttp2 is done. */
1085 result = CURLE_OK;
1086 if(Curl_ngh2_session_want_write(&ctx->ngh2))
1087 result = proxy_h2_progress_egress(cf, data);
1088 if(!result && Curl_ngh2_session_want_read(&ctx->ngh2))
1089 result = proxy_h2_progress_ingress(cf, data);
1091 *done = (ctx->conn_closed ||
1092 (!result && !Curl_ngh2_session_want_write(&ctx->ngh2) &&
1093 !Curl_ngh2_session_want_read(&ctx->ngh2)));
1094out:
1095 CF_DATA_RESTORE(cf, save);
1096 cf->shutdown = (result || *done);
1097 return result;
1098}
1100static bool cf_h2_proxy_data_pending(struct Curl_cfilter *cf,
1101 const struct Curl_easy *data)
1102{
1103 struct cf_h2_proxy_ctx *ctx = cf->ctx;
1104 if((ctx && !Curl_bufq_is_empty(&ctx->inbufq)) ||
1105 (ctx && ctx->tunnel.state == H2_TUNNEL_ESTABLISHED &&
1106 !Curl_bufq_is_empty(&ctx->tunnel.recvbuf)))
1107 return TRUE;
1108 return cf->next ? cf->next->cft->has_data_pending(cf->next, data) : FALSE;
1109}
1111static CURLcode cf_h2_proxy_adjust_pollset(struct Curl_cfilter *cf,
1112 struct Curl_easy *data,
1113 struct easy_pollset *ps)
1114{
1115 struct cf_h2_proxy_ctx *ctx = cf->ctx;
1116 struct cf_call_data save;
1117 curl_socket_t sock = Curl_conn_cf_get_socket(cf, data);
1118 bool want_recv, want_send;
1119 CURLcode result = CURLE_OK;
1121 if(!cf->connected && Curl_ngh2_valid(&ctx->ngh2)) {
1122 want_send = Curl_ngh2_session_want_write(&ctx->ngh2) ||
1123 !Curl_bufq_is_empty(&ctx->outbufq) ||
1124 !Curl_bufq_is_empty(&ctx->tunnel.sendbuf);
1125 want_recv = Curl_ngh2_session_want_read(&ctx->ngh2);
1126 }
1127 else
1128 Curl_pollset_check(data, ps, sock, &want_recv, &want_send);
1130 if(Curl_ngh2_valid(&ctx->ngh2) && (want_recv || want_send)) {
1131 bool c_exhaust, s_exhaust;
1133 CF_DATA_SAVE(save, cf, data);
1134 c_exhaust = !Curl_ngh2_session_get_remote_window_size(&ctx->ngh2);
1135 s_exhaust = ctx->tunnel.stream_id >= 0 &&
1136 !Curl_ngh2_session_get_stream_remote_window_size(
1137 &ctx->ngh2, ctx->tunnel.stream_id);
1138 want_recv = (want_recv || c_exhaust || s_exhaust);
1139 want_send = (!s_exhaust && want_send) ||
1140 (!c_exhaust && Curl_ngh2_session_want_write(&ctx->ngh2)) ||
1141 !Curl_bufq_is_empty(&ctx->outbufq) ||
1142 !Curl_bufq_is_empty(&ctx->tunnel.sendbuf);
1144 result = Curl_pollset_set(data, ps, sock, want_recv, want_send);
1145 CURL_TRC_CF(data, cf, "adjust_pollset, want_recv=%d want_send=%d -> %d",
1146 want_recv, want_send, (int)result);
1147 CF_DATA_RESTORE(cf, save);
1148 }
1149 else if(ctx->sent_goaway && !cf->shutdown) {
1150 /* shutdown in progress */
1151 CF_DATA_SAVE(save, cf, data);
1152 want_send = Curl_ngh2_session_want_write(&ctx->ngh2) ||
1153 !Curl_bufq_is_empty(&ctx->outbufq) ||
1154 !Curl_bufq_is_empty(&ctx->tunnel.sendbuf);
1155 want_recv = Curl_ngh2_session_want_read(&ctx->ngh2);
1156 result = Curl_pollset_set(data, ps, sock, want_recv, want_send);
1157 CURL_TRC_CF(data, cf, "adjust_pollset, want_recv=%d want_send=%d -> %d",
1158 want_recv, want_send, (int)result);
1159 CF_DATA_RESTORE(cf, save);
1160 }
1161 return result;
1162}
1164static CURLcode h2_handle_tunnel_close(struct Curl_cfilter *cf,
1165 struct Curl_easy *data,
1166 size_t *pnread)
1167{
1168 struct cf_h2_proxy_ctx *ctx = cf->ctx;
1170 *pnread = 0;
1171 if(ctx->tunnel.error) {
1172 failf(data, "HTTP/2 stream %d reset by %s (error 0x%x %s)",
1173 ctx->tunnel.stream_id, ctx->tunnel.reset ? "server" : "curl",
1174 ctx->tunnel.error, nghttp2_http2_strerror(ctx->tunnel.error));
1175 return CURLE_RECV_ERROR;
1176 }
1178 CURL_TRC_CF(data, cf, "[%d] handle_tunnel_close -> 0",
1179 ctx->tunnel.stream_id);
1180 return CURLE_OK;
1181}
1183static CURLcode tunnel_recv(struct Curl_cfilter *cf, struct Curl_easy *data,
1184 char *buf, size_t len, size_t *pnread)
1185{
1186 struct cf_h2_proxy_ctx *ctx = cf->ctx;
1187 CURLcode result = CURLE_AGAIN;
1189 *pnread = 0;
1190 if(!Curl_bufq_is_empty(&ctx->tunnel.recvbuf))
1191 result = Curl_bufq_cread(&ctx->tunnel.recvbuf, buf, len, pnread);
1192 else {
1193 if(ctx->tunnel.closed) {
1194 result = h2_handle_tunnel_close(cf, data, pnread);
1195 }
1196 else if(ctx->tunnel.reset ||
1197 (ctx->conn_closed && Curl_bufq_is_empty(&ctx->inbufq)) ||
1198 (ctx->rcvd_goaway &&
1199 ctx->remote_max_sid < ctx->tunnel.stream_id)) {
1200 result = CURLE_RECV_ERROR;
1201 }
1202 else
1203 result = CURLE_AGAIN;
1204 }
1206 CURL_TRC_CF(data, cf, "[%d] tunnel_recv(len=%zu) -> %d, %zu",
1207 ctx->tunnel.stream_id, len, (int)result, *pnread);
1208 return result;
1209}
1211static CURLcode cf_h2_proxy_recv(struct Curl_cfilter *cf,
1212 struct Curl_easy *data,
1213 char *buf, size_t len,
1214 size_t *pnread)
1215{
1216 struct cf_h2_proxy_ctx *ctx = cf->ctx;
1217 struct cf_call_data save;
1218 CURLcode result;
1220 *pnread = 0;
1221 CF_DATA_SAVE(save, cf, data);
1223 if(ctx->tunnel.state != H2_TUNNEL_ESTABLISHED) {
1224 result = CURLE_RECV_ERROR;
1225 goto out;
1226 }
1228 if(Curl_bufq_is_empty(&ctx->tunnel.recvbuf)) {
1229 result = proxy_h2_progress_ingress(cf, data);
1230 if(result)
1231 goto out;
1232 }
1234 result = tunnel_recv(cf, data, buf, len, pnread);
1236 if(!result) {
1237 CURL_TRC_CF(data, cf, "[%d] increase window by %zu",
1238 ctx->tunnel.stream_id, *pnread);
1239 Curl_ngh2_session_consume(&ctx->ngh2, ctx->tunnel.stream_id, *pnread);
1240 }
1242 result = Curl_1st_fatal(result, proxy_h2_progress_egress(cf, data));
1244out:
1245 if((!Curl_bufq_is_empty(&ctx->tunnel.recvbuf) ||
1246 !Curl_bufq_is_empty(&ctx->tunnel.sendbuf)) &&
1247 (!result || (result == CURLE_AGAIN))) {
1248 /* data pending and no fatal error to report. Need to trigger
1249 * draining to avoid stalling when no socket events happen. */
1250 drain_tunnel(cf, data, &ctx->tunnel);
1251 }
1252 CURL_TRC_CF(data, cf, "[%d] cf_recv(len=%zu) -> %d, %zu",
1253 ctx->tunnel.stream_id, len, (int)result, *pnread);
1254 CF_DATA_RESTORE(cf, save);
1255 return result;
1256}
1258static CURLcode cf_h2_proxy_send(struct Curl_cfilter *cf,
1259 struct Curl_easy *data,
1260 const uint8_t *buf, size_t len, bool eos,
1261 size_t *pnwritten)
1262{
1263 struct cf_h2_proxy_ctx *ctx = cf->ctx;
1264 struct cf_call_data save;
1265 CURLcode result;
1267 (void)eos;
1268 *pnwritten = 0;
1269 CF_DATA_SAVE(save, cf, data);
1271 if(ctx->tunnel.state != H2_TUNNEL_ESTABLISHED) {
1272 result = CURLE_SEND_ERROR;
1273 goto out;
1274 }
1276 if(ctx->tunnel.closed) {
1277 result = CURLE_SEND_ERROR;
1278 goto out;
1279 }
1281 result = Curl_bufq_write(&ctx->tunnel.sendbuf, buf, len, pnwritten);
1282 CURL_TRC_CF(data, cf, "cf_send(), bufq_write %d, %zu", (int)result,
1283 *pnwritten);
1284 if(result && (result != CURLE_AGAIN))
1285 goto out;
1287 if(!Curl_bufq_is_empty(&ctx->tunnel.sendbuf)) {
1288 /* req body data is buffered, resume the potentially suspended stream */
1289 if(Curl_ngh2_is_fatal(&ctx->ngh2,
1290 Curl_ngh2_session_resume_data(&ctx->ngh2, ctx->tunnel.stream_id))) {
1291 result = CURLE_SEND_ERROR;
1292 goto out;
1293 }
1294 }
1296 result = Curl_1st_fatal(result, proxy_h2_progress_ingress(cf, data));
1297 result = Curl_1st_fatal(result, proxy_h2_progress_egress(cf, data));
1299 if(!result && proxy_h2_should_close_session(ctx)) {
1300 /* nghttp2 thinks this session is done. If the stream has not been
1301 * closed, this is an error state for out transfer */
1302 if(ctx->tunnel.closed) {
1303 result = CURLE_SEND_ERROR;
1304 }
1305 else {
1306 CURL_TRC_CF(data, cf, "[0] send: nothing to do in this session");
1307 result = CURLE_HTTP2;
1308 }
1309 }
1311out:
1312 if((!Curl_bufq_is_empty(&ctx->tunnel.recvbuf) ||
1313 !Curl_bufq_is_empty(&ctx->tunnel.sendbuf)) &&
1314 (!result || (result == CURLE_AGAIN))) {
1315 /* data pending and no fatal error to report. Need to trigger
1316 * draining to avoid stalling when no socket events happen. */
1317 drain_tunnel(cf, data, &ctx->tunnel);
1318 }
1319 CURL_TRC_CF(data, cf, "[%d] cf_send(len=%zu) -> %d, %zu, "
1320 "h2 windows %d-%d (stream-conn), buffers %zu-%zu (stream-conn)",
1321 ctx->tunnel.stream_id, len, (int)result, *pnwritten,
1322 Curl_ngh2_session_get_stream_remote_window_size(
1323 &ctx->ngh2, ctx->tunnel.stream_id),
1324 Curl_ngh2_session_get_remote_window_size(&ctx->ngh2),
1325 Curl_bufq_len(&ctx->tunnel.sendbuf),
1326 Curl_bufq_len(&ctx->outbufq));
1327 CF_DATA_RESTORE(cf, save);
1328 return result;
1329}
1331static CURLcode cf_h2_proxy_flush(struct Curl_cfilter *cf,
1332 struct Curl_easy *data)
1333{
1334 struct cf_h2_proxy_ctx *ctx = cf->ctx;
1335 struct cf_call_data save;
1336 CURLcode result = CURLE_OK;
1338 CF_DATA_SAVE(save, cf, data);
1339 if(!Curl_bufq_is_empty(&ctx->tunnel.sendbuf)) {
1340 /* resume the potentially suspended tunnel */
1341 if(Curl_ngh2_is_fatal(&ctx->ngh2,
1342 Curl_ngh2_session_resume_data(&ctx->ngh2, ctx->tunnel.stream_id))) {
1343 result = CURLE_SEND_ERROR;
1344 goto out;
1345 }
1346 }
1348 result = proxy_h2_progress_egress(cf, data);
1350out:
1351 CURL_TRC_CF(data, cf, "[%d] flush -> %d, "
1352 "h2 windows %d-%d (stream-conn), buffers %zu-%zu (stream-conn)",
1353 ctx->tunnel.stream_id, (int)result,
1354 Curl_ngh2_session_get_stream_remote_window_size(
1355 &ctx->ngh2, ctx->tunnel.stream_id),
1356 Curl_ngh2_session_get_remote_window_size(&ctx->ngh2),
1357 Curl_bufq_len(&ctx->tunnel.sendbuf),
1358 Curl_bufq_len(&ctx->outbufq));
1359 CF_DATA_RESTORE(cf, save);
1360 return result;
1361}
1363static bool proxy_h2_connisalive(struct Curl_cfilter *cf,
1364 struct Curl_easy *data,
1365 bool *input_pending)
1366{
1367 struct cf_h2_proxy_ctx *ctx = cf->ctx;
1368 bool alive = TRUE;
1370 *input_pending = FALSE;
1371 if(!cf->next || !cf->next->cft->is_alive(cf->next, data, input_pending))
1372 return FALSE;
1374 if(*input_pending) {
1375 /* This happens before we have sent off a request and the connection is
1376 not in use by any other transfer, there should not be any data here,
1377 only "protocol frames" */
1378 CURLcode result;
1379 size_t nread;
1381 *input_pending = FALSE;
1382 result = Curl_cf_recv_bufq(cf->next, data, &ctx->inbufq, 0, &nread);
1383 if(!result) {
1384 if(proxy_h2_process_pending_input(cf, data))
1385 /* immediate error, considered dead */
1386 alive = FALSE;
1387 else {
1388 alive = !proxy_h2_should_close_session(ctx);
1389 }
1390 }
1391 else if(result != CURLE_AGAIN) {
1392 /* the read failed so let's say this is dead anyway */
1393 alive = FALSE;
1394 }
1395 }
1397 return alive;
1398}
1400static bool cf_h2_proxy_is_alive(struct Curl_cfilter *cf,
1401 struct Curl_easy *data,
1402 bool *input_pending)
1403{
1404 struct cf_h2_proxy_ctx *ctx = cf->ctx;
1405 bool alive;
1406 struct cf_call_data save;
1408 *input_pending = FALSE;
1409 CF_DATA_SAVE(save, cf, data);
1410 alive = (ctx && Curl_ngh2_valid(&ctx->ngh2) &&
1411 proxy_h2_connisalive(cf, data, input_pending));
1412 CURL_TRC_CF(data, cf, "[0] conn alive -> %d, input_pending=%d",
1413 alive, *input_pending);
1414 CF_DATA_RESTORE(cf, save);
1415 return alive;
1416}
1418static CURLcode cf_h2_proxy_query(struct Curl_cfilter *cf,
1419 struct Curl_easy *data,
1420 int query, int *pres1, void *pres2)
1421{
1422 struct cf_h2_proxy_ctx *ctx = cf->ctx;
1424 switch(query) {
1425 case CF_QUERY_HOST_PORT:
1426 *pres1 = (int)ctx->dest->port;
1427 *((const char **)pres2) = ctx->dest->hostname;
1428 return CURLE_OK;
1429 case CF_QUERY_NEED_FLUSH: {
1430 if(!Curl_bufq_is_empty(&ctx->outbufq) ||
1431 !Curl_bufq_is_empty(&ctx->tunnel.sendbuf)) {
1432 CURL_TRC_CF(data, cf, "needs flush");
1433 *pres1 = TRUE;
1434 return CURLE_OK;
1435 }
1436 break;
1437 }
1438 case CF_QUERY_ALPN_NEGOTIATED: {
1439 const char **palpn = pres2;
1440 DEBUGASSERT(palpn);
1441 *palpn = NULL;
1442 return CURLE_OK;
1443 }
1444 default:
1445 break;
1446 }
1447 return cf->next ?
1448 cf->next->cft->query(cf->next, data, query, pres1, pres2) :
1449 CURLE_UNKNOWN_OPTION;
1450}
1452static CURLcode cf_h2_proxy_cntrl(struct Curl_cfilter *cf,
1453 struct Curl_easy *data,
1454 int event, int arg1, void *arg2)
1455{
1456 CURLcode result = CURLE_OK;
1457 struct cf_call_data save;
1459 (void)arg1;
1460 (void)arg2;
1462 switch(event) {
1463 case CF_CTRL_FLUSH:
1464 CF_DATA_SAVE(save, cf, data);
1465 result = cf_h2_proxy_flush(cf, data);
1466 CF_DATA_RESTORE(cf, save);
1467 break;
1468 default:
1469 break;
1470 }
1471 return result;
1472}
1474struct Curl_cftype Curl_cft_h2_proxy = {
1475 "H2-PROXY",
1476 CF_TYPE_IP_CONNECT | CF_TYPE_PROXY,
1477 CURL_LOG_LVL_NONE,
1478 cf_h2_proxy_destroy,
1479 cf_h2_proxy_connect,
1480 cf_h2_proxy_shutdown,
1481 cf_h2_proxy_adjust_pollset,
1482 cf_h2_proxy_data_pending,
1483 cf_h2_proxy_send,
1484 cf_h2_proxy_recv,
1485 cf_h2_proxy_cntrl,
1486 cf_h2_proxy_is_alive,
1487 Curl_cf_def_conn_keep_alive,
1488 cf_h2_proxy_query,
1489};
1491CURLcode Curl_cf_h2_proxy_insert_after(struct Curl_cfilter *cf,
1492 struct Curl_easy *data,
1493 struct Curl_peer *peer,
1494 struct Curl_peer *dest,
1495 bool udp_tunnel)
1496{
1497 struct Curl_cfilter *cf_h2_proxy = NULL;
1498 struct cf_h2_proxy_ctx *ctx;
1499 CURLcode result = CURLE_OUT_OF_MEMORY;
1501 (void)data;
1502 ctx = curlx_calloc(1, sizeof(*ctx));
1503 if(!ctx)
1504 goto out;
1505 Curl_peer_link(&ctx->peer, peer);
1506 Curl_peer_link(&ctx->dest, dest);
1507 ctx->udp_tunnel = udp_tunnel;
1509 result = Curl_cf_create(&cf_h2_proxy, &Curl_cft_h2_proxy, ctx);
1510 if(result)
1511 goto out;
1512 ctx = NULL;
1513 Curl_conn_cf_insert_after(cf, cf_h2_proxy);
1515out:
1516 cf_h2_proxy_ctx_free(ctx);
1517 return result;
1518}
1520#endif /* !CURL_DISABLE_HTTP && !CURL_DISABLE_PROXY && USE_NGHTTP2 */
Diagnostics4 git calls · 29 ms
CallStatusSizeTime029 ms
discover200152 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=1 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200149 B · hit2 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=149 git.response.cache=HIT git.response.duration_ms=2 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=149 git.pkt.payload.max=78 git.ls_refs.count=2 git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit1 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=25 git.response.bytes=293470 git.response.cache=HIT git.response.duration_ms=1 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=10 git.pkt.data=8 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=293470 git.pkt.payload.max=65516 git.sideband.band1.bytes=293354 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=87 git.pack.bytes=293354 git.pack.ofs_delta=0 git.pack.ref_delta=1 git.pack.chain.max=1 git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0 git.pack.full=86Open pack map
fetch20012 KiB · hit25 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=11801 git.response.cache=HIT git.response.duration_ms=25 git.fetch.wants=a555abc4b4904585089586b44723610f188ff474 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=11801 git.pkt.payload.max=11780 git.sideband.band1.bytes=11779 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
- Ref
- refs/heads/master
- Commit
- 31c911efe5652725e930f4d2e60c9060e5f6646d
- cf-ray
- a5e2c147ae5e89da
- Request
- 0d59322e…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format