1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
9 * Example source code to show how the callback function can be used to
10 * download data into a chunk of memory instead of storing it in a file.
11 * After successful download we use curl_easy_getinfo() calls to get the
12 * amount of downloaded bytes, the time used for the whole download, and
13 * the average download speed.
14 * On Linux you can create the download test files with:
15 * dd if=/dev/urandom of=file_1M.bin bs=1M count=1
24 #include <curl/curl.h>
25 #include <curl/types.h>
26 #include <curl/easy.h>
28 #define URL_BASE "http://speedtest.your.domain/"
29 #define URL_1M URL_BASE "file_1M.bin"
30 #define URL_2M URL_BASE "file_2M.bin"
31 #define URL_5M URL_BASE "file_5M.bin"
32 #define URL_10M URL_BASE "file_10M.bin"
33 #define URL_20M URL_BASE "file_20M.bin"
34 #define URL_50M URL_BASE "file_50M.bin"
35 #define URL_100M URL_BASE "file_100M.bin"
37 #define CHKSPEED_VERSION "1.0"
39 static size_t WriteCallback(void *ptr, size_t size, size_t nmemb, void *data)
41 /* we are not interested in the downloaded bytes itself,
42 so we only return the size we would have saved ... */
43 (void)ptr; /* unused */
44 (void)data; /* unused */
45 return (size_t)(size * nmemb);
48 int main(int argc, char *argv[])
52 int prtsep = 0, prttime = 0;
53 const char *url = URL_1M;
54 char *appname = argv[0];
57 /* parse input parameters */
58 for (argc--, argv++; *argv; argc--, argv++) {
59 if (strncasecmp(*argv, "-", 1) == 0) {
60 if (strncasecmp(*argv, "-H", 2) == 0) {
62 "\rUsage: %s [-m=1|2|5|10|20|50|100] [-t] [-x] [url]\n",
65 } else if (strncasecmp(*argv, "-V", 2) == 0) {
66 fprintf(stderr, "\r%s %s - %s\n",
67 appname, CHKSPEED_VERSION, curl_version());
69 } else if (strncasecmp(*argv, "-X", 2) == 0) {
71 } else if (strncasecmp(*argv, "-T", 2) == 0) {
73 } else if (strncasecmp(*argv, "-M=", 3) == 0) {
74 long m = strtol(argv+3, NULL, 10);
82 case 10: url = URL_10M;
84 case 20: url = URL_20M;
86 case 50: url = URL_50M;
88 case 100: url = URL_100M;
90 default: fprintf(stderr, "\r%s: invalid parameter %s\n",
95 fprintf(stderr, "\r%s: invalid or unknown option %s\n",
105 /* print separator line */
107 printf("-------------------------------------------------\n");
109 /* print localtime */
111 time_t t = time(NULL);
112 printf("Localtime: %s", ctime(&t));
116 curl_global_init(CURL_GLOBAL_ALL);
118 /* init the curl session */
119 curl_handle = curl_easy_init();
121 /* specify URL to get */
122 curl_easy_setopt(curl_handle, CURLOPT_URL, url);
124 /* send all data to this function */
125 curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteCallback);
127 /* some servers don't like requests that are made without a user-agent
128 field, so we provide one */
129 curl_easy_setopt(curl_handle, CURLOPT_USERAGENT,
130 "libcurl-speedchecker/" CHKSPEED_VERSION);
133 res = curl_easy_perform(curl_handle);
135 if(CURLE_OK == res) {
138 /* check for bytes downloaded */
139 res = curl_easy_getinfo(curl_handle, CURLINFO_SIZE_DOWNLOAD, &val);
140 if((CURLE_OK == res) && (val>0))
141 printf("Data downloaded: %0.0f bytes.\n", val);
143 /* check for total download time */
144 res = curl_easy_getinfo(curl_handle, CURLINFO_TOTAL_TIME, &val);
145 if((CURLE_OK == res) && (val>0))
146 printf("Total download time: %0.3f sec.\n", val);
148 /* check for average download speed */
149 res = curl_easy_getinfo(curl_handle, CURLINFO_SPEED_DOWNLOAD, &val);
150 if((CURLE_OK == res) && (val>0))
151 printf("Average download speed: %0.3f kbyte/sec.\n", val / 1024);
154 fprintf(stderr, "Error while fetching '%s' : %s\n",
155 url, curl_easy_strerror(res));
158 /* cleanup curl stuff */
159 curl_easy_cleanup(curl_handle);
161 /* we're done with libcurl, so clean it up */
162 curl_global_cleanup();