诸暨麻将添加redis
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

309 lines
8.1 KiB

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