1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
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 http://curl.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
22 /* This is an example showing the multi interface and the debug callback. */
27 /* somewhat unix-specific */
32 #include <curl/curl.h>
38 void dump(const char *text,
39 FILE *stream, unsigned char *ptr, size_t size,
45 unsigned int width=0x10;
48 /* without the hex output, we can fit more on screen */
51 fprintf(stream, "%s, %10.10ld bytes (0x%8.8lx)\n",
52 text, (long)size, (long)size);
54 for(i=0; i<size; i+= width) {
56 fprintf(stream, "%4.4lx: ", (long)i);
59 /* hex not disabled, show it */
60 for(c = 0; c < width; c++)
62 fprintf(stream, "%02x ", ptr[i+c]);
67 for(c = 0; (c < width) && (i+c < size); c++) {
68 /* check for 0D0A; if found, skip past and start a new line of output */
69 if (nohex && (i+c+1 < size) && ptr[i+c]==0x0D && ptr[i+c+1]==0x0A) {
74 (ptr[i+c]>=0x20) && (ptr[i+c]<0x80)?ptr[i+c]:'.');
75 /* check again for 0D0A, to avoid an extra \n if it's at width */
76 if (nohex && (i+c+2 < size) && ptr[i+c+1]==0x0D && ptr[i+c+2]==0x0A) {
81 fputc('\n', stream); /* newline */
87 int my_trace(CURL *handle, curl_infotype type,
88 unsigned char *data, size_t size,
94 (void)handle; /* prevent compiler warning */
98 fprintf(stderr, "== Info: %s", data);
99 default: /* in case a new one is introduced to shock us */
102 case CURLINFO_HEADER_OUT:
103 text = "=> Send header";
105 case CURLINFO_DATA_OUT:
106 text = "=> Send data";
108 case CURLINFO_HEADER_IN:
109 text = "<= Recv header";
111 case CURLINFO_DATA_IN:
112 text = "<= Recv data";
116 dump(text, stderr, data, size, TRUE);
121 * Simply download a HTTP file.
128 int still_running; /* keep number of running handles */
130 http_handle = curl_easy_init();
132 /* set the options (I left out a few, you'll get the point anyway) */
133 curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.example.com/");
135 curl_easy_setopt(http_handle, CURLOPT_DEBUGFUNCTION, my_trace);
136 curl_easy_setopt(http_handle, CURLOPT_VERBOSE, 1L);
138 /* init a multi stack */
139 multi_handle = curl_multi_init();
141 /* add the individual transfers */
142 curl_multi_add_handle(multi_handle, http_handle);
144 /* we start some action by calling perform right away */
145 curl_multi_perform(multi_handle, &still_running);
148 struct timeval timeout;
149 int rc; /* select() return code */
150 CURLMcode mc; /* curl_multi_fdset() return code */
157 long curl_timeo = -1;
163 /* set a suitable timeout to play around with */
167 curl_multi_timeout(multi_handle, &curl_timeo);
168 if(curl_timeo >= 0) {
169 timeout.tv_sec = curl_timeo / 1000;
170 if(timeout.tv_sec > 1)
173 timeout.tv_usec = (curl_timeo % 1000) * 1000;
176 /* get file descriptors from the transfers */
177 mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
181 fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
185 /* On success the value of maxfd is guaranteed to be >= -1. We call
186 select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
187 no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
188 to sleep 100ms, which is the minimum suggested value in the
189 curl_multi_fdset() doc. */
196 /* Portable sleep for platforms other than Windows. */
197 struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
198 rc = select(0, NULL, NULL, NULL, &wait);
202 /* Note that on some platforms 'timeout' may be modified by select().
203 If you need access to the original value save a copy beforehand. */
204 rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
211 printf("select() returns error, this is badness\n");
215 /* timeout or readable/writable sockets */
216 curl_multi_perform(multi_handle, &still_running);
219 } while(still_running);
221 curl_multi_cleanup(multi_handle);
223 curl_easy_cleanup(http_handle);