This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
/
urlmatch.js
194 lines (174 loc) · 7.19 KB
/
urlmatch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
* urlmatch.js
*
* Includes parseUri (c) Steven Levithan <steven@levithan.com> Under the MIT License
*
* Features:
* + parse a url into components
* + url validiation
* + semantically lossless normalization
* + url prefix matching
*
* window.URLParse(string) -
* parse a url using the 'parseUri' algorithm, returning an object containing various
* uri components. returns an object with the following properties (all optional):
*
* PROPERTIES:
* anchor - stuff after the #
* authority - everything after the :// and before the path. Including user auth, host, and port
* directory - path with trailing filename and everything after removed
* file - path without directory
* host - host
* password - password part when user:pass@ is prepended to host
* path - full path, sans query or anchor
* port - port, when present in url
* query - ?XXX
* relative -
* scheme - url scheme (http, file, https, etc.)
* source - full string passed to URLParse()
* user - user part when user:pass@ is prepended to host
* userInfo -
*
* FUNCTIONS:
* (string) toString() - generate a string representation of the url
*
* (this) validate() - validate the url, possbly throwing a string exception
* if determined to not be a valid URL. Returns this, thus may be chained.
*
* (this) normalize() - perform in-place modification of the url to place it in a normal
* (and verbose) form. Returns this, thus may be chained.
*
* (bool) contains(str) - returns whether the object upon which contains() is called is a
* "url prefix" for the passed in string, after normalization.
*
* (this) originOnly() - removes everything that would occur after port, including
* path, query, and anchor.
*
*/
URLParse = (function() {
/* const */ var INV_URL = "invalid url: ";
var parseURL = function(s) {
var toString = function() {
var str = this.scheme + "://";
if (this.user) str += this.user;
if (this.password) str += ":" + this.password;
if (this.user || this.password) str += "@";
if (this.host) str += this.host;
if (this.port) str += ":" + this.port;
if (this.path) str += this.path;
if (this.query) str += "?" + this.query;
if (this.anchor) str += "#" + this.anchor;
return str;
};
var originOnly = function() {
this.path = this.query = this.anchor = undefined;
return this;
};
var validate = function() {
if (!this.scheme) throw INV_URL +"missing scheme";
if (this.scheme !== 'http' && this.scheme !== 'https')
throw INV_URL + "unsupported scheme: " + this.scheme;
if (!this.host) throw INV_URL + "missing host";
if (this.port) {
var p = parseInt(this.port);
if (!this.port.match(/^\d+$/)) throw INV_URL + "non-numeric numbers in port";
if (p <= 0 || p >= 65536) throw INV_URL + "port out of range (" +this.port+")";
}
if (this.path && this.path.indexOf('/') != 0) throw INV_URL + "path must start with '/'";
return this;
};
var normalize = function() {
// lowercase scheme
if (this.scheme) this.scheme = this.scheme.toLowerCase();
// for directory references, append trailing slash
if (!this.path) this.path = "/";
// remove port numbers same as default
if (this.port === "80" && 'http' === this.scheme) delete this.port;
if (this.port === "443" && 'https' === this.scheme) delete this.port;
// remove dot segments from path, algorithm
// http://tools.ietf.org/html/rfc3986#section-5.2.4
this.path = (function (p) {
var out = [];
while (p) {
if (p.indexOf('../') === 0) p = p.substr(3);
else if (p.indexOf('./') === 0) p = p.substr(2);
else if (p.indexOf('/./') === 0) p = p.substr(2);
else if (p === '/.') p = '/';
else if (p.indexOf('/../') === 0 || p === '/..') {
if (out.length > 0) out.pop();
p = '/' + p.substr(4);
} else if (p === '.' || p === '..') p = '';
else {
var m = p.match(/^\/?([^\/]*)/);
// remove path match from input
p = p.substr(m[0].length);
// add path to output
out.push(m[1]);
}
}
return '/' + out.join('/');
})(this.path);
// XXX: upcase chars in % escaping?
// now we need to update all members
var n = parseURL(this.toString()),
i = 14,
o = parseUri.options;
while (i--) {
var k = o.key[i];
if (n[k] && typeof(n[k]) === 'string') this[k] = n[k];
else if (this[k] && typeof(this[k]) === 'string') delete this[k];
}
return this;
};
var contains = function(str) {
try {
this.validate();
var prefix = parseURL(this.toString()).normalize().toString();
var url = parseURL(str).validate().normalize().toString();
return (url.indexOf(prefix) === 0);
} catch(e) {
console.log(e);
// if any exceptions are raised, then the comparison fails
return false;
}
};
// parseUri 1.2.2
// (c) Steven Levithan <stevenlevithan.com>
// MIT License
var parseUri = function(str) {
var o = parseUri.options,
m = o.parser.exec(str),
uri = {},
i = 14;
while (i--) if (m[i]) uri[o.key[i]] = m[i];
if (uri[o.key[12]]) {
uri[o.q.name] = {};
uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
if ($1) uri[o.q.name][$1] = $2;
});
}
// member functions
uri.toString = toString;
uri.validate = validate;
uri.normalize = normalize;
uri.contains = contains;
uri.originOnly = originOnly;
return uri;
};
parseUri.options = {
key: ["source","scheme","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
q: {
name: "queryKey",
parser: /(?:^|&)([^&=]*)=?([^&]*)/g
},
parser: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/
};
// end parseUri
// parse URI using the parseUri code and return the resultant object
return parseUri(s);
};
return parseURL;
})();
/* Jetpack specific export */
if (typeof exports !== "undefined")
exports.URLParse = URLParse;