aboutsummaryrefslogtreecommitdiffstats
path: root/lib/UriTools.jsm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/UriTools.jsm')
-rw-r--r--lib/UriTools.jsm390
1 files changed, 195 insertions, 195 deletions
diff --git a/lib/UriTools.jsm b/lib/UriTools.jsm
index 892dc44..1c3fa0d 100644
--- a/lib/UriTools.jsm
+++ b/lib/UriTools.jsm
@@ -2,7 +2,7 @@
ηMatrix - a browser extension to black/white list requests.
Copyright (C) 2014-2019 Raymond Hill
- Copyright (C) 2019-2020 Alessio Vanni
+ Copyright (C) 2019 Alessio Vanni
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -17,7 +17,7 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see {http://www.gnu.org/licenses/}.
- Home: https://libregit.spks.xyz/heckyel/ematrix
+ Home: https://gitlab.com/vannilla/ematrix
uMatrix Home: https://github.com/gorhill/uMatrix
*/
@@ -99,9 +99,9 @@ function URI() {
this.fragmentBit = (1 << 7),
this.allBits = (0xFFFF),
this.authorityBit =
- (this.userBit | this.passwordBit | this.hostnameBit | this.portBit);
+ (this.userBit | this.passwordBit | this.hostnameBit | this.portBit);
this.normalizeBits =
- (this.schemeBit | this.hostnameBit | this.pathBit | this.queryBit);
+ (this.schemeBit | this.hostnameBit | this.pathBit | this.queryBit);
}
var cached = new URI();
@@ -125,14 +125,14 @@ DomainCacheEntry.prototype.init = function (domain) {
DomainCacheEntry.prototype.dispose = function () {
this.domain = '';
if (cacheJunkyard.length < junkyardMax) {
- cacheJunkyard.push(this);
+ cacheJunkyard.push(this);
}
};
var domainCacheEntryFactory = function (domain) {
let entry = cacheJunkyard.pop();
if (entry) {
- return entry.init(domain);
+ return entry.init(domain);
}
return new DomainCacheEntry(domain);
};
@@ -141,12 +141,12 @@ var domainCacheAdd = function (hostname, domain) {
let entry = domainCache.get(hostname);
if (entry !== undefined) {
- entry.tstamp = Date.now();
+ entry.tstamp = Date.now();
} else {
- domainCache.set(hostname, domainCacheEntryFactory(domain));
- if (domainCache.size === cacheCountHigh) {
- domainCachePrune();
- }
+ domainCache.set(hostname, domainCacheEntryFactory(domain));
+ if (domainCache.size === cacheCountHigh) {
+ domainCachePrune();
+ }
}
return domain;
@@ -158,11 +158,11 @@ var domainCacheSort = function (a, b) {
var domainCachePrune = function () {
let hostnames =
- Array.from(domainCache.keys()).sort(domainCacheSort).slice(cacheCountLow);
+ Array.from(domainCache.keys()).sort(domainCacheSort).slice(cacheCountLow);
for (let i=hostnames.length-1; i>=0; --i) {
- domainCache.get(hostnames[i]).dispose();
- domainCache.delete(hostnames[i]);
+ domainCache.get(hostnames[i]).dispose();
+ domainCache.delete(hostnames[i]);
}
};
@@ -174,232 +174,232 @@ publicSuffixList.onChanged.addListener(domainCacheReset);
var UriTools = {
set: function (uri) {
- if (uri === undefined) {
- return reset(cached);
- }
-
- let matches = reRFC3986.exec(uri);
- if (!matches) {
- return reset(cached);
- }
-
- cached.scheme = matches[1] !== undefined ?
- matches[1].slice(0, -1) :
- '';
- cached.authority = matches[2] !== undefined ?
- matches[2].slice(2).toLowerCase() :
- '';
- cached.path = matches[3] !== undefined ?
- matches[3] :
- '';
-
- // As per RFC3986
- if (cached.authority !== '' && cached.path === '') {
- cached.path = '/';
- }
-
- cached.query = matches[4] !== undefined ?
- matches[4].slice(1) :
- '';
- cached.fragment = matches[5] !== undefined ?
- matches[5].slice(1) :
- '';
-
- if (reHostFromNakedAuthority.test(cached.authority)) {
- cached.hostname = cached.authority;
- cached.port = '';
- return cached;
- }
-
- matches = reHostPortFromAuthority.exec(cached.authority);
- if (!matches) {
- matches = reIPv6PortFromAuthority.exec(cached.authority);
- if (!matches) {
- return resetAuthority(cached);
- }
- }
-
- cached.hostname = matches[1] !== undefined ?
- matches[1] :
- '';
-
- if (cached.hostname.slice(-1) === '.') {
- cached.hostname = cached.hostname.slice(0, -1);
- }
-
- cached.port = matches[2] !== undefined ?
- matches[2].slice(1) :
- '';
-
- return cached;
+ if (uri === undefined) {
+ return reset(cached);
+ }
+
+ let matches = reRFC3986.exec(uri);
+ if (!matches) {
+ return reset(cached);
+ }
+
+ cached.scheme = matches[1] !== undefined ?
+ matches[1].slice(0, -1) :
+ '';
+ cached.authority = matches[2] !== undefined ?
+ matches[2].slice(2).toLowerCase() :
+ '';
+ cached.path = matches[3] !== undefined ?
+ matches[3] :
+ '';
+
+ // As per RFC3986
+ if (cached.authority !== '' && cached.path === '') {
+ cached.path = '/';
+ }
+
+ cached.query = matches[4] !== undefined ?
+ matches[4].slice(1) :
+ '';
+ cached.fragment = matches[5] !== undefined ?
+ matches[5].slice(1) :
+ '';
+
+ if (reHostFromNakedAuthority.test(cached.authority)) {
+ cached.hostname = cached.authority;
+ cached.port = '';
+ return cached;
+ }
+
+ matches = reHostPortFromAuthority.exec(cached.authority);
+ if (!matches) {
+ matches = reIPv6PortFromAuthority.exec(cached.authority);
+ if (!matches) {
+ return resetAuthority(cached);
+ }
+ }
+
+ cached.hostname = matches[1] !== undefined ?
+ matches[1] :
+ '';
+
+ if (cached.hostname.slice(-1) === '.') {
+ cached.hostname = cached.hostname.slice(0, -1);
+ }
+
+ cached.port = matches[2] !== undefined ?
+ matches[2].slice(1) :
+ '';
+
+ return cached;
},
assemble: function (bits) {
- if (bits === undefined) {
- bits = cached.allBits;
- }
+ if (bits === undefined) {
+ bits = cached.allBits;
+ }
- let s = [];
+ let s = [];
- if (cached.scheme && (bits && cached.schemeBit)) {
- s.push(cached.scheme, ':');
- }
- if (cached.hostname && (bits & cached.hostnameBit)) {
+ if (cached.scheme && (bits && cached.schemeBit)) {
+ s.push(cached.scheme, ':');
+ }
+ if (cached.hostname && (bits & cached.hostnameBit)) {
s.push('//', cached.hostname);
- }
- if (cached.port && (bits & cached.portBit)) {
+ }
+ if (cached.port && (bits & cached.portBit)) {
s.push(':', cached.port);
- }
- if (cached.path && (bits & cached.pathBit)) {
+ }
+ if (cached.path && (bits & cached.pathBit)) {
s.push(cached.path);
- }
- if (cached.query && (bits & cached.queryBit)) {
+ }
+ if (cached.query && (bits & cached.queryBit)) {
s.push('?', cached.query);
- }
- if (cached.fragment && (bits & cached.fragmentBit)) {
+ }
+ if (cached.fragment && (bits & cached.fragmentBit)) {
s.push('#', cached.fragment);
- }
+ }
- return s.join('');
+ return s.join('');
},
isNetworkScheme: function (scheme) {
- return reNetworkScheme.test(scheme);
+ return reNetworkScheme.test(scheme);
},
isSecureScheme: function(scheme) {
- return reSecureScheme.test(scheme);
+ return reSecureScheme.test(scheme);
},
originFromURI: function (uri) {
- let matches = reOriginFromURI.exec(uri);
- return matches !== null ? matches[0].toLowerCase() : '';
+ let matches = reOriginFromURI.exec(uri);
+ return matches !== null ? matches[0].toLowerCase() : '';
},
schemeFromURI: function (uri) {
- let matches = reSchemeFromURI.exec(uri);
- return matches !== null ? matches[0].slice(0, -1).toLowerCase() : '';
+ let matches = reSchemeFromURI.exec(uri);
+ return matches !== null ? matches[0].slice(0, -1).toLowerCase() : '';
},
authorityFromURI: function (uri) {
- let matches = reAuthorityFromURI.exec(uri);
- return matches !== null ? matches[1].slice(1).toLowerCase() : '';
+ let matches = reAuthorityFromURI.exec(uri);
+ return matches !== null ? matches[1].slice(1).toLowerCase() : '';
},
hostnameFromURI: function (uri) {
- let matches = reCommonHostnameFromURL.exec(uri);
- if (matches) {
- return matches[1];
- }
-
- matches = reAuthorityFromURI.exec(uri);
- if (!matches) {
- return '';
- }
-
- let auth = matches[1].slice(2);
-
- if (reHostFromNakedAuthority.test(auth)) {
- return auth.toLowerCase();
- }
-
- matches = reHostFromAuthority.exec(auth);
- if (!matches) {
- matches = reIPv6FromAuthority.exec(auth);
- if (!matches) {
- return '';
- }
- }
-
- let hostname = matches[1];
- while (hostname.endsWith('.')) {
- hostname = hostname.slice(0, -1);
- }
-
- if (reMustNormalizeHostname.test(hostname)) {
- Punycode.toASCII(hostname.toLowerCase());
- }
-
- return hostname;
+ let matches = reCommonHostnameFromURL.exec(uri);
+ if (matches) {
+ return matches[1];
+ }
+
+ matches = reAuthorityFromURI.exec(uri);
+ if (!matches) {
+ return '';
+ }
+
+ let auth = matches[1].slice(2);
+
+ if (reHostFromNakedAuthority.test(auth)) {
+ return auth.toLowerCase();
+ }
+
+ matches = reHostFromAuthority.exec(auth);
+ if (!matches) {
+ matches = reIPv6FromAuthority.exec(auth);
+ if (!matches) {
+ return '';
+ }
+ }
+
+ let hostname = matches[1];
+ while (hostname.endsWith('.')) {
+ hostname = hostname.slice(0, -1);
+ }
+
+ if (reMustNormalizeHostname.test(hostname)) {
+ Punycode.toASCII(hostname.toLowerCase());
+ }
+
+ return hostname;
},
domainFromHostname: function (hostname) {
- let entry = domainCache.get(hostname);
- if (entry !== undefined) {
- entry.tstamp = Date.now();
- return entry.domain;
- }
-
- if (reIPAddressNaive.test(hostname) == false) {
- return domainCacheAdd(hostname,
- publicSuffixList.getDomain(hostname));
- }
-
- return domainCacheAdd(hostname, hostname);
+ let entry = domainCache.get(hostname);
+ if (entry !== undefined) {
+ entry.tstamp = Date.now();
+ return entry.domain;
+ }
+
+ if (reIPAddressNaive.test(hostname) == false) {
+ return domainCacheAdd(hostname,
+ publicSuffixList.getDomain(hostname));
+ }
+
+ return domainCacheAdd(hostname, hostname);
},
domainFromURI: function (uri) {
- if (!uri) {
- return '';
- }
- return UriTools.domainFromHostname(UriTools.hostnameFromURI(uri));
+ if (!uri) {
+ return '';
+ }
+ return UriTools.domainFromHostname(UriTools.hostnameFromURI(uri));
},
domain: function() {
- return UriTools.domainFromHostname(cached.hostname);
+ return UriTools.domainFromHostname(cached.hostname);
},
pathFromURI: function (uri) {
- let matches = rePathFromURI.exec(uri);
- return matches !== null ? matches[1] : '';
+ let matches = rePathFromURI.exec(uri);
+ return matches !== null ? matches[1] : '';
},
normalizedURI: function () {
- return UriTools.assemble(cached.normalizeBits);
+ return UriTools.assemble(cached.normalizeBits);
},
rootURL: function () {
- if (!cached.hostname) {
- return '';
- }
- return UriTools.assemble(cached.scemeBit | cached.hostnameBit);
+ if (!cached.hostname) {
+ return '';
+ }
+ return UriTools.assemble(cached.scemeBit | cached.hostnameBit);
},
isValidHostname: function (hostname) {
- try {
- let r = reValidHostname.test(hostname);
- return r;
- } catch (e) {
- return false;
- }
+ try {
+ let r = reValidHostname.test(hostname);
+ return r;
+ } catch (e) {
+ return false;
+ }
},
parentHostnameFromHostname: function (hostname) {
- // "locahost" => ""
- // "example.org" => "example.org"
- // "www.example.org" => "example.org"
- // "tomato.www.example.org" => "example.org"
- let domain = UriTools.domainFromHostname(hostname);
+ // "locahost" => ""
+ // "example.org" => "example.org"
+ // "www.example.org" => "example.org"
+ // "tomato.www.example.org" => "example.org"
+ let domain = UriTools.domainFromHostname(hostname);
- if (domain === '' || domain === hostname) {
- return undefined;
- }
+ if (domain === '' || domain === hostname) {
+ return undefined;
+ }
- return hostname.slice(hostname.indexOf('.') + 1);
+ return hostname.slice(hostname.indexOf('.') + 1);
},
parentHostnamesFromHostname: function (hostname) {
- let domain = UriTools.domainFromHostname(hostname);
- if (domain === '' || domain === hostname) {
- return [];
- }
-
- let nodes = [];
- for (;;) {
- let pos = hostname.indexOf('.');
- if (pos < 0) {
- break;
- }
-
- hostname = hostname.slice(pos+1);
- nodes.push(hostname);
- if (hostname === domain) {
- break;
- }
- }
-
- return nodes;
+ let domain = UriTools.domainFromHostname(hostname);
+ if (domain === '' || domain === hostname) {
+ return [];
+ }
+
+ let nodes = [];
+ for (;;) {
+ let pos = hostname.indexOf('.');
+ if (pos < 0) {
+ break;
+ }
+
+ hostname = hostname.slice(pos+1);
+ nodes.push(hostname);
+ if (hostname === domain) {
+ break;
+ }
+ }
+
+ return nodes;
},
allHostNamesFromHostname: function (hostname) {
- let nodes = UriTools.parentHostnamesFromHostname(hostname);
- nodes.unshift(hostname);
- return nodes;
+ let nodes = UriTools.parentHostnamesFromHostname(hostname);
+ nodes.unshift(hostname);
+ return nodes;
},
toString: function () {
- return UriTools.assemble();
+ return UriTools.assemble();
},
};