1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
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 http://curl.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
24 #include <curl/mprintf.h>
28 static const char *HOSTHEADER = "Host: www.host.foo.com";
29 static const char *JAR = "log/jar506";
32 /* struct containing data of a thread */
46 static void my_lock(CURL *handle, curl_lock_data data, curl_lock_access laccess,
50 struct userdata *user = (struct userdata *)useptr;
57 case CURL_LOCK_DATA_SHARE:
61 case CURL_LOCK_DATA_DNS:
65 case CURL_LOCK_DATA_COOKIE:
70 fprintf(stderr, "lock: no such data: %d\n", (int)data);
74 /* detect locking of locked locks */
76 printf("lock: double locked %s\n", what);
81 printf("lock: %-6s [%s]: %d\n", what, user->text, user->counter);
86 static void my_unlock(CURL *handle, curl_lock_data data, void *useptr )
89 struct userdata *user = (struct userdata *)useptr;
93 case CURL_LOCK_DATA_SHARE:
97 case CURL_LOCK_DATA_DNS:
101 case CURL_LOCK_DATA_COOKIE:
106 fprintf(stderr, "unlock: no such data: %d\n", (int)data);
110 /* detect unlocking of unlocked locks */
112 printf("unlock: double unlocked %s\n", what);
117 printf("unlock: %-6s [%s]: %d\n", what, user->text, user->counter);
122 /* build host entry */
123 static struct curl_slist *sethost(struct curl_slist *headers)
126 return curl_slist_append(NULL, HOSTHEADER );
130 /* the dummy thread function */
131 static void *fire(void *ptr)
134 struct curl_slist *headers;
135 struct Tdata *tdata = (struct Tdata*)ptr;
139 if ((curl = curl_easy_init()) == NULL) {
140 fprintf(stderr, "curl_easy_init() failed\n");
144 headers = sethost(NULL);
145 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
146 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
147 curl_easy_setopt(curl, CURLOPT_URL, tdata->url);
148 printf( "CURLOPT_SHARE\n" );
149 curl_easy_setopt(curl, CURLOPT_SHARE, tdata->share);
151 printf( "PERFORM\n" );
152 code = curl_easy_perform(curl);
153 if( code != CURLE_OK ) {
154 fprintf(stderr, "perform url '%s' repeat %d failed, curlcode %d\n",
155 tdata->url, i, (int)code);
158 printf( "CLEANUP\n" );
159 curl_easy_cleanup(curl);
160 curl_slist_free_all(headers);
166 /* build request url */
167 static char *suburl(const char *base, int i)
169 return curl_maprintf("%s%.4d", base, i);
177 CURLSHcode scode = CURLSHE_OK;
182 struct curl_slist *headers = NULL;
184 struct userdata user;
186 user.text = (char *)"Pigs in space";
189 printf( "GLOBAL_INIT\n" );
190 if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
191 fprintf(stderr, "curl_global_init() failed\n");
192 return TEST_ERR_MAJOR_BAD;
196 printf( "SHARE_INIT\n" );
197 if ((share = curl_share_init()) == NULL) {
198 fprintf(stderr, "curl_share_init() failed\n");
199 curl_global_cleanup();
200 return TEST_ERR_MAJOR_BAD;
203 if ( CURLSHE_OK == scode ) {
204 printf( "CURLSHOPT_LOCKFUNC\n" );
205 scode = curl_share_setopt( share, CURLSHOPT_LOCKFUNC, my_lock);
207 if ( CURLSHE_OK == scode ) {
208 printf( "CURLSHOPT_UNLOCKFUNC\n" );
209 scode = curl_share_setopt( share, CURLSHOPT_UNLOCKFUNC, my_unlock);
211 if ( CURLSHE_OK == scode ) {
212 printf( "CURLSHOPT_USERDATA\n" );
213 scode = curl_share_setopt( share, CURLSHOPT_USERDATA, &user);
215 if ( CURLSHE_OK == scode ) {
216 printf( "CURL_LOCK_DATA_COOKIE\n" );
217 scode = curl_share_setopt( share, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);
219 if ( CURLSHE_OK == scode ) {
220 printf( "CURL_LOCK_DATA_DNS\n" );
221 scode = curl_share_setopt( share, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
224 if ( CURLSHE_OK != scode ) {
225 fprintf(stderr, "curl_share_setopt() failed\n");
226 curl_share_cleanup(share);
227 curl_global_cleanup();
228 return TEST_ERR_MAJOR_BAD;
231 /* initial cookie manipulation */
232 if ((curl = curl_easy_init()) == NULL) {
233 fprintf(stderr, "curl_easy_init() failed\n");
234 curl_share_cleanup(share);
235 curl_global_cleanup();
236 return TEST_ERR_MAJOR_BAD;
238 printf( "CURLOPT_SHARE\n" );
239 test_setopt( curl, CURLOPT_SHARE, share );
240 printf( "CURLOPT_COOKIELIST injected_and_clobbered\n" );
241 test_setopt( curl, CURLOPT_COOKIELIST,
242 "Set-Cookie: injected_and_clobbered=yes; "
243 "domain=host.foo.com; expires=Sat Feb 2 11:56:27 GMT 2030" );
244 printf( "CURLOPT_COOKIELIST ALL\n" );
245 test_setopt( curl, CURLOPT_COOKIELIST, "ALL" );
246 printf( "CURLOPT_COOKIELIST session\n" );
247 test_setopt( curl, CURLOPT_COOKIELIST, "Set-Cookie: session=elephants" );
248 printf( "CURLOPT_COOKIELIST injected\n" );
249 test_setopt( curl, CURLOPT_COOKIELIST,
250 "Set-Cookie: injected=yes; domain=host.foo.com; "
251 "expires=Sat Feb 2 11:56:27 GMT 2030" );
252 printf( "CURLOPT_COOKIELIST SESS\n" );
253 test_setopt( curl, CURLOPT_COOKIELIST, "SESS" );
254 printf( "CLEANUP\n" );
255 curl_easy_cleanup( curl );
261 for (i=1; i<=THREADS; i++ ) {
263 /* set thread data */
264 tdata.url = suburl( URL, i ); /* must be curl_free()d */
267 /* simulate thread, direct call of "thread" function */
268 printf( "*** run %d\n",i );
271 curl_free( tdata.url );
276 /* fetch a another one and save cookies */
277 printf( "*** run %d\n", i );
278 if ((curl = curl_easy_init()) == NULL) {
279 fprintf(stderr, "curl_easy_init() failed\n");
280 curl_share_cleanup(share);
281 curl_global_cleanup();
282 return TEST_ERR_MAJOR_BAD;
285 url = suburl( URL, i );
286 headers = sethost( NULL );
287 test_setopt( curl, CURLOPT_HTTPHEADER, headers );
288 test_setopt( curl, CURLOPT_URL, url );
289 printf( "CURLOPT_SHARE\n" );
290 test_setopt( curl, CURLOPT_SHARE, share );
291 printf( "CURLOPT_COOKIEJAR\n" );
292 test_setopt( curl, CURLOPT_COOKIEJAR, JAR );
293 printf( "CURLOPT_COOKIELIST FLUSH\n" );
294 test_setopt( curl, CURLOPT_COOKIELIST, "FLUSH" );
296 printf( "PERFORM\n" );
297 curl_easy_perform( curl );
299 /* try to free share, expect to fail because share is in use*/
300 printf( "try SHARE_CLEANUP...\n" );
301 scode = curl_share_cleanup( share );
302 if ( scode==CURLSHE_OK )
304 fprintf(stderr, "curl_share_cleanup succeed but error expected\n");
307 printf( "SHARE_CLEANUP failed, correct\n" );
312 /* clean up last handle */
313 printf( "CLEANUP\n" );
314 curl_easy_cleanup( curl );
317 curl_slist_free_all( headers );
323 printf( "SHARE_CLEANUP\n" );
324 scode = curl_share_cleanup( share );
325 if ( scode!=CURLSHE_OK )
326 fprintf(stderr, "curl_share_cleanup failed, code errno %d\n",
329 printf( "GLOBAL_CLEANUP\n" );
330 curl_global_cleanup();