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