1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
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.
13  *
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.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  ***************************************************************************/
22 #include "tool_setup.h"
23 
24 #define ENABLE_CURLX_PRINTF
25 /* use our own printf() functions */
26 #include "curlx.h"
27 
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"
33 
34 #include "memdebug.h" /* keep this as LAST include */
35 
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);
39 
40 /*
41 ** callback for CURLOPT_DEBUGFUNCTION
42 */
43 
tool_debug_cb(CURL * handle,curl_infotype type,char * data,size_t size,void * userdata)44 int tool_debug_cb(CURL *handle, curl_infotype type,
45                   char *data, size_t size,
46                   void *userdata)
47 {
48   struct OperationConfig *operation = userdata;
49   struct GlobalConfig *config = operation->global;
50   FILE *output = config->errors;
51   const char *text;
52   struct timeval tv;
53   char timebuf[20];
54   time_t secs;
55 
56   (void)handle; /* not used */
57 
58   if(config->tracetime) {
59     struct tm *now;
60     static time_t epoch_offset;
61     static int    known_offset;
62     tv = tvnow();
63     if(!known_offset) {
64       epoch_offset = time(NULL) - tv.tv_sec;
65       known_offset = 1;
66     }
67     secs = epoch_offset + tv.tv_sec;
68     /* !checksrc! disable BANNEDFUNC 1 */
69     now = localtime(&secs);  /* not thread safe but we don't care */
70     msnprintf(timebuf, sizeof(timebuf), "%02d:%02d:%02d.%06ld ",
71               now->tm_hour, now->tm_min, now->tm_sec, (long)tv.tv_usec);
72   }
73   else
74     timebuf[0] = 0;
75 
76   if(!config->trace_stream) {
77     /* open for append */
78     if(!strcmp("-", config->trace_dump))
79       config->trace_stream = stdout;
80     else if(!strcmp("%", config->trace_dump))
81       /* Ok, this is somewhat hackish but we do it undocumented for now */
82       config->trace_stream = config->errors;  /* aka stderr */
83     else {
84       config->trace_stream = fopen(config->trace_dump, FOPEN_WRITETEXT);
85       config->trace_fopened = TRUE;
86     }
87   }
88 
89   if(config->trace_stream)
90     output = config->trace_stream;
91 
92   if(!output) {
93     warnf(config, "Failed to create/open output");
94     return 0;
95   }
96 
97   if(config->tracetype == TRACE_PLAIN) {
98     /*
99      * This is the trace look that is similar to what libcurl makes on its
100      * own.
101      */
102     static const char * const s_infotype[] = {
103       "*", "<", ">", "{", "}", "{", "}"
104     };
105     static bool newl = FALSE;
106     static bool traced_data = FALSE;
107 
108     switch(type) {
109     case CURLINFO_HEADER_OUT:
110       if(size > 0) {
111         size_t st = 0;
112         size_t i;
113         for(i = 0; i < size - 1; i++) {
114           if(data[i] == '\n') { /* LF */
115             if(!newl) {
116               fprintf(output, "%s%s ", timebuf, s_infotype[type]);
117             }
118             (void)fwrite(data + st, i - st + 1, 1, output);
119             st = i + 1;
120             newl = FALSE;
121           }
122         }
123         if(!newl)
124           fprintf(output, "%s%s ", timebuf, s_infotype[type]);
125         (void)fwrite(data + st, i - st + 1, 1, output);
126       }
127       newl = (size && (data[size - 1] != '\n')) ? TRUE : FALSE;
128       traced_data = FALSE;
129       break;
130     case CURLINFO_TEXT:
131     case CURLINFO_HEADER_IN:
132       if(!newl)
133         fprintf(output, "%s%s ", timebuf, s_infotype[type]);
134       (void)fwrite(data, size, 1, output);
135       newl = (size && (data[size - 1] != '\n')) ? TRUE : FALSE;
136       traced_data = FALSE;
137       break;
138     case CURLINFO_DATA_OUT:
139     case CURLINFO_DATA_IN:
140     case CURLINFO_SSL_DATA_IN:
141     case CURLINFO_SSL_DATA_OUT:
142       if(!traced_data) {
143         /* if the data is output to a tty and we're sending this debug trace
144            to stderr or stdout, we don't display the alert about the data not
145            being shown as the data _is_ shown then just not via this
146            function */
147         if(!config->isatty || ((output != stderr) && (output != stdout))) {
148           if(!newl)
149             fprintf(output, "%s%s ", timebuf, s_infotype[type]);
150           fprintf(output, "[%zu bytes data]\n", size);
151           newl = FALSE;
152           traced_data = TRUE;
153         }
154       }
155       break;
156     default: /* nada */
157       newl = FALSE;
158       traced_data = FALSE;
159       break;
160     }
161 
162     return 0;
163   }
164 
165 #ifdef CURL_DOES_CONVERSIONS
166   /* Special processing is needed for CURLINFO_HEADER_OUT blocks
167    * if they contain both headers and data (separated by CRLFCRLF).
168    * We dump the header text and then switch type to CURLINFO_DATA_OUT.
169    */
170   if((type == CURLINFO_HEADER_OUT) && (size > 4)) {
171     size_t i;
172     for(i = 0; i < size - 4; i++) {
173       if(memcmp(&data[i], "\r\n\r\n", 4) == 0) {
174         /* dump everything through the CRLFCRLF as a sent header */
175         text = "=> Send header";
176         dump(timebuf, text, output, (unsigned char *)data, i + 4,
177              config->tracetype, type);
178         data += i + 3;
179         size -= i + 4;
180         type = CURLINFO_DATA_OUT;
181         data += 1;
182         break;
183       }
184     }
185   }
186 #endif /* CURL_DOES_CONVERSIONS */
187 
188   switch(type) {
189   case CURLINFO_TEXT:
190     fprintf(output, "%s== Info: %s", timebuf, data);
191     /* FALLTHROUGH */
192   default: /* in case a new one is introduced to shock us */
193     return 0;
194 
195   case CURLINFO_HEADER_OUT:
196     text = "=> Send header";
197     break;
198   case CURLINFO_DATA_OUT:
199     text = "=> Send data";
200     break;
201   case CURLINFO_HEADER_IN:
202     text = "<= Recv header";
203     break;
204   case CURLINFO_DATA_IN:
205     text = "<= Recv data";
206     break;
207   case CURLINFO_SSL_DATA_IN:
208     text = "<= Recv SSL data";
209     break;
210   case CURLINFO_SSL_DATA_OUT:
211     text = "=> Send SSL data";
212     break;
213   }
214 
215   dump(timebuf, text, output, (unsigned char *) data, size, config->tracetype,
216        type);
217   return 0;
218 }
219 
dump(const char * timebuf,const char * text,FILE * stream,const unsigned char * ptr,size_t size,trace tracetype,curl_infotype infotype)220 static void dump(const char *timebuf, const char *text,
221                  FILE *stream, const unsigned char *ptr, size_t size,
222                  trace tracetype, curl_infotype infotype)
223 {
224   size_t i;
225   size_t c;
226 
227   unsigned int width = 0x10;
228 
229   if(tracetype == TRACE_ASCII)
230     /* without the hex output, we can fit more on screen */
231     width = 0x40;
232 
233   fprintf(stream, "%s%s, %zu bytes (0x%zx)\n", timebuf, text, size, size);
234 
235   for(i = 0; i < size; i += width) {
236 
237     fprintf(stream, "%04zx: ", i);
238 
239     if(tracetype == TRACE_BIN) {
240       /* hex not disabled, show it */
241       for(c = 0; c < width; c++)
242         if(i + c < size)
243           fprintf(stream, "%02x ", ptr[i + c]);
244         else
245           fputs("   ", stream);
246     }
247 
248     for(c = 0; (c < width) && (i + c < size); c++) {
249       /* check for 0D0A; if found, skip past and start a new line of output */
250       if((tracetype == TRACE_ASCII) &&
251          (i + c + 1 < size) && (ptr[i + c] == 0x0D) &&
252          (ptr[i + c + 1] == 0x0A)) {
253         i += (c + 2 - width);
254         break;
255       }
256 #ifdef CURL_DOES_CONVERSIONS
257       /* repeat the 0D0A check above but use the host encoding for CRLF */
258       if((tracetype == TRACE_ASCII) &&
259          (i + c + 1 < size) && (ptr[i + c] == '\r') &&
260          (ptr[i + c + 1] == '\n')) {
261         i += (c + 2 - width);
262         break;
263       }
264       /* convert to host encoding and print this character */
265       fprintf(stream, "%c", convert_char(infotype, ptr[i + c]));
266 #else
267       (void)infotype;
268       fprintf(stream, "%c", ((ptr[i + c] >= 0x20) && (ptr[i + c] < 0x80)) ?
269               ptr[i + c] : UNPRINTABLE_CHAR);
270 #endif /* CURL_DOES_CONVERSIONS */
271       /* check again for 0D0A, to avoid an extra \n if it's at width */
272       if((tracetype == TRACE_ASCII) &&
273          (i + c + 2 < size) && (ptr[i + c + 1] == 0x0D) &&
274          (ptr[i + c + 2] == 0x0A)) {
275         i += (c + 3 - width);
276         break;
277       }
278     }
279     fputc('\n', stream); /* newline */
280   }
281   fflush(stream);
282 }
283