lib1900.c 6.1 KB

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