Left: | ||
Right: |
OLD | NEW |
---|---|
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-present eyeo GmbH | 3 * Copyright (C) 2006-present eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 "use strict"; | 18 "use strict"; |
19 | 19 |
20 const publicSuffixes = require("../data/publicSuffixList.json"); | 20 const publicSuffixes = require("../data/publicSuffixList.json"); |
21 | 21 |
22 /** | 22 /** |
23 * Maximum number of entries to keep in <code>{@link cache}</code>. | |
24 * @type {number} | |
25 */ | |
26 const maxCacheEntries = 1000; | |
27 | |
28 /** | |
29 * Cached results from previous <code>{@link isThirdParty}</code> calls. | |
30 * @type {Map.<string,boolean>} | |
31 */ | |
32 let cache = new Map(); | |
33 | |
34 /** | |
23 * Checks whether the given hostname is a domain. | 35 * Checks whether the given hostname is a domain. |
24 * | 36 * |
25 * @param {string} hostname | 37 * @param {string} hostname |
26 * @returns {boolean} | 38 * @returns {boolean} |
27 */ | 39 */ |
28 function isDomain(hostname) | 40 function isDomain(hostname) |
29 { | 41 { |
30 // No hostname or IPv4 address, also considering hexadecimal octets. | 42 // No hostname or IPv4 address, also considering hexadecimal octets. |
31 if (/^((0x[\da-f]+|\d+)(\.|$))*$/i.test(hostname)) | 43 if (/^((0x[\da-f]+|\d+)(\.|$))*$/i.test(hostname)) |
32 return false; | 44 return false; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
67 /** | 79 /** |
68 * Checks whether a request's origin is different from its document's origin. | 80 * Checks whether a request's origin is different from its document's origin. |
69 * | 81 * |
70 * @param {URL} url The request URL. | 82 * @param {URL} url The request URL. |
71 * @param {string} documentHostname The IDNA-encoded hostname of the document. | 83 * @param {string} documentHostname The IDNA-encoded hostname of the document. |
72 * | 84 * |
73 * @returns {boolean} | 85 * @returns {boolean} |
74 */ | 86 */ |
75 function isThirdParty(url, documentHostname) | 87 function isThirdParty(url, documentHostname) |
76 { | 88 { |
77 let requestHostname = url.hostname.replace(/\.+$/, ""); | 89 let requestHostname = url.hostname; |
78 documentHostname = documentHostname.replace(/\.+$/, ""); | 90 |
91 if (requestHostname[requestHostname.length - 1] == ".") | |
Manish Jethani
2019/01/23 08:51:06
Avoid calling replace() since it's very rarely nec
| |
92 requestHostname = requestHostname.replace(/\.+$/, ""); | |
93 | |
94 if (documentHostname[documentHostname.length - 1] == ".") | |
95 documentHostname = documentHostname.replace(/\.+$/, ""); | |
79 | 96 |
80 if (requestHostname == documentHostname) | 97 if (requestHostname == documentHostname) |
Manish Jethani
2019/01/23 08:51:06
No need to cache if the hostnames are literally eq
| |
81 return false; | 98 return false; |
82 | 99 |
83 if (!isDomain(requestHostname) || !isDomain(documentHostname)) | 100 let key = requestHostname + " " + documentHostname; |
84 return true; | 101 let value = cache.get(key); |
85 | 102 |
86 return getDomain(requestHostname) != getDomain(documentHostname); | 103 if (typeof value != "undefined") |
104 return value; | |
105 | |
106 value = !isDomain(requestHostname) || !isDomain(documentHostname) || | |
107 getDomain(requestHostname) != getDomain(documentHostname); | |
108 | |
109 if (cache.size >= maxCacheEntries) | |
110 cache.clear(); | |
111 | |
112 cache.set(key, value); | |
113 | |
114 return value; | |
87 } | 115 } |
88 | 116 |
89 exports.isThirdParty = isThirdParty; | 117 exports.isThirdParty = isThirdParty; |
OLD | NEW |