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