Coverage Report

Created: 2024-02-25 06:14

/src/PROJ/curl/lib/curl_trc.c
Line
Count
Source (jump to first uncovered line)
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
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
0
{
60
0
  if(data->set.verbose) {
61
0
    static const char s_infotype[CURLINFO_END][3] = {
62
0
      "* ", "< ", "> ", "{ ", "} ", "{ ", "} " };
63
0
    if(data->set.fdebug) {
64
0
      bool inCallback = Curl_is_in_callback(data);
65
0
      Curl_set_in_callback(data, true);
66
0
      (void)(*data->set.fdebug)(data, type, ptr, size, data->set.debugdata);
67
0
      Curl_set_in_callback(data, inCallback);
68
0
    }
69
0
    else {
70
0
      switch(type) {
71
0
      case CURLINFO_TEXT:
72
0
      case CURLINFO_HEADER_OUT:
73
0
      case CURLINFO_HEADER_IN:
74
0
        fwrite(s_infotype[type], 2, 1, data->set.err);
75
0
        fwrite(ptr, size, 1, data->set.err);
76
0
        break;
77
0
      default: /* nada */
78
0
        break;
79
0
      }
80
0
    }
81
0
  }
82
0
}
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
0
{
90
0
  DEBUGASSERT(!strchr(fmt, '\n'));
91
0
  if(data->set.verbose || data->set.errorbuffer) {
92
0
    va_list ap;
93
0
    int len;
94
0
    char error[CURL_ERROR_SIZE + 2];
95
0
    va_start(ap, fmt);
96
0
    len = mvsnprintf(error, CURL_ERROR_SIZE, fmt, ap);
97
98
0
    if(data->set.errorbuffer && !data->state.errorbuf) {
99
0
      strcpy(data->set.errorbuffer, error);
100
0
      data->state.errorbuf = TRUE; /* wrote error string */
101
0
    }
102
0
    error[len++] = '\n';
103
0
    error[len] = '\0';
104
0
    Curl_debug(data, CURLINFO_TEXT, error, len);
105
0
    va_end(ap);
106
0
  }
107
0
}
108
109
#if !defined(CURL_DISABLE_VERBOSE_STRINGS)
110
111
/* Curl_infof() is for info message along the way */
112
0
#define MAXINFO 2048
113
114
void Curl_infof(struct Curl_easy *data, const char *fmt, ...)
115
0
{
116
0
  DEBUGASSERT(!strchr(fmt, '\n'));
117
0
  if(Curl_trc_is_verbose(data)) {
118
0
    va_list ap;
119
0
    int len = 0;
120
0
    char buffer[MAXINFO + 2];
121
0
    if(data->state.feat)
122
0
      len = msnprintf(buffer, MAXINFO, "[%s] ", data->state.feat->name);
123
0
    va_start(ap, fmt);
124
0
    len += mvsnprintf(buffer + len, MAXINFO - len, fmt, ap);
125
0
    va_end(ap);
126
0
    buffer[len++] = '\n';
127
0
    buffer[len] = '\0';
128
0
    Curl_debug(data, CURLINFO_TEXT, buffer, len);
129
0
  }
130
0
}
131
132
void Curl_trc_cf_infof(struct Curl_easy *data, struct Curl_cfilter *cf,
133
                       const char *fmt, ...)
134
0
{
135
0
  DEBUGASSERT(cf);
136
0
  if(Curl_trc_cf_is_verbose(cf, data)) {
137
0
    va_list ap;
138
0
    int len = 0;
139
0
    char buffer[MAXINFO + 2];
140
0
    if(data->state.feat)
141
0
      len += msnprintf(buffer + len, MAXINFO - len, "[%s] ",
142
0
                       data->state.feat->name);
143
0
    if(cf->sockindex)
144
0
      len += msnprintf(buffer + len, MAXINFO - len, "[%s-%d] ",
145
0
                      cf->cft->name, cf->sockindex);
146
0
    else
147
0
      len += msnprintf(buffer + len, MAXINFO - len, "[%s] ", cf->cft->name);
148
0
    va_start(ap, fmt);
149
0
    len += mvsnprintf(buffer + len, MAXINFO - len, fmt, ap);
150
0
    va_end(ap);
151
0
    buffer[len++] = '\n';
152
0
    buffer[len] = '\0';
153
0
    Curl_debug(data, CURLINFO_TEXT, buffer, len);
154
0
  }
155
0
}
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
0
{
202
0
  char *token, *tok_buf, *tmp;
203
0
  size_t i;
204
0
  int lvl;
205
206
0
  tmp = strdup(config);
207
0
  if(!tmp)
208
0
    return CURLE_OUT_OF_MEMORY;
209
210
0
  token = strtok_r(tmp, ", ", &tok_buf);
211
0
  while(token) {
212
0
    switch(*token) {
213
0
      case '-':
214
0
        lvl = CURL_LOG_LVL_NONE;
215
0
        ++token;
216
0
        break;
217
0
      case '+':
218
0
        lvl = CURL_LOG_LVL_INFO;
219
0
        ++token;
220
0
        break;
221
0
      default:
222
0
        lvl = CURL_LOG_LVL_INFO;
223
0
        break;
224
0
    }
225
0
    for(i = 0; cf_types[i]; ++i) {
226
0
      if(strcasecompare(token, "all")) {
227
0
        cf_types[i]->log_level = lvl;
228
0
      }
229
0
      else if(strcasecompare(token, cf_types[i]->name)) {
230
0
        cf_types[i]->log_level = lvl;
231
0
        break;
232
0
      }
233
0
    }
234
0
    for(i = 0; trc_feats[i]; ++i) {
235
0
      if(strcasecompare(token, "all")) {
236
0
        trc_feats[i]->log_level = lvl;
237
0
      }
238
0
      else if(strcasecompare(token, trc_feats[i]->name)) {
239
0
        trc_feats[i]->log_level = lvl;
240
0
        break;
241
0
      }
242
0
    }
243
0
    token = strtok_r(NULL, ", ", &tok_buf);
244
0
  }
245
0
  free(tmp);
246
0
  return CURLE_OK;
247
0
}
248
249
CURLcode Curl_trc_init(void)
250
0
{
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
0
  return CURLE_OK;
260
0
}
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) */