master
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 "tool_setup.h"
26#include "tool_cfgable.h"
27#include "tool_getparam.h"
28#include "tool_helpers.h"
29#include "tool_findfile.h"
30#include "tool_msgs.h"
31#include "tool_parsecfg.h"
32#include "tool_util.h"
34/* only acknowledge colon or equals as separators if the option was not
35 specified with an initial dash! */
36#define ISSEP(x, dash) (!(dash) && (((x) == '=') || ((x) == ':')))
38/*
39 * Copies the string from line to the param dynbuf, unquoting backslash-quoted
40 * characters and null-terminating the output string. Stops at the first
41 * non-backslash-quoted double quote character or the end of the input string.
42 * param must be at least as long as the input string. Returns 0 on success.
43 */
44static int unslashquote(const char *line, struct dynbuf *param)
45{
46 curlx_dyn_reset(param);
48 while(*line && (*line != '\"')) {
49 if(*line == '\\') {
50 char out;
51 line++;
53 /* default is to output the letter after the backslash */
54 switch(out = *line) {
55 case '\0':
56 continue; /* this breaks out of the loop */
57 case 't':
58 out = '\t';
59 break;
60 case 'n':
61 out = '\n';
62 break;
63 case 'r':
64 out = '\r';
65 break;
66 case 'v':
67 out = '\v';
68 break;
69 }
70 if(curlx_dyn_addn(param, &out, 1))
71 return 1;
72 line++;
73 }
74 else if(curlx_dyn_addn(param, line++, 1))
75 return 1;
76 }
77 return 0; /* ok */
78}
80/*
81 * Open the config file. When filename is NULL, tries to find .curlrc in the
82 * home directory (and on Windows, in the executable directory). Updates
83 * *namep to the effective filename and *pathalloc to any allocated path
84 * that must be freed by the caller. Returns the opened FILE or NULL.
85 */
86static FILE *open_config_file(const char *filename,
87 const char **namep,
88 char **pathalloc)
89{
90 FILE *file = NULL;
91 *pathalloc = NULL;
93 if(!filename) {
94 /* NULL means load .curlrc from homedir! */
95 char *curlrc = findfile(".curlrc", CURLRC_DOTSCORE);
96 if(curlrc) {
97 file = curlx_fopen(curlrc, FOPEN_READTEXT);
98 if(!file) {
99 curlx_free(curlrc);
100 return NULL;
101 }
102 *namep = *pathalloc = curlrc;
103 }
104#ifdef _WIN32
105 else {
106 char *fullp;
107 /* check for .curlrc then _curlrc in the directory of the executable */
108 file = tool_execpath(".curlrc", &fullp);
109 if(!file)
110 file = tool_execpath("_curlrc", &fullp);
111 if(file)
112 /* this is the filename we read from */
113 *namep = fullp;
114 }
115#endif
116 }
117 else {
118 if(strcmp(filename, "-"))
119 file = curlx_fopen(filename, FOPEN_READTEXT);
120 else {
121 file = stdin;
122 *namep = "<stdin>";
123 }
124 }
125 return file;
126}
128/*
129 * Extract the parameter value from a config line. The line pointer should
130 * be positioned after the option keyword has been null-terminated.
131 * Skips separators and whitespace, then handles quoted and unquoted
132 * parameter values. Sets *param_out to the parameter string; unquoted empty
133 * values set it to NULL, while quoted empty values become an empty string.
134 */
135static ParameterError extract_param(char *line,
136 bool dashed_option,
137 struct dynbuf *pbuf,
138 const char *filename,
139 int lineno,
140 const char *option,
141 char **param_out)
142{
143 /* pass spaces and separator(s) */
144 while(ISBLANK(*line) || ISSEP(*line, dashed_option))
145 line++;
147 /* the parameter starts here (unless quoted) */
148 if(*line == '\"') {
149 /* quoted parameter, do the quote dance */
150 int rc = unslashquote(++line, pbuf);
151 if(rc)
152 return PARAM_BAD_USE;
153 *param_out = curlx_dyn_len(pbuf) ? curlx_dyn_ptr(pbuf) : CURL_UNCONST("");
154 }
155 else {
156 if(*line == '\'') {
157 warnf("%s:%d Option '%s' uses argument with leading single quote. "
158 "It is probably a mistake. Consider double quotes.",
159 filename, lineno, option);
160 }
161 *param_out = line; /* parameter starts here */
162 while(*line && !ISSPACE(*line)) /* stop also on CRLF */
163 line++;
165 if(*line) {
166 *line = '\0'; /* null-terminate */
168 /* to detect mistakes better, see if there is data following */
169 line++;
170 /* pass all spaces */
171 while(ISBLANK(*line))
172 line++;
174 switch(*line) {
175 case '\0':
176 case '\r':
177 case '\n':
178 case '#': /* comment */
179 break;
180 default:
181 warnf("%s:%d Option '%s' uses argument with unquoted whitespace. "
182 "This may cause side-effects. Consider double quotes.",
183 filename, lineno, option);
184 }
185 }
186 if(!**param_out)
187 /* do this so getparameter can check for required parameters.
188 Otherwise it always thinks there is a parameter. */
189 *param_out = NULL;
190 }
191 return PARAM_OK;
192}
194/*
195 * Process the result from getparameter. Handles PARAM_NEXT_OPERATION
196 * by allocating a new config, and reports errors for other non-OK results.
197 * Updates *configp if a new operation config is allocated.
198 * Returns PARAM_OK if processing should continue, or an error code.
199 */
200static ParameterError process_config_result(ParameterError res,
201 struct OperationConfig **configp,
202 const char *param,
203 bool usedarg,
204 const char *filename,
205 int lineno,
206 const char *option)
207{
208 if(!res && param && *param && !usedarg)
209 /* we passed in a parameter that was not used! */
210 res = PARAM_GOT_EXTRA_PARAMETER;
212 if(res == PARAM_NEXT_OPERATION) {
213 struct OperationConfig *config = *configp;
214 if(config->url_list && config->url_list->url) {
215 /* Allocate the next config */
216 config->next = config_alloc();
217 if(config->next) {
218 /* Update the last operation pointer */
219 global->last = config->next;
221 /* Move onto the new config */
222 config->next->prev = config;
223 *configp = config->next;
224 }
225 else
226 res = PARAM_NO_MEM;
227 }
228 }
230 if(res != PARAM_OK && res != PARAM_NEXT_OPERATION) {
231 const char *display = filename;
232 /* the help request is not really an error */
233 if(!strcmp(filename, "-"))
234 display = "<stdin>";
235 if(res != PARAM_HELP_REQUESTED &&
236 res != PARAM_MANUAL_REQUESTED &&
237 res != PARAM_VERSION_INFO_REQUESTED &&
238 res != PARAM_ENGINES_REQUESTED &&
239 res != PARAM_CA_EMBED_REQUESTED) {
240 const char *reason = param2text(res);
241 errorf("%s:%d config file option '%s' %s",
242 display, lineno, option, reason);
243 if(res == PARAM_OPTION_UNKNOWN)
244 res = PARAM_CONFIG_OPTION_UNKNOWN;
245 return res;
246 }
247 }
248 return PARAM_OK;
249}
251ParameterError parseconfig(const char *filename, int max_recursive,
252 char **resolved)
253{
254 FILE *file = NULL;
255 bool usedarg = FALSE;
256 ParameterError err = PARAM_OK;
257 struct OperationConfig *config = global->last;
258 char *pathalloc = NULL;
260 file = open_config_file(filename, &filename, &pathalloc);
262 if(file) {
263 char *line;
264 char *option;
265 char *param;
266 int lineno = 0;
267 bool dashed_option;
268 struct dynbuf buf;
269 struct dynbuf pbuf;
270 bool fileerror = FALSE;
271 curlx_dyn_init(&buf, MAX_CONFIG_LINE_LENGTH);
272 curlx_dyn_init(&pbuf, MAX_CONFIG_LINE_LENGTH);
273 DEBUGASSERT(filename);
275 while(!err && my_get_line(file, &buf, &fileerror, &lineno)) {
276 ParameterError res;
277 line = curlx_dyn_ptr(&buf);
278 if(!line) {
279 err = PARAM_NO_MEM; /* out of memory */
280 break;
281 }
283 /* skip leading whitespace, ignore lines with nothing else (like a lone
284 CR from a CRLF line ending) */
285 while(ISSPACE(*line))
286 line++;
287 if(!*line)
288 continue;
290 /* the option keywords starts here */
291 option = line;
293 /* the option starts with a dash? */
294 dashed_option = (option[0] == '-');
296 while(*line && !ISSPACE(*line) && !ISSEP(*line, dashed_option))
297 line++;
298 /* ... and has ended here */
300 if(*line)
301 *line++ = '\0'; /* null-terminate, we have a local copy */
303 /* if there is a parameter for this option, extract it */
304 err = extract_param(line, dashed_option, &pbuf, filename, lineno,
305 option, ¶m);
306 if(err)
307 break;
309 res = getparameter(option, param, &usedarg, config, max_recursive);
311 config = global->last;
313 err = process_config_result(res, &config, param, usedarg, filename,
314 lineno, option);
315 }
316 curlx_dyn_free(&buf);
317 curlx_dyn_free(&pbuf);
318 if(file != stdin)
319 curlx_fclose(file);
320 /* Silence false positive about failing to close stdin.
321 NOLINTNEXTLINE(clang-analyzer-unix.Stream) */
322 if(fileerror)
323 err = PARAM_READ_ERROR;
324 }
325 else
326 err = PARAM_READ_ERROR; /* could not open the file */
328 if((err == PARAM_READ_ERROR) && filename)
329 errorf("cannot read config from '%s'", filename);
331 if(!err && resolved) {
332 *resolved = curlx_strdup(filename);
333 if(!*resolved)
334 err = PARAM_NO_MEM;
335 }
336 curlx_free(pathalloc);
337 return err;
338}
340static bool get_line(FILE *input, struct dynbuf *buf, bool *error)
341{
342 CURLcode result;
343 char buffer[128];
344 curlx_dyn_reset(buf);
345 while(1) {
346 const char *b = fgets(buffer, sizeof(buffer), input);
348 if(b) {
349 size_t rlen = strlen(b);
351 if(!rlen)
352 break;
354 result = curlx_dyn_addn(buf, b, rlen);
355 if(result) {
356 /* too long line or out of memory */
357 *error = TRUE;
358 return FALSE; /* error */
359 }
361 else if(b[rlen - 1] == '\n') {
362 /* end of the line, drop the newline */
363 size_t len = curlx_dyn_len(buf);
364 if(len)
365 curlx_dyn_setlen(buf, len - 1);
366 return TRUE; /* all good */
367 }
369 else if(feof(input))
370 return TRUE; /* all good */
371 }
372 else if(curlx_dyn_len(buf))
373 return TRUE; /* all good */
374 else
375 break;
376 }
377 return FALSE;
378}
380/*
381 * Returns a line from the given file. Every line is null-terminated (no
382 * newline). Skips #-commented and space/tabs-only lines automatically.
383 * If lineno is not NULL, it is increased for every line read, including the
384 * skipped ones.
385 */
386bool my_get_line(FILE *input, struct dynbuf *buf, bool *error, int *lineno)
387{
388 bool retcode;
389 do {
390 retcode = get_line(input, buf, error);
391 if(!*error && retcode) {
392 size_t len = curlx_dyn_len(buf);
393 if(lineno)
394 (*lineno)++;
395 if(len) {
396 const char *line = curlx_dyn_ptr(buf);
397 while(ISBLANK(*line))
398 line++;
400 /* a line with # in the first non-blank column is a comment! */
401 if((*line == '#') || !*line)
402 continue;
403 }
404 else
405 continue; /* avoid returning an empty line */
406 }
407 break;
408 } while(retcode);
409 return retcode;
410}
Diagnostics4 git calls · 33 ms
CallStatusSizeTime033 ms
discover200152 B4 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=4 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200149 B · hit4 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=4 git.response.bytes=149 git.response.cache=HIT git.response.duration_ms=4 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=149 git.pkt.payload.max=78 git.ls_refs.count=2 git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=293470 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=10 git.pkt.data=8 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=293470 git.pkt.payload.max=65516 git.sideband.band1.bytes=293354 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=87 git.pack.bytes=293354 git.pack.ofs_delta=0 git.pack.ref_delta=1 git.pack.chain.max=1 git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0 git.pack.full=86Open pack map
fetch2004.3 KiB · hit26 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=26 git.response.bytes=4427 git.response.cache=HIT git.response.duration_ms=26 git.fetch.wants=976581aa66e60f27401fd41308d0de1edf2ba707 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=4427 git.pkt.payload.max=4406 git.sideband.band1.bytes=4405 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
- Ref
- refs/heads/master
- Commit
- 31c911efe5652725e930f4d2e60c9060e5f6646d
- cf-ray
- a5e2b0ea5a16e6b0
- Request
- a3c97bfd…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format