Artifactspublic-v0

curl

master
ply4 git calls33 ms291 KiB3 cachedprotocol v2
512 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 "curl_setup.h"
26#include "urldata.h"
27#include "sendf.h"
28#include "curl_trc.h"
29#include "headers.h"
30#include "strcase.h"
32#if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_HEADERS_API)
34/* Generate the curl_header struct for the user. This function MUST assign all
35 struct fields in the output struct. */
36static void copy_header_external(struct Curl_header_store *hs,
37 size_t index,
38 size_t amount,
39 struct Curl_llist_node *e,
40 struct curl_header *hout)
42 struct curl_header *h = hout;
43 h->name = hs->name;
44 h->value = hs->value;
45 h->amount = amount;
46 h->index = index;
47 /* this will randomly OR a reserved bit for the sole purpose of making it
48 impossible for applications to do == comparisons, as that would otherwise
49 be tempting and then lead to the reserved bits not being reserved
50 anymore. */
51 h->origin = (unsigned int)(hs->type | (1 << 27));
52 h->anchor = e;
55/* public API */
56CURLHcode curl_easy_header(CURL *curl,
57 const char *name,
58 size_t nameindex,
59 unsigned int origin,
60 int request,
61 struct curl_header **hout)
63 struct Curl_eapi_guard guard;
64 CURLHcode hresult = CURLHE_OK;
65 CURLcode result;
67 if(CURL_EAPI_ENTER(&guard, curl, easy_header, &result)) {
68 struct Curl_easy *data = curl;
69 struct Curl_llist_node *e;
70 struct Curl_llist_node *e_pick = NULL;
71 size_t match = 0;
72 size_t amount = 0;
73 struct Curl_header_store *hs = NULL;
74 struct Curl_header_store *pick = NULL;
75 if(!name || !hout || !data ||
76 (origin > (CURLH_HEADER | CURLH_TRAILER | CURLH_CONNECT | CURLH_1XX |
77 CURLH_PSEUDO)) || !origin || (request < -1)) {
78 hresult = CURLHE_BAD_ARGUMENT;
79 goto out;
80 }
81 if(!Curl_llist_count(&data->state.httphdrs)) {
82 hresult = CURLHE_NOHEADERS; /* no headers available */
83 goto out;
84 }
85 if(request > data->state.requests) {
86 hresult = CURLHE_NOREQUEST;
87 goto out;
88 }
89 if(request == -1)
90 request = data->state.requests;
92 /* we need a first round to count amount of this header */
93 for(e = Curl_llist_head(&data->state.httphdrs); e; e = Curl_node_next(e)) {
94 hs = Curl_node_elem(e);
95 if(curl_strequal(hs->name, name) &&
96 (hs->type & origin) &&
97 (hs->request == request)) {
98 amount++;
99 pick = hs;
100 e_pick = e;
101 }
102 }
103 if(!amount)
104 hresult = CURLHE_MISSING;
105 else if(nameindex >= amount)
106 hresult = CURLHE_BADINDEX;
107 if(hresult)
108 goto out;
110 if(nameindex == amount - 1)
111 /* if the last or only occurrence is what's asked for, then we know it */
112 hs = pick;
113 else {
114 for(e = Curl_llist_head(&data->state.httphdrs); e;
115 e = Curl_node_next(e)) {
116 hs = Curl_node_elem(e);
117 if(curl_strequal(hs->name, name) &&
118 (hs->type & origin) &&
119 (hs->request == request) &&
120 (match++ == nameindex)) {
121 e_pick = e;
122 break;
123 }
124 }
125 if(!e) { /* this should not happen */
126 hresult = CURLHE_MISSING;
127 goto out;
128 }
129 }
130 /* this is the name we want */
131 copy_header_external(hs, nameindex, amount, e_pick,
132 &data->state.headerout[0]);
133 *hout = &data->state.headerout[0];
134 hresult = CURLHE_OK;
135 }
136out:
137 CURL_EAPI_LEAVE(&guard);
138 if(result)
139 hresult = Curl_eapi_hcode(result);
140 return hresult;
143struct nextheader_cache_entry {
144 struct Curl_header_store *header;
145 size_t order;
146};
148static int nextheader_compare(const void *p1, const void *p2)
150 const struct nextheader_cache_entry *e1 = p1;
151 const struct nextheader_cache_entry *e2 = p2;
152 const struct Curl_header_store *h1 = e1->header;
153 const struct Curl_header_store *h2 = e2->header;
154 const char *n1 = h1->name;
155 const char *n2 = h2->name;
157 while(*n1 && *n2) {
158 unsigned char c1 = (unsigned char)Curl_raw_toupper(*n1++);
159 unsigned char c2 = (unsigned char)Curl_raw_toupper(*n2++);
160 if(c1 != c2)
161 return c1 > c2 ? 1 : -1;
162 }
163 if(*n1)
164 return 1;
165 if(*n2)
166 return -1;
167 if(e1->order != e2->order)
168 return e1->order > e2->order ? 1 : -1;
169 return 0;
172static bool nextheader_cache_build(struct Curl_easy *data,
173 unsigned int origin,
174 int request, size_t count)
176 struct nextheader_cache_entry *headers;
177 struct Curl_llist_node *e;
178 size_t i = 0;
179 size_t first;
181 headers = curlx_malloc(sizeof(*headers) * count);
182 if(!headers)
183 return FALSE;
185 for(e = Curl_llist_head(&data->state.httphdrs); e;
186 e = Curl_node_next(e)) {
187 struct Curl_header_store *hs = Curl_node_elem(e);
188 if((hs->type & origin) && (hs->request == request)) {
189 headers[i].header = hs;
190 headers[i].order = i;
191 i++;
192 }
193 }
195 qsort(headers, i, sizeof(*headers), nextheader_compare);
196 for(first = 0; first < i;) {
197 size_t last = first + 1;
198 size_t index;
199 while((last < i) &&
200 curl_strequal(headers[first].header->name,
201 headers[last].header->name))
202 last++;
203 for(index = first; index < last; index++) {
204 headers[index].header->nh_amount = last - first;
205 headers[index].header->nh_index = index - first;
206 }
207 first = last;
208 }
209 curlx_free(headers);
211 data->state.nh_origin = origin;
212 data->state.nh_request = request;
213 data->state.nh_count = count;
214 return TRUE;
217/* public API */
218struct curl_header *curl_easy_nextheader(CURL *curl,
219 unsigned int origin,
220 int request,
221 struct curl_header *prev)
223 struct Curl_eapi_guard guard;
224 struct curl_header *hd = NULL;
225 CURLcode result;
227 if(CURL_EAPI_ENTER(&guard, curl, easy_nextheader, &result)) {
228 struct Curl_easy *data = curl;
229 struct Curl_llist_node *pick;
230 struct Curl_llist_node *e;
231 struct Curl_header_store *hs;
232 size_t amount = 0;
233 size_t index = 0;
234 size_t count;
236 if(request > data->state.requests)
237 goto out;
238 if(request == -1)
239 request = data->state.requests;
241 if(prev) {
242 pick = prev->anchor;
243 if(!pick)
244 /* something is wrong */
245 goto out;
246 pick = Curl_node_next(pick);
247 }
248 else
249 pick = Curl_llist_head(&data->state.httphdrs);
251 if(pick) {
252 /* make sure it is the next header of the desired type */
253 do {
254 hs = Curl_node_elem(pick);
255 if((hs->type & origin) && (hs->request == request))
256 break;
257 pick = Curl_node_next(pick);
258 } while(pick);
259 }
261 if(!pick)
262 /* no more headers available */
263 goto out;
265 hs = Curl_node_elem(pick);
266 count = Curl_llist_count(&data->state.httphdrs);
268 if(((data->state.nh_count == count) &&
269 (data->state.nh_origin == origin) &&
270 (data->state.nh_request == request)) ||
271 nextheader_cache_build(data, origin, request, count)) {
272 amount = hs->nh_amount;
273 index = hs->nh_index;
274 }
275 else {
276 /* count number of occurrences of this name within the mask and figure
277 out the index for the currently selected entry */
278 for(e = Curl_llist_head(&data->state.httphdrs); e;
279 e = Curl_node_next(e)) {
280 struct Curl_header_store *check = Curl_node_elem(e);
281 if(curl_strequal(hs->name, check->name) &&
282 (check->request == request) &&
283 (check->type & origin))
284 amount++;
285 if(e == pick)
286 index = amount - 1;
287 }
288 }
289 copy_header_external(hs, index, amount, pick,
290 &data->state.headerout[1]);
291 hd = &data->state.headerout[1];
292 }
293out:
294 CURL_EAPI_LEAVE(&guard);
295 return hd;
298static CURLcode namevalue(char *header, size_t hlen, unsigned int type,
299 char **name, char **value)
301 char *end = header + hlen - 1; /* point to the last byte */
302 DEBUGASSERT(hlen);
303 *name = header;
305 if(type == CURLH_PSEUDO) {
306 if(*header != ':')
307 return CURLE_BAD_FUNCTION_ARGUMENT;
308 header++;
309 }
311 /* Find the end of the header name */
312 while(*header && (*header != ':'))
313 ++header;
315 if(*header)
316 /* Skip over colon, null it */
317 *header++ = 0;
318 else
319 return CURLE_BAD_FUNCTION_ARGUMENT;
321 /* skip all leading blank letters */
322 while(ISBLANK(*header))
323 header++;
325 *value = header;
327 /* skip all trailing space letters */
328 while((end > header) && ISBLANK(*end))
329 *end-- = 0; /* null-terminate */
330 return CURLE_OK;
333/*
334 * Curl_headers_push() gets passed a full HTTP header to store. It gets called
335 * immediately before the header callback. The header is CRLF, CR or LF
336 * terminated.
337 */
338CURLcode Curl_headers_push(struct Curl_easy *data, const char *header,
339 size_t hlen, /* length of header */
340 unsigned char type)
342 char *value = NULL;
343 char *name = NULL;
344 struct Curl_header_store *hs;
345 CURLcode result = CURLE_OUT_OF_MEMORY;
346 const size_t ilen = hlen;
348 if((header[0] == '\r') || (header[0] == '\n'))
349 /* ignore the body separator */
350 return CURLE_OK;
352 /* trim off newline characters */
353 if(hlen && (header[hlen - 1] == '\n'))
354 hlen--;
355 if(hlen && (header[hlen - 1] == '\r'))
356 hlen--;
357 if(hlen == ilen)
358 /* neither CR nor LF as terminator is not a valid header */
359 return CURLE_WEIRD_SERVER_REPLY;
361 if(ISBLANK(header[0])) {
362 /* pass leading blanks */
363 while(hlen && ISBLANK(*header)) {
364 header++;
365 hlen--;
366 }
367 if(!hlen)
368 return CURLE_WEIRD_SERVER_REPLY;
369 }
370 if(Curl_llist_count(&data->state.httphdrs) >= MAX_HTTP_RESP_HEADER_COUNT) {
371 failf(data, "Too many response headers, %d is max",
372 MAX_HTTP_RESP_HEADER_COUNT);
373 return CURLE_TOO_LARGE;
374 }
376 hs = curlx_calloc(1, sizeof(*hs) + hlen);
377 if(!hs)
378 return CURLE_OUT_OF_MEMORY;
379 memcpy(hs->buffer, header, hlen);
380 hs->buffer[hlen] = 0; /* null-terminate */
382 result = namevalue(hs->buffer, hlen, type, &name, &value);
383 if(!result) {
384 hs->name = name;
385 hs->value = value;
386 hs->type = type;
387 hs->request = data->state.requests;
389 /* insert this node into the list of headers */
390 Curl_llist_append(&data->state.httphdrs, hs, &hs->node);
391 }
392 else {
393 failf(data, "Invalid response header");
394 curlx_free(hs);
395 }
396 return result;
399/*
400 * Curl_headers_reset(). Reset the headers subsystem.
401 */
402static void headers_reset(struct Curl_easy *data)
404 Curl_llist_init(&data->state.httphdrs, NULL);
405 data->state.nh_count = 0;
408struct hds_cw_collect_ctx {
409 struct Curl_cwriter super;
410};
412static CURLcode hds_cw_collect_write(struct Curl_easy *data,
413 struct Curl_cwriter *writer, int type,
414 const char *buf, size_t blen)
416 if((type & CLIENTWRITE_HEADER) && !(type & CLIENTWRITE_STATUS)) {
417 unsigned char htype = (unsigned char)
418 (type & CLIENTWRITE_CONNECT ? CURLH_CONNECT :
419 (type & CLIENTWRITE_1XX ? CURLH_1XX :
420 (type & CLIENTWRITE_TRAILER ? CURLH_TRAILER :
421 CURLH_HEADER)));
422 CURLcode result = Curl_headers_push(data, buf, blen, htype);
423 CURL_TRC_WRITE(data, "header_collect pushed(type=%x, len=%zu) -> %d",
424 htype, blen, (int)result);
425 if(result)
426 return result;
427 }
428 return Curl_cwriter_write(data, writer->next, type, buf, blen);
431static const struct Curl_cwtype hds_cw_collect = {
432 "hds-collect",
433 NULL,
434 0,
435 Curl_cwriter_def_init,
436 hds_cw_collect_write,
437 Curl_cwriter_def_flush,
438 Curl_cwriter_def_close,
439 sizeof(struct hds_cw_collect_ctx)
440};
442CURLcode Curl_headers_init(struct Curl_easy *data)
444 struct Curl_cwriter *writer;
445 CURLcode result;
447 if(data->conn && (data->conn->scheme->protocol & PROTO_FAMILY_HTTP)) {
448 /* avoid installing it twice */
449 if(Curl_cwriter_get_by_name(data, hds_cw_collect.name))
450 return CURLE_OK;
452 result = Curl_cwriter_create(&writer, data, &hds_cw_collect,
453 CURL_CW_PROTOCOL);
454 if(result)
455 return result;
457 result = Curl_cwriter_add(data, writer);
458 if(result) {
459 Curl_cwriter_free(data, writer);
460 return result;
461 }
462 }
463 return CURLE_OK;
466/*
467 * Curl_headers_cleanup(). Free all stored headers and associated memory.
468 */
469CURLcode Curl_headers_cleanup(struct Curl_easy *data)
471 struct Curl_llist_node *e;
472 struct Curl_llist_node *n;
474 for(e = Curl_llist_head(&data->state.httphdrs); e; e = n) {
475 struct Curl_header_store *hs = Curl_node_elem(e);
476 n = Curl_node_next(e);
477 curlx_free(hs);
478 }
479 headers_reset(data);
480 return CURLE_OK;
483#else /* HTTP-disabled builds below */
485CURLHcode curl_easy_header(CURL *easy,
486 const char *name,
487 size_t index,
488 unsigned int origin,
489 int request,
490 struct curl_header **hout)
492 (void)easy;
493 (void)name;
494 (void)index;
495 (void)origin;
496 (void)request;
497 (void)hout;
498 return CURLHE_NOT_BUILT_IN;
501struct curl_header *curl_easy_nextheader(CURL *easy,
502 unsigned int type,
503 int request,
504 struct curl_header *prev)
506 (void)easy;
507 (void)type;
508 (void)request;
509 (void)prev;
510 return NULL;
512#endif
Diagnostics4 git calls · 33 ms
CallStatusSizeTime033 ms
discover200152 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
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 · hit6 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=6
git.response.bytes=149
git.response.cache=HIT
git.response.duration_ms=6
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=86
Open pack map
fetch2004.6 KiB · hit24 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=24
git.response.bytes=4660
git.response.cache=HIT
git.response.duration_ms=24
git.fetch.wants=438eb5464ce724ea8f764692ea7c800eb9e17b08
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=4660
git.pkt.payload.max=4639
git.sideband.band1.bytes=4638
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
a5e2435b3d2ccf56
Request
df169aec…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format