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