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