curl_trc.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #include <curl/curl.h>
  26. #include "curl_trc.h"
  27. #include "urldata.h"
  28. #include "easyif.h"
  29. #include "cfilters.h"
  30. #include "timeval.h"
  31. #include "multiif.h"
  32. #include "strcase.h"
  33. #include "cf-socket.h"
  34. #include "connect.h"
  35. #include "http2.h"
  36. #include "http_proxy.h"
  37. #include "cf-h1-proxy.h"
  38. #include "cf-h2-proxy.h"
  39. #include "cf-haproxy.h"
  40. #include "cf-https-connect.h"
  41. #include "socks.h"
  42. #include "strtok.h"
  43. #include "vtls/vtls.h"
  44. #include "vquic/vquic.h"
  45. /* The last 3 #include files should be in this order */
  46. #include "curl_printf.h"
  47. #include "curl_memory.h"
  48. #include "memdebug.h"
  49. void Curl_debug(struct Curl_easy *data, curl_infotype type,
  50. char *ptr, size_t size)
  51. {
  52. if(data->set.verbose) {
  53. static const char s_infotype[CURLINFO_END][3] = {
  54. "* ", "< ", "> ", "{ ", "} ", "{ ", "} " };
  55. if(data->set.fdebug) {
  56. bool inCallback = Curl_is_in_callback(data);
  57. Curl_set_in_callback(data, true);
  58. (void)(*data->set.fdebug)(data, type, ptr, size, data->set.debugdata);
  59. Curl_set_in_callback(data, inCallback);
  60. }
  61. else {
  62. switch(type) {
  63. case CURLINFO_TEXT:
  64. case CURLINFO_HEADER_OUT:
  65. case CURLINFO_HEADER_IN:
  66. fwrite(s_infotype[type], 2, 1, data->set.err);
  67. fwrite(ptr, size, 1, data->set.err);
  68. break;
  69. default: /* nada */
  70. break;
  71. }
  72. }
  73. }
  74. }
  75. /* Curl_failf() is for messages stating why we failed.
  76. * The message SHALL NOT include any LF or CR.
  77. */
  78. void Curl_failf(struct Curl_easy *data, const char *fmt, ...)
  79. {
  80. DEBUGASSERT(!strchr(fmt, '\n'));
  81. if(data->set.verbose || data->set.errorbuffer) {
  82. va_list ap;
  83. int len;
  84. char error[CURL_ERROR_SIZE + 2];
  85. va_start(ap, fmt);
  86. len = mvsnprintf(error, CURL_ERROR_SIZE, fmt, ap);
  87. if(data->set.errorbuffer && !data->state.errorbuf) {
  88. strcpy(data->set.errorbuffer, error);
  89. data->state.errorbuf = TRUE; /* wrote error string */
  90. }
  91. error[len++] = '\n';
  92. error[len] = '\0';
  93. Curl_debug(data, CURLINFO_TEXT, error, len);
  94. va_end(ap);
  95. }
  96. }
  97. #if !defined(CURL_DISABLE_VERBOSE_STRINGS)
  98. /* Curl_infof() is for info message along the way */
  99. #define MAXINFO 2048
  100. void Curl_infof(struct Curl_easy *data, const char *fmt, ...)
  101. {
  102. DEBUGASSERT(!strchr(fmt, '\n'));
  103. if(data && data->set.verbose) {
  104. va_list ap;
  105. int len;
  106. char buffer[MAXINFO + 2];
  107. va_start(ap, fmt);
  108. len = mvsnprintf(buffer, MAXINFO, fmt, ap);
  109. va_end(ap);
  110. buffer[len++] = '\n';
  111. buffer[len] = '\0';
  112. Curl_debug(data, CURLINFO_TEXT, buffer, len);
  113. }
  114. }
  115. void Curl_trc_cf_infof(struct Curl_easy *data, struct Curl_cfilter *cf,
  116. const char *fmt, ...)
  117. {
  118. DEBUGASSERT(cf);
  119. if(data && Curl_trc_cf_is_verbose(cf, data)) {
  120. va_list ap;
  121. int len;
  122. char buffer[MAXINFO + 2];
  123. len = msnprintf(buffer, MAXINFO, "[%s] ", cf->cft->name);
  124. va_start(ap, fmt);
  125. len += mvsnprintf(buffer + len, MAXINFO - len, fmt, ap);
  126. va_end(ap);
  127. buffer[len++] = '\n';
  128. buffer[len] = '\0';
  129. Curl_debug(data, CURLINFO_TEXT, buffer, len);
  130. }
  131. }
  132. static struct Curl_cftype *cf_types[] = {
  133. &Curl_cft_tcp,
  134. &Curl_cft_udp,
  135. &Curl_cft_unix,
  136. &Curl_cft_tcp_accept,
  137. &Curl_cft_happy_eyeballs,
  138. &Curl_cft_setup,
  139. #ifdef USE_NGHTTP2
  140. &Curl_cft_nghttp2,
  141. #endif
  142. #ifdef USE_SSL
  143. &Curl_cft_ssl,
  144. &Curl_cft_ssl_proxy,
  145. #endif
  146. #if !defined(CURL_DISABLE_PROXY)
  147. #if !defined(CURL_DISABLE_HTTP)
  148. &Curl_cft_h1_proxy,
  149. #ifdef USE_NGHTTP2
  150. &Curl_cft_h2_proxy,
  151. #endif
  152. &Curl_cft_http_proxy,
  153. #endif /* !CURL_DISABLE_HTTP */
  154. &Curl_cft_haproxy,
  155. &Curl_cft_socks_proxy,
  156. #endif /* !CURL_DISABLE_PROXY */
  157. #ifdef ENABLE_QUIC
  158. &Curl_cft_http3,
  159. #endif
  160. #if !defined(CURL_DISABLE_HTTP) && !defined(USE_HYPER)
  161. &Curl_cft_http_connect,
  162. #endif
  163. NULL,
  164. };
  165. CURLcode Curl_trc_opt(const char *config)
  166. {
  167. char *token, *tok_buf, *tmp;
  168. size_t i;
  169. int lvl;
  170. tmp = strdup(config);
  171. if(!tmp)
  172. return CURLE_OUT_OF_MEMORY;
  173. token = strtok_r(tmp, ", ", &tok_buf);
  174. while(token) {
  175. switch(*token) {
  176. case '-':
  177. lvl = CURL_LOG_LVL_NONE;
  178. ++token;
  179. break;
  180. case '+':
  181. lvl = CURL_LOG_LVL_INFO;
  182. ++token;
  183. break;
  184. default:
  185. lvl = CURL_LOG_LVL_INFO;
  186. break;
  187. }
  188. for(i = 0; cf_types[i]; ++i) {
  189. if(strcasecompare(token, "all")) {
  190. cf_types[i]->log_level = lvl;
  191. }
  192. else if(strcasecompare(token, cf_types[i]->name)) {
  193. cf_types[i]->log_level = lvl;
  194. break;
  195. }
  196. }
  197. token = strtok_r(NULL, ", ", &tok_buf);
  198. }
  199. free(tmp);
  200. return CURLE_OK;
  201. }
  202. CURLcode Curl_trc_init(void)
  203. {
  204. #ifdef DEBUGBUILD
  205. /* WIP: we use the auto-init from an env var only in DEBUG builds for
  206. * convenience. */
  207. const char *config = getenv("CURL_DEBUG");
  208. if(config) {
  209. return Curl_trc_opt(config);
  210. }
  211. #endif
  212. return CURLE_OK;
  213. }
  214. #else /* !CURL_DISABLE_VERBOSE_STRINGS) */
  215. CURLcode Curl_trc_init(void)
  216. {
  217. return CURLE_OK;
  218. }
  219. #endif /* !DEBUGBUILD */