1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2013, Linus Nielsen Feltzing, <linus@haxx.se>
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
28 #define TEST_HANG_TIMEOUT 60 * 1000
30 #define MAX_BLACKLIST 20
32 int urltime[MAX_URLS];
33 char *urlstring[MAX_URLS];
34 CURL *handles[MAX_URLS];
35 char *site_blacklist[MAX_BLACKLIST];
36 char *server_blacklist[MAX_BLACKLIST];
38 int blacklist_num_servers;
39 int blacklist_num_sites;
42 write_callback(void *contents, size_t size, size_t nmemb, void *userp)
44 size_t realsize = size * nmemb;
51 static int parse_url_file(const char *filename)
58 blacklist_num_sites = 0;
59 blacklist_num_servers = 0;
61 f = fopen(filename, "rb");
66 if(fscanf(f, "%d %s\n", &filetime, buf)) {
67 urltime[num_handles] = filetime;
68 urlstring[num_handles] = strdup(buf);
73 if(fscanf(f, "blacklist_site %s\n", buf)) {
74 site_blacklist[blacklist_num_sites] = strdup(buf);
75 blacklist_num_sites++;
83 site_blacklist[blacklist_num_sites] = NULL;
84 server_blacklist[blacklist_num_servers] = NULL;
88 static void free_urls(void)
91 for(i = 0;i < num_handles;i++) {
92 Curl_safefree(urlstring[i]);
94 for(i = 0;i < blacklist_num_servers;i++) {
95 Curl_safefree(server_blacklist[i]);
97 for(i = 0;i < blacklist_num_sites;i++) {
98 Curl_safefree(site_blacklist[i]);
102 static int create_handles(void)
106 for(i = 0;i < num_handles;i++) {
107 handles[i] = curl_easy_init();
112 static void setup_handle(char *base_url, CURLM *m, int handlenum)
116 sprintf(urlbuf, "%s%s", base_url, urlstring[handlenum]);
117 curl_easy_setopt(handles[handlenum], CURLOPT_URL, urlbuf);
118 curl_easy_setopt(handles[handlenum], CURLOPT_VERBOSE, 1L);
119 curl_easy_setopt(handles[handlenum], CURLOPT_FAILONERROR, 1L);
120 curl_easy_setopt(handles[handlenum], CURLOPT_WRITEFUNCTION, write_callback);
121 curl_easy_setopt(handles[handlenum], CURLOPT_WRITEDATA, NULL);
122 curl_multi_add_handle(m, handles[handlenum]);
125 static void remove_handles(void)
129 for(i = 0;i < num_handles;i++) {
131 curl_easy_cleanup(handles[i]);
139 CURLMsg *msg; /* for picking up messages with the transfer status */
140 int msgs_left; /* how many messages are left */
143 struct timeval last_handle_add;
145 if(parse_url_file("log/urls.txt") <= 0)
150 curl_global_init(CURL_GLOBAL_ALL);
156 multi_setopt(m, CURLMOPT_PIPELINING, 1L);
157 multi_setopt(m, CURLMOPT_MAX_HOST_CONNECTIONS, 2L);
158 multi_setopt(m, CURLMOPT_MAX_PIPELINE_LENGTH, 3L);
159 multi_setopt(m, CURLMOPT_CONTENT_LENGTH_PENALTY_SIZE, 15000L);
160 multi_setopt(m, CURLMOPT_CHUNK_LENGTH_PENALTY_SIZE, 10000L);
162 multi_setopt(m, CURLMOPT_PIPELINING_SITE_BL, site_blacklist);
163 multi_setopt(m, CURLMOPT_PIPELINING_SERVER_BL, server_blacklist);
165 last_handle_add = tutil_tvnow();
168 struct timeval interval;
170 long int msnow, mslast;
176 interval.tv_usec = 0;
178 if(handlenum < num_handles) {
180 msnow = now.tv_sec * 1000 + now.tv_usec / 1000;
181 mslast = last_handle_add.tv_sec * 1000 + last_handle_add.tv_usec / 1000;
182 if(msnow - mslast >= urltime[handlenum] && handlenum < num_handles) {
183 fprintf(stdout, "Adding handle %d\n", handlenum);
184 setup_handle(URL, m, handlenum);
185 last_handle_add = now;
190 curl_multi_perform(m, &running);
192 abort_on_test_timeout();
194 /* See how the transfers went */
195 while ((msg = curl_multi_info_read(m, &msgs_left))) {
196 if (msg->msg == CURLMSG_DONE) {
199 /* Find out which handle this message is about */
200 for (i = 0; i < num_handles; i++) {
201 found = (msg->easy_handle == handles[i]);
206 printf("Handle %d Completed with status %d\n", i, msg->data.result);
207 curl_multi_remove_handle(m, handles[i]);
211 if(handlenum == num_handles && !running) {
219 curl_multi_fdset(m, &rd, &wr, &exc, &maxfd);
221 /* At this point, maxfd is guaranteed to be greater or equal than -1. */
223 curl_multi_timeout(m, &timeout);
228 interval.tv_sec = timeout / 1000;
229 interval.tv_usec = (timeout % 1000) * 1000;
232 interval.tv_usec = 1000;
234 select_test(maxfd+1, &rd, &wr, &exc, &interval);
236 abort_on_test_timeout();
243 /* undocumented cleanup sequence - type UB */
245 curl_multi_cleanup(m);
246 curl_global_cleanup();