2
0

curl_trc.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. /* CURLOPT_DEBUGFUNCTION doc says the user may set CURLOPT_PRIVATE to
  58. distinguish their handle from internal handles. */
  59. if(data->internal)
  60. DEBUGASSERT(!data->set.private_data);
  61. Curl_set_in_callback(data, true);
  62. (void)(*data->set.fdebug)(data, type, ptr, size, data->set.debugdata);
  63. Curl_set_in_callback(data, inCallback);
  64. }
  65. else {
  66. switch(type) {
  67. case CURLINFO_TEXT:
  68. case CURLINFO_HEADER_OUT:
  69. case CURLINFO_HEADER_IN:
  70. fwrite(s_infotype[type], 2, 1, data->set.err);
  71. fwrite(ptr, size, 1, data->set.err);
  72. break;
  73. default: /* nada */
  74. break;
  75. }
  76. }
  77. }
  78. }
  79. /* Curl_failf() is for messages stating why we failed.
  80. * The message SHALL NOT include any LF or CR.
  81. */
  82. void Curl_failf(struct Curl_easy *data, const char *fmt, ...)
  83. {
  84. DEBUGASSERT(!strchr(fmt, '\n'));
  85. if(data->set.verbose || data->set.errorbuffer) {
  86. va_list ap;
  87. int len;
  88. char error[CURL_ERROR_SIZE + 2];
  89. va_start(ap, fmt);
  90. len = mvsnprintf(error, CURL_ERROR_SIZE, fmt, ap);
  91. if(data->set.errorbuffer && !data->state.errorbuf) {
  92. strcpy(data->set.errorbuffer, error);
  93. data->state.errorbuf = TRUE; /* wrote error string */
  94. }
  95. error[len++] = '\n';
  96. error[len] = '\0';
  97. Curl_debug(data, CURLINFO_TEXT, error, len);
  98. va_end(ap);
  99. }
  100. }
  101. /* Curl_infof() is for info message along the way */
  102. #define MAXINFO 2048
  103. void Curl_infof(struct Curl_easy *data, const char *fmt, ...)
  104. {
  105. DEBUGASSERT(!strchr(fmt, '\n'));
  106. if(data && data->set.verbose) {
  107. va_list ap;
  108. int len;
  109. char buffer[MAXINFO + 2];
  110. va_start(ap, fmt);
  111. len = mvsnprintf(buffer, MAXINFO, fmt, ap);
  112. va_end(ap);
  113. buffer[len++] = '\n';
  114. buffer[len] = '\0';
  115. Curl_debug(data, CURLINFO_TEXT, buffer, len);
  116. }
  117. }
  118. #if !defined(CURL_DISABLE_VERBOSE_STRINGS)
  119. void Curl_trc_cf_infof(struct Curl_easy *data, struct Curl_cfilter *cf,
  120. const char *fmt, ...)
  121. {
  122. DEBUGASSERT(cf);
  123. if(data && Curl_trc_cf_is_verbose(cf, data)) {
  124. va_list ap;
  125. int len;
  126. char buffer[MAXINFO + 2];
  127. len = msnprintf(buffer, MAXINFO, "[%s] ", cf->cft->name);
  128. va_start(ap, fmt);
  129. len += mvsnprintf(buffer + len, MAXINFO - len, fmt, ap);
  130. va_end(ap);
  131. buffer[len++] = '\n';
  132. buffer[len] = '\0';
  133. Curl_debug(data, CURLINFO_TEXT, buffer, len);
  134. }
  135. }
  136. static struct Curl_cftype *cf_types[] = {
  137. &Curl_cft_tcp,
  138. &Curl_cft_udp,
  139. &Curl_cft_unix,
  140. &Curl_cft_tcp_accept,
  141. &Curl_cft_happy_eyeballs,
  142. &Curl_cft_setup,
  143. #ifdef USE_NGHTTP2
  144. &Curl_cft_nghttp2,
  145. #endif
  146. #ifdef USE_SSL
  147. &Curl_cft_ssl,
  148. &Curl_cft_ssl_proxy,
  149. #endif
  150. #if !defined(CURL_DISABLE_PROXY)
  151. #if !defined(CURL_DISABLE_HTTP)
  152. &Curl_cft_h1_proxy,
  153. #ifdef USE_NGHTTP2
  154. &Curl_cft_h2_proxy,
  155. #endif
  156. &Curl_cft_http_proxy,
  157. #endif /* !CURL_DISABLE_HTTP */
  158. &Curl_cft_haproxy,
  159. &Curl_cft_socks_proxy,
  160. #endif /* !CURL_DISABLE_PROXY */
  161. #ifdef ENABLE_QUIC
  162. &Curl_cft_http3,
  163. #endif
  164. #if !defined(CURL_DISABLE_HTTP) && !defined(USE_HYPER)
  165. &Curl_cft_http_connect,
  166. #endif
  167. NULL,
  168. };
  169. CURLcode Curl_trc_opt(const char *config)
  170. {
  171. char *token, *tok_buf, *tmp;
  172. size_t i;
  173. int lvl;
  174. tmp = strdup(config);
  175. if(!tmp)
  176. return CURLE_OUT_OF_MEMORY;
  177. token = strtok_r(tmp, ", ", &tok_buf);
  178. while(token) {
  179. switch(*token) {
  180. case '-':
  181. lvl = CURL_LOG_LVL_NONE;
  182. ++token;
  183. break;
  184. case '+':
  185. lvl = CURL_LOG_LVL_INFO;
  186. ++token;
  187. break;
  188. default:
  189. lvl = CURL_LOG_LVL_INFO;
  190. break;
  191. }
  192. for(i = 0; cf_types[i]; ++i) {
  193. if(strcasecompare(token, "all")) {
  194. cf_types[i]->log_level = lvl;
  195. }
  196. else if(strcasecompare(token, cf_types[i]->name)) {
  197. cf_types[i]->log_level = lvl;
  198. break;
  199. }
  200. }
  201. token = strtok_r(NULL, ", ", &tok_buf);
  202. }
  203. free(tmp);
  204. return CURLE_OK;
  205. }
  206. CURLcode Curl_trc_init(void)
  207. {
  208. #ifdef DEBUGBUILD
  209. /* WIP: we use the auto-init from an env var only in DEBUG builds for
  210. * convenience. */
  211. const char *config = getenv("CURL_DEBUG");
  212. if(config) {
  213. return Curl_trc_opt(config);
  214. }
  215. #endif
  216. return CURLE_OK;
  217. }
  218. #else /* !CURL_DISABLE_VERBOSE_STRINGS) */
  219. CURLcode Curl_trc_init(void)
  220. {
  221. return CURLE_OK;
  222. }
  223. #if !defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L)
  224. void Curl_trc_cf_infof(struct Curl_easy *data, struct Curl_cfilter *cf,
  225. const char *fmt, ...)
  226. {
  227. (void)data;
  228. (void)cf;
  229. (void)fmt;
  230. }
  231. #endif
  232. #endif /* !DEBUGBUILD */