Skip to content

Commit

Permalink
added _ to the list of valid hostnames
Browse files Browse the repository at this point in the history
  • Loading branch information
vzaidman committed Aug 9, 2017
1 parent 8043e13 commit f7d6bfc
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/URI.js
Expand Up @@ -207,7 +207,7 @@
URI.escapeQuerySpace = true;
// static properties
URI.protocol_expression = /^[a-z][a-z0-9.+-]*$/i;
URI.idn_expression = /[^a-z0-9\.-]/i;
URI.idn_expression = /[^a-z0-9\._-]/i;
URI.punycode_expression = /(xn--)/i;
// well, 333.444.555.666 matches, but it sure ain't no IPv4 - do we care?
URI.ip4_expression = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
Expand Down Expand Up @@ -248,8 +248,8 @@

// allowed hostname characters according to RFC 3986
// ALPHA DIGIT "-" "." "_" "~" "!" "$" "&" "'" "(" ")" "*" "+" "," ";" "=" %encoded
// I've never seen a (non-IDN) hostname other than: ALPHA DIGIT . -
URI.invalid_hostname_characters = /[^a-zA-Z0-9\.\-:]/;
// I've never seen a (non-IDN) hostname other than: ALPHA DIGIT . - _
URI.invalid_hostname_characters = /[^a-zA-Z0-9\.\-:_]/;
// map DOM Elements to their URI attribute
URI.domAttributes = {
'a': 'href',
Expand Down Expand Up @@ -1044,10 +1044,10 @@
} else if (v && v.match(URI.invalid_hostname_characters)) {
// test punycode
if (!punycode) {
throw new TypeError('Hostname "' + v + '" contains characters other than [A-Z0-9.-] and Punycode.js is not available');
throw new TypeError('Hostname "' + v + '" contains characters other than [A-Z0-9.-:_] and Punycode.js is not available');
}
if (punycode.toASCII(v).match(URI.invalid_hostname_characters)) {
throw new TypeError('Hostname "' + v + '" contains characters other than [A-Z0-9.:-]');
throw new TypeError('Hostname "' + v + '" contains characters other than [A-Z0-9.-:_]');
}
}
};
Expand Down
19 changes: 19 additions & 0 deletions test/test.js
Expand Up @@ -243,6 +243,10 @@
equal(u.hostname(), 'abc.foobar.lala', 'hostname changed');
equal(u+'', 'http://abc.foobar.lala/foo.html', 'hostname changed url');

u.hostname('some_where.exa_mple.org');
equal(u.hostname(), 'some_where.exa_mple.org', 'hostname changed');
equal(u+'', 'http://some_where.exa_mple.org/foo.html', 'hostname changed url');

raises(function() {
u.hostname('foo\\bar.com');
}, TypeError, 'Failing backslash detection in hostname');
Expand Down Expand Up @@ -395,6 +399,11 @@
equal(u.port(), '', 'host removed port');
equal(u+'', 'http://some-domain.com/foo.html', 'host modified url');

u.host('some_where.exa_mple.org:44');
equal(u.hostname(), 'some_where.exa_mple.org', 'host modified hostname #2');
equal(u.port(), '44', 'port restored');
equal(u+'', 'http://some_where.exa_mple.org:44/foo.html', 'host modified url #2');

raises(function() {
u.host('foo\\bar.com');
}, TypeError, 'Failing backslash detection in host');
Expand Down Expand Up @@ -524,6 +533,9 @@
equal(u.hostname(), 'foo.example.org', 'changed subdomain foo.');
equal(u+'', 'http://foo.example.org/foo.html', 'changed url foo.');

u.subdomain('foo_bar');
equal(u.hostname(), 'foo_bar.example.org', 'changed subdomain foo_bar');
equal(u+'', 'http://foo_bar.example.org/foo.html', 'changed url foo_bar');
});
test('domain', function() {
var u = new URI('http://www.example.org/foo.html');
Expand Down Expand Up @@ -554,6 +566,13 @@

u.subdomain('foo');
equal(u.href(), 'http://foo.test/', 'subdomain set on (dot-less)');

u.subdomain('bar');
equal(u.href(), 'http://bar.foo.test/', 'subdomain set on foo.test');

u.domain('exam_ple.org');
equal(u.domain(), 'exam_ple.org', 'domain after changed domain exam_ple.org');
equal(u+'', 'http://bar.exam_ple.org/', 'url after changed domain exam_ple.org');
});
test('tld', function() {
var u = new URI('http://www.example.org/foo.html');
Expand Down
49 changes: 49 additions & 0 deletions test/urls.js
Expand Up @@ -1112,6 +1112,55 @@ var urls = [{
idn: false,
punycode: false
}
}, {
// https://github.com/medialize/URI.js/issues/347
name: 'Underscore in domain',
url: 'http://user:pass@some_where.exa_mple.org:123/some/directory/file.html?query=string#fragment',
parts: {
protocol: 'http',
username: 'user',
password: 'pass',
hostname: 'some_where.exa_mple.org',
port: '123',
path: '/some/directory/file.html',
query: 'query=string',
fragment: 'fragment'
},
accessors: {
protocol: 'http',
username: 'user',
password: 'pass',
port: '123',
path: '/some/directory/file.html',
query: 'query=string',
fragment: 'fragment',
resource: '/some/directory/file.html?query=string#fragment',
authority: 'user:pass@some_where.exa_mple.org:123',
origin: 'http://user:pass@some_where.exa_mple.org:123',
userinfo: 'user:pass',
subdomain: 'some_where',
domain: 'exa_mple.org',
tld: 'org',
directory: '/some/directory',
filename: 'file.html',
suffix: 'html',
hash: '#fragment',
search: '?query=string',
host: 'some_where.exa_mple.org:123',
hostname: 'some_where.exa_mple.org'
},
is: {
urn: false,
url: true,
relative: false,
name: true,
sld: false,
ip: false,
ip4: false,
ip6: false,
idn: false,
punycode: false
}
}, {
name: 'IDN (punycode)',
url: 'http://user:pass@xn--exmple-cua.org:123/some/directory/file.html?query=string#fragment',
Expand Down

0 comments on commit f7d6bfc

Please sign in to comment.