1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
9 * This is a very simple example using the multi interface and the debug
16 /* somewhat unix-specific */
21 #include <curl/curl.h>
27 void dump(const char *text,
28 FILE *stream, unsigned char *ptr, size_t size,
34 unsigned int width=0x10;
37 /* without the hex output, we can fit more on screen */
40 fprintf(stream, "%s, %010.10ld bytes (0x%08.8lx)\n",
41 text, (long)size, (long)size);
43 for(i=0; i<size; i+= width) {
45 fprintf(stream, "%04.4lx: ", (long)i);
48 /* hex not disabled, show it */
49 for(c = 0; c < width; c++)
51 fprintf(stream, "%02x ", ptr[i+c]);
56 for(c = 0; (c < width) && (i+c < size); c++) {
57 /* check for 0D0A; if found, skip past and start a new line of output */
58 if (nohex && (i+c+1 < size) && ptr[i+c]==0x0D && ptr[i+c+1]==0x0A) {
63 (ptr[i+c]>=0x20) && (ptr[i+c]<0x80)?ptr[i+c]:'.');
64 /* check again for 0D0A, to avoid an extra \n if it's at width */
65 if (nohex && (i+c+2 < size) && ptr[i+c+1]==0x0D && ptr[i+c+2]==0x0A) {
70 fputc('\n', stream); /* newline */
76 int my_trace(CURL *handle, curl_infotype type,
77 unsigned char *data, size_t size,
82 (void)handle; /* prevent compiler warning */
86 fprintf(stderr, "== Info: %s", data);
87 default: /* in case a new one is introduced to shock us */
90 case CURLINFO_HEADER_OUT:
91 text = "=> Send header";
93 case CURLINFO_DATA_OUT:
94 text = "=> Send data";
96 case CURLINFO_HEADER_IN:
97 text = "<= Recv header";
99 case CURLINFO_DATA_IN:
100 text = "<= Recv data";
104 dump(text, stderr, data, size, TRUE);
109 * Simply download a HTTP file.
111 int main(int argc, char **argv)
116 int still_running; /* keep number of running handles */
118 http_handle = curl_easy_init();
120 /* set the options (I left out a few, you'll get the point anyway) */
121 curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.example.com/");
123 curl_easy_setopt(http_handle, CURLOPT_DEBUGFUNCTION, my_trace);
124 curl_easy_setopt(http_handle, CURLOPT_VERBOSE, 1L);
126 /* init a multi stack */
127 multi_handle = curl_multi_init();
129 /* add the individual transfers */
130 curl_multi_add_handle(multi_handle, http_handle);
132 /* we start some action by calling perform right away */
133 curl_multi_perform(multi_handle, &still_running);
135 while(still_running) {
136 struct timeval timeout;
137 int rc; /* select() return code */
144 long curl_timeo = -1;
150 /* set a suitable timeout to play around with */
154 curl_multi_timeout(multi_handle, &curl_timeo);
155 if(curl_timeo >= 0) {
156 timeout.tv_sec = curl_timeo / 1000;
157 if(timeout.tv_sec > 1)
160 timeout.tv_usec = (curl_timeo % 1000) * 1000;
163 /* get file descriptors from the transfers */
164 curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
166 /* In a real-world program you OF COURSE check the return code of the
167 function calls. On success, the value of maxfd is guaranteed to be
168 greater or equal than -1. We call select(maxfd + 1, ...), specially in
169 case of (maxfd == -1), we call select(0, ...), which is basically equal
172 rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
178 printf("select() returns error, this is badness\n");
182 /* timeout or readable/writable sockets */
183 curl_multi_perform(multi_handle, &still_running);
188 curl_multi_cleanup(multi_handle);
190 curl_easy_cleanup(http_handle);