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#include "testtrace.h"
28#ifndef CURL_DISABLE_WEBSOCKETS
30static CURLcode test_ws_data_m2_check_recv(const struct curl_ws_frame *frame,
31 size_t r_offset, size_t nread,
32 size_t exp_len)
33{
34 if(!frame)
35 return CURLE_OK;
37 if(frame->flags & CURLWS_CLOSE) {
38 curl_mfprintf(stderr, "recv_data: unexpected CLOSE frame from server, "
39 "got %zu bytes, offset=%zu, rflags %x\n",
40 nread, r_offset, (unsigned int)frame->flags);
41 return CURLE_RECV_ERROR;
42 }
43 if(!r_offset && !(frame->flags & CURLWS_BINARY)) {
44 curl_mfprintf(stderr, "recv_data: wrong frame, got %zu bytes, offset=%zu, "
45 "rflags %x\n",
46 nread, r_offset, (unsigned int)frame->flags);
47 return CURLE_RECV_ERROR;
48 }
49 if(frame->offset != (curl_off_t)r_offset) {
50 curl_mfprintf(stderr, "recv_data: frame offset, expected %zu, "
51 "got %" CURL_FORMAT_CURL_OFF_T "\n",
52 r_offset, frame->offset);
53 return CURLE_RECV_ERROR;
54 }
55 if(frame->bytesleft != (curl_off_t)(exp_len - r_offset - nread)) {
56 curl_mfprintf(stderr, "recv_data: frame bytesleft, "
57 "expected %" CURL_FORMAT_CURL_OFF_T ", "
58 "got %" CURL_FORMAT_CURL_OFF_T "\n",
59 (curl_off_t)(exp_len - r_offset - nread), frame->bytesleft);
60 return CURLE_RECV_ERROR;
61 }
62 if(r_offset + nread > exp_len) {
63 curl_mfprintf(stderr, "recv_data: data length, expected %zu, now at %zu\n",
64 exp_len, r_offset + nread);
65 return CURLE_RECV_ERROR;
66 }
67 return CURLE_OK;
68}
70/* WebSocket Mode 2: CONNECT_ONLY 2, curl_ws_send()/curl_ws_recv() */
71static CURLcode test_ws_data_m2_echo(const char *url,
72 size_t count,
73 size_t plen_min,
74 size_t plen_max)
75{
76 CURL *curl = NULL;
77 CURLcode result = CURLE_OK;
78 const struct curl_ws_frame *frame;
79 size_t len;
80 char *send_buf = NULL, *recv_buf = NULL;
81 size_t i, scount = count, rcount = count;
82 int rblock, sblock;
84 send_buf = curlx_calloc(1, plen_max + 1);
85 recv_buf = curlx_calloc(1, plen_max + 1);
86 if(!send_buf || !recv_buf) {
87 result = CURLE_OUT_OF_MEMORY;
88 goto out;
89 }
90 for(i = 0; i < plen_max; ++i) {
91 send_buf[i] = (char)('0' + ((int)i % 10));
92 }
94 curl = curl_easy_init();
95 if(!curl) {
96 result = CURLE_OUT_OF_MEMORY;
97 goto out;
98 }
100 curl_easy_setopt(curl, CURLOPT_URL, url);
102 /* use the callback style */
103 curl_easy_setopt(curl, CURLOPT_USERAGENT, "ws-data");
104 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
105 curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 2L); /* websocket style */
106 result = curl_easy_perform(curl);
107 curl_mfprintf(stderr, "curl_easy_perform() returned %d\n", (int)result);
108 if(result != CURLE_OK)
109 goto out;
111 for(len = plen_min; len <= plen_max; ++len) {
112 size_t nwritten, nread, slen = len, rlen = len;
113 char *sbuf = send_buf, *rbuf = recv_buf;
115 memset(recv_buf, 0, plen_max);
116 while(slen || rlen || scount || rcount) {
117 sblock = rblock = 1;
118 if(slen) {
119 result = curl_ws_send(curl, sbuf, slen, &nwritten, 0, CURLWS_BINARY);
120 sblock = (result == CURLE_AGAIN);
121 if(!result || (result == CURLE_AGAIN)) {
122 curl_mfprintf(stderr, "curl_ws_send(len=%zu) -> %d, "
123 "%zu (%" CURL_FORMAT_CURL_OFF_T "/%zu)\n", slen,
124 (int)result, nwritten, (curl_off_t)(len - slen), len);
125 sbuf += nwritten;
126 slen -= nwritten;
127 }
128 else
129 goto out;
130 }
131 if(!slen && scount) { /* go again? */
132 scount--;
133 sbuf = send_buf;
134 slen = len;
135 }
137 if(rlen) {
138 size_t max_recv = (64 * 1024);
139 result = curl_ws_recv(curl, rbuf, (rlen > max_recv) ? max_recv : rlen,
140 &nread, &frame);
141 if(!result || (result == CURLE_AGAIN)) {
142 rblock = (result == CURLE_AGAIN);
143 curl_mfprintf(stderr, "curl_ws_recv(len=%zu) -> %d, %zu (%ld/%zu)\n",
144 rlen, (int)result, nread, (long)(len - rlen), len);
145 if(!result) {
146 result = test_ws_data_m2_check_recv(frame, len - rlen, nread, len);
147 if(result)
148 goto out;
149 }
150 rbuf += nread;
151 rlen -= nread;
152 }
153 else
154 goto out;
155 }
156 if(!rlen && rcount) { /* go again? */
157 rcount--;
158 rbuf = recv_buf;
159 rlen = len;
160 }
162 if(rblock && sblock) {
163 curl_mfprintf(stderr, "EAGAIN, sleep, try again\n");
164 curlx_wait_ms(1);
165 }
166 }
168 if(memcmp(send_buf, recv_buf, len)) {
169 curl_mfprintf(stderr, "recv_data: data differs\n");
170 debug_dump("", "expected:", stderr,
171 (const unsigned char *)send_buf, len, FALSE);
172 debug_dump("", "received:", stderr,
173 (const unsigned char *)recv_buf, len, FALSE);
174 result = CURLE_RECV_ERROR;
175 goto out;
176 }
177 }
179out:
180 if(curl) {
181 if(!result)
182 ws_close(curl);
183 curl_easy_cleanup(curl);
184 }
185 curlx_free(send_buf);
186 curlx_free(recv_buf);
187 return result;
188}
190struct test_ws_m1_ctx {
191 CURL *curl;
192 char *send_buf;
193 char *recv_buf;
194 size_t send_len, nsent;
195 size_t recv_len, nrcvd;
196 size_t nframes;
197 size_t read_calls;
198 size_t write_calls;
199 size_t frames_read;
200 size_t frames_written;
201 BIT(frame_reading);
202};
204static size_t test_ws_data_m1_read(char *buf, size_t nitems, size_t buflen,
205 void *userdata)
206{
207 struct test_ws_m1_ctx *ctx = userdata;
208 size_t len = nitems * buflen;
209 size_t left = ctx->send_len - ctx->nsent;
211 ctx->read_calls++;
213 if(ctx->frames_read >= ctx->nframes)
214 goto out;
216 if(!ctx->frame_reading) {
217 curl_ws_start_frame(ctx->curl, CURLWS_BINARY, ctx->send_len);
218 ctx->frame_reading = TRUE;
219 }
221 if(ctx->frame_reading) {
222 bool complete;
223 if(left > len)
224 left = len;
225 memcpy(buf, ctx->send_buf + ctx->nsent, left);
226 ctx->nsent += left;
227 complete = (ctx->send_len == ctx->nsent);
228 curl_mfprintf(stderr, "m1_read(len=%zu, call #%zu, frame #%zu%s) -> %zu\n",
229 len, ctx->read_calls, ctx->frames_read,
230 complete ? " complete" : "", left);
231 if(complete) {
232 ++ctx->frames_read;
233 ctx->frame_reading = FALSE;
234 ctx->nsent = 0;
235 }
236 return left;
237 }
238out:
239 curl_mfprintf(stderr, "m1_read(len=%zu, call #%zu) -> EOS\n",
240 len, ctx->read_calls);
241 return 0;
242}
244static size_t test_ws_data_m1_write(char *buf, size_t nitems, size_t buflen,
245 void *userdata)
246{
247 struct test_ws_m1_ctx *ctx = userdata;
248 size_t len = nitems * buflen;
249 bool complete;
251 ctx->write_calls++;
252 if(len > (ctx->recv_len - ctx->nrcvd)) {
253 curl_mfprintf(stderr, "m1_write(len=%zu, call #%zu) -> ERROR\n",
254 len, ctx->write_calls);
255 return CURL_WRITEFUNC_ERROR;
256 }
257 memcpy(ctx->recv_buf + ctx->nrcvd, buf, len);
258 ctx->nrcvd += len;
259 complete = (ctx->recv_len == ctx->nrcvd);
261 if(memcmp(ctx->send_buf, ctx->recv_buf, ctx->nrcvd)) {
262 curl_mfprintf(stderr, "m1_write(len=%zu, call #%zu, frame #%zu) -> "
263 "data differs\n",
264 len, ctx->write_calls, ctx->frames_written);
265 debug_dump("", "expected:", stderr,
266 (unsigned char *)ctx->send_buf, ctx->nrcvd, 0);
267 debug_dump("", "received:", stderr,
268 (unsigned char *)ctx->recv_buf, ctx->nrcvd, 0);
269 return CURL_WRITEFUNC_ERROR;
270 }
272 curl_mfprintf(stderr, "m1_write(len=%zu, call #%zu, frame #%zu%s) -> %zu\n",
273 len, ctx->write_calls, ctx->frames_written,
274 complete ? " complete" : "", len);
275 if(complete) {
276 ++ctx->frames_written;
277 ctx->nrcvd = 0;
278 }
279 return len;
280}
282/* WebSocket Mode 1: multi handle, READ/WRITEFUNCTION use */
283static CURLcode test_ws_data_m1_echo(const char *url,
284 size_t count,
285 size_t plen_min,
286 size_t plen_max)
287{
288 CURLM *multi = NULL;
289 CURLcode result = CURLE_OK;
290 struct test_ws_m1_ctx m1_ctx;
291 size_t i, len;
293 curl_mfprintf(stderr, "test_ws_data_m1_echo(min=%zu, max=%zu)\n",
294 plen_min, plen_max);
295 memset(&m1_ctx, 0, sizeof(m1_ctx));
296 m1_ctx.send_buf = curlx_calloc(1, plen_max + 1);
297 m1_ctx.recv_buf = curlx_calloc(1, plen_max + 1);
298 if(!m1_ctx.send_buf || !m1_ctx.recv_buf) {
299 result = CURLE_OUT_OF_MEMORY;
300 goto out;
301 }
302 for(i = 0; i < plen_max; ++i) {
303 m1_ctx.send_buf[i] = (char)('0' + ((int)i % 10));
304 }
306 multi = curl_multi_init();
307 if(!multi) {
308 result = CURLE_OUT_OF_MEMORY;
309 goto out;
310 }
312 m1_ctx.curl = curl_easy_init();
313 if(!m1_ctx.curl) {
314 result = CURLE_OUT_OF_MEMORY;
315 goto out;
316 }
318 for(len = plen_min; len <= plen_max; ++len) {
319 /* init what we want to send and expect to receive */
320 curl_mfprintf(stderr, "m1_echo, iter len=%zu\n", len);
322 m1_ctx.send_len = len;
323 m1_ctx.nsent = 0;
324 m1_ctx.recv_len = len;
325 m1_ctx.nrcvd = 0;
326 m1_ctx.nframes = count;
327 m1_ctx.read_calls = 0;
328 m1_ctx.write_calls = 0;
329 m1_ctx.frames_read = 0;
330 m1_ctx.frames_written = 0;
331 memset(m1_ctx.recv_buf, 0, plen_max);
332 curl_easy_pause(m1_ctx.curl, CURLPAUSE_CONT);
334 curl_easy_reset(m1_ctx.curl);
335 curl_easy_setopt(m1_ctx.curl, CURLOPT_URL, url);
336 /* use the callback style */
337 curl_easy_setopt(m1_ctx.curl, CURLOPT_USERAGENT, "ws-data");
338 curl_easy_setopt(m1_ctx.curl, CURLOPT_VERBOSE, 1L);
339 /* we want to send */
340 curl_easy_setopt(m1_ctx.curl, CURLOPT_UPLOAD, 1L);
341 curl_easy_setopt(m1_ctx.curl, CURLOPT_READFUNCTION, test_ws_data_m1_read);
342 curl_easy_setopt(m1_ctx.curl, CURLOPT_READDATA, &m1_ctx);
343 curl_easy_setopt(m1_ctx.curl, CURLOPT_WRITEFUNCTION,
344 test_ws_data_m1_write);
345 curl_easy_setopt(m1_ctx.curl, CURLOPT_WRITEDATA, &m1_ctx);
347 curl_multi_add_handle(multi, m1_ctx.curl);
349 while(1) {
350 int still_running; /* keep number of running handles */
351 CURLMcode mresult = curl_multi_perform(multi, &still_running);
353 if(!still_running || (m1_ctx.frames_written >= m1_ctx.nframes)) {
354 /* got the full echo back or failed */
355 break;
356 }
358 if(!mresult && still_running) {
359 mresult = curl_multi_poll(multi, NULL, 0, 1, NULL);
360 }
361 if(mresult) {
362 result = CURLE_RECV_ERROR;
363 goto out;
364 }
365 }
367 curl_multi_remove_handle(multi, m1_ctx.curl);
369 /* check results */
370 if(m1_ctx.frames_read < m1_ctx.nframes) {
371 curl_mfprintf(stderr, "m1_echo, sent only %zu/%zu frames\n",
372 m1_ctx.frames_read, m1_ctx.nframes);
373 result = CURLE_SEND_ERROR;
374 goto out;
375 }
376 if(m1_ctx.frames_written < m1_ctx.frames_read) {
377 curl_mfprintf(stderr, "m1_echo, received only %zu/%zu frames\n",
378 m1_ctx.frames_written, m1_ctx.frames_read);
379 result = CURLE_RECV_ERROR;
380 goto out;
381 }
382 }
384out:
385 if(multi)
386 curl_multi_cleanup(multi);
387 if(m1_ctx.curl) {
388 curl_easy_cleanup(m1_ctx.curl);
389 }
390 curlx_free(m1_ctx.send_buf);
391 curlx_free(m1_ctx.recv_buf);
392 return result;
393}
395static void test_ws_data_usage(const char *msg)
396{
397 if(msg)
398 curl_mfprintf(stderr, "%s\n", msg);
399 curl_mfprintf(stderr,
400 "usage: [options] url\n"
401 " -m number minimum frame size\n"
402 " -M number maximum frame size\n"
403 );
404}
406#endif
408static CURLcode test_cli_ws_data(const char *URL)
409{
410#ifndef CURL_DISABLE_WEBSOCKETS
411 CURLcode result = CURLE_OK;
412 const char *url;
413 size_t plen_min = 0, plen_max = 0, count = 2;
414 int ch, model = 2;
416 (void)URL;
418 while((ch = cgetopt(test_argc, test_argv, "12c:hm:M:")) != -1) {
419 const char *opt = coptarg;
420 curl_off_t num;
421 switch(ch) {
422 case '1':
423 model = 1;
424 break;
425 case '2':
426 model = 2;
427 break;
428 case 'h':
429 test_ws_data_usage(NULL);
430 return CURLE_BAD_FUNCTION_ARGUMENT;
431 case 'c':
432 if(!curlx_str_number(&opt, &num, LONG_MAX))
433 count = (size_t)num;
434 break;
435 case 'm':
436 if(!curlx_str_number(&opt, &num, LONG_MAX))
437 plen_min = (size_t)num;
438 break;
439 case 'M':
440 if(!curlx_str_number(&opt, &num, LONG_MAX))
441 plen_max = (size_t)num;
442 break;
443 default:
444 test_ws_data_usage("invalid option");
445 return CURLE_BAD_FUNCTION_ARGUMENT;
446 }
447 }
448 test_argc -= coptind;
449 test_argv += coptind;
451 if(!plen_max)
452 plen_max = plen_min;
454 if(plen_max < plen_min) {
455 curl_mfprintf(stderr, "maxlen must be >= minlen, got %zu-%zu\n",
456 plen_min, plen_max);
457 return CURLE_BAD_FUNCTION_ARGUMENT;
458 }
460 if(test_argc != 1) {
461 test_ws_data_usage(NULL);
462 return CURLE_BAD_FUNCTION_ARGUMENT;
463 }
464 url = test_argv[0];
466 if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
467 curl_mfprintf(stderr, "curl_global_init() failed\n");
468 return (CURLcode)3;
469 }
471 if(model == 1)
472 result = test_ws_data_m1_echo(url, count, plen_min, plen_max);
473 else
474 result = test_ws_data_m2_echo(url, count, plen_min, plen_max);
476 curl_global_cleanup();
478 return result;
480#else /* !CURL_DISABLE_WEBSOCKETS */
481 (void)URL;
482 curl_mfprintf(stderr, "WebSockets not enabled in libcurl\n");
483 return (CURLcode)1;
484#endif /* CURL_DISABLE_WEBSOCKETS */
485}
Diagnostics4 git calls · 32 ms
CallStatusSizeTime032 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
fetch2004.1 KiB · hit27 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=4217 git.response.cache=HIT git.response.duration_ms=27 git.fetch.wants=41d962ca98234dd422bd3a5be943d9bbd4232bc1 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=4217 git.pkt.payload.max=4196 git.sideband.band1.bytes=4195 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
- a5e2ba42bfd7cf46
- Request
- a46339f5…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format