2011-03-10 09:54:52 +01:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2011-06-13 14:43:16 +02:00
|
|
|
var punycode = require('punycode');
|
2013-07-26 23:38:08 +02:00
|
|
|
var util = require('util');
|
2011-06-13 14:43:16 +02:00
|
|
|
|
2010-02-22 15:49:14 +01:00
|
|
|
exports.parse = urlParse;
|
|
|
|
exports.resolve = urlResolve;
|
|
|
|
exports.resolveObject = urlResolveObject;
|
|
|
|
exports.format = urlFormat;
|
2010-01-04 08:14:12 +01:00
|
|
|
|
2012-09-13 20:09:54 +02:00
|
|
|
exports.Url = Url;
|
|
|
|
|
|
|
|
function Url() {
|
|
|
|
this.protocol = null;
|
|
|
|
this.slashes = null;
|
|
|
|
this.auth = null;
|
|
|
|
this.host = null;
|
|
|
|
this.port = null;
|
|
|
|
this.hostname = null;
|
|
|
|
this.hash = null;
|
|
|
|
this.search = null;
|
|
|
|
this.query = null;
|
|
|
|
this.pathname = null;
|
|
|
|
this.path = null;
|
2013-06-04 01:02:51 +02:00
|
|
|
this.href = null;
|
2012-09-13 20:09:54 +02:00
|
|
|
}
|
|
|
|
|
2011-04-21 00:44:34 +02:00
|
|
|
// Reference: RFC 3986, RFC 1808, RFC 2396
|
|
|
|
|
2010-12-02 20:34:53 +01:00
|
|
|
// define these here so at least they only have to be
|
|
|
|
// compiled once on the first module load.
|
2011-11-04 13:22:18 +01:00
|
|
|
var protocolPattern = /^([a-z0-9.+-]+:)/i,
|
2012-03-12 19:59:43 +01:00
|
|
|
portPattern = /:[0-9]*$/,
|
2012-05-16 04:06:15 +02:00
|
|
|
|
2011-04-21 00:44:34 +02:00
|
|
|
// RFC 2396: characters reserved for delimiting URLs.
|
2012-05-16 04:06:15 +02:00
|
|
|
// We actually just auto-escape these.
|
2011-04-21 00:44:34 +02:00
|
|
|
delims = ['<', '>', '"', '`', ' ', '\r', '\n', '\t'],
|
2012-05-16 04:06:15 +02:00
|
|
|
|
2011-04-21 00:44:34 +02:00
|
|
|
// RFC 2396: characters not allowed for various reasons.
|
2013-04-13 01:27:42 +02:00
|
|
|
unwise = ['{', '}', '|', '\\', '^', '`'].concat(delims),
|
2012-05-16 04:06:15 +02:00
|
|
|
|
2011-04-21 00:44:34 +02:00
|
|
|
// Allowed by RFCs, but cause of XSS attacks. Always escape these.
|
2013-04-12 20:39:28 +02:00
|
|
|
autoEscape = ['\''].concat(unwise),
|
2011-04-21 00:44:34 +02:00
|
|
|
// Characters that are never ever allowed in a hostname.
|
|
|
|
// Note that any invalid chars are also handled, but these
|
|
|
|
// are the ones that are *expected* to be seen, so we fast-path
|
|
|
|
// them.
|
2013-04-12 20:39:28 +02:00
|
|
|
nonHostChars = ['%', '/', '?', ';', '#'].concat(autoEscape),
|
url: Properly parse certain oddly formed urls
In cases where there are multiple @-chars in a url, Node currently
parses the hostname and auth sections differently than web browsers.
This part of the bug is serious, and should be landed in v0.10, and
also ported to v0.8, and releases made as soon as possible.
The less serious issue is that there are many other sorts of malformed
urls which Node either accepts when it should reject, or interprets
differently than web browsers. For example, `http://a.com*foo` is
interpreted by Node like `http://a.com/*foo` when web browsers treat
this as `http://a.com%3Bfoo/`.
In general, *only* the `hostEndingChars` should be the characters that
delimit the host portion of the URL. Most of the current `nonHostChars`
that appear in the hostname should be escaped, but some of them (such as
`;` and `%` when it does not introduce a hex pair) should raise an
error.
We need to have a broader discussion about whether it's best to throw in
these cases, and potentially break extant programs, or return an object
that has every field set to `null` so that any attempt to read the
hostname/auth/etc. will appear to be empty.
2013-06-03 22:39:57 +02:00
|
|
|
hostEndingChars = ['/', '?', '#'],
|
2011-02-27 19:21:23 +01:00
|
|
|
hostnameMaxLen = 255,
|
2012-10-23 14:06:04 +02:00
|
|
|
hostnamePartPattern = /^[a-z0-9A-Z_-]{0,63}$/,
|
|
|
|
hostnamePartStart = /^([a-z0-9A-Z_-]{0,63})(.*)$/,
|
2011-04-21 00:44:34 +02:00
|
|
|
// protocols that can allow "unsafe" and "unwise" chars.
|
2011-02-27 19:21:23 +01:00
|
|
|
unsafeProtocol = {
|
|
|
|
'javascript': true,
|
|
|
|
'javascript:': true
|
|
|
|
},
|
2011-04-21 00:44:34 +02:00
|
|
|
// protocols that never have a hostname.
|
2010-12-02 20:34:53 +01:00
|
|
|
hostlessProtocol = {
|
2011-02-27 19:21:23 +01:00
|
|
|
'javascript': true,
|
2011-05-20 06:50:35 +02:00
|
|
|
'javascript:': true
|
2010-12-02 20:34:53 +01:00
|
|
|
},
|
2011-04-21 00:44:34 +02:00
|
|
|
// protocols that always contain a // bit.
|
2010-12-02 20:34:53 +01:00
|
|
|
slashedProtocol = {
|
|
|
|
'http': true,
|
|
|
|
'https': true,
|
|
|
|
'ftp': true,
|
|
|
|
'gopher': true,
|
|
|
|
'file': true,
|
|
|
|
'http:': true,
|
|
|
|
'https:': true,
|
|
|
|
'ftp:': true,
|
|
|
|
'gopher:': true,
|
|
|
|
'file:': true
|
|
|
|
},
|
|
|
|
querystring = require('querystring');
|
|
|
|
|
|
|
|
function urlParse(url, parseQueryString, slashesDenoteHost) {
|
2013-07-26 23:38:08 +02:00
|
|
|
if (url && util.isObject(url) && url instanceof Url) return url;
|
2012-09-13 20:09:54 +02:00
|
|
|
|
|
|
|
var u = new Url;
|
|
|
|
u.parse(url, parseQueryString, slashesDenoteHost);
|
|
|
|
return u;
|
|
|
|
}
|
2010-12-02 20:34:53 +01:00
|
|
|
|
2012-09-13 20:09:54 +02:00
|
|
|
Url.prototype.parse = function(url, parseQueryString, slashesDenoteHost) {
|
2013-07-26 23:38:08 +02:00
|
|
|
if (!util.isString(url)) {
|
2011-07-20 22:15:41 +02:00
|
|
|
throw new TypeError("Parameter 'url' must be a string, not " + typeof url);
|
|
|
|
}
|
|
|
|
|
2012-09-13 20:09:54 +02:00
|
|
|
var rest = url;
|
2010-04-11 22:46:24 +02:00
|
|
|
|
2012-05-16 04:06:15 +02:00
|
|
|
// trim before proceeding.
|
|
|
|
// This is to support parse stuff like " http://foo.com \n"
|
|
|
|
rest = rest.trim();
|
2011-04-21 00:44:34 +02:00
|
|
|
|
2010-01-04 08:14:12 +01:00
|
|
|
var proto = protocolPattern.exec(rest);
|
|
|
|
if (proto) {
|
|
|
|
proto = proto[0];
|
2011-04-21 00:44:34 +02:00
|
|
|
var lowerProto = proto.toLowerCase();
|
2012-09-13 20:09:54 +02:00
|
|
|
this.protocol = lowerProto;
|
2010-01-04 08:14:12 +01:00
|
|
|
rest = rest.substr(proto.length);
|
|
|
|
}
|
2010-04-11 22:46:24 +02:00
|
|
|
|
2010-01-04 08:14:12 +01:00
|
|
|
// figure out if it's got a host
|
2010-09-01 02:59:50 +02:00
|
|
|
// user@server is *always* interpreted as a hostname, and url
|
|
|
|
// resolution will treat //foo/bar as host=foo,path=bar because that's
|
|
|
|
// how the browser resolves relative URLs.
|
|
|
|
if (slashesDenoteHost || proto || rest.match(/^\/\/[^@\/]+@[^@\/]+/)) {
|
2010-12-02 20:34:53 +01:00
|
|
|
var slashes = rest.substr(0, 2) === '//';
|
2010-09-01 02:59:50 +02:00
|
|
|
if (slashes && !(proto && hostlessProtocol[proto])) {
|
|
|
|
rest = rest.substr(2);
|
2012-09-13 20:09:54 +02:00
|
|
|
this.slashes = true;
|
2010-09-01 02:59:50 +02:00
|
|
|
}
|
2010-01-04 08:14:12 +01:00
|
|
|
}
|
2011-02-27 19:21:23 +01:00
|
|
|
|
2010-12-02 20:34:53 +01:00
|
|
|
if (!hostlessProtocol[proto] &&
|
|
|
|
(slashes || (proto && !slashedProtocol[proto]))) {
|
url: Properly parse certain oddly formed urls
In cases where there are multiple @-chars in a url, Node currently
parses the hostname and auth sections differently than web browsers.
This part of the bug is serious, and should be landed in v0.10, and
also ported to v0.8, and releases made as soon as possible.
The less serious issue is that there are many other sorts of malformed
urls which Node either accepts when it should reject, or interprets
differently than web browsers. For example, `http://a.com*foo` is
interpreted by Node like `http://a.com/*foo` when web browsers treat
this as `http://a.com%3Bfoo/`.
In general, *only* the `hostEndingChars` should be the characters that
delimit the host portion of the URL. Most of the current `nonHostChars`
that appear in the hostname should be escaped, but some of them (such as
`;` and `%` when it does not introduce a hex pair) should raise an
error.
We need to have a broader discussion about whether it's best to throw in
these cases, and potentially break extant programs, or return an object
that has every field set to `null` so that any attempt to read the
hostname/auth/etc. will appear to be empty.
2013-06-03 22:39:57 +02:00
|
|
|
|
2010-01-04 08:14:12 +01:00
|
|
|
// there's a hostname.
|
|
|
|
// the first instance of /, ?, ;, or # ends the host.
|
url: Properly parse certain oddly formed urls
In cases where there are multiple @-chars in a url, Node currently
parses the hostname and auth sections differently than web browsers.
This part of the bug is serious, and should be landed in v0.10, and
also ported to v0.8, and releases made as soon as possible.
The less serious issue is that there are many other sorts of malformed
urls which Node either accepts when it should reject, or interprets
differently than web browsers. For example, `http://a.com*foo` is
interpreted by Node like `http://a.com/*foo` when web browsers treat
this as `http://a.com%3Bfoo/`.
In general, *only* the `hostEndingChars` should be the characters that
delimit the host portion of the URL. Most of the current `nonHostChars`
that appear in the hostname should be escaped, but some of them (such as
`;` and `%` when it does not introduce a hex pair) should raise an
error.
We need to have a broader discussion about whether it's best to throw in
these cases, and potentially break extant programs, or return an object
that has every field set to `null` so that any attempt to read the
hostname/auth/etc. will appear to be empty.
2013-06-03 22:39:57 +02:00
|
|
|
//
|
2011-05-10 22:42:49 +02:00
|
|
|
// If there is an @ in the hostname, then non-host chars *are* allowed
|
url: Properly parse certain oddly formed urls
In cases where there are multiple @-chars in a url, Node currently
parses the hostname and auth sections differently than web browsers.
This part of the bug is serious, and should be landed in v0.10, and
also ported to v0.8, and releases made as soon as possible.
The less serious issue is that there are many other sorts of malformed
urls which Node either accepts when it should reject, or interprets
differently than web browsers. For example, `http://a.com*foo` is
interpreted by Node like `http://a.com/*foo` when web browsers treat
this as `http://a.com%3Bfoo/`.
In general, *only* the `hostEndingChars` should be the characters that
delimit the host portion of the URL. Most of the current `nonHostChars`
that appear in the hostname should be escaped, but some of them (such as
`;` and `%` when it does not introduce a hex pair) should raise an
error.
We need to have a broader discussion about whether it's best to throw in
these cases, and potentially break extant programs, or return an object
that has every field set to `null` so that any attempt to read the
hostname/auth/etc. will appear to be empty.
2013-06-03 22:39:57 +02:00
|
|
|
// to the left of the last @ sign, unless some host-ending character
|
2011-05-10 22:42:49 +02:00
|
|
|
// comes *before* the @-sign.
|
|
|
|
// URLs are obnoxious.
|
url: Properly parse certain oddly formed urls
In cases where there are multiple @-chars in a url, Node currently
parses the hostname and auth sections differently than web browsers.
This part of the bug is serious, and should be landed in v0.10, and
also ported to v0.8, and releases made as soon as possible.
The less serious issue is that there are many other sorts of malformed
urls which Node either accepts when it should reject, or interprets
differently than web browsers. For example, `http://a.com*foo` is
interpreted by Node like `http://a.com/*foo` when web browsers treat
this as `http://a.com%3Bfoo/`.
In general, *only* the `hostEndingChars` should be the characters that
delimit the host portion of the URL. Most of the current `nonHostChars`
that appear in the hostname should be escaped, but some of them (such as
`;` and `%` when it does not introduce a hex pair) should raise an
error.
We need to have a broader discussion about whether it's best to throw in
these cases, and potentially break extant programs, or return an object
that has every field set to `null` so that any attempt to read the
hostname/auth/etc. will appear to be empty.
2013-06-03 22:39:57 +02:00
|
|
|
//
|
|
|
|
// ex:
|
|
|
|
// http://a@b@c/ => user:a@b host:c
|
|
|
|
// http://a@b?@c => user:a host:c path:/?@c
|
|
|
|
|
|
|
|
// v0.12 TODO(isaacs): This is not quite how Chrome does things.
|
|
|
|
// Review our test case against browsers more comprehensively.
|
|
|
|
|
|
|
|
// find the first instance of any hostEndingChars
|
|
|
|
var hostEnd = -1;
|
|
|
|
for (var i = 0; i < hostEndingChars.length; i++) {
|
|
|
|
var hec = rest.indexOf(hostEndingChars[i]);
|
|
|
|
if (hec !== -1 && (hostEnd === -1 || hec < hostEnd))
|
|
|
|
hostEnd = hec;
|
2011-05-10 22:42:49 +02:00
|
|
|
}
|
|
|
|
|
url: Properly parse certain oddly formed urls
In cases where there are multiple @-chars in a url, Node currently
parses the hostname and auth sections differently than web browsers.
This part of the bug is serious, and should be landed in v0.10, and
also ported to v0.8, and releases made as soon as possible.
The less serious issue is that there are many other sorts of malformed
urls which Node either accepts when it should reject, or interprets
differently than web browsers. For example, `http://a.com*foo` is
interpreted by Node like `http://a.com/*foo` when web browsers treat
this as `http://a.com%3Bfoo/`.
In general, *only* the `hostEndingChars` should be the characters that
delimit the host portion of the URL. Most of the current `nonHostChars`
that appear in the hostname should be escaped, but some of them (such as
`;` and `%` when it does not introduce a hex pair) should raise an
error.
We need to have a broader discussion about whether it's best to throw in
these cases, and potentially break extant programs, or return an object
that has every field set to `null` so that any attempt to read the
hostname/auth/etc. will appear to be empty.
2013-06-03 22:39:57 +02:00
|
|
|
// at this point, either we have an explicit point where the
|
|
|
|
// auth portion cannot go past, or the last @ char is the decider.
|
|
|
|
var auth, atSign;
|
|
|
|
if (hostEnd === -1) {
|
|
|
|
// atSign can be anywhere.
|
|
|
|
atSign = rest.lastIndexOf('@');
|
|
|
|
} else {
|
|
|
|
// atSign must be in auth portion.
|
|
|
|
// http://a@b/c@d => host:b auth:a path:/c@d
|
|
|
|
atSign = rest.lastIndexOf('@', hostEnd);
|
2010-01-04 08:14:12 +01:00
|
|
|
}
|
2011-05-10 22:42:49 +02:00
|
|
|
|
url: Properly parse certain oddly formed urls
In cases where there are multiple @-chars in a url, Node currently
parses the hostname and auth sections differently than web browsers.
This part of the bug is serious, and should be landed in v0.10, and
also ported to v0.8, and releases made as soon as possible.
The less serious issue is that there are many other sorts of malformed
urls which Node either accepts when it should reject, or interprets
differently than web browsers. For example, `http://a.com*foo` is
interpreted by Node like `http://a.com/*foo` when web browsers treat
this as `http://a.com%3Bfoo/`.
In general, *only* the `hostEndingChars` should be the characters that
delimit the host portion of the URL. Most of the current `nonHostChars`
that appear in the hostname should be escaped, but some of them (such as
`;` and `%` when it does not introduce a hex pair) should raise an
error.
We need to have a broader discussion about whether it's best to throw in
these cases, and potentially break extant programs, or return an object
that has every field set to `null` so that any attempt to read the
hostname/auth/etc. will appear to be empty.
2013-06-03 22:39:57 +02:00
|
|
|
// Now we have a portion which is definitely the auth.
|
|
|
|
// Pull that off.
|
|
|
|
if (atSign !== -1) {
|
|
|
|
auth = rest.slice(0, atSign);
|
|
|
|
rest = rest.slice(atSign + 1);
|
|
|
|
this.auth = decodeURIComponent(auth);
|
|
|
|
}
|
|
|
|
|
|
|
|
// the host is the remaining to the left of the first non-host char
|
|
|
|
hostEnd = -1;
|
|
|
|
for (var i = 0; i < nonHostChars.length; i++) {
|
|
|
|
var hec = rest.indexOf(nonHostChars[i]);
|
|
|
|
if (hec !== -1 && (hostEnd === -1 || hec < hostEnd))
|
|
|
|
hostEnd = hec;
|
2010-01-04 08:14:12 +01:00
|
|
|
}
|
url: Properly parse certain oddly formed urls
In cases where there are multiple @-chars in a url, Node currently
parses the hostname and auth sections differently than web browsers.
This part of the bug is serious, and should be landed in v0.10, and
also ported to v0.8, and releases made as soon as possible.
The less serious issue is that there are many other sorts of malformed
urls which Node either accepts when it should reject, or interprets
differently than web browsers. For example, `http://a.com*foo` is
interpreted by Node like `http://a.com/*foo` when web browsers treat
this as `http://a.com%3Bfoo/`.
In general, *only* the `hostEndingChars` should be the characters that
delimit the host portion of the URL. Most of the current `nonHostChars`
that appear in the hostname should be escaped, but some of them (such as
`;` and `%` when it does not introduce a hex pair) should raise an
error.
We need to have a broader discussion about whether it's best to throw in
these cases, and potentially break extant programs, or return an object
that has every field set to `null` so that any attempt to read the
hostname/auth/etc. will appear to be empty.
2013-06-03 22:39:57 +02:00
|
|
|
// if we still have not hit it, then the entire thing is a host.
|
|
|
|
if (hostEnd === -1)
|
|
|
|
hostEnd = rest.length;
|
|
|
|
|
|
|
|
this.host = rest.slice(0, hostEnd);
|
|
|
|
rest = rest.slice(hostEnd);
|
2010-04-11 22:46:24 +02:00
|
|
|
|
2011-05-10 22:42:49 +02:00
|
|
|
// pull out port.
|
2012-09-13 20:09:54 +02:00
|
|
|
this.parseHost();
|
2011-04-21 00:44:34 +02:00
|
|
|
|
2010-12-02 20:34:53 +01:00
|
|
|
// we've indicated that there is a hostname,
|
|
|
|
// so even if it's empty, it has to be present.
|
2012-09-13 20:09:54 +02:00
|
|
|
this.hostname = this.hostname || '';
|
2011-02-27 19:21:23 +01:00
|
|
|
|
2012-01-26 00:12:00 +01:00
|
|
|
// if hostname begins with [ and ends with ]
|
|
|
|
// assume that it's an IPv6 address.
|
2012-09-13 20:09:54 +02:00
|
|
|
var ipv6Hostname = this.hostname[0] === '[' &&
|
|
|
|
this.hostname[this.hostname.length - 1] === ']';
|
2012-01-26 00:12:00 +01:00
|
|
|
|
2011-02-27 19:21:23 +01:00
|
|
|
// validate a little.
|
2012-09-13 20:09:54 +02:00
|
|
|
if (!ipv6Hostname) {
|
|
|
|
var hostparts = this.hostname.split(/\./);
|
2011-02-27 19:21:23 +01:00
|
|
|
for (var i = 0, l = hostparts.length; i < l; i++) {
|
|
|
|
var part = hostparts[i];
|
2011-04-21 00:44:34 +02:00
|
|
|
if (!part) continue;
|
2011-02-27 19:21:23 +01:00
|
|
|
if (!part.match(hostnamePartPattern)) {
|
2011-06-13 14:43:16 +02:00
|
|
|
var newpart = '';
|
|
|
|
for (var j = 0, k = part.length; j < k; j++) {
|
|
|
|
if (part.charCodeAt(j) > 127) {
|
|
|
|
// we replace non-ASCII char with a temporary placeholder
|
|
|
|
// we need this to make sure size of hostname is not
|
|
|
|
// broken by replacing non-ASCII by nothing
|
|
|
|
newpart += 'x';
|
|
|
|
} else {
|
|
|
|
newpart += part[j];
|
|
|
|
}
|
2011-04-21 00:44:34 +02:00
|
|
|
}
|
2011-06-13 14:43:16 +02:00
|
|
|
// we test again with ASCII char only
|
|
|
|
if (!newpart.match(hostnamePartPattern)) {
|
|
|
|
var validParts = hostparts.slice(0, i);
|
|
|
|
var notHost = hostparts.slice(i + 1);
|
|
|
|
var bit = part.match(hostnamePartStart);
|
|
|
|
if (bit) {
|
|
|
|
validParts.push(bit[1]);
|
|
|
|
notHost.unshift(bit[2]);
|
|
|
|
}
|
|
|
|
if (notHost.length) {
|
|
|
|
rest = '/' + notHost.join('.') + rest;
|
|
|
|
}
|
2012-09-13 20:09:54 +02:00
|
|
|
this.hostname = validParts.join('.');
|
2011-06-13 14:43:16 +02:00
|
|
|
break;
|
2011-04-21 00:44:34 +02:00
|
|
|
}
|
2011-02-27 19:21:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-06-13 14:43:16 +02:00
|
|
|
|
2012-09-13 20:09:54 +02:00
|
|
|
if (this.hostname.length > hostnameMaxLen) {
|
|
|
|
this.hostname = '';
|
|
|
|
} else {
|
|
|
|
// hostnames are always lower case.
|
|
|
|
this.hostname = this.hostname.toLowerCase();
|
|
|
|
}
|
2011-04-21 00:44:34 +02:00
|
|
|
|
2012-01-26 00:12:00 +01:00
|
|
|
if (!ipv6Hostname) {
|
|
|
|
// IDNA Support: Returns a puny coded representation of "domain".
|
|
|
|
// It only converts the part of the domain name that
|
|
|
|
// has non ASCII characters. I.e. it dosent matter if
|
|
|
|
// you call it with a domain that already is in ASCII.
|
2012-09-13 20:09:54 +02:00
|
|
|
var domainArray = this.hostname.split('.');
|
2012-01-26 00:12:00 +01:00
|
|
|
var newOut = [];
|
|
|
|
for (var i = 0; i < domainArray.length; ++i) {
|
|
|
|
var s = domainArray[i];
|
|
|
|
newOut.push(s.match(/[^A-Za-z0-9_-]/) ?
|
|
|
|
'xn--' + punycode.encode(s) : s);
|
|
|
|
}
|
2012-09-13 20:09:54 +02:00
|
|
|
this.hostname = newOut.join('.');
|
2011-06-13 14:43:16 +02:00
|
|
|
}
|
|
|
|
|
2012-09-13 20:09:54 +02:00
|
|
|
var p = this.port ? ':' + this.port : '';
|
|
|
|
var h = this.hostname || '';
|
|
|
|
this.host = h + p;
|
|
|
|
this.href += this.host;
|
2012-01-26 00:12:00 +01:00
|
|
|
|
|
|
|
// strip [ and ] from the hostname
|
2012-09-13 20:09:54 +02:00
|
|
|
// the host field still retains them, though
|
2012-01-26 00:12:00 +01:00
|
|
|
if (ipv6Hostname) {
|
2012-09-13 20:09:54 +02:00
|
|
|
this.hostname = this.hostname.substr(1, this.hostname.length - 2);
|
2012-01-26 00:12:00 +01:00
|
|
|
if (rest[0] !== '/') {
|
|
|
|
rest = '/' + rest;
|
|
|
|
}
|
|
|
|
}
|
2010-01-04 08:14:12 +01:00
|
|
|
}
|
2010-04-11 22:46:24 +02:00
|
|
|
|
2010-01-04 08:14:12 +01:00
|
|
|
// now rest is set to the post-host stuff.
|
2011-02-27 19:21:23 +01:00
|
|
|
// chop off any delim chars.
|
2011-04-21 00:44:34 +02:00
|
|
|
if (!unsafeProtocol[lowerProto]) {
|
|
|
|
|
|
|
|
// First, make 100% sure that any "autoEscape" chars get
|
|
|
|
// escaped, even if encodeURIComponent doesn't think they
|
|
|
|
// need to be.
|
|
|
|
for (var i = 0, l = autoEscape.length; i < l; i++) {
|
|
|
|
var ae = autoEscape[i];
|
|
|
|
var esc = encodeURIComponent(ae);
|
|
|
|
if (esc === ae) {
|
|
|
|
esc = escape(ae);
|
|
|
|
}
|
|
|
|
rest = rest.split(ae).join(esc);
|
|
|
|
}
|
2011-02-27 19:21:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-04 08:14:12 +01:00
|
|
|
// chop off from the tail first.
|
2010-12-02 20:34:53 +01:00
|
|
|
var hash = rest.indexOf('#');
|
2010-01-04 08:14:12 +01:00
|
|
|
if (hash !== -1) {
|
|
|
|
// got a fragment string.
|
2012-09-13 20:09:54 +02:00
|
|
|
this.hash = rest.substr(hash);
|
2010-01-04 08:14:12 +01:00
|
|
|
rest = rest.slice(0, hash);
|
|
|
|
}
|
2010-12-02 20:34:53 +01:00
|
|
|
var qm = rest.indexOf('?');
|
2010-01-04 08:14:12 +01:00
|
|
|
if (qm !== -1) {
|
2012-09-13 20:09:54 +02:00
|
|
|
this.search = rest.substr(qm);
|
|
|
|
this.query = rest.substr(qm + 1);
|
2010-01-06 04:03:14 +01:00
|
|
|
if (parseQueryString) {
|
2012-09-13 20:09:54 +02:00
|
|
|
this.query = querystring.parse(this.query);
|
2010-01-06 04:03:14 +01:00
|
|
|
}
|
2010-01-04 08:14:12 +01:00
|
|
|
rest = rest.slice(0, qm);
|
2010-12-20 22:21:02 +01:00
|
|
|
} else if (parseQueryString) {
|
|
|
|
// no query string, but parseQueryString still requested
|
2012-09-13 20:09:54 +02:00
|
|
|
this.search = '';
|
|
|
|
this.query = {};
|
2010-01-04 08:14:12 +01:00
|
|
|
}
|
2012-09-13 20:09:54 +02:00
|
|
|
if (rest) this.pathname = rest;
|
2013-07-17 22:10:09 +02:00
|
|
|
if (slashedProtocol[lowerProto] &&
|
2012-09-13 20:09:54 +02:00
|
|
|
this.hostname && !this.pathname) {
|
|
|
|
this.pathname = '/';
|
2011-02-27 19:21:23 +01:00
|
|
|
}
|
|
|
|
|
2011-09-13 22:55:18 +02:00
|
|
|
//to support http.request
|
2012-09-13 20:09:54 +02:00
|
|
|
if (this.pathname || this.search) {
|
|
|
|
var p = this.pathname || '';
|
|
|
|
var s = this.search || '';
|
|
|
|
this.path = p + s;
|
2011-09-13 22:55:18 +02:00
|
|
|
}
|
|
|
|
|
2011-02-27 19:21:23 +01:00
|
|
|
// finally, reconstruct the href based on what has been validated.
|
2012-09-13 20:09:54 +02:00
|
|
|
this.href = this.format();
|
|
|
|
return this;
|
|
|
|
};
|
2010-01-04 08:14:12 +01:00
|
|
|
|
|
|
|
// format a parsed object into a url string
|
2010-12-02 20:34:53 +01:00
|
|
|
function urlFormat(obj) {
|
|
|
|
// ensure it's an object, and not a string url.
|
|
|
|
// If it's an obj, this is a no-op.
|
|
|
|
// this way, you can call url_format() on strings
|
|
|
|
// to clean up potentially wonky urls.
|
2013-07-26 23:38:08 +02:00
|
|
|
if (util.isString(obj)) obj = urlParse(obj);
|
2012-09-13 20:09:54 +02:00
|
|
|
if (!(obj instanceof Url)) return Url.prototype.format.call(obj);
|
|
|
|
return obj.format();
|
|
|
|
}
|
2010-12-02 20:34:53 +01:00
|
|
|
|
2012-09-13 20:09:54 +02:00
|
|
|
Url.prototype.format = function() {
|
|
|
|
var auth = this.auth || '';
|
2011-05-10 22:42:49 +02:00
|
|
|
if (auth) {
|
2012-02-17 18:08:48 +01:00
|
|
|
auth = encodeURIComponent(auth);
|
|
|
|
auth = auth.replace(/%3A/i, ':');
|
2011-10-15 02:13:16 +02:00
|
|
|
auth += '@';
|
2011-05-10 22:42:49 +02:00
|
|
|
}
|
|
|
|
|
2012-09-13 20:09:54 +02:00
|
|
|
var protocol = this.protocol || '',
|
|
|
|
pathname = this.pathname || '',
|
|
|
|
hash = this.hash || '',
|
2012-01-26 00:12:00 +01:00
|
|
|
host = false,
|
|
|
|
query = '';
|
|
|
|
|
2012-09-13 20:09:54 +02:00
|
|
|
if (this.host) {
|
|
|
|
host = auth + this.host;
|
|
|
|
} else if (this.hostname) {
|
|
|
|
host = auth + (this.hostname.indexOf(':') === -1 ?
|
|
|
|
this.hostname :
|
|
|
|
'[' + this.hostname + ']');
|
|
|
|
if (this.port) {
|
|
|
|
host += ':' + this.port;
|
2012-01-26 00:12:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-26 23:38:08 +02:00
|
|
|
if (this.query &&
|
|
|
|
util.isObject(this.query) &&
|
|
|
|
Object.keys(this.query).length) {
|
2012-09-13 20:09:54 +02:00
|
|
|
query = querystring.stringify(this.query);
|
2012-01-26 00:12:00 +01:00
|
|
|
}
|
|
|
|
|
2012-09-13 20:09:54 +02:00
|
|
|
var search = this.search || (query && ('?' + query)) || '';
|
2010-12-02 20:34:53 +01:00
|
|
|
|
|
|
|
if (protocol && protocol.substr(-1) !== ':') protocol += ':';
|
2010-04-11 22:46:24 +02:00
|
|
|
|
2010-01-04 08:14:12 +01:00
|
|
|
// only the slashedProtocols get the //. Not mailto:, xmpp:, etc.
|
|
|
|
// unless they had them to begin with.
|
2012-09-13 20:09:54 +02:00
|
|
|
if (this.slashes ||
|
2010-12-02 20:34:53 +01:00
|
|
|
(!protocol || slashedProtocol[protocol]) && host !== false) {
|
|
|
|
host = '//' + (host || '');
|
|
|
|
if (pathname && pathname.charAt(0) !== '/') pathname = '/' + pathname;
|
|
|
|
} else if (!host) {
|
|
|
|
host = '';
|
|
|
|
}
|
2010-04-11 22:46:24 +02:00
|
|
|
|
2010-12-02 20:34:53 +01:00
|
|
|
if (hash && hash.charAt(0) !== '#') hash = '#' + hash;
|
|
|
|
if (search && search.charAt(0) !== '?') search = '?' + search;
|
2010-04-11 22:46:24 +02:00
|
|
|
|
2012-10-15 19:07:24 +02:00
|
|
|
pathname = pathname.replace(/[?#]/g, function(match) {
|
|
|
|
return encodeURIComponent(match);
|
|
|
|
});
|
|
|
|
search = search.replace('#', '%23');
|
|
|
|
|
2010-01-04 08:14:12 +01:00
|
|
|
return protocol + host + pathname + search + hash;
|
2012-09-13 20:09:54 +02:00
|
|
|
};
|
2010-01-04 08:14:12 +01:00
|
|
|
|
2010-12-02 20:34:53 +01:00
|
|
|
function urlResolve(source, relative) {
|
2012-09-13 20:09:54 +02:00
|
|
|
return urlParse(source, false, true).resolve(relative);
|
2010-12-02 20:34:53 +01:00
|
|
|
}
|
2010-01-04 08:14:12 +01:00
|
|
|
|
2012-09-13 20:09:54 +02:00
|
|
|
Url.prototype.resolve = function(relative) {
|
|
|
|
return this.resolveObject(urlParse(relative, false, true)).format();
|
|
|
|
};
|
|
|
|
|
2010-12-02 20:34:53 +01:00
|
|
|
function urlResolveObject(source, relative) {
|
2010-01-04 08:14:12 +01:00
|
|
|
if (!source) return relative;
|
2012-09-13 20:09:54 +02:00
|
|
|
return urlParse(source, false, true).resolveObject(relative);
|
|
|
|
}
|
|
|
|
|
|
|
|
Url.prototype.resolveObject = function(relative) {
|
2013-07-26 23:38:08 +02:00
|
|
|
if (util.isString(relative)) {
|
2012-09-13 20:09:54 +02:00
|
|
|
var rel = new Url();
|
|
|
|
rel.parse(relative, false, true);
|
|
|
|
relative = rel;
|
|
|
|
}
|
2010-04-11 22:46:24 +02:00
|
|
|
|
2012-09-13 20:09:54 +02:00
|
|
|
var result = new Url();
|
|
|
|
Object.keys(this).forEach(function(k) {
|
|
|
|
result[k] = this[k];
|
|
|
|
}, this);
|
2010-01-04 08:14:12 +01:00
|
|
|
|
|
|
|
// hash is always overridden, no matter what.
|
2012-09-13 20:09:54 +02:00
|
|
|
// even href="" will remove it.
|
|
|
|
result.hash = relative.hash;
|
2010-04-11 22:46:24 +02:00
|
|
|
|
2012-09-13 20:09:54 +02:00
|
|
|
// if the relative url is empty, then there's nothing left to do here.
|
2011-09-13 22:55:18 +02:00
|
|
|
if (relative.href === '') {
|
2012-09-13 20:09:54 +02:00
|
|
|
result.href = result.format();
|
|
|
|
return result;
|
2011-09-13 22:55:18 +02:00
|
|
|
}
|
2010-04-11 22:46:24 +02:00
|
|
|
|
2010-01-04 08:14:12 +01:00
|
|
|
// hrefs like //foo/bar always cut to the protocol.
|
|
|
|
if (relative.slashes && !relative.protocol) {
|
2012-09-13 20:09:54 +02:00
|
|
|
// take everything except the protocol from relative
|
|
|
|
Object.keys(relative).forEach(function(k) {
|
|
|
|
if (k !== 'protocol')
|
|
|
|
result[k] = relative[k];
|
|
|
|
});
|
|
|
|
|
2011-09-13 22:55:18 +02:00
|
|
|
//urlParse appends trailing / to urls like http://www.example.com
|
2012-09-13 20:09:54 +02:00
|
|
|
if (slashedProtocol[result.protocol] &&
|
|
|
|
result.hostname && !result.pathname) {
|
|
|
|
result.path = result.pathname = '/';
|
2011-09-13 22:55:18 +02:00
|
|
|
}
|
2012-09-13 20:09:54 +02:00
|
|
|
|
|
|
|
result.href = result.format();
|
|
|
|
return result;
|
2010-01-04 08:14:12 +01:00
|
|
|
}
|
2010-04-11 22:46:24 +02:00
|
|
|
|
2012-09-13 20:09:54 +02:00
|
|
|
if (relative.protocol && relative.protocol !== result.protocol) {
|
2010-12-02 20:34:53 +01:00
|
|
|
// if it's a known url protocol, then changing
|
|
|
|
// the protocol does weird things
|
|
|
|
// first, if it's not file:, then we MUST have a host,
|
|
|
|
// and if there was a path
|
2010-01-04 08:14:12 +01:00
|
|
|
// to begin with, then we MUST have a path.
|
2010-12-02 20:34:53 +01:00
|
|
|
// if it is file:, then the host is dropped,
|
|
|
|
// because that's known to be hostless.
|
2010-01-04 08:14:12 +01:00
|
|
|
// anything else is assumed to be absolute.
|
2011-09-13 22:55:18 +02:00
|
|
|
if (!slashedProtocol[relative.protocol]) {
|
2012-09-13 20:09:54 +02:00
|
|
|
Object.keys(relative).forEach(function(k) {
|
|
|
|
result[k] = relative[k];
|
|
|
|
});
|
|
|
|
result.href = result.format();
|
|
|
|
return result;
|
2011-09-13 22:55:18 +02:00
|
|
|
}
|
2012-09-13 20:09:54 +02:00
|
|
|
|
|
|
|
result.protocol = relative.protocol;
|
2010-01-04 08:14:12 +01:00
|
|
|
if (!relative.host && !hostlessProtocol[relative.protocol]) {
|
2010-12-02 20:34:53 +01:00
|
|
|
var relPath = (relative.pathname || '').split('/');
|
2010-01-04 08:14:12 +01:00
|
|
|
while (relPath.length && !(relative.host = relPath.shift()));
|
2010-12-02 20:34:53 +01:00
|
|
|
if (!relative.host) relative.host = '';
|
2011-09-13 22:55:18 +02:00
|
|
|
if (!relative.hostname) relative.hostname = '';
|
2010-12-02 20:34:53 +01:00
|
|
|
if (relPath[0] !== '') relPath.unshift('');
|
|
|
|
if (relPath.length < 2) relPath.unshift('');
|
2012-09-13 20:09:54 +02:00
|
|
|
result.pathname = relPath.join('/');
|
|
|
|
} else {
|
|
|
|
result.pathname = relative.pathname;
|
2010-01-04 08:14:12 +01:00
|
|
|
}
|
2012-09-13 20:09:54 +02:00
|
|
|
result.search = relative.search;
|
|
|
|
result.query = relative.query;
|
|
|
|
result.host = relative.host || '';
|
|
|
|
result.auth = relative.auth;
|
|
|
|
result.hostname = relative.hostname || relative.host;
|
|
|
|
result.port = relative.port;
|
|
|
|
// to support http.request
|
|
|
|
if (result.pathname || result.search) {
|
|
|
|
var p = result.pathname || '';
|
|
|
|
var s = result.search || '';
|
|
|
|
result.path = p + s;
|
2011-09-13 22:55:18 +02:00
|
|
|
}
|
2012-09-13 20:09:54 +02:00
|
|
|
result.slashes = result.slashes || relative.slashes;
|
|
|
|
result.href = result.format();
|
|
|
|
return result;
|
2010-01-04 08:14:12 +01:00
|
|
|
}
|
|
|
|
|
2012-09-13 20:09:54 +02:00
|
|
|
var isSourceAbs = (result.pathname && result.pathname.charAt(0) === '/'),
|
2010-12-02 20:34:53 +01:00
|
|
|
isRelAbs = (
|
2012-09-13 20:09:54 +02:00
|
|
|
relative.host ||
|
2010-12-02 20:34:53 +01:00
|
|
|
relative.pathname && relative.pathname.charAt(0) === '/'
|
|
|
|
),
|
|
|
|
mustEndAbs = (isRelAbs || isSourceAbs ||
|
2012-09-13 20:09:54 +02:00
|
|
|
(result.host && relative.pathname)),
|
2010-12-02 20:34:53 +01:00
|
|
|
removeAllDots = mustEndAbs,
|
2012-09-13 20:09:54 +02:00
|
|
|
srcPath = result.pathname && result.pathname.split('/') || [],
|
2010-12-02 20:34:53 +01:00
|
|
|
relPath = relative.pathname && relative.pathname.split('/') || [],
|
2012-09-13 20:09:54 +02:00
|
|
|
psychotic = result.protocol && !slashedProtocol[result.protocol];
|
2010-12-02 20:34:53 +01:00
|
|
|
|
|
|
|
// if the url is a non-slashed url, then relative
|
|
|
|
// links like ../.. should be able
|
2010-01-04 08:14:12 +01:00
|
|
|
// to crawl up to the hostname, as well. This is strange.
|
2012-09-13 20:09:54 +02:00
|
|
|
// result.protocol has already been set by now.
|
2010-01-04 08:14:12 +01:00
|
|
|
// Later on, put the first path part into the host field.
|
2010-12-02 20:34:53 +01:00
|
|
|
if (psychotic) {
|
2012-09-13 20:09:54 +02:00
|
|
|
result.hostname = '';
|
|
|
|
result.port = null;
|
|
|
|
if (result.host) {
|
|
|
|
if (srcPath[0] === '') srcPath[0] = result.host;
|
|
|
|
else srcPath.unshift(result.host);
|
2010-01-04 08:14:12 +01:00
|
|
|
}
|
2012-09-13 20:09:54 +02:00
|
|
|
result.host = '';
|
2010-01-04 08:14:12 +01:00
|
|
|
if (relative.protocol) {
|
2012-09-13 20:09:54 +02:00
|
|
|
relative.hostname = null;
|
|
|
|
relative.port = null;
|
2010-01-04 08:14:12 +01:00
|
|
|
if (relative.host) {
|
2010-12-02 20:34:53 +01:00
|
|
|
if (relPath[0] === '') relPath[0] = relative.host;
|
2010-01-04 08:14:12 +01:00
|
|
|
else relPath.unshift(relative.host);
|
|
|
|
}
|
2012-09-13 20:09:54 +02:00
|
|
|
relative.host = null;
|
2010-01-04 08:14:12 +01:00
|
|
|
}
|
2010-12-02 20:34:53 +01:00
|
|
|
mustEndAbs = mustEndAbs && (relPath[0] === '' || srcPath[0] === '');
|
2010-01-04 08:14:12 +01:00
|
|
|
}
|
2010-04-11 22:46:24 +02:00
|
|
|
|
2010-01-04 08:14:12 +01:00
|
|
|
if (isRelAbs) {
|
|
|
|
// it's absolute.
|
2012-09-13 20:09:54 +02:00
|
|
|
result.host = (relative.host || relative.host === '') ?
|
|
|
|
relative.host : result.host;
|
|
|
|
result.hostname = (relative.hostname || relative.hostname === '') ?
|
|
|
|
relative.hostname : result.hostname;
|
|
|
|
result.search = relative.search;
|
|
|
|
result.query = relative.query;
|
2010-01-04 08:14:12 +01:00
|
|
|
srcPath = relPath;
|
|
|
|
// fall through to the dot-handling below.
|
|
|
|
} else if (relPath.length) {
|
|
|
|
// it's relative
|
|
|
|
// throw away the existing file, and take the new path instead.
|
|
|
|
if (!srcPath) srcPath = [];
|
|
|
|
srcPath.pop();
|
|
|
|
srcPath = srcPath.concat(relPath);
|
2012-09-13 20:09:54 +02:00
|
|
|
result.search = relative.search;
|
|
|
|
result.query = relative.query;
|
2013-07-26 23:38:08 +02:00
|
|
|
} else if (!util.isNullOrUndefined(relative.search)) {
|
2010-01-04 08:14:12 +01:00
|
|
|
// just pull out the search.
|
2010-12-02 20:34:53 +01:00
|
|
|
// like href='?foo'.
|
2010-01-04 08:14:12 +01:00
|
|
|
// Put this after the other two cases because it simplifies the booleans
|
|
|
|
if (psychotic) {
|
2012-09-13 20:09:54 +02:00
|
|
|
result.hostname = result.host = srcPath.shift();
|
2011-09-13 22:55:18 +02:00
|
|
|
//occationaly the auth can get stuck only in host
|
|
|
|
//this especialy happens in cases like
|
|
|
|
//url.resolveObject('mailto:local1@domain1', 'local2@domain2')
|
2012-09-13 20:09:54 +02:00
|
|
|
var authInHost = result.host && result.host.indexOf('@') > 0 ?
|
|
|
|
result.host.split('@') : false;
|
2011-09-13 22:55:18 +02:00
|
|
|
if (authInHost) {
|
2012-09-13 20:09:54 +02:00
|
|
|
result.auth = authInHost.shift();
|
|
|
|
result.host = result.hostname = authInHost.shift();
|
2011-09-13 22:55:18 +02:00
|
|
|
}
|
2010-01-04 08:14:12 +01:00
|
|
|
}
|
2012-09-13 20:09:54 +02:00
|
|
|
result.search = relative.search;
|
|
|
|
result.query = relative.query;
|
2011-09-13 22:55:18 +02:00
|
|
|
//to support http.request
|
2013-07-26 23:38:08 +02:00
|
|
|
if (!util.isNull(result.pathname) || !util.isNull(result.search)) {
|
2012-09-13 20:09:54 +02:00
|
|
|
result.path = (result.pathname ? result.pathname : '') +
|
|
|
|
(result.search ? result.search : '');
|
2011-09-13 22:55:18 +02:00
|
|
|
}
|
2012-09-13 20:09:54 +02:00
|
|
|
result.href = result.format();
|
|
|
|
return result;
|
2010-01-04 08:14:12 +01:00
|
|
|
}
|
2012-09-13 20:09:54 +02:00
|
|
|
|
2010-01-04 08:14:12 +01:00
|
|
|
if (!srcPath.length) {
|
|
|
|
// no path at all. easy.
|
|
|
|
// we've already handled the other stuff above.
|
2012-09-13 20:09:54 +02:00
|
|
|
result.pathname = null;
|
2011-09-13 22:55:18 +02:00
|
|
|
//to support http.request
|
2012-09-13 20:09:54 +02:00
|
|
|
if (result.search) {
|
|
|
|
result.path = '/' + result.search;
|
2011-09-13 22:55:18 +02:00
|
|
|
} else {
|
2012-09-13 20:09:54 +02:00
|
|
|
result.path = null;
|
2011-09-13 22:55:18 +02:00
|
|
|
}
|
2012-09-13 20:09:54 +02:00
|
|
|
result.href = result.format();
|
|
|
|
return result;
|
2010-01-04 08:14:12 +01:00
|
|
|
}
|
2012-09-13 20:09:54 +02:00
|
|
|
|
2010-01-04 08:14:12 +01:00
|
|
|
// if a url ENDs in . or .., then it must get a trailing slash.
|
2010-12-02 20:34:53 +01:00
|
|
|
// however, if it ends in anything else non-slashy,
|
|
|
|
// then it must NOT get a trailing slash.
|
2010-01-04 08:14:12 +01:00
|
|
|
var last = srcPath.slice(-1)[0];
|
|
|
|
var hasTrailingSlash = (
|
2012-09-13 20:09:54 +02:00
|
|
|
(result.host || relative.host) && (last === '.' || last === '..') ||
|
2010-12-02 20:34:53 +01:00
|
|
|
last === '');
|
2010-04-11 22:46:24 +02:00
|
|
|
|
2011-01-05 16:32:04 +01:00
|
|
|
// strip single dots, resolve double dots to parent dir
|
|
|
|
// if the path tries to go above the root, `up` ends up > 0
|
|
|
|
var up = 0;
|
|
|
|
for (var i = srcPath.length; i >= 0; i--) {
|
|
|
|
last = srcPath[i];
|
|
|
|
if (last == '.') {
|
|
|
|
srcPath.splice(i, 1);
|
|
|
|
} else if (last === '..') {
|
|
|
|
srcPath.splice(i, 1);
|
|
|
|
up++;
|
|
|
|
} else if (up) {
|
|
|
|
srcPath.splice(i, 1);
|
|
|
|
up--;
|
|
|
|
}
|
|
|
|
}
|
2010-04-11 22:46:24 +02:00
|
|
|
|
2011-01-05 16:32:04 +01:00
|
|
|
// if the path is allowed to go above the root, restore leading ..s
|
|
|
|
if (!mustEndAbs && !removeAllDots) {
|
2011-01-07 01:06:27 +01:00
|
|
|
for (; up--; up) {
|
2011-01-05 16:32:04 +01:00
|
|
|
srcPath.unshift('..');
|
2010-01-04 08:14:12 +01:00
|
|
|
}
|
|
|
|
}
|
2011-01-05 16:32:04 +01:00
|
|
|
|
|
|
|
if (mustEndAbs && srcPath[0] !== '' &&
|
|
|
|
(!srcPath[0] || srcPath[0].charAt(0) !== '/')) {
|
|
|
|
srcPath.unshift('');
|
|
|
|
}
|
|
|
|
|
2010-12-02 20:34:53 +01:00
|
|
|
if (hasTrailingSlash && (srcPath.join('/').substr(-1) !== '/')) {
|
|
|
|
srcPath.push('');
|
2010-10-26 23:41:06 +02:00
|
|
|
}
|
|
|
|
|
2010-12-02 20:34:53 +01:00
|
|
|
var isAbsolute = srcPath[0] === '' ||
|
|
|
|
(srcPath[0] && srcPath[0].charAt(0) === '/');
|
2010-04-11 22:46:24 +02:00
|
|
|
|
2010-01-04 08:14:12 +01:00
|
|
|
// put the host back
|
2010-12-02 20:34:53 +01:00
|
|
|
if (psychotic) {
|
2012-09-13 20:09:54 +02:00
|
|
|
result.hostname = result.host = isAbsolute ? '' :
|
2011-09-13 22:55:18 +02:00
|
|
|
srcPath.length ? srcPath.shift() : '';
|
|
|
|
//occationaly the auth can get stuck only in host
|
|
|
|
//this especialy happens in cases like
|
|
|
|
//url.resolveObject('mailto:local1@domain1', 'local2@domain2')
|
2012-09-13 20:09:54 +02:00
|
|
|
var authInHost = result.host && result.host.indexOf('@') > 0 ?
|
|
|
|
result.host.split('@') : false;
|
2011-09-13 22:55:18 +02:00
|
|
|
if (authInHost) {
|
2012-09-13 20:09:54 +02:00
|
|
|
result.auth = authInHost.shift();
|
|
|
|
result.host = result.hostname = authInHost.shift();
|
2011-09-13 22:55:18 +02:00
|
|
|
}
|
2010-10-26 23:41:06 +02:00
|
|
|
}
|
2010-04-11 22:46:24 +02:00
|
|
|
|
2012-09-13 20:09:54 +02:00
|
|
|
mustEndAbs = mustEndAbs || (result.host && srcPath.length);
|
2010-04-11 22:46:24 +02:00
|
|
|
|
2010-10-26 23:41:06 +02:00
|
|
|
if (mustEndAbs && !isAbsolute) {
|
2010-12-02 20:34:53 +01:00
|
|
|
srcPath.unshift('');
|
2010-10-26 23:41:06 +02:00
|
|
|
}
|
2010-01-04 08:14:12 +01:00
|
|
|
|
2012-09-13 20:09:54 +02:00
|
|
|
if (!srcPath.length) {
|
|
|
|
result.pathname = null;
|
|
|
|
result.path = null;
|
|
|
|
} else {
|
|
|
|
result.pathname = srcPath.join('/');
|
2011-09-13 22:55:18 +02:00
|
|
|
}
|
2010-01-04 08:14:12 +01:00
|
|
|
|
2012-09-13 20:09:54 +02:00
|
|
|
//to support request.http
|
2013-07-26 23:38:08 +02:00
|
|
|
if (!util.isNull(result.pathname) || !util.isNull(result.search)) {
|
2012-09-13 20:09:54 +02:00
|
|
|
result.path = (result.pathname ? result.pathname : '') +
|
|
|
|
(result.search ? result.search : '');
|
|
|
|
}
|
|
|
|
result.auth = relative.auth || result.auth;
|
|
|
|
result.slashes = result.slashes || relative.slashes;
|
|
|
|
result.href = result.format();
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
Url.prototype.parseHost = function() {
|
|
|
|
var host = this.host;
|
2010-01-04 08:14:12 +01:00
|
|
|
var port = portPattern.exec(host);
|
|
|
|
if (port) {
|
|
|
|
port = port[0];
|
2012-03-12 19:59:43 +01:00
|
|
|
if (port !== ':') {
|
2012-09-13 20:09:54 +02:00
|
|
|
this.port = port.substr(1);
|
2012-03-12 19:59:43 +01:00
|
|
|
}
|
2010-01-04 08:14:12 +01:00
|
|
|
host = host.substr(0, host.length - port.length);
|
|
|
|
}
|
2012-09-13 20:09:54 +02:00
|
|
|
if (host) this.hostname = host;
|
|
|
|
};
|