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

Side by Side Diff: lib/url.js

Issue 29336748: Issue 3671 - Expose getBaseDomain for abp2blocklist (Closed)
Patch Set: Created Feb. 20, 2016, 3:29 p.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-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 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
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 { 85 {
86 // No hostname or IPv4 address, also considering hexadecimal octets. 86 // No hostname or IPv4 address, also considering hexadecimal octets.
87 if (/^((0x[\da-f]+|\d+)(\.|$))*$/i.test(hostname)) 87 if (/^((0x[\da-f]+|\d+)(\.|$))*$/i.test(hostname))
88 return false; 88 return false;
89 89
90 // IPv6 address. Since there can't be colons in domains, we can 90 // IPv6 address. Since there can't be colons in domains, we can
91 // just check whether there are any colons to exclude IPv6 addresses. 91 // just check whether there are any colons to exclude IPv6 addresses.
92 return hostname.indexOf(":") == -1; 92 return hostname.indexOf(":") == -1;
93 } 93 }
94 94
95 function getBaseDomain(hostname) 95 let getBaseDomain =
96 /**
97 * Returns the base domain for the given hostname.
98 *
99 * @param {string} hostname
100 * @return {string} base domain
101 */
102 exports.getBaseDomain = function(hostname)
96 { 103 {
97 let bits = hostname.split("."); 104 let bits = hostname.split(".");
98 let cutoff = bits.length - 2; 105 let cutoff = bits.length - 2;
99 106
100 for (let i = 0; i < bits.length; i++) 107 for (let i = 0; i < bits.length; i++)
101 { 108 {
102 let offset = publicSuffixes[bits.slice(i).join(".")]; 109 let offset = publicSuffixes[bits.slice(i).join(".")];
103 110
104 if (typeof offset != "undefined") 111 if (typeof offset != "undefined")
105 { 112 {
106 cutoff = i - offset; 113 cutoff = i - offset;
107 break; 114 break;
108 } 115 }
109 } 116 }
110 117
111 if (cutoff <= 0) 118 if (cutoff <= 0)
112 return hostname; 119 return hostname;
113 120
114 return bits.slice(cutoff).join("."); 121 return bits.slice(cutoff).join(".");
115 } 122 };
116 123
117 /** 124 /**
118 * Checks whether the request's origin is different from the document's origin. 125 * Checks whether the request's origin is different from the document's origin.
119 * 126 *
120 * @param {URL} url The request URL 127 * @param {URL} url The request URL
121 * @param {string} documentHost The IDN-decoded hostname of the document 128 * @param {string} documentHost The IDN-decoded hostname of the document
122 * @return {Boolean} 129 * @return {Boolean}
123 */ 130 */
124 exports.isThirdParty = function(url, documentHost) 131 exports.isThirdParty = function(url, documentHost)
125 { 132 {
126 let requestHost = getDecodedHostname(url).replace(/\.+$/, ""); 133 let requestHost = getDecodedHostname(url).replace(/\.+$/, "");
127 documentHost = documentHost.replace(/\.+$/, ""); 134 documentHost = documentHost.replace(/\.+$/, "");
128 135
129 if (requestHost == documentHost) 136 if (requestHost == documentHost)
130 return false; 137 return false;
131 138
132 if (!isDomain(requestHost) || !isDomain(documentHost)) 139 if (!isDomain(requestHost) || !isDomain(documentHost))
133 return true; 140 return true;
134 141
135 return getBaseDomain(requestHost) != getBaseDomain(documentHost); 142 return getBaseDomain(requestHost) != getBaseDomain(documentHost);
136 }; 143 };
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