xref: /curl/docs/libcurl/opts/CURLOPT_SHARE.md (revision 5a488251)
1---
2c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
3SPDX-License-Identifier: curl
4Title: CURLOPT_SHARE
5Section: 3
6Source: libcurl
7See-also:
8  - CURLOPT_COOKIE (3)
9  - CURLSHOPT_SHARE (3)
10Protocol:
11  - All
12Added-in: 7.10
13---
14
15# NAME
16
17CURLOPT_SHARE - share handle to use
18
19# SYNOPSIS
20
21~~~c
22#include <curl/curl.h>
23
24CURLcode curl_easy_setopt(CURL *handle, CURLOPT_SHARE, CURLSH *share);
25~~~
26
27# DESCRIPTION
28
29Pass a *share* handle as a parameter. The share handle must have been
30created by a previous call to curl_share_init(3). Setting this option,
31makes this curl handle use the data from the shared handle instead of keeping
32the data to itself. This enables several curl handles to share data. If the
33curl handles are used simultaneously in multiple threads, you **MUST** use
34the locking methods in the share handle. See curl_share_setopt(3) for
35details.
36
37If you add a share that is set to share cookies, your easy handle uses that
38cookie cache and get the cookie engine enabled. If you stop sharing an object
39that was using cookies (or change to another object that does not share
40cookies), the easy handle gets its cookie engine disabled.
41
42Data that the share object is not set to share is dealt with the usual way, as
43if no share was used.
44
45Set this option to NULL again to stop using that share object.
46
47# DEFAULT
48
49NULL
50
51# %PROTOCOLS%
52
53# EXAMPLE
54
55~~~c
56int main(void)
57{
58  CURL *curl = curl_easy_init();
59  CURL *curl2 = curl_easy_init(); /* a second handle */
60  if(curl) {
61    CURLcode res;
62    CURLSH *shobject = curl_share_init();
63    curl_share_setopt(shobject, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);
64
65    curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/");
66    curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "");
67    curl_easy_setopt(curl, CURLOPT_SHARE, shobject);
68    res = curl_easy_perform(curl);
69    curl_easy_cleanup(curl);
70
71    /* the second handle shares cookies from the first */
72    curl_easy_setopt(curl2, CURLOPT_URL, "https://example.com/second");
73    curl_easy_setopt(curl2, CURLOPT_COOKIEFILE, "");
74    curl_easy_setopt(curl2, CURLOPT_SHARE, shobject);
75    res = curl_easy_perform(curl2);
76    curl_easy_cleanup(curl2);
77
78    curl_share_cleanup(shobject);
79  }
80}
81~~~
82
83# %AVAILABILITY%
84
85# RETURN VALUE
86
87Returns CURLE_OK
88