1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
15 #include <curl/mprintf.h>
19 static const char *HOSTHEADER = "Host: www.host.foo.com";
20 static const char *JAR = "log/jar506";
23 /* struct containing data of a thread */
35 static void my_lock(CURL *handle, curl_lock_data data, curl_lock_access laccess,
39 struct userdata *user = (struct userdata *)useptr;
45 case CURL_LOCK_DATA_SHARE:
48 case CURL_LOCK_DATA_DNS:
51 case CURL_LOCK_DATA_COOKIE:
55 fprintf(stderr, "lock: no such data: %d\n", (int)data);
58 printf("lock: %-6s [%s]: %d\n", what, user->text, user->counter);
63 static void my_unlock(CURL *handle, curl_lock_data data, void *useptr )
66 struct userdata *user = (struct userdata *)useptr;
69 case CURL_LOCK_DATA_SHARE:
72 case CURL_LOCK_DATA_DNS:
75 case CURL_LOCK_DATA_COOKIE:
79 fprintf(stderr, "unlock: no such data: %d\n", (int)data);
82 printf("unlock: %-6s [%s]: %d\n", what, user->text, user->counter);
87 /* build host entry */
88 static struct curl_slist *sethost(struct curl_slist *headers)
91 return curl_slist_append(NULL, HOSTHEADER );
95 /* the dummy thread function */
96 static void *fire(void *ptr)
99 struct curl_slist *headers;
100 struct Tdata *tdata = (struct Tdata*)ptr;
104 if ((curl = curl_easy_init()) == NULL) {
105 fprintf(stderr, "curl_easy_init() failed\n");
109 headers = sethost(NULL);
110 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
111 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
112 curl_easy_setopt(curl, CURLOPT_URL, tdata->url);
113 printf( "CURLOPT_SHARE\n" );
114 curl_easy_setopt(curl, CURLOPT_SHARE, tdata->share);
116 printf( "PERFORM\n" );
117 code = curl_easy_perform(curl);
118 if( code != CURLE_OK ) {
119 fprintf(stderr, "perform url '%s' repeat %d failed, curlcode %d\n",
120 tdata->url, i, (int)code);
123 printf( "CLEANUP\n" );
124 curl_easy_cleanup(curl);
125 curl_slist_free_all(headers);
131 /* build request url */
132 static char *suburl(const char *base, int i)
134 return curl_maprintf("%s%.4d", base, i);
142 CURLSHcode scode = CURLSHE_OK;
147 struct curl_slist *headers;
149 struct userdata user;
151 user.text = (char *)"Pigs in space";
154 printf( "GLOBAL_INIT\n" );
155 if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
156 fprintf(stderr, "curl_global_init() failed\n");
157 return TEST_ERR_MAJOR_BAD;
161 printf( "SHARE_INIT\n" );
162 if ((share = curl_share_init()) == NULL) {
163 fprintf(stderr, "curl_share_init() failed\n");
164 curl_global_cleanup();
165 return TEST_ERR_MAJOR_BAD;
168 if ( CURLSHE_OK == scode ) {
169 printf( "CURLSHOPT_LOCKFUNC\n" );
170 scode = curl_share_setopt( share, CURLSHOPT_LOCKFUNC, my_lock);
172 if ( CURLSHE_OK == scode ) {
173 printf( "CURLSHOPT_UNLOCKFUNC\n" );
174 scode = curl_share_setopt( share, CURLSHOPT_UNLOCKFUNC, my_unlock);
176 if ( CURLSHE_OK == scode ) {
177 printf( "CURLSHOPT_USERDATA\n" );
178 scode = curl_share_setopt( share, CURLSHOPT_USERDATA, &user);
180 if ( CURLSHE_OK == scode ) {
181 printf( "CURL_LOCK_DATA_COOKIE\n" );
182 scode = curl_share_setopt( share, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);
184 if ( CURLSHE_OK == scode ) {
185 printf( "CURL_LOCK_DATA_DNS\n" );
186 scode = curl_share_setopt( share, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
189 if ( CURLSHE_OK != scode ) {
190 fprintf(stderr, "curl_share_setopt() failed\n");
191 curl_share_cleanup(share);
192 curl_global_cleanup();
193 return TEST_ERR_MAJOR_BAD;
200 for (i=1; i<=THREADS; i++ ) {
202 /* set thread data */
203 tdata.url = suburl( URL, i ); /* must be curl_free()d */
206 /* simulate thread, direct call of "thread" function */
207 printf( "*** run %d\n",i );
210 curl_free( tdata.url );
215 /* fetch a another one and save cookies */
216 printf( "*** run %d\n", i );
217 if ((curl = curl_easy_init()) == NULL) {
218 fprintf(stderr, "curl_easy_init() failed\n");
219 curl_share_cleanup(share);
220 curl_global_cleanup();
221 return TEST_ERR_MAJOR_BAD;
224 url = suburl( URL, i );
225 headers = sethost( NULL );
226 test_setopt( curl, CURLOPT_HTTPHEADER, headers );
227 test_setopt( curl, CURLOPT_URL, url );
228 printf( "CURLOPT_SHARE\n" );
229 test_setopt( curl, CURLOPT_SHARE, share );
230 printf( "CURLOPT_COOKIEJAR\n" );
231 test_setopt( curl, CURLOPT_COOKIEJAR, JAR );
233 printf( "PERFORM\n" );
234 curl_easy_perform( curl );
236 /* try to free share, expect to fail because share is in use*/
237 printf( "try SHARE_CLEANUP...\n" );
238 scode = curl_share_cleanup( share );
239 if ( scode==CURLSHE_OK )
241 fprintf(stderr, "curl_share_cleanup succeed but error expected\n");
244 printf( "SHARE_CLEANUP failed, correct\n" );
249 /* clean up last handle */
250 printf( "CLEANUP\n" );
251 curl_easy_cleanup( curl );
252 curl_slist_free_all( headers );
257 printf( "SHARE_CLEANUP\n" );
258 scode = curl_share_cleanup( share );
259 if ( scode!=CURLSHE_OK )
260 fprintf(stderr, "curl_share_cleanup failed, code errno %d\n",
263 printf( "GLOBAL_CLEANUP\n" );
264 curl_global_cleanup();