|
5 | 5 |
|
6 | 6 | import { URI } from '../../../../base/common/uri.js';
|
7 | 7 |
|
8 |
| -// TODO: rewrite this to use URIs directly and validate each part individually |
9 |
| -// instead of relying on memoization of the stringified URI. |
10 |
| -export const testUrlMatchesGlob = (uri: URI, globUrl: string): boolean => { |
11 |
| - let url = uri.with({ query: null, fragment: null }).toString(true); |
12 |
| - const normalize = (url: string) => url.replace(/\/+$/, ''); |
13 |
| - globUrl = normalize(globUrl); |
14 |
| - url = normalize(url); |
15 |
| - |
16 |
| - const memo = Array.from({ length: url.length + 1 }).map(() => |
17 |
| - Array.from({ length: globUrl.length + 1 }).map(() => undefined), |
18 |
| - ); |
| 8 | +/** |
| 9 | + * Normalizes a URL by removing trailing slashes and query/fragment components. |
| 10 | + * @param url The URL to normalize. |
| 11 | + * @returns URI - The normalized URI object. |
| 12 | + */ |
| 13 | +function normalizeURL(url: string | URI): URI { |
| 14 | + const uri = typeof url === 'string' ? URI.parse(url) : url; |
| 15 | + return uri.with({ |
| 16 | + // Remove trailing slashes |
| 17 | + path: uri.path.replace(/\/+$/, ''), |
| 18 | + // Remove query and fragment |
| 19 | + query: null, |
| 20 | + fragment: null, |
| 21 | + }); |
| 22 | +} |
19 | 23 |
|
20 |
| - if (/^[^./:]*:\/\//.test(globUrl)) { |
21 |
| - return doUrlMatch(memo, url, globUrl, 0, 0); |
22 |
| - } |
| 24 | +/** |
| 25 | + * Checks if a given URL matches a glob URL pattern. |
| 26 | + * The glob URL pattern can contain wildcards (*) and subdomain matching (*.) |
| 27 | + * @param uri The URL to check. |
| 28 | + * @param globUrl The glob URL pattern to match against. |
| 29 | + * @returns boolean - True if the URL matches the glob URL pattern, false otherwise. |
| 30 | + */ |
| 31 | +export function testUrlMatchesGlob(uri: string | URI, globUrl: string): boolean { |
| 32 | + const normalizedUrl = normalizeURL(uri); |
| 33 | + let normalizedGlobUrl = normalizeURL(globUrl); |
23 | 34 |
|
24 |
| - const scheme = /^(https?):\/\//.exec(url)?.[1]; |
25 |
| - if (scheme) { |
26 |
| - return doUrlMatch(memo, url, `${scheme}://${globUrl}`, 0, 0); |
| 35 | + const globHasScheme = /^[^./:]*:\/\//.test(globUrl); |
| 36 | + // if the glob does not have a scheme we assume the scheme is http or https |
| 37 | + // so if the url doesn't have a scheme of http or https we return false |
| 38 | + if (!globHasScheme) { |
| 39 | + if (normalizedUrl.scheme !== 'http' && normalizedUrl.scheme !== 'https') { |
| 40 | + return false; |
| 41 | + } |
| 42 | + normalizedGlobUrl = normalizeURL(`${normalizedUrl.scheme}://${globUrl}`); |
27 | 43 | }
|
28 | 44 |
|
29 |
| - return false; |
30 |
| -}; |
| 45 | + return ( |
| 46 | + doMemoUrlMatch(normalizedUrl.scheme, normalizedGlobUrl.scheme) && |
| 47 | + // The authority is the only thing that should do port logic. |
| 48 | + doMemoUrlMatch(normalizedUrl.authority, normalizedGlobUrl.authority, true) && |
| 49 | + ( |
| 50 | + // |
| 51 | + normalizedGlobUrl.path === '/' || |
| 52 | + doMemoUrlMatch(normalizedUrl.path, normalizedGlobUrl.path) |
| 53 | + ) |
| 54 | + ); |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * @param normalizedUrlPart The normalized URL part to match. |
| 59 | + * @param normalizedGlobUrlPart The normalized glob URL part to match against. |
| 60 | + * @param includePortLogic Whether to include port logic in the matching process. |
| 61 | + * @returns boolean - True if the URL part matches the glob URL part, false otherwise. |
| 62 | + */ |
| 63 | +function doMemoUrlMatch( |
| 64 | + normalizedUrlPart: string, |
| 65 | + normalizedGlobUrlPart: string, |
| 66 | + includePortLogic: boolean = false, |
| 67 | +) { |
| 68 | + const memo = Array.from({ length: normalizedUrlPart.length + 1 }).map(() => |
| 69 | + Array.from({ length: normalizedGlobUrlPart.length + 1 }).map(() => undefined), |
| 70 | + ); |
31 | 71 |
|
32 |
| -const doUrlMatch = ( |
| 72 | + return doUrlPartMatch(memo, includePortLogic, normalizedUrlPart, normalizedGlobUrlPart, 0, 0); |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Recursively checks if a URL part matches a glob URL part. |
| 77 | + * This function uses memoization to avoid recomputing results for the same inputs. |
| 78 | + * It handles various cases such as exact matches, wildcard matches, and port logic. |
| 79 | + * @param memo A memoization table to avoid recomputing results for the same inputs. |
| 80 | + * @param includePortLogic Whether to include port logic in the matching process. |
| 81 | + * @param urlPart The URL part to match with. |
| 82 | + * @param globUrlPart The glob URL part to match against. |
| 83 | + * @param urlOffset The current offset in the URL part. |
| 84 | + * @param globUrlOffset The current offset in the glob URL part. |
| 85 | + * @returns boolean - True if the URL part matches the glob URL part, false otherwise. |
| 86 | + */ |
| 87 | +function doUrlPartMatch( |
33 | 88 | memo: (boolean | undefined)[][],
|
34 |
| - url: string, |
35 |
| - globUrl: string, |
| 89 | + includePortLogic: boolean, |
| 90 | + urlPart: string, |
| 91 | + globUrlPart: string, |
36 | 92 | urlOffset: number,
|
37 |
| - globUrlOffset: number, |
38 |
| -): boolean => { |
| 93 | + globUrlOffset: number |
| 94 | +): boolean { |
39 | 95 | if (memo[urlOffset]?.[globUrlOffset] !== undefined) {
|
40 | 96 | return memo[urlOffset][globUrlOffset]!;
|
41 | 97 | }
|
42 | 98 |
|
43 | 99 | const options = [];
|
44 | 100 |
|
45 |
| - // Endgame. |
46 |
| - // Fully exact match |
47 |
| - if (urlOffset === url.length) { |
48 |
| - return globUrlOffset === globUrl.length; |
| 101 | + // We've reached the end of the url. |
| 102 | + if (urlOffset === urlPart.length) { |
| 103 | + // We're also at the end of the glob url as well so we have an exact match. |
| 104 | + if (globUrlOffset === globUrlPart.length) { |
| 105 | + return true; |
| 106 | + } |
| 107 | + |
| 108 | + if (includePortLogic && globUrlPart[globUrlOffset] + globUrlPart[globUrlOffset + 1] === ':*') { |
| 109 | + // any port match. Consume a port if it exists otherwise nothing. Always consume the base. |
| 110 | + return globUrlOffset + 2 === globUrlPart.length; |
| 111 | + } |
| 112 | + |
| 113 | + return false; |
49 | 114 | }
|
50 | 115 |
|
51 | 116 | // Some path remaining in url
|
52 |
| - if (globUrlOffset === globUrl.length) { |
53 |
| - const remaining = url.slice(urlOffset); |
| 117 | + if (globUrlOffset === globUrlPart.length) { |
| 118 | + const remaining = urlPart.slice(urlOffset); |
54 | 119 | return remaining[0] === '/';
|
55 | 120 | }
|
56 | 121 |
|
57 |
| - if (url[urlOffset] === globUrl[globUrlOffset]) { |
| 122 | + if (urlPart[urlOffset] === globUrlPart[globUrlOffset]) { |
58 | 123 | // Exact match.
|
59 |
| - options.push(doUrlMatch(memo, url, globUrl, urlOffset + 1, globUrlOffset + 1)); |
| 124 | + options.push(doUrlPartMatch(memo, includePortLogic, urlPart, globUrlPart, urlOffset + 1, globUrlOffset + 1)); |
60 | 125 | }
|
61 | 126 |
|
62 |
| - if (globUrl[globUrlOffset] + globUrl[globUrlOffset + 1] === '*.') { |
| 127 | + if (globUrlPart[globUrlOffset] + globUrlPart[globUrlOffset + 1] === '*.') { |
63 | 128 | // Any subdomain match. Either consume one thing that's not a / or : and don't advance base or consume nothing and do.
|
64 |
| - if (!['/', ':'].includes(url[urlOffset])) { |
65 |
| - options.push(doUrlMatch(memo, url, globUrl, urlOffset + 1, globUrlOffset)); |
| 129 | + if (!['/', ':'].includes(urlPart[urlOffset])) { |
| 130 | + options.push(doUrlPartMatch(memo, includePortLogic, urlPart, globUrlPart, urlOffset + 1, globUrlOffset)); |
66 | 131 | }
|
67 |
| - options.push(doUrlMatch(memo, url, globUrl, urlOffset, globUrlOffset + 2)); |
| 132 | + options.push(doUrlPartMatch(memo, includePortLogic, urlPart, globUrlPart, urlOffset, globUrlOffset + 2)); |
68 | 133 | }
|
69 | 134 |
|
70 |
| - if (globUrl[globUrlOffset] === '*') { |
| 135 | + if (globUrlPart[globUrlOffset] === '*') { |
71 | 136 | // Any match. Either consume one thing and don't advance base or consume nothing and do.
|
72 |
| - if (urlOffset + 1 === url.length) { |
| 137 | + if (urlOffset + 1 === urlPart.length) { |
73 | 138 | // If we're at the end of the input url consume one from both.
|
74 |
| - options.push(doUrlMatch(memo, url, globUrl, urlOffset + 1, globUrlOffset + 1)); |
| 139 | + options.push(doUrlPartMatch(memo, includePortLogic, urlPart, globUrlPart, urlOffset + 1, globUrlOffset + 1)); |
75 | 140 | } else {
|
76 |
| - options.push(doUrlMatch(memo, url, globUrl, urlOffset + 1, globUrlOffset)); |
| 141 | + options.push(doUrlPartMatch(memo, includePortLogic, urlPart, globUrlPart, urlOffset + 1, globUrlOffset)); |
77 | 142 | }
|
78 |
| - options.push(doUrlMatch(memo, url, globUrl, urlOffset, globUrlOffset + 1)); |
| 143 | + options.push(doUrlPartMatch(memo, includePortLogic, urlPart, globUrlPart, urlOffset, globUrlOffset + 1)); |
79 | 144 | }
|
80 | 145 |
|
81 |
| - if (globUrl[globUrlOffset] + globUrl[globUrlOffset + 1] === ':*') { |
82 |
| - // any port match. Consume a port if it exists otherwise nothing. Always comsume the base. |
83 |
| - if (url[urlOffset] === ':') { |
| 146 | + if (includePortLogic && globUrlPart[globUrlOffset] + globUrlPart[globUrlOffset + 1] === ':*') { |
| 147 | + // any port match. Consume a port if it exists otherwise nothing. Always consume the base. |
| 148 | + if (urlPart[urlOffset] === ':') { |
84 | 149 | let endPortIndex = urlOffset + 1;
|
85 |
| - do { endPortIndex++; } while (/[0-9]/.test(url[endPortIndex])); |
86 |
| - options.push(doUrlMatch(memo, url, globUrl, endPortIndex, globUrlOffset + 2)); |
| 150 | + do { endPortIndex++; } while (/[0-9]/.test(urlPart[endPortIndex])); |
| 151 | + options.push(doUrlPartMatch(memo, includePortLogic, urlPart, globUrlPart, endPortIndex, globUrlOffset + 2)); |
87 | 152 | } else {
|
88 |
| - options.push(doUrlMatch(memo, url, globUrl, urlOffset, globUrlOffset + 2)); |
| 153 | + options.push(doUrlPartMatch(memo, includePortLogic, urlPart, globUrlPart, urlOffset, globUrlOffset + 2)); |
89 | 154 | }
|
90 | 155 | }
|
91 | 156 |
|
92 | 157 | return (memo[urlOffset][globUrlOffset] = options.some(a => a === true));
|
93 |
| -}; |
| 158 | +} |
0 commit comments