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) && \
27 !defined(CURL_DISABLE_WEBSOCKETS)
29/* libcurl does not check Sec-WebSocket-Accept, so any value works */
30static const char wr_upgrade[] =
31 "HTTP/1.1 101 Switching Protocols\r\n"
32 "Upgrade: websocket\r\n"
33 "Connection: Upgrade\r\n"
34 "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n"
35 "\r\n";
37/* keep at most this much unread in the socket pair */
38#define WR_INFLIGHT (128 * 1024)
40struct wrctx {
41 curl_socket_t sock[2]; /* [0] is curl's end, [1] is the "server" end */
42 CURL *curl;
43 unsigned char block[64 * 1024]; /* whole frames, sent over and over */
44 size_t blocklen;
45 size_t blockpos; /* next byte of block to send */
46 size_t flen; /* bytes of one frame on the wire */
47 size_t hlen; /* bytes of its header */
48 size_t closepos; /* bytes of the CLOSE frame sent */
49 curl_off_t towrite; /* frame bytes still to send */
50 curl_off_t written; /* frame bytes sent */
51 curl_off_t frames; /* complete frames received */
52 curl_off_t received; /* frame bytes received */
53 bool bad; /* unexpected frame data */
54};
56static curl_socket_t wr_opensocket(void *clientp, curlsocktype purpose,
57 struct curl_sockaddr *address)
58{
59 struct wrctx *ctx = clientp;
60 (void)purpose;
61 (void)address;
62 return ctx->sock[0];
63}
65static int wr_sockopt(void *clientp, curl_socket_t curlfd,
66 curlsocktype purpose)
67{
68 (void)clientp;
69 (void)curlfd;
70 (void)purpose;
71 return CURL_SOCKOPT_ALREADY_CONNECTED;
72}
74static int wr_closesocket(void *clientp, curl_socket_t item)
75{
76 (void)clientp;
77 (void)item;
78 return 0; /* closed at the end of the test */
79}
81/* drop what libcurl sent, then send frames until enough are in flight,
82 and a CLOSE frame after the last one */
83static void wr_feed(struct wrctx *ctx)
84{
85 char buf[4096];
86 while(sread(ctx->sock[1], buf, sizeof(buf)) > 0)
87 ;
88 while(ctx->towrite && (ctx->written - ctx->received < WR_INFLIGHT)) {
89 size_t len = ctx->blocklen - ctx->blockpos;
90 ssize_t n;
91 if((curl_off_t)len > ctx->towrite)
92 len = (size_t)ctx->towrite;
93 n = swrite(ctx->sock[1], &ctx->block[ctx->blockpos], len);
94 if(n <= 0)
95 return;
96 ctx->blockpos = (ctx->blockpos + (size_t)n) % ctx->blocklen;
97 ctx->towrite -= n;
98 ctx->written += n;
99 }
100 while(!ctx->towrite && ctx->closepos < 2) {
101 static const unsigned char close_frame[] = { 0x88, 0x00 };
102 ssize_t n = swrite(ctx->sock[1], &close_frame[ctx->closepos],
103 sizeof(close_frame) - ctx->closepos);
104 if(n <= 0)
105 return;
106 ctx->closepos += (size_t)n;
107 }
108}
110static int wr_prereq(void *clientp, char *conn_primary_ip,
111 char *conn_local_ip, int conn_primary_port,
112 int conn_local_port)
113{
114 struct wrctx *ctx = clientp;
115 (void)conn_primary_ip;
116 (void)conn_local_ip;
117 (void)conn_primary_port;
118 (void)conn_local_port;
120 if(swrite(ctx->sock[1], wr_upgrade, sizeof(wr_upgrade) - 1) !=
121 (ssize_t)(sizeof(wr_upgrade) - 1))
122 return CURL_PREREQFUNC_ABORT;
123 wr_feed(ctx);
124 return CURL_PREREQFUNC_OK;
125}
127/* called with the payload of each frame, as an application would be */
128static size_t wr_write(char *ptr, size_t size, size_t nmemb, void *userdata)
129{
130 struct wrctx *ctx = userdata;
131 const struct curl_ws_frame *meta = curl_ws_meta(ctx->curl);
132 (void)ptr;
133 if(meta && (meta->flags & CURLWS_CLOSE))
134 ; /* the end */
135 else if(!meta || !(meta->flags & CURLWS_BINARY))
136 ctx->bad = TRUE;
137 else {
138 /* a frame can arrive in pieces */
139 if(!meta->offset)
140 ctx->received += (curl_off_t)ctx->hlen;
141 ctx->received += (curl_off_t)(size * nmemb);
142 if(!meta->bytesleft)
143 ctx->frames++;
144 if(ctx->written - ctx->received < WR_INFLIGHT / 2)
145 wr_feed(ctx);
146 }
147 return size * nmemb;
148}
150static int wr_run_one(const char *name, size_t plen, curl_off_t loops)
151{
152 static struct wrctx wrctx;
153 struct wrctx *ctx = &wrctx;
154 struct curltime start;
155 struct curltime end;
156 timediff_t us;
157 long long hn;
158 CURLcode result;
159 size_t i;
160 int size = 256 * 1024;
161 int rc = 0;
163 memset(ctx, 0, sizeof(*ctx));
164 if(CURL_SOCKETPAIR(AF_UNIX, SOCK_STREAM, 0, ctx->sock)) {
165 curl_mfprintf(stderr, "socketpair() failed\n");
166 return 1;
167 }
168 if(curlx_nonblock(ctx->sock[1], TRUE)) {
169 curl_mfprintf(stderr, "curlx_nonblock() failed\n");
170 rc = 1;
171 goto out;
172 }
173 (void)setsockopt(ctx->sock[1], SOL_SOCKET, SO_SNDBUF,
174 (void *)&size, sizeof(size));
175 (void)setsockopt(ctx->sock[0], SOL_SOCKET, SO_RCVBUF,
176 (void *)&size, sizeof(size));
178 /* unmasked binary frames, as a server sends them */
179 ctx->hlen = plen < 126 ? 2 : 4;
180 ctx->flen = ctx->hlen + plen;
181 ctx->blocklen = (sizeof(ctx->block) / ctx->flen) * ctx->flen;
182 for(i = 0; i < ctx->blocklen; i += ctx->flen) {
183 unsigned char *f = &ctx->block[i];
184 f[0] = 0x82;
185 if(ctx->hlen == 2)
186 f[1] = (unsigned char)plen;
187 else {
188 f[1] = 126;
189 f[2] = (unsigned char)(plen >> 8);
190 f[3] = (unsigned char)(plen & 0xff);
191 }
192 memset(f + ctx->hlen, 'x', plen);
193 }
194 ctx->towrite = loops * (curl_off_t)ctx->flen;
196 ctx->curl = curl_easy_init();
197 if(!ctx->curl) {
198 rc = 1;
199 goto out;
200 }
201 curl_easy_setopt(ctx->curl, CURLOPT_URL, "ws://127.0.0.1:1/");
202 curl_easy_setopt(ctx->curl, CURLOPT_PROXY, "");
203 curl_easy_setopt(ctx->curl, CURLOPT_OPENSOCKETFUNCTION, wr_opensocket);
204 curl_easy_setopt(ctx->curl, CURLOPT_OPENSOCKETDATA, ctx);
205 curl_easy_setopt(ctx->curl, CURLOPT_SOCKOPTFUNCTION, wr_sockopt);
206 curl_easy_setopt(ctx->curl, CURLOPT_CLOSESOCKETFUNCTION, wr_closesocket);
207 curl_easy_setopt(ctx->curl, CURLOPT_PREREQFUNCTION, wr_prereq);
208 curl_easy_setopt(ctx->curl, CURLOPT_PREREQDATA, ctx);
209 curl_easy_setopt(ctx->curl, CURLOPT_WRITEFUNCTION, wr_write);
210 curl_easy_setopt(ctx->curl, CURLOPT_WRITEDATA, ctx);
211 curl_easy_setopt(ctx->curl, CURLOPT_TIMEOUT, 120L);
213 start = curlx_now();
214 result = curl_easy_perform(ctx->curl);
215 end = curlx_now();
216 if(result || ctx->bad || ctx->frames != loops) {
217 curl_mfprintf(stderr, "%s: %s, %" CURL_FORMAT_CURL_OFF_T " of %"
218 CURL_FORMAT_CURL_OFF_T " frames%s\n", name,
219 curl_easy_strerror(result), ctx->frames, loops,
220 ctx->bad ? ", unexpected frame" : "");
221 rc = 1;
222 }
223 else {
224 us = curlx_timediff_us(end, start); /* how many microseconds */
225 hn = us * 100000 / loops; /* 100 times too big */
226 curl_mprintf("%-8s %" CURL_FORMAT_CURL_OFF_T " frames, %lld usecs, "
227 "%lld.%02lld ns/frame, %lld MB/s\n",
228 name, loops, (long long)us,
229 (long long)hn / 100, (long long)hn % 100,
230 us ? (long long)(loops * (curl_off_t)plen / us) : 0LL);
231 }
232 curl_easy_cleanup(ctx->curl);
233out:
234 sclose(ctx->sock[0]);
235 sclose(ctx->sock[1]);
236 return rc;
237}
239static int test_wsrecv(int argc, const char **argv)
240{
241 curl_off_t loops = 500000;
242 int rc = 0;
244 if(argc > 1) {
245 const char *ptr = argv[1];
246 curl_off_t num;
247 if(!curlx_str_number(&ptr, &num, CURL_OFF_T_MAX) && !*ptr && num)
248 loops = num;
249 }
251 curl_global_init(CURL_GLOBAL_ALL);
252 rc |= wr_run_one("16", 16, loops);
253 rc |= wr_run_one("125", 125, loops);
254 rc |= wr_run_one("1024", 1024, loops);
255 rc |= wr_run_one("16384", 16384, loops / 16 ? loops / 16 : 1);
256 curl_global_cleanup();
257 return rc;
258}
260#else
262static int test_wsrecv(int argc, const char **argv)
263{
264 (void)argc;
265 (void)argv;
266 curl_mfprintf(stderr, "wsrecv needs socketpair() and WebSocket support\n");
267 return 0;
268}
270#endif
Diagnostics4 git calls · 142 ms
CallStatusSizeTime0142 ms
discover200152 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs 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 · 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
fetch2003.1 KiB137 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=137 git.response.bytes=3157 git.response.cache=MISS git.response.duration_ms=137 git.fetch.wants=2a6a1dbcbb6483338c4f9f79875759710361dd18 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=3157 git.pkt.payload.max=3136 git.sideband.band1.bytes=3135 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
- a5e31ae0f95e3e6e
- Request
- 483cf2e1…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format