lib506.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2014, 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 http://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 <curl/mprintf.h>
  24. #include "memdebug.h"
  25. static const char *HOSTHEADER = "Host: www.host.foo.com";
  26. static const char *JAR = "log/jar506";
  27. #define THREADS 2
  28. /* struct containing data of a thread */
  29. struct Tdata {
  30. CURLSH *share;
  31. char *url;
  32. };
  33. struct userdata {
  34. char *text;
  35. int counter;
  36. };
  37. int lock[3];
  38. /* lock callback */
  39. static void my_lock(CURL *handle, curl_lock_data data, curl_lock_access laccess,
  40. void *useptr )
  41. {
  42. const char *what;
  43. struct userdata *user = (struct userdata *)useptr;
  44. int locknum;
  45. (void)handle;
  46. (void)laccess;
  47. switch ( data ) {
  48. case CURL_LOCK_DATA_SHARE:
  49. what = "share";
  50. locknum = 0;
  51. break;
  52. case CURL_LOCK_DATA_DNS:
  53. what = "dns";
  54. locknum = 1;
  55. break;
  56. case CURL_LOCK_DATA_COOKIE:
  57. what = "cookie";
  58. locknum = 2;
  59. break;
  60. default:
  61. fprintf(stderr, "lock: no such data: %d\n", (int)data);
  62. return;
  63. }
  64. /* detect locking of locked locks */
  65. if(lock[locknum]) {
  66. printf("lock: double locked %s\n", what);
  67. return;
  68. }
  69. lock[locknum]++;
  70. printf("lock: %-6s [%s]: %d\n", what, user->text, user->counter);
  71. user->counter++;
  72. }
  73. /* unlock callback */
  74. static void my_unlock(CURL *handle, curl_lock_data data, void *useptr )
  75. {
  76. const char *what;
  77. struct userdata *user = (struct userdata *)useptr;
  78. int locknum;
  79. (void)handle;
  80. switch ( data ) {
  81. case CURL_LOCK_DATA_SHARE:
  82. what = "share";
  83. locknum = 0;
  84. break;
  85. case CURL_LOCK_DATA_DNS:
  86. what = "dns";
  87. locknum = 1;
  88. break;
  89. case CURL_LOCK_DATA_COOKIE:
  90. what = "cookie";
  91. locknum = 2;
  92. break;
  93. default:
  94. fprintf(stderr, "unlock: no such data: %d\n", (int)data);
  95. return;
  96. }
  97. /* detect unlocking of unlocked locks */
  98. if(!lock[locknum]) {
  99. printf("unlock: double unlocked %s\n", what);
  100. return;
  101. }
  102. lock[locknum]--;
  103. printf("unlock: %-6s [%s]: %d\n", what, user->text, user->counter);
  104. user->counter++;
  105. }
  106. /* build host entry */
  107. static struct curl_slist *sethost(struct curl_slist *headers)
  108. {
  109. (void)headers;
  110. return curl_slist_append(NULL, HOSTHEADER );
  111. }
  112. /* the dummy thread function */
  113. static void *fire(void *ptr)
  114. {
  115. CURLcode code;
  116. struct curl_slist *headers;
  117. struct Tdata *tdata = (struct Tdata*)ptr;
  118. CURL *curl;
  119. int i=0;
  120. if ((curl = curl_easy_init()) == NULL) {
  121. fprintf(stderr, "curl_easy_init() failed\n");
  122. return NULL;
  123. }
  124. headers = sethost(NULL);
  125. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  126. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  127. curl_easy_setopt(curl, CURLOPT_URL, tdata->url);
  128. printf( "CURLOPT_SHARE\n" );
  129. curl_easy_setopt(curl, CURLOPT_SHARE, tdata->share);
  130. printf( "PERFORM\n" );
  131. code = curl_easy_perform(curl);
  132. if( code != CURLE_OK ) {
  133. fprintf(stderr, "perform url '%s' repeat %d failed, curlcode %d\n",
  134. tdata->url, i, (int)code);
  135. }
  136. printf( "CLEANUP\n" );
  137. curl_easy_cleanup(curl);
  138. curl_slist_free_all(headers);
  139. return NULL;
  140. }
  141. /* build request url */
  142. static char *suburl(const char *base, int i)
  143. {
  144. return curl_maprintf("%s%.4d", base, i);
  145. }
  146. /* test function */
  147. int test(char *URL)
  148. {
  149. int res;
  150. CURLSHcode scode = CURLSHE_OK;
  151. char *url = NULL;
  152. struct Tdata tdata;
  153. CURL *curl;
  154. CURLSH *share;
  155. struct curl_slist *headers = NULL;
  156. int i;
  157. struct userdata user;
  158. user.text = (char *)"Pigs in space";
  159. user.counter = 0;
  160. printf( "GLOBAL_INIT\n" );
  161. if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  162. fprintf(stderr, "curl_global_init() failed\n");
  163. return TEST_ERR_MAJOR_BAD;
  164. }
  165. /* prepare share */
  166. printf( "SHARE_INIT\n" );
  167. if ((share = curl_share_init()) == NULL) {
  168. fprintf(stderr, "curl_share_init() failed\n");
  169. curl_global_cleanup();
  170. return TEST_ERR_MAJOR_BAD;
  171. }
  172. if ( CURLSHE_OK == scode ) {
  173. printf( "CURLSHOPT_LOCKFUNC\n" );
  174. scode = curl_share_setopt( share, CURLSHOPT_LOCKFUNC, my_lock);
  175. }
  176. if ( CURLSHE_OK == scode ) {
  177. printf( "CURLSHOPT_UNLOCKFUNC\n" );
  178. scode = curl_share_setopt( share, CURLSHOPT_UNLOCKFUNC, my_unlock);
  179. }
  180. if ( CURLSHE_OK == scode ) {
  181. printf( "CURLSHOPT_USERDATA\n" );
  182. scode = curl_share_setopt( share, CURLSHOPT_USERDATA, &user);
  183. }
  184. if ( CURLSHE_OK == scode ) {
  185. printf( "CURL_LOCK_DATA_COOKIE\n" );
  186. scode = curl_share_setopt( share, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);
  187. }
  188. if ( CURLSHE_OK == scode ) {
  189. printf( "CURL_LOCK_DATA_DNS\n" );
  190. scode = curl_share_setopt( share, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
  191. }
  192. if ( CURLSHE_OK != scode ) {
  193. fprintf(stderr, "curl_share_setopt() failed\n");
  194. curl_share_cleanup(share);
  195. curl_global_cleanup();
  196. return TEST_ERR_MAJOR_BAD;
  197. }
  198. /* initial cookie manipulation */
  199. if ((curl = curl_easy_init()) == NULL) {
  200. fprintf(stderr, "curl_easy_init() failed\n");
  201. curl_share_cleanup(share);
  202. curl_global_cleanup();
  203. return TEST_ERR_MAJOR_BAD;
  204. }
  205. printf( "CURLOPT_SHARE\n" );
  206. test_setopt( curl, CURLOPT_SHARE, share );
  207. printf( "CURLOPT_COOKIELIST injected_and_clobbered\n" );
  208. test_setopt( curl, CURLOPT_COOKIELIST,
  209. "Set-Cookie: injected_and_clobbered=yes; "
  210. "domain=host.foo.com; expires=Sat Feb 2 11:56:27 GMT 2030" );
  211. printf( "CURLOPT_COOKIELIST ALL\n" );
  212. test_setopt( curl, CURLOPT_COOKIELIST, "ALL" );
  213. printf( "CURLOPT_COOKIELIST session\n" );
  214. test_setopt( curl, CURLOPT_COOKIELIST, "Set-Cookie: session=elephants" );
  215. printf( "CURLOPT_COOKIELIST injected\n" );
  216. test_setopt( curl, CURLOPT_COOKIELIST,
  217. "Set-Cookie: injected=yes; domain=host.foo.com; "
  218. "expires=Sat Feb 2 11:56:27 GMT 2030" );
  219. printf( "CURLOPT_COOKIELIST SESS\n" );
  220. test_setopt( curl, CURLOPT_COOKIELIST, "SESS" );
  221. printf( "CLEANUP\n" );
  222. curl_easy_cleanup( curl );
  223. res = 0;
  224. /* start treads */
  225. for (i=1; i<=THREADS; i++ ) {
  226. /* set thread data */
  227. tdata.url = suburl( URL, i ); /* must be curl_free()d */
  228. tdata.share = share;
  229. /* simulate thread, direct call of "thread" function */
  230. printf( "*** run %d\n",i );
  231. fire( &tdata );
  232. curl_free( tdata.url );
  233. }
  234. /* fetch a another one and save cookies */
  235. printf( "*** run %d\n", i );
  236. if ((curl = curl_easy_init()) == NULL) {
  237. fprintf(stderr, "curl_easy_init() failed\n");
  238. curl_share_cleanup(share);
  239. curl_global_cleanup();
  240. return TEST_ERR_MAJOR_BAD;
  241. }
  242. url = suburl( URL, i );
  243. headers = sethost( NULL );
  244. test_setopt( curl, CURLOPT_HTTPHEADER, headers );
  245. test_setopt( curl, CURLOPT_URL, url );
  246. printf( "CURLOPT_SHARE\n" );
  247. test_setopt( curl, CURLOPT_SHARE, share );
  248. printf( "CURLOPT_COOKIEJAR\n" );
  249. test_setopt( curl, CURLOPT_COOKIEJAR, JAR );
  250. printf( "CURLOPT_COOKIELIST FLUSH\n" );
  251. test_setopt( curl, CURLOPT_COOKIELIST, "FLUSH" );
  252. printf( "PERFORM\n" );
  253. curl_easy_perform( curl );
  254. /* try to free share, expect to fail because share is in use*/
  255. printf( "try SHARE_CLEANUP...\n" );
  256. scode = curl_share_cleanup( share );
  257. if ( scode==CURLSHE_OK )
  258. {
  259. fprintf(stderr, "curl_share_cleanup succeed but error expected\n");
  260. share = NULL;
  261. } else {
  262. printf( "SHARE_CLEANUP failed, correct\n" );
  263. }
  264. test_cleanup:
  265. /* clean up last handle */
  266. printf( "CLEANUP\n" );
  267. curl_easy_cleanup( curl );
  268. if ( headers )
  269. curl_slist_free_all( headers );
  270. if ( url )
  271. curl_free(url);
  272. /* free share */
  273. printf( "SHARE_CLEANUP\n" );
  274. scode = curl_share_cleanup( share );
  275. if ( scode!=CURLSHE_OK )
  276. fprintf(stderr, "curl_share_cleanup failed, code errno %d\n",
  277. (int)scode);
  278. printf( "GLOBAL_CLEANUP\n" );
  279. curl_global_cleanup();
  280. return res;
  281. }