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 "curl_setup.h"
26#include "urldata.h"
27#include "multiif.h"
28#include "curl_threads.h"
29#include "curl_share.h"
30#include "vtls/vtls.h"
31#include "vtls/vtls_scache.h"
32#include "hsts.h"
33#include "url.h"
35static void share_destroy(struct Curl_share *share)
36{
37 if(!share)
38 return;
40 if(share->specifier & (1 << CURL_LOCK_DATA_CONNECT)) {
41 Curl_cpool_destroy(&share->cpool, share->admin);
42 }
44 Curl_dnscache_destroy(&share->dnscache);
46#if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
47 Curl_cookie_cleanup(share->cookies);
48#endif
50#ifndef CURL_DISABLE_HSTS
51 Curl_hsts_cleanup(&share->hsts);
52#endif
54#ifdef USE_SSL
55 if(share->ssl_scache) {
56 Curl_ssl_scache_destroy(share->ssl_scache);
57 share->ssl_scache = NULL;
58 }
59#endif
61 Curl_psl_destroy(&share->psl);
62 Curl_close(&share->admin);
64#ifdef USE_MUTEX
65 Curl_mutex_destroy(&share->lock);
66#endif
67 curlx_memzero(share, sizeof(*share));
68 curlx_free(share);
69}
71CURLSH *curl_share_init(void)
72{
73 struct Curl_share *share = curlx_calloc(1, sizeof(struct Curl_share));
74 if(share) {
75 share->magic = CURL_GOOD_SHARE;
76 share->specifier |= (1 << CURL_LOCK_DATA_SHARE);
77#ifdef USE_MUTEX
78 Curl_mutex_init(&share->lock);
79#endif
80 share->ref_count = 1;
81 Curl_dnscache_init(&share->dnscache, 23);
82 share->admin = curl_easy_init();
83 if(!share->admin) {
84 share_destroy(share);
85 return NULL;
86 }
87 /* admin handles have mid 0 */
88 share->admin->mid = 0;
89 share->admin->state.internal = TRUE;
90#ifdef DEBUGBUILD
91 if(getenv("CURL_DEBUG"))
92 share->admin->set.verbose = TRUE;
93#endif
94 }
95 return share;
96}
98static uint32_t share_ref_inc(struct Curl_share *share)
99{
100 uint32_t n;
101#ifdef USE_MUTEX
102 Curl_mutex_acquire(&share->lock);
103 n = ++share->ref_count;
104 share->has_been_shared = TRUE;
105 Curl_mutex_release(&share->lock);
106#else
107 n = ++share->ref_count;
108 share->has_been_shared = TRUE;
109#endif
110 return n;
111}
113static uint32_t share_ref_dec(struct Curl_share *share)
114{
115 uint32_t n;
116#ifdef USE_MUTEX
117 Curl_mutex_acquire(&share->lock);
118 DEBUGASSERT(share->ref_count);
119 n = --share->ref_count;
120 Curl_mutex_release(&share->lock);
121#else
122 n = --share->ref_count;
123#endif
124 return n;
125}
127static bool share_has_been_shared(struct Curl_share *share)
128{
129 bool was_shared;
130#ifdef USE_MUTEX
131 Curl_mutex_acquire(&share->lock);
132 was_shared = share->has_been_shared;
133 Curl_mutex_release(&share->lock);
134#else
135 was_shared = share->has_been_shared;
136#endif
137 return was_shared;
138}
140static bool share_lock_acquire(struct Curl_share *share,
141 struct Curl_easy *data)
142{
143 if(share->lockfunc && share->unlockfunc &&
144 (data || share_has_been_shared(share))) {
145 share->lockfunc(data, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE,
146 share->clientdata);
147 return TRUE;
148 }
149 return FALSE;
150}
152static void share_lock_release(struct Curl_share *share,
153 struct Curl_easy *data,
154 bool locked)
155{
156 if(locked) {
157 DEBUGASSERT(share->unlockfunc);
158 if(share->unlockfunc)
159 share->unlockfunc(data, CURL_LOCK_DATA_SHARE, share->clientdata);
160 }
161}
163static bool share_in_use(struct Curl_share *share)
164{
165 bool in_use;
166#ifdef USE_MUTEX
167 Curl_mutex_acquire(&share->lock);
168 in_use = (share->ref_count > 1);
169 Curl_mutex_release(&share->lock);
170#else
171 bool locked = share_lock_acquire(share, NULL);
172 in_use = (share->ref_count > 1);
173 share_lock_release(share, NULL, locked);
174#endif
175 return in_use;
176}
178static void share_unlink(struct Curl_share **pshare,
179 struct Curl_easy *data,
180 bool locked)
181{
182 struct Curl_share *share = *pshare;
183 uint32_t n;
185 *pshare = NULL;
186 n = share_ref_dec(share);
187 if(locked)
188 share_lock_release(share, data, locked);
189 if(!n) /* last reference gone */
190 share_destroy(share);
191}
193#undef curl_share_setopt
194CURLSHcode curl_share_setopt(CURLSH *sh, CURLSHoption option, ...)
195{
196 va_list param;
197 int type;
198 curl_lock_function lockfunc;
199 curl_unlock_function unlockfunc;
200 void *ptr;
201 CURLSHcode res = CURLSHE_OK;
202 struct Curl_share *share = sh;
204 if(!GOOD_SHARE_HANDLE(share))
205 return CURLSHE_INVALID;
207 if(share_in_use(share)) {
208 /* do not allow setting options while one or more handles are already
209 using this share */
210 return CURLSHE_IN_USE;
211 }
213 va_start(param, option);
215 switch(option) {
216 case CURLSHOPT_SHARE:
217 /* this is a type this share will share */
218 type = va_arg(param, int);
220 switch(type) {
221 case CURL_LOCK_DATA_DNS:
222 break;
224 case CURL_LOCK_DATA_COOKIE:
225#if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
226 if(!share->cookies) {
227 share->cookies = Curl_cookie_init();
228 if(!share->cookies)
229 res = CURLSHE_NOMEM;
230 }
231#else /* CURL_DISABLE_HTTP || CURL_DISABLE_COOKIES */
232 res = CURLSHE_NOT_BUILT_IN;
233#endif
234 break;
236 case CURL_LOCK_DATA_HSTS:
237#ifndef CURL_DISABLE_HSTS
238 if(!share->hsts) {
239 share->hsts = Curl_hsts_init();
240 if(!share->hsts)
241 res = CURLSHE_NOMEM;
242 }
243#else /* CURL_DISABLE_HSTS */
244 res = CURLSHE_NOT_BUILT_IN;
245#endif
246 break;
248 case CURL_LOCK_DATA_SSL_SESSION:
249#ifdef USE_SSL
250 if(!share->ssl_scache) {
251 /* There is no way (yet) for the application to configure the
252 * session cache size, shared between many transfers. As for curl
253 * itself, a high session count will impact startup time. Also, the
254 * scache is not optimized for several hundreds of peers.
255 * Keep it at a reasonable level. */
256 if(Curl_ssl_scache_create(25, 2, &share->ssl_scache))
257 res = CURLSHE_NOMEM;
258 }
259#else
260 res = CURLSHE_NOT_BUILT_IN;
261#endif
262 break;
264 case CURL_LOCK_DATA_CONNECT:
265 /* It is safe to set this option several times on a share. */
266 if(!share->cpool.initialized) {
267 Curl_cpool_init(&share->cpool, share, 103);
268 }
269 break;
271 case CURL_LOCK_DATA_PSL:
272#ifndef USE_LIBPSL
273 res = CURLSHE_NOT_BUILT_IN;
274#endif
275 break;
277 default:
278 res = CURLSHE_BAD_OPTION;
279 }
280 if(!res)
281 share->specifier |= (unsigned int)(1 << type);
282 break;
284 case CURLSHOPT_UNSHARE:
285 /* this is a type this share will no longer share */
286 type = va_arg(param, int);
287 switch(type) {
288 case CURL_LOCK_DATA_DNS:
289 break;
291 case CURL_LOCK_DATA_COOKIE:
292#if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
293 if(share->cookies) {
294 Curl_cookie_cleanup(share->cookies);
295 share->cookies = NULL;
296 }
297#else /* CURL_DISABLE_HTTP || CURL_DISABLE_COOKIES */
298 res = CURLSHE_NOT_BUILT_IN;
299#endif
300 break;
302 case CURL_LOCK_DATA_HSTS:
303#ifndef CURL_DISABLE_HSTS
304 if(share->hsts) {
305 Curl_hsts_cleanup(&share->hsts);
306 }
307#else /* CURL_DISABLE_HSTS */
308 res = CURLSHE_NOT_BUILT_IN;
309#endif
310 break;
312 case CURL_LOCK_DATA_SSL_SESSION:
313#ifdef USE_SSL
314 if(share->ssl_scache) {
315 Curl_ssl_scache_destroy(share->ssl_scache);
316 share->ssl_scache = NULL;
317 }
318#else
319 res = CURLSHE_NOT_BUILT_IN;
320#endif
321 break;
323 case CURL_LOCK_DATA_CONNECT:
324 break;
326 default:
327 res = CURLSHE_BAD_OPTION;
328 break;
329 }
330 if(!res)
331 share->specifier &= ~(unsigned int)(1 << type);
332 break;
334 case CURLSHOPT_LOCKFUNC:
335 lockfunc = va_arg(param, curl_lock_function);
336 share->lockfunc = lockfunc;
337 break;
339 case CURLSHOPT_UNLOCKFUNC:
340 unlockfunc = va_arg(param, curl_unlock_function);
341 share->unlockfunc = unlockfunc;
342 break;
344 case CURLSHOPT_USERDATA:
345 ptr = va_arg(param, void *);
346 share->clientdata = ptr;
347 break;
349 default:
350 res = CURLSHE_BAD_OPTION;
351 break;
352 }
354 va_end(param);
356 return res;
357}
359CURLSHcode curl_share_cleanup(CURLSH *sh)
360{
361 struct Curl_share *share = sh;
362 bool locked;
363 if(!GOOD_SHARE_HANDLE(share))
364 return CURLSHE_INVALID;
366 if(share_in_use(share))
367 return CURLSHE_IN_USE;
369 locked = share_lock_acquire(share, NULL);
370 share_unlink(&share, NULL, locked);
371 return CURLSHE_OK;
372}
374CURLSHcode Curl_share_lock_share(struct Curl_share *share,
375 struct Curl_easy *data,
376 curl_lock_data type,
377 curl_lock_access accesstype)
378{
379 if(!share)
380 return CURLSHE_INVALID;
382 if(share->specifier & (unsigned int)(1 << type) &&
383 share->lockfunc) /* only call this if set! */
384 share->lockfunc(data, type, accesstype, share->clientdata);
385 /* else if we do not share this, pretend successful lock */
387 return CURLSHE_OK;
388}
390CURLSHcode Curl_share_lock(struct Curl_easy *data, curl_lock_data type,
391 curl_lock_access accesstype)
392{
393 return Curl_share_lock_share(data->share, data, type, accesstype);
394}
396CURLSHcode Curl_share_unlock_share(struct Curl_share *share,
397 struct Curl_easy *data,
398 curl_lock_data type)
399{
400 if(!share)
401 return CURLSHE_INVALID;
403 if(share->specifier & (unsigned int)(1 << type) &&
404 share->unlockfunc) /* only call this if set! */
405 share->unlockfunc(data, type, share->clientdata);
407 return CURLSHE_OK;
408}
410CURLSHcode Curl_share_unlock(struct Curl_easy *data, curl_lock_data type)
411{
412 return Curl_share_unlock_share(data->share, data, type);
413}
415CURLcode Curl_share_easy_unlink(struct Curl_easy *data)
416{
417 struct Curl_share *share = data->share;
419 if(share) {
420 bool locked = share_lock_acquire(share, data);
422 /* If share caches connections, detach any existing connection and
423 * forget its identifier. */
424 if((share->specifier & (1 << CURL_LOCK_DATA_CONNECT))) {
425 if(data->conn)
426 Curl_detach_connection(data);
427 data->state.last_conn_id = -1;
428 data->state.last_cpid = UINT32_MAX;
429 }
431#if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
432 if(share->cookies == data->cookies)
433 data->cookies = NULL;
434#endif
436#ifndef CURL_DISABLE_HSTS
437 if(share->hsts == data->hsts)
438 data->hsts = NULL;
439#endif
440#ifdef USE_LIBPSL
441 if(&share->psl == data->psl)
442 data->psl = data->multi ? &data->multi->psl : NULL;
443#endif
445 share_unlink(&data->share, data, locked);
446 }
447 return CURLE_OK;
448}
450CURLcode Curl_share_easy_link(struct Curl_easy *data,
451 struct Curl_share *share)
452{
453 if(data->share) {
454 DEBUGASSERT(0);
455 return CURLE_FAILED_INIT;
456 }
458 if(share) {
459 bool locked = share_lock_acquire(share, data);
461 /* Forget identifiers from the previously used connection cache. */
462 if((share->specifier & (1 << CURL_LOCK_DATA_CONNECT))) {
463 data->state.last_conn_id = -1;
464 data->state.last_cpid = UINT32_MAX;
465 }
467 share_ref_inc(share);
468 data->share = share;
470#if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
471 if(share->cookies) {
472 /* use shared cookie list, first free own one if any */
473 Curl_cookie_cleanup(data->cookies);
474 /* enable cookies since we now use a share that uses cookies! */
475 data->cookies = share->cookies;
476 }
477#endif /* CURL_DISABLE_HTTP */
478#ifndef CURL_DISABLE_HSTS
479 if(share->hsts) {
480 /* first free the private one if any */
481 Curl_hsts_cleanup(&data->hsts);
482 data->hsts = share->hsts;
483 }
484#endif
485#ifdef USE_LIBPSL
486 if(share->specifier & (1 << CURL_LOCK_DATA_PSL))
487 data->psl = &share->psl;
488#endif
490 /* check for host cache not needed,
491 * it will be done by curl_easy_perform */
492 share_lock_release(share, data, locked);
493 }
494 return CURLE_OK;
495}
Diagnostics4 git calls · 52 ms
CallStatusSizeTime052 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=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 · 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 · hit29 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=20 git.response.bytes=293470 git.response.cache=HIT git.response.duration_ms=29 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
fetch2003.5 KiB · hit20 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=3567 git.response.cache=HIT git.response.duration_ms=20 git.fetch.wants=21ed3e9ec6798e753fd9c096a33f18d12e2dae0a 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=3567 git.pkt.payload.max=3546 git.sideband.band1.bytes=3545 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
- a5e194f7de90ddf0
- Request
- 72317608…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format