node/lib/url.js

435 строки
13 KiB
JavaScript
Исходник Обычный вид История

2011-03-10 11:54:52 +03: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.
2010-02-22 17:49:14 +03:00
exports.parse = urlParse;
exports.resolve = urlResolve;
exports.resolveObject = urlResolveObject;
exports.format = urlFormat;
2010-12-02 22:34:53 +03:00
// define these here so at least they only have to be
// compiled once on the first module load.
var protocolPattern = /^([a-z0-9]+:)/,
2010-12-02 22:34:53 +03:00
portPattern = /:[0-9]+$/,
delims = ['<', '>', '"', '\'', '`', /\s/],
unwise = ['{', '}', '|', '\\', '^', '~', '[', ']', '`'].concat(delims),
nonHostChars = ['/', '?', ';', '#'].concat(unwise),
hostnameMaxLen = 255,
hostnamePartPattern = /^[a-z0-9][a-z0-9A-Z-]{0,62}$/,
unsafeProtocol = {
'javascript': true,
'javascript:': true
},
2010-12-02 22:34:53 +03:00
hostlessProtocol = {
'javascript': true,
'javascript:': true,
2010-12-02 22:34:53 +03:00
'file': true,
'file:': true
},
pathedProtocol = {
'http': true,
'https': true,
'ftp': true,
'gopher': true,
'file': true,
'http:': true,
'ftp:': true,
'gopher:': true,
'file:': true
},
2010-12-02 22:34:53 +03: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) {
if (url && typeof(url) === 'object' && url.href) return url;
var out = {},
2010-12-02 22:34:53 +03:00
rest = url;
2010-04-12 00:46:24 +04:00
var proto = protocolPattern.exec(rest);
if (proto) {
proto = proto[0];
out.protocol = proto;
rest = rest.substr(proto.length);
}
2010-04-12 00:46:24 +04:00
// figure out if it's got a host
// 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 22:34:53 +03:00
var slashes = rest.substr(0, 2) === '//';
if (slashes && !(proto && hostlessProtocol[proto])) {
rest = rest.substr(2);
out.slashes = true;
}
}
2010-12-02 22:34:53 +03:00
if (!hostlessProtocol[proto] &&
(slashes || (proto && !slashedProtocol[proto]))) {
// there's a hostname.
// the first instance of /, ?, ;, or # ends the host.
// don't enforce full RFC correctness, just be unstupid about it.
var firstNonHost = -1;
2010-12-02 22:34:53 +03:00
for (var i = 0, l = nonHostChars.length; i < l; i++) {
var index = rest.indexOf(nonHostChars[i]);
2010-12-02 22:34:53 +03:00
if (index !== -1 &&
(firstNonHost < 0 || index < firstNonHost)) firstNonHost = index;
}
if (firstNonHost !== -1) {
out.host = rest.substr(0, firstNonHost);
2010-04-12 00:46:24 +04:00
rest = rest.substr(firstNonHost);
} else {
out.host = rest;
2010-12-02 22:34:53 +03:00
rest = '';
}
2010-04-12 00:46:24 +04:00
// pull out the auth and port.
var p = parseHost(out.host);
var keys = Object.keys(p);
for (var i = 0, l = keys.length; i < l; i++) {
var key = keys[i];
out[key] = p[key];
}
2010-12-02 22:34:53 +03:00
// we've indicated that there is a hostname,
// so even if it's empty, it has to be present.
out.hostname = out.hostname || '';
// validate a little.
if (out.hostname.length > hostnameMaxLen) {
out.hostname = '';
} else {
var hostparts = out.hostname.split(/\./);
for (var i = 0, l = hostparts.length; i < l; i++) {
var part = hostparts[i];
if (!part.match(hostnamePartPattern)) {
out.hostname = '';
break;
}
}
}
}
2010-04-12 00:46:24 +04:00
// now rest is set to the post-host stuff.
// chop off any delim chars.
if (!unsafeProtocol[proto]) {
var chop = rest.length;
for (var i = 0, l = delims.length; i < l; i++) {
var c = rest.indexOf(delims[i]);
if (c !== -1) {
chop = Math.min(c, chop);
}
}
rest = rest.substr(0, chop);
}
// chop off from the tail first.
2010-12-02 22:34:53 +03:00
var hash = rest.indexOf('#');
if (hash !== -1) {
// got a fragment string.
out.hash = rest.substr(hash);
rest = rest.slice(0, hash);
}
2010-12-02 22:34:53 +03:00
var qm = rest.indexOf('?');
if (qm !== -1) {
out.search = rest.substr(qm);
2010-12-02 22:34:53 +03:00
out.query = rest.substr(qm + 1);
if (parseQueryString) {
out.query = querystring.parse(out.query);
}
rest = rest.slice(0, qm);
} else if (parseQueryString) {
// no query string, but parseQueryString still requested
out.search = '';
out.query = {};
}
if (rest) out.pathname = rest;
if (slashedProtocol[proto] &&
out.hostname && !out.pathname) {
out.pathname = '/';
}
// finally, reconstruct the href based on what has been validated.
out.href = urlFormat(out);
2010-04-12 00:46:24 +04:00
return out;
2010-12-02 22:34:53 +03:00
}
// format a parsed object into a url string
2010-12-02 22:34:53 +03: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.
if (typeof(obj) === 'string') obj = urlParse(obj);
var protocol = obj.protocol || '',
host = (obj.host !== undefined) ? obj.host :
obj.hostname !== undefined ? (
(obj.auth ? obj.auth + '@' : '') +
obj.hostname +
(obj.port ? ':' + obj.port : '')
) :
false,
pathname = obj.pathname || '',
query = obj.query &&
((typeof obj.query === 'object' &&
Object.keys(obj.query).length) ?
querystring.stringify(obj.query) :
'') || '',
search = obj.search || (query && ('?' + query)) || '',
2010-12-02 22:34:53 +03:00
hash = obj.hash || '';
if (protocol && protocol.substr(-1) !== ':') protocol += ':';
2010-04-12 00:46:24 +04:00
// only the slashedProtocols get the //. Not mailto:, xmpp:, etc.
// unless they had them to begin with.
2010-12-02 22:34:53 +03:00
if (obj.slashes ||
(!protocol || slashedProtocol[protocol]) && host !== false) {
host = '//' + (host || '');
if (pathname && pathname.charAt(0) !== '/') pathname = '/' + pathname;
} else if (!host) {
host = '';
}
2010-04-12 00:46:24 +04:00
2010-12-02 22:34:53 +03:00
if (hash && hash.charAt(0) !== '#') hash = '#' + hash;
if (search && search.charAt(0) !== '?') search = '?' + search;
2010-04-12 00:46:24 +04:00
return protocol + host + pathname + search + hash;
2010-12-02 22:34:53 +03:00
}
2010-12-02 22:34:53 +03:00
function urlResolve(source, relative) {
2010-02-22 17:49:14 +03:00
return urlFormat(urlResolveObject(source, relative));
2010-12-02 22:34:53 +03:00
}
2010-12-02 22:34:53 +03:00
function urlResolveObject(source, relative) {
if (!source) return relative;
2010-04-12 00:46:24 +04:00
source = urlParse(urlFormat(source), false, true);
relative = urlParse(urlFormat(relative), false, true);
// hash is always overridden, no matter what.
source.hash = relative.hash;
2010-04-12 00:46:24 +04:00
2010-12-02 22:34:53 +03:00
if (relative.href === '') return source;
2010-04-12 00:46:24 +04:00
// hrefs like //foo/bar always cut to the protocol.
if (relative.slashes && !relative.protocol) {
relative.protocol = source.protocol;
return relative;
}
2010-04-12 00:46:24 +04:00
if (relative.protocol && relative.protocol !== source.protocol) {
2010-12-02 22:34:53 +03: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
// to begin with, then we MUST have a path.
2010-12-02 22:34:53 +03:00
// if it is file:, then the host is dropped,
// because that's known to be hostless.
// anything else is assumed to be absolute.
2010-04-12 00:46:24 +04:00
if (!slashedProtocol[relative.protocol]) return relative;
2010-04-12 00:46:24 +04:00
source.protocol = relative.protocol;
if (!relative.host && !hostlessProtocol[relative.protocol]) {
2010-12-02 22:34:53 +03:00
var relPath = (relative.pathname || '').split('/');
while (relPath.length && !(relative.host = relPath.shift()));
2010-12-02 22:34:53 +03:00
if (!relative.host) relative.host = '';
if (relPath[0] !== '') relPath.unshift('');
if (relPath.length < 2) relPath.unshift('');
relative.pathname = relPath.join('/');
}
source.pathname = relative.pathname;
source.search = relative.search;
source.query = relative.query;
2010-12-02 22:34:53 +03:00
source.host = relative.host || '';
delete source.auth;
delete source.hostname;
source.port = relative.port;
return source;
}
2010-12-02 22:34:53 +03:00
var isSourceAbs = (source.pathname && source.pathname.charAt(0) === '/'),
isRelAbs = (
relative.host !== undefined ||
relative.pathname && relative.pathname.charAt(0) === '/'
),
mustEndAbs = (isRelAbs || isSourceAbs ||
(source.host && relative.pathname)),
removeAllDots = mustEndAbs,
srcPath = source.pathname && source.pathname.split('/') || [],
relPath = relative.pathname && relative.pathname.split('/') || [],
psychotic = source.protocol &&
!slashedProtocol[source.protocol] &&
source.host !== undefined;
// if the url is a non-slashed url, then relative
// links like ../.. should be able
// to crawl up to the hostname, as well. This is strange.
// source.protocol has already been set by now.
// Later on, put the first path part into the host field.
2010-12-02 22:34:53 +03:00
if (psychotic) {
2010-04-12 00:46:24 +04:00
delete source.hostname;
delete source.auth;
delete source.port;
if (source.host) {
2010-12-02 22:34:53 +03:00
if (srcPath[0] === '') srcPath[0] = source.host;
else srcPath.unshift(source.host);
}
delete source.host;
2010-04-12 00:46:24 +04:00
if (relative.protocol) {
delete relative.hostname;
delete relative.auth;
delete relative.port;
if (relative.host) {
2010-12-02 22:34:53 +03:00
if (relPath[0] === '') relPath[0] = relative.host;
else relPath.unshift(relative.host);
}
delete relative.host;
}
2010-12-02 22:34:53 +03:00
mustEndAbs = mustEndAbs && (relPath[0] === '' || srcPath[0] === '');
}
2010-04-12 00:46:24 +04:00
if (isRelAbs) {
// it's absolute.
2010-12-02 22:34:53 +03:00
source.host = (relative.host || relative.host === '') ?
relative.host : source.host;
source.search = relative.search;
source.query = relative.query;
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);
source.search = relative.search;
source.query = relative.query;
2010-12-02 22:34:53 +03:00
} else if ('search' in relative) {
// just pull out the search.
2010-12-02 22:34:53 +03:00
// like href='?foo'.
// Put this after the other two cases because it simplifies the booleans
if (psychotic) {
source.host = srcPath.shift();
}
source.search = relative.search;
source.query = relative.query;
return source;
}
if (!srcPath.length) {
// no path at all. easy.
// we've already handled the other stuff above.
delete source.pathname;
return source;
}
2010-04-12 00:46:24 +04:00
// if a url ENDs in . or .., then it must get a trailing slash.
2010-12-02 22:34:53 +03:00
// however, if it ends in anything else non-slashy,
// then it must NOT get a trailing slash.
var last = srcPath.slice(-1)[0];
var hasTrailingSlash = (
2010-12-02 22:34:53 +03:00
(source.host || relative.host) && (last === '.' || last === '..') ||
last === '');
2010-04-12 00:46:24 +04: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-12 00:46:24 +04:00
// if the path is allowed to go above the root, restore leading ..s
if (!mustEndAbs && !removeAllDots) {
2011-01-07 03:06:27 +03:00
for (; up--; up) {
srcPath.unshift('..');
}
}
if (mustEndAbs && srcPath[0] !== '' &&
(!srcPath[0] || srcPath[0].charAt(0) !== '/')) {
srcPath.unshift('');
}
2010-12-02 22:34:53 +03:00
if (hasTrailingSlash && (srcPath.join('/').substr(-1) !== '/')) {
srcPath.push('');
}
2010-12-02 22:34:53 +03:00
var isAbsolute = srcPath[0] === '' ||
(srcPath[0] && srcPath[0].charAt(0) === '/');
2010-04-12 00:46:24 +04:00
// put the host back
2010-12-02 22:34:53 +03:00
if (psychotic) {
source.host = isAbsolute ? '' : srcPath.shift();
}
2010-04-12 00:46:24 +04:00
mustEndAbs = mustEndAbs || (source.host && srcPath.length);
2010-04-12 00:46:24 +04:00
if (mustEndAbs && !isAbsolute) {
2010-12-02 22:34:53 +03:00
srcPath.unshift('');
}
2010-12-02 22:34:53 +03:00
source.pathname = srcPath.join('/');
2010-04-12 00:46:24 +04:00
return source;
2010-12-02 22:34:53 +03:00
}
2010-12-02 22:34:53 +03:00
function parseHost(host) {
var out = {};
2010-12-02 22:34:53 +03:00
var at = host.indexOf('@');
if (at !== -1) {
out.auth = host.substr(0, at);
2010-12-02 22:34:53 +03:00
host = host.substr(at + 1); // drop the @
}
var port = portPattern.exec(host);
if (port) {
port = port[0];
out.port = port.substr(1);
host = host.substr(0, host.length - port.length);
}
if (host) out.hostname = host;
return out;
}