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 "first.h"
26#if defined(HAVE_SOCKETPAIR) && defined(AF_UNIX)
28/* A response much like what a large web site sends, in pieces since C89
29 compilers need not support string literals longer than 509 bytes */
30static const char *const resp_typical[] = {
31 "HTTP/1.1 200 OK\r\n"
32 "Date: Thu, 23 Oct 2026 10:00:00 GMT\r\n"
33 "Content-Type: text/html; charset=utf-8\r\n"
34 "Content-Length: 2\r\n"
35 "Vary: X-PJAX, X-PJAX-Container, Turbo-Visit, Turbo-Frame, "
36 "Accept-Encoding, Accept, X-Requested-With\r\n"
37 "ETag: W/\"9b2f6c1d0e8a4b7c3f5e2d1a0b9c8d7e\"\r\n"
38 "Cache-Control: max-age=0, private, must-revalidate\r\n"
39 "Strict-Transport-Security: max-age=31536000; includeSubdomains; "
40 "preload\r\n",
41 "X-Frame-Options: deny\r\n"
42 "X-Content-Type-Options: nosniff\r\n"
43 "X-XSS-Protection: 0\r\n"
44 "Referrer-Policy: origin-when-cross-origin, "
45 "strict-origin-when-cross-origin\r\n"
46 "Content-Security-Policy: default-src 'none'; base-uri 'self'; "
47 "child-src 'self' blob:; connect-src 'self' api.example.com "
48 "*.example.net wss://live.example.com; font-src assets.example.com; ",
49 "form-action 'self' example.com; frame-ancestors 'none'; img-src 'self' "
50 "data: blob: assets.example.com *.examplecdn.com; manifest-src 'self'; "
51 "media-src example.com; script-src assets.example.com; style-src "
52 "'unsafe-inline' assets.example.com; upgrade-insecure-requests; "
53 "worker-src assets.example.com blob:\r\n",
54 "Set-Cookie: _session=c2Vzc2lvbi12YWx1ZS1mb3ItdGVzdGluZy1vbmx5%3D"
55 "--1a2b3c4d5e6f; path=/; secure; HttpOnly; SameSite=Lax\r\n"
56 "Set-Cookie: _visit=GH1.1.123456789.1761210000; Path=/; "
57 "Domain=example.com; Expires=Fri, 23 Oct 2027 10:00:00 GMT; Secure; "
58 "SameSite=Lax\r\n"
59 "Set-Cookie: logged_in=no; Path=/; Domain=example.com; "
60 "Expires=Fri, 23 Oct 2027 10:00:00 GMT; HttpOnly; Secure; "
61 "SameSite=Lax\r\n",
62 "Accept-Ranges: bytes\r\n"
63 "Via: 1.1 varnish\r\n"
64 "Age: 0\r\n"
65 "X-Served-By: cache-ams21000-AMS\r\n"
66 "X-Cache: MISS\r\n"
67 "X-Cache-Hits: 0\r\n"
68 "X-Timer: S1761210000.123456,VS0,VE120\r\n"
69 "Server-Timing: total;dur=118, db;dur=23, view;dur=41\r\n"
70 "X-Request-Id: 7f3c2a1b-9d8e-4f6a-b5c4-3d2e1f0a9b8c\r\n"
71 "Alt-Svc: h3=\":443\"; ma=86400\r\n"
72 "Server: perf\r\n"
73 "\r\n"
74 "ok"
75};
77static const char resp_small[] =
78 "HTTP/1.1 200 OK\r\n"
79 "Content-Type: text/plain\r\n"
80 "Date: Thu, 23 Oct 2026 10:00:00 GMT\r\n"
81 "Server: perf\r\n"
82 "Cache-Control: max-age=60\r\n"
83 "Content-Length: 2\r\n"
84 "\r\n"
85 "ok";
87#define MANY_HEADERS 200
89struct respctx {
90 curl_socket_t sock[2]; /* [0] is curl's end, [1] is the "server" end */
91 const char *resp;
92 size_t resplen;
93 bool handed_out;
94};
96static curl_socket_t rh_opensocket(void *clientp, curlsocktype purpose,
97 struct curl_sockaddr *address)
98{
99 struct respctx *ctx = clientp;
100 (void)purpose;
101 (void)address;
102 if(ctx->handed_out)
103 return CURL_SOCKET_BAD; /* the one connection should be reused */
104 ctx->handed_out = TRUE;
105 return ctx->sock[0];
106}
108static int rh_sockopt(void *clientp, curl_socket_t curlfd,
109 curlsocktype purpose)
110{
111 (void)clientp;
112 (void)curlfd;
113 (void)purpose;
114 return CURL_SOCKOPT_ALREADY_CONNECTED;
115}
117static int rh_closesocket(void *clientp, curl_socket_t item)
118{
119 (void)clientp;
120 (void)item;
121 return 0; /* closed at the end of the test */
122}
124/* Called after the connection is picked, before the request is sent:
125 discard the previous request and queue the next response. Doing it
126 earlier would make the idle connection look dead. */
127static int rh_prereq(void *clientp, char *conn_primary_ip,
128 char *conn_local_ip, int conn_primary_port,
129 int conn_local_port)
130{
131 struct respctx *ctx = clientp;
132 char buf[4096];
133 size_t left = ctx->resplen;
134 const char *p = ctx->resp;
135 (void)conn_primary_ip;
136 (void)conn_local_ip;
137 (void)conn_primary_port;
138 (void)conn_local_port;
140 while(sread(ctx->sock[1], buf, sizeof(buf)) > 0)
141 ;
142 while(left) {
143 ssize_t n = swrite(ctx->sock[1], p, left);
144 if(n <= 0)
145 return CURL_PREREQFUNC_ABORT;
146 p += n;
147 left -= (size_t)n;
148 }
149 return CURL_PREREQFUNC_OK;
150}
152/* The whole response is queued before libcurl reads any of it, so it must
153 fit in the socket buffers. Ask for more room than the default, which is
154 only 8 KiB on some systems, and check that a response fits. */
155static bool rh_fits(struct respctx *ctx)
156{
157 int size = 256 * 1024;
158 char buf[4096];
159 const char *p = ctx->resp;
160 size_t left = ctx->resplen;
161 size_t got = 0;
163 (void)setsockopt(ctx->sock[1], SOL_SOCKET, SO_SNDBUF,
164 (void *)&size, sizeof(size));
165 (void)setsockopt(ctx->sock[0], SOL_SOCKET, SO_RCVBUF,
166 (void *)&size, sizeof(size));
167 while(left) {
168 ssize_t n = swrite(ctx->sock[1], p, left);
169 if(n <= 0)
170 break;
171 p += n;
172 left -= (size_t)n;
173 }
174 /* read back what was written to leave the socket pair empty */
175 while(got < ctx->resplen - left) {
176 ssize_t n = sread(ctx->sock[0], buf, sizeof(buf));
177 if(n <= 0)
178 return FALSE;
179 got += (size_t)n;
180 }
181 return !left;
182}
184static size_t rh_write(char *ptr, size_t size, size_t nmemb, void *userdata)
185{
186 (void)ptr;
187 (void)userdata;
188 return size * nmemb;
189}
191static int run_one(const char *name, const char *resp, size_t resplen,
192 curl_off_t loops)
193{
194 struct respctx ctx;
195 struct curltime start;
196 struct curltime end;
197 timediff_t us;
198 long long hn;
199 curl_off_t loop;
200 CURL *curl;
201 int rc = 0;
203 memset(&ctx, 0, sizeof(ctx));
204 if(CURL_SOCKETPAIR(AF_UNIX, SOCK_STREAM, 0, ctx.sock)) {
205 curl_mfprintf(stderr, "socketpair() failed\n");
206 return 1;
207 }
208 /* the "server" end only reads what is already there */
209 if(curlx_nonblock(ctx.sock[1], TRUE)) {
210 curl_mfprintf(stderr, "curlx_nonblock() failed\n");
211 rc = 1;
212 goto out;
213 }
214 ctx.resp = resp;
215 ctx.resplen = resplen;
216 if(!rh_fits(&ctx)) {
217 curl_mfprintf(stderr, "%s: a %zu byte response does not fit in the "
218 "socket buffers\n", name, resplen);
219 rc = 1;
220 goto out;
221 }
223 curl = curl_easy_init();
224 if(!curl) {
225 rc = 1;
226 goto out;
227 }
228 curl_easy_setopt(curl, CURLOPT_URL, "http://127.0.0.1:1/");
229 curl_easy_setopt(curl, CURLOPT_PROXY, "");
230 curl_easy_setopt(curl, CURLOPT_OPENSOCKETFUNCTION, rh_opensocket);
231 curl_easy_setopt(curl, CURLOPT_OPENSOCKETDATA, &ctx);
232 curl_easy_setopt(curl, CURLOPT_SOCKOPTFUNCTION, rh_sockopt);
233 curl_easy_setopt(curl, CURLOPT_CLOSESOCKETFUNCTION, rh_closesocket);
234 curl_easy_setopt(curl, CURLOPT_PREREQFUNCTION, rh_prereq);
235 curl_easy_setopt(curl, CURLOPT_PREREQDATA, &ctx);
236 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, rh_write);
238 start = curlx_now();
239 for(loop = 0; loop < loops; loop++) {
240 CURLcode result = curl_easy_perform(curl);
241 if(result) {
242 curl_mfprintf(stderr, "%s: transfer %" CURL_FORMAT_CURL_OFF_T
243 " failed: %s\n", name, loop, curl_easy_strerror(result));
244 rc = 1;
245 break;
246 }
247 }
248 end = curlx_now();
249 us = curlx_timediff_us(end, start); /* how many microseconds */
250 hn = loop ? us * 100000 / loop : 0; /* 100 times too big */
251 curl_mprintf("%-8s %" CURL_FORMAT_CURL_OFF_T " loops, %lld usecs, "
252 "%lld.%02lld ns/loop\n",
253 name, loop, (long long)us,
254 (long long)hn / 100, (long long)hn % 100);
255 curl_easy_cleanup(curl);
256out:
257 sclose(ctx.sock[0]);
258 sclose(ctx.sock[1]);
259 return rc;
260}
262static int test_respheaders(int argc, const char **argv)
263{
264 static char typical[4096];
265 static char many[64 * 1024];
266 curl_off_t loops = 50000;
267 size_t tlen = 0;
268 size_t len;
269 int rc = 0;
270 int i;
272 if(argc > 1) {
273 const char *ptr = argv[1];
274 curl_off_t num;
275 if(!curlx_str_number(&ptr, &num, CURL_OFF_T_MAX) && !*ptr)
276 loops = num;
277 }
279 for(i = 0; i < (int)CURL_ARRAYSIZE(resp_typical); i++)
280 tlen += (size_t)curl_msnprintf(&typical[tlen], sizeof(typical) - tlen,
281 "%s", resp_typical[i]);
283 /* a response with lots of headers */
284 len = (size_t)curl_msnprintf(many, sizeof(many),
285 "HTTP/1.1 200 OK\r\nContent-Length: 2\r\n");
286 for(i = 0; i < MANY_HEADERS; i++)
287 len += (size_t)curl_msnprintf(&many[len], sizeof(many) - len,
288 "X-Header-%03d: some typical header "
289 "value text, max-age=3600; path=/\r\n", i);
290 len += (size_t)curl_msnprintf(&many[len], sizeof(many) - len, "\r\nok");
292 curl_global_init(CURL_GLOBAL_ALL);
293 rc |= run_one("small", resp_small, sizeof(resp_small) - 1, loops);
294 rc |= run_one("typical", typical, tlen, loops);
295 rc |= run_one("many", many, len, loops / 4);
296 curl_global_cleanup();
297 return rc;
298}
300#else
302static int test_respheaders(int argc, const char **argv)
303{
304 (void)argc;
305 (void)argv;
306 curl_mfprintf(stderr, "respheaders needs socketpair()\n");
307 return 0;
308}
310#endif
Diagnostics4 git calls · 175 ms
CallStatusSizeTime0175 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=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 · 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 · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=170 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=86Open pack map
fetch2003.7 KiB170 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=3832 git.response.cache=MISS git.response.duration_ms=170 git.fetch.wants=599a9bd97dfd27e14f30ba2595eac58278c297d1 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=3832 git.pkt.payload.max=3811 git.sideband.band1.bytes=3810 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
- a5e2d584fb7d689d
- Request
- 7d26e724…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format