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 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 return (requestHost.substr(requestHost.length - documentDomain.length - 1) !
= "." + documentDomain); | 132 return (requestHost.substr(requestHost.length - documentDomain.length - 1) !
= "." + documentDomain); |
133 else | 133 else |
134 return (requestHost != documentDomain); | 134 return (requestHost != documentDomain); |
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) | 142 if (url && extractHostFromURL._lastURL == url) |
143 return ""; | |
144 | |
145 if (extractHostFromURL._lastURL == url) | |
146 return extractHostFromURL._lastDomain; | 143 return extractHostFromURL._lastDomain; |
147 | 144 |
148 var x = url.substr(url.indexOf("://") + 3); | 145 var host = ""; |
149 x = x.substr(0, x.indexOf("/")); | 146 try |
150 x = x.substr(x.indexOf("@") + 1); | |
151 if (x.indexOf("[") == 0 && x.indexOf("]") > 0) | |
152 { | 147 { |
153 x = x.substring(1,x.indexOf("]")); | 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; |
| 156 extractHostFromURL._lastDomain = host; |
| 157 return host; |
| 158 } |
| 159 |
| 160 /** |
| 161 * Parses URLs and provides an interface similar to nsIURI in Gecko, see |
| 162 * https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIURI. |
| 163 * TODO: Make sure the parsing actually works the same as nsStandardURL. |
| 164 * @constructor |
| 165 */ |
| 166 function URI(/**String*/ spec) |
| 167 { |
| 168 this.spec = spec; |
| 169 this._schemeEnd = spec.indexOf(":"); |
| 170 if (this._schemeEnd < 0) |
| 171 throw new Error("Invalid URI scheme"); |
| 172 |
| 173 if (spec.substr(this._schemeEnd + 1, 2) != "//") |
| 174 throw new Error("Unexpected URI structure"); |
| 175 |
| 176 this._hostPortStart = this._schemeEnd + 3; |
| 177 this._hostPortEnd = spec.indexOf("/", this._hostPortStart); |
| 178 if (this._hostPortEnd < 0) |
| 179 throw new Error("Invalid URI host"); |
| 180 |
| 181 var authEnd = spec.indexOf("@", this._hostPortStart); |
| 182 if (authEnd >= 0 && authEnd < this._hostPortEnd) |
| 183 this._hostPortStart = authEnd + 1; |
| 184 |
| 185 this._portStart = -1; |
| 186 this._hostEnd = spec.indexOf("]", this._hostPortStart + 1); |
| 187 if (spec[this._hostPortStart] == "[" && this._hostEnd >= 0 && this._hostEnd <
this._hostPortEnd) |
| 188 { |
| 189 // The host is an IPv6 literal |
| 190 this._hostStart = this._hostPortStart + 1; |
| 191 if (spec[this._hostEnd + 1] == ":") |
| 192 this._portStart = this._hostEnd + 2; |
154 } | 193 } |
155 else | 194 else |
156 { | 195 { |
157 colPos = x.indexOf(":"); | 196 this._hostStart = this._hostPortStart; |
158 if (colPos >= 0) | 197 this._hostEnd = spec.indexOf(":", this._hostStart); |
159 x = x.substr(0, colPos); | 198 if (this._hostEnd >= 0 && this._hostEnd < this._hostPortEnd) |
| 199 this._portStart = this._hostEnd + 1; |
| 200 else |
| 201 this._hostEnd = this._hostPortEnd; |
160 } | 202 } |
161 | |
162 extractHostFromURL._lastURL = url; | |
163 extractHostFromURL._lastDomain = x; | |
164 return x; | |
165 } | 203 } |
| 204 URI.prototype = |
| 205 { |
| 206 spec: null, |
| 207 get scheme() |
| 208 { |
| 209 return this.spec.substring(0, this._schemeEnd).toLowerCase(); |
| 210 }, |
| 211 get host() |
| 212 { |
| 213 return this.spec.substring(this._hostStart, this._hostEnd); |
| 214 }, |
| 215 get asciiHost() |
| 216 { |
| 217 var host = this.host; |
| 218 if (/^[\x00-\x7F]+$/.test(host)) |
| 219 return host; |
| 220 else |
| 221 return punycode.toASCII(host); |
| 222 }, |
| 223 get hostPort() |
| 224 { |
| 225 return this.spec.substring(this._hostPortStart, this._hostPortEnd); |
| 226 }, |
| 227 get port() |
| 228 { |
| 229 if (this._portStart < 0) |
| 230 return -1; |
| 231 else |
| 232 return parseInt(this.spec.substring(this._portStart, this._hostPortEnd), 1
0); |
| 233 }, |
| 234 get path() |
| 235 { |
| 236 return this.spec.substring(this._hostPortEnd); |
| 237 }, |
| 238 get prePath() |
| 239 { |
| 240 return this.spec.substring(0, this._hostPortEnd); |
| 241 } |
| 242 }; |
OLD | NEW |