lib1900.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 2013 - 2016, Linus Nielsen Feltzing, <linus@haxx.se>
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 #include "test.h"
00023 
00024 #include "testutil.h"
00025 #include "warnless.h"
00026 #include "memdebug.h"
00027 
00028 #define TEST_HANG_TIMEOUT 60 * 1000
00029 #define MAX_URLS 200
00030 #define MAX_BLACKLIST 20
00031 
00032 int urltime[MAX_URLS];
00033 char *urlstring[MAX_URLS];
00034 CURL *handles[MAX_URLS];
00035 char *site_blacklist[MAX_BLACKLIST];
00036 char *server_blacklist[MAX_BLACKLIST];
00037 int num_handles;
00038 int blacklist_num_servers;
00039 int blacklist_num_sites;
00040 
00041 static size_t
00042 write_callback(void *contents, size_t size, size_t nmemb, void *userp)
00043 {
00044   size_t realsize = size * nmemb;
00045   (void)contents;
00046   (void)userp;
00047 
00048   return realsize;
00049 }
00050 
00051 static int parse_url_file(const char *filename)
00052 {
00053   FILE *f;
00054   int filetime;
00055   char buf[200];
00056 
00057   num_handles = 0;
00058   blacklist_num_sites = 0;
00059   blacklist_num_servers = 0;
00060 
00061   f = fopen(filename, "rb");
00062   if(!f)
00063     return 0;
00064 
00065   while(!feof(f)) {
00066     if(fscanf(f, "%d %s\n", &filetime, buf)) {
00067       urltime[num_handles] = filetime;
00068       urlstring[num_handles] = strdup(buf);
00069       num_handles++;
00070       continue;
00071     }
00072 
00073     if(fscanf(f, "blacklist_site %s\n", buf)) {
00074       site_blacklist[blacklist_num_sites] = strdup(buf);
00075       blacklist_num_sites++;
00076       continue;
00077     }
00078 
00079     break;
00080   }
00081   fclose(f);
00082 
00083   site_blacklist[blacklist_num_sites] = NULL;
00084   server_blacklist[blacklist_num_servers] = NULL;
00085   return num_handles;
00086 }
00087 
00088 static void free_urls(void)
00089 {
00090   int i;
00091   for(i = 0;i < num_handles;i++) {
00092     Curl_safefree(urlstring[i]);
00093   }
00094   for(i = 0;i < blacklist_num_servers;i++) {
00095     Curl_safefree(server_blacklist[i]);
00096   }
00097   for(i = 0;i < blacklist_num_sites;i++) {
00098     Curl_safefree(site_blacklist[i]);
00099   }
00100 }
00101 
00102 static int create_handles(void)
00103 {
00104   int i;
00105 
00106   for(i = 0;i < num_handles;i++) {
00107     handles[i] = curl_easy_init();
00108   }
00109   return 0;
00110 }
00111 
00112 static void setup_handle(char *base_url, CURLM *m, int handlenum)
00113 {
00114   char urlbuf[256];
00115 
00116   snprintf(urlbuf, sizeof(urlbuf), "%s%s", base_url, urlstring[handlenum]);
00117   curl_easy_setopt(handles[handlenum], CURLOPT_URL, urlbuf);
00118   curl_easy_setopt(handles[handlenum], CURLOPT_VERBOSE, 1L);
00119   curl_easy_setopt(handles[handlenum], CURLOPT_FAILONERROR, 1L);
00120   curl_easy_setopt(handles[handlenum], CURLOPT_WRITEFUNCTION, write_callback);
00121   curl_easy_setopt(handles[handlenum], CURLOPT_WRITEDATA, NULL);
00122   curl_multi_add_handle(m, handles[handlenum]);
00123 }
00124 
00125 static void remove_handles(void)
00126 {
00127   int i;
00128 
00129   for(i = 0;i < num_handles;i++) {
00130     if(handles[i])
00131       curl_easy_cleanup(handles[i]);
00132   }
00133 }
00134 
00135 int test(char *URL)
00136 {
00137   int res = 0;
00138   CURLM *m = NULL;
00139   CURLMsg *msg; /* for picking up messages with the transfer status */
00140   int msgs_left; /* how many messages are left */
00141   int running;
00142   int handlenum = 0;
00143   struct timeval last_handle_add;
00144 
00145   if(parse_url_file("log/urls.txt") <= 0)
00146     goto test_cleanup;
00147 
00148   start_test_timing();
00149 
00150   curl_global_init(CURL_GLOBAL_ALL);
00151 
00152   multi_init(m);
00153 
00154   create_handles();
00155 
00156   multi_setopt(m, CURLMOPT_PIPELINING, 1L);
00157   multi_setopt(m, CURLMOPT_MAX_HOST_CONNECTIONS, 2L);
00158   multi_setopt(m, CURLMOPT_MAX_PIPELINE_LENGTH, 3L);
00159   multi_setopt(m, CURLMOPT_CONTENT_LENGTH_PENALTY_SIZE, 15000L);
00160   multi_setopt(m, CURLMOPT_CHUNK_LENGTH_PENALTY_SIZE, 10000L);
00161 
00162   multi_setopt(m, CURLMOPT_PIPELINING_SITE_BL, site_blacklist);
00163   multi_setopt(m, CURLMOPT_PIPELINING_SERVER_BL, server_blacklist);
00164 
00165   last_handle_add = tutil_tvnow();
00166 
00167   for(;;) {
00168     struct timeval interval;
00169     struct timeval now;
00170     long int msnow, mslast;
00171     fd_set rd, wr, exc;
00172     int maxfd = -99;
00173     long timeout;
00174 
00175     interval.tv_sec = 1;
00176     interval.tv_usec = 0;
00177 
00178     if(handlenum < num_handles) {
00179       now = tutil_tvnow();
00180       msnow = now.tv_sec * 1000 + now.tv_usec / 1000;
00181       mslast = last_handle_add.tv_sec * 1000 + last_handle_add.tv_usec / 1000;
00182       if((msnow - mslast) >= urltime[handlenum]) {
00183         fprintf(stdout, "Adding handle %d\n", handlenum);
00184         setup_handle(URL, m, handlenum);
00185         last_handle_add = now;
00186         handlenum++;
00187       }
00188     }
00189 
00190     curl_multi_perform(m, &running);
00191 
00192     abort_on_test_timeout();
00193 
00194     /* See how the transfers went */
00195     while((msg = curl_multi_info_read(m, &msgs_left))) {
00196       if(msg->msg == CURLMSG_DONE) {
00197         int i, found = 0;
00198 
00199         /* Find out which handle this message is about */
00200         for(i = 0; i < num_handles; i++) {
00201           found = (msg->easy_handle == handles[i]);
00202           if(found)
00203             break;
00204         }
00205 
00206         printf("Handle %d Completed with status %d\n", i, msg->data.result);
00207         curl_multi_remove_handle(m, handles[i]);
00208       }
00209     }
00210 
00211     if(handlenum == num_handles && !running) {
00212       break; /* done */
00213     }
00214 
00215     FD_ZERO(&rd);
00216     FD_ZERO(&wr);
00217     FD_ZERO(&exc);
00218 
00219     curl_multi_fdset(m, &rd, &wr, &exc, &maxfd);
00220 
00221     /* At this point, maxfd is guaranteed to be greater or equal than -1. */
00222 
00223     curl_multi_timeout(m, &timeout);
00224 
00225     if(timeout < 0)
00226       timeout = 1;
00227 
00228     interval.tv_sec = timeout / 1000;
00229     interval.tv_usec = (timeout % 1000) * 1000;
00230 
00231     interval.tv_sec = 0;
00232     interval.tv_usec = 1000;
00233 
00234     select_test(maxfd+1, &rd, &wr, &exc, &interval);
00235 
00236     abort_on_test_timeout();
00237   }
00238 
00239 test_cleanup:
00240 
00241   remove_handles();
00242 
00243   /* undocumented cleanup sequence - type UB */
00244 
00245   curl_multi_cleanup(m);
00246   curl_global_cleanup();
00247 
00248   free_urls();
00249   return res;
00250 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:05