Raw
1 #define DISABLE_SIGN_COMPARE_WARNINGS
2
3 #include "git-compat-util.h"
4 #include "gettext.h"
5 #include "hex-ll.h"
6 #include "strbuf.h"
7 #include "urlmatch.h"
8 #include "url.h"
9
10 #define URL_ALPHA "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
11 #define URL_DIGIT "0123456789"
12 #define URL_ALPHADIGIT URL_ALPHA URL_DIGIT
13 #define URL_SCHEME_CHARS URL_ALPHADIGIT "+.-"
14 #define URL_HOST_CHARS URL_ALPHADIGIT ".-_[:]" /* IPv6 literals need [:] */
15 #define URL_UNSAFE_CHARS " <>\"%{}|\\^`" /* plus 0x00-0x1F,0x7F-0xFF */
16 #define URL_GEN_RESERVED ":/?#[]@"
17 #define URL_SUB_RESERVED "!$&'()*+,;="
18 #define URL_RESERVED URL_GEN_RESERVED URL_SUB_RESERVED /* only allowed delims */
19
20 static int append_normalized_escapes(struct strbuf *buf,
21 const char *from,
22 size_t from_len,
23 const char *esc_extra,
24 const char *esc_ok)
25 {
26 /*
27 * Append to strbuf 'buf' characters from string 'from' with length
28 * 'from_len' while unescaping characters that do not need to be escaped
29 * and escaping characters that do. The set of characters to escape
30 * (the complement of which is unescaped) starts out as the RFC 3986
31 * unsafe characters (0x00-0x1F,0x7F-0xFF," <>\"#%{}|\\^`"). If
32 * 'esc_extra' is not NULL, those additional characters will also always
33 * be escaped. If 'esc_ok' is not NULL, those characters will be left
34 * escaped if found that way, but will not be unescaped otherwise (used
35 * for delimiters). If a %-escape sequence is encountered that is not
36 * followed by 2 hexadecimal digits, the sequence is invalid and
37 * false (0) will be returned. Otherwise true (1) will be returned for
38 * success.
39 *
40 * Note that all %-escape sequences will be normalized to UPPERCASE
41 * as indicated in RFC 3986. Unless included in esc_extra or esc_ok
42 * alphanumerics and "-._~" will always be unescaped as per RFC 3986.
43 */
44
45 while (from_len) {
46 int ch = *from++;
47 int was_esc = 0;
48
49 from_len--;
50 if (ch == '%') {
51 if (from_len < 2)
52 return 0;
53 ch = hex2chr(from);
54 if (ch < 0)
55 return 0;
56 from += 2;
57 from_len -= 2;
58 was_esc = 1;
59 }
60 if ((unsigned char)ch <= 0x1F || (unsigned char)ch >= 0x7F ||
61 strchr(URL_UNSAFE_CHARS, ch) ||
62 (esc_extra && strchr(esc_extra, ch)) ||
63 (was_esc && strchr(esc_ok, ch)))
64 strbuf_addf(buf, "%%%02X", (unsigned char)ch);
65 else
66 strbuf_addch(buf, ch);
67 }
68
69 return 1;
70 }
71
72 static const char *end_of_token(const char *s, int c, size_t n)
73 {
74 const char *next = memchr(s, c, n);
75 if (!next)
76 next = s + n;
77 return next;
78 }
79
80 static int match_host(const struct url_info *url_info,
81 const struct url_info *pattern_info)
82 {
83 const char *url = url_info->url + url_info->host_off;
84 const char *pat = pattern_info->url + pattern_info->host_off;
85 int url_len = url_info->host_len;
86 int pat_len = pattern_info->host_len;
87
88 while (url_len && pat_len) {
89 const char *url_next = end_of_token(url, '.', url_len);
90 const char *pat_next = end_of_token(pat, '.', pat_len);
91
92 if (pat_next == pat + 1 && pat[0] == '*')
93 /* wildcard matches anything */
94 ;
95 else if ((pat_next - pat) == (url_next - url) &&
96 !memcmp(url, pat, url_next - url))
97 /* the components are the same */
98 ;
99 else
100 return 0; /* found an unmatch */
101
102 if (url_next < url + url_len)
103 url_next++;
104 url_len -= url_next - url;
105 url = url_next;
106 if (pat_next < pat + pat_len)
107 pat_next++;
108 pat_len -= pat_next - pat;
109 pat = pat_next;
110 }
111
112 return (!url_len && !pat_len);
113 }
114
115 static char *url_normalize_1(const char *url, struct url_info *out_info, char allow_globs)
116 {
117 /*
118 * Normalize NUL-terminated url using the following rules:
119 *
120 * 1. Case-insensitive parts of url will be converted to lower case
121 * 2. %-encoded characters that do not need to be will be unencoded
122 * 3. Characters that are not %-encoded and must be will be encoded
123 * 4. All %-encodings will be converted to upper case hexadecimal
124 * 5. Leading 0s are removed from port numbers
125 * 6. If the default port for the scheme is given it will be removed
126 * 7. A path part (including empty) not starting with '/' has one added
127 * 8. Any dot segments (. or ..) in the path are resolved and removed
128 * 9. IPv6 host literals are allowed (but not normalized or validated)
129 *
130 * The rules are based on information in RFC 3986.
131 *
132 * Please note this function requires a full URL including a scheme
133 * and host part (except for file: URLs which may have an empty host).
134 *
135 * The return value is a newly allocated string that must be freed
136 * or NULL if the url is not valid.
137 *
138 * If out_info is non-NULL, the url and err fields therein will always
139 * be set. If a non-NULL value is returned, it will be stored in
140 * out_info->url as well, out_info->err will be set to NULL and the
141 * other fields of *out_info will also be filled in. If a NULL value
142 * is returned, NULL will be stored in out_info->url and out_info->err
143 * will be set to a brief, translated, error message, but no other
144 * fields will be filled in.
145 *
146 * This is NOT a URL validation function. Full URL validation is NOT
147 * performed. Some invalid host names are passed through this function
148 * undetected. However, most all other problems that make a URL invalid
149 * will be detected (including a missing host for non file: URLs).
150 */
151
152 size_t url_len = strlen(url);
153 struct strbuf norm;
154 size_t spanned;
155 size_t scheme_len, user_off=0, user_len=0, passwd_off=0, passwd_len=0;
156 size_t host_off=0, host_len=0, port_off=0, port_len=0, path_off, path_len, result_len;
157 const char *slash_ptr, *at_ptr, *colon_ptr, *path_start;
158 char *result;
159
160 /*
161 * Copy lowercased scheme and :// suffix, %-escapes are not allowed
162 * First character of scheme must be URL_ALPHA
163 */
164 spanned = strspn(url, URL_SCHEME_CHARS);
165 if (!spanned || !isalpha(url[0]) || spanned + 3 > url_len ||
166 url[spanned] != ':' || url[spanned+1] != '/' || url[spanned+2] != '/') {
167 if (out_info) {
168 out_info->url = NULL;
169 out_info->err = _("invalid URL scheme name or missing '://' suffix");
170 }
171 return NULL; /* Bad scheme and/or missing "://" part */
172 }
173 strbuf_init(&norm, url_len);
174 scheme_len = spanned;
175 spanned += 3;
176 url_len -= spanned;
177 while (spanned--)
178 strbuf_addch(&norm, tolower(*url++));
179
180
181 /*
182 * Copy any username:password if present normalizing %-escapes
183 */
184 at_ptr = strchr(url, '@');
185 slash_ptr = url + strcspn(url, "/?#");
186 if (at_ptr && at_ptr < slash_ptr) {
187 user_off = norm.len;
188 if (at_ptr > url) {
189 if (!append_normalized_escapes(&norm, url, at_ptr - url,
190 "", URL_RESERVED)) {
191 if (out_info) {
192 out_info->url = NULL;
193 out_info->err = _("invalid %XX escape sequence");
194 }
195 strbuf_release(&norm);
196 return NULL;
197 }
198 colon_ptr = strchr(norm.buf + scheme_len + 3, ':');
199 if (colon_ptr) {
200 passwd_off = (colon_ptr + 1) - norm.buf;
201 passwd_len = norm.len - passwd_off;
202 user_len = (passwd_off - 1) - (scheme_len + 3);
203 } else {
204 user_len = norm.len - (scheme_len + 3);
205 }
206 }
207 strbuf_addch(&norm, '@');
208 url_len -= (++at_ptr - url);
209 url = at_ptr;
210 }
211
212
213 /*
214 * Copy the host part excluding any port part, no %-escapes allowed
215 */
216 if (!url_len || strchr(":/?#", *url)) {
217 /* Missing host invalid for all URL schemes except file */
218 if (!starts_with(norm.buf, "file:")) {
219 if (out_info) {
220 out_info->url = NULL;
221 out_info->err = _("missing host and scheme is not 'file:'");
222 }
223 strbuf_release(&norm);
224 return NULL;
225 }
226 } else {
227 host_off = norm.len;
228 }
229 colon_ptr = slash_ptr - 1;
230 while (colon_ptr > url && *colon_ptr != ':' && *colon_ptr != ']')
231 colon_ptr--;
232 if (*colon_ptr != ':') {
233 colon_ptr = slash_ptr;
234 } else if (!host_off && colon_ptr < slash_ptr && colon_ptr + 1 != slash_ptr) {
235 /* file: URLs may not have a port number */
236 if (out_info) {
237 out_info->url = NULL;
238 out_info->err = _("a 'file:' URL may not have a port number");
239 }
240 strbuf_release(&norm);
241 return NULL;
242 }
243
244 if (allow_globs)
245 spanned = strspn(url, URL_HOST_CHARS "*");
246 else
247 spanned = strspn(url, URL_HOST_CHARS);
248
249 if (spanned < colon_ptr - url) {
250 /* Host name has invalid characters */
251 if (out_info) {
252 out_info->url = NULL;
253 out_info->err = _("invalid characters in host name");
254 }
255 strbuf_release(&norm);
256 return NULL;
257 }
258 while (url < colon_ptr) {
259 strbuf_addch(&norm, tolower(*url++));
260 url_len--;
261 }
262
263
264 /*
265 * Check the port part and copy if not the default (after removing any
266 * leading 0s); no %-escapes allowed
267 */
268 if (colon_ptr < slash_ptr) {
269 /* skip the ':' and leading 0s but not the last one if all 0s */
270 url++;
271 url += strspn(url, "0");
272 if (url == slash_ptr && url[-1] == '0')
273 url--;
274 if (url == slash_ptr) {
275 /* Skip ":" port with no number, it's same as default */
276 } else if (slash_ptr - url == 2 &&
277 starts_with(norm.buf, "http:") &&
278 !strncmp(url, "80", 2)) {
279 /* Skip http :80 as it's the default */
280 } else if (slash_ptr - url == 3 &&
281 starts_with(norm.buf, "https:") &&
282 !strncmp(url, "443", 3)) {
283 /* Skip https :443 as it's the default */
284 } else {
285 /*
286 * Port number must be all digits with leading 0s removed
287 * and since all the protocols we deal with have a 16-bit
288 * port number it must also be in the range 1..65535
289 * 0 is not allowed because that means "next available"
290 * on just about every system and therefore cannot be used
291 */
292 unsigned long pnum = 0;
293 spanned = strspn(url, URL_DIGIT);
294 if (spanned < slash_ptr - url) {
295 /* port number has invalid characters */
296 if (out_info) {
297 out_info->url = NULL;
298 out_info->err = _("invalid port number");
299 }
300 strbuf_release(&norm);
301 return NULL;
302 }
303 if (slash_ptr - url <= 5)
304 pnum = strtoul(url, NULL, 10);
305 if (pnum == 0 || pnum > 65535) {
306 /* port number not in range 1..65535 */
307 if (out_info) {
308 out_info->url = NULL;
309 out_info->err = _("invalid port number");
310 }
311 strbuf_release(&norm);
312 return NULL;
313 }
314 strbuf_addch(&norm, ':');
315 port_off = norm.len;
316 strbuf_add(&norm, url, slash_ptr - url);
317 port_len = slash_ptr - url;
318 }
319 url_len -= slash_ptr - colon_ptr;
320 url = slash_ptr;
321 }
322 if (host_off)
323 host_len = norm.len - host_off - (port_len ? port_len + 1 : 0);
324
325
326 /*
327 * Now copy the path resolving any . and .. segments being careful not
328 * to corrupt the URL by unescaping any delimiters, but do add an
329 * initial '/' if it's missing and do normalize any %-escape sequences.
330 */
331 path_off = norm.len;
332 path_start = norm.buf + path_off;
333 strbuf_addch(&norm, '/');
334 if (*url == '/') {
335 url++;
336 url_len--;
337 }
338 for (;;) {
339 const char *seg_start;
340 size_t seg_start_off = norm.len;
341 const char *next_slash = url + strcspn(url, "/?#");
342 int skip_add_slash = 0;
343
344 /*
345 * RFC 3689 indicates that any . or .. segments should be
346 * unescaped before being checked for.
347 */
348 if (!append_normalized_escapes(&norm, url, next_slash - url, "",
349 URL_RESERVED)) {
350 if (out_info) {
351 out_info->url = NULL;
352 out_info->err = _("invalid %XX escape sequence");
353 }
354 strbuf_release(&norm);
355 return NULL;
356 }
357
358 seg_start = norm.buf + seg_start_off;
359 if (!strcmp(seg_start, ".")) {
360 /* ignore a . segment; be careful not to remove initial '/' */
361 if (seg_start == path_start + 1) {
362 strbuf_setlen(&norm, norm.len - 1);
363 skip_add_slash = 1;
364 } else {
365 strbuf_setlen(&norm, norm.len - 2);
366 }
367 } else if (!strcmp(seg_start, "..")) {
368 /*
369 * ignore a .. segment and remove the previous segment;
370 * be careful not to remove initial '/' from path
371 */
372 const char *prev_slash = norm.buf + norm.len - 3;
373 if (prev_slash == path_start) {
374 /* invalid .. because no previous segment to remove */
375 if (out_info) {
376 out_info->url = NULL;
377 out_info->err = _("invalid '..' path segment");
378 }
379 strbuf_release(&norm);
380 return NULL;
381 }
382 while (*--prev_slash != '/') {}
383 if (prev_slash == path_start) {
384 strbuf_setlen(&norm, prev_slash - norm.buf + 1);
385 skip_add_slash = 1;
386 } else {
387 strbuf_setlen(&norm, prev_slash - norm.buf);
388 }
389 }
390 url_len -= next_slash - url;
391 url = next_slash;
392 /* if the next char is not '/' done with the path */
393 if (*url != '/')
394 break;
395 url++;
396 url_len--;
397 if (!skip_add_slash)
398 strbuf_addch(&norm, '/');
399 }
400 path_len = norm.len - path_off;
401
402
403 /*
404 * Now simply copy the rest, if any, only normalizing %-escapes and
405 * being careful not to corrupt the URL by unescaping any delimiters.
406 */
407 if (*url) {
408 if (!append_normalized_escapes(&norm, url, url_len, "", URL_RESERVED)) {
409 if (out_info) {
410 out_info->url = NULL;
411 out_info->err = _("invalid %XX escape sequence");
412 }
413 strbuf_release(&norm);
414 return NULL;
415 }
416 }
417
418
419 result = strbuf_detach(&norm, &result_len);
420 if (out_info) {
421 out_info->url = result;
422 out_info->err = NULL;
423 out_info->url_len = result_len;
424 out_info->scheme_len = scheme_len;
425 out_info->user_off = user_off;
426 out_info->user_len = user_len;
427 out_info->passwd_off = passwd_off;
428 out_info->passwd_len = passwd_len;
429 out_info->host_off = host_off;
430 out_info->host_len = host_len;
431 out_info->port_off = port_off;
432 out_info->port_len = port_len;
433 out_info->path_off = path_off;
434 out_info->path_len = path_len;
435 }
436 return result;
437 }
438
439 char *url_normalize(const char *url, struct url_info *out_info)
440 {
441 return url_normalize_1(url, out_info, 0);
442 }
443
444 char *url_parse(const char *url_orig, struct url_info *out_info)
445 {
446 struct strbuf url;
447 char *host, *separator;
448 char *detached, *normalized;
449 char *url_decoded;
450 enum url_scheme scheme = URL_SCHEME_LOCAL;
451 struct url_info local_info;
452 struct url_info *info = out_info ? out_info : &local_info;
453 bool scp_syntax = false;
454
455 if (is_url(url_orig))
456 url_decoded = url_decode(url_orig);
457 else
458 url_decoded = xstrdup(url_orig);
459
460 strbuf_init(&url, strlen(url_decoded) + sizeof("ssh://"));
461 strbuf_addstr(&url, url_decoded);
462 free(url_decoded);
463
464 host = strstr(url.buf, "://");
465 if (host) {
466 /*
467 * Temporarily NUL-terminate the scheme name
468 * so we can pass it to url_get_scheme(),
469 * then restore the ':' so the buffer
470 * is intact for url_normalize() below.
471 */
472 char saved = *host;
473 *host = '\0';
474 scheme = url_get_scheme(url.buf);
475 *host = saved;
476 host += 3;
477 } else {
478 if (!url_is_local_not_ssh(url.buf)) {
479 scp_syntax = true;
480 scheme = URL_SCHEME_SSH;
481 strbuf_insertstr(&url, 0, "ssh://");
482 host = url.buf + strlen("ssh://");
483 }
484 }
485
486 /*
487 * Path starts after ':' in scp style SSH URLs.
488 *
489 * The host portion can begin with an optional "user@",
490 * and the host itself can be wrapped in '[' ']' brackets.
491 * The bracket form is git's legacy way of supporting:
492 *
493 * - IPv6 literals: [::1]:repo
494 * - host:port pairs in the short form: [myhost:123]:src
495 * - Plain hostnames that happen to need bracketing: [host]:path
496 *
497 * Treat '[' followed by 0 or 1 inner colons as the host:port
498 * or plain hostname form and strip the brackets so url_normalize
499 * sees host[:port] natively. Two or more inner colons mark an
500 * IPv6 literal: keep the brackets for url_normalize to recognize.
501 *
502 * The scp path separator is the ':' that follows the host part,
503 * and we must skip over user@ and any '[...]' before searching.
504 */
505 if (scp_syntax) {
506 char *user_at;
507 char *host_start;
508 char *bracket_end;
509
510 user_at = strchr(host, '@');
511 host_start = user_at ? user_at + 1 : host;
512
513 if (*host_start == '[') {
514 char *p;
515 int inner_colons;
516
517 bracket_end = strchr(host_start, ']');
518 inner_colons = 0;
519 for (p = host_start + 1; bracket_end && p < bracket_end; p++)
520 if (*p == ':')
521 inner_colons++;
522
523 if (bracket_end && inner_colons <= 1) {
524 size_t close_off = bracket_end - url.buf;
525 size_t open_off = host_start - url.buf;
526 strbuf_remove(&url, close_off, 1);
527 strbuf_remove(&url, open_off, 1);
528 separator = url.buf + close_off - 1;
529 } else if (bracket_end) {
530 separator = strchr(bracket_end + 1, ':');
531 } else {
532 separator = strchr(host_start, ':');
533 }
534 } else {
535 separator = strchr(host_start, ':');
536 }
537
538 if (separator) {
539 if (separator[1] == '/')
540 strbuf_remove(&url, separator - url.buf, 1);
541 else
542 *separator = '/';
543 }
544 }
545
546 detached = strbuf_detach(&url, NULL);
547 normalized = url_normalize(detached, info);
548 free(detached);
549
550 if (!normalized)
551 return NULL;
552
553 /*
554 * Point path to ~ for URLs like this:
555 *
556 * ssh://host.xz/~user/repo
557 * git://host.xz/~user/repo
558 * host.xz:~user/repo
559 */
560 if (scheme == URL_SCHEME_GIT || scheme == URL_SCHEME_SSH) {
561 if (normalized[info->path_off + 1] == '~') {
562 info->path_off++;
563 info->path_len--;
564 }
565 }
566
567 return normalized;
568 }
569
570 static size_t url_match_prefix(const char *url,
571 const char *url_prefix,
572 size_t url_prefix_len)
573 {
574 /*
575 * url_prefix matches url if url_prefix is an exact match for url or it
576 * is a prefix of url and the match ends on a path component boundary.
577 * Both url and url_prefix are considered to have an implicit '/' on the
578 * end for matching purposes if they do not already.
579 *
580 * url must be NUL terminated. url_prefix_len is the length of
581 * url_prefix which need not be NUL terminated.
582 *
583 * The return value is the length of the match in characters (including
584 * the final '/' even if it's implicit) or 0 for no match.
585 *
586 * Passing NULL as url and/or url_prefix will always cause 0 to be
587 * returned without causing any faults.
588 */
589 if (!url || !url_prefix)
590 return 0;
591 if (!url_prefix_len || (url_prefix_len == 1 && *url_prefix == '/'))
592 return (!*url || *url == '/') ? 1 : 0;
593 if (url_prefix[url_prefix_len - 1] == '/')
594 url_prefix_len--;
595 if (strncmp(url, url_prefix, url_prefix_len))
596 return 0;
597 if ((strlen(url) == url_prefix_len) || (url[url_prefix_len] == '/'))
598 return url_prefix_len + 1;
599 return 0;
600 }
601
602 static int match_urls(const struct url_info *url,
603 const struct url_info *url_prefix,
604 struct urlmatch_item *match)
605 {
606 /*
607 * url_prefix matches url if the scheme, host and port of url_prefix
608 * are the same as those of url and the path portion of url_prefix
609 * is the same as the path portion of url or it is a prefix that
610 * matches at a '/' boundary. If url_prefix contains a user name,
611 * that must also exactly match the user name in url.
612 *
613 * If the user, host, port and path match in this fashion, the returned
614 * value is the length of the path match including any implicit
615 * final '/'. For example, "http://me@example.com/path" is matched by
616 * "http://example.com" with a path length of 1.
617 *
618 * If there is a match and exactusermatch is not NULL, then
619 * *exactusermatch will be set to true if both url and url_prefix
620 * contained a user name or false if url_prefix did not have a
621 * user name. If there is no match *exactusermatch is left untouched.
622 */
623 char usermatched = 0;
624 size_t pathmatchlen;
625
626 if (!url || !url_prefix || !url->url || !url_prefix->url)
627 return 0;
628
629 /* check the scheme */
630 if (url_prefix->scheme_len != url->scheme_len ||
631 strncmp(url->url, url_prefix->url, url->scheme_len))
632 return 0; /* schemes do not match */
633
634 /* check the user name if url_prefix has one */
635 if (url_prefix->user_off) {
636 if (!url->user_off || url->user_len != url_prefix->user_len ||
637 strncmp(url->url + url->user_off,
638 url_prefix->url + url_prefix->user_off,
639 url->user_len))
640 return 0; /* url_prefix has a user but it's not a match */
641 usermatched = 1;
642 }
643
644 /* check the host */
645 if (!match_host(url, url_prefix))
646 return 0; /* host names do not match */
647
648 /* check the port */
649 if (url_prefix->port_len != url->port_len ||
650 strncmp(url->url + url->port_off,
651 url_prefix->url + url_prefix->port_off, url->port_len))
652 return 0; /* ports do not match */
653
654 /* check the path */
655 pathmatchlen = url_match_prefix(
656 url->url + url->path_off,
657 url_prefix->url + url_prefix->path_off,
658 url_prefix->url_len - url_prefix->path_off);
659 if (!pathmatchlen)
660 return 0; /* paths do not match */
661
662 if (match) {
663 match->hostmatch_len = url_prefix->host_len;
664 match->pathmatch_len = pathmatchlen;
665 match->user_matched = usermatched;
666 }
667
668 return 1;
669 }
670
671 static int cmp_matches(const struct urlmatch_item *a,
672 const struct urlmatch_item *b)
673 {
674 if (a->hostmatch_len != b->hostmatch_len)
675 return a->hostmatch_len < b->hostmatch_len ? -1 : 1;
676 if (a->pathmatch_len != b->pathmatch_len)
677 return a->pathmatch_len < b->pathmatch_len ? -1 : 1;
678 if (a->user_matched != b->user_matched)
679 return b->user_matched ? -1 : 1;
680 return 0;
681 }
682
683 int urlmatch_config_entry(const char *var, const char *value,
684 const struct config_context *ctx, void *cb)
685 {
686 struct string_list_item *item;
687 struct urlmatch_config *collect = cb;
688 struct urlmatch_item matched = {0};
689 struct url_info *url = &collect->url;
690 const char *key, *dot;
691 struct strbuf synthkey = STRBUF_INIT;
692 int retval;
693 int (*select_fn)(const struct urlmatch_item *a, const struct urlmatch_item *b) =
694 collect->select_fn ? collect->select_fn : cmp_matches;
695
696 if (!skip_prefix(var, collect->section, &key) || *(key++) != '.') {
697 if (collect->cascade_fn)
698 return collect->cascade_fn(var, value, ctx, cb);
699 return 0; /* not interested */
700 }
701 dot = strrchr(key, '.');
702 if (dot) {
703 char *config_url, *norm_url;
704 struct url_info norm_info;
705
706 config_url = xmemdupz(key, dot - key);
707 norm_url = url_normalize_1(config_url, &norm_info, 1);
708 if (norm_url)
709 retval = match_urls(url, &norm_info, &matched);
710 else if (collect->fallback_match_fn)
711 retval = collect->fallback_match_fn(config_url,
712 collect->cb);
713 else
714 retval = 0;
715 free(config_url);
716 free(norm_url);
717 if (!retval)
718 return 0;
719 key = dot + 1;
720 }
721
722 if (collect->key && strcmp(key, collect->key))
723 return 0;
724
725 item = string_list_insert(&collect->vars, key);
726 if (!item->util) {
727 item->util = xcalloc(1, sizeof(matched));
728 } else {
729 if (select_fn(&matched, item->util) < 0)
730 /*
731 * Our match is worse than the old one,
732 * we cannot use it.
733 */
734 return 0;
735 /* Otherwise, replace it with this one. */
736 }
737
738 memcpy(item->util, &matched, sizeof(matched));
739 strbuf_addstr(&synthkey, collect->section);
740 strbuf_addch(&synthkey, '.');
741 strbuf_addstr(&synthkey, key);
742 retval = collect->collect_fn(synthkey.buf, value, ctx, collect->cb);
743
744 strbuf_release(&synthkey);
745 return retval;
746 }
747
748 void urlmatch_config_release(struct urlmatch_config *config)
749 {
750 string_list_clear(&config->vars, 1);
751 }