Coverage Report

Created: 2023-06-07 07:02

/src/curl/lib/curl_log.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_log.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 "http2.h"
40
#include "http_proxy.h"
41
#include "cf-h1-proxy.h"
42
#include "cf-h2-proxy.h"
43
#include "cf-haproxy.h"
44
#include "cf-https-connect.h"
45
#include "socks.h"
46
#include "strtok.h"
47
#include "vtls/vtls.h"
48
#include "vquic/vquic.h"
49
50
/* The last 3 #include files should be in this order */
51
#include "curl_printf.h"
52
#include "curl_memory.h"
53
#include "memdebug.h"
54
55
56
void Curl_debug(struct Curl_easy *data, curl_infotype type,
57
                char *ptr, size_t size)
58
0
{
59
0
  if(data->set.verbose) {
60
0
    static const char s_infotype[CURLINFO_END][3] = {
61
0
      "* ", "< ", "> ", "{ ", "} ", "{ ", "} " };
62
0
    if(data->set.fdebug) {
63
0
      bool inCallback = Curl_is_in_callback(data);
64
0
      Curl_set_in_callback(data, true);
65
0
      (void)(*data->set.fdebug)(data, type, ptr, size, data->set.debugdata);
66
0
      Curl_set_in_callback(data, inCallback);
67
0
    }
68
0
    else {
69
0
      switch(type) {
70
0
      case CURLINFO_TEXT:
71
0
      case CURLINFO_HEADER_OUT:
72
0
      case CURLINFO_HEADER_IN:
73
0
        fwrite(s_infotype[type], 2, 1, data->set.err);
74
0
        fwrite(ptr, size, 1, data->set.err);
75
0
        break;
76
0
      default: /* nada */
77
0
        break;
78
0
      }
79
0
    }
80
0
  }
81
0
}
82
83
84
/* Curl_failf() is for messages stating why we failed.
85
 * The message SHALL NOT include any LF or CR.
86
 */
87
void Curl_failf(struct Curl_easy *data, const char *fmt, ...)
88
0
{
89
0
  DEBUGASSERT(!strchr(fmt, '\n'));
90
0
  if(data->set.verbose || data->set.errorbuffer) {
91
0
    va_list ap;
92
0
    int len;
93
0
    char error[CURL_ERROR_SIZE + 2];
94
0
    va_start(ap, fmt);
95
0
    len = mvsnprintf(error, CURL_ERROR_SIZE, fmt, ap);
96
97
0
    if(data->set.errorbuffer && !data->state.errorbuf) {
98
0
      strcpy(data->set.errorbuffer, error);
99
0
      data->state.errorbuf = TRUE; /* wrote error string */
100
0
    }
101
0
    error[len++] = '\n';
102
0
    error[len] = '\0';
103
0
    Curl_debug(data, CURLINFO_TEXT, error, len);
104
0
    va_end(ap);
105
0
  }
106
0
}
107
108
/* Curl_infof() is for info message along the way */
109
0
#define MAXINFO 2048
110
111
void Curl_infof(struct Curl_easy *data, const char *fmt, ...)
112
1
{
113
1
  DEBUGASSERT(!strchr(fmt, '\n'));
114
1
  if(data && data->set.verbose) {
115
0
    va_list ap;
116
0
    int len;
117
0
    char buffer[MAXINFO + 2];
118
0
    va_start(ap, fmt);
119
0
    len = mvsnprintf(buffer, MAXINFO, fmt, ap);
120
0
    va_end(ap);
121
0
    buffer[len++] = '\n';
122
0
    buffer[len] = '\0';
123
0
    Curl_debug(data, CURLINFO_TEXT, buffer, len);
124
0
  }
125
1
}
126
127
#ifdef DEBUGBUILD
128
129
void Curl_log_cf_debug(struct Curl_easy *data, struct Curl_cfilter *cf,
130
                       const char *fmt, ...)
131
0
{
132
0
  DEBUGASSERT(cf);
133
0
  if(data && Curl_log_cf_is_debug(cf, data)) {
134
0
    va_list ap;
135
0
    int len;
136
0
    char buffer[MAXINFO + 2];
137
0
    len = msnprintf(buffer, MAXINFO, "[CONN-%ld%s-%s] ",
138
0
                    cf->conn->connection_id, cf->sockindex? "/2" : "",
139
0
                    cf->cft->name);
140
0
    va_start(ap, fmt);
141
0
    len += mvsnprintf(buffer + len, MAXINFO - len, fmt, ap);
142
0
    va_end(ap);
143
0
    buffer[len++] = '\n';
144
0
    buffer[len] = '\0';
145
0
    Curl_debug(data, CURLINFO_TEXT, buffer, len);
146
0
  }
147
0
}
148
149
150
static struct Curl_cftype *cf_types[] = {
151
  &Curl_cft_tcp,
152
  &Curl_cft_udp,
153
  &Curl_cft_unix,
154
  &Curl_cft_tcp_accept,
155
  &Curl_cft_happy_eyeballs,
156
  &Curl_cft_setup,
157
#ifdef USE_NGHTTP2
158
  &Curl_cft_nghttp2,
159
#endif
160
#ifdef USE_SSL
161
  &Curl_cft_ssl,
162
  &Curl_cft_ssl_proxy,
163
#endif
164
#if !defined(CURL_DISABLE_PROXY)
165
#if !defined(CURL_DISABLE_HTTP)
166
  &Curl_cft_h1_proxy,
167
#ifdef USE_NGHTTP2
168
  &Curl_cft_h2_proxy,
169
#endif
170
  &Curl_cft_http_proxy,
171
#endif /* !CURL_DISABLE_HTTP */
172
  &Curl_cft_haproxy,
173
  &Curl_cft_socks_proxy,
174
#endif /* !CURL_DISABLE_PROXY */
175
#ifdef ENABLE_QUIC
176
  &Curl_cft_http3,
177
#endif
178
#if !defined(CURL_DISABLE_HTTP) && !defined(USE_HYPER)
179
  &Curl_cft_http_connect,
180
#endif
181
  NULL,
182
};
183
184
#ifndef ARRAYSIZE
185
#define ARRAYSIZE(A) (sizeof(A)/sizeof((A)[0]))
186
#endif
187
188
CURLcode Curl_log_init(void)
189
1
{
190
1
  const char *setting = getenv("CURL_DEBUG");
191
1
  if(setting) {
192
0
    char *token, *tok_buf, *tmp;
193
0
    size_t i;
194
195
0
    tmp = strdup(setting);
196
0
    if(!tmp)
197
0
      return CURLE_OUT_OF_MEMORY;
198
199
0
    token = strtok_r(tmp, ", ", &tok_buf);
200
0
    while(token) {
201
0
      for(i = 0; cf_types[i]; ++i) {
202
0
        if(strcasecompare(token, cf_types[i]->name)) {
203
0
          cf_types[i]->log_level = CURL_LOG_DEBUG;
204
0
          break;
205
0
        }
206
0
      }
207
0
      token = strtok_r(NULL, ", ", &tok_buf);
208
0
    }
209
0
    free(tmp);
210
0
  }
211
1
  return CURLE_OK;
212
1
}
213
#else /* DEBUGBUILD */
214
215
CURLcode Curl_log_init(void)
216
{
217
  return CURLE_OK;
218
}
219
220
#if !defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L)
221
void Curl_log_cf_debug(struct Curl_easy *data, struct Curl_cfilter *cf,
222
                       const char *fmt, ...)
223
{
224
  (void)data;
225
  (void)cf;
226
  (void)fmt;
227
}
228
#endif
229
230
#endif /* !DEBUGBUILD */