Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl-7.19.0 / docs / examples / multi-debugcallback.c
blobfbe096c8cc2da576b6ed89a43aa7a614a01d06d1
1 /*****************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * $Id: multi-debugcallback.c,v 1.1.1.1 2008-09-23 16:32:05 hoffman Exp $
10 * This is a very simple example using the multi interface and the debug
11 * callback.
14 #include <stdio.h>
15 #include <string.h>
17 /* somewhat unix-specific */
18 #include <sys/time.h>
19 #include <unistd.h>
21 /* curl stuff */
22 #include <curl/curl.h>
24 typedef char bool;
25 #define TRUE 1
27 static
28 void dump(const char *text,
29 FILE *stream, unsigned char *ptr, size_t size,
30 bool nohex)
32 size_t i;
33 size_t c;
35 unsigned int width=0x10;
37 if(nohex)
38 /* without the hex output, we can fit more on screen */
39 width = 0x40;
41 fprintf(stream, "%s, %zd bytes (0x%zx)\n", text, size, size);
43 for(i=0; i<size; i+= width) {
45 fprintf(stream, "%04zx: ", i);
47 if(!nohex) {
48 /* hex not disabled, show it */
49 for(c = 0; c < width; c++)
50 if(i+c < size)
51 fprintf(stream, "%02x ", ptr[i+c]);
52 else
53 fputs(" ", stream);
56 for(c = 0; (c < width) && (i+c < size); c++) {
57 /* check for 0D0A; if found, skip past and start a new line of output */
58 if (nohex && (i+c+1 < size) && ptr[i+c]==0x0D && ptr[i+c+1]==0x0A) {
59 i+=(c+2-width);
60 break;
62 fprintf(stream, "%c",
63 (ptr[i+c]>=0x20) && (ptr[i+c]<0x80)?ptr[i+c]:'.');
64 /* check again for 0D0A, to avoid an extra \n if it's at width */
65 if (nohex && (i+c+2 < size) && ptr[i+c+1]==0x0D && ptr[i+c+2]==0x0A) {
66 i+=(c+3-width);
67 break;
70 fputc('\n', stream); /* newline */
72 fflush(stream);
75 static
76 int my_trace(CURL *handle, curl_infotype type,
77 char *data, size_t size,
78 void *userp)
80 const char *text;
82 (void)handle; /* prevent compiler warning */
84 switch (type) {
85 case CURLINFO_TEXT:
86 fprintf(stderr, "== Info: %s", data);
87 default: /* in case a new one is introduced to shock us */
88 return 0;
90 case CURLINFO_HEADER_OUT:
91 text = "=> Send header";
92 break;
93 case CURLINFO_DATA_OUT:
94 text = "=> Send data";
95 break;
96 case CURLINFO_HEADER_IN:
97 text = "<= Recv header";
98 break;
99 case CURLINFO_DATA_IN:
100 text = "<= Recv data";
101 break;
104 dump(text, stderr, data, size, TRUE);
105 return 0;
109 * Simply download a HTTP file.
111 int main(int argc, char **argv)
113 CURL *http_handle;
114 CURLM *multi_handle;
116 int still_running; /* keep number of running handles */
118 http_handle = curl_easy_init();
120 /* set the options (I left out a few, you'll get the point anyway) */
121 curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.haxx.se/");
123 curl_easy_setopt(http_handle, CURLOPT_DEBUGFUNCTION, my_trace);
124 curl_easy_setopt(http_handle, CURLOPT_VERBOSE, 1L);
126 /* init a multi stack */
127 multi_handle = curl_multi_init();
129 /* add the individual transfers */
130 curl_multi_add_handle(multi_handle, http_handle);
132 /* we start some action by calling perform right away */
133 while(CURLM_CALL_MULTI_PERFORM ==
134 curl_multi_perform(multi_handle, &still_running));
136 while(still_running) {
137 struct timeval timeout;
138 int rc; /* select() return code */
140 fd_set fdread;
141 fd_set fdwrite;
142 fd_set fdexcep;
143 int maxfd;
145 FD_ZERO(&fdread);
146 FD_ZERO(&fdwrite);
147 FD_ZERO(&fdexcep);
149 /* set a suitable timeout to play around with */
150 timeout.tv_sec = 1;
151 timeout.tv_usec = 0;
153 /* get file descriptors from the transfers */
154 curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
156 /* In a real-world program you OF COURSE check the return code of the
157 function calls, *and* you make sure that maxfd is bigger than -1
158 so that the call to select() below makes sense! */
160 rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
162 switch(rc) {
163 case -1:
164 /* select error */
165 still_running = 0;
166 printf("select() returns error, this is badness\n");
167 break;
168 case 0:
169 default:
170 /* timeout or readable/writable sockets */
171 while(CURLM_CALL_MULTI_PERFORM ==
172 curl_multi_perform(multi_handle, &still_running));
173 break;
177 curl_multi_cleanup(multi_handle);
179 curl_easy_cleanup(http_handle);
181 return 0;