1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
16 #include <curl/mprintf.h>
20 static const char *HOSTHEADER = "Host: www.host.foo.com";
21 static const char *JAR = "log/jar506";
24 /* struct containing data of a thread */
36 static void my_lock(CURL *handle, curl_lock_data data, curl_lock_access laccess,
40 struct userdata *user = (struct userdata *)useptr;
46 case CURL_LOCK_DATA_SHARE:
49 case CURL_LOCK_DATA_DNS:
52 case CURL_LOCK_DATA_COOKIE:
56 fprintf(stderr, "lock: no such data: %d\n", (int)data);
59 printf("lock: %-6s [%s]: %d\n", what, user->text, user->counter);
64 static void my_unlock(CURL *handle, curl_lock_data data, void *useptr )
67 struct userdata *user = (struct userdata *)useptr;
70 case CURL_LOCK_DATA_SHARE:
73 case CURL_LOCK_DATA_DNS:
76 case CURL_LOCK_DATA_COOKIE:
80 fprintf(stderr, "unlock: no such data: %d\n", (int)data);
83 printf("unlock: %-6s [%s]: %d\n", what, user->text, user->counter);
88 /* build host entry */
89 static struct curl_slist *sethost(struct curl_slist *headers)
92 return curl_slist_append(NULL, HOSTHEADER );
96 /* the dummy thread function */
97 static void *fire(void *ptr)
100 struct curl_slist *headers;
101 struct Tdata *tdata = (struct Tdata*)ptr;
105 if ((curl = curl_easy_init()) == NULL) {
106 fprintf(stderr, "curl_easy_init() failed\n");
110 headers = sethost(NULL);
111 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
112 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
113 curl_easy_setopt(curl, CURLOPT_URL, tdata->url);
114 printf( "CURLOPT_SHARE\n" );
115 curl_easy_setopt(curl, CURLOPT_SHARE, tdata->share);
117 printf( "PERFORM\n" );
118 code = curl_easy_perform(curl);
119 if( code != CURLE_OK ) {
120 fprintf(stderr, "perform url '%s' repeat %d failed, curlcode %d\n",
121 tdata->url, i, (int)code);
124 printf( "CLEANUP\n" );
125 curl_easy_cleanup(curl);
126 curl_slist_free_all(headers);
132 /* build request url */
133 static char *suburl(const char *base, int i)
135 return curl_maprintf("%s%.4d", base, i);
143 CURLSHcode scode = CURLSHE_OK;
148 struct curl_slist *headers;
150 struct userdata user;
152 user.text = (char *)"Pigs in space";
155 printf( "GLOBAL_INIT\n" );
156 if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
157 fprintf(stderr, "curl_global_init() failed\n");
158 return TEST_ERR_MAJOR_BAD;
162 printf( "SHARE_INIT\n" );
163 if ((share = curl_share_init()) == NULL) {
164 fprintf(stderr, "curl_share_init() failed\n");
165 curl_global_cleanup();
166 return TEST_ERR_MAJOR_BAD;
169 if ( CURLSHE_OK == scode ) {
170 printf( "CURLSHOPT_LOCKFUNC\n" );
171 scode = curl_share_setopt( share, CURLSHOPT_LOCKFUNC, my_lock);
173 if ( CURLSHE_OK == scode ) {
174 printf( "CURLSHOPT_UNLOCKFUNC\n" );
175 scode = curl_share_setopt( share, CURLSHOPT_UNLOCKFUNC, my_unlock);
177 if ( CURLSHE_OK == scode ) {
178 printf( "CURLSHOPT_USERDATA\n" );
179 scode = curl_share_setopt( share, CURLSHOPT_USERDATA, &user);
181 if ( CURLSHE_OK == scode ) {
182 printf( "CURL_LOCK_DATA_COOKIE\n" );
183 scode = curl_share_setopt( share, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);
185 if ( CURLSHE_OK == scode ) {
186 printf( "CURL_LOCK_DATA_DNS\n" );
187 scode = curl_share_setopt( share, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
190 if ( CURLSHE_OK != scode ) {
191 fprintf(stderr, "curl_share_setopt() failed\n");
192 curl_share_cleanup(share);
193 curl_global_cleanup();
194 return TEST_ERR_MAJOR_BAD;
201 for (i=1; i<=THREADS; i++ ) {
203 /* set thread data */
204 tdata.url = suburl( URL, i ); /* must be curl_free()d */
207 /* simulate thread, direct call of "thread" function */
208 printf( "*** run %d\n",i );
211 curl_free( tdata.url );
216 /* fetch a another one and save cookies */
217 printf( "*** run %d\n", i );
218 if ((curl = curl_easy_init()) == NULL) {
219 fprintf(stderr, "curl_easy_init() failed\n");
220 curl_share_cleanup(share);
221 curl_global_cleanup();
222 return TEST_ERR_MAJOR_BAD;
225 url = suburl( URL, i );
226 headers = sethost( NULL );
227 test_setopt( curl, CURLOPT_HTTPHEADER, headers );
228 test_setopt( curl, CURLOPT_URL, url );
229 printf( "CURLOPT_SHARE\n" );
230 test_setopt( curl, CURLOPT_SHARE, share );
231 printf( "CURLOPT_COOKIEJAR\n" );
232 test_setopt( curl, CURLOPT_COOKIEJAR, JAR );
234 printf( "PERFORM\n" );
235 curl_easy_perform( curl );
237 /* try to free share, expect to fail because share is in use*/
238 printf( "try SHARE_CLEANUP...\n" );
239 scode = curl_share_cleanup( share );
240 if ( scode==CURLSHE_OK )
242 fprintf(stderr, "curl_share_cleanup succeed but error expected\n");
245 printf( "SHARE_CLEANUP failed, correct\n" );
250 /* clean up last handle */
251 printf( "CLEANUP\n" );
252 curl_easy_cleanup( curl );
253 curl_slist_free_all( headers );
258 printf( "SHARE_CLEANUP\n" );
259 scode = curl_share_cleanup( share );
260 if ( scode!=CURLSHE_OK )
261 fprintf(stderr, "curl_share_cleanup failed, code errno %d\n",
264 printf( "GLOBAL_CLEANUP\n" );
265 curl_global_cleanup();