2011-09-24 23:38:16 +08:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2023-01-02 20:51:48 +08:00
|
|
|
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
|
2011-09-24 23:38:16 +08:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2020-11-04 21:02:01 +08:00
|
|
|
* are also available at https://curl.se/docs/copyright.html.
|
2011-09-24 23:38:16 +08:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: curl
|
2022-05-17 17:16:50 +08:00
|
|
|
*
|
2011-09-24 23:38:16 +08:00
|
|
|
***************************************************************************/
|
2012-04-07 05:35:15 +08:00
|
|
|
#include "tool_setup.h"
|
2011-09-24 23:38:16 +08:00
|
|
|
|
|
|
|
#define ENABLE_CURLX_PRINTF
|
|
|
|
/* use our own printf() functions */
|
|
|
|
#include "curlx.h"
|
|
|
|
|
|
|
|
#include "tool_cfgable.h"
|
|
|
|
#include "tool_msgs.h"
|
|
|
|
#include "tool_cb_dbg.h"
|
2011-10-06 23:39:00 +08:00
|
|
|
#include "tool_util.h"
|
2011-09-24 23:38:16 +08:00
|
|
|
|
2013-01-04 09:50:28 +08:00
|
|
|
#include "memdebug.h" /* keep this as LAST include */
|
2011-09-24 23:38:16 +08:00
|
|
|
|
2023-06-11 17:02:29 +08:00
|
|
|
static void dump(const char *timebuf, const char *idsbuf, const char *text,
|
2011-09-24 23:38:16 +08:00
|
|
|
FILE *stream, const unsigned char *ptr, size_t size,
|
|
|
|
trace tracetype, curl_infotype infotype);
|
|
|
|
|
2023-05-26 17:29:25 +08:00
|
|
|
/*
|
|
|
|
* Return the formatted HH:MM:SS for the tv_sec given.
|
|
|
|
* NOT thread safe.
|
|
|
|
*/
|
|
|
|
static const char *hms_for_sec(time_t tv_sec)
|
|
|
|
{
|
|
|
|
static time_t cached_tv_sec;
|
|
|
|
static char hms_buf[12];
|
|
|
|
static time_t epoch_offset;
|
|
|
|
static int known_epoch;
|
|
|
|
|
|
|
|
if(tv_sec != cached_tv_sec) {
|
|
|
|
struct tm *now;
|
|
|
|
time_t secs;
|
|
|
|
/* recalculate */
|
|
|
|
if(!known_epoch) {
|
|
|
|
epoch_offset = time(NULL) - tv_sec;
|
|
|
|
known_epoch = 1;
|
|
|
|
}
|
|
|
|
secs = epoch_offset + tv_sec;
|
|
|
|
/* !checksrc! disable BANNEDFUNC 1 */
|
|
|
|
now = localtime(&secs); /* not thread safe but we don't care */
|
|
|
|
msnprintf(hms_buf, sizeof(hms_buf), "%02d:%02d:%02d",
|
|
|
|
now->tm_hour, now->tm_min, now->tm_sec);
|
|
|
|
cached_tv_sec = tv_sec;
|
|
|
|
}
|
|
|
|
return hms_buf;
|
|
|
|
}
|
|
|
|
|
2023-06-11 17:02:29 +08:00
|
|
|
static void log_line_start(FILE *log, const char *timebuf,
|
|
|
|
const char *idsbuf, curl_infotype type)
|
2023-05-26 17:29:25 +08:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* This is the trace look that is similar to what libcurl makes on its
|
|
|
|
* own.
|
|
|
|
*/
|
|
|
|
static const char * const s_infotype[] = {
|
|
|
|
"* ", "< ", "> ", "{ ", "} ", "{ ", "} "
|
|
|
|
};
|
2023-06-11 17:02:29 +08:00
|
|
|
if((timebuf && *timebuf) || (idsbuf && *idsbuf))
|
|
|
|
fprintf(log, "%s%s%s", timebuf, idsbuf, s_infotype[type]);
|
2023-05-26 17:29:25 +08:00
|
|
|
else
|
|
|
|
fputs(s_infotype[type], log);
|
|
|
|
}
|
|
|
|
|
2023-06-11 17:02:29 +08:00
|
|
|
#define TRC_IDS_FORMAT_IDS_1 "[%" CURL_FORMAT_CURL_OFF_T "-x] "
|
|
|
|
#define TRC_IDS_FORMAT_IDS_2 "[%" CURL_FORMAT_CURL_OFF_T "-%" \
|
|
|
|
CURL_FORMAT_CURL_OFF_T "] "
|
2011-09-24 23:38:16 +08:00
|
|
|
/*
|
|
|
|
** callback for CURLOPT_DEBUGFUNCTION
|
|
|
|
*/
|
|
|
|
int tool_debug_cb(CURL *handle, curl_infotype type,
|
2017-03-30 20:59:14 +08:00
|
|
|
char *data, size_t size,
|
2011-09-24 23:38:16 +08:00
|
|
|
void *userdata)
|
|
|
|
{
|
2014-02-28 04:51:49 +08:00
|
|
|
struct OperationConfig *operation = userdata;
|
|
|
|
struct GlobalConfig *config = operation->global;
|
2023-09-28 18:50:07 +08:00
|
|
|
FILE *output = tool_stderr;
|
2011-09-24 23:38:16 +08:00
|
|
|
const char *text;
|
|
|
|
struct timeval tv;
|
|
|
|
char timebuf[20];
|
2023-06-11 17:02:29 +08:00
|
|
|
/* largest signed 64bit is: 9,223,372,036,854,775,807
|
|
|
|
* max length in decimal: 1 + (6*3) = 19
|
|
|
|
* formatted via TRC_IDS_FORMAT_IDS_2 this becomes 2 + 19 + 1 + 19 + 2 = 43
|
|
|
|
* negative xfer-id are not printed, negative conn-ids use TRC_IDS_FORMAT_1
|
|
|
|
*/
|
|
|
|
char idsbuf[60];
|
|
|
|
curl_off_t xfer_id, conn_id;
|
2011-09-24 23:38:16 +08:00
|
|
|
|
|
|
|
(void)handle; /* not used */
|
|
|
|
|
|
|
|
if(config->tracetime) {
|
2011-10-06 23:39:00 +08:00
|
|
|
tv = tvnow();
|
2023-05-26 17:29:25 +08:00
|
|
|
msnprintf(timebuf, sizeof(timebuf), "%s.%06ld ",
|
|
|
|
hms_for_sec(tv.tv_sec), (long)tv.tv_usec);
|
2011-09-24 23:38:16 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
timebuf[0] = 0;
|
|
|
|
|
2023-06-11 17:02:29 +08:00
|
|
|
if(handle && config->traceids &&
|
|
|
|
!curl_easy_getinfo(handle, CURLINFO_XFER_ID, &xfer_id) && xfer_id >= 0) {
|
|
|
|
if(!curl_easy_getinfo(handle, CURLINFO_CONN_ID, &conn_id) &&
|
|
|
|
conn_id >= 0) {
|
|
|
|
msnprintf(idsbuf, sizeof(idsbuf), TRC_IDS_FORMAT_IDS_2,
|
|
|
|
xfer_id, conn_id);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
msnprintf(idsbuf, sizeof(idsbuf), TRC_IDS_FORMAT_IDS_1, xfer_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
idsbuf[0] = 0;
|
|
|
|
|
2011-09-24 23:38:16 +08:00
|
|
|
if(!config->trace_stream) {
|
|
|
|
/* open for append */
|
2016-09-30 23:15:05 +08:00
|
|
|
if(!strcmp("-", config->trace_dump))
|
2011-09-24 23:38:16 +08:00
|
|
|
config->trace_stream = stdout;
|
2016-09-30 23:15:05 +08:00
|
|
|
else if(!strcmp("%", config->trace_dump))
|
2011-09-24 23:38:16 +08:00
|
|
|
/* Ok, this is somewhat hackish but we do it undocumented for now */
|
2023-09-28 18:50:07 +08:00
|
|
|
config->trace_stream = tool_stderr;
|
2011-09-24 23:38:16 +08:00
|
|
|
else {
|
2015-06-01 15:20:18 +08:00
|
|
|
config->trace_stream = fopen(config->trace_dump, FOPEN_WRITETEXT);
|
2011-09-24 23:38:16 +08:00
|
|
|
config->trace_fopened = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(config->trace_stream)
|
|
|
|
output = config->trace_stream;
|
|
|
|
|
|
|
|
if(!output) {
|
2015-02-28 04:48:38 +08:00
|
|
|
warnf(config, "Failed to create/open output");
|
2011-09-24 23:38:16 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(config->tracetype == TRACE_PLAIN) {
|
|
|
|
static bool newl = FALSE;
|
|
|
|
static bool traced_data = FALSE;
|
|
|
|
|
|
|
|
switch(type) {
|
|
|
|
case CURLINFO_HEADER_OUT:
|
2012-03-27 15:32:19 +08:00
|
|
|
if(size > 0) {
|
2018-06-03 04:52:56 +08:00
|
|
|
size_t st = 0;
|
|
|
|
size_t i;
|
2012-03-27 15:32:19 +08:00
|
|
|
for(i = 0; i < size - 1; i++) {
|
|
|
|
if(data[i] == '\n') { /* LF */
|
|
|
|
if(!newl) {
|
2023-06-11 17:02:29 +08:00
|
|
|
log_line_start(output, timebuf, idsbuf, type);
|
2012-03-27 15:32:19 +08:00
|
|
|
}
|
|
|
|
(void)fwrite(data + st, i - st + 1, 1, output);
|
|
|
|
st = i + 1;
|
|
|
|
newl = FALSE;
|
2011-09-24 23:38:16 +08:00
|
|
|
}
|
|
|
|
}
|
2012-03-27 15:32:19 +08:00
|
|
|
if(!newl)
|
2023-06-11 17:02:29 +08:00
|
|
|
log_line_start(output, timebuf, idsbuf, type);
|
2012-03-27 15:32:19 +08:00
|
|
|
(void)fwrite(data + st, i - st + 1, 1, output);
|
2011-09-24 23:38:16 +08:00
|
|
|
}
|
|
|
|
newl = (size && (data[size - 1] != '\n')) ? TRUE : FALSE;
|
|
|
|
traced_data = FALSE;
|
|
|
|
break;
|
|
|
|
case CURLINFO_TEXT:
|
|
|
|
case CURLINFO_HEADER_IN:
|
|
|
|
if(!newl)
|
2023-06-11 17:02:29 +08:00
|
|
|
log_line_start(output, timebuf, idsbuf, type);
|
2011-09-24 23:38:16 +08:00
|
|
|
(void)fwrite(data, size, 1, output);
|
|
|
|
newl = (size && (data[size - 1] != '\n')) ? TRUE : FALSE;
|
|
|
|
traced_data = FALSE;
|
|
|
|
break;
|
|
|
|
case CURLINFO_DATA_OUT:
|
|
|
|
case CURLINFO_DATA_IN:
|
|
|
|
case CURLINFO_SSL_DATA_IN:
|
|
|
|
case CURLINFO_SSL_DATA_OUT:
|
|
|
|
if(!traced_data) {
|
|
|
|
/* if the data is output to a tty and we're sending this debug trace
|
|
|
|
to stderr or stdout, we don't display the alert about the data not
|
|
|
|
being shown as the data _is_ shown then just not via this
|
|
|
|
function */
|
2023-09-28 18:50:07 +08:00
|
|
|
if(!config->isatty ||
|
|
|
|
((output != tool_stderr) && (output != stdout))) {
|
2011-09-24 23:38:16 +08:00
|
|
|
if(!newl)
|
2023-06-11 17:02:29 +08:00
|
|
|
log_line_start(output, timebuf, idsbuf, type);
|
2018-05-11 23:34:55 +08:00
|
|
|
fprintf(output, "[%zu bytes data]\n", size);
|
2011-09-24 23:38:16 +08:00
|
|
|
newl = FALSE;
|
|
|
|
traced_data = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default: /* nada */
|
|
|
|
newl = FALSE;
|
|
|
|
traced_data = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-12-14 06:34:59 +08:00
|
|
|
switch(type) {
|
2011-09-24 23:38:16 +08:00
|
|
|
case CURLINFO_TEXT:
|
2023-06-11 17:02:29 +08:00
|
|
|
fprintf(output, "%s%s== Info: %.*s", timebuf, idsbuf, (int)size, data);
|
2023-12-08 21:05:09 +08:00
|
|
|
FALLTHROUGH();
|
2011-09-24 23:38:16 +08:00
|
|
|
default: /* in case a new one is introduced to shock us */
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case CURLINFO_HEADER_OUT:
|
|
|
|
text = "=> Send header";
|
|
|
|
break;
|
|
|
|
case CURLINFO_DATA_OUT:
|
|
|
|
text = "=> Send data";
|
|
|
|
break;
|
|
|
|
case CURLINFO_HEADER_IN:
|
|
|
|
text = "<= Recv header";
|
|
|
|
break;
|
|
|
|
case CURLINFO_DATA_IN:
|
|
|
|
text = "<= Recv data";
|
|
|
|
break;
|
|
|
|
case CURLINFO_SSL_DATA_IN:
|
|
|
|
text = "<= Recv SSL data";
|
|
|
|
break;
|
|
|
|
case CURLINFO_SSL_DATA_OUT:
|
|
|
|
text = "=> Send SSL data";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-06-11 17:02:29 +08:00
|
|
|
dump(timebuf, idsbuf, text, output, (unsigned char *) data, size,
|
|
|
|
config->tracetype, type);
|
2011-09-24 23:38:16 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-06-11 17:02:29 +08:00
|
|
|
static void dump(const char *timebuf, const char *idsbuf, const char *text,
|
2011-09-24 23:38:16 +08:00
|
|
|
FILE *stream, const unsigned char *ptr, size_t size,
|
|
|
|
trace tracetype, curl_infotype infotype)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
size_t c;
|
|
|
|
|
|
|
|
unsigned int width = 0x10;
|
|
|
|
|
|
|
|
if(tracetype == TRACE_ASCII)
|
|
|
|
/* without the hex output, we can fit more on screen */
|
|
|
|
width = 0x40;
|
|
|
|
|
2023-06-11 17:02:29 +08:00
|
|
|
fprintf(stream, "%s%s%s, %zu bytes (0x%zx)\n", timebuf, idsbuf,
|
|
|
|
text, size, size);
|
2011-09-24 23:38:16 +08:00
|
|
|
|
|
|
|
for(i = 0; i < size; i += width) {
|
|
|
|
|
|
|
|
fprintf(stream, "%04zx: ", i);
|
|
|
|
|
|
|
|
if(tracetype == TRACE_BIN) {
|
|
|
|
/* hex not disabled, show it */
|
|
|
|
for(c = 0; c < width; c++)
|
2017-09-10 05:55:08 +08:00
|
|
|
if(i + c < size)
|
|
|
|
fprintf(stream, "%02x ", ptr[i + c]);
|
2011-09-24 23:38:16 +08:00
|
|
|
else
|
|
|
|
fputs(" ", stream);
|
|
|
|
}
|
|
|
|
|
2017-09-10 05:55:08 +08:00
|
|
|
for(c = 0; (c < width) && (i + c < size); c++) {
|
2011-09-24 23:38:16 +08:00
|
|
|
/* check for 0D0A; if found, skip past and start a new line of output */
|
|
|
|
if((tracetype == TRACE_ASCII) &&
|
2017-09-10 05:55:08 +08:00
|
|
|
(i + c + 1 < size) && (ptr[i + c] == 0x0D) &&
|
|
|
|
(ptr[i + c + 1] == 0x0A)) {
|
|
|
|
i += (c + 2 - width);
|
2011-09-24 23:38:16 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
(void)infotype;
|
2022-07-17 18:10:35 +08:00
|
|
|
fprintf(stream, "%c", ((ptr[i + c] >= 0x20) && (ptr[i + c] < 0x7F)) ?
|
2017-09-10 05:55:08 +08:00
|
|
|
ptr[i + c] : UNPRINTABLE_CHAR);
|
2011-09-24 23:38:16 +08:00
|
|
|
/* check again for 0D0A, to avoid an extra \n if it's at width */
|
|
|
|
if((tracetype == TRACE_ASCII) &&
|
2017-09-10 05:55:08 +08:00
|
|
|
(i + c + 2 < size) && (ptr[i + c + 1] == 0x0D) &&
|
|
|
|
(ptr[i + c + 2] == 0x0A)) {
|
|
|
|
i += (c + 3 - width);
|
2011-09-24 23:38:16 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fputc('\n', stream); /* newline */
|
|
|
|
}
|
|
|
|
fflush(stream);
|
|
|
|
}
|