Project

General

Profile

Download (5.83 KB) Statistics
| Branch: | Tag: | Revision:

haketilo / common / patterns.js @ e1282a63

1
/**
2
 * This file is part of Haketilo.
3
 *
4
 * Function: Operations on page URL patterns.
5
 *
6
 * Copyright (C) 2021 Wojtek Kosior
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * As additional permission under GNU GPL version 3 section 7, you
19
 * may distribute forms of that code without the copy of the GNU
20
 * GPL normally required by section 4, provided you include this
21
 * license notice and, in case of non-source distribution, a URL
22
 * through which recipients can access the Corresponding Source.
23
 * If you modify file(s) with this exception, you may extend this
24
 * exception to your version of the file(s), but you are not
25
 * obligated to do so. If you do not wish to do so, delete this
26
 * exception statement from your version.
27
 *
28
 * As a special exception to the GPL, any HTML file which merely
29
 * makes function calls to this code, and for that purpose
30
 * includes it by reference shall be deemed a separate work for
31
 * copyright law purposes. If you modify this code, you may extend
32
 * this exception to your version of the code, but you are not
33
 * obligated to do so. If you do not wish to do so, delete this
34
 * exception statement from your version.
35
 *
36
 * You should have received a copy of the GNU General Public License
37
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
38
 *
39
 * I, Wojtek Kosior, thereby promise not to sue for violation of this file's
40
 * license. Although I request that you do not make use this code in a
41
 * proprietary program, I am not going to enforce this in court.
42
 */
43

    
44
const MAX = {
45
    URL_PATH_LEN:   12,
46
    URL_PATH_CHARS: 255,
47
    DOMAIN_LEN:     7,
48
    DOMAIN_CHARS:   100
49
};
50

    
51
const proto_regex = /^(\w+):\/\/(.*)$/;
52

    
53
const user_re = "[^/?#@]+@"
54
const domain_re = "[.*a-zA-Z0-9-]+";
55
const path_re = "[^?#]*";
56
const query_re = "\\??[^#]*";
57

    
58
const http_regex = new RegExp(`^(${domain_re})(${path_re})(${query_re}).*`);
59

    
60
const file_regex = new RegExp(`^(/${path_re}).*`);
61

    
62
const ftp_regex = new RegExp(`^(${user_re})?(${domain_re})(${path_re}).*`);
63

    
64
function match_or_throw(regex, string, error_msg)
65
{
66
    const match = regex.exec(string);
67
    if (match === null)
68
	throw error_msg;
69

    
70
    return match;
71
}
72

    
73
function deconstruct_url(url, use_limits=true)
74
{
75
    const max = MAX;
76
    if (!use_limits) {
77
	for (key in MAX)
78
	    max[key] = Infinity;
79
    }
80

    
81
    const matcher = (re, str) => match_or_throw(re, str, `bad url '${url}'`)
82

    
83
    const proto_match = matcher(proto_regex, url);
84
    const deco = {proto: proto_match[1]};
85

    
86
    if (deco.proto === "file") {
87
	deco.path = matcher(file_regex, proto_match[2])[1];
88
    } else if (deco.proto === "ftp") {
89
	[deco.domain, deco.path] =
90
	    matcher(ftp_regex, proto_match[2]).slice(2, 4);
91
    } else if (deco.proto === "http" || deco.proto === "https") {
92
	[deco.domain, deco.path, deco.query] =
93
	    matcher(http_regex, proto_match[2]).slice(1, 4);
94
	deco.domain = deco.domain.toLowerCase();
95
    } else {
96
	throw `unsupported protocol in url '${url}'`;
97
    }
98

    
99
    deco.trailing_slash = deco.path[deco.path.length - 1] === "/";
100

    
101
    if (deco.domain) {
102
	if (deco.domain.length > max.DOMAIN_CHARS) {
103
	    const idx = deco.domain.indexOf(".", deco.domain.length -
104
					    max.DOMAIN_CHARS);
105
	    if (idx === -1)
106
		deco.domain = [];
107
	    else
108
		deco.domain = deco.domain.substring(idx + 1);
109

    
110
	    deco.domain_truncated = true;
111
	}
112

    
113
	if (deco.path.length > max.URL_PATH_CHARS) {
114
	    deco.path = deco.path.substring(0, deco.path.lastIndexOf("/"));
115
	    deco.path_truncated = true;
116
	}
117
    }
118

    
119
    if (typeof deco.domain === "string") {
120
	deco.domain = deco.domain.split(".");
121
	if (deco.domain.splice(0, deco.domain.length - max.DOMAIN_LEN).length
122
	    > 0)
123
	    deco.domain_truncated = true;
124
    }
125

    
126
    deco.path = deco.path.split("/").filter(s => s !== "");
127
    if (deco.domain && deco.path.splice(max.URL_PATH_LEN).length > 0)
128
	deco.path_truncated = true;
129

    
130
    return deco;
131
}
132

    
133
function* each_domain_pattern(deco)
134
{
135
    for (let slice = 0; slice < deco.domain.length - 1; slice++) {
136
	const domain_part = deco.domain.slice(slice).join(".");
137
	const domain_wildcards = [];
138
	if (slice === 0 && !deco.domain_truncated)
139
	    yield domain_part;
140
	if (slice === 1 && !deco.domain_truncated)
141
	    yield "*." + domain_part;
142
	if (slice > 1)
143
	    yield "**." + domain_part;
144
	yield "***." + domain_part;
145
    }
146
}
147

    
148
function* each_path_pattern(deco)
149
{
150
    for (let slice = deco.path.length; slice >= 0; slice--) {
151
	const path_part = ["", ...deco.path.slice(0, slice)].join("/");
152
	const path_wildcards = [];
153
	if (slice === deco.path.length && !deco.path_truncated) {
154
	    if (deco.trailing_slash)
155
		yield path_part + "/";
156
	    if (slice > 0 || deco.proto !== "file")
157
		yield path_part;
158
	}
159
	if (slice === deco.path.length - 1 && !deco.path_truncated &&
160
	    deco.path[slice] !== "*")
161
	    yield path_part + "/*";
162
	if (slice < deco.path.length - 1)
163
	    yield path_part + "/**";
164
	if (slice !== deco.path.length - 1 || deco.path_truncated ||
165
	    deco.path[slice] !== "***")
166
	    yield path_part + "/***";
167
    }
168
}
169

    
170
/* Generate every possible pattern that matches url. */
171
function* each_url_pattern(url)
172
{
173
    const deco = deconstruct_url(url);
174

    
175
    if (deco === undefined) {
176
	console.error("bad url format", url);
177
	return false;
178
    }
179

    
180
    const all_domains = deco.domain ? each_domain_pattern(deco) : [""];
181
    for (const domain of all_domains) {
182
	for (const path of each_path_pattern(deco))
183
	    yield `${deco.proto}://${domain}${path}`;
184
    }
185
}
186

    
187
/*
188
 * EXPORTS_START
189
 * EXPORT each_url_pattern
190
 * EXPORT deconstruct_url
191
 * EXPORTS_END
192
 */
(8-8/16)