Artifactspublic-v0

curl

master
ply4 git calls31 ms291 KiB3 cachedprotocol v2
495 lines14 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 "tool_setup.h"
26#include "tool_cfgable.h"
27#include "tool_getparam.h"
28#include "tool_msgs.h"
29#include "tool_paramhlp.h"
30#include "tool_writeout_json.h"
31#include "var.h"
33#define MAX_EXPAND_CONTENT 10000000
34#define MAX_VAR_LEN 128 /* max length of a name */
36/* free everything */
37void varcleanup(void)
39 struct tool_var *list = global->variables;
40 while(list) {
41 struct tool_var *t = list;
42 list = list->next;
43 curlx_free(CURL_UNCONST(t->content));
44 curlx_free(t);
45 }
48static const struct tool_var *varcontent(const char *name, size_t nlen)
50 struct tool_var *list = global->variables;
51 while(list) {
52 if((strlen(list->name) == nlen) && !strncmp(name, list->name, nlen)) {
53 return list;
54 }
55 list = list->next;
56 }
57 return NULL;
60#define ENDOFFUNC(x) (((x) == '}') || ((x) == ':'))
61#define FUNCMATCH(ptr, name, len) \
62 (!strncmp(ptr, name, len) && ENDOFFUNC((ptr)[len]))
64#define FUNC_TRIM "trim"
65#define FUNC_TRIM_LEN CURL_CSTRLEN(FUNC_TRIM)
66#define FUNC_JSON "json"
67#define FUNC_JSON_LEN CURL_CSTRLEN(FUNC_JSON)
68#define FUNC_URL "url"
69#define FUNC_URL_LEN CURL_CSTRLEN(FUNC_URL)
70#define FUNC_B64 "b64"
71#define FUNC_B64_LEN CURL_CSTRLEN(FUNC_B64)
72#define FUNC_64DEC "64dec" /* base64 decode */
73#define FUNC_64DEC_LEN CURL_CSTRLEN(FUNC_64DEC)
75static ParameterError varfunc(char *c, /* content */
76 size_t clen, /* content length */
77 const char *f, /* functions */
78 size_t flen, /* function string length */
79 struct dynbuf *out)
81 char *allocptr = NULL;
82 ParameterError err = PARAM_OK;
83 const char *finput = f;
85 /* The functions are independent and runs left to right */
86 while(*f && !err) {
87 if(*f == '}')
88 /* end of functions */
89 break;
90 /* On entry, this is known to be a colon already. In subsequent laps, it
91 is also known to be a colon since that is part of the FUNCMATCH()
92 checks */
93 f++;
94 if(FUNCMATCH(f, FUNC_TRIM, FUNC_TRIM_LEN)) {
95 size_t len = clen;
96 f += FUNC_TRIM_LEN;
97 if(clen) {
98 /* skip leading white space, including CRLF */
99 while(ISSPACE(*c)) {
100 c++;
101 len--;
102 }
103 while(len && ISSPACE(c[len - 1]))
104 len--;
105 }
106 /* put it in the output */
107 curlx_dyn_reset(out);
108 if(curlx_dyn_addn(out, c, len)) {
109 err = PARAM_NO_MEM;
110 break;
111 }
112 }
113 else if(FUNCMATCH(f, FUNC_JSON, FUNC_JSON_LEN)) {
114 f += FUNC_JSON_LEN;
115 curlx_dyn_reset(out);
116 if(clen && jsonquoted(c, clen, out, FALSE)) {
117 err = PARAM_NO_MEM;
118 break;
119 }
120 }
121 else if(FUNCMATCH(f, FUNC_URL, FUNC_URL_LEN)) {
122 f += FUNC_URL_LEN;
123 curlx_dyn_reset(out);
124 if(clen) {
125 char *enc;
126 if(clen > INT_MAX) { /* curl_easy_escape() takes an int length */
127 errorf("too much data to URL encode");
128 err = PARAM_EXPAND_ERROR;
129 break;
130 }
131 enc = curl_easy_escape(NULL, c, (int)clen);
132 if(!enc) {
133 err = PARAM_NO_MEM;
134 break;
135 }
137 /* put it in the output */
138 if(curlx_dyn_add(out, enc))
139 err = PARAM_NO_MEM;
140 curl_free(enc);
141 if(err)
142 break;
143 }
144 }
145 else if(FUNCMATCH(f, FUNC_B64, FUNC_B64_LEN)) {
146 f += FUNC_B64_LEN;
147 curlx_dyn_reset(out);
148 if(clen) {
149 char *enc;
150 size_t elen;
151 CURLcode result = curlx_base64_encode((uint8_t *)c, clen, &enc, &elen);
152 if(result) {
153 err = PARAM_NO_MEM;
154 break;
155 }
157 /* put it in the output */
158 if(curlx_dyn_addn(out, enc, elen))
159 err = PARAM_NO_MEM;
160 curlx_free(enc);
161 if(err)
162 break;
163 }
164 }
165 else if(FUNCMATCH(f, FUNC_64DEC, FUNC_64DEC_LEN)) {
166 f += FUNC_64DEC_LEN;
167 curlx_dyn_reset(out);
168 if(clen) {
169 unsigned char *enc;
170 size_t elen;
171 CURLcode result = curlx_base64_decode(c, &enc, &elen);
172 /* put it in the output */
173 if(result) {
174 if(curlx_dyn_add(out, "[64dec-fail]"))
175 err = PARAM_NO_MEM;
176 }
177 else {
178 if(curlx_dyn_addn(out, enc, elen))
179 err = PARAM_NO_MEM;
180 curlx_free(enc);
181 }
182 if(err)
183 break;
184 }
185 }
186 else {
187 /* unsupported function */
188 errorf("unknown variable function in '%.*s'", (int)flen, finput);
189 err = PARAM_EXPAND_ERROR;
190 break;
191 }
192 if(allocptr)
193 curlx_free(allocptr);
195 clen = curlx_dyn_len(out);
196 allocptr = c = curlx_memdup0(curlx_dyn_ptr(out), clen);
197 if(!c) {
198 err = PARAM_NO_MEM;
199 break;
200 }
201 }
202 if(allocptr)
203 curlx_free(allocptr);
204 if(err)
205 curlx_dyn_free(out);
206 return err;
209ParameterError varexpand(const char *line, struct dynbuf *out, bool *replaced)
211 CURLcode result;
212 const char *envp;
213 bool added = FALSE;
214 const char *input = line;
215 *replaced = FALSE;
216 curlx_dyn_init(out, MAX_EXPAND_CONTENT);
217 do {
218 envp = strstr(line, "{{");
219 if(!envp)
220 break;
221 if((envp > line) && envp[-1] == '\\') {
222 /* preceding backslash, we want this verbatim */
224 /* insert the text up to this point, minus the backslash */
225 result = curlx_dyn_addn(out, line, envp - line - 1);
226 if(result)
227 return PARAM_NO_MEM;
229 /* output '{{' then continue from here */
230 result = curlx_dyn_addn(out, "{{", 2);
231 if(result)
232 return PARAM_NO_MEM;
233 line = &envp[2];
234 }
235 else {
236 char name[MAX_VAR_LEN];
237 size_t nlen;
238 size_t i;
239 const char *funcp;
240 const char *clp = strstr(envp, "}}");
241 size_t prefix;
243 if(!clp) {
244 /* uneven braces */
245 warnf("missing close '}}' in '%s'", input);
246 break;
247 }
249 prefix = 2;
250 envp += 2; /* move over the {{ */
252 /* if there is a function, it ends the name with a colon */
253 funcp = memchr(envp, ':', clp - envp);
254 if(funcp)
255 nlen = funcp - envp;
256 else
257 nlen = clp - envp;
258 if(!nlen || (nlen >= sizeof(name))) {
259 warnf("bad variable name length '%s'", input);
260 /* insert the text as-is since this is not an env variable */
261 result = curlx_dyn_addn(out, line, clp - line + prefix);
262 if(result)
263 return PARAM_NO_MEM;
264 }
265 else {
266 /* insert the text up to this point */
267 result = curlx_dyn_addn(out, line, envp - prefix - line);
268 if(result)
269 return PARAM_NO_MEM;
271 /* copy the name to separate buffer */
272 memcpy(name, envp, nlen);
273 name[nlen] = 0;
275 /* verify that the name looks sensible */
276 for(i = 0; (i < nlen) && (ISALNUM(name[i]) || (name[i] == '_')); i++)
277 ;
278 if(i != nlen) {
279 warnf("bad variable name: %s", name);
280 /* insert the text as-is since this is not an env variable */
281 result = curlx_dyn_addn(out, envp - prefix, clp - envp + prefix + 2);
282 if(result)
283 return PARAM_NO_MEM;
284 }
285 else {
286 char *value;
287 size_t vlen = 0;
288 struct dynbuf buf;
289 const struct tool_var *v = varcontent(name, nlen);
290 if(v) {
291 value = (char *)CURL_UNCONST(v->content);
292 vlen = v->clen;
293 }
294 else
295 value = NULL;
297 curlx_dyn_init(&buf, MAX_EXPAND_CONTENT);
298 if(funcp) {
299 /* apply the list of functions on the value */
300 size_t flen = clp - funcp;
301 ParameterError err = varfunc(value, vlen, funcp, flen, &buf);
302 if(err)
303 return err;
304 value = curlx_dyn_ptr(&buf);
305 vlen = curlx_dyn_len(&buf);
306 }
308 if(value && vlen > 0) {
309 /* A variable might contain NUL bytes. Such bytes cannot be shown
310 using normal means, this is an error. */
311 const char *nb = memchr(value, '\0', vlen);
312 if(nb) {
313 errorf("variable contains NUL byte");
314 return PARAM_EXPAND_ERROR;
315 }
316 }
317 /* insert the value */
318 result = curlx_dyn_addn(out, value, vlen);
319 curlx_dyn_free(&buf);
320 if(result)
321 return PARAM_NO_MEM;
323 added = TRUE;
324 }
325 }
326 line = &clp[2];
327 }
328 } while(1);
329 if(added && *line) {
330 /* add the "suffix" as well */
331 result = curlx_dyn_add(out, line);
332 if(result)
333 return PARAM_NO_MEM;
334 }
335 *replaced = added;
336 if(!added)
337 curlx_dyn_free(out);
338 return PARAM_OK;
341/*
342 * Created in a way that is not revealing how variables are actually stored so
343 * that we can improve this if we want better performance when managing many
344 * at a later point.
345 */
346static ParameterError addvariable(const char *name,
347 size_t nlen,
348 const char *content,
349 size_t clen,
350 bool contalloc)
352 struct tool_var *p;
353 const struct tool_var *check = varcontent(name, nlen);
354 DEBUGASSERT(nlen);
355 if(check)
356 notef("Overwriting variable '%s'", check->name);
358 p = curlx_calloc(1, sizeof(struct tool_var) + nlen);
359 if(p) {
360 memcpy(p->name, name, nlen);
361 /* the null-terminator is already present from above */
363 p->content = contalloc ? content : curlx_memdup0(content, clen);
364 if(p->content) {
365 p->clen = clen;
367 p->next = global->variables;
368 global->variables = p;
369 return PARAM_OK;
370 }
371 curlx_free(p);
372 }
373 return PARAM_NO_MEM;
376ParameterError setvariable(const char *input)
378 const char *name;
379 size_t nlen;
380 char *content = NULL;
381 size_t clen = 0;
382 bool contalloc = FALSE;
383 const char *line = input;
384 ParameterError err = PARAM_OK;
385 bool import = FALSE;
386 char *ge = NULL;
387 char buf[MAX_VAR_LEN];
388 curl_off_t startoffset = 0;
389 curl_off_t endoffset = CURL_OFF_T_MAX;
391 if(*input == '%') {
392 import = TRUE;
393 line++;
394 }
395 name = line;
396 while(*line && (ISALNUM(*line) || (*line == '_')))
397 line++;
398 nlen = line - name;
399 if(!nlen || (nlen >= MAX_VAR_LEN)) {
400 warnf("Bad variable name length (%zu), skipping", nlen);
401 return PARAM_OK;
402 }
403 if(import) {
404 /* this does not use curl_getenv() because we want "" support for blank
405 content */
406 if(*line) {
407 /* if there is a default action, we need to copy the name */
408 memcpy(buf, name, nlen);
409 buf[nlen] = 0;
410 name = buf;
411 }
412 ge = getenv(name);
413 if(!*line && !ge) {
414 /* no assign, no variable, fail */
415 errorf("Variable '%s' import fail, not set", name);
416 return PARAM_EXPAND_ERROR;
417 }
418 else if(ge) {
419 /* there is a value to use */
420 content = ge;
421 clen = strlen(ge);
422 }
423 }
424 if(*line == '[' && ISDIGIT(line[1])) {
425 /* is there a byte range specified? [num-num] */
426 line++;
427 if(curlx_str_number(&line, &startoffset, CURL_OFF_T_MAX) ||
428 curlx_str_single(&line, '-'))
429 return PARAM_VAR_SYNTAX;
430 if(curlx_str_single(&line, ']') &&
431 (curlx_str_number(&line, &endoffset, CURL_OFF_T_MAX) ||
432 curlx_str_single(&line, ']')))
433 return PARAM_VAR_SYNTAX;
434 if(startoffset > endoffset)
435 return PARAM_VAR_SYNTAX;
436 }
437 if(content)
438 ;
439 else if(*line == '@') {
440 /* read from file or stdin */
441 FILE *file;
442 bool use_stdin;
443 line++;
445 use_stdin = !strcmp(line, "-");
446 if(use_stdin)
447 file = stdin;
448 else {
449 file = curlx_fopen(line, "rb");
450 if(!file) {
451 char errbuf[STRERROR_LEN];
452 errorf("Failed to open %s: %s", line,
453 curlx_strerror(errno, errbuf, sizeof(errbuf)));
454 err = PARAM_READ_ERROR;
455 }
456 }
457 if(!err) {
458 err = file2memory_range(&content, &clen, file, startoffset, endoffset);
459 /* in case of out of memory, this should fail the entire operation */
460 if(clen)
461 contalloc = TRUE;
462 }
463 if(!use_stdin && file)
464 curlx_fclose(file);
465 if(err)
466 return err;
467 }
468 else if(*line == '=') {
469 line++;
470 clen = strlen(line);
471 /* this is the exact content */
472 content = (char *)CURL_UNCONST(line);
473 if(startoffset || (endoffset != CURL_OFF_T_MAX)) {
474 if(startoffset >= (curl_off_t)clen)
475 clen = 0;
476 else {
477 /* make the end offset no larger than the last byte */
478 if(endoffset >= (curl_off_t)clen)
479 endoffset = clen - 1;
480 clen = (size_t)(endoffset - startoffset) + 1;
481 content += startoffset;
482 }
483 }
484 }
485 else {
486 warnf("Bad --variable syntax, skipping: %s", input);
487 return PARAM_OK;
488 }
489 err = addvariable(name, nlen, content, clen, contalloc);
490 if(err) {
491 if(contalloc)
492 curlx_free(content);
493 }
494 return err;
Diagnostics4 git calls · 31 ms
CallStatusSizeTime031 ms
discover200152 B4 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
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 · hit5 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=22
git.response.bytes=293470
git.response.cache=HIT
git.response.duration_ms=5
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.6 KiB · hit22 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=4688
git.response.cache=HIT
git.response.duration_ms=22
git.fetch.wants=702157e737d455f3c685efa869f19c63678f37e3
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=4688
git.pkt.payload.max=4667
git.sideband.band1.bytes=4666
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
a5e9e6a05f1aeefa
Request
6316c9f0…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format