Revision 5c583de8
Added by koszko almost 2 years ago
| common/patterns.js | ||
|---|---|---|
| 17 | 17 |
const proto_regex = /^(\w+):\/\/(.*)$/; |
| 18 | 18 |
|
| 19 | 19 |
const user_re = "[^/?#@]+@" |
| 20 |
const domain_re = "[.a-zA-Z0-9-]+"; |
|
| 20 |
const domain_re = "[.*a-zA-Z0-9-]+";
|
|
| 21 | 21 |
const path_re = "[^?#]*"; |
| 22 | 22 |
const query_re = "\\??[^#]*"; |
| 23 | 23 |
|
| 24 | 24 |
const http_regex = new RegExp(`^(${domain_re})(${path_re})(${query_re}).*`);
|
| 25 | 25 |
|
| 26 |
const file_regex = new RegExp(`^(${path_re}).*`);
|
|
| 26 |
const file_regex = new RegExp(`^(/${path_re}).*`);
|
|
| 27 | 27 |
|
| 28 | 28 |
const ftp_regex = new RegExp(`^(${user_re})?(${domain_re})(${path_re}).*`);
|
| 29 | 29 |
|
| 30 |
function match_or_throw(regex, string, error_msg) |
|
| 31 |
{
|
|
| 32 |
const match = regex.exec(string); |
|
| 33 |
if (match === null) |
|
| 34 |
throw error_msg; |
|
| 35 |
|
|
| 36 |
return match; |
|
| 37 |
} |
|
| 38 |
|
|
| 30 | 39 |
function deconstruct_url(url, use_limits=true) |
| 31 | 40 |
{
|
| 32 | 41 |
const max = MAX; |
| ... | ... | |
| 35 | 44 |
max[key] = Infinity; |
| 36 | 45 |
} |
| 37 | 46 |
|
| 38 |
const proto_match = proto_regex.exec(url); |
|
| 39 |
if (proto_match === null) |
|
| 40 |
throw `bad url '${url}'`;
|
|
| 47 |
const matcher = (re, str) => match_or_throw(re, str, `bad url '${url}'`)
|
|
| 41 | 48 |
|
| 49 |
const proto_match = matcher(proto_regex, url); |
|
| 42 | 50 |
const deco = {proto: proto_match[1]};
|
| 43 | 51 |
|
| 44 | 52 |
if (deco.proto === "file") {
|
| 45 |
deco.path = file_regex.exec(proto_match[2])[1];
|
|
| 53 |
deco.path = matcher(file_regex, proto_match[2])[1];
|
|
| 46 | 54 |
} else if (deco.proto === "ftp") {
|
| 47 |
[deco.domain, deco.path] = ftp_regex.exec(proto_match[2]).slice(2, 4); |
|
| 55 |
[deco.domain, deco.path] = |
|
| 56 |
matcher(ftp_regex, proto_match[2]).slice(2, 4); |
|
| 48 | 57 |
} else if (deco.proto === "http" || deco.proto === "https") {
|
| 49 |
const http_match = http_regex.exec(proto_match[2]); |
|
| 50 |
if (!http_match) |
|
| 51 |
return undefined; |
|
| 52 |
[deco.domain, deco.path, deco.query] = http_match.slice(1, 4); |
|
| 58 |
[deco.domain, deco.path, deco.query] = |
|
| 59 |
matcher(http_regex, proto_match[2]).slice(1, 4); |
|
| 53 | 60 |
deco.domain = deco.domain.toLowerCase(); |
| 54 | 61 |
} else {
|
| 55 | 62 |
throw `unsupported protocol in url '${url}'`;
|
Also available in: Unified diff
start implementing more efficient querying of URL patterns