lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /*************************************************************************** |
| 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
| 8 | * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@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 | #define STOP_DOWNLOAD_AFTER_THIS_MANY_BYTES 6000 |
| 31 | #define MINIMAL_PROGRESS_FUNCTIONALITY_INTERVAL 3 |
| 32 | |
| 33 | struct myprogress { |
| 34 | double lastruntime; |
| 35 | CURL *curl; |
| 36 | }; |
| 37 | |
| 38 | /* this is how the CURLOPT_XFERINFOFUNCTION callback works */ |
| 39 | static int xferinfo(void *p, |
| 40 | curl_off_t dltotal, curl_off_t dlnow, |
| 41 | curl_off_t ultotal, curl_off_t ulnow) |
| 42 | { |
| 43 | struct myprogress *myp = (struct myprogress *)p; |
| 44 | CURL *curl = myp->curl; |
| 45 | double curtime = 0; |
| 46 | |
| 47 | curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &curtime); |
| 48 | |
| 49 | /* under certain circumstances it may be desirable for certain functionality |
| 50 | to only run every N seconds, in order to do this the transaction time can |
| 51 | be used */ |
| 52 | if((curtime - myp->lastruntime) >= MINIMAL_PROGRESS_FUNCTIONALITY_INTERVAL) { |
| 53 | myp->lastruntime = curtime; |
| 54 | fprintf(stderr, "TOTAL TIME: %f \r\n", curtime); |
| 55 | } |
| 56 | |
| 57 | fprintf(stderr, "UP: %" CURL_FORMAT_CURL_OFF_T " of %" CURL_FORMAT_CURL_OFF_T |
| 58 | " DOWN: %" CURL_FORMAT_CURL_OFF_T " of %" CURL_FORMAT_CURL_OFF_T |
| 59 | "\r\n", |
| 60 | ulnow, ultotal, dlnow, dltotal); |
| 61 | |
| 62 | if(dlnow > STOP_DOWNLOAD_AFTER_THIS_MANY_BYTES) |
| 63 | return 1; |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | /* for libcurl older than 7.32.0 (CURLOPT_PROGRESSFUNCTION) */ |
| 68 | static int older_progress(void *p, |
| 69 | double dltotal, double dlnow, |
| 70 | double ultotal, double ulnow) |
| 71 | { |
| 72 | return xferinfo(p, |
| 73 | (curl_off_t)dltotal, |
| 74 | (curl_off_t)dlnow, |
| 75 | (curl_off_t)ultotal, |
| 76 | (curl_off_t)ulnow); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | int main(void) |
| 81 | { |
| 82 | CURL *curl; |
| 83 | CURLcode res = CURLE_OK; |
| 84 | struct myprogress prog; |
| 85 | |
| 86 | curl = curl_easy_init(); |
| 87 | if(curl) { |
| 88 | prog.lastruntime = 0; |
| 89 | prog.curl = curl; |
| 90 | |
| 91 | curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/"); |
| 92 | |
| 93 | curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, older_progress); |
| 94 | /* pass the struct pointer into the progress function */ |
| 95 | curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, &prog); |
| 96 | |
| 97 | #if LIBCURL_VERSION_NUM >= 0x072000 |
| 98 | /* xferinfo was introduced in 7.32.0, no earlier libcurl versions will |
| 99 | compile as they won't have the symbols around. |
| 100 | |
| 101 | If built with a newer libcurl, but running with an older libcurl: |
| 102 | curl_easy_setopt() will fail in run-time trying to set the new |
| 103 | callback, making the older callback get used. |
| 104 | |
| 105 | New libcurls will prefer the new callback and instead use that one even |
| 106 | if both callbacks are set. */ |
| 107 | |
| 108 | curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, xferinfo); |
| 109 | /* pass the struct pointer into the xferinfo function, note that this is |
| 110 | an alias to CURLOPT_PROGRESSDATA */ |
| 111 | curl_easy_setopt(curl, CURLOPT_XFERINFODATA, &prog); |
| 112 | #endif |
| 113 | |
| 114 | curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L); |
| 115 | res = curl_easy_perform(curl); |
| 116 | |
| 117 | if(res != CURLE_OK) |
| 118 | fprintf(stderr, "%s\n", curl_easy_strerror(res)); |
| 119 | |
| 120 | /* always cleanup */ |
| 121 | curl_easy_cleanup(curl); |
| 122 | } |
| 123 | return (int)res; |
| 124 | } |