Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/domain.js

Issue 29994555: Issue 7250 - Optimize third-party request check (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Created Jan. 31, 2019, 10:11 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 let publicSuffixMap = buildPublicSuffixMap();
Manish Jethani 2019/01/31 10:14:15 Maps are faster for dynamic lookups (where the pro
23
24 function buildPublicSuffixMap()
25 {
26 let map = new Map();
27
28 for (let key in publicSuffixes)
29 map.set(key, publicSuffixes[key]);
30
31 return map;
32 }
33
34 function* suffixes(domain)
Manish Jethani 2019/01/31 10:14:15 We will reuse this function in other places (e.g.
35 {
36 while (domain != "")
37 {
38 yield domain;
39
40 let dotIndex = domain.indexOf(".");
41 domain = dotIndex == -1 ? "" : domain.substr(dotIndex + 1);
42 }
43 }
44
22 /** 45 /**
23 * Checks whether the given hostname is a domain. 46 * Checks whether the given hostname is a domain.
24 * 47 *
25 * @param {string} hostname 48 * @param {string} hostname
26 * @returns {boolean} 49 * @returns {boolean}
27 */ 50 */
28 function isDomain(hostname) 51 function isDomain(hostname)
29 { 52 {
30 // No hostname or IPv4 address, also considering hexadecimal octets. 53 // No hostname or IPv4 address, also considering hexadecimal octets.
31 if (/^((0x[\da-f]+|\d+)(\.|$))*$/i.test(hostname)) 54 if (/^((0x[\da-f]+|\d+)(\.|$))*$/i.test(hostname))
32 return false; 55 return false;
33 56
34 // IPv6 address. Since there can't be colons in domains, we can 57 // IPv6 address. Since there can't be colons in domains, we can
35 // just check whether there are any colons to exclude IPv6 addresses. 58 // just check whether there are any colons to exclude IPv6 addresses.
36 return hostname.indexOf(":") == -1; 59 return hostname.indexOf(":") == -1;
37 } 60 }
38 61
39 /** 62 /**
40 * Gets the base domain for the given hostname. 63 * Gets the base domain for the given hostname.
41 * 64 *
42 * @param {string} hostname 65 * @param {string} hostname
43 * @returns {string} 66 * @returns {string}
44 */ 67 */
45 function getDomain(hostname) 68 function getDomain(hostname)
Manish Jethani 2019/01/31 10:14:15 At least twice as fast as the previous implementat
46 { 69 {
47 let bits = hostname.split("."); 70 let slices = [];
48 let cutoff = bits.length - 2; 71 let cutoff = null;
49 72
50 for (let i = 0; i < bits.length; i++) 73 for (let suffix of suffixes(hostname))
51 { 74 {
52 let offset = publicSuffixes[bits.slice(i).join(".")]; 75 slices.push(suffix);
76
77 let offset = publicSuffixMap.get(suffix);
53 78
54 if (typeof offset != "undefined") 79 if (typeof offset != "undefined")
55 { 80 {
56 cutoff = i - offset; 81 cutoff = slices.length - 1 - offset;
57 break; 82 break;
58 } 83 }
59 } 84 }
60 85
86 if (cutoff == null)
87 return slices.length > 2 ? slices[slices.length - 2] : hostname;
88
61 if (cutoff <= 0) 89 if (cutoff <= 0)
62 return hostname; 90 return hostname;
63 91
64 return bits.slice(cutoff).join("."); 92 return slices[cutoff];
65 } 93 }
66 94
95 exports.getDomain = getDomain;
96
67 /** 97 /**
68 * Checks whether a request's origin is different from its document's origin. 98 * Checks whether a request's origin is different from its document's origin.
69 * 99 *
70 * @param {URL} url The request URL. 100 * @param {URL} url The request URL.
71 * @param {string} documentHostname The IDNA-encoded hostname of the document. 101 * @param {string} documentHostname The IDNA-encoded hostname of the document.
72 * 102 *
73 * @returns {boolean} 103 * @returns {boolean}
74 */ 104 */
75 function isThirdParty(url, documentHostname) 105 function isThirdParty(url, documentHostname)
76 { 106 {
77 let requestHostname = url.hostname.replace(/\.+$/, ""); 107 let requestHostname = url.hostname;
78 documentHostname = documentHostname.replace(/\.+$/, ""); 108
109 if (requestHostname[requestHostname.length - 1] == ".")
110 requestHostname = requestHostname.replace(/\.+$/, "");
111
112 if (documentHostname[documentHostname.length - 1] == ".")
113 documentHostname = documentHostname.replace(/\.+$/, "");
79 114
80 if (requestHostname == documentHostname) 115 if (requestHostname == documentHostname)
81 return false; 116 return false;
82 117
83 if (!isDomain(requestHostname) || !isDomain(documentHostname)) 118 if (!isDomain(requestHostname) || !isDomain(documentHostname))
84 return true; 119 return true;
85 120
86 return getDomain(requestHostname) != getDomain(documentHostname); 121 return getDomain(requestHostname) != getDomain(documentHostname);
87 } 122 }
88 123
89 exports.isThirdParty = isThirdParty; 124 exports.isThirdParty = isThirdParty;
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld