lib506.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #include "test.h"
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <errno.h>
  5. #include <mprintf.h>
  6. const char *HOSTHEADER = "Host: www.host.foo.com";
  7. const char *JAR = "log/jar506";
  8. #define THREADS 2
  9. void lock(CURL *handle, curl_lock_data data, curl_lock_access access,
  10. void *useptr );
  11. void unlock(CURL *handle, curl_lock_data data, void *useptr );
  12. struct curl_slist *sethost(struct curl_slist *headers);
  13. void *fire(void *ptr);
  14. char *suburl(char *base, int i);
  15. /* struct containing data of a thread */
  16. struct Tdata {
  17. CURLSH *share;
  18. char *url;
  19. };
  20. struct userdata {
  21. char *text;
  22. int counter;
  23. };
  24. /* lock callback */
  25. void lock(CURL *handle, curl_lock_data data, curl_lock_access access,
  26. void *useptr )
  27. {
  28. const char *what;
  29. struct userdata *user = (struct userdata *)useptr;
  30. (void)handle;
  31. (void)access;
  32. switch ( data ) {
  33. case CURL_LOCK_DATA_SHARE:
  34. what = "share";
  35. break;
  36. case CURL_LOCK_DATA_DNS:
  37. what = "dns";
  38. break;
  39. case CURL_LOCK_DATA_COOKIE:
  40. what = "cookie";
  41. break;
  42. default:
  43. fprintf(stderr, "lock: no such data: %d\n", (int)data);
  44. return;
  45. }
  46. printf("lock: %-6s <%s>: %d\n", what, user->text, user->counter);
  47. user->counter++;
  48. }
  49. /* unlock callback */
  50. void unlock(CURL *handle, curl_lock_data data, void *useptr )
  51. {
  52. const char *what;
  53. struct userdata *user = (struct userdata *)useptr;
  54. (void)handle;
  55. switch ( data ) {
  56. case CURL_LOCK_DATA_SHARE:
  57. what = "share";
  58. break;
  59. case CURL_LOCK_DATA_DNS:
  60. what = "dns";
  61. break;
  62. case CURL_LOCK_DATA_COOKIE:
  63. what = "cookie";
  64. break;
  65. default:
  66. fprintf(stderr, "unlock: no such data: %d\n", (int)data);
  67. return;
  68. }
  69. printf("unlock: %-6s <%s>: %d\n", what, user->text, user->counter);
  70. user->counter++;
  71. }
  72. /* build host entry */
  73. struct curl_slist *sethost(struct curl_slist *headers)
  74. {
  75. (void)headers;
  76. return curl_slist_append(NULL, HOSTHEADER );
  77. }
  78. /* the dummy thread function */
  79. void *fire(void *ptr)
  80. {
  81. CURLcode code;
  82. struct curl_slist *headers;
  83. struct Tdata *tdata = (struct Tdata*)ptr;
  84. CURL *curl = curl_easy_init();
  85. int i=0;
  86. headers = sethost(NULL);
  87. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  88. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, (void*)headers);
  89. curl_easy_setopt(curl, CURLOPT_URL, (void*)tdata->url);
  90. printf( "CURLOPT_SHARE\n" );
  91. curl_easy_setopt(curl, CURLOPT_SHARE, (void*)tdata->share);
  92. printf( "PERFORM\n" );
  93. code = curl_easy_perform(curl);
  94. if( code != CURLE_OK ) {
  95. fprintf(stderr, "perform url '%s' repeat %d failed, curlcode %d\n",
  96. tdata->url, i, (int)code);
  97. }
  98. printf( "CLEANUP\n" );
  99. curl_easy_cleanup(curl);
  100. curl_slist_free_all(headers);
  101. return NULL;
  102. }
  103. /* build request url */
  104. char *suburl(char *base, int i)
  105. {
  106. return curl_maprintf("%s000%c", base, 48+i);
  107. }
  108. /* test function */
  109. int test(char *URL)
  110. {
  111. int res;
  112. CURLSHcode scode;
  113. char *url;
  114. struct Tdata tdata;
  115. CURL *curl;
  116. CURLSH *share;
  117. struct curl_slist *headers;
  118. int i;
  119. struct userdata user;
  120. user.text = (char *)"Pigs in space";
  121. user.counter = 0;
  122. printf( "GLOBAL_INIT\n" );
  123. curl_global_init( CURL_GLOBAL_ALL );
  124. /* prepare share */
  125. printf( "SHARE_INIT\n" );
  126. share = curl_share_init();
  127. scode = curl_share_setopt( share, CURLSHOPT_LOCKFUNC, lock);
  128. scode += curl_share_setopt( share, CURLSHOPT_UNLOCKFUNC, unlock);
  129. scode += curl_share_setopt( share, CURLSHOPT_USERDATA, &user);
  130. printf( "CURL_LOCK_DATA_COOKIE\n" );
  131. scode += curl_share_setopt( share, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);
  132. printf( "CURL_LOCK_DATA_DNS\n" );
  133. scode += curl_share_setopt( share, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
  134. if(scode) {
  135. curl_share_cleanup(share);
  136. return 2;
  137. }
  138. res = 0;
  139. /* start treads */
  140. for (i=1; i<=THREADS; i++ ) {
  141. /* set thread data */
  142. tdata.url = suburl( URL, i ); /* must be curl_free()d */
  143. tdata.share = share;
  144. /* simulate thread, direct call of "thread" function */
  145. printf( "*** run %d\n",i );
  146. fire( &tdata );
  147. curl_free( tdata.url );
  148. }
  149. /* fetch a another one and save cookies */
  150. printf( "*** run %d\n", i );
  151. curl = curl_easy_init();
  152. url = suburl( URL, i );
  153. headers = sethost( NULL );
  154. curl_easy_setopt( curl, CURLOPT_HTTPHEADER, (void*)headers );
  155. curl_easy_setopt( curl, CURLOPT_URL, url );
  156. printf( "CURLOPT_SHARE\n" );
  157. curl_easy_setopt( curl, CURLOPT_SHARE, share );
  158. printf( "CURLOPT_COOKIEJAR\n" );
  159. curl_easy_setopt( curl, CURLOPT_COOKIEJAR, JAR );
  160. printf( "PERFORM\n" );
  161. curl_easy_perform( curl );
  162. /* try to free share, expect to fail because share is in use*/
  163. printf( "try SHARE_CLEANUP...\n" );
  164. scode = curl_share_cleanup( share );
  165. if ( scode==CURLSHE_OK )
  166. {
  167. fprintf(stderr, "curl_share_cleanup succeed but error expected\n");
  168. share = NULL;
  169. } else {
  170. printf( "SHARE_CLEANUP failed, correct\n" );
  171. }
  172. /* clean up last handle */
  173. printf( "CLEANUP\n" );
  174. curl_easy_cleanup( curl );
  175. curl_slist_free_all( headers );
  176. curl_free(url);
  177. /* free share */
  178. printf( "SHARE_CLEANUP\n" );
  179. scode = curl_share_cleanup( share );
  180. if ( scode!=CURLSHE_OK )
  181. fprintf(stderr, "curl_share_cleanup failed, code errno %d\n",
  182. (int)scode);
  183. printf( "GLOBAL_CLEANUP\n" );
  184. curl_global_cleanup();
  185. return res;
  186. }