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

Side by Side Diff: lib/url.js

Issue 5564089086509056: Issue 1801 - Use URL objects to process URLs in the background page (Closed)
Patch Set: Rebased and addressed comments Created Feb. 11, 2015, 5:06 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 | « lib/basedomain.js ('k') | lib/whitelisting.js » ('j') | 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-2015 Eyeo GmbH 3 * Copyright (C) 2006-2015 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 27 matching lines...) Expand all
38 base.href = baseUrl || ""; 38 base.href = baseUrl || "";
39 anchor.href = url; 39 anchor.href = url;
40 40
41 for (let prop of URLProperties) 41 for (let prop of URLProperties)
42 this[prop] = anchor[prop]; 42 this[prop] = anchor[prop];
43 }; 43 };
44 } 44 }
45 45
46 return URL; 46 return URL;
47 })(); 47 })();
48
49 /**
50 * Gets the IDN-decoded hostname from a URL object.
51 *
52 * @param {URL} [url]
53 * @return {string}
54 */
55 function getDecodedHostname(url)
56 {
57 let hostname = url.hostname;
58
59 if (hostname.indexOf("xn--") == -1)
60 return hostname;
61
62 return punycode.toUnicode(hostname);
63 }
64 exports.getDecodedHostname = getDecodedHostname;
65
66 /**
67 * Gets the IDN-decoded hostname from the URL of a frame.
68 * If the URL don't have host information (like "about:blank"
69 * and "data:" URLs) it falls back to the parent frame.
70 *
71 * @param {Frame} [frame]
72 * @return {string}
73 */
74 function extractHostFromFrame(frame)
75 {
76 for (; frame; frame = frame.parent)
77 {
78 let hostname = getDecodedHostname(frame.url);
79 if (hostname)
80 return hostname;
81 }
82
83 return "";
84 }
85 exports.extractHostFromFrame = extractHostFromFrame;
86
87 /**
88 * Converts a URL object into a string. For HTTP(S) URLs the hash and
89 * auth crendetials are stripped, and the hostname gets IDN-decoded.
90 *
91 * @param {URL} [url]
92 * @return {string}
93 */
94 function stringifyURL(url)
95 {
96 let protocol = url.protocol;
97 if (protocol != "http:" && protocol != "https:")
98 return url.href;
99
100 let host = getDecodedHostname(url);
101 if (url.port)
102 host += ":" + url.port;
103 return protocol + "//" + host + url.pathname + url.search;
104 }
105 exports.stringifyURL = stringifyURL;
106
107 function isDomain(hostname)
108 {
109 // No hostname or IPv4 address, also considering hexadecimal octets.
110 if (/^((0x[\da-f]+|\d+)(\.|$))*$/i.test(hostname))
111 return false;
112
113 // IPv6 address. Since there can't be colons in domains, we can
114 // just check whether there are any colons to exclude IPv6 addresses.
115 return hostname.indexOf(":") == -1;
116 }
117
118 function getBaseDomain(hostname)
119 {
120 let bits = hostname.split(".");
121 let cutoff = bits.length - 2;
122
123 for (let i = 0; i < bits.length; i++)
124 {
125 let offset = publicSuffixes[bits.slice(i).join(".")];
126
127 if (typeof offset != "undefined")
128 {
129 cutoff = i - offset;
130 break;
131 }
132 }
133
134 if (cutoff <= 0)
135 return hostname;
136
137 return bits.slice(cutoff).join(".");
138 }
139
140 /**
141 * Checks whether the request's origin is different from the document's origin.
142 *
143 * @param {URL} [url] The request URL
144 * @param {string} [documentHost] The IDN-decoded hostname of the document
145 * @return {Boolean}
146 */
147 function isThirdParty(url, documentHost)
148 {
149 let requestHost = getDecodedHostname(url).replace(/\.+$/, "");
150 documentHost = documentHost.replace(/\.+$/, "");
151
152 if (requestHost == documentHost)
153 return false;
154
155 if (!isDomain(requestHost) || !isDomain(documentHost))
156 return true;
157
158 return getBaseDomain(requestHost) != getBaseDomain(documentHost);
159 }
160 exports.isThirdParty = isThirdParty;
OLDNEW
« no previous file with comments | « lib/basedomain.js ('k') | lib/whitelisting.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld