1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2012, 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 ***************************************************************************/
22 #include "tool_setup.h"
24 #define ENABLE_CURLX_PRINTF
25 /* use our own printf() functions */
28 #include "tool_cfgable.h"
29 #include "tool_msgs.h"
30 #include "tool_cb_dbg.h"
31 #include "tool_util.h"
33 #include "memdebug.h" /* keep this as LAST include */
35 static void dump(const char *timebuf, const char *text,
36 FILE *stream, const unsigned char *ptr, size_t size,
37 trace tracetype, curl_infotype infotype);
40 ** callback for CURLOPT_DEBUGFUNCTION
43 int tool_debug_cb(CURL *handle, curl_infotype type,
44 unsigned char *data, size_t size,
47 struct Configurable *config = userdata;
48 FILE *output = config->errors;
54 static time_t epoch_offset;
55 static int known_offset;
57 (void)handle; /* not used */
59 if(config->tracetime) {
62 epoch_offset = time(NULL) - tv.tv_sec;
65 secs = epoch_offset + tv.tv_sec;
66 now = localtime(&secs); /* not thread safe but we don't care */
67 snprintf(timebuf, sizeof(timebuf), "%02d:%02d:%02d.%06ld ",
68 now->tm_hour, now->tm_min, now->tm_sec, (long)tv.tv_usec);
73 if(!config->trace_stream) {
75 if(curlx_strequal("-", config->trace_dump))
76 config->trace_stream = stdout;
77 else if(curlx_strequal("%", config->trace_dump))
78 /* Ok, this is somewhat hackish but we do it undocumented for now */
79 config->trace_stream = config->errors; /* aka stderr */
81 config->trace_stream = fopen(config->trace_dump, "w");
82 config->trace_fopened = TRUE;
86 if(config->trace_stream)
87 output = config->trace_stream;
90 warnf(config, "Failed to create/open output");
94 if(config->tracetype == TRACE_PLAIN) {
96 * This is the trace look that is similar to what libcurl makes on its
99 static const char * const s_infotype[] = {
100 "*", "<", ">", "{", "}", "{", "}"
104 static bool newl = FALSE;
105 static bool traced_data = FALSE;
108 case CURLINFO_HEADER_OUT:
110 for(i = 0; i < size - 1; i++) {
111 if(data[i] == '\n') { /* LF */
113 fprintf(output, "%s%s ", timebuf, s_infotype[type]);
115 (void)fwrite(data + st, i - st + 1, 1, output);
121 fprintf(output, "%s%s ", timebuf, s_infotype[type]);
122 (void)fwrite(data + st, i - st + 1, 1, output);
124 newl = (size && (data[size - 1] != '\n')) ? TRUE : FALSE;
128 case CURLINFO_HEADER_IN:
130 fprintf(output, "%s%s ", timebuf, s_infotype[type]);
131 (void)fwrite(data, size, 1, output);
132 newl = (size && (data[size - 1] != '\n')) ? TRUE : FALSE;
135 case CURLINFO_DATA_OUT:
136 case CURLINFO_DATA_IN:
137 case CURLINFO_SSL_DATA_IN:
138 case CURLINFO_SSL_DATA_OUT:
140 /* if the data is output to a tty and we're sending this debug trace
141 to stderr or stdout, we don't display the alert about the data not
142 being shown as the data _is_ shown then just not via this
144 if(!config->isatty ||
145 ((output != stderr) && (output != stdout))) {
147 fprintf(output, "%s%s ", timebuf, s_infotype[type]);
148 fprintf(output, "[data not shown]\n");
163 #ifdef CURL_DOES_CONVERSIONS
164 /* Special processing is needed for CURLINFO_HEADER_OUT blocks
165 * if they contain both headers and data (separated by CRLFCRLF).
166 * We dump the header text and then switch type to CURLINFO_DATA_OUT.
168 if((type == CURLINFO_HEADER_OUT) && (size > 4)) {
170 for(i = 0; i < size - 4; i++) {
171 if(memcmp(&data[i], "\r\n\r\n", 4) == 0) {
172 /* dump everything through the CRLFCRLF as a sent header */
173 text = "=> Send header";
174 dump(timebuf, text, output, data, i + 4, config->tracetype, type);
177 type = CURLINFO_DATA_OUT;
183 #endif /* CURL_DOES_CONVERSIONS */
187 fprintf(output, "%s== Info: %s", timebuf, data);
188 default: /* in case a new one is introduced to shock us */
191 case CURLINFO_HEADER_OUT:
192 text = "=> Send header";
194 case CURLINFO_DATA_OUT:
195 text = "=> Send data";
197 case CURLINFO_HEADER_IN:
198 text = "<= Recv header";
200 case CURLINFO_DATA_IN:
201 text = "<= Recv data";
203 case CURLINFO_SSL_DATA_IN:
204 text = "<= Recv SSL data";
206 case CURLINFO_SSL_DATA_OUT:
207 text = "=> Send SSL data";
211 dump(timebuf, text, output, data, size, config->tracetype, type);
215 static void dump(const char *timebuf, const char *text,
216 FILE *stream, const unsigned char *ptr, size_t size,
217 trace tracetype, curl_infotype infotype)
222 unsigned int width = 0x10;
224 if(tracetype == TRACE_ASCII)
225 /* without the hex output, we can fit more on screen */
228 fprintf(stream, "%s%s, %zd bytes (0x%zx)\n", timebuf, text, size, size);
230 for(i = 0; i < size; i += width) {
232 fprintf(stream, "%04zx: ", i);
234 if(tracetype == TRACE_BIN) {
235 /* hex not disabled, show it */
236 for(c = 0; c < width; c++)
238 fprintf(stream, "%02x ", ptr[i+c]);
243 for(c = 0; (c < width) && (i+c < size); c++) {
244 /* check for 0D0A; if found, skip past and start a new line of output */
245 if((tracetype == TRACE_ASCII) &&
246 (i+c+1 < size) && (ptr[i+c] == 0x0D) && (ptr[i+c+1] == 0x0A)) {
250 #ifdef CURL_DOES_CONVERSIONS
251 /* repeat the 0D0A check above but use the host encoding for CRLF */
252 if((tracetype == TRACE_ASCII) &&
253 (i+c+1 < size) && (ptr[i+c] == '\r') && (ptr[i+c+1] == '\n')) {
257 /* convert to host encoding and print this character */
258 fprintf(stream, "%c", convert_char(infotype, ptr[i+c]));
261 fprintf(stream, "%c", ((ptr[i+c] >= 0x20) && (ptr[i+c] < 0x80)) ?
262 ptr[i+c] : UNPRINTABLE_CHAR);
263 #endif /* CURL_DOES_CONVERSIONS */
264 /* check again for 0D0A, to avoid an extra \n if it's at width */
265 if((tracetype == TRACE_ASCII) &&
266 (i+c+2 < size) && (ptr[i+c+1] == 0x0D) && (ptr[i+c+2] == 0x0A)) {
271 fputc('\n', stream); /* newline */