1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
10 * This is a very simple example using the multi interface and the debug
17 /* somewhat unix-specific */
22 #include <curl/curl.h>
28 void dump(const char *text,
29 FILE *stream, unsigned char *ptr, size_t size,
35 unsigned int width=0x10;
38 /* without the hex output, we can fit more on screen */
41 fprintf(stream, "%s, %010.10ld bytes (0x%08.8lx)\n",
42 text, (long)size, (long)size);
44 for(i=0; i<size; i+= width) {
46 fprintf(stream, "%04.4lx: ", (long)i);
49 /* hex not disabled, show it */
50 for(c = 0; c < width; c++)
52 fprintf(stream, "%02x ", ptr[i+c]);
57 for(c = 0; (c < width) && (i+c < size); c++) {
58 /* check for 0D0A; if found, skip past and start a new line of output */
59 if (nohex && (i+c+1 < size) && ptr[i+c]==0x0D && ptr[i+c+1]==0x0A) {
64 (ptr[i+c]>=0x20) && (ptr[i+c]<0x80)?ptr[i+c]:'.');
65 /* check again for 0D0A, to avoid an extra \n if it's at width */
66 if (nohex && (i+c+2 < size) && ptr[i+c+1]==0x0D && ptr[i+c+2]==0x0A) {
71 fputc('\n', stream); /* newline */
77 int my_trace(CURL *handle, curl_infotype type,
78 unsigned char *data, size_t size,
83 (void)handle; /* prevent compiler warning */
87 fprintf(stderr, "== Info: %s", data);
88 default: /* in case a new one is introduced to shock us */
91 case CURLINFO_HEADER_OUT:
92 text = "=> Send header";
94 case CURLINFO_DATA_OUT:
95 text = "=> Send data";
97 case CURLINFO_HEADER_IN:
98 text = "<= Recv header";
100 case CURLINFO_DATA_IN:
101 text = "<= Recv data";
105 dump(text, stderr, data, size, TRUE);
110 * Simply download a HTTP file.
112 int main(int argc, char **argv)
117 int still_running; /* keep number of running handles */
119 http_handle = curl_easy_init();
121 /* set the options (I left out a few, you'll get the point anyway) */
122 curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.haxx.se/");
124 curl_easy_setopt(http_handle, CURLOPT_DEBUGFUNCTION, my_trace);
125 curl_easy_setopt(http_handle, CURLOPT_VERBOSE, 1L);
127 /* init a multi stack */
128 multi_handle = curl_multi_init();
130 /* add the individual transfers */
131 curl_multi_add_handle(multi_handle, http_handle);
133 /* we start some action by calling perform right away */
134 while(CURLM_CALL_MULTI_PERFORM ==
135 curl_multi_perform(multi_handle, &still_running));
137 while(still_running) {
138 struct timeval timeout;
139 int rc; /* select() return code */
150 /* set a suitable timeout to play around with */
154 /* get file descriptors from the transfers */
155 curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
157 /* In a real-world program you OF COURSE check the return code of the
158 function calls, *and* you make sure that maxfd is bigger than -1
159 so that the call to select() below makes sense! */
161 rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
167 printf("select() returns error, this is badness\n");
171 /* timeout or readable/writable sockets */
172 while(CURLM_CALL_MULTI_PERFORM ==
173 curl_multi_perform(multi_handle, &still_running));
178 curl_multi_cleanup(multi_handle);
180 curl_easy_cleanup(http_handle);