OLD | NEW |
1 /*! | 1 /*! |
2 * Parts of original code from ipv6.js <https://github.com/beaugunderson/javascr
ipt-ipv6> | 2 * Parts of original code from ipv6.js <https://github.com/beaugunderson/javascr
ipt-ipv6> |
3 * Copyright 2011 Beau Gunderson | 3 * Copyright 2011 Beau Gunderson |
4 * Available under MIT license <http://mths.be/mit> | 4 * Available under MIT license <http://mths.be/mit> |
5 */ | 5 */ |
6 | 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; | 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; | 8 const RE_V4_HEX = /^0x([0-9a-f]{8})$/i; |
9 const RE_V4_NUMERIC = /^[0-9]+$/; | 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]?)$/; | 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]?)$/; |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 } | 135 } |
136 | 136 |
137 /** | 137 /** |
138 * Extracts host name from a URL. | 138 * Extracts host name from a URL. |
139 */ | 139 */ |
140 function extractHostFromURL(/**String*/ url) | 140 function extractHostFromURL(/**String*/ url) |
141 { | 141 { |
142 if (url && extractHostFromURL._lastURL == url) | 142 if (url && extractHostFromURL._lastURL == url) |
143 return extractHostFromURL._lastDomain; | 143 return extractHostFromURL._lastDomain; |
144 | 144 |
145 var host = ""; | 145 var host = new URL(url).hostname; |
146 try | |
147 { | |
148 host = new URI(url).host; | |
149 } | |
150 catch (e) | |
151 { | |
152 // Keep the empty string for invalid URIs. | |
153 } | |
154 | |
155 extractHostFromURL._lastURL = url; | 146 extractHostFromURL._lastURL = url; |
156 extractHostFromURL._lastDomain = host; | 147 extractHostFromURL._lastDomain = host; |
157 return host; | 148 return host; |
158 } | 149 } |
159 | 150 |
160 /** | 151 /** |
161 * Extracts host name from the URL of the given frame. If the URL don't have hos
t | 152 * Extracts host name from the URL of the given frame. If the URL don't have hos
t |
162 * information (like about:blank or data: URLs) it falls back to the parent fram
e. | 153 * information (like about:blank or data: URLs) it falls back to the parent fram
e. |
163 */ | 154 */ |
164 function extractHostFromFrame(frame) | 155 function extractHostFromFrame(frame) |
165 { | 156 { |
166 var host = extractHostFromURL(frame.url); | 157 var host = extractHostFromURL(frame.url); |
167 if (!host) | 158 if (!host) |
168 { | 159 { |
169 var parentFrame = frame.parent; | 160 var parentFrame = frame.parent; |
170 if (parentFrame) | 161 if (parentFrame) |
171 return extractHostFromFrame(parentFrame); | 162 return extractHostFromFrame(parentFrame); |
172 } | 163 } |
173 return host; | 164 return host; |
174 } | 165 } |
175 | 166 |
176 /** | 167 /** |
177 * Strips the fragment from a URL. | 168 * Strips the fragment from a URL. |
178 */ | 169 */ |
179 function stripFragmentFromURL(/**String*/ url) | 170 function stripFragmentFromURL(/**String*/ url) |
180 { | 171 { |
181 return url.replace(/#.*/, ""); | 172 return url.replace(/#.*/, ""); |
182 } | 173 } |
183 | |
184 /** | |
185 * Parses URLs and provides an interface similar to nsIURI in Gecko, see | |
186 * https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIURI. | |
187 * TODO: Make sure the parsing actually works the same as nsStandardURL. | |
188 * @constructor | |
189 */ | |
190 function URI(/**String*/ spec) | |
191 { | |
192 this.spec = spec; | |
193 this._schemeEnd = spec.indexOf(":"); | |
194 if (this._schemeEnd < 0) | |
195 throw new Error("Invalid URI scheme"); | |
196 | |
197 if (spec.substr(this._schemeEnd + 1, 2) != "//") | |
198 throw new Error("Unexpected URI structure"); | |
199 | |
200 this._hostPortStart = this._schemeEnd + 3; | |
201 if (this._hostPortStart == spec.length) | |
202 throw new Error("Empty URI host"); | |
203 | |
204 this._hostPortEnd = spec.indexOf("/", this._hostPortStart); | |
205 if (this._hostPortEnd < 0) | |
206 { | |
207 var queryIndex = spec.indexOf("?", this._hostPortStart); | |
208 var fragmentIndex = spec.indexOf("#", this._hostPortStart); | |
209 if (queryIndex >= 0 && fragmentIndex >= 0) | |
210 this._hostPortEnd = Math.min(queryIndex, fragmentIndex); | |
211 else if (queryIndex >= 0) | |
212 this._hostPortEnd = queryIndex; | |
213 else if (fragmentIndex >= 0) | |
214 this._hostPortEnd = fragmentIndex; | |
215 else | |
216 this._hostPortEnd = spec.length; | |
217 } | |
218 | |
219 var authEnd = spec.indexOf("@", this._hostPortStart); | |
220 if (authEnd >= 0 && authEnd < this._hostPortEnd) | |
221 this._hostPortStart = authEnd + 1; | |
222 | |
223 this._portStart = -1; | |
224 this._hostEnd = spec.indexOf("]", this._hostPortStart + 1); | |
225 if (spec[this._hostPortStart] == "[" && this._hostEnd >= 0 && this._hostEnd <
this._hostPortEnd) | |
226 { | |
227 // The host is an IPv6 literal | |
228 this._hostStart = this._hostPortStart + 1; | |
229 if (spec[this._hostEnd + 1] == ":") | |
230 this._portStart = this._hostEnd + 2; | |
231 } | |
232 else | |
233 { | |
234 this._hostStart = this._hostPortStart; | |
235 this._hostEnd = spec.indexOf(":", this._hostStart); | |
236 if (this._hostEnd >= 0 && this._hostEnd < this._hostPortEnd) | |
237 this._portStart = this._hostEnd + 1; | |
238 else | |
239 this._hostEnd = this._hostPortEnd; | |
240 } | |
241 } | |
242 URI.prototype = | |
243 { | |
244 spec: null, | |
245 get scheme() | |
246 { | |
247 return this.spec.substring(0, this._schemeEnd).toLowerCase(); | |
248 }, | |
249 get host() | |
250 { | |
251 return this.spec.substring(this._hostStart, this._hostEnd); | |
252 }, | |
253 get asciiHost() | |
254 { | |
255 var host = this.host; | |
256 if (/^[\x00-\x7F]+$/.test(host)) | |
257 return host; | |
258 else | |
259 return punycode.toASCII(host); | |
260 }, | |
261 get hostPort() | |
262 { | |
263 return this.spec.substring(this._hostPortStart, this._hostPortEnd); | |
264 }, | |
265 get port() | |
266 { | |
267 if (this._portStart < 0) | |
268 return -1; | |
269 else | |
270 return parseInt(this.spec.substring(this._portStart, this._hostPortEnd), 1
0); | |
271 }, | |
272 get path() | |
273 { | |
274 return this.spec.substring(this._hostPortEnd); | |
275 }, | |
276 get prePath() | |
277 { | |
278 return this.spec.substring(0, this._hostPortEnd); | |
279 } | |
280 }; | |
OLD | NEW |