tool_cb_dbg.c 8.4 KB

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