1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2013, 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 http://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 #include <curl/curl.h>
26 char trace_ascii; /* 1 or 0 */
30 void dump(const char *text,
31 FILE *stream, unsigned char *ptr, size_t size,
37 unsigned int width=0x10;
40 /* without the hex output, we can fit more on screen */
43 fprintf(stream, "%s, %10.10ld bytes (0x%8.8lx)\n",
44 text, (long)size, (long)size);
46 for(i=0; i<size; i+= width) {
48 fprintf(stream, "%4.4lx: ", (long)i);
51 /* hex not disabled, show it */
52 for(c = 0; c < width; c++)
54 fprintf(stream, "%02x ", ptr[i+c]);
59 for(c = 0; (c < width) && (i+c < size); c++) {
60 /* check for 0D0A; if found, skip past and start a new line of output */
61 if (nohex && (i+c+1 < size) && ptr[i+c]==0x0D && ptr[i+c+1]==0x0A) {
66 (ptr[i+c]>=0x20) && (ptr[i+c]<0x80)?ptr[i+c]:'.');
67 /* check again for 0D0A, to avoid an extra \n if it's at width */
68 if (nohex && (i+c+2 < size) && ptr[i+c+1]==0x0D && ptr[i+c+2]==0x0A) {
73 fputc('\n', stream); /* newline */
79 int my_trace(CURL *handle, curl_infotype type,
80 char *data, size_t size,
83 struct data *config = (struct data *)userp;
85 (void)handle; /* prevent compiler warning */
89 fprintf(stderr, "== Info: %s", data);
90 default: /* in case a new one is introduced to shock us */
93 case CURLINFO_HEADER_OUT:
94 text = "=> Send header";
96 case CURLINFO_DATA_OUT:
97 text = "=> Send data";
99 case CURLINFO_SSL_DATA_OUT:
100 text = "=> Send SSL data";
102 case CURLINFO_HEADER_IN:
103 text = "<= Recv header";
105 case CURLINFO_DATA_IN:
106 text = "<= Recv data";
108 case CURLINFO_SSL_DATA_IN:
109 text = "<= Recv SSL data";
113 dump(text, stderr, (unsigned char *)data, size, config->trace_ascii);
123 config.trace_ascii = 1; /* enable ascii tracing */
125 curl = curl_easy_init();
127 curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace);
128 curl_easy_setopt(curl, CURLOPT_DEBUGDATA, &config);
130 /* the DEBUGFUNCTION has no effect until we enable VERBOSE */
131 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
133 /* example.com is redirected, so we tell libcurl to follow redirection */
134 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
136 curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/");
137 res = curl_easy_perform(curl);
138 /* Check for errors */
140 fprintf(stderr, "curl_easy_perform() failed: %s\n",
141 curl_easy_strerror(res));
144 curl_easy_cleanup(curl);