1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2016, 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 https://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 ***************************************************************************/
23 * multi interface and debug callback
30 /* somewhat unix-specific */
35 #include <curl/curl.h>
41 void dump(const char *text,
42 FILE *stream, unsigned char *ptr, size_t size,
48 unsigned int width=0x10;
51 /* without the hex output, we can fit more on screen */
54 fprintf(stream, "%s, %10.10ld bytes (0x%8.8lx)\n",
55 text, (long)size, (long)size);
57 for(i=0; i<size; i+= width) {
59 fprintf(stream, "%4.4lx: ", (long)i);
62 /* hex not disabled, show it */
63 for(c = 0; c < width; c++)
65 fprintf(stream, "%02x ", ptr[i+c]);
70 for(c = 0; (c < width) && (i+c < size); c++) {
71 /* check for 0D0A; if found, skip past and start a new line of output */
72 if(nohex && (i+c+1 < size) && ptr[i+c]==0x0D && ptr[i+c+1]==0x0A) {
77 (ptr[i+c]>=0x20) && (ptr[i+c]<0x80)?ptr[i+c]:'.');
78 /* check again for 0D0A, to avoid an extra \n if it's at width */
79 if(nohex && (i+c+2 < size) && ptr[i+c+1]==0x0D && ptr[i+c+2]==0x0A) {
84 fputc('\n', stream); /* newline */
90 int my_trace(CURL *handle, curl_infotype type,
91 unsigned char *data, size_t size,
97 (void)handle; /* prevent compiler warning */
101 fprintf(stderr, "== Info: %s", data);
102 default: /* in case a new one is introduced to shock us */
105 case CURLINFO_HEADER_OUT:
106 text = "=> Send header";
108 case CURLINFO_DATA_OUT:
109 text = "=> Send data";
111 case CURLINFO_HEADER_IN:
112 text = "<= Recv header";
114 case CURLINFO_DATA_IN:
115 text = "<= Recv data";
119 dump(text, stderr, data, size, TRUE);
124 * Simply download a HTTP file.
131 int still_running; /* keep number of running handles */
133 http_handle = curl_easy_init();
135 /* set the options (I left out a few, you'll get the point anyway) */
136 curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.example.com/");
138 curl_easy_setopt(http_handle, CURLOPT_DEBUGFUNCTION, my_trace);
139 curl_easy_setopt(http_handle, CURLOPT_VERBOSE, 1L);
141 /* init a multi stack */
142 multi_handle = curl_multi_init();
144 /* add the individual transfers */
145 curl_multi_add_handle(multi_handle, http_handle);
147 /* we start some action by calling perform right away */
148 curl_multi_perform(multi_handle, &still_running);
151 struct timeval timeout;
152 int rc; /* select() return code */
153 CURLMcode mc; /* curl_multi_fdset() return code */
160 long curl_timeo = -1;
166 /* set a suitable timeout to play around with */
170 curl_multi_timeout(multi_handle, &curl_timeo);
171 if(curl_timeo >= 0) {
172 timeout.tv_sec = curl_timeo / 1000;
173 if(timeout.tv_sec > 1)
176 timeout.tv_usec = (curl_timeo % 1000) * 1000;
179 /* get file descriptors from the transfers */
180 mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
183 fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
187 /* On success the value of maxfd is guaranteed to be >= -1. We call
188 select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
189 no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
190 to sleep 100ms, which is the minimum suggested value in the
191 curl_multi_fdset() doc. */
198 /* Portable sleep for platforms other than Windows. */
199 struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
200 rc = select(0, NULL, NULL, NULL, &wait);
204 /* Note that on some platforms 'timeout' may be modified by select().
205 If you need access to the original value save a copy beforehand. */
206 rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
213 printf("select() returns error, this is badness\n");
217 /* timeout or readable/writable sockets */
218 curl_multi_perform(multi_handle, &still_running);
221 } while(still_running);
223 curl_multi_cleanup(multi_handle);
225 curl_easy_cleanup(http_handle);