Artifactspublic-v0

curl

master
ply4 git calls28 ms293 KiB3 cachedprotocol v2
551 lines16 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#ifdef HAVE_UNISTD_H
27#include <unistd.h>
28#endif
30#include "tool_cfgable.h"
31#include "tool_doswin.h"
32#include "tool_msgs.h"
33#include "tool_cb_hdr.h"
34#include "tool_cb_wrt.h"
35#include "tool_operate.h"
36#include "tool_libinfo.h"
38#ifdef _WIN32
39#define BOLD "\x1b[1m"
40#define BOLDOFF "\x1b[22m"
41#else
42#define BOLD "\x1b[1m"
43/* Switch off bold by setting "all attributes off" since the explicit
44 bold-off code (21) is not supported everywhere - like in the mac
45 Terminal. */
46#define BOLDOFF "\x1b[0m"
47/* OSC 8 hyperlink escape sequence */
48#define LINK "\x1b]8;;"
49#define LINKST "\x1b\\"
50#define LINKOFF LINK LINKST
51#endif
53#ifdef LINK
54/*
55 * Treat the Location: header specially, by writing a special escape
56 * sequence that adds a hyperlink to the displayed text. This makes
57 * the absolute URL of the redirect clickable in supported terminals,
58 * which could not happen otherwise for relative URLs. The Location:
59 * header is supposed to always be absolute so this theoretically
60 * should not be needed but the real world returns plenty of relative
61 * URLs here.
62 */
63static void write_linked_location(CURL *curl, const char *location,
64 size_t loclen, FILE *stream)
66 /* This would so simple if CURLINFO_REDIRECT_URL were available here */
67 CURLU *u = NULL;
68 char *copyloc = NULL, *scheme = NULL, *finalurl = NULL;
69 const char *loc = location, *locurl = NULL;
70 size_t llen = loclen;
71 int space_skipped = 0;
72 const char *vver = getenv("VTE_VERSION");
74 if(vver) {
75 curl_off_t num;
76 if(curlx_str_number(&vver, &num, CURL_OFF_T_MAX) ||
77 /* Skip formatting for old versions of VTE <= 0.48.1 (Mar 2017) since
78 some of those versions have formatting bugs. (#10428) */
79 (num <= 4801))
80 goto locout;
81 }
83 /* Strip leading whitespace of the redirect URL */
84 while(llen && (*loc == ' ' || *loc == '\t')) {
85 ++loc;
86 --llen;
87 ++space_skipped;
88 }
90 /* Strip the trailing end-of-line characters, normally "\r\n" */
91 while(llen && (loc[llen - 1] == '\n' || loc[llen - 1] == '\r'))
92 --llen;
94 /* CURLU makes it easy to handle the relative URL case */
95 u = curl_url();
96 if(!u)
97 goto locout;
99 /* Create a null-terminated and whitespace-stripped copy of Location: */
100 copyloc = curlx_memdup0(loc, llen);
101 if(!copyloc)
102 goto locout;
104 /* The original URL to use as a base for a relative redirect URL */
105 if(curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &locurl))
106 goto locout;
107 if(curl_url_set(u, CURLUPART_URL, locurl, 0))
108 goto locout;
110 /* Redirected location. This can be either absolute or relative. */
111 if(curl_url_set(u, CURLUPART_URL, copyloc, 0))
112 goto locout;
114 if(curl_url_get(u, CURLUPART_URL, &finalurl, CURLU_NO_DEFAULT_PORT))
115 goto locout;
117 if(curl_url_get(u, CURLUPART_SCHEME, &scheme, 0))
118 goto locout;
120 if(!strcmp("http", scheme) ||
121 !strcmp("https", scheme) ||
122 !strcmp("ftp", scheme) ||
123 !strcmp("ftps", scheme)) {
124 curl_mfprintf(stream, "%.*s" LINK "%s" LINKST "%.*s" LINKOFF,
125 space_skipped, location,
126 finalurl,
127 (int)loclen - space_skipped, loc);
128 goto locdone;
129 }
131 /* Not a "safe" URL: do not linkify it */
133locout:
134 /* Write the normal output in case of error or unsafe */
135 fwrite(location, loclen, 1, stream);
137locdone:
138 if(u) {
139 curl_free(finalurl);
140 curl_free(scheme);
141 curl_url_cleanup(u);
142 curlx_free(copyloc);
143 }
145#endif /* LINK */
147/*
148 * Copies a filename part and returns an ALLOCATED data buffer.
149 */
150static char *parse_filename(const char *ptr, size_t len, char stop)
152 char *copy;
153 char *p;
154 char *q;
156 copy = curlx_memdup0(ptr, len);
157 if(!copy)
158 return NULL;
160 p = copy;
161 if(stop) {
162 /* a Content-Disposition: header */
163 if(*p == '\'' || *p == '"') {
164 /* store the starting quote */
165 stop = *p;
166 p++;
167 }
169 /* scan for the end letter and stop there */
170 q = strchr(p, stop);
171 if(q)
172 *q = '\0';
173 }
174 else {
175 /* this is a Location: header, so we need to trim off any queries and
176 fragments present */
177 q = strchr(p, '?'); /* trim off query, if present */
178 if(q)
179 *q = '\0';
180 q = strchr(p, '#'); /* trim off fragment, if present */
181 if(q)
182 *q = '\0';
183 }
185 /* if the filename contains a path, only use filename portion */
186 q = strrchr(p, '/');
187 if(q)
188 p = q + 1;
190 /* If the filename contains a backslash, only use filename portion. The idea
191 is that even systems that do not handle backslashes as path separators
192 probably want the path removed for convenience. */
193 q = strrchr(p, '\\');
194 if(q)
195 p = q + 1;
197 /* make sure the filename does not end in \r or \n */
198 q = strchr(p, '\r');
199 if(q)
200 *q = '\0';
202 q = strchr(p, '\n');
203 if(q)
204 *q = '\0';
206 if(copy != p)
207 memmove(copy, p, strlen(p) + 1);
209#if defined(_WIN32) || defined(MSDOS)
210 {
211 char *sanitized;
212 SANITIZEcode sc = sanitize_file_name(&sanitized, copy, 0);
213 curlx_safefree(copy);
214 if(sc)
215 return NULL;
216 copy = sanitized;
217 }
218#endif /* _WIN32 || MSDOS */
220 /* an empty filename, "." or ".." is no filename */
221 if(!*copy || !strcmp(copy, ".") || !strcmp(copy, "..")) {
222 curlx_safefree(copy);
223 return NULL;
224 }
226 return copy;
229int tool_write_headers(struct HdrCbData *hdrcbdata, FILE *stream)
231 struct curl_slist *h = hdrcbdata->headlist ?
232 hdrcbdata->headlist->first : NULL;
233 int rc = 1;
234 while(h) {
235 /* not "handled", show it */
236 size_t len = strlen(h->data);
237 if(len != fwrite(h->data, 1, len, stream))
238 goto fail;
239 h = h->next;
240 }
241 rc = 0; /* success */
242fail:
243 slist_wc_free_all(hdrcbdata->headlist);
244 hdrcbdata->headlist = NULL;
245 return rc;
248/* Store a header until the output filename has been decided. */
249static size_t buffer_header(struct HdrCbData *hdrcbdata,
250 const char *str, size_t cb)
252 char *clone = curlx_memdup0(str, cb);
253 if(clone) {
254 struct slist_wc *entry = slist_wc_append(hdrcbdata->headlist, clone);
255 curlx_free(clone);
256 if(!entry)
257 goto fail;
258 hdrcbdata->headlist = entry;
259 }
260 else
261 goto fail;
262 return cb;
263fail:
264 slist_wc_free_all(hdrcbdata->headlist);
265 hdrcbdata->headlist = NULL;
266 return CURL_WRITEFUNC_ERROR;
269/*
270 * Write etag to file when --etag-save option is given.
271 */
272static size_t save_etag(const char *etag_h, const char *endp,
273 struct OutStruct *etag_save)
275 const char *eot = endp - 1;
276 if(*eot == '\n') {
277 while(ISBLANK(*etag_h) && (etag_h < eot))
278 etag_h++;
279 while(ISSPACE(*eot))
280 eot--;
282 if(eot >= etag_h) {
283 size_t etag_length = eot - etag_h + 1;
284 curlx_struct_stat file;
285 int fd = fileno(etag_save->stream);
287 /* Truncate regular files to avoid stale etag content */
288 if((fd != -1) &&
289 etag_save->regular_file &&
290 !curlx_fstat(fd, &file) &&
291 (S_ISREG(file.st_mode) &&
292 toolx_ftruncate(fd, 0)))
293 return CURL_WRITEFUNC_ERROR;
295 fwrite(etag_h, 1, etag_length, etag_save->stream);
296 /* terminate with newline */
297 fputc('\n', etag_save->stream);
298 (void)fflush(etag_save->stream);
299 }
300 }
301 return 0; /* ok */
304static bool set_filename(struct OutStruct *outs,
305 struct per_transfer *per, char *filename)
307 if(outs->stream) {
308 /* indication of problem, get out! */
309 curlx_free(filename);
310 return FALSE;
311 }
312 if(outs->alloc_filename)
313 curlx_safefree(outs->filename);
315 if(per->config->output_dir) {
316 char *f = curl_maprintf("%s/%s", per->config->output_dir, filename);
317 curlx_free(filename);
318 if(!f)
319 return FALSE;
320 outs->filename = curlx_strdup(f);
321 curl_free(f);
322 if(!outs->filename)
323 return FALSE;
324 }
325 else
326 outs->filename = filename;
327 return TRUE;
330/*
331 * This function sets the filename where output shall be written when curl
332 * options --remote-name (-O) and --remote-header-name (-J) have been
333 * simultaneously given and additionally server returns an HTTP
334 * Content-Disposition header specifying a filename property.
335 */
336static size_t content_disposition(const char *str, const char *end,
337 size_t cb, struct per_transfer *per,
338 long response) /* response code */
340 struct HdrCbData *hdrcbdata = &per->hdrcbdata;
341 struct OutStruct *outs = &per->outs;
343 if((cb > 9) && checkprefix("Location:", str) && (response/100 == 3)) {
344 /* Get the name off the location header as a temporary measure in case
345 there is no Content-Disposition */
346 const char *p = &str[9];
347 curlx_str_passblanks(&p);
348 if(p < end) { /* as a precaution */
349 char *filename = parse_filename(p, cb - (p - str), 0);
350 if(filename) {
351 if(!set_filename(outs, per, filename))
352 return CURL_WRITEFUNC_ERROR;
353 outs->alloc_filename = TRUE;
354 outs->is_cd_filename = TRUE; /* set to avoid clobbering existing files
355 by default */
356 }
357 }
358 }
359 else if((cb > 20) && checkprefix("Content-disposition:", str)) {
360 const char *p = str + 20;
361 /* look for the 'filename=' parameter (encoded filenames (*=) are not
362 supported) */
363 for(;;) {
364 char *filename;
365 size_t len;
367 while((p < end) && *p && !ISALPHA(*p))
368 p++;
369 if(p > end - 9)
370 break;
372 if(memcmp(p, "filename=", 9)) {
373 /* no match, find next parameter */
374 while((p < end) && *p && (*p != ';'))
375 p++;
376 if((p < end) && *p)
377 continue;
378 else
379 break;
380 }
381 p += 9;
383 curlx_str_passblanks(&p);
384 len = cb - (size_t)(p - str);
385 filename = parse_filename(p, len, ';');
386 if(filename) {
387 if(!set_filename(outs, per, filename))
388 return CURL_WRITEFUNC_ERROR;
389 outs->is_cd_filename = TRUE;
390 outs->regular_file = TRUE;
391 outs->fopened = FALSE;
392 outs->alloc_filename = TRUE;
393 hdrcbdata->honor_cd_filename = FALSE; /* done now! */
394 if(!tool_create_output_file(outs, per->config))
395 return CURL_WRITEFUNC_ERROR;
396 if(tool_write_headers(&per->hdrcbdata, outs->stream))
397 return CURL_WRITEFUNC_ERROR;
398 }
399 break;
400 }
401 /* No usable filename in this header. Do not create the output file
402 yet: a later Content-Disposition header may still provide a name, and
403 opening the URL-derived name here would truncate an existing file
404 that -J is meant to leave alone. The file is created once the body
405 arrives (or at the end of the transfer) and any buffered headers are
406 written then. */
407 } /* content-disposition handling */
409 if(hdrcbdata->honor_cd_filename &&
410 hdrcbdata->config->show_headers) {
411 /* still awaiting the Content-Disposition header */
412 return buffer_header(hdrcbdata, str, cb);
413 }
415 return 0; /* ok */
418/*
419 * callback for CURLOPT_HEADERFUNCTION
420 *
421 * 'size' is always 1
422 */
423size_t tool_header_cb(char *ptr, size_t size, size_t nmemb, void *userdata)
425 struct per_transfer *per = userdata;
426 struct HdrCbData *hdrcbdata = &per->hdrcbdata;
427 struct OutStruct *outs = &per->outs;
428 struct OutStruct *heads = &per->heads;
429 struct OutStruct *etag_save = &per->etag_save;
430 const char *str = ptr;
431 const size_t cb = size * nmemb;
432 const char *end = ptr + cb;
433 bool cd_checked = FALSE;
435 if(!per->config)
436 return CURL_WRITEFUNC_ERROR;
438#ifdef DEBUGBUILD
439 if(size * nmemb > (size_t)CURL_MAX_HTTP_HEADER) {
440 warnf("Header data exceeds write limit");
441 return CURL_WRITEFUNC_ERROR;
442 }
443#endif
445#ifdef _WIN32
446 /* Discard incomplete UTF-8 sequence buffered from body */
447 if(outs->utf8seq[0])
448 memset(outs->utf8seq, 0, sizeof(outs->utf8seq));
449#endif
451 /* If the last header was empty, check scheme again as we follow
452 * protocol switches on redirects */
453 if(!per->out_scheme || per->was_last_header_empty) {
454 char *scheme;
455 curl_easy_getinfo(per->curl, CURLINFO_SCHEME, &scheme);
456 per->out_scheme = proto_token(scheme);
457 per->num_headers = 0;
458 }
459 per->was_last_header_empty = (ptr[0] == '\r' || ptr[0] == '\n');
461 /*
462 * Write header data when curl option --dump-header (-D) is given.
463 */
465 if(per->config->headerfile && heads->stream) {
466 size_t rc = fwrite(ptr, size, nmemb, heads->stream);
467 if(rc != nmemb)
468 return rc;
469 /* flush the stream to send off what we got earlier */
470 if(fflush(heads->stream)) {
471 errorf("Failed writing headers to %s", per->config->headerfile);
472 return CURL_WRITEFUNC_ERROR;
473 }
474 }
476 if((per->out_scheme == proto_http || per->out_scheme == proto_https)) {
477 long response = 0;
478 curl_easy_getinfo(per->curl, CURLINFO_RESPONSE_CODE, &response);
480 if((response / 100 != 2) && (response / 100 != 3))
481 /* only care about etag and content-disposition headers in 2xx and 3xx
482 responses */
483 ;
484 else if(per->config->etag_save_file && etag_save->stream &&
485 /* match only header that start with etag (case insensitive) */
486 checkprefix("etag:", str)) {
487 size_t rc = save_etag(&str[5], end, etag_save);
488 if(rc)
489 return rc;
490 }
492 /* Parse the content-disposition and location headers. When
493 honor_cd_filename is true, other headers may be stored until the
494 content-disposition header is reached, at which point the saved headers
495 can be written. That means the content_disposition() may return an rc
496 when it has saved a different header for writing later. */
497 else if(hdrcbdata->honor_cd_filename) {
498 size_t rc = content_disposition(str, end, cb, per, response);
499 if(rc)
500 return rc;
501 /* remember that this was checked to avoid duplicating it further
502 down */
503 cd_checked = TRUE;
504 }
505 }
507 if(hdrcbdata->config->writeout && memchr(ptr, ':', cb))
508 per->num_headers++;
510 if(hdrcbdata->config->show_headers && !outs->out_null &&
511 (per->out_scheme == proto_http || per->out_scheme == proto_https ||
512 per->out_scheme == proto_rtsp || per->out_scheme == proto_file)) {
513 /* bold headers only for selected protocols */
514 const char *value = NULL;
516 /* Informational responses, and ETag headers handled by --etag-save,
517 bypass the content-disposition handling above. Do not open the output
518 file until the filename decision is complete. Only for HTTP(S). */
519 if(hdrcbdata->honor_cd_filename && !cd_checked &&
520 (per->out_scheme == proto_http || per->out_scheme == proto_https))
521 return buffer_header(hdrcbdata, str, cb);
523 if(!outs->stream && !tool_create_output_file(outs, per->config))
524 return CURL_WRITEFUNC_ERROR;
526 if(global->isatty &&
527#ifdef _WIN32
528 tool_term_has_bold &&
529#endif
530 global->styled_output)
531 value = memchr(ptr, ':', cb);
532 if(value) {
533 size_t namelen = value - ptr;
534 curl_mfprintf(outs->stream, BOLD "%.*s" BOLDOFF ":", (int)namelen, ptr);
535#ifndef LINK
536 fwrite(&value[1], cb - namelen - 1, 1, outs->stream);
537#else
538 if(curl_strnequal("Location", ptr, namelen)) {
539 write_linked_location(per->curl, &value[1], cb - namelen - 1,
540 outs->stream);
541 }
542 else
543 fwrite(&value[1], cb - namelen - 1, 1, outs->stream);
544#endif
545 }
546 else
547 /* not "handled", show it */
548 fwrite(ptr, cb, 1, outs->stream);
549 }
550 return cb;
Diagnostics4 git calls · 28 ms
CallStatusSizeTime028 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=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 · hit3 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=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=86
Open pack map
fetch2006.0 KiB · hit22 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=6173
git.response.cache=HIT
git.response.duration_ms=22
git.fetch.wants=a3dce165e71d770f7cc7d77c2401e7305f07325c
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=6173
git.pkt.payload.max=6152
git.sideband.band1.bytes=6151
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
a5e2c1d0e9e489da
Request
1acf727f…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format