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: Created Jan. 25, 2015, 1:18 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
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)
kzar 2015/01/26 10:27:38 Took me a second to grok this loop, wouldn't a whi
Sebastian Noack 2015/01/26 10:38:39 I think this a typical case for-lops exist for, to
kzar 2015/01/26 17:19:05 Hmm well I guess it's subjective which form is eas
Sebastian Noack 2015/01/26 17:26:31 As long as you don't consider for-loops generally
Wladimir Palant 2015/02/11 13:45:45 I think I wrote the for loop in the original code,
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 return /[^\d.]/.test(hostname) && hostname.indexOf(":") == -1;
Wladimir Palant 2015/02/09 12:54:29 new URL("http://0x5bec7a38/").hostname - gives you
Sebastian Noack 2015/02/11 10:55:51 Done.
110 }
111
112 function getBaseDomain(hostname)
113 {
114 let bits = hostname.split(".");
115 let cutoff = bits.length - 2;
116
117 for (let i = 0; i < bits.length; i++)
118 {
119 let offset = publicSuffixes[bits.slice(i).join(".")];
120
121 if (typeof offset != "undefined")
122 {
123 cutoff = i - offset;
124 break;
125 }
126 }
127
128 if (cutoff <= 0)
129 return hostname;
130
131 return bits.slice(cutoff).join(".");
132 }
133
134 /**
135 * Checks whether the request's origin is different from the document's origin.
136 *
137 * @param {URL} [url] The request URL
138 * @param {string} [documentHost] The IDN-decoded hostname of the document
139 * @return {Boolean}
140 */
141 function isThirdParty(url, documentHost)
142 {
143 let requestHost = getDecodedHostname(url).replace(/\.+$/, "");
144 documentHost = documentHost.replace(/\.+$/, "");
145
146 if (requestHost == documentHost)
147 return false;
148
149 if (!isDomain(requestHost) || !isDomain(documentHost))
150 return true;
151
152 return getBaseDomain(requestHost) != getBaseDomain(documentHost);
153 }
154 exports.isThirdParty = isThirdParty;
OLDNEW

Powered by Google App Engine
This is Rietveld