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

Side by Side Diff: lib/basedomain.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 | « chrome/ext/background.js ('k') | lib/url.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*!
2 * Parts of original code from ipv6.js <https://github.com/beaugunderson/javascr ipt-ipv6>
3 * Copyright 2011 Beau Gunderson
4 * Available under MIT license <http://mths.be/mit>
5 */
6
7 const RE_V4 = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|0x[0-9a-f][0-9a-f]?|0 [0-7]{3})\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|0x[0-9a-f][0-9a-f]?|0[0-7 ]{3})$/i;
8 const RE_V4_HEX = /^0x([0-9a-f]{8})$/i;
9 const RE_V4_NUMERIC = /^[0-9]+$/;
10 const RE_V4inV6 = /(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2 [0-4][0-9]|[01]?[0-9][0-9]?)$/;
11
12 const RE_BAD_CHARACTERS = /([^0-9a-f:])/i;
13 const RE_BAD_ADDRESS = /([0-9a-f]{5,}|:{3,}|[^:]:$|^:[^:]$)/i;
14
15 function isIPv4(address)
16 {
17 if (RE_V4.test(address))
18 return true;
19 if (RE_V4_HEX.test(address))
20 return true;
21 if (RE_V4_NUMERIC.test(address))
22 return true;
23 return false;
24 }
25
26 function isIPv6(address)
27 {
28 var a4addon = 0;
29 var address4 = address.match(RE_V4inV6);
30 if (address4)
31 {
32 var temp4 = address4[0].split('.');
33 for (var i = 0; i < 4; i++)
34 {
35 if (/^0[0-9]+/.test(temp4[i]))
36 return false;
37 }
38 address = address.replace(RE_V4inV6, '');
39 if (/[0-9]$/.test(address))
40 return false;
41
42 address = address + temp4.join(':');
43 a4addon = 2;
44 }
45
46 if (RE_BAD_CHARACTERS.test(address))
47 return false;
48
49 if (RE_BAD_ADDRESS.test(address))
50 return false;
51
52 function count(string, substring)
53 {
54 return (string.length - string.replace(new RegExp(substring,"g"), '').length ) / substring.length;
55 }
56
57 var halves = count(address, '::');
58 if (halves == 1 && count(address, ':') <= 6 + 2 + a4addon)
59 return true;
60 if (halves == 0 && count(address, ':') == 7 + a4addon)
61 return true;
62 return false;
63 }
64
65 /**
66 * Returns base domain for specified host based on Public Suffix List.
67 */
68 function getBaseDomain(/**String*/ hostname) /**String*/
69 {
70 // remove trailing dot(s)
71 hostname = hostname.replace(/\.+$/, '');
72
73 // return IP address untouched
74 if (isIPv6(hostname) || isIPv4(hostname))
75 return hostname;
76
77 // decode punycode if exists
78 if (hostname.indexOf('xn--') >= 0)
79 {
80 hostname = punycode.toUnicode(hostname);
81 }
82
83 // search through PSL
84 var prevDomains = [];
85 var curDomain = hostname;
86 var nextDot = curDomain.indexOf('.');
87 var tld = 0;
88
89 while (true)
90 {
91 var suffix = publicSuffixes[curDomain];
92 if (typeof(suffix) != 'undefined')
93 {
94 tld = suffix;
95 break;
96 }
97
98 if (nextDot < 0)
99 {
100 tld = 1;
101 break;
102 }
103
104 prevDomains.push(curDomain.substring(0,nextDot));
105 curDomain = curDomain.substring(nextDot+1);
106 nextDot = curDomain.indexOf('.');
107 }
108
109 while (tld > 0 && prevDomains.length > 0)
110 {
111 curDomain = prevDomains.pop() + '.' + curDomain;
112 tld--;
113 }
114
115 return curDomain;
116 }
117
118 /**
119 * Checks whether a request is third party for the given document, uses
120 * information from the public suffix list to determine the effective domain
121 * name for the document.
122 */
123 function isThirdParty(/**String*/ requestHost, /**String*/ documentHost)
124 {
125 // Remove trailing dots
126 requestHost = requestHost.replace(/\.+$/, "");
127 documentHost = documentHost.replace(/\.+$/, "");
128
129 // Extract domain name - leave IP addresses unchanged, otherwise leave only ba se domain
130 var documentDomain = getBaseDomain(documentHost);
131 if (requestHost.length > documentDomain.length)
132 return (requestHost.substr(requestHost.length - documentDomain.length - 1) ! = "." + documentDomain);
133 else
134 return (requestHost != documentDomain);
135 }
136
137 /**
138 * Extracts host name from a URL.
139 */
140 function extractHostFromURL(/**String*/ url)
141 {
142 if (url && extractHostFromURL._lastURL == url)
143 return extractHostFromURL._lastDomain;
144
145 var host = new URL(url).hostname;
146 extractHostFromURL._lastURL = url;
147 extractHostFromURL._lastDomain = host;
148 return host;
149 }
150
151 /**
152 * Extracts host name from the URL of the given frame. If the URL don't have hos t
153 * information (like about:blank or data: URLs) it falls back to the parent fram e.
154 */
155 function extractHostFromFrame(frame)
156 {
157 var host = extractHostFromURL(frame.url);
158 if (!host)
159 {
160 var parentFrame = frame.parent;
161 if (parentFrame)
162 return extractHostFromFrame(parentFrame);
163 }
164 return host;
165 }
166
167 /**
168 * Strips the fragment from a URL.
169 */
170 function stripFragmentFromURL(/**String*/ url)
171 {
172 return url.replace(/#.*/, "");
173 }
OLDNEW
« no previous file with comments | « chrome/ext/background.js ('k') | lib/url.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld