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_PROXY) && !defined(CURL_DISABLE_HTTP)
29#include <curl/curl.h>
30#include "urldata.h"
31#include "curlx/dynbuf.h"
32#include "sendf.h"
33#include "http.h"
34#include "http1.h"
35#include "http_proxy.h"
36#include "select.h"
37#include "progress.h"
38#include "multiif.h"
39#include "cfilters.h"
40#include "cf-h1-proxy.h"
41#include "connect.h"
42#include "curl_trc.h"
43#include "strcase.h"
44#include "curlx/strparse.h"
46typedef enum {
47 H1_TUNNEL_INIT, /* init/default/no tunnel state */
48 H1_TUNNEL_CONNECT, /* CONNECT request is being send */
49 H1_TUNNEL_RECEIVE, /* CONNECT answer is being received */
50 H1_TUNNEL_RESPONSE, /* CONNECT response received completely */
51 H1_TUNNEL_ESTABLISHED,
52 H1_TUNNEL_FAILED
53} h1_tunnel_state;
55/* struct for HTTP CONNECT tunneling */
56struct h1_tunnel_state {
57 struct Curl_peer *dest;
58 struct dynbuf rcvbuf;
59 struct dynbuf request_data;
60 size_t nsent;
61 size_t headerlines;
62 struct Curl_chunker ch;
63 int httpversion;
64 enum keeponval {
65 KEEPON_DONE,
66 KEEPON_CONNECT,
67 KEEPON_IGNORE
68 } keepon;
69 curl_off_t cl; /* size of content to read and ignore */
70 h1_tunnel_state tunnel_state;
71 BIT(chunked_encoding);
72 BIT(close_connection);
73 BIT(maybe_folded);
74 BIT(leading_unfold);
75 BIT(udp_tunnel);
76};
78/* Persistent context for the H1-PROXY filter */
79struct cf_h1_proxy_ctx {
80 struct Curl_peer *peer;
81 struct h1_tunnel_state *ts;
82};
84static bool tunnel_is_established(struct h1_tunnel_state *ts)
85{
86 return ts && (ts->tunnel_state == H1_TUNNEL_ESTABLISHED);
87}
89static bool tunnel_is_failed(struct h1_tunnel_state *ts)
90{
91 return ts && (ts->tunnel_state == H1_TUNNEL_FAILED);
92}
94static bool h1_proxy_is_udp(struct Curl_cfilter *cf)
95{
96 struct cf_h1_proxy_ctx *pctx = cf->ctx;
97 return (bool)(pctx->ts && pctx->ts->udp_tunnel);
98}
100static CURLcode tunnel_reinit(struct Curl_cfilter *cf,
101 struct Curl_easy *data,
102 struct h1_tunnel_state *ts)
103{
104 (void)data;
105 (void)cf;
106 DEBUGASSERT(ts);
107 curlx_dyn_reset(&ts->rcvbuf);
108 curlx_dyn_reset(&ts->request_data);
109 ts->tunnel_state = H1_TUNNEL_INIT;
110 ts->keepon = KEEPON_CONNECT;
111 ts->cl = 0;
112 ts->close_connection = FALSE;
113 ts->maybe_folded = FALSE;
114 ts->leading_unfold = FALSE;
115 ts->chunked_encoding = FALSE;
116 ts->nsent = 0;
117 ts->headerlines = 0;
118 return CURLE_OK;
119}
121static CURLcode tunnel_init(struct Curl_cfilter *cf,
122 struct Curl_easy *data,
123 struct h1_tunnel_state **pts)
124{
125 struct h1_tunnel_state *ts;
127 if(cf->conn->scheme->flags & PROTOPT_NOTCPPROXY) {
128 failf(data, "%s cannot be done over CONNECT", cf->conn->scheme->name);
129 return CURLE_UNSUPPORTED_PROTOCOL;
130 }
132 ts = curlx_calloc(1, sizeof(*ts));
133 if(!ts)
134 return CURLE_OUT_OF_MEMORY;
136 infof(data, "allocate connect buffer");
138 curlx_dyn_init(&ts->rcvbuf, DYN_PROXY_CONNECT_HEADERS);
139 curlx_dyn_init(&ts->request_data, DYN_HTTP_REQUEST);
140 Curl_httpchunk_init(data, &ts->ch, TRUE, TRUE);
142 *pts = ts;
143 return tunnel_reinit(cf, data, ts);
144}
146static void h1_tunnel_go_state(struct Curl_cfilter *cf,
147 struct h1_tunnel_state *ts,
148 h1_tunnel_state new_state,
149 struct Curl_easy *data)
150{
151 if(ts->tunnel_state == new_state)
152 return;
153 /* entering this one */
154 switch(new_state) {
155 case H1_TUNNEL_INIT:
156 CURL_TRC_CF(data, cf, "new tunnel state 'init'");
157 tunnel_reinit(cf, data, ts);
158 break;
160 case H1_TUNNEL_CONNECT:
161 CURL_TRC_CF(data, cf, "new tunnel state 'connect'");
162 ts->tunnel_state = H1_TUNNEL_CONNECT;
163 ts->keepon = KEEPON_CONNECT;
164 curlx_dyn_reset(&ts->rcvbuf);
165 break;
167 case H1_TUNNEL_RECEIVE:
168 CURL_TRC_CF(data, cf, "new tunnel state 'receive'");
169 ts->tunnel_state = H1_TUNNEL_RECEIVE;
170 break;
172 case H1_TUNNEL_RESPONSE:
173 CURL_TRC_CF(data, cf, "new tunnel state 'response'");
174 ts->tunnel_state = H1_TUNNEL_RESPONSE;
175 break;
177 case H1_TUNNEL_ESTABLISHED:
178 CURL_TRC_CF(data, cf, "new tunnel state 'established'");
179 infof(data, "CONNECT%s phase completed for HTTP proxy",
180 h1_proxy_is_udp(cf) ? "-UDP" : "");
182 data->state.authproxy.done = TRUE;
183 data->state.authproxy.multipass = FALSE;
184 FALLTHROUGH();
185 case H1_TUNNEL_FAILED:
186 if(new_state == H1_TUNNEL_FAILED)
187 CURL_TRC_CF(data, cf, "new tunnel state 'failed'");
188 ts->tunnel_state = new_state;
189 curlx_dyn_reset(&ts->rcvbuf);
190 curlx_dyn_reset(&ts->request_data);
191 /* restore the protocol pointer */
192 data->info.httpcode = 0; /* clear it as it might have been used for the
193 proxy */
194 /* If a proxy-authorization header was used for the proxy, then we should
195 make sure that it is not accidentally used for the document request
196 after we have connected. Let's thus free and clear it here. */
197 curlx_safefree(data->req.hd_proxy_auth);
198 break;
199 }
200}
202static void tunnel_free(struct h1_tunnel_state *ts,
203 struct Curl_easy *data)
204{
205 if(ts) {
206 Curl_peer_unlink(&ts->dest);
207 curlx_dyn_free(&ts->rcvbuf);
208 curlx_dyn_free(&ts->request_data);
209 Curl_httpchunk_free(data, &ts->ch);
210 curlx_free(ts);
211 }
212}
214static void cf_tunnel_free(struct Curl_cfilter *cf,
215 struct Curl_easy *data)
216{
217 if(cf) {
218 struct cf_h1_proxy_ctx *pctx = cf->ctx;
219 struct h1_tunnel_state *ts = pctx ? pctx->ts : NULL;
220 if(ts) {
221 h1_tunnel_go_state(cf, ts, H1_TUNNEL_FAILED, data);
222 tunnel_free(ts, data);
223 pctx->ts = NULL;
224 }
225 }
226}
228static bool tunnel_want_send(struct h1_tunnel_state *ts)
229{
230 return ts->tunnel_state == H1_TUNNEL_CONNECT;
231}
233static CURLcode start_CONNECT(struct Curl_cfilter *cf,
234 struct Curl_easy *data,
235 struct h1_tunnel_state *ts)
236{
237 struct cf_h1_proxy_ctx *pctx = cf->ctx;
238 struct httpreq *req = NULL;
239 int http_minor;
240 CURLcode result;
242 DEBUGASSERT(data);
243 /* This only happens if we have looped here due to authentication reasons,
244 and we do not really use the newly cloned URL here then. Free it. */
245 curlx_safefree(data->req.newurl);
247 result = Curl_http_proxy_create_tunnel_request(&req, cf, data,
248 pctx->peer, ts->dest,
249 PROXY_HTTP_V1,
250 h1_proxy_is_udp(cf));
251 if(result)
252 goto out;
254 curlx_dyn_reset(&ts->request_data);
255 ts->nsent = 0;
256 ts->headerlines = 0;
257 http_minor = ts->httpversion % 10;
259 result = Curl_h1_req_write_head(req, http_minor, &ts->request_data);
260 if(!result)
261 result = Curl_creader_set_null(data);
263out:
264 if(result)
265 failf(data, "Failed sending CONNECT to proxy");
266 if(req)
267 Curl_http_req_free(req);
268 return result;
269}
271static CURLcode send_CONNECT(struct Curl_cfilter *cf,
272 struct Curl_easy *data,
273 struct h1_tunnel_state *ts,
274 bool *done)
275{
276 const uint8_t *buf = curlx_dyn_uptr(&ts->request_data);
277 size_t request_len = curlx_dyn_len(&ts->request_data);
278 size_t blen = request_len;
279 CURLcode result = CURLE_OK;
280 size_t nwritten;
282 if(blen <= ts->nsent)
283 goto out; /* we are done */
285 blen -= ts->nsent;
286 buf += ts->nsent;
288 result = cf->next->cft->do_send(cf->next, data, buf, blen, FALSE, &nwritten);
289 if(result) {
290 if(result == CURLE_AGAIN)
291 result = CURLE_OK;
292 goto out;
293 }
295 DEBUGASSERT(blen >= nwritten);
296 ts->nsent += nwritten;
297 Curl_debug(data, CURLINFO_HEADER_OUT, (const char *)buf, nwritten);
299out:
300 if(result)
301 failf(data, "Failed sending CONNECT to proxy");
302 *done = (!result && (ts->nsent >= request_len));
303 return result;
304}
306static CURLcode on_resp_status(struct Curl_cfilter *cf,
307 struct Curl_easy *data,
308 struct h1_tunnel_state *ts,
309 const char *header)
310{
311 struct SingleRequest *k = &data->req;
313 (void)ts;
314 if(!strncmp(header, "HTTP/1.", 7) &&
315 ((header[7] == '0') || (header[7] == '1')) &&
316 (header[8] == ' ') &&
317 ISDIGIT(header[9]) && ISDIGIT(header[10]) && ISDIGIT(header[11]) &&
318 !ISDIGIT(header[12])) {
319 /* store the HTTP code from the proxy */
320 data->info.httpproxycode = k->httpcode = ((header[9] - '0') * 100) +
321 ((header[10] - '0') * 10) + (header[11] - '0');
322 CURL_TRC_CF(data, cf, "CONNECT%s HTTP status %d",
323 h1_proxy_is_udp(cf) ? "-UDP" : "", k->httpcode);
324 return CURLE_OK;
325 }
326 failf(data, "Invalid response header");
327 return CURLE_WEIRD_SERVER_REPLY;
328}
330static CURLcode on_resp_header(struct Curl_cfilter *cf,
331 struct Curl_easy *data,
332 struct h1_tunnel_state *ts,
333 const char *header)
334{
335 CURLcode result = CURLE_OK;
336 struct SingleRequest *k = &data->req;
337 bool is_udp = h1_proxy_is_udp(cf);
339 if(checkprefix("Proxy-authenticate:", header) && (407 == k->httpcode)) {
341 bool proxy = (k->httpcode == 407);
342 char *auth = Curl_copy_header_value(header);
343 if(!auth)
344 return CURLE_OUT_OF_MEMORY;
346 CURL_TRC_CF(data, cf, "CONNECT%s: fwd auth header '%s'",
347 is_udp ? "-UDP" : "", header);
348 result = Curl_http_input_auth(data, proxy, auth);
350 curlx_free(auth);
352 if(result)
353 return result;
354 }
355 else if(checkprefix("Content-Length:", header)) {
356 if(k->httpcode < 300) {
357 if(k->httpcode < 200) {
358 /* Informational 1xx responses cannot carry a body. RFC 9110 15.2 */
359 failf(data, "Invalid Content-Length: in %03d response", k->httpcode);
360 return CURLE_WEIRD_SERVER_REPLY;
361 }
362 /* A client MUST ignore any Content-Length or Transfer-Encoding
363 header fields received in a successful response to CONNECT.
364 "Successful" described as: 2xx (Successful). RFC 7231 4.3.6 */
365 infof(data, "Ignoring Content-Length in CONNECT%s %03d response",
366 is_udp ? "-UDP" : "", k->httpcode);
367 }
368 else {
369 const char *p = header + CURL_CSTRLEN("Content-Length:");
370 if(curlx_str_numblanks(&p, &ts->cl)) {
371 failf(data, "Unsupported Content-Length value");
372 return CURLE_WEIRD_SERVER_REPLY;
373 }
374 }
375 }
376 else if(Curl_compareheader(header,
377 STRCONST("Connection:"), STRCONST("close"))) {
378 CURL_TRC_CF(data, cf, "CONNECT%s Connection: close", is_udp ? "-UDP" : "");
379 ts->close_connection = TRUE;
380 }
381 else if(checkprefix("Transfer-Encoding:", header)) {
382 if(k->httpcode < 300) {
383 if(k->httpcode < 200) {
384 /* Informational 1xx responses cannot carry a body. RFC 9110 15.2 */
385 failf(data, "Invalid Transfer-Encoding: in %03d response",
386 k->httpcode);
387 return CURLE_WEIRD_SERVER_REPLY;
388 }
389 /* A client MUST ignore any Content-Length or Transfer-Encoding
390 header fields received in a successful response to CONNECT.
391 "Successful" described as: 2xx (Successful). RFC 7231 4.3.6 */
392 infof(data, "Ignoring Transfer-Encoding in "
393 "CONNECT%s %03d response", is_udp ? "-UDP" : "", k->httpcode);
394 }
395 else if(Curl_compareheader(header,
396 STRCONST("Transfer-Encoding:"),
397 STRCONST("chunked"))) {
398 CURL_TRC_CF(data, cf, "CONNECT%s response chunked",
399 is_udp ? "-UDP" : "");
400 ts->chunked_encoding = TRUE;
401 Curl_httpchunk_reset(data, &ts->ch, TRUE);
402 }
403 }
404 else if(is_udp && checkprefix("Capsule-protocol:", header)) {
405 if(Curl_compareheader(header,
406 STRCONST("Capsule-protocol:"),
407 STRCONST("?1"))) {
408 CURL_TRC_CF(data, cf, "CONNECT-UDP Response --> Capsule-protocol: ?1");
409 }
410 }
411 else if(Curl_compareheader(header,
412 STRCONST("Proxy-Connection:"),
413 STRCONST("close"))) {
414 CURL_TRC_CF(data, cf, "CONNECT%s Proxy-Connection: close",
415 is_udp ? "-UDP" : "");
416 ts->close_connection = TRUE;
417 }
418 return result;
419}
421static CURLcode single_header(struct Curl_cfilter *cf,
422 struct Curl_easy *data,
423 struct h1_tunnel_state *ts)
424{
425 CURLcode result = CURLE_OK;
426 const char *linep = curlx_dyn_ptr(&ts->rcvbuf);
427 size_t line_len = curlx_dyn_len(&ts->rcvbuf); /* bytes in this line */
428 const struct SingleRequest *k = &data->req;
429 int writetype;
430 ts->headerlines++;
432 /* output debug if that is requested */
433 Curl_debug(data, CURLINFO_HEADER_IN, linep, line_len);
435 /* a CONNECT response line is handed to the client as a header, so it must
436 pass the same checks as a regular response header before delivery */
437 result = Curl_verify_header(data, linep, line_len);
438 if(result)
439 return result;
441 /* send the header to the callback */
442 writetype = CLIENTWRITE_HEADER | CLIENTWRITE_CONNECT |
443 (ts->headerlines == 1 ? CLIENTWRITE_STATUS : 0);
444 result = Curl_client_write(data, writetype, linep, line_len);
445 if(result)
446 return result;
448 result = Curl_bump_headersize(data, line_len, TRUE);
449 if(result)
450 return result;
452 /* Newlines are CRLF, so the CR is ignored as the line is not
453 really terminated until the LF comes. Treat a following CR
454 as end-of-headers as well.*/
456 if(ISNEWLINE(linep[0])) {
457 /* end of response-headers from the proxy */
458 if(ts->headerlines == 1) {
459 ts->keepon = KEEPON_DONE;
460 result = CURLE_WEIRD_SERVER_REPLY;
461 }
462 else if((407 == k->httpcode) && !data->state.authproblem) {
463 /* If we get a 407 response code with content length
464 when we have no auth problem, we must ignore the
465 whole response-body */
466 ts->keepon = KEEPON_IGNORE;
468 if(ts->cl) {
469 infof(data, "Ignore %" FMT_OFF_T " bytes of response-body", ts->cl);
470 }
471 else if(ts->chunked_encoding) {
472 infof(data, "Ignore chunked response-body");
473 }
474 else {
475 /* without content-length or chunked encoding, we
476 cannot keep the connection alive since the close is
477 the end signal so we bail out at once instead */
478 CURL_TRC_CF(data, cf, "CONNECT: no content-length or chunked");
479 ts->keepon = KEEPON_DONE;
480 }
481 }
482 else {
483 ts->keepon = KEEPON_DONE;
484 }
486 DEBUGASSERT(ts->keepon == KEEPON_IGNORE ||
487 ts->keepon == KEEPON_DONE);
488 return result;
489 }
491 if(ts->headerlines == 1)
492 result = on_resp_status(cf, data, ts, linep);
493 else
494 result = on_resp_header(cf, data, ts, linep);
495 if(result)
496 return result;
498 curlx_dyn_reset(&ts->rcvbuf);
499 return result;
500}
502static CURLcode recv_CONNECT_resp(struct Curl_cfilter *cf,
503 struct Curl_easy *data,
504 struct h1_tunnel_state *ts,
505 bool *done)
506{
507 CURLcode result = CURLE_OK;
508 int error;
510#define SELECT_OK 0
511#define SELECT_ERROR 1
513 error = SELECT_OK;
514 *done = FALSE;
516 while(ts->keepon) {
517 size_t nread;
518 char byte;
520 /* Read one byte at a time to avoid a race condition. Wait at most one
521 second before looping to ensure continuous pgrsUpdates. */
522 result = Curl_conn_recv(data, cf->sockindex, &byte, 1, &nread);
523 if(result == CURLE_AGAIN)
524 /* socket buffer drained, return */
525 return CURLE_OK;
527 if(!result)
528 result = Curl_pgrsUpdate(data);
530 if(result) {
531 ts->keepon = KEEPON_DONE;
532 break;
533 }
535 if(!nread) {
536 if(ts->maybe_folded) {
537 /* EOF right after LF: finalize the pending header line. */
538 result = single_header(cf, data, ts);
539 if(result)
540 return result;
541 ts->maybe_folded = FALSE;
542 }
543 if(data->set.proxyauth && data->state.authproxy.avail &&
544 data->req.hd_proxy_auth) {
545 /* proxy auth was requested and there was proxy auth available,
546 then deem this as "mere" proxy disconnect */
547 ts->close_connection = TRUE;
548 infof(data, "Proxy CONNECT connection closed");
549 }
550 else {
551 error = SELECT_ERROR;
552 failf(data, "Proxy CONNECT aborted");
553 }
554 ts->keepon = KEEPON_DONE;
555 break;
556 }
558 if(ts->keepon == KEEPON_IGNORE) {
559 /* This means we are currently ignoring a response-body */
560 if(ts->chunked_encoding) {
561 /* chunked-encoded body, so we need to do the chunked dance
562 properly to know when the end of the body is reached */
563 size_t consumed = 0;
565 /* now parse the chunked piece of data so that we can
566 properly tell when the stream ends */
567 result = Curl_httpchunk_read(data, &ts->ch, &byte, 1, &consumed);
568 if(result)
569 return result;
570 if(Curl_httpchunk_is_done(data, &ts->ch)) {
571 /* we are done reading chunks! */
572 infof(data, "chunk reading DONE");
573 ts->keepon = KEEPON_DONE;
574 }
575 }
576 else if(ts->cl) {
577 /* A Content-Length based body: count down the counter
578 and make sure to break out of the loop when we are done! */
579 ts->cl--;
580 if(ts->cl <= 0) {
581 ts->keepon = KEEPON_DONE;
582 break;
583 }
584 }
585 continue;
586 }
588 if(ts->maybe_folded) {
589 if(ISBLANK(byte)) {
590 Curl_http_to_fold(&ts->rcvbuf);
591 ts->leading_unfold = TRUE;
592 }
593 else {
594 result = single_header(cf, data, ts);
595 if(result)
596 return result;
597 /* now handle the new byte */
598 }
599 ts->maybe_folded = FALSE;
600 }
602 if(ts->leading_unfold) {
603 if(ISBLANK(byte))
604 /* skip a bit brother */
605 continue;
606 /* non-blank, insert a space then continue the unfolding */
607 if(curlx_dyn_addn(&ts->rcvbuf, " ", 1)) {
608 failf(data, "CONNECT response too large");
609 return CURLE_RECV_ERROR;
610 }
611 ts->leading_unfold = FALSE;
612 }
613 if(curlx_dyn_addn(&ts->rcvbuf, &byte, 1)) {
614 failf(data, "CONNECT response too large");
615 return CURLE_RECV_ERROR;
616 }
618 /* if this is not the end of a header line then continue */
619 if(byte != 0x0a)
620 continue;
621 else {
622 const char *linep = curlx_dyn_ptr(&ts->rcvbuf);
623 size_t hlen = curlx_dyn_len(&ts->rcvbuf);
624 if(hlen && ISNEWLINE(linep[0])) {
625 /* end of headers */
626 result = single_header(cf, data, ts);
627 if(result)
628 return result;
629 }
630 else
631 ts->maybe_folded = TRUE;
632 }
634 if(result)
635 return result;
636 } /* while there is buffer left and loop is requested */
638 if(error)
639 result = CURLE_RECV_ERROR;
640 *done = (ts->keepon == KEEPON_DONE);
641 if(!result && *done &&
642 data->info.httpproxycode / 100 != 2 &&
643 !(h1_proxy_is_udp(cf) && data->info.httpproxycode == 101)) {
644 /* Deal with the possibly already received authenticate
645 headers. 'newurl' is set to a new URL if we must loop. */
646 result = Curl_http_auth_act(data);
647 }
648 return result;
649}
651static CURLcode H1_CONNECT(struct Curl_cfilter *cf,
652 struct Curl_easy *data,
653 struct h1_tunnel_state *ts)
654{
655 struct connectdata *conn = cf->conn;
656 CURLcode result;
657 bool done;
659 if(tunnel_is_established(ts))
660 return CURLE_OK;
661 if(tunnel_is_failed(ts))
662 return CURLE_RECV_ERROR; /* Need a cfilter close and new bootstrap */
664 do {
666 if(Curl_timeleft_ms(data) < 0) {
667 failf(data, "Proxy CONNECT aborted due to timeout");
668 result = CURLE_OPERATION_TIMEDOUT;
669 goto out;
670 }
672 switch(ts->tunnel_state) {
673 case H1_TUNNEL_INIT:
674 /* Prepare the CONNECT request and make a first attempt to send. */
675 CURL_TRC_CF(data, cf, "CONNECT start");
676 result = start_CONNECT(cf, data, ts);
677 if(result)
678 goto out;
679 h1_tunnel_go_state(cf, ts, H1_TUNNEL_CONNECT, data);
680 FALLTHROUGH();
682 case H1_TUNNEL_CONNECT:
683 /* see that the request is completely sent */
684 CURL_TRC_CF(data, cf, "CONNECT send");
685 result = send_CONNECT(cf, data, ts, &done);
686 if(result || !done)
687 goto out;
688 h1_tunnel_go_state(cf, ts, H1_TUNNEL_RECEIVE, data);
689 FALLTHROUGH();
691 case H1_TUNNEL_RECEIVE:
692 /* read what is there */
693 CURL_TRC_CF(data, cf, "CONNECT receive");
694 result = recv_CONNECT_resp(cf, data, ts, &done);
695 if(result)
696 CURL_TRC_CF(data, cf, "error receiving CONNECT response: %d",
697 (int)result);
698 if(!result)
699 result = Curl_pgrsUpdate(data);
700 /* error or not complete yet. return for more multi-multi */
701 if(result || !done)
702 goto out;
703 /* got it */
704 h1_tunnel_go_state(cf, ts, H1_TUNNEL_RESPONSE, data);
705 FALLTHROUGH();
707 case H1_TUNNEL_RESPONSE:
708 CURL_TRC_CF(data, cf, "CONNECT response");
709 if(data->req.newurl) {
710 /* not the "final" response, we need to do a follow up request.
711 * If the other side indicated a connection close, or if someone
712 * else told us to close this connection, do so now.
713 */
714 Curl_req_soft_reset(&data->req, data);
715 if(ts->close_connection || conn->bits.close) {
716 /* Close this filter and the sub-chain, re-connect the
717 * sub-chain and continue. Closing this filter will
718 * reset our tunnel state. To avoid recursion, we return
719 * and expect to be called again.
720 */
721 CURL_TRC_CF(data, cf, "CONNECT need to close+open");
722 infof(data, "Connect me again please");
723 return CURLE_AGAIN;
724 }
725 else {
726 /* staying on this connection, reset state */
727 h1_tunnel_go_state(cf, ts, H1_TUNNEL_INIT, data);
728 }
729 }
730 break;
732 default:
733 break;
734 }
736 } while(data->req.newurl);
738 DEBUGASSERT(ts->tunnel_state == H1_TUNNEL_RESPONSE);
739 if(h1_proxy_is_udp(cf)) {
740 /* RFC 9298: Accept 101 Upgrade for HTTP/1.1 and
741 * 2xx responses for HTTP/2 and HTTP/3 proxies. */
742 if(data->info.httpproxycode / 100 != 2 &&
743 data->info.httpproxycode != 101) {
744 curlx_safefree(data->req.newurl);
745 h1_tunnel_go_state(cf, ts, H1_TUNNEL_FAILED, data);
746 failf(data, "CONNECT-UDP tunnel failed, response %d",
747 data->req.httpcode);
748 return CURLE_COULDNT_CONNECT;
749 }
750 }
751 else {
752 if(data->info.httpproxycode / 100 != 2) {
753 /* a non-2xx response and we have no next URL to try. */
754 curlx_safefree(data->req.newurl);
755 h1_tunnel_go_state(cf, ts, H1_TUNNEL_FAILED, data);
756 failf(data, "CONNECT tunnel failed, response %d", data->req.httpcode);
757 return CURLE_COULDNT_CONNECT;
758 }
759 }
760 /* 2xx response, SUCCESS! */
761 /* 101 Switching Protocol for CONNECT-UDP */
762 h1_tunnel_go_state(cf, ts, H1_TUNNEL_ESTABLISHED, data);
763 infof(data, "CONNECT%s tunnel established, response %d",
764 h1_proxy_is_udp(cf) ? "-UDP" : "", data->info.httpproxycode);
765 result = CURLE_OK;
767out:
768 if(result)
769 h1_tunnel_go_state(cf, ts, H1_TUNNEL_FAILED, data);
770 return result;
771}
773static CURLcode cf_h1_proxy_connect(struct Curl_cfilter *cf,
774 struct Curl_easy *data,
775 bool *done)
776{
777 CURLcode result;
778 struct cf_h1_proxy_ctx *pctx = cf->ctx;
779 struct h1_tunnel_state *ts = pctx->ts;
781 if(cf->connected) {
782 *done = TRUE;
783 return CURLE_OK;
784 }
786 CURL_TRC_CF(data, cf, "connect");
787 result = cf->next->cft->do_connect(cf->next, data, done);
788 if(result || !*done)
789 return result;
791 *done = FALSE;
792 if(!ts) {
793 result = tunnel_init(cf, data, &ts);
794 if(result)
795 return result;
796 pctx->ts = ts;
797 }
799 /* We want "seamless" operations through HTTP proxy tunnel */
801 result = H1_CONNECT(cf, data, ts);
802 if(result)
803 goto out;
804 curlx_safefree(data->req.hd_proxy_auth);
806out:
807 *done = (result == CURLE_OK) && tunnel_is_established(pctx->ts);
808 if(*done) {
809 cf->connected = TRUE;
810 /* The real request will follow the CONNECT, reset request partially */
811 Curl_req_soft_reset(&data->req, data);
812 Curl_client_reset(data);
813 Curl_pgrsReset(data);
814 cf_tunnel_free(cf, data);
815 }
816 return result;
817}
819static CURLcode cf_h1_proxy_adjust_pollset(struct Curl_cfilter *cf,
820 struct Curl_easy *data,
821 struct easy_pollset *ps)
822{
823 struct cf_h1_proxy_ctx *pctx = cf->ctx;
824 struct h1_tunnel_state *ts = pctx->ts;
825 CURLcode result = CURLE_OK;
827 if(!cf->connected) {
828 /* If we are not connected, but the filter "below" is
829 * and not waiting on something, we are tunneling. */
830 curl_socket_t sock = Curl_conn_cf_get_socket(cf, data);
831 if(ts) {
832 /* when we have sent a CONNECT to a proxy, we should rather either
833 wait for the socket to become readable to be able to get the
834 response headers or if we are still sending the request, wait
835 for write. */
836 if(tunnel_want_send(ts))
837 result = Curl_pollset_set_out_only(data, ps, sock);
838 else
839 result = Curl_pollset_set_in_only(data, ps, sock);
840 }
841 else
842 result = Curl_pollset_set_out_only(data, ps, sock);
843 }
844 else {
845 if(cf->next)
846 result = cf->next->cft->adjust_pollset(cf->next, data, ps);
847 }
848 return result;
849}
851static bool cf_h1_proxy_data_pending(struct Curl_cfilter *cf,
852 const struct Curl_easy *data)
853{
854 return cf->next ? cf->next->cft->has_data_pending(cf->next, data) : FALSE;
855}
857static void cf_h1_proxy_destroy(struct Curl_cfilter *cf,
858 struct Curl_easy *data)
859{
860 struct cf_h1_proxy_ctx *pctx = cf->ctx;
862 CURL_TRC_CF(data, cf, "destroy");
863 if(pctx) {
864 cf_tunnel_free(cf, data);
865 Curl_peer_unlink(&pctx->peer);
866 curlx_safefree(cf->ctx);
867 }
868}
870static CURLcode cf_h1_proxy_query(struct Curl_cfilter *cf,
871 struct Curl_easy *data,
872 int query, int *pres1, void *pres2)
873{
874 struct cf_h1_proxy_ctx *pctx = cf->ctx;
875 struct h1_tunnel_state *ts = pctx ? pctx->ts : NULL;
876 switch(query) {
877 case CF_QUERY_HOST_PORT:
878 if(!ts || !ts->dest)
879 break;
880 *pres1 = (int)ts->dest->port;
881 *((const char **)pres2) = ts->dest->hostname;
882 return CURLE_OK;
883 case CF_QUERY_ALPN_NEGOTIATED: {
884 const char **palpn = pres2;
885 DEBUGASSERT(palpn);
886 *palpn = NULL;
887 return CURLE_OK;
888 }
889 default:
890 break;
891 }
892 return cf->next ?
893 cf->next->cft->query(cf->next, data, query, pres1, pres2) :
894 CURLE_UNKNOWN_OPTION;
895}
897struct Curl_cftype Curl_cft_h1_proxy = {
898 "H1-PROXY",
899 CF_TYPE_IP_CONNECT | CF_TYPE_PROXY,
900 0,
901 cf_h1_proxy_destroy,
902 cf_h1_proxy_connect,
903 Curl_cf_def_shutdown,
904 cf_h1_proxy_adjust_pollset,
905 cf_h1_proxy_data_pending,
906 Curl_cf_def_send,
907 Curl_cf_def_recv,
908 Curl_cf_def_cntrl,
909 Curl_cf_def_conn_is_alive,
910 Curl_cf_def_conn_keep_alive,
911 cf_h1_proxy_query,
912};
914CURLcode Curl_cf_h1_proxy_insert_after(struct Curl_cfilter *cf_at,
915 struct Curl_easy *data,
916 struct Curl_peer *peer,
917 struct Curl_peer *dest,
918 int httpversion,
919 bool udp_tunnel)
920{
921 struct Curl_cfilter *cf;
922 struct cf_h1_proxy_ctx *pctx;
923 struct h1_tunnel_state *ts;
924 CURLcode result;
926 (void)data;
927 if(!dest)
928 return CURLE_FAILED_INIT;
929 if((httpversion < 10) || (httpversion >= 20))
930 return CURLE_FAILED_INIT;
932 ts = curlx_calloc(1, sizeof(*ts));
933 if(!ts) {
934 result = CURLE_OUT_OF_MEMORY;
935 goto out;
936 }
937 Curl_peer_link(&ts->dest, dest);
938 ts->udp_tunnel = udp_tunnel;
939 ts->httpversion = httpversion;
940 curlx_dyn_init(&ts->rcvbuf, DYN_PROXY_CONNECT_HEADERS);
941 curlx_dyn_init(&ts->request_data, DYN_HTTP_REQUEST);
942 Curl_httpchunk_init(data, &ts->ch, TRUE, TRUE);
944 pctx = curlx_calloc(1, sizeof(*pctx));
945 if(!pctx) {
946 result = CURLE_OUT_OF_MEMORY;
947 goto out;
948 }
949 Curl_peer_link(&pctx->peer, peer);
950 pctx->ts = ts;
951 result = Curl_cf_create(&cf, &Curl_cft_h1_proxy, pctx);
952 if(result) {
953 Curl_peer_unlink(&pctx->peer);
954 curlx_free(pctx);
955 goto out;
956 }
957 ts = NULL;
958 Curl_conn_cf_insert_after(cf_at, cf);
960out:
961 tunnel_free(ts, data);
962 return result;
963}
965#endif /* !CURL_DISABLE_PROXY && !CURL_DISABLE_HTTP */
Diagnostics4 git calls · 34 ms
CallStatusSizeTime034 ms
discover200152 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 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 · hit3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=149 git.response.cache=HIT git.response.duration_ms=3 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 · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=293470 git.response.cache=HIT git.response.duration_ms=3 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
fetch2008.3 KiB · hit28 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=27 git.response.bytes=8507 git.response.cache=HIT git.response.duration_ms=28 git.fetch.wants=bf0cc4c92db5c9132fb29a7f89fe0c17a2c15ce3 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=8507 git.pkt.payload.max=8486 git.sideband.band1.bytes=8485 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
- a5e2da3d0b6114c2
- Request
- f9409714…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format