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