Annotation of embedaddon/curl/docs/examples/multi-debugcallback.c, revision 1.1.1.1
1.1 misho 1: /***************************************************************************
2: * _ _ ____ _
3: * Project ___| | | | _ \| |
4: * / __| | | | |_) | |
5: * | (__| |_| | _ <| |___
6: * \___|\___/|_| \_\_____|
7: *
8: * Copyright (C) 1998 - 2018, 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:
37: typedef char bool;
38: #define TRUE 1
39:
40: static
41: void 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.10lu bytes (0x%8.8lx)\n",
55: text, (unsigned long)size, (unsigned long)size);
56:
57: for(i = 0; i<size; i += width) {
58:
59: fprintf(stream, "%4.4lx: ", (unsigned 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 &&
73: ptr[i + c + 1] == 0x0A) {
74: i += (c + 2 - width);
75: break;
76: }
77: fprintf(stream, "%c",
78: (ptr[i + c] >= 0x20) && (ptr[i + c]<0x80)?ptr[i + c]:'.');
79: /* check again for 0D0A, to avoid an extra \n if it's at width */
80: if(nohex && (i + c + 2 < size) && ptr[i + c + 1] == 0x0D &&
81: ptr[i + c + 2] == 0x0A) {
82: i += (c + 3 - width);
83: break;
84: }
85: }
86: fputc('\n', stream); /* newline */
87: }
88: fflush(stream);
89: }
90:
91: static
92: int my_trace(CURL *handle, curl_infotype type,
93: unsigned char *data, size_t size,
94: void *userp)
95: {
96: const char *text;
97:
98: (void)userp;
99: (void)handle; /* prevent compiler warning */
100:
101: switch(type) {
102: case CURLINFO_TEXT:
103: fprintf(stderr, "== Info: %s", data);
104: /* FALLTHROUGH */
105: default: /* in case a new one is introduced to shock us */
106: return 0;
107:
108: case CURLINFO_HEADER_OUT:
109: text = "=> Send header";
110: break;
111: case CURLINFO_DATA_OUT:
112: text = "=> Send data";
113: break;
114: case CURLINFO_HEADER_IN:
115: text = "<= Recv header";
116: break;
117: case CURLINFO_DATA_IN:
118: text = "<= Recv data";
119: break;
120: }
121:
122: dump(text, stderr, data, size, TRUE);
123: return 0;
124: }
125:
126: /*
127: * Simply download a HTTP file.
128: */
129: int main(void)
130: {
131: CURL *http_handle;
132: CURLM *multi_handle;
133:
134: int still_running = 0; /* keep number of running handles */
135:
136: http_handle = curl_easy_init();
137:
138: /* set the options (I left out a few, you'll get the point anyway) */
139: curl_easy_setopt(http_handle, CURLOPT_URL, "https://www.example.com/");
140:
141: curl_easy_setopt(http_handle, CURLOPT_DEBUGFUNCTION, my_trace);
142: curl_easy_setopt(http_handle, CURLOPT_VERBOSE, 1L);
143:
144: /* init a multi stack */
145: multi_handle = curl_multi_init();
146:
147: /* add the individual transfers */
148: curl_multi_add_handle(multi_handle, http_handle);
149:
150: /* we start some action by calling perform right away */
151: curl_multi_perform(multi_handle, &still_running);
152:
153: while(still_running) {
154: struct timeval timeout;
155: int rc; /* select() return code */
156: CURLMcode mc; /* curl_multi_fdset() return code */
157:
158: fd_set fdread;
159: fd_set fdwrite;
160: fd_set fdexcep;
161: int maxfd = -1;
162:
163: long curl_timeo = -1;
164:
165: FD_ZERO(&fdread);
166: FD_ZERO(&fdwrite);
167: FD_ZERO(&fdexcep);
168:
169: /* set a suitable timeout to play around with */
170: timeout.tv_sec = 1;
171: timeout.tv_usec = 0;
172:
173: curl_multi_timeout(multi_handle, &curl_timeo);
174: if(curl_timeo >= 0) {
175: timeout.tv_sec = curl_timeo / 1000;
176: if(timeout.tv_sec > 1)
177: timeout.tv_sec = 1;
178: else
179: timeout.tv_usec = (curl_timeo % 1000) * 1000;
180: }
181:
182: /* get file descriptors from the transfers */
183: mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
184:
185: if(mc != CURLM_OK) {
186: fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
187: break;
188: }
189:
190: /* On success the value of maxfd is guaranteed to be >= -1. We call
191: select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
192: no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
193: to sleep 100ms, which is the minimum suggested value in the
194: curl_multi_fdset() doc. */
195:
196: if(maxfd == -1) {
197: #ifdef _WIN32
198: Sleep(100);
199: rc = 0;
200: #else
201: /* Portable sleep for platforms other than Windows. */
202: struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
203: rc = select(0, NULL, NULL, NULL, &wait);
204: #endif
205: }
206: else {
207: /* Note that on some platforms 'timeout' may be modified by select().
208: If you need access to the original value save a copy beforehand. */
209: rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
210: }
211:
212: switch(rc) {
213: case -1:
214: /* select error */
215: still_running = 0;
216: printf("select() returns error, this is badness\n");
217: break;
218: case 0:
219: default:
220: /* timeout or readable/writable sockets */
221: curl_multi_perform(multi_handle, &still_running);
222: break;
223: }
224: }
225:
226: curl_multi_cleanup(multi_handle);
227:
228: curl_easy_cleanup(http_handle);
229:
230: return 0;
231: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>