blob: 07f044b4ea53ba8347ef271d1ead721e5ea92af2 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
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.
13 *
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.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ***************************************************************************/
22/* <DESC>
23 * multi interface and debug callback
24 * </DESC>
25 */
26
27#include <stdio.h>
28#include <string.h>
29
30/* somewhat unix-specific */
31#include <sys/time.h>
32#include <unistd.h>
33
34/* curl stuff */
35#include <curl/curl.h>
36
37typedef char bool;
38#define TRUE 1
39
40static
41void dump(const char *text,
42 FILE *stream, unsigned char *ptr, size_t size,
43 bool nohex)
44{
45 size_t i;
46 size_t c;
47
48 unsigned int width=0x10;
49
50 if(nohex)
51 /* without the hex output, we can fit more on screen */
52 width = 0x40;
53
54 fprintf(stream, "%s, %10.10ld bytes (0x%8.8lx)\n",
55 text, (long)size, (long)size);
56
57 for(i=0; i<size; i+= width) {
58
59 fprintf(stream, "%4.4lx: ", (long)i);
60
61 if(!nohex) {
62 /* hex not disabled, show it */
63 for(c = 0; c < width; c++)
64 if(i+c < size)
65 fprintf(stream, "%02x ", ptr[i+c]);
66 else
67 fputs(" ", stream);
68 }
69
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) {
73 i+=(c+2-width);
74 break;
75 }
76 fprintf(stream, "%c",
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) {
80 i+=(c+3-width);
81 break;
82 }
83 }
84 fputc('\n', stream); /* newline */
85 }
86 fflush(stream);
87}
88
89static
90int my_trace(CURL *handle, curl_infotype type,
91 unsigned char *data, size_t size,
92 void *userp)
93{
94 const char *text;
95
96 (void)userp;
97 (void)handle; /* prevent compiler warning */
98
99 switch(type) {
100 case CURLINFO_TEXT:
101 fprintf(stderr, "== Info: %s", data);
102 /* FALLTHROUGH */
103 default: /* in case a new one is introduced to shock us */
104 return 0;
105
106 case CURLINFO_HEADER_OUT:
107 text = "=> Send header";
108 break;
109 case CURLINFO_DATA_OUT:
110 text = "=> Send data";
111 break;
112 case CURLINFO_HEADER_IN:
113 text = "<= Recv header";
114 break;
115 case CURLINFO_DATA_IN:
116 text = "<= Recv data";
117 break;
118 }
119
120 dump(text, stderr, data, size, TRUE);
121 return 0;
122}
123
124/*
125 * Simply download a HTTP file.
126 */
127int main(void)
128{
129 CURL *http_handle;
130 CURLM *multi_handle;
131
132 int still_running; /* keep number of running handles */
133
134 http_handle = curl_easy_init();
135
136 /* set the options (I left out a few, you'll get the point anyway) */
137 curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.example.com/");
138
139 curl_easy_setopt(http_handle, CURLOPT_DEBUGFUNCTION, my_trace);
140 curl_easy_setopt(http_handle, CURLOPT_VERBOSE, 1L);
141
142 /* init a multi stack */
143 multi_handle = curl_multi_init();
144
145 /* add the individual transfers */
146 curl_multi_add_handle(multi_handle, http_handle);
147
148 /* we start some action by calling perform right away */
149 curl_multi_perform(multi_handle, &still_running);
150
151 do {
152 struct timeval timeout;
153 int rc; /* select() return code */
154 CURLMcode mc; /* curl_multi_fdset() return code */
155
156 fd_set fdread;
157 fd_set fdwrite;
158 fd_set fdexcep;
159 int maxfd = -1;
160
161 long curl_timeo = -1;
162
163 FD_ZERO(&fdread);
164 FD_ZERO(&fdwrite);
165 FD_ZERO(&fdexcep);
166
167 /* set a suitable timeout to play around with */
168 timeout.tv_sec = 1;
169 timeout.tv_usec = 0;
170
171 curl_multi_timeout(multi_handle, &curl_timeo);
172 if(curl_timeo >= 0) {
173 timeout.tv_sec = curl_timeo / 1000;
174 if(timeout.tv_sec > 1)
175 timeout.tv_sec = 1;
176 else
177 timeout.tv_usec = (curl_timeo % 1000) * 1000;
178 }
179
180 /* get file descriptors from the transfers */
181 mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
182
183 if(mc != CURLM_OK) {
184 fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
185 break;
186 }
187
188 /* On success the value of maxfd is guaranteed to be >= -1. We call
189 select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
190 no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
191 to sleep 100ms, which is the minimum suggested value in the
192 curl_multi_fdset() doc. */
193
194 if(maxfd == -1) {
195#ifdef _WIN32
196 Sleep(100);
197 rc = 0;
198#else
199 /* Portable sleep for platforms other than Windows. */
200 struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
201 rc = select(0, NULL, NULL, NULL, &wait);
202#endif
203 }
204 else {
205 /* Note that on some platforms 'timeout' may be modified by select().
206 If you need access to the original value save a copy beforehand. */
207 rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
208 }
209
210 switch(rc) {
211 case -1:
212 /* select error */
213 still_running = 0;
214 printf("select() returns error, this is badness\n");
215 break;
216 case 0:
217 default:
218 /* timeout or readable/writable sockets */
219 curl_multi_perform(multi_handle, &still_running);
220 break;
221 }
222 } while(still_running);
223
224 curl_multi_cleanup(multi_handle);
225
226 curl_easy_cleanup(http_handle);
227
228 return 0;
229}