Home | History | Annotate | Download | only in examples
      1 /***************************************************************************
      2  *                                  _   _ ____  _
      3  *  Project                     ___| | | |  _ \| |
      4  *                             / __| | | | |_) | |
      5  *                            | (__| |_| |  _ <| |___
      6  *                             \___|\___/|_| \_\_____|
      7  *
      8  * Copyright (C) 1998 - 2018, 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  * Use the progress callbacks, old and/or new one depending on available
     24  * libcurl version.
     25  * </DESC>
     26  */
     27 #include <stdio.h>
     28 #include <curl/curl.h>
     29 
     30 #if LIBCURL_VERSION_NUM >= 0x073d00
     31 /* In libcurl 7.61.0, support was added for extracting the time in plain
     32    microseconds. Older libcurl versions are stuck in using 'double' for this
     33    information so we complicate this example a bit by supporting either
     34    approach. */
     35 #define TIME_IN_US 1 /* microseconds */
     36 #define TIMETYPE curl_off_t
     37 #define TIMEOPT CURLINFO_TOTAL_TIME_T
     38 #define MINIMAL_PROGRESS_FUNCTIONALITY_INTERVAL     3000000
     39 #else
     40 #define TIMETYPE double
     41 #define TIMEOPT CURLINFO_TOTAL_TIME
     42 #define MINIMAL_PROGRESS_FUNCTIONALITY_INTERVAL     3
     43 #endif
     44 
     45 #define STOP_DOWNLOAD_AFTER_THIS_MANY_BYTES         6000
     46 
     47 struct myprogress {
     48   TIMETYPE lastruntime; /* type depends on version, see above */
     49   CURL *curl;
     50 };
     51 
     52 /* this is how the CURLOPT_XFERINFOFUNCTION callback works */
     53 static int xferinfo(void *p,
     54                     curl_off_t dltotal, curl_off_t dlnow,
     55                     curl_off_t ultotal, curl_off_t ulnow)
     56 {
     57   struct myprogress *myp = (struct myprogress *)p;
     58   CURL *curl = myp->curl;
     59   TIMETYPE curtime = 0;
     60 
     61   curl_easy_getinfo(curl, TIMEOPT, &curtime);
     62 
     63   /* under certain circumstances it may be desirable for certain functionality
     64      to only run every N seconds, in order to do this the transaction time can
     65      be used */
     66   if((curtime - myp->lastruntime) >= MINIMAL_PROGRESS_FUNCTIONALITY_INTERVAL) {
     67     myp->lastruntime = curtime;
     68 #ifdef TIME_IN_US
     69     fprintf(stderr, "TOTAL TIME: %" CURL_FORMAT_CURL_OFF_T ".%06ld\r\n",
     70             (curtime / 1000000), (long)(curtime % 1000000));
     71 #else
     72     fprintf(stderr, "TOTAL TIME: %f \r\n", curtime);
     73 #endif
     74   }
     75 
     76   fprintf(stderr, "UP: %" CURL_FORMAT_CURL_OFF_T " of %" CURL_FORMAT_CURL_OFF_T
     77           "  DOWN: %" CURL_FORMAT_CURL_OFF_T " of %" CURL_FORMAT_CURL_OFF_T
     78           "\r\n",
     79           ulnow, ultotal, dlnow, dltotal);
     80 
     81   if(dlnow > STOP_DOWNLOAD_AFTER_THIS_MANY_BYTES)
     82     return 1;
     83   return 0;
     84 }
     85 
     86 #if LIBCURL_VERSION_NUM < 0x072000
     87 /* for libcurl older than 7.32.0 (CURLOPT_PROGRESSFUNCTION) */
     88 static int older_progress(void *p,
     89                           double dltotal, double dlnow,
     90                           double ultotal, double ulnow)
     91 {
     92   return xferinfo(p,
     93                   (curl_off_t)dltotal,
     94                   (curl_off_t)dlnow,
     95                   (curl_off_t)ultotal,
     96                   (curl_off_t)ulnow);
     97 }
     98 #endif
     99 
    100 int main(void)
    101 {
    102   CURL *curl;
    103   CURLcode res = CURLE_OK;
    104   struct myprogress prog;
    105 
    106   curl = curl_easy_init();
    107   if(curl) {
    108     prog.lastruntime = 0;
    109     prog.curl = curl;
    110 
    111     curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/");
    112 
    113 #if LIBCURL_VERSION_NUM >= 0x072000
    114     /* xferinfo was introduced in 7.32.0, no earlier libcurl versions will
    115        compile as they won't have the symbols around.
    116 
    117        If built with a newer libcurl, but running with an older libcurl:
    118        curl_easy_setopt() will fail in run-time trying to set the new
    119        callback, making the older callback get used.
    120 
    121        New libcurls will prefer the new callback and instead use that one even
    122        if both callbacks are set. */
    123 
    124     curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, xferinfo);
    125     /* pass the struct pointer into the xferinfo function, note that this is
    126        an alias to CURLOPT_PROGRESSDATA */
    127     curl_easy_setopt(curl, CURLOPT_XFERINFODATA, &prog);
    128 #else
    129     curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, older_progress);
    130     /* pass the struct pointer into the progress function */
    131     curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, &prog);
    132 #endif
    133 
    134     curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
    135     res = curl_easy_perform(curl);
    136 
    137     if(res != CURLE_OK)
    138       fprintf(stderr, "%s\n", curl_easy_strerror(res));
    139 
    140     /* always cleanup */
    141     curl_easy_cleanup(curl);
    142   }
    143   return (int)res;
    144 }
    145