1 /*****************************************************************************
3 * Project ___| | | | _ \| |
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
17 /* somewhat unix-specific */
22 #include <curl/curl.h>
28 void dump(const char *text
,
29 FILE *stream
, unsigned char *ptr
, size_t size
,
35 unsigned int width
=0x10;
38 /* without the hex output, we can fit more on screen */
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
);
48 /* hex not disabled, show it */
49 for(c
= 0; c
< width
; c
++)
51 fprintf(stream
, "%02x ", ptr
[i
+c
]);
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) {
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) {
70 fputc('\n', stream
); /* newline */
76 int my_trace(CURL
*handle
, curl_infotype type
,
77 char *data
, size_t size
,
82 (void)handle
; /* prevent compiler warning */
86 fprintf(stderr
, "== Info: %s", data
);
87 default: /* in case a new one is introduced to shock us */
90 case CURLINFO_HEADER_OUT
:
91 text
= "=> Send header";
93 case CURLINFO_DATA_OUT
:
94 text
= "=> Send data";
96 case CURLINFO_HEADER_IN
:
97 text
= "<= Recv header";
99 case CURLINFO_DATA_IN
:
100 text
= "<= Recv data";
104 dump(text
, stderr
, data
, size
, TRUE
);
109 * Simply download a HTTP file.
111 int main(int argc
, char **argv
)
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 */
149 /* set a suitable timeout to play around with */
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
);
166 printf("select() returns error, this is badness\n");
170 /* timeout or readable/writable sockets */
171 while(CURLM_CALL_MULTI_PERFORM
==
172 curl_multi_perform(multi_handle
, &still_running
));
177 curl_multi_cleanup(multi_handle
);
179 curl_easy_cleanup(http_handle
);