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