lib1900.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2013 - 2020, Linus Nielsen Feltzing, <linus@haxx.se>
  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 https://curl.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 "testutil.h"
  24. #include "warnless.h"
  25. #include "memdebug.h"
  26. #define TEST_HANG_TIMEOUT 60 * 1000
  27. #define MAX_URLS 200
  28. #define MAX_BLOCKLIST 20
  29. static int urltime[MAX_URLS];
  30. static char *urlstring[MAX_URLS];
  31. static CURL *handles[MAX_URLS];
  32. static char *site_blocklist[MAX_BLOCKLIST];
  33. static char *server_blocklist[MAX_BLOCKLIST];
  34. static int num_handles;
  35. static int blocklist_num_servers;
  36. static int blocklist_num_sites;
  37. static size_t
  38. write_callback(void *contents, size_t size, size_t nmemb, void *userp)
  39. {
  40. size_t realsize = size * nmemb;
  41. (void)contents;
  42. (void)userp;
  43. return realsize;
  44. }
  45. static int parse_url_file(const char *filename)
  46. {
  47. FILE *f;
  48. int filetime;
  49. char buf[200];
  50. num_handles = 0;
  51. blocklist_num_sites = 0;
  52. blocklist_num_servers = 0;
  53. f = fopen(filename, "rb");
  54. if(!f)
  55. return 0;
  56. while(!feof(f)) {
  57. if(fscanf(f, "%d %199s\n", &filetime, buf)) {
  58. urltime[num_handles] = filetime;
  59. urlstring[num_handles] = strdup(buf);
  60. num_handles++;
  61. continue;
  62. }
  63. if(fscanf(f, "blocklist_site %199s\n", buf)) {
  64. site_blocklist[blocklist_num_sites] = strdup(buf);
  65. blocklist_num_sites++;
  66. continue;
  67. }
  68. break;
  69. }
  70. fclose(f);
  71. site_blocklist[blocklist_num_sites] = NULL;
  72. server_blocklist[blocklist_num_servers] = NULL;
  73. return num_handles;
  74. }
  75. static void free_urls(void)
  76. {
  77. int i;
  78. for(i = 0; i < num_handles; i++) {
  79. Curl_safefree(urlstring[i]);
  80. }
  81. for(i = 0; i < blocklist_num_servers; i++) {
  82. Curl_safefree(server_blocklist[i]);
  83. }
  84. for(i = 0; i < blocklist_num_sites; i++) {
  85. Curl_safefree(site_blocklist[i]);
  86. }
  87. }
  88. static int create_handles(void)
  89. {
  90. int i;
  91. for(i = 0; i < num_handles; i++) {
  92. handles[i] = curl_easy_init();
  93. }
  94. return 0;
  95. }
  96. static void setup_handle(char *base_url, CURLM *m, int handlenum)
  97. {
  98. char urlbuf[256];
  99. msnprintf(urlbuf, sizeof(urlbuf), "%s%s", base_url, urlstring[handlenum]);
  100. curl_easy_setopt(handles[handlenum], CURLOPT_URL, urlbuf);
  101. curl_easy_setopt(handles[handlenum], CURLOPT_VERBOSE, 1L);
  102. curl_easy_setopt(handles[handlenum], CURLOPT_FAILONERROR, 1L);
  103. curl_easy_setopt(handles[handlenum], CURLOPT_WRITEFUNCTION, write_callback);
  104. curl_easy_setopt(handles[handlenum], CURLOPT_WRITEDATA, NULL);
  105. curl_multi_add_handle(m, handles[handlenum]);
  106. }
  107. static void remove_handles(void)
  108. {
  109. int i;
  110. for(i = 0; i < num_handles; i++) {
  111. if(handles[i])
  112. curl_easy_cleanup(handles[i]);
  113. }
  114. }
  115. int test(char *URL)
  116. {
  117. int res = 0;
  118. CURLM *m = NULL;
  119. CURLMsg *msg; /* for picking up messages with the transfer status */
  120. int msgs_left; /* how many messages are left */
  121. int running = 0;
  122. int handlenum = 0;
  123. struct timeval last_handle_add;
  124. if(parse_url_file(libtest_arg2) <= 0)
  125. goto test_cleanup;
  126. start_test_timing();
  127. curl_global_init(CURL_GLOBAL_ALL);
  128. multi_init(m);
  129. create_handles();
  130. multi_setopt(m, CURLMOPT_PIPELINING, 1L);
  131. multi_setopt(m, CURLMOPT_MAX_HOST_CONNECTIONS, 2L);
  132. multi_setopt(m, CURLMOPT_MAX_PIPELINE_LENGTH, 3L);
  133. multi_setopt(m, CURLMOPT_CONTENT_LENGTH_PENALTY_SIZE, 15000L);
  134. multi_setopt(m, CURLMOPT_CHUNK_LENGTH_PENALTY_SIZE, 10000L);
  135. multi_setopt(m, CURLMOPT_PIPELINING_SITE_BL, site_blocklist);
  136. multi_setopt(m, CURLMOPT_PIPELINING_SERVER_BL, server_blocklist);
  137. last_handle_add = tutil_tvnow();
  138. for(;;) {
  139. struct timeval interval;
  140. struct timeval now;
  141. fd_set rd, wr, exc;
  142. int maxfd = -99;
  143. long timeout;
  144. interval.tv_sec = 1;
  145. interval.tv_usec = 0;
  146. if(handlenum < num_handles) {
  147. now = tutil_tvnow();
  148. if(tutil_tvdiff(now, last_handle_add) >= urltime[handlenum]) {
  149. fprintf(stdout, "Adding handle %d\n", handlenum);
  150. setup_handle(URL, m, handlenum);
  151. last_handle_add = now;
  152. handlenum++;
  153. }
  154. }
  155. curl_multi_perform(m, &running);
  156. abort_on_test_timeout();
  157. /* See how the transfers went */
  158. do {
  159. msg = curl_multi_info_read(m, &msgs_left);
  160. if(msg && msg->msg == CURLMSG_DONE) {
  161. int i;
  162. /* Find out which handle this message is about */
  163. for(i = 0; i < num_handles; i++) {
  164. int found = (msg->easy_handle == handles[i]);
  165. if(found)
  166. break;
  167. }
  168. printf("Handle %d Completed with status %d\n", i, msg->data.result);
  169. curl_multi_remove_handle(m, handles[i]);
  170. }
  171. } while(msg);
  172. if(handlenum == num_handles && !running) {
  173. break; /* done */
  174. }
  175. FD_ZERO(&rd);
  176. FD_ZERO(&wr);
  177. FD_ZERO(&exc);
  178. curl_multi_fdset(m, &rd, &wr, &exc, &maxfd);
  179. /* At this point, maxfd is guaranteed to be greater or equal than -1. */
  180. curl_multi_timeout(m, &timeout);
  181. if(timeout < 0)
  182. timeout = 1;
  183. interval.tv_sec = timeout / 1000;
  184. interval.tv_usec = (timeout % 1000) * 1000;
  185. interval.tv_sec = 0;
  186. interval.tv_usec = 1000;
  187. select_test(maxfd + 1, &rd, &wr, &exc, &interval);
  188. abort_on_test_timeout();
  189. }
  190. test_cleanup:
  191. remove_handles();
  192. /* undocumented cleanup sequence - type UB */
  193. curl_multi_cleanup(m);
  194. curl_global_cleanup();
  195. free_urls();
  196. return res;
  197. }