Browse Source

remove auth from host

Fixes #1626
v0.7.4-release
seebees 13 years ago
committed by koichik
parent
commit
1ead20f274
  1. 23
      lib/url.js
  2. 18
      test/simple/test-url.js

23
lib/url.js

@ -169,7 +169,6 @@ function urlParse(url, parseQueryString, slashesDenoteHost) {
// pull out port. // pull out port.
var p = parseHost(out.host); var p = parseHost(out.host);
if (out.auth) out.host = out.auth + '@' + out.host;
var keys = Object.keys(p); var keys = Object.keys(p);
for (var i = 0, l = keys.length; i < l; i++) { for (var i = 0, l = keys.length; i < l; i++) {
var key = keys[i]; var key = keys[i];
@ -235,8 +234,7 @@ function urlParse(url, parseQueryString, slashesDenoteHost) {
} }
out.hostname = newOut.join('.'); out.hostname = newOut.join('.');
out.host = ((out.auth) ? out.auth + '@' : '') + out.host = (out.hostname || '') +
(out.hostname || '') +
((out.port) ? ':' + out.port : ''); ((out.port) ? ':' + out.port : '');
out.href += out.host; out.href += out.host;
} }
@ -314,20 +312,20 @@ function urlFormat(obj) {
// to clean up potentially wonky urls. // to clean up potentially wonky urls.
if (typeof(obj) === 'string') obj = urlParse(obj); if (typeof(obj) === 'string') obj = urlParse(obj);
var auth = obj.auth; var auth = obj.auth || '';
if (auth) { if (auth) {
auth = auth.split('@').join('%40'); auth = auth.split('@').join('%40');
for (var i = 0, l = nonAuthChars.length; i < l; i++) { for (var i = 0, l = nonAuthChars.length; i < l; i++) {
var nAC = nonAuthChars[i]; var nAC = nonAuthChars[i];
auth = auth.split(nAC).join(encodeURIComponent(nAC)); auth = auth.split(nAC).join(encodeURIComponent(nAC));
} }
auth += '@';
} }
var protocol = obj.protocol || '', var protocol = obj.protocol || '',
host = (obj.host !== undefined) ? obj.host : host = (obj.host !== undefined) ? auth + obj.host :
obj.hostname !== undefined ? ( obj.hostname !== undefined ? (
(auth ? auth + '@' : '') + auth + obj.hostname +
obj.hostname +
(obj.port ? ':' + obj.port : '') (obj.port ? ':' + obj.port : '')
) : ) :
false, false,
@ -449,17 +447,14 @@ function urlResolveObject(source, relative) {
if (psychotic) { if (psychotic) {
delete source.hostname; delete source.hostname;
delete source.auth;
delete source.port; delete source.port;
if (source.host) { if (source.host) {
if (srcPath[0] === '') srcPath[0] = source.host; if (srcPath[0] === '') srcPath[0] = source.host;
else srcPath.unshift(source.host); else srcPath.unshift(source.host);
} }
delete source.host; delete source.host;
if (relative.protocol) { if (relative.protocol) {
delete relative.hostname; delete relative.hostname;
delete relative.auth;
delete relative.port; delete relative.port;
if (relative.host) { if (relative.host) {
if (relPath[0] === '') relPath[0] = relative.host; if (relPath[0] === '') relPath[0] = relative.host;
@ -501,7 +496,7 @@ function urlResolveObject(source, relative) {
source.host.split('@') : false; source.host.split('@') : false;
if (authInHost) { if (authInHost) {
source.auth = authInHost.shift(); source.auth = authInHost.shift();
source.hostname = authInHost.shift(); source.host = source.hostname = authInHost.shift();
} }
} }
source.search = relative.search; source.search = relative.search;
@ -580,8 +575,8 @@ function urlResolveObject(source, relative) {
var authInHost = source.host && source.host.indexOf('@') > 0 ? var authInHost = source.host && source.host.indexOf('@') > 0 ?
source.host.split('@') : false; source.host.split('@') : false;
if (authInHost) { if (authInHost) {
relative.auth = authInHost.shift(); source.auth = authInHost.shift();
source.hostname = authInHost.shift(); source.host = source.hostname = authInHost.shift();
} }
} }
@ -597,7 +592,7 @@ function urlResolveObject(source, relative) {
source.path = (source.pathname ? source.pathname : '') + source.path = (source.pathname ? source.pathname : '') +
(source.search ? source.search : ''); (source.search ? source.search : '');
} }
source.auth = relative.auth; source.auth = relative.auth || source.auth;
source.slashes = source.slashes || relative.slashes; source.slashes = source.slashes || relative.slashes;
source.href = urlFormat(source); source.href = urlFormat(source);
return source; return source;

18
test/simple/test-url.js

@ -56,7 +56,7 @@ var parseTests = {
'protocol': 'http:', 'protocol': 'http:',
'slashes': true, 'slashes': true,
'auth': 'user:pw', 'auth': 'user:pw',
'host': 'user:pw@www.example.com', 'host': 'www.example.com',
'hostname': 'www.example.com', 'hostname': 'www.example.com',
'pathname': '/', 'pathname': '/',
'path': '/' 'path': '/'
@ -66,7 +66,7 @@ var parseTests = {
'protocol': 'http:', 'protocol': 'http:',
'slashes': true, 'slashes': true,
'auth': 'USER:PW', 'auth': 'USER:PW',
'host': 'USER:PW@www.example.com', 'host': 'www.example.com',
'hostname': 'www.example.com', 'hostname': 'www.example.com',
'pathname': '/', 'pathname': '/',
'path': '/' 'path': '/'
@ -184,7 +184,7 @@ var parseTests = {
'&hl=en&src=api&x=2&y=2&z=3&s=', '&hl=en&src=api&x=2&y=2&z=3&s=',
'protocol': 'http:', 'protocol': 'http:',
'slashes': true, 'slashes': true,
'host': 'user:pass@mt0.google.com', 'host': 'mt0.google.com',
'auth': 'user:pass', 'auth': 'user:pass',
'hostname': 'mt0.google.com', 'hostname': 'mt0.google.com',
'search': '???&hl=en&src=api&x=2&y=2&z=3&s=', 'search': '???&hl=en&src=api&x=2&y=2&z=3&s=',
@ -256,7 +256,7 @@ var parseTests = {
'href': 'http://user:pass@example.com:8000/foo/bar?baz=quux#frag', 'href': 'http://user:pass@example.com:8000/foo/bar?baz=quux#frag',
'protocol': 'http:', 'protocol': 'http:',
'slashes': true, 'slashes': true,
'host': 'user:pass@example.com:8000', 'host': 'example.com:8000',
'auth': 'user:pass', 'auth': 'user:pass',
'port': '8000', 'port': '8000',
'hostname': 'example.com', 'hostname': 'example.com',
@ -269,7 +269,7 @@ var parseTests = {
'//user:pass@example.com:8000/foo/bar?baz=quux#frag' : { '//user:pass@example.com:8000/foo/bar?baz=quux#frag' : {
'href': '//user:pass@example.com:8000/foo/bar?baz=quux#frag', 'href': '//user:pass@example.com:8000/foo/bar?baz=quux#frag',
'slashes': true, 'slashes': true,
'host': 'user:pass@example.com:8000', 'host': 'example.com:8000',
'auth': 'user:pass', 'auth': 'user:pass',
'port': '8000', 'port': '8000',
'hostname': 'example.com', 'hostname': 'example.com',
@ -299,7 +299,7 @@ var parseTests = {
'mailto:foo@bar.com?subject=hello' : { 'mailto:foo@bar.com?subject=hello' : {
'href': 'mailto:foo@bar.com?subject=hello', 'href': 'mailto:foo@bar.com?subject=hello',
'protocol': 'mailto:', 'protocol': 'mailto:',
'host': 'foo@bar.com', 'host': 'bar.com',
'auth' : 'foo', 'auth' : 'foo',
'hostname' : 'bar.com', 'hostname' : 'bar.com',
'search': '?subject=hello', 'search': '?subject=hello',
@ -315,7 +315,7 @@ var parseTests = {
'xmpp:isaacschlueter@jabber.org' : { 'xmpp:isaacschlueter@jabber.org' : {
'href': 'xmpp:isaacschlueter@jabber.org', 'href': 'xmpp:isaacschlueter@jabber.org',
'protocol': 'xmpp:', 'protocol': 'xmpp:',
'host': 'isaacschlueter@jabber.org', 'host': 'jabber.org',
'auth': 'isaacschlueter', 'auth': 'isaacschlueter',
'hostname': 'jabber.org' 'hostname': 'jabber.org'
}, },
@ -323,7 +323,7 @@ var parseTests = {
'href' : 'http://atpass:foo%40bar@127.0.0.1:8080/path?search=foo#bar', 'href' : 'http://atpass:foo%40bar@127.0.0.1:8080/path?search=foo#bar',
'protocol' : 'http:', 'protocol' : 'http:',
'slashes': true, 'slashes': true,
'host' : 'atpass:foo%40bar@127.0.0.1:8080', 'host' : '127.0.0.1:8080',
'auth' : 'atpass:foo%40bar', 'auth' : 'atpass:foo%40bar',
'hostname' : '127.0.0.1', 'hostname' : '127.0.0.1',
'port' : '8080', 'port' : '8080',
@ -557,7 +557,7 @@ var formatTests = {
'xmpp:isaacschlueter@jabber.org' : { 'xmpp:isaacschlueter@jabber.org' : {
'href': 'xmpp:isaacschlueter@jabber.org', 'href': 'xmpp:isaacschlueter@jabber.org',
'protocol': 'xmpp:', 'protocol': 'xmpp:',
'host': 'isaacschlueter@jabber.org', 'host': 'jabber.org',
'auth': 'isaacschlueter', 'auth': 'isaacschlueter',
'hostname': 'jabber.org' 'hostname': 'jabber.org'
}, },

Loading…
Cancel
Save