1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2017, 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 https://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_convert.h"
30 #include "tool_msgs.h"
31 #include "tool_cb_dbg.h"
32 #include "tool_util.h"
34 #include "memdebug.h" /* keep this as LAST include */
36 static void dump(const char *timebuf, const char *text,
37 FILE *stream, const unsigned char *ptr, size_t size,
38 trace tracetype, curl_infotype infotype);
41 ** callback for CURLOPT_DEBUGFUNCTION
44 int tool_debug_cb(CURL *handle, curl_infotype type,
45 char *data, size_t size,
48 struct OperationConfig *operation = userdata;
49 struct GlobalConfig *config = operation->global;
50 FILE *output = config->errors;
56 static time_t epoch_offset;
57 static int known_offset;
59 (void)handle; /* not used */
61 if(config->tracetime) {
64 epoch_offset = time(NULL) - tv.tv_sec;
67 secs = epoch_offset + tv.tv_sec;
68 now = localtime(&secs); /* not thread safe but we don't care */
69 snprintf(timebuf, sizeof(timebuf), "%02d:%02d:%02d.%06ld ",
70 now->tm_hour, now->tm_min, now->tm_sec, (long)tv.tv_usec);
75 if(!config->trace_stream) {
77 if(!strcmp("-", config->trace_dump))
78 config->trace_stream = stdout;
79 else if(!strcmp("%", config->trace_dump))
80 /* Ok, this is somewhat hackish but we do it undocumented for now */
81 config->trace_stream = config->errors; /* aka stderr */
83 config->trace_stream = fopen(config->trace_dump, FOPEN_WRITETEXT);
84 config->trace_fopened = TRUE;
88 if(config->trace_stream)
89 output = config->trace_stream;
92 warnf(config, "Failed to create/open output");
96 if(config->tracetype == TRACE_PLAIN) {
98 * This is the trace look that is similar to what libcurl makes on its
101 static const char * const s_infotype[] = {
102 "*", "<", ">", "{", "}", "{", "}"
106 static bool newl = FALSE;
107 static bool traced_data = FALSE;
110 case CURLINFO_HEADER_OUT:
112 for(i = 0; i < size - 1; i++) {
113 if(data[i] == '\n') { /* LF */
115 fprintf(output, "%s%s ", timebuf, s_infotype[type]);
117 (void)fwrite(data + st, i - st + 1, 1, output);
123 fprintf(output, "%s%s ", timebuf, s_infotype[type]);
124 (void)fwrite(data + st, i - st + 1, 1, output);
126 newl = (size && (data[size - 1] != '\n')) ? TRUE : FALSE;
130 case CURLINFO_HEADER_IN:
132 fprintf(output, "%s%s ", timebuf, s_infotype[type]);
133 (void)fwrite(data, size, 1, output);
134 newl = (size && (data[size - 1] != '\n')) ? TRUE : FALSE;
137 case CURLINFO_DATA_OUT:
138 case CURLINFO_DATA_IN:
139 case CURLINFO_SSL_DATA_IN:
140 case CURLINFO_SSL_DATA_OUT:
142 /* if the data is output to a tty and we're sending this debug trace
143 to stderr or stdout, we don't display the alert about the data not
144 being shown as the data _is_ shown then just not via this
146 if(!config->isatty || ((output != stderr) && (output != stdout))) {
148 fprintf(output, "%s%s ", timebuf, s_infotype[type]);
149 fprintf(output, "[%zd bytes data]\n", size);
164 #ifdef CURL_DOES_CONVERSIONS
165 /* Special processing is needed for CURLINFO_HEADER_OUT blocks
166 * if they contain both headers and data (separated by CRLFCRLF).
167 * We dump the header text and then switch type to CURLINFO_DATA_OUT.
169 if((type == CURLINFO_HEADER_OUT) && (size > 4)) {
171 for(i = 0; i < size - 4; i++) {
172 if(memcmp(&data[i], "\r\n\r\n", 4) == 0) {
173 /* dump everything through the CRLFCRLF as a sent header */
174 text = "=> Send header";
175 dump(timebuf, text, output, (unsigned char *)data, i + 4,
176 config->tracetype, type);
179 type = CURLINFO_DATA_OUT;
185 #endif /* CURL_DOES_CONVERSIONS */
189 fprintf(output, "%s== Info: %s", timebuf, data);
191 default: /* in case a new one is introduced to shock us */
194 case CURLINFO_HEADER_OUT:
195 text = "=> Send header";
197 case CURLINFO_DATA_OUT:
198 text = "=> Send data";
200 case CURLINFO_HEADER_IN:
201 text = "<= Recv header";
203 case CURLINFO_DATA_IN:
204 text = "<= Recv data";
206 case CURLINFO_SSL_DATA_IN:
207 text = "<= Recv SSL data";
209 case CURLINFO_SSL_DATA_OUT:
210 text = "=> Send SSL data";
214 dump(timebuf, text, output, (unsigned char *) data, size, config->tracetype,
219 static void dump(const char *timebuf, const char *text,
220 FILE *stream, const unsigned char *ptr, size_t size,
221 trace tracetype, curl_infotype infotype)
226 unsigned int width = 0x10;
228 if(tracetype == TRACE_ASCII)
229 /* without the hex output, we can fit more on screen */
232 fprintf(stream, "%s%s, %zd bytes (0x%zx)\n", timebuf, text, size, size);
234 for(i = 0; i < size; i += width) {
236 fprintf(stream, "%04zx: ", i);
238 if(tracetype == TRACE_BIN) {
239 /* hex not disabled, show it */
240 for(c = 0; c < width; c++)
242 fprintf(stream, "%02x ", ptr[i + c]);
247 for(c = 0; (c < width) && (i + c < size); c++) {
248 /* check for 0D0A; if found, skip past and start a new line of output */
249 if((tracetype == TRACE_ASCII) &&
250 (i + c + 1 < size) && (ptr[i + c] == 0x0D) &&
251 (ptr[i + c + 1] == 0x0A)) {
252 i += (c + 2 - width);
255 #ifdef CURL_DOES_CONVERSIONS
256 /* repeat the 0D0A check above but use the host encoding for CRLF */
257 if((tracetype == TRACE_ASCII) &&
258 (i + c + 1 < size) && (ptr[i + c] == '\r') &&
259 (ptr[i + c + 1] == '\n')) {
260 i += (c + 2 - width);
263 /* convert to host encoding and print this character */
264 fprintf(stream, "%c", convert_char(infotype, ptr[i + c]));
267 fprintf(stream, "%c", ((ptr[i + c] >= 0x20) && (ptr[i + c] < 0x80)) ?
268 ptr[i + c] : UNPRINTABLE_CHAR);
269 #endif /* CURL_DOES_CONVERSIONS */
270 /* check again for 0D0A, to avoid an extra \n if it's at width */
271 if((tracetype == TRACE_ASCII) &&
272 (i + c + 2 < size) && (ptr[i + c + 1] == 0x0D) &&
273 (ptr[i + c + 2] == 0x0A)) {
274 i += (c + 3 - width);
278 fputc('\n', stream); /* newline */