lib1554.c (2981B)
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 "first.h" 25 26 #include "memdebug.h" 27 28 static const char *ldata_names[] = { 29 "NONE", 30 "SHARE", 31 "COOKIE", 32 "DNS", 33 "SESSION", 34 "CONNECT", 35 "PSL", 36 "HSTS", 37 "NULL", 38 }; 39 40 static void t1554_test_lock(CURL *handle, curl_lock_data data, 41 curl_lock_access laccess, void *useptr) 42 { 43 (void)handle; 44 (void)data; 45 (void)laccess; 46 (void)useptr; 47 curl_mprintf("-> Mutex lock %s\n", ldata_names[data]); 48 } 49 50 static void t1554_test_unlock(CURL *handle, curl_lock_data data, void *useptr) 51 { 52 (void)handle; 53 (void)data; 54 (void)useptr; 55 curl_mprintf("<- Mutex unlock %s\n", ldata_names[data]); 56 } 57 58 /* test function */ 59 static CURLcode test_lib1554(char *URL) 60 { 61 CURLcode res = CURLE_OK; 62 CURLSH *share = NULL; 63 int i; 64 65 global_init(CURL_GLOBAL_ALL); 66 67 share = curl_share_init(); 68 if(!share) { 69 curl_mfprintf(stderr, "curl_share_init() failed\n"); 70 goto test_cleanup; 71 } 72 73 curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT); 74 curl_share_setopt(share, CURLSHOPT_LOCKFUNC, t1554_test_lock); 75 curl_share_setopt(share, CURLSHOPT_UNLOCKFUNC, t1554_test_unlock); 76 77 /* Loop the transfer and cleanup the handle properly every lap. This will 78 still reuse connections since the pool is in the shared object! */ 79 80 for(i = 0; i < 3; i++) { 81 CURL *curl = curl_easy_init(); 82 if(curl) { 83 curl_easy_setopt(curl, CURLOPT_URL, URL); 84 85 /* use the share object */ 86 curl_easy_setopt(curl, CURLOPT_SHARE, share); 87 88 /* Perform the request, res will get the return code */ 89 res = curl_easy_perform(curl); 90 91 /* always cleanup */ 92 curl_easy_cleanup(curl); 93 94 /* Check for errors */ 95 if(res != CURLE_OK) { 96 curl_mfprintf(stderr, "curl_easy_perform() failed: %s\n", 97 curl_easy_strerror(res)); 98 goto test_cleanup; 99 } 100 } 101 } 102 103 test_cleanup: 104 curl_share_cleanup(share); 105 curl_global_cleanup(); 106 107 return res; 108 }