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_json.h"
28#include "tool_writeout.h"
30/* return the length of the valid UTF-8 sequence that starts at 'i', or 0 */
31static size_t utf8len(const unsigned char *i, const unsigned char *end)
32{
33 unsigned char lo = 0x80, hi = 0xbf; /* allowed range of the second byte */
34 size_t n, k;
36 if(*i >= 0xc2 && *i <= 0xdf)
37 n = 2;
38 else if(*i >= 0xe0 && *i <= 0xef) {
39 n = 3;
40 if(*i == 0xe0)
41 lo = 0xa0; /* no overlong forms */
42 else if(*i == 0xed)
43 hi = 0x9f; /* no surrogates */
44 }
45 else if(*i >= 0xf0 && *i <= 0xf4) {
46 n = 4;
47 if(*i == 0xf0)
48 lo = 0x90; /* no overlong forms */
49 else if(*i == 0xf4)
50 hi = 0x8f; /* nothing above U+10FFFF */
51 }
52 else
53 return 0;
55 if((size_t)(end - i) < n || i[1] < lo || i[1] > hi)
56 return 0;
57 for(k = 2; k < n; k++)
58 if((i[k] & 0xc0) != 0x80)
59 return 0;
60 return n;
61}
63/* The longest json form of one character is \u00XX. */
64#define JSON_ESC_MAX 6
66/* Write the json form of the character at *inp to buf, which must have room
67 for JSON_ESC_MAX bytes, and move *inp past it. A valid UTF-8 sequence is
68 one character. Return the number of bytes written. */
69static size_t jsonescape(const unsigned char **inp, const unsigned char *end,
70 bool lowercase, char *buf)
71{
72 static const char hex[] = "0123456789abcdef";
73 const unsigned char *i = *inp;
74 const char *esc = NULL;
76 *inp = i + 1;
77 switch(*i) {
78 case '\\':
79 esc = "\\\\";
80 break;
81 case '\"':
82 esc = "\\\"";
83 break;
84 case '\b':
85 esc = "\\b";
86 break;
87 case '\f':
88 esc = "\\f";
89 break;
90 case '\n':
91 esc = "\\n";
92 break;
93 case '\r':
94 esc = "\\r";
95 break;
96 case '\t':
97 esc = "\\t";
98 break;
99 default:
100 break;
101 }
102 if(esc) {
103 memcpy(buf, esc, 2);
104 return 2;
105 }
106 if(*i >= 0x80) {
107 size_t n = utf8len(i, end);
108 if(n) {
109 memcpy(buf, i, n);
110 *inp = i + n;
111 return n;
112 }
113 /* not UTF-8, escape the byte as if it was ISO-8859-1 */
114 }
115 if(*i < 32 || *i >= 0x80) {
116 buf[0] = '\\';
117 buf[1] = 'u';
118 buf[2] = buf[3] = '0';
119 buf[4] = hex[*i >> 4];
120 buf[5] = hex[*i & 0xf];
121 return JSON_ESC_MAX;
122 }
123 buf[0] = (char)*i;
124 if(lowercase && (*i >= 'A' && *i <= 'Z'))
125 /* do not use tolower() since that is locale specific */
126 buf[0] |= ('a' - 'A');
127 return 1;
128}
130/* provide the given string in dynbuf as a quoted json string, but without the
131 outer quotes. The buffer is not inited by this function.
133 Return 0 on success, non-zero on error. */
134int jsonquoted(const char *in, size_t len, struct dynbuf *out, bool lowercase)
135{
136 const unsigned char *i = (const unsigned char *)in;
137 const unsigned char *in_end = &i[len];
138 CURLcode result = CURLE_OK;
140 while((i < in_end) && !result) {
141 char esc[JSON_ESC_MAX];
142 size_t elen = jsonescape(&i, in_end, lowercase, esc);
143 result = curlx_dyn_addn(out, esc, elen);
144 }
145 if(result)
146 return (int)result;
147 return 0;
148}
150void jsonWriteString(FILE *stream, const char *in, bool lowercase)
151{
152 const unsigned char *i = (const unsigned char *)in;
153 const unsigned char *in_end = &i[strlen(in)];
154 char buf[128];
155 size_t len = 0;
157 /* escape into a small buffer and write it whenever it gets full: there is
158 no size limit to pick and no allocation that can fail and lose the value,
159 and an unbuffered stream gets no write call per byte */
160 fputc('\"', stream);
161 while(i < in_end) {
162 len += jsonescape(&i, in_end, lowercase, &buf[len]);
163 if(len > sizeof(buf) - JSON_ESC_MAX) {
164 fwrite(buf, 1, len, stream);
165 len = 0;
166 }
167 }
168 if(len)
169 fwrite(buf, 1, len, stream);
170 fputc('\"', stream);
171}
173void ourWriteOutJSON(FILE *stream, const struct writeoutvar mappings[],
174 size_t nentries,
175 struct per_transfer *per, CURLcode per_result)
176{
177 size_t i;
179 fputs("{", stream);
181 for(i = 0; i < nentries; i++) {
182 if(mappings[i].writefunc &&
183 mappings[i].writefunc(stream, &mappings[i], per, per_result, TRUE))
184 fputs(",", stream);
185 }
187 /* The variables are sorted in alphabetical order but as a special case
188 curl_version (which is not actually a --write-out variable) is last. */
189 curl_mfprintf(stream, "\"curl_version\":");
190 jsonWriteString(stream, curl_version(), FALSE);
191 curl_mfprintf(stream, "}");
192}
194void headerJSON(FILE *stream, struct per_transfer *per)
195{
196 struct curl_header *header;
197 struct curl_header *prev = NULL;
199 fputc('{', stream);
200 while((header = curl_easy_nextheader(per->curl, CURLH_HEADER, -1,
201 prev)) != NULL) {
202 if(header->amount > 1) {
203 if(!header->index) {
204 /* act on the 0-index entry and pull the others in, then output in a
205 JSON list */
206 size_t a = header->amount;
207 size_t i = 0;
208 char *name = header->name;
209 if(prev)
210 fputs(",\n", stream);
211 jsonWriteString(stream, header->name, TRUE);
212 fputc(':', stream);
213 prev = header;
214 fputc('[', stream);
215 do {
216 jsonWriteString(stream, header->value, FALSE);
217 if(++i >= a)
218 break;
219 fputc(',', stream);
220 if(curl_easy_header(per->curl, name, i, CURLH_HEADER, -1, &header))
221 break;
222 } while(1);
223 fputc(']', stream);
224 }
225 }
226 else {
227 if(prev)
228 fputs(",\n", stream);
229 jsonWriteString(stream, header->name, TRUE);
230 fputc(':', stream);
231 fputc('[', stream);
232 jsonWriteString(stream, header->value, FALSE);
233 fputc(']', stream);
234 prev = header;
235 }
236 }
237 fputs("\n}", stream);
238}
Diagnostics4 git calls · 558 ms
CallStatusSizeTime0558 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 · hit27 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=27 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
fetch2002.6 KiB529 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=529 git.response.bytes=2644 git.response.cache=MISS git.response.duration_ms=529 git.fetch.wants=6e22e3121d36865b5d9c371bee36eedab38b1214 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=2644 git.pkt.payload.max=2623 git.sideband.band1.bytes=2622 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
- a5e1aa60af842c38
- Request
- d0f2cb4f…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format