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 "test.h"
25 #include "memdebug.h"
26
my_lock(CURL * handle,curl_lock_data data,curl_lock_access laccess,void * useptr)27 static void my_lock(CURL *handle, curl_lock_data data,
28 curl_lock_access laccess, void *useptr)
29 {
30 (void)handle;
31 (void)data;
32 (void)laccess;
33 (void)useptr;
34 printf("-> Mutex lock\n");
35 }
36
my_unlock(CURL * handle,curl_lock_data data,void * useptr)37 static void my_unlock(CURL *handle, curl_lock_data data, void *useptr)
38 {
39 (void)handle;
40 (void)data;
41 (void)useptr;
42 printf("<- Mutex unlock\n");
43 }
44
45 /* test function */
test(char * URL)46 int test(char *URL)
47 {
48 CURLcode res = CURLE_OK;
49 CURLSH *share;
50 int i;
51
52 global_init(CURL_GLOBAL_ALL);
53
54 share = curl_share_init();
55 if(!share) {
56 fprintf(stderr, "curl_share_init() failed\n");
57 curl_global_cleanup();
58 return TEST_ERR_MAJOR_BAD;
59 }
60
61 curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT);
62 curl_share_setopt(share, CURLSHOPT_LOCKFUNC, my_lock);
63 curl_share_setopt(share, CURLSHOPT_UNLOCKFUNC, my_unlock);
64
65 /* Loop the transfer and cleanup the handle properly every lap. This will
66 still reuse connections since the pool is in the shared object! */
67
68 for(i = 0; i < 3; i++) {
69 CURL *curl = curl_easy_init();
70 if(curl) {
71 curl_easy_setopt(curl, CURLOPT_URL, URL);
72
73 /* use the share object */
74 curl_easy_setopt(curl, CURLOPT_SHARE, share);
75
76 /* Perform the request, res will get the return code */
77 res = curl_easy_perform(curl);
78 /* Check for errors */
79 if(res != CURLE_OK)
80 fprintf(stderr, "curl_easy_perform() failed: %s\n",
81 curl_easy_strerror(res));
82
83 /* always cleanup */
84 curl_easy_cleanup(curl);
85 }
86 }
87
88 curl_share_cleanup(share);
89 curl_global_cleanup();
90
91 return 0;
92 }
93