| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
| import {toASCII} from 'punycode'; |
| import {isObject,isString,isNullOrUndefined,isNull} from 'util'; |
| import {parse as qsParse,stringify as qsStringify} from 'querystring'; |
| export { |
| urlParse as parse, |
| urlResolve as resolve, |
| urlResolveObject as resolveObject, |
| urlFormat as format |
| }; |
| export default { |
| parse: urlParse, |
| resolve: urlResolve, |
| resolveObject: urlResolveObject, |
| format: urlFormat, |
| Url: Url |
| } |
| export 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; |
| this.href = null; |
| } |
|
|
| |
|
|
| |
| |
| var protocolPattern = /^([a-z0-9.+-]+:)/i, |
| portPattern = /:[0-9]*$/, |
|
|
| |
| simplePathPattern = /^(\/\/?(?!\/)[^\?\s]*)(\?[^\s]*)?$/, |
|
|
| |
| |
| delims = ['<', '>', '"', '`', ' ', '\r', '\n', '\t'], |
|
|
| |
| unwise = ['{', '}', '|', '\\', '^', '`'].concat(delims), |
|
|
| |
| autoEscape = ['\''].concat(unwise), |
| |
| |
| |
| |
| nonHostChars = ['%', '/', '?', ';', '#'].concat(autoEscape), |
| hostEndingChars = ['/', '?', '#'], |
| hostnameMaxLen = 255, |
| hostnamePartPattern = /^[+a-z0-9A-Z_-]{0,63}$/, |
| hostnamePartStart = /^([+a-z0-9A-Z_-]{0,63})(.*)$/, |
| |
| unsafeProtocol = { |
| 'javascript': true, |
| 'javascript:': true |
| }, |
| |
| hostlessProtocol = { |
| 'javascript': true, |
| 'javascript:': true |
| }, |
| |
| slashedProtocol = { |
| 'http': true, |
| 'https': true, |
| 'ftp': true, |
| 'gopher': true, |
| 'file': true, |
| 'http:': true, |
| 'https:': true, |
| 'ftp:': true, |
| 'gopher:': true, |
| 'file:': true |
| }; |
|
|
| function urlParse(url, parseQueryString, slashesDenoteHost) { |
| if (url && isObject(url) && url instanceof Url) return url; |
|
|
| var u = new Url; |
| u.parse(url, parseQueryString, slashesDenoteHost); |
| return u; |
| } |
| Url.prototype.parse = function(url, parseQueryString, slashesDenoteHost) { |
| return parse(this, url, parseQueryString, slashesDenoteHost); |
| } |
|
|
| function parse(self, url, parseQueryString, slashesDenoteHost) { |
| if (!isString(url)) { |
| throw new TypeError('Parameter \'url\' must be a string, not ' + typeof url); |
| } |
|
|
| |
| |
| |
| var queryIndex = url.indexOf('?'), |
| splitter = |
| (queryIndex !== -1 && queryIndex < url.indexOf('#')) ? '?' : '#', |
| uSplit = url.split(splitter), |
| slashRegex = /\\/g; |
| uSplit[0] = uSplit[0].replace(slashRegex, '/'); |
| url = uSplit.join(splitter); |
|
|
| var rest = url; |
|
|
| |
| |
| rest = rest.trim(); |
|
|
| if (!slashesDenoteHost && url.split('#').length === 1) { |
| |
| var simplePath = simplePathPattern.exec(rest); |
| if (simplePath) { |
| self.path = rest; |
| self.href = rest; |
| self.pathname = simplePath[1]; |
| if (simplePath[2]) { |
| self.search = simplePath[2]; |
| if (parseQueryString) { |
| self.query = qsParse(self.search.substr(1)); |
| } else { |
| self.query = self.search.substr(1); |
| } |
| } else if (parseQueryString) { |
| self.search = ''; |
| self.query = {}; |
| } |
| return self; |
| } |
| } |
|
|
| var proto = protocolPattern.exec(rest); |
| if (proto) { |
| proto = proto[0]; |
| var lowerProto = proto.toLowerCase(); |
| self.protocol = lowerProto; |
| rest = rest.substr(proto.length); |
| } |
|
|
| |
| |
| |
| |
| if (slashesDenoteHost || proto || rest.match(/^\/\/[^@\/]+@[^@\/]+/)) { |
| var slashes = rest.substr(0, 2) === '//'; |
| if (slashes && !(proto && hostlessProtocol[proto])) { |
| rest = rest.substr(2); |
| self.slashes = true; |
| } |
| } |
| var i, hec, l, p; |
| if (!hostlessProtocol[proto] && |
| (slashes || (proto && !slashedProtocol[proto]))) { |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
|
|
| |
| var hostEnd = -1; |
| for (i = 0; i < hostEndingChars.length; i++) { |
| hec = rest.indexOf(hostEndingChars[i]); |
| if (hec !== -1 && (hostEnd === -1 || hec < hostEnd)) |
| hostEnd = hec; |
| } |
|
|
| |
| |
| var auth, atSign; |
| if (hostEnd === -1) { |
| |
| atSign = rest.lastIndexOf('@'); |
| } else { |
| |
| |
| atSign = rest.lastIndexOf('@', hostEnd); |
| } |
|
|
| |
| |
| if (atSign !== -1) { |
| auth = rest.slice(0, atSign); |
| rest = rest.slice(atSign + 1); |
| self.auth = decodeURIComponent(auth); |
| } |
|
|
| |
| hostEnd = -1; |
| for (i = 0; i < nonHostChars.length; i++) { |
| hec = rest.indexOf(nonHostChars[i]); |
| if (hec !== -1 && (hostEnd === -1 || hec < hostEnd)) |
| hostEnd = hec; |
| } |
| |
| if (hostEnd === -1) |
| hostEnd = rest.length; |
|
|
| self.host = rest.slice(0, hostEnd); |
| rest = rest.slice(hostEnd); |
|
|
| |
| parseHost(self); |
|
|
| |
| |
| self.hostname = self.hostname || ''; |
|
|
| |
| |
| var ipv6Hostname = self.hostname[0] === '[' && |
| self.hostname[self.hostname.length - 1] === ']'; |
|
|
| |
| if (!ipv6Hostname) { |
| var hostparts = self.hostname.split(/\./); |
| for (i = 0, l = hostparts.length; i < l; i++) { |
| var part = hostparts[i]; |
| if (!part) continue; |
| if (!part.match(hostnamePartPattern)) { |
| var newpart = ''; |
| for (var j = 0, k = part.length; j < k; j++) { |
| if (part.charCodeAt(j) > 127) { |
| |
| |
| |
| newpart += 'x'; |
| } else { |
| newpart += part[j]; |
| } |
| } |
| |
| 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; |
| } |
| self.hostname = validParts.join('.'); |
| break; |
| } |
| } |
| } |
| } |
|
|
| if (self.hostname.length > hostnameMaxLen) { |
| self.hostname = ''; |
| } else { |
| |
| self.hostname = self.hostname.toLowerCase(); |
| } |
|
|
| if (!ipv6Hostname) { |
| |
| |
| |
| |
| self.hostname = toASCII(self.hostname); |
| } |
|
|
| p = self.port ? ':' + self.port : ''; |
| var h = self.hostname || ''; |
| self.host = h + p; |
| self.href += self.host; |
|
|
| |
| |
| if (ipv6Hostname) { |
| self.hostname = self.hostname.substr(1, self.hostname.length - 2); |
| if (rest[0] !== '/') { |
| rest = '/' + rest; |
| } |
| } |
| } |
|
|
| |
| |
| if (!unsafeProtocol[lowerProto]) { |
|
|
| |
| |
| |
| for (i = 0, l = autoEscape.length; i < l; i++) { |
| var ae = autoEscape[i]; |
| if (rest.indexOf(ae) === -1) |
| continue; |
| var esc = encodeURIComponent(ae); |
| if (esc === ae) { |
| esc = escape(ae); |
| } |
| rest = rest.split(ae).join(esc); |
| } |
| } |
|
|
|
|
| |
| var hash = rest.indexOf('#'); |
| if (hash !== -1) { |
| |
| self.hash = rest.substr(hash); |
| rest = rest.slice(0, hash); |
| } |
| var qm = rest.indexOf('?'); |
| if (qm !== -1) { |
| self.search = rest.substr(qm); |
| self.query = rest.substr(qm + 1); |
| if (parseQueryString) { |
| self.query = qsParse(self.query); |
| } |
| rest = rest.slice(0, qm); |
| } else if (parseQueryString) { |
| |
| self.search = ''; |
| self.query = {}; |
| } |
| if (rest) self.pathname = rest; |
| if (slashedProtocol[lowerProto] && |
| self.hostname && !self.pathname) { |
| self.pathname = '/'; |
| } |
|
|
| |
| if (self.pathname || self.search) { |
| p = self.pathname || ''; |
| var s = self.search || ''; |
| self.path = p + s; |
| } |
|
|
| |
| self.href = format(self); |
| return self; |
| } |
|
|
| |
| function urlFormat(obj) { |
| |
| |
| |
| |
| if (isString(obj)) obj = parse({}, obj); |
| return format(obj); |
| } |
|
|
| function format(self) { |
| var auth = self.auth || ''; |
| if (auth) { |
| auth = encodeURIComponent(auth); |
| auth = auth.replace(/%3A/i, ':'); |
| auth += '@'; |
| } |
|
|
| var protocol = self.protocol || '', |
| pathname = self.pathname || '', |
| hash = self.hash || '', |
| host = false, |
| query = ''; |
|
|
| if (self.host) { |
| host = auth + self.host; |
| } else if (self.hostname) { |
| host = auth + (self.hostname.indexOf(':') === -1 ? |
| self.hostname : |
| '[' + this.hostname + ']'); |
| if (self.port) { |
| host += ':' + self.port; |
| } |
| } |
|
|
| if (self.query && |
| isObject(self.query) && |
| Object.keys(self.query).length) { |
| query = qsStringify(self.query); |
| } |
|
|
| var search = self.search || (query && ('?' + query)) || ''; |
|
|
| if (protocol && protocol.substr(-1) !== ':') protocol += ':'; |
|
|
| |
| |
| if (self.slashes || |
| (!protocol || slashedProtocol[protocol]) && host !== false) { |
| host = '//' + (host || ''); |
| if (pathname && pathname.charAt(0) !== '/') pathname = '/' + pathname; |
| } else if (!host) { |
| host = ''; |
| } |
|
|
| if (hash && hash.charAt(0) !== '#') hash = '#' + hash; |
| if (search && search.charAt(0) !== '?') search = '?' + search; |
|
|
| pathname = pathname.replace(/[?#]/g, function(match) { |
| return encodeURIComponent(match); |
| }); |
| search = search.replace('#', '%23'); |
|
|
| return protocol + host + pathname + search + hash; |
| } |
|
|
| Url.prototype.format = function() { |
| return format(this); |
| } |
|
|
| function urlResolve(source, relative) { |
| return urlParse(source, false, true).resolve(relative); |
| } |
|
|
| Url.prototype.resolve = function(relative) { |
| return this.resolveObject(urlParse(relative, false, true)).format(); |
| }; |
|
|
| function urlResolveObject(source, relative) { |
| if (!source) return relative; |
| return urlParse(source, false, true).resolveObject(relative); |
| } |
|
|
| Url.prototype.resolveObject = function(relative) { |
| if (isString(relative)) { |
| var rel = new Url(); |
| rel.parse(relative, false, true); |
| relative = rel; |
| } |
|
|
| var result = new Url(); |
| var tkeys = Object.keys(this); |
| for (var tk = 0; tk < tkeys.length; tk++) { |
| var tkey = tkeys[tk]; |
| result[tkey] = this[tkey]; |
| } |
|
|
| |
| |
| result.hash = relative.hash; |
|
|
| |
| if (relative.href === '') { |
| result.href = result.format(); |
| return result; |
| } |
|
|
| |
| if (relative.slashes && !relative.protocol) { |
| |
| var rkeys = Object.keys(relative); |
| for (var rk = 0; rk < rkeys.length; rk++) { |
| var rkey = rkeys[rk]; |
| if (rkey !== 'protocol') |
| result[rkey] = relative[rkey]; |
| } |
|
|
| |
| if (slashedProtocol[result.protocol] && |
| result.hostname && !result.pathname) { |
| result.path = result.pathname = '/'; |
| } |
|
|
| result.href = result.format(); |
| return result; |
| } |
| var relPath; |
| if (relative.protocol && relative.protocol !== result.protocol) { |
| |
| |
| |
| |
| |
| |
| |
| |
| if (!slashedProtocol[relative.protocol]) { |
| var keys = Object.keys(relative); |
| for (var v = 0; v < keys.length; v++) { |
| var k = keys[v]; |
| result[k] = relative[k]; |
| } |
| result.href = result.format(); |
| return result; |
| } |
|
|
| result.protocol = relative.protocol; |
| if (!relative.host && !hostlessProtocol[relative.protocol]) { |
| relPath = (relative.pathname || '').split('/'); |
| while (relPath.length && !(relative.host = relPath.shift())); |
| if (!relative.host) relative.host = ''; |
| if (!relative.hostname) relative.hostname = ''; |
| if (relPath[0] !== '') relPath.unshift(''); |
| if (relPath.length < 2) relPath.unshift(''); |
| result.pathname = relPath.join('/'); |
| } else { |
| result.pathname = relative.pathname; |
| } |
| 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; |
| |
| if (result.pathname || result.search) { |
| var p = result.pathname || ''; |
| var s = result.search || ''; |
| result.path = p + s; |
| } |
| result.slashes = result.slashes || relative.slashes; |
| result.href = result.format(); |
| return result; |
| } |
|
|
| var isSourceAbs = (result.pathname && result.pathname.charAt(0) === '/'), |
| isRelAbs = ( |
| relative.host || |
| relative.pathname && relative.pathname.charAt(0) === '/' |
| ), |
| mustEndAbs = (isRelAbs || isSourceAbs || |
| (result.host && relative.pathname)), |
| removeAllDots = mustEndAbs, |
| srcPath = result.pathname && result.pathname.split('/') || [], |
| psychotic = result.protocol && !slashedProtocol[result.protocol]; |
| relPath = relative.pathname && relative.pathname.split('/') || []; |
| |
| |
| |
| |
| |
| if (psychotic) { |
| result.hostname = ''; |
| result.port = null; |
| if (result.host) { |
| if (srcPath[0] === '') srcPath[0] = result.host; |
| else srcPath.unshift(result.host); |
| } |
| result.host = ''; |
| if (relative.protocol) { |
| relative.hostname = null; |
| relative.port = null; |
| if (relative.host) { |
| if (relPath[0] === '') relPath[0] = relative.host; |
| else relPath.unshift(relative.host); |
| } |
| relative.host = null; |
| } |
| mustEndAbs = mustEndAbs && (relPath[0] === '' || srcPath[0] === ''); |
| } |
| var authInHost; |
| if (isRelAbs) { |
| |
| 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; |
| srcPath = relPath; |
| |
| } else if (relPath.length) { |
| |
| |
| if (!srcPath) srcPath = []; |
| srcPath.pop(); |
| srcPath = srcPath.concat(relPath); |
| result.search = relative.search; |
| result.query = relative.query; |
| } else if (!isNullOrUndefined(relative.search)) { |
| |
| |
| |
| if (psychotic) { |
| result.hostname = result.host = srcPath.shift(); |
| |
| |
| |
| authInHost = result.host && result.host.indexOf('@') > 0 ? |
| result.host.split('@') : false; |
| if (authInHost) { |
| result.auth = authInHost.shift(); |
| result.host = result.hostname = authInHost.shift(); |
| } |
| } |
| result.search = relative.search; |
| result.query = relative.query; |
| |
| if (!isNull(result.pathname) || !isNull(result.search)) { |
| result.path = (result.pathname ? result.pathname : '') + |
| (result.search ? result.search : ''); |
| } |
| result.href = result.format(); |
| return result; |
| } |
|
|
| if (!srcPath.length) { |
| |
| |
| result.pathname = null; |
| |
| if (result.search) { |
| result.path = '/' + result.search; |
| } else { |
| result.path = null; |
| } |
| result.href = result.format(); |
| return result; |
| } |
|
|
| |
| |
| |
| var last = srcPath.slice(-1)[0]; |
| var hasTrailingSlash = ( |
| (result.host || relative.host || srcPath.length > 1) && |
| (last === '.' || last === '..') || last === ''); |
|
|
| |
| |
| 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--; |
| } |
| } |
|
|
| |
| if (!mustEndAbs && !removeAllDots) { |
| for (; up--; up) { |
| srcPath.unshift('..'); |
| } |
| } |
|
|
| if (mustEndAbs && srcPath[0] !== '' && |
| (!srcPath[0] || srcPath[0].charAt(0) !== '/')) { |
| srcPath.unshift(''); |
| } |
|
|
| if (hasTrailingSlash && (srcPath.join('/').substr(-1) !== '/')) { |
| srcPath.push(''); |
| } |
|
|
| var isAbsolute = srcPath[0] === '' || |
| (srcPath[0] && srcPath[0].charAt(0) === '/'); |
|
|
| |
| if (psychotic) { |
| result.hostname = result.host = isAbsolute ? '' : |
| srcPath.length ? srcPath.shift() : ''; |
| |
| |
| |
| authInHost = result.host && result.host.indexOf('@') > 0 ? |
| result.host.split('@') : false; |
| if (authInHost) { |
| result.auth = authInHost.shift(); |
| result.host = result.hostname = authInHost.shift(); |
| } |
| } |
|
|
| mustEndAbs = mustEndAbs || (result.host && srcPath.length); |
|
|
| if (mustEndAbs && !isAbsolute) { |
| srcPath.unshift(''); |
| } |
|
|
| if (!srcPath.length) { |
| result.pathname = null; |
| result.path = null; |
| } else { |
| result.pathname = srcPath.join('/'); |
| } |
|
|
| |
| if (!isNull(result.pathname) || !isNull(result.search)) { |
| 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() { |
| return parseHost(this); |
| }; |
|
|
| function parseHost(self) { |
| var host = self.host; |
| var port = portPattern.exec(host); |
| if (port) { |
| port = port[0]; |
| if (port !== ':') { |
| self.port = port.substr(1); |
| } |
| host = host.substr(0, host.length - port.length); |
| } |
| if (host) self.hostname = host; |
| } |
|
|