CURLOPT_SHARE.md (2398B)
1 --- 2 c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 3 SPDX-License-Identifier: curl 4 Title: CURLOPT_SHARE 5 Section: 3 6 Source: libcurl 7 See-also: 8 - CURLOPT_COOKIE (3) 9 - CURLSHOPT_SHARE (3) 10 Protocol: 11 - All 12 Added-in: 7.10 13 --- 14 15 # NAME 16 17 CURLOPT_SHARE - share handle to use 18 19 # SYNOPSIS 20 21 ~~~c 22 #include <curl/curl.h> 23 24 CURLcode curl_easy_setopt(CURL *handle, CURLOPT_SHARE, CURLSH *share); 25 ~~~ 26 27 # DESCRIPTION 28 29 Pass a *share* handle as a parameter. The share handle must have been 30 created by a previous call to curl_share_init(3). Setting this option, 31 makes this curl handle use the data from the shared handle instead of keeping 32 the data to itself. This enables several curl handles to share data. If the 33 curl handles are used simultaneously in multiple threads, you **MUST** use 34 the locking methods in the share handle. See curl_share_setopt(3) for 35 details. 36 37 If you add a share that is set to share cookies, your easy handle uses that 38 cookie cache and get the cookie engine enabled. If you stop sharing an object 39 that was using cookies (or change to another object that does not share 40 cookies), the easy handle gets its cookie engine disabled. 41 42 Data that the share object is not set to share is dealt with the usual way, as 43 if no share was used. 44 45 Set this option to NULL again to stop using that share object. 46 47 # DEFAULT 48 49 NULL 50 51 # %PROTOCOLS% 52 53 # EXAMPLE 54 55 ~~~c 56 int 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 87 curl_easy_setopt(3) returns a CURLcode indicating success or error. 88 89 CURLE_OK (0) means everything was OK, non-zero means an error occurred, see 90 libcurl-errors(3).