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