Artifactspublic-v0

curl

master
ply4 git calls45 ms291 KiB3 cachedprotocol v2
507 lines15 KiBRaw
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#include "urldata.h"
27#include "cfilters.h"
28#include "cf-haproxy.h"
29#include "cf-ip-happy.h"
30#include "cf-setup.h"
31#include "curl_trc.h"
32#include "connect.h"
33#include "http_proxy.h"
34#include "socks.h"
35#include "vquic/cf-capsule.h"
36#include "vquic/vquic.h"
37#include "vtls/vtls.h"
40typedef enum {
41 CF_SETUP_INIT,
42 CF_SETUP_CNNCT_EYEBALLS,
43 CF_SETUP_CNNCT_SOCKS,
44 CF_SETUP_CNNCT_HTTP_PROXY,
45 CF_SETUP_CNNCT_HAPROXY,
46 CF_SETUP_CNNCT_SSL,
47 CF_SETUP_CNNCT_PROTOCOL,
48 CF_SETUP_DONE
49} cf_setup_state;
51struct cf_setup_ctx {
52 cf_setup_state state;
53 int ssl_mode;
54 uint8_t transport;
55 uint8_t retry_count;
56};
58#ifndef CURL_DISABLE_PROXY
60static CURLcode cf_setup_add_haproxy(struct Curl_cfilter *cf,
61 struct Curl_easy *data)
63 struct cf_setup_ctx *ctx = cf->ctx;
64 CURLcode result = CURLE_OK;
66 if(ctx->state < CF_SETUP_CNNCT_HAPROXY) {
67 if(data->set.haproxyprotocol) {
68 if(ctx->transport == TRNSPRT_QUIC) {
69 failf(data, "haproxy protocol does not support QUIC");
70 return CURLE_UNSUPPORTED_PROTOCOL;
71 }
72 result = Curl_cf_haproxy_insert_after(cf, data);
73 if(result) {
74 CURL_TRC_CF(data, cf, "adding HAPROXY filter failed -> %d",
75 (int)result);
76 return result;
77 }
78 CURL_TRC_CF(data, cf, "added HAPROXY filter");
79 }
80 ctx->state = CF_SETUP_CNNCT_HAPROXY;
81 }
82 return result;
85static CURLcode cf_setup_add_socks(struct Curl_cfilter *cf,
86 struct Curl_easy *data)
88 struct cf_setup_ctx *ctx = cf->ctx;
89 CURLcode result = CURLE_OK;
90 if(ctx->state < CF_SETUP_CNNCT_SOCKS && cf->conn->socks_proxy.peer) {
91 /* Add a SOCKS proxy to go through `first_peer` to `second_peer`*/
92 struct Curl_peer *second_peer;
94 if(cf->conn->http_proxy.peer)
95 second_peer = cf->conn->http_proxy.peer;
96 else
97 second_peer = Curl_conn_get_destination(cf->conn, cf->sockindex);
98 if(!second_peer)
99 return CURLE_FAILED_INIT;
101 result = Curl_cf_socks_proxy_insert_after(
102 cf, data, second_peer, cf->conn->ip_version,
103 cf->conn->socks_proxy.proxytype,
104 cf->conn->socks_proxy.creds);
105 if(result) {
106 CURL_TRC_CF(data, cf, "adding SOCKS filter failed -> %d", (int)result);
107 return result;
108 }
110 CURL_TRC_CF(data, cf, "added SOCKS filter to %s:%u",
111 second_peer->hostname, second_peer->port);
112 ctx->state = CF_SETUP_CNNCT_SOCKS;
113 }
114 return result;
117#ifndef CURL_DISABLE_HTTP
118static CURLcode cf_setup_add_http_proxy(struct Curl_cfilter *cf,
119 struct Curl_easy *data)
121 struct cf_setup_ctx *ctx = cf->ctx;
122 CURLcode result = CURLE_OK;
124 if(ctx->state < CF_SETUP_CNNCT_HTTP_PROXY &&
125 cf->conn->http_proxy.peer && !cf->conn->bits.origin_is_proxy) {
126 struct Curl_peer *peer = cf->conn->http_proxy.peer;
127 struct Curl_peer *tunnel_peer =
128 Curl_conn_get_destination(cf->conn, cf->sockindex);
130#ifdef USE_SSL
131 if(CURL_PROXY_IS_HTTPS(cf->conn->http_proxy.proxytype) &&
132 !Curl_conn_is_ssl(cf->conn, cf->sockindex)) {
133 result = Curl_cf_ssl_proxy_insert_after(
134 cf, data, cf->conn->http_proxy.peer);
135 if(result) {
136 CURL_TRC_CF(data, cf, "adding SSL filter for HTTP proxy failed -> %d",
137 (int)result);
138 return result;
139 }
140 CURL_TRC_CF(data, cf, "added SSL filter for HTTP proxy");
141 }
142#endif /* USE_SSL */
144 result = Curl_cf_http_proxy_insert_after(
145 cf, data, peer, tunnel_peer,
146 ctx->transport, cf->conn->http_proxy.proxytype);
147 if(result) {
148 CURL_TRC_CF(data, cf, "adding HTTP proxy tunnel filter failed -> %d",
149 (int)result);
150 return result;
151 }
152 CURL_TRC_CF(data, cf, "added HTTP proxy tunnel filter");
153 ctx->state = CF_SETUP_CNNCT_HTTP_PROXY;
154 }
155 return result;
157#endif /* !CURL_DISABLE_HTTP */
158#endif /* CURL_DISABLE_PROXY */
160/* Get the origin curl connects its socket to.
161 * Can be origin or the first proxy. */
162static struct Curl_peer *conn_get_first_origin(struct connectdata *conn,
163 int8_t sockindex)
165#ifndef CURL_DISABLE_PROXY
166 if(conn->socks_proxy.peer)
167 return conn->socks_proxy.peer;
168 if(conn->http_proxy.peer)
169 return conn->http_proxy.peer;
170#endif
171 return (sockindex == SECONDARYSOCKET) ? conn->origin2 : conn->origin;
174static CURLcode cf_setup_add_ip_happy(struct Curl_cfilter *cf,
175 struct Curl_easy *data)
177 struct cf_setup_ctx *ctx = cf->ctx;
178 CURLcode result = CURLE_OK;
180 if(ctx->state < CF_SETUP_CNNCT_EYEBALLS) {
181 /* What is the first hop we directly connect to and what transport
182 * do we use for it? Only on the first hop we can do Happy Eyeballs.
183 * first_origin and first_peer differ on --connect-to. */
184 struct Curl_peer *first_origin =
185 conn_get_first_origin(cf->conn, cf->sockindex);
186 struct Curl_peer *first_peer =
187 Curl_conn_get_first_peer(cf->conn, cf->sockindex);
188 struct Curl_peer *tunnel_peer = NULL;
189 uint8_t first_transport = ctx->transport;
191 if(!first_peer)
192 return CURLE_FAILED_INIT;
194#if !defined(CURL_DISABLE_PROXY) && !defined(CURL_DISABLE_HTTP)
195 if(cf->conn->http_proxy.peer && !cf->conn->bits.origin_is_proxy) {
196 first_transport =
197 Curl_http_proxy_transport(cf->conn->http_proxy.proxytype);
198 tunnel_peer = Curl_conn_get_destination(cf->conn, cf->sockindex);
199 if((first_transport == TRNSPRT_QUIC) && cf->conn->socks_proxy.peer) {
200 failf(data, "HTTP/3 proxy not possible via SOCKS");
201 return CURLE_UNSUPPORTED_PROTOCOL;
202 }
203 }
204#endif /* !CURL_DISABLE_PROXY && !CURL_DISABLE_HTTP */
206 result = Curl_cf_ip_happy_insert_after(cf, data, first_origin, first_peer,
207 first_transport,
208 tunnel_peer, ctx->transport);
209 if(result) {
210 CURL_TRC_CF(data, cf, "adding happy eyeballs failed -> %d", (int)result);
211 return result;
212 }
214 if(tunnel_peer && (first_transport == TRNSPRT_QUIC)) {
215 CURL_TRC_CF(data, cf, "happy eyeballing to HTTP/3 proxy %s:%u",
216 first_peer->hostname, first_peer->port);
217 ctx->state = CF_SETUP_CNNCT_HTTP_PROXY;
218 }
219 else {
220 CURL_TRC_CF(data, cf, "happy eyeballing to %s %s:%u",
221 tunnel_peer ? "proxy" : "origin",
222 first_peer->hostname, first_peer->port);
223 ctx->state = CF_SETUP_CNNCT_EYEBALLS;
224 }
225 }
226 return result;
229static CURLcode cf_setup_add_origin_filters(struct Curl_cfilter *cf,
230 struct Curl_easy *data)
232 struct cf_setup_ctx *ctx = cf->ctx;
233 CURLcode result = CURLE_OK;
235 (void)data; /* not used in all builds */
236 if(ctx->state < CF_SETUP_CNNCT_SSL) {
237#if !defined(CURL_DISABLE_HTTP) && defined(USE_HTTP3) && \
238 !defined(CURL_DISABLE_PROXY)
240 /* Wanting QUIC with an HTTP tunneling filter, we now need to add
241 * the QUIC filter on top. Without tunneling, this has already
242 * happened in the Happy Eyeball filter. */
243 if(ctx->transport == TRNSPRT_QUIC &&
244 cf->conn->http_proxy.peer && !cf->conn->bits.origin_is_proxy) {
245 struct Curl_peer *origin = Curl_conn_get_origin(cf->conn, cf->sockindex);
246 struct Curl_peer *peer =
247 Curl_conn_get_destination(cf->conn, cf->sockindex);
249 result = Curl_cf_capsule_insert_after(cf, data);
250 if(result) {
251 CURL_TRC_CF(data, cf, "adding capsule filter failed -> %d",
252 (int)result);
253 return result;
254 }
255 result = Curl_cf_quic_insert_after(cf, data, origin, peer);
256 if(result) {
257 CURL_TRC_CF(data, cf, "adding QUIC filter failed -> %d", (int)result);
258 return result;
259 }
260 CURL_TRC_CF(data, cf, "added QUIC filter for origin");
261 }
262 else
263#endif /* !CURL_DISABLE_HTTP && USE_HTTP3 && CURL_DISABLE_PROXY */
264#ifdef USE_SSL
265 if((ctx->ssl_mode == CURL_CF_SSL_ENABLE ||
266 (ctx->ssl_mode != CURL_CF_SSL_DISABLE &&
267 cf->conn->scheme->flags & PROTOPT_SSL)) && /* we want SSL */
268 !Curl_conn_is_ssl(cf->conn, cf->sockindex)) { /* it is missing */
270#ifndef CURL_DISABLE_PROXY
271 if(cf->conn->bits.origin_is_proxy) {
272 result = Curl_cf_ssl_proxy_insert_after(cf, data, cf->conn->origin);
273 }
274 else
275#endif
276 {
277 /* FTP is a bitch. Wherever we really connect to on the DATA
278 * (secondary) connection, many servers require TLS sessions reuse
279 * to prove they are talking to the same client.
280 * For the TLS session lookup to work, we need to instantiate
281 * the SSL filter with the same peers as FIRSTSOCKET. See #22225
282 * Meaning: cf->sockindex does not matter here. */
283 result = Curl_cf_ssl_insert_after(cf, data,
284 Curl_conn_get_origin(cf->conn, FIRSTSOCKET),
285 Curl_conn_get_destination(cf->conn, FIRSTSOCKET));
286 }
287 if(result) {
288 CURL_TRC_CF(data, cf, "adding SSL filter for origin failed -> %d",
289 (int)result);
290 return result;
291 }
292 CURL_TRC_CF(data, cf, "added SSL filter for origin");
293 }
294#endif /* USE_SSL */
295 ctx->state = CF_SETUP_CNNCT_SSL;
296 }
297 return result;
300static CURLcode cf_setup_add_protocol_filters(struct Curl_cfilter *cf,
301 struct Curl_easy *data)
303 struct cf_setup_ctx *ctx = cf->ctx;
304 CURLcode result = CURLE_OK;
306 (void)data; /* not used in all builds */
307 if(ctx->state < CF_SETUP_CNNCT_PROTOCOL) {
308 if(cf->conn->scheme->run->setup_filters) {
309 result = cf->conn->scheme->run->setup_filters(data, cf);
310 if(result) {
311 CURL_TRC_CF(data, cf, "adding protocol filters failed -> %d",
312 (int)result);
313 return result;
314 }
315 }
316 ctx->state = CF_SETUP_CNNCT_PROTOCOL;
317 }
318 return result;
321static CURLcode cf_setup_connect_steps(struct Curl_cfilter *cf,
322 struct Curl_easy *data,
323 bool *done)
325 struct cf_setup_ctx *ctx = cf->ctx;
326 CURLcode result = CURLE_OK;
328 if(cf->connected) {
329 *done = TRUE;
330 return CURLE_OK;
331 }
333 /* connect current sub-chain */
334connect_sub_chain:
335 VERBOSE(Curl_conn_trc_filters(data, cf->sockindex, "cf_setup_connect"));
337 if(cf->next && !cf->next->connected) {
338 result = Curl_conn_cf_connect(cf->next, data, done);
339 if(result || !*done)
340 return result;
341 }
343 result = cf_setup_add_ip_happy(cf, data);
344 if(result)
345 return result;
346 if(!cf->next || !cf->next->connected)
347 goto connect_sub_chain;
349#ifndef CURL_DISABLE_PROXY
350 result = cf_setup_add_socks(cf, data);
351 if(result)
352 return result;
353 if(!cf->next || !cf->next->connected)
354 goto connect_sub_chain;
356#ifndef CURL_DISABLE_HTTP
357 result = cf_setup_add_http_proxy(cf, data);
358 if(result)
359 return result;
360 if(!cf->next || !cf->next->connected)
361 goto connect_sub_chain;
362#endif /* !CURL_DISABLE_HTTP */
364 result = cf_setup_add_haproxy(cf, data);
365 if(result)
366 return result;
367 if(!cf->next || !cf->next->connected)
368 goto connect_sub_chain;
369#endif /* !CURL_DISABLE_PROXY */
371 result = cf_setup_add_origin_filters(cf, data);
372 if(result)
373 return result;
374 if(!cf->next || !cf->next->connected)
375 goto connect_sub_chain;
377 result = cf_setup_add_protocol_filters(cf, data);
378 if(result)
379 return result;
380 if(!cf->next || !cf->next->connected)
381 goto connect_sub_chain;
383 ctx->state = CF_SETUP_DONE;
384 cf->connected = TRUE;
385 *done = TRUE;
386 return CURLE_OK;
389static CURLcode cf_setup_connect(struct Curl_cfilter *cf,
390 struct Curl_easy *data,
391 bool *done)
393 struct cf_setup_ctx *ctx = cf->ctx;
394 CURLcode result;
396 /* In some situations, a server/proxy may close the connection and
397 * we need to connect again (HTTP/1.x proxy auth, for example).
398 * We used to close the filters and reuse them for another attempt,
399 * however that complicates filter code and it is simpler to tear them
400 * all down and start over. */
401retry:
402 result = cf_setup_connect_steps(cf, data, done);
404 if(result == CURLE_AGAIN) {
405 ++ctx->retry_count;
406 if(ctx->retry_count > 5) /* arbitrary limit, better just timeout? */
407 return CURLE_COULDNT_CONNECT;
409 CURL_TRC_CF(data, cf, "retrying connect, %d. time", ctx->retry_count);
410 Curl_conn_cf_discard_chain(&cf->next, data);
411 ctx->state = CF_SETUP_INIT;
412 goto retry;
413 }
414 return result;
417static void cf_setup_destroy(struct Curl_cfilter *cf, struct Curl_easy *data)
419 struct cf_setup_ctx *ctx = cf->ctx;
421 CURL_TRC_CF(data, cf, "destroy");
422 curlx_safefree(ctx);
425struct Curl_cftype Curl_cft_setup = {
426 "SETUP",
427 CF_TYPE_SETUP,
428 CURL_LOG_LVL_NONE,
429 cf_setup_destroy,
430 cf_setup_connect,
431 Curl_cf_def_shutdown,
432 Curl_cf_def_adjust_pollset,
433 Curl_cf_def_data_pending,
434 Curl_cf_def_send,
435 Curl_cf_def_recv,
436 Curl_cf_def_cntrl,
437 Curl_cf_def_conn_is_alive,
438 Curl_cf_def_conn_keep_alive,
439 Curl_cf_def_query,
440};
442static CURLcode cf_setup_create(struct Curl_cfilter **pcf,
443 struct Curl_easy *data,
444 uint8_t transport,
445 int ssl_mode)
447 struct Curl_cfilter *cf = NULL;
448 struct cf_setup_ctx *ctx;
449 CURLcode result = CURLE_OK;
451 (void)data;
452 ctx = curlx_calloc(1, sizeof(*ctx));
453 if(!ctx) {
454 result = CURLE_OUT_OF_MEMORY;
455 goto out;
456 }
457 ctx->state = CF_SETUP_INIT;
458 ctx->ssl_mode = ssl_mode;
459 ctx->transport = transport;
461 result = Curl_cf_create(&cf, &Curl_cft_setup, ctx);
462 if(result)
463 goto out;
464 ctx = NULL;
466out:
467 *pcf = result ? NULL : cf;
468 if(ctx) {
469 curlx_free(ctx);
470 }
471 return result;
474CURLcode Curl_cf_setup_add(struct Curl_easy *data,
475 struct connectdata *conn,
476 int8_t sockindex,
477 uint8_t transport,
478 int ssl_mode)
480 struct Curl_cfilter *cf;
481 CURLcode result = CURLE_OK;
483 DEBUGASSERT(data);
484 result = cf_setup_create(&cf, data, transport, ssl_mode);
485 if(result)
486 goto out;
487 Curl_conn_cf_add(data, conn, sockindex, cf);
488out:
489 return result;
492CURLcode Curl_cf_setup_insert_after(struct Curl_cfilter *cf_at,
493 struct Curl_easy *data,
494 uint8_t transport,
495 int ssl_mode)
497 struct Curl_cfilter *cf;
498 CURLcode result;
500 DEBUGASSERT(data);
501 result = cf_setup_create(&cf, data, transport, ssl_mode);
502 if(result)
503 goto out;
504 Curl_conn_cf_insert_after(cf_at, cf);
505out:
506 return result;
Diagnostics4 git calls · 45 ms
CallStatusSizeTime045 ms
discover200152 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
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 · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=41
git.response.bytes=293470
git.response.cache=HIT
git.response.duration_ms=2
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=86
Open pack map
fetch2004.0 KiB · hit41 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=4094
git.response.cache=HIT
git.response.duration_ms=41
git.fetch.wants=a5dc8edfce5e37833eecb5a5fd43cc1a916552c0
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=4094
git.pkt.payload.max=4073
git.sideband.band1.bytes=4072
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
a5e289cf7da8bd4a
Request
5dc2fca8…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format