Home | History | Annotate | Download | only in examples
      1 /***************************************************************************
      2  *                                  _   _ ____  _
      3  *  Project                     ___| | | |  _ \| |
      4  *                             / __| | | | |_) | |
      5  *                            | (__| |_| |  _ <| |___
      6  *                             \___|\___/|_| \_\_____|
      7  *
      8  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel (at) haxx.se>, et al.
      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.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 /* <DESC>
     23  * Multiplexed HTTP/2 downloads over a single connection
     24  * </DESC>
     25  */
     26 #include <stdio.h>
     27 #include <stdlib.h>
     28 #include <string.h>
     29 
     30 /* somewhat unix-specific */
     31 #include <sys/time.h>
     32 #include <unistd.h>
     33 
     34 /* curl stuff */
     35 #include <curl/curl.h>
     36 
     37 #ifndef CURLPIPE_MULTIPLEX
     38 /* This little trick will just make sure that we don't enable pipelining for
     39    libcurls old enough to not have this symbol. It is _not_ defined to zero in
     40    a recent libcurl header. */
     41 #define CURLPIPE_MULTIPLEX 0
     42 #endif
     43 
     44 #define NUM_HANDLES 1000
     45 
     46 void *curl_hnd[NUM_HANDLES];
     47 int num_transfers;
     48 
     49 /* a handle to number lookup, highly ineffective when we do many
     50    transfers... */
     51 static int hnd2num(CURL *hnd)
     52 {
     53   int i;
     54   for(i=0; i< num_transfers; i++) {
     55     if(curl_hnd[i] == hnd)
     56       return i;
     57   }
     58   return 0; /* weird, but just a fail-safe */
     59 }
     60 
     61 static
     62 void dump(const char *text, int num, unsigned char *ptr, size_t size,
     63           char nohex)
     64 {
     65   size_t i;
     66   size_t c;
     67 
     68   unsigned int width=0x10;
     69 
     70   if(nohex)
     71     /* without the hex output, we can fit more on screen */
     72     width = 0x40;
     73 
     74   fprintf(stderr, "%d %s, %ld bytes (0x%lx)\n",
     75           num, text, (long)size, (long)size);
     76 
     77   for(i=0; i<size; i+= width) {
     78 
     79     fprintf(stderr, "%4.4lx: ", (long)i);
     80 
     81     if(!nohex) {
     82       /* hex not disabled, show it */
     83       for(c = 0; c < width; c++)
     84         if(i+c < size)
     85           fprintf(stderr, "%02x ", ptr[i+c]);
     86         else
     87           fputs("   ", stderr);
     88     }
     89 
     90     for(c = 0; (c < width) && (i+c < size); c++) {
     91       /* check for 0D0A; if found, skip past and start a new line of output */
     92       if(nohex && (i+c+1 < size) && ptr[i+c]==0x0D && ptr[i+c+1]==0x0A) {
     93         i+=(c+2-width);
     94         break;
     95       }
     96       fprintf(stderr, "%c",
     97               (ptr[i+c]>=0x20) && (ptr[i+c]<0x80)?ptr[i+c]:'.');
     98       /* check again for 0D0A, to avoid an extra \n if it's at width */
     99       if(nohex && (i+c+2 < size) && ptr[i+c+1]==0x0D && ptr[i+c+2]==0x0A) {
    100         i+=(c+3-width);
    101         break;
    102       }
    103     }
    104     fputc('\n', stderr); /* newline */
    105   }
    106 }
    107 
    108 static
    109 int my_trace(CURL *handle, curl_infotype type,
    110              char *data, size_t size,
    111              void *userp)
    112 {
    113   const char *text;
    114   int num = hnd2num(handle);
    115   (void)handle; /* prevent compiler warning */
    116   (void)userp;
    117   switch (type) {
    118   case CURLINFO_TEXT:
    119     fprintf(stderr, "== %d Info: %s", num, data);
    120   default: /* in case a new one is introduced to shock us */
    121     return 0;
    122 
    123   case CURLINFO_HEADER_OUT:
    124     text = "=> Send header";
    125     break;
    126   case CURLINFO_DATA_OUT:
    127     text = "=> Send data";
    128     break;
    129   case CURLINFO_SSL_DATA_OUT:
    130     text = "=> Send SSL data";
    131     break;
    132   case CURLINFO_HEADER_IN:
    133     text = "<= Recv header";
    134     break;
    135   case CURLINFO_DATA_IN:
    136     text = "<= Recv data";
    137     break;
    138   case CURLINFO_SSL_DATA_IN:
    139     text = "<= Recv SSL data";
    140     break;
    141   }
    142 
    143   dump(text, num, (unsigned char *)data, size, 1);
    144   return 0;
    145 }
    146 
    147 static void setup(CURL *hnd, int num)
    148 {
    149   FILE *out;
    150   char filename[128];
    151 
    152   snprintf(filename, 128, "dl-%d", num);
    153 
    154   out = fopen(filename, "wb");
    155 
    156   /* write to this file */
    157   curl_easy_setopt(hnd, CURLOPT_WRITEDATA, out);
    158 
    159   /* set the same URL */
    160   curl_easy_setopt(hnd, CURLOPT_URL, "https://localhost:8443/index.html");
    161 
    162   /* send it verbose for max debuggaility */
    163   curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
    164   curl_easy_setopt(hnd, CURLOPT_DEBUGFUNCTION, my_trace);
    165 
    166   /* HTTP/2 please */
    167   curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
    168 
    169   /* we use a self-signed test server, skip verification during debugging */
    170   curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
    171   curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L);
    172 
    173 #if (CURLPIPE_MULTIPLEX > 0)
    174   /* wait for pipe connection to confirm */
    175   curl_easy_setopt(hnd, CURLOPT_PIPEWAIT, 1L);
    176 #endif
    177 
    178   curl_hnd[num] = hnd;
    179 }
    180 
    181 /*
    182  * Simply download two files over HTTP/2, using the same physical connection!
    183  */
    184 int main(int argc, char **argv)
    185 {
    186   CURL *easy[NUM_HANDLES];
    187   CURLM *multi_handle;
    188   int i;
    189   int still_running; /* keep number of running handles */
    190 
    191   if(argc > 1)
    192     /* if given a number, do that many transfers */
    193     num_transfers = atoi(argv[1]);
    194 
    195   if(!num_transfers || (num_transfers > NUM_HANDLES))
    196     num_transfers = 3; /* a suitable low default */
    197 
    198   /* init a multi stack */
    199   multi_handle = curl_multi_init();
    200 
    201   for(i=0; i<num_transfers; i++) {
    202     easy[i] = curl_easy_init();
    203     /* set options */
    204     setup(easy[i], i);
    205 
    206     /* add the individual transfer */
    207     curl_multi_add_handle(multi_handle, easy[i]);
    208   }
    209 
    210   curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
    211 
    212   /* we start some action by calling perform right away */
    213   curl_multi_perform(multi_handle, &still_running);
    214 
    215   do {
    216     struct timeval timeout;
    217     int rc; /* select() return code */
    218     CURLMcode mc; /* curl_multi_fdset() return code */
    219 
    220     fd_set fdread;
    221     fd_set fdwrite;
    222     fd_set fdexcep;
    223     int maxfd = -1;
    224 
    225     long curl_timeo = -1;
    226 
    227     FD_ZERO(&fdread);
    228     FD_ZERO(&fdwrite);
    229     FD_ZERO(&fdexcep);
    230 
    231     /* set a suitable timeout to play around with */
    232     timeout.tv_sec = 1;
    233     timeout.tv_usec = 0;
    234 
    235     curl_multi_timeout(multi_handle, &curl_timeo);
    236     if(curl_timeo >= 0) {
    237       timeout.tv_sec = curl_timeo / 1000;
    238       if(timeout.tv_sec > 1)
    239         timeout.tv_sec = 1;
    240       else
    241         timeout.tv_usec = (curl_timeo % 1000) * 1000;
    242     }
    243 
    244     /* get file descriptors from the transfers */
    245     mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
    246 
    247     if(mc != CURLM_OK) {
    248       fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
    249       break;
    250     }
    251 
    252     /* On success the value of maxfd is guaranteed to be >= -1. We call
    253        select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
    254        no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
    255        to sleep 100ms, which is the minimum suggested value in the
    256        curl_multi_fdset() doc. */
    257 
    258     if(maxfd == -1) {
    259 #ifdef _WIN32
    260       Sleep(100);
    261       rc = 0;
    262 #else
    263       /* Portable sleep for platforms other than Windows. */
    264       struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
    265       rc = select(0, NULL, NULL, NULL, &wait);
    266 #endif
    267     }
    268     else {
    269       /* Note that on some platforms 'timeout' may be modified by select().
    270          If you need access to the original value save a copy beforehand. */
    271       rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
    272     }
    273 
    274     switch(rc) {
    275     case -1:
    276       /* select error */
    277       break;
    278     case 0:
    279     default:
    280       /* timeout or readable/writable sockets */
    281       curl_multi_perform(multi_handle, &still_running);
    282       break;
    283     }
    284   } while(still_running);
    285 
    286   curl_multi_cleanup(multi_handle);
    287 
    288   for(i=0; i<num_transfers; i++)
    289     curl_easy_cleanup(easy[i]);
    290 
    291   return 0;
    292 }
    293