Home | History | Annotate | Download | only in examples
      1 /***************************************************************************
      2  *                                  _   _ ____  _
      3  *  Project                     ___| | | |  _ \| |
      4  *                             / __| | | | |_) | |
      5  *                            | (__| |_| |  _ <| |___
      6  *                             \___|\___/|_| \_\_____|
      7  *
      8  * Copyright (C) 1998 - 2011, 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 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 /* This is an example showing the multi interface and the debug callback. */
     23 
     24 #include <stdio.h>
     25 #include <string.h>
     26 
     27 /* somewhat unix-specific */
     28 #include <sys/time.h>
     29 #include <unistd.h>
     30 
     31 /* curl stuff */
     32 #include <curl/curl.h>
     33 
     34 typedef char bool;
     35 #define TRUE 1
     36 
     37 static
     38 void dump(const char *text,
     39           FILE *stream, unsigned char *ptr, size_t size,
     40           bool nohex)
     41 {
     42   size_t i;
     43   size_t c;
     44 
     45   unsigned int width=0x10;
     46 
     47   if(nohex)
     48     /* without the hex output, we can fit more on screen */
     49     width = 0x40;
     50 
     51   fprintf(stream, "%s, %10.10ld bytes (0x%8.8lx)\n",
     52           text, (long)size, (long)size);
     53 
     54   for(i=0; i<size; i+= width) {
     55 
     56     fprintf(stream, "%4.4lx: ", (long)i);
     57 
     58     if(!nohex) {
     59       /* hex not disabled, show it */
     60       for(c = 0; c < width; c++)
     61         if(i+c < size)
     62           fprintf(stream, "%02x ", ptr[i+c]);
     63         else
     64           fputs("   ", stream);
     65     }
     66 
     67     for(c = 0; (c < width) && (i+c < size); c++) {
     68       /* check for 0D0A; if found, skip past and start a new line of output */
     69       if (nohex && (i+c+1 < size) && ptr[i+c]==0x0D && ptr[i+c+1]==0x0A) {
     70         i+=(c+2-width);
     71         break;
     72       }
     73       fprintf(stream, "%c",
     74               (ptr[i+c]>=0x20) && (ptr[i+c]<0x80)?ptr[i+c]:'.');
     75       /* check again for 0D0A, to avoid an extra \n if it's at width */
     76       if (nohex && (i+c+2 < size) && ptr[i+c+1]==0x0D && ptr[i+c+2]==0x0A) {
     77         i+=(c+3-width);
     78         break;
     79       }
     80     }
     81     fputc('\n', stream); /* newline */
     82   }
     83   fflush(stream);
     84 }
     85 
     86 static
     87 int my_trace(CURL *handle, curl_infotype type,
     88              unsigned char *data, size_t size,
     89              void *userp)
     90 {
     91   const char *text;
     92 
     93   (void)userp;
     94   (void)handle; /* prevent compiler warning */
     95 
     96   switch (type) {
     97   case CURLINFO_TEXT:
     98     fprintf(stderr, "== Info: %s", data);
     99   default: /* in case a new one is introduced to shock us */
    100     return 0;
    101 
    102   case CURLINFO_HEADER_OUT:
    103     text = "=> Send header";
    104     break;
    105   case CURLINFO_DATA_OUT:
    106     text = "=> Send data";
    107     break;
    108   case CURLINFO_HEADER_IN:
    109     text = "<= Recv header";
    110     break;
    111   case CURLINFO_DATA_IN:
    112     text = "<= Recv data";
    113     break;
    114   }
    115 
    116   dump(text, stderr, data, size, TRUE);
    117   return 0;
    118 }
    119 
    120 /*
    121  * Simply download a HTTP file.
    122  */
    123 int main(void)
    124 {
    125   CURL *http_handle;
    126   CURLM *multi_handle;
    127 
    128   int still_running; /* keep number of running handles */
    129 
    130   http_handle = curl_easy_init();
    131 
    132   /* set the options (I left out a few, you'll get the point anyway) */
    133   curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.example.com/");
    134 
    135   curl_easy_setopt(http_handle, CURLOPT_DEBUGFUNCTION, my_trace);
    136   curl_easy_setopt(http_handle, CURLOPT_VERBOSE, 1L);
    137 
    138   /* init a multi stack */
    139   multi_handle = curl_multi_init();
    140 
    141   /* add the individual transfers */
    142   curl_multi_add_handle(multi_handle, http_handle);
    143 
    144   /* we start some action by calling perform right away */
    145   curl_multi_perform(multi_handle, &still_running);
    146 
    147   do {
    148     struct timeval timeout;
    149     int rc; /* select() return code */
    150     CURLMcode mc; /* curl_multi_fdset() return code */
    151 
    152     fd_set fdread;
    153     fd_set fdwrite;
    154     fd_set fdexcep;
    155     int maxfd = -1;
    156 
    157     long curl_timeo = -1;
    158 
    159     FD_ZERO(&fdread);
    160     FD_ZERO(&fdwrite);
    161     FD_ZERO(&fdexcep);
    162 
    163     /* set a suitable timeout to play around with */
    164     timeout.tv_sec = 1;
    165     timeout.tv_usec = 0;
    166 
    167     curl_multi_timeout(multi_handle, &curl_timeo);
    168     if(curl_timeo >= 0) {
    169       timeout.tv_sec = curl_timeo / 1000;
    170       if(timeout.tv_sec > 1)
    171         timeout.tv_sec = 1;
    172       else
    173         timeout.tv_usec = (curl_timeo % 1000) * 1000;
    174     }
    175 
    176     /* get file descriptors from the transfers */
    177     mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
    178 
    179     if(mc != CURLM_OK)
    180     {
    181       fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
    182       break;
    183     }
    184 
    185     /* On success the value of maxfd is guaranteed to be >= -1. We call
    186        select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
    187        no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
    188        to sleep 100ms, which is the minimum suggested value in the
    189        curl_multi_fdset() doc. */
    190 
    191     if(maxfd == -1) {
    192 #ifdef _WIN32
    193       Sleep(100);
    194       rc = 0;
    195 #else
    196       /* Portable sleep for platforms other than Windows. */
    197       struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
    198       rc = select(0, NULL, NULL, NULL, &wait);
    199 #endif
    200     }
    201     else {
    202       /* Note that on some platforms 'timeout' may be modified by select().
    203          If you need access to the original value save a copy beforehand. */
    204       rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
    205     }
    206 
    207     switch(rc) {
    208     case -1:
    209       /* select error */
    210       still_running = 0;
    211       printf("select() returns error, this is badness\n");
    212       break;
    213     case 0:
    214     default:
    215       /* timeout or readable/writable sockets */
    216       curl_multi_perform(multi_handle, &still_running);
    217       break;
    218     }
    219   } while(still_running);
    220 
    221   curl_multi_cleanup(multi_handle);
    222 
    223   curl_easy_cleanup(http_handle);
    224 
    225   return 0;
    226 }
    227