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 "tool_setup.h"
26#include "tool_cfgable.h"
27#include "tool_writeout.h"
28#include "tool_writeout_json.h"
30struct httpmap {
31 const char *str;
32 int num;
33};
35static const struct httpmap http_version[] = {
36 { "0", CURL_HTTP_VERSION_NONE },
37 { "1", CURL_HTTP_VERSION_1_0 },
38 { "1.1", CURL_HTTP_VERSION_1_1 },
39 { "2", CURL_HTTP_VERSION_2 },
40 { "3", CURL_HTTP_VERSION_3 },
41 { NULL, 0 } /* end of list */
42};
44static int writeTime(FILE *stream, const struct writeoutvar *wovar,
45 struct per_transfer *per, CURLcode per_result,
46 bool use_json)
47{
48 bool valid = FALSE;
49 curl_off_t us = 0;
51 (void)per;
52 (void)per_result;
53 DEBUGASSERT(wovar->writefunc == writeTime);
55 if(wovar->ci) {
56 if(!curl_easy_getinfo(per->curl, wovar->ci, &us))
57 valid = TRUE;
58 }
59 else {
60 DEBUGASSERT(0);
61 }
63 if(valid) {
64 curl_off_t secs = us / 1000000;
65 us %= 1000000;
67 if(use_json)
68 curl_mfprintf(stream, "\"%s\":", wovar->name);
70 curl_mfprintf(stream, "%" CURL_FORMAT_CURL_OFF_T
71 ".%06" CURL_FORMAT_CURL_OFF_T, secs, us);
72 }
73 else {
74 if(use_json)
75 curl_mfprintf(stream, "\"%s\":null", wovar->name);
76 }
78 return 1; /* return 1 if anything was written */
79}
81static int urlpart(struct per_transfer *per, writeoutid vid,
82 const char **contentp)
83{
84 CURLU *uh = curl_url();
85 int rc = 0;
86 if(uh) {
87 CURLUPart cpart = CURLUPART_HOST;
88 char *part = NULL;
89 char *url = NULL;
91 if(vid >= VAR_INPUT_URLESCHEME) {
92 if(curl_easy_getinfo(per->curl, CURLINFO_EFFECTIVE_URL, &url))
93 rc = 5;
94 }
95 else
96 url = per->url;
98 if(!rc) {
99 switch(vid) {
100 case VAR_INPUT_URLSCHEME:
101 case VAR_INPUT_URLESCHEME:
102 cpart = CURLUPART_SCHEME;
103 break;
104 case VAR_INPUT_URLUSER:
105 case VAR_INPUT_URLEUSER:
106 cpart = CURLUPART_USER;
107 break;
108 case VAR_INPUT_URLPASSWORD:
109 case VAR_INPUT_URLEPASSWORD:
110 cpart = CURLUPART_PASSWORD;
111 break;
112 case VAR_INPUT_URLOPTIONS:
113 case VAR_INPUT_URLEOPTIONS:
114 cpart = CURLUPART_OPTIONS;
115 break;
116 case VAR_INPUT_URLHOST:
117 case VAR_INPUT_URLEHOST:
118 cpart = CURLUPART_HOST;
119 break;
120 case VAR_INPUT_URLPORT:
121 case VAR_INPUT_URLEPORT:
122 cpart = CURLUPART_PORT;
123 break;
124 case VAR_INPUT_URLPATH:
125 case VAR_INPUT_URLEPATH:
126 cpart = CURLUPART_PATH;
127 break;
128 case VAR_INPUT_URLQUERY:
129 case VAR_INPUT_URLEQUERY:
130 cpart = CURLUPART_QUERY;
131 break;
132 case VAR_INPUT_URLFRAGMENT:
133 case VAR_INPUT_URLEFRAGMENT:
134 cpart = CURLUPART_FRAGMENT;
135 break;
136 case VAR_INPUT_URLZONEID:
137 case VAR_INPUT_URLEZONEID:
138 cpart = CURLUPART_ZONEID;
139 break;
140 default:
141 /* not implemented */
142 rc = 4;
143 break;
144 }
145 }
146 if(!rc && curl_url_set(uh, CURLUPART_URL, url,
147 CURLU_GUESS_SCHEME | CURLU_NON_SUPPORT_SCHEME))
148 rc = 2;
150 if(!rc && curl_url_get(uh, cpart, &part, CURLU_DEFAULT_PORT))
151 rc = 3;
153 if(!rc && part)
154 *contentp = part;
155 curl_url_cleanup(uh);
156 }
157 else
158 return 1;
159 return rc;
160}
162static void certinfo(struct per_transfer *per)
163{
164 if(!per->certinfo) {
165 const struct curl_certinfo *certinfo;
166 CURLcode result = curl_easy_getinfo(per->curl, CURLINFO_CERTINFO,
167 &certinfo);
168 per->certinfo = (!result && certinfo) ? certinfo : NULL;
169 }
170}
172static int writeString(FILE *stream, const struct writeoutvar *wovar,
173 struct per_transfer *per, CURLcode per_result,
174 bool use_json)
175{
176 bool valid = FALSE;
177 const char *strinfo = NULL;
178 const char *freestr = NULL;
179 struct dynbuf buf;
180 curlx_dyn_init(&buf, 256 * 1024);
182 DEBUGASSERT(wovar->writefunc == writeString);
184 if(wovar->ci) {
185 if(wovar->ci == CURLINFO_HTTP_VERSION) {
186 long version = 0;
187 if(!curl_easy_getinfo(per->curl, CURLINFO_HTTP_VERSION, &version)) {
188 const struct httpmap *m = &http_version[0];
189 while(m->str) {
190 if(m->num == version) {
191 strinfo = m->str;
192 valid = TRUE;
193 break;
194 }
195 m++;
196 }
197 }
198 }
199 else {
200 if(!curl_easy_getinfo(per->curl, wovar->ci, &strinfo) && strinfo)
201 valid = TRUE;
202 }
203 }
204 else {
205 switch(wovar->id) {
206 case VAR_CERT:
207 certinfo(per);
208 if(per->certinfo) {
209 int i;
210 bool error = FALSE;
211 for(i = 0; (i < per->certinfo->num_of_certs) && !error; i++) {
212 struct curl_slist *slist;
214 for(slist = per->certinfo->certinfo[i]; slist; slist = slist->next) {
215 size_t len;
216 if(curl_strnequal(slist->data, "cert:", 5)) {
217 if(curlx_dyn_add(&buf, &slist->data[5])) {
218 error = TRUE;
219 break;
220 }
221 }
222 else {
223 if(curlx_dyn_add(&buf, slist->data)) {
224 error = TRUE;
225 break;
226 }
227 }
228 len = curlx_dyn_len(&buf);
229 if(len) {
230 char *ptr = curlx_dyn_ptr(&buf);
231 /* add a newline to make things look better */
232 if(ptr[len - 1] != '\n' && curlx_dyn_addn(&buf, "\n", 1)) {
233 error = TRUE;
234 break;
235 }
236 }
237 }
238 }
239 if(!error) {
240 strinfo = curlx_dyn_ptr(&buf);
241 if(!strinfo)
242 /* maybe not a TLS protocol */
243 strinfo = "";
244 valid = TRUE;
245 }
246 }
247 else
248 strinfo = ""; /* no cert info */
249 break;
250 case VAR_ERRORMSG:
251 if(per_result) {
252 strinfo = per->errorbuffer[0] ? per->errorbuffer :
253 curl_easy_strerror(per_result);
254 valid = TRUE;
255 }
256 break;
257 case VAR_EFFECTIVE_FILENAME:
258 if(per->outs.filename) {
259 strinfo = per->outs.filename;
260 valid = TRUE;
261 }
262 break;
263 case VAR_INPUT_URL:
264 if(per->url) {
265 strinfo = per->url;
266 valid = TRUE;
267 }
268 break;
269 case VAR_INPUT_URLSCHEME:
270 case VAR_INPUT_URLUSER:
271 case VAR_INPUT_URLPASSWORD:
272 case VAR_INPUT_URLOPTIONS:
273 case VAR_INPUT_URLHOST:
274 case VAR_INPUT_URLPORT:
275 case VAR_INPUT_URLPATH:
276 case VAR_INPUT_URLQUERY:
277 case VAR_INPUT_URLFRAGMENT:
278 case VAR_INPUT_URLZONEID:
279 case VAR_INPUT_URLESCHEME:
280 case VAR_INPUT_URLEUSER:
281 case VAR_INPUT_URLEPASSWORD:
282 case VAR_INPUT_URLEOPTIONS:
283 case VAR_INPUT_URLEHOST:
284 case VAR_INPUT_URLEPORT:
285 case VAR_INPUT_URLEPATH:
286 case VAR_INPUT_URLEQUERY:
287 case VAR_INPUT_URLEFRAGMENT:
288 case VAR_INPUT_URLEZONEID:
289 if(per->url && !urlpart(per, wovar->id, &strinfo)) {
290 freestr = strinfo;
291 valid = TRUE;
292 }
293 break;
294 default:
295 DEBUGASSERT(0);
296 break;
297 }
298 }
300 DEBUGASSERT(!valid || strinfo);
301 if(valid && strinfo) {
302 if(use_json) {
303 curl_mfprintf(stream, "\"%s\":", wovar->name);
304 jsonWriteString(stream, strinfo, FALSE);
305 }
306 else
307 fputs(strinfo, stream);
308 }
309 else {
310 if(use_json)
311 curl_mfprintf(stream, "\"%s\":null", wovar->name);
312 }
313 curl_free((char *)CURL_UNCONST(freestr));
315 curlx_dyn_free(&buf);
316 return 1; /* return 1 if anything was written */
317}
319static int writeLong(FILE *stream, const struct writeoutvar *wovar,
320 struct per_transfer *per, CURLcode per_result,
321 bool use_json)
322{
323 bool valid = FALSE;
324 long longinfo = 0;
326 DEBUGASSERT(wovar->writefunc == writeLong);
328 if(wovar->ci) {
329 if(!curl_easy_getinfo(per->curl, wovar->ci, &longinfo))
330 valid = TRUE;
331 }
332 else {
333 switch(wovar->id) {
334 case VAR_NUM_RETRY:
335 longinfo = per->num_retries;
336 valid = TRUE;
337 break;
338 case VAR_NUM_CERTS:
339 certinfo(per);
340 longinfo = per->certinfo ? per->certinfo->num_of_certs : 0;
341 valid = TRUE;
342 break;
343 case VAR_NUM_HEADERS:
344 longinfo = per->num_headers;
345 valid = TRUE;
346 break;
347 case VAR_EXITCODE:
348 longinfo = (long)per_result;
349 valid = TRUE;
350 break;
351 default:
352 DEBUGASSERT(0);
353 break;
354 }
355 }
357 if(valid) {
358 if(use_json)
359 curl_mfprintf(stream, "\"%s\":%ld", wovar->name, longinfo);
360 else {
361 if(wovar->id == VAR_HTTP_CODE || wovar->id == VAR_HTTP_CODE_PROXY)
362 curl_mfprintf(stream, "%03ld", longinfo);
363 else
364 curl_mfprintf(stream, "%ld", longinfo);
365 }
366 }
367 else {
368 if(use_json)
369 curl_mfprintf(stream, "\"%s\":null", wovar->name);
370 }
372 return 1; /* return 1 if anything was written */
373}
375static int writeOffset(FILE *stream, const struct writeoutvar *wovar,
376 struct per_transfer *per, CURLcode per_result,
377 bool use_json)
378{
379 bool valid = FALSE;
380 curl_off_t offinfo = 0;
382 (void)per;
383 (void)per_result;
384 DEBUGASSERT(wovar->writefunc == writeOffset);
386 if(wovar->ci) {
387 if(!curl_easy_getinfo(per->curl, wovar->ci, &offinfo))
388 valid = TRUE;
389 }
390 else {
391 switch(wovar->id) {
392 case VAR_URLNUM:
393 if(per->urlnum <= INT_MAX) {
394 offinfo = per->urlnum;
395 valid = TRUE;
396 }
397 break;
398 default:
399 DEBUGASSERT(0);
400 }
401 }
403 if(valid) {
404 if(use_json)
405 curl_mfprintf(stream, "\"%s\":", wovar->name);
407 curl_mfprintf(stream, "%" CURL_FORMAT_CURL_OFF_T, offinfo);
408 }
409 else {
410 if(use_json)
411 curl_mfprintf(stream, "\"%s\":null", wovar->name);
412 }
414 return 1; /* return 1 if anything was written */
415}
417/* The designated write function should be the same as the CURLINFO return type
418 with exceptions special cased in the respective function. For example,
419 http_version uses CURLINFO_HTTP_VERSION which returns the version as a long,
420 however it is output as a string and therefore is handled in writeString.
422 Yes: "http_version": "1.1"
423 No: "http_version": 1.1
425 Variable names MUST be in alphabetical order.
426 */
427static const struct writeoutvar variables[] = {
428 { "certs", VAR_CERT, CURLINFO_NONE, writeString },
429 { "conn_id", VAR_CONN_ID, CURLINFO_CONN_ID, writeOffset },
430 { "content_type", VAR_CONTENT_TYPE, CURLINFO_CONTENT_TYPE, writeString },
431 { "errormsg", VAR_ERRORMSG, CURLINFO_NONE, writeString },
432 { "exitcode", VAR_EXITCODE, CURLINFO_NONE, writeLong },
433 { "filename_effective", VAR_EFFECTIVE_FILENAME, CURLINFO_NONE, writeString },
434 { "ftp_entry_path", VAR_FTP_ENTRY_PATH, CURLINFO_FTP_ENTRY_PATH,
435 writeString },
436 { "header_json", VAR_HEADER_JSON, CURLINFO_NONE, NULL },
437 { "http_code", VAR_HTTP_CODE, CURLINFO_RESPONSE_CODE, writeLong },
438 { "http_connect", VAR_HTTP_CODE_PROXY, CURLINFO_HTTP_CONNECTCODE,
439 writeLong },
440 { "http_version", VAR_HTTP_VERSION, CURLINFO_HTTP_VERSION, writeString },
441 { "json", VAR_JSON, CURLINFO_NONE, NULL },
442 { "local_ip", VAR_LOCAL_IP, CURLINFO_LOCAL_IP, writeString },
443 { "local_port", VAR_LOCAL_PORT, CURLINFO_LOCAL_PORT, writeLong },
444 { "method", VAR_EFFECTIVE_METHOD, CURLINFO_EFFECTIVE_METHOD, writeString },
445 { "num_certs", VAR_NUM_CERTS, CURLINFO_NONE, writeLong },
446 { "num_connects", VAR_NUM_CONNECTS, CURLINFO_NUM_CONNECTS, writeLong },
447 { "num_headers", VAR_NUM_HEADERS, CURLINFO_NONE, writeLong },
448 { "num_redirects", VAR_REDIRECT_COUNT, CURLINFO_REDIRECT_COUNT, writeLong },
449 { "num_retries", VAR_NUM_RETRY, CURLINFO_NONE, writeLong },
450 { "onerror", VAR_ONERROR, CURLINFO_NONE, NULL },
451 { "proxy_ssl_verify_result", VAR_PROXY_SSL_VERIFY_RESULT,
452 CURLINFO_PROXY_SSL_VERIFYRESULT, writeLong },
453 { "proxy_used", VAR_PROXY_USED, CURLINFO_USED_PROXY, writeLong },
454 { "redirect_url", VAR_REDIRECT_URL, CURLINFO_REDIRECT_URL, writeString },
455 { "referer", VAR_REFERER, CURLINFO_REFERER, writeString },
456 { "remote_ip", VAR_PRIMARY_IP, CURLINFO_PRIMARY_IP, writeString },
457 { "remote_port", VAR_PRIMARY_PORT, CURLINFO_PRIMARY_PORT, writeLong },
458 { "response_code", VAR_HTTP_CODE, CURLINFO_RESPONSE_CODE, writeLong },
459 { "scheme", VAR_SCHEME, CURLINFO_SCHEME, writeString },
460 { "size_delivered", VAR_SIZE_DELIVERED, CURLINFO_SIZE_DELIVERED,
461 writeOffset },
462 { "size_download", VAR_SIZE_DOWNLOAD, CURLINFO_SIZE_DOWNLOAD_T,
463 writeOffset },
464 { "size_header", VAR_HEADER_SIZE, CURLINFO_HEADER_SIZE, writeLong },
465 { "size_request", VAR_REQUEST_SIZE, CURLINFO_REQUEST_SIZE, writeLong },
466 { "size_upload", VAR_SIZE_UPLOAD, CURLINFO_SIZE_UPLOAD_T, writeOffset },
467 { "speed_download", VAR_SPEED_DOWNLOAD, CURLINFO_SPEED_DOWNLOAD_T,
468 writeOffset },
469 { "speed_upload", VAR_SPEED_UPLOAD, CURLINFO_SPEED_UPLOAD_T, writeOffset },
470 { "ssl_verify_result", VAR_SSL_VERIFY_RESULT, CURLINFO_SSL_VERIFYRESULT,
471 writeLong },
472 { "stderr", VAR_STDERR, CURLINFO_NONE, NULL },
473 { "stdout", VAR_STDOUT, CURLINFO_NONE, NULL },
474 { "time_appconnect", VAR_APPCONNECT_TIME, CURLINFO_APPCONNECT_TIME_T,
475 writeTime },
476 { "time_connect", VAR_CONNECT_TIME, CURLINFO_CONNECT_TIME_T, writeTime },
477 { "time_namelookup", VAR_NAMELOOKUP_TIME, CURLINFO_NAMELOOKUP_TIME_T,
478 writeTime },
479 { "time_posttransfer", VAR_POSTTRANSFER_TIME, CURLINFO_POSTTRANSFER_TIME_T,
480 writeTime },
481 { "time_pretransfer", VAR_PRETRANSFER_TIME, CURLINFO_PRETRANSFER_TIME_T,
482 writeTime },
483 { "time_queue", VAR_QUEUE_TIME, CURLINFO_QUEUE_TIME_T, writeTime },
484 { "time_redirect", VAR_REDIRECT_TIME, CURLINFO_REDIRECT_TIME_T, writeTime },
485 { "time_starttransfer", VAR_STARTTRANSFER_TIME,
486 CURLINFO_STARTTRANSFER_TIME_T, writeTime },
487 { "time_total", VAR_TOTAL_TIME, CURLINFO_TOTAL_TIME_T, writeTime },
488 { "tls_earlydata", VAR_TLS_EARLYDATA_SENT, CURLINFO_EARLYDATA_SENT_T,
489 writeOffset },
490 { "url", VAR_INPUT_URL, CURLINFO_NONE, writeString },
491 { "url.fragment", VAR_INPUT_URLFRAGMENT, CURLINFO_NONE, writeString },
492 { "url.host", VAR_INPUT_URLHOST, CURLINFO_NONE, writeString },
493 { "url.options", VAR_INPUT_URLOPTIONS, CURLINFO_NONE, writeString },
494 { "url.password", VAR_INPUT_URLPASSWORD, CURLINFO_NONE, writeString },
495 { "url.path", VAR_INPUT_URLPATH, CURLINFO_NONE, writeString },
496 { "url.port", VAR_INPUT_URLPORT, CURLINFO_NONE, writeString },
497 { "url.query", VAR_INPUT_URLQUERY, CURLINFO_NONE, writeString },
498 { "url.scheme", VAR_INPUT_URLSCHEME, CURLINFO_NONE, writeString },
499 { "url.user", VAR_INPUT_URLUSER, CURLINFO_NONE, writeString },
500 { "url.zoneid", VAR_INPUT_URLZONEID, CURLINFO_NONE, writeString },
501 { "url_effective", VAR_EFFECTIVE_URL, CURLINFO_EFFECTIVE_URL, writeString },
502 { "urle.fragment", VAR_INPUT_URLEFRAGMENT, CURLINFO_NONE, writeString },
503 { "urle.host", VAR_INPUT_URLEHOST, CURLINFO_NONE, writeString },
504 { "urle.options", VAR_INPUT_URLEOPTIONS, CURLINFO_NONE, writeString },
505 { "urle.password", VAR_INPUT_URLEPASSWORD, CURLINFO_NONE, writeString },
506 { "urle.path", VAR_INPUT_URLEPATH, CURLINFO_NONE, writeString },
507 { "urle.port", VAR_INPUT_URLEPORT, CURLINFO_NONE, writeString },
508 { "urle.query", VAR_INPUT_URLEQUERY, CURLINFO_NONE, writeString },
509 { "urle.scheme", VAR_INPUT_URLESCHEME, CURLINFO_NONE, writeString },
510 { "urle.user", VAR_INPUT_URLEUSER, CURLINFO_NONE, writeString },
511 { "urle.zoneid", VAR_INPUT_URLEZONEID, CURLINFO_NONE, writeString },
512 { "urlnum", VAR_URLNUM, CURLINFO_NONE, writeOffset },
513 { "xfer_id", VAR_EASY_ID, CURLINFO_XFER_ID, writeOffset }
514};
516#define MAX_WRITEOUT_NAME_LENGTH 24
518/* return the position after %time{} */
519static const char *outtime(const char *ptr, /* %time{ ... */
520 FILE *stream)
521{
522 const char *end;
523 ptr += 6;
524 end = strchr(ptr, '}');
525 if(end) {
526 struct dynbuf format;
527 char output[256]; /* max output time length */
528#ifdef HAVE_GETTIMEOFDAY
529 struct timeval cnow;
530#else
531 struct curltime cnow;
532#endif
533 time_t secs;
534 unsigned int usecs;
535 size_t i;
536 size_t vlen;
537 CURLcode result = CURLE_OK;
539#ifdef HAVE_GETTIMEOFDAY
540 gettimeofday(&cnow, NULL);
541#else
542 cnow.tv_sec = time(NULL);
543 cnow.tv_usec = 0;
544#endif
545 secs = cnow.tv_sec;
546 usecs = (unsigned int)cnow.tv_usec;
547#ifdef DEBUGBUILD
548 {
549 const char *timestr = getenv("CURL_TIME");
550 if(timestr) {
551 curl_off_t val;
552 curlx_str_number(×tr, &val, TIME_T_MAX);
553 secs = (time_t)val;
554 usecs = (unsigned int)(val % 1000000);
555 }
556 }
557#endif
558 vlen = end - ptr;
559 curlx_dyn_init(&format, 1024);
561 /* Insert:
562 - sub-seconds for %f
563 - epoch seconds for %s; strftime %s uses mktime() and assumes
564 local time, which breaks UTC output on non-UTC hosts
565 - +0000 for %z because it is otherwise not portable
566 - UTC for %Z because it is otherwise not portable
567 - Keep '%%' as-is so that strftime() makes a single % out of them
568 */
569 for(i = 0; !result && i < vlen; i++) {
570 if((i < vlen - 1) && ptr[i] == '%') {
571 switch(ptr[i + 1]) {
572 case 'f':
573 result = curlx_dyn_addf(&format, "%06u", usecs);
574 break;
575 case 's': {
576 /* time_t might be either 32 or 64 bits big */
577 curl_off_t tsecs = secs;
578 result = curlx_dyn_addf(&format, "%" CURL_FORMAT_CURL_OFF_T, tsecs);
579 break;
580 }
581 case 'Z':
582 result = curlx_dyn_addn(&format, "UTC", 3);
583 break;
584 case 'z':
585 result = curlx_dyn_addn(&format, "+0000", 5);
586 break;
587 case '%':
588 result = curlx_dyn_addn(&format, "%%", 2);
589 break;
590 default:
591 result = curlx_dyn_addn(&format, &ptr[i], 1);
592 continue;
593 }
594 i++;
595 }
596 else
597 result = curlx_dyn_addn(&format, &ptr[i], 1);
598 }
599 if(!result) {
600 struct tm utc;
601 result = curlx_gmtime(secs, &utc);
602#ifdef CURL_HAVE_DIAG /* includes llvm/clang, but not affected as of v22.1.0 */
603#pragma GCC diagnostic push
604#pragma GCC diagnostic ignored "-Wformat-nonliteral"
605#endif
606 if(curlx_dyn_len(&format) && !result &&
607 strftime(output, sizeof(output), curlx_dyn_ptr(&format), &utc))
608 fputs(output, stream);
609#ifdef CURL_HAVE_DIAG
610#pragma GCC diagnostic pop
611#endif
612 curlx_dyn_free(&format);
613 }
614 ptr = end + 1;
615 }
616 else
617 fputs("%time{", stream);
618 return ptr;
619}
621static void separator(const char *sep, size_t seplen, FILE *stream)
622{
623 while(seplen) {
624 if(*sep == '\\') {
625 switch(sep[1]) {
626 case 'r':
627 fputc('\r', stream);
628 break;
629 case 'n':
630 fputc('\n', stream);
631 break;
632 case 't':
633 fputc('\t', stream);
634 break;
635 case '}':
636 fputc('}', stream);
637 break;
638 case '\0':
639 break;
640 default:
641 /* unknown, output this */
642 fputc(sep[0], stream);
643 fputc(sep[1], stream);
644 break;
645 }
646 sep += 2;
647 seplen -= 2;
648 }
649 else {
650 fputc(*sep, stream);
651 sep++;
652 seplen--;
653 }
654 }
655}
657static void output_header(struct per_transfer *per,
658 FILE *stream,
659 const char **pptr)
660{
661 const char *ptr = *pptr;
662 const char *end;
663 end = strchr(ptr, '}');
664 do {
665 if(!end || (end[-1] != '\\'))
666 break;
667 end = strchr(&end[1], '}');
668 } while(end);
669 if(end) {
670 char hname[256]; /* holds the longest header field name */
671 struct curl_header *header;
672 const char *instr;
673 const char *sep = NULL;
674 size_t seplen = 0;
675 size_t vlen = end - ptr;
676 instr = memchr(ptr, ':', vlen);
677 /* instructions follow */
678 if(instr && !strncmp(&instr[1], "all:", 4)) {
679 sep = &instr[5];
680 seplen = end - sep;
681 vlen -= (seplen + 5);
682 }
683 if(vlen < sizeof(hname)) {
684 memcpy(hname, ptr, vlen);
685 hname[vlen] = 0;
686 if(sep) {
687 /* get headers from all requests */
688 int reqno = 0;
689 size_t indno = 0;
690 bool output = FALSE;
691 do {
692 if(CURLHE_OK == curl_easy_header(per->curl, hname, indno,
693 CURLH_HEADER, reqno,
694 &header)) {
695 if(output)
696 /* output separator */
697 separator(sep, seplen, stream);
698 fputs(header->value, stream);
699 output = TRUE;
700 }
701 else
702 break;
703 if((header->index + 1) < header->amount)
704 indno++;
705 else {
706 ++reqno;
707 indno = 0;
708 }
709 } while(1);
710 }
711 else {
712 if(CURLHE_OK == curl_easy_header(per->curl, hname, 0,
713 CURLH_HEADER, -1, &header))
714 fputs(header->value, stream);
715 }
716 }
717 ptr = end + 1;
718 }
719 else
720 fputs("%header{", stream);
721 *pptr = ptr;
722}
724static int matchvar(const void *m1, const void *m2)
725{
726 const struct writeoutvar *v1 = m1;
727 const struct writeoutvar *v2 = m2;
729 return strcmp(v1->name, v2->name);
730}
732void ourWriteOut(struct OperationConfig *config, struct per_transfer *per,
733 CURLcode per_result)
734{
735 FILE *stream = stdout;
736 const char *writeinfo = config->writeout;
737 const char *ptr = writeinfo;
738 bool done = FALSE;
739 bool fclose_stream = FALSE;
740 struct dynbuf name;
742 if(!writeinfo)
743 return;
745 curlx_dyn_init(&name, MAX_WRITEOUT_NAME_LENGTH);
746 while(ptr && *ptr && !done) {
747 if('%' == *ptr && ptr[1]) {
748 if('%' == ptr[1]) {
749 /* an escaped %-letter */
750 fputc('%', stream);
751 ptr += 2;
752 }
753 else {
754 /* this is meant as a variable to output */
755 const char *end;
756 size_t vlen;
757 if('{' == ptr[1]) {
758 const struct writeoutvar *wv = NULL;
759 struct writeoutvar find = { 0 };
760 end = strchr(ptr, '}');
761 ptr += 2; /* pass the % and the { */
762 if(!end) {
763 fputs("%{", stream);
764 continue;
765 }
766 vlen = end - ptr;
768 curlx_dyn_reset(&name);
769 if(!curlx_dyn_addn(&name, ptr, vlen)) {
770 find.name = curlx_dyn_ptr(&name);
771 wv = bsearch(&find,
772 variables, CURL_ARRAYSIZE(variables),
773 sizeof(variables[0]), matchvar);
774 }
775 else
776 break;
777 if(wv) {
778 switch(wv->id) {
779 case VAR_ONERROR:
780 if(per_result == CURLE_OK)
781 /* this is not error so skip the rest */
782 done = TRUE;
783 break;
784 case VAR_STDOUT:
785 if(fclose_stream)
786 curlx_fclose(stream);
787 fclose_stream = FALSE;
788 stream = stdout;
789 break;
790 case VAR_STDERR:
791 if(fclose_stream)
792 curlx_fclose(stream);
793 fclose_stream = FALSE;
794 stream = tool_stderr;
795 break;
796 case VAR_JSON:
797 ourWriteOutJSON(stream, variables,
798 CURL_ARRAYSIZE(variables),
799 per, per_result);
800 break;
801 case VAR_HEADER_JSON:
802 headerJSON(stream, per);
803 break;
804 default:
805 (void)wv->writefunc(stream, wv, per, per_result, FALSE);
806 break;
807 }
808 }
809 else {
810 curl_mfprintf(tool_stderr,
811 "curl: unknown --write-out variable: '%.*s'\n",
812 (int)vlen, ptr);
813 }
814 ptr = end + 1; /* pass the end */
815 }
816 else if(!strncmp("header{", &ptr[1], 7)) {
817 ptr += 8;
818 output_header(per, stream, &ptr);
819 }
820 else if(!strncmp("time{", &ptr[1], 5)) {
821 ptr = outtime(ptr, stream);
822 }
823 else if(!strncmp("output{", &ptr[1], 7)) {
824 bool append = FALSE;
825 ptr += 8;
826 if((ptr[0] == '>') && (ptr[1] == '>')) {
827 append = TRUE;
828 ptr += 2;
829 }
830 end = strchr(ptr, '}');
831 if(end) {
832 char fname[512]; /* holds the longest filename */
833 size_t flen = end - ptr;
834 if(flen < sizeof(fname)) {
835 FILE *stream2;
836 memcpy(fname, ptr, flen);
837 fname[flen] = 0;
838 stream2 = curlx_fopen(fname, append ? FOPEN_APPENDTEXT :
839 FOPEN_WRITETEXT);
840 if(stream2) {
841 /* only change if the open worked */
842 if(fclose_stream)
843 curlx_fclose(stream);
844 stream = stream2;
845 fclose_stream = TRUE;
846 }
847 }
848 ptr = end + 1;
849 }
850 else
851 fputs("%output{", stream);
852 }
853 else {
854 /* illegal syntax, then output the characters that are used */
855 fputc('%', stream);
856 fputc(ptr[1], stream);
857 ptr += 2;
858 }
859 }
860 }
861 else if('\\' == *ptr && ptr[1]) {
862 switch(ptr[1]) {
863 case 'r':
864 fputc('\r', stream);
865 break;
866 case 'n':
867 fputc('\n', stream);
868 break;
869 case 't':
870 fputc('\t', stream);
871 break;
872 default:
873 /* unknown, output this */
874 fputc(*ptr, stream);
875 fputc(ptr[1], stream);
876 break;
877 }
878 ptr += 2;
879 }
880 else {
881 fputc(*ptr, stream);
882 ptr++;
883 }
884 }
885 if(fclose_stream)
886 curlx_fclose(stream);
887 curlx_dyn_free(&name);
888}
Diagnostics4 git calls · 28 ms
CallStatusSizeTime028 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
fetch2007.1 KiB · hit23 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=7305 git.response.cache=HIT git.response.duration_ms=23 git.fetch.wants=fc346353ff658c4f16a1a2462bc830257dd37bb7 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=7305 git.pkt.payload.max=7284 git.sideband.band1.bytes=7283 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
- a5e236063de7a594
- Request
- 71200626…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format