Skip to content

Commit

Permalink
socket: fix host parsing for IPv6 URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
lpinca committed May 2, 2015
1 parent 9bbfbec commit f9d1bec
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
21 changes: 14 additions & 7 deletions lib/socket.js
Expand Up @@ -55,14 +55,21 @@ function Socket(uri, opts){
(global.location && 'https:' == location.protocol);

if (opts.host) {
var pieces = opts.host.split(':');
opts.hostname = pieces.shift();
if (pieces.length) {
opts.port = pieces.pop();
} else if (!opts.port) {
// if no port is specified manually, use the protocol default
opts.port = this.secure ? '443' : '80';
var match = opts.host.match(/(\[.+\])(.+)?/)
, pieces;

if (match) {
opts.hostname = match[1];
if (match[2]) opts.port = match[2].slice(1);
} else {
pieces = opts.host.split(':');
opts.hostname = pieces.shift();
if (pieces.length) opts.port = pieces.pop();
}

// if `host` does not include a port and one is not specified manually,
// use the protocol default
if (!opts.port) opts.port = this.secure ? '443' : '80';
}

this.agent = opts.agent || false;
Expand Down
30 changes: 30 additions & 0 deletions test/engine.io-client.js
Expand Up @@ -44,4 +44,34 @@ describe('engine.io-client', function () {
client.close();
});

it('should properly parse an IPv6 host without port (1/2)', function(done) {
var client = eio({ host: '[::1]' });
client.on('close', function() {
done();
});
expect(client.hostname).to.be('[::1]');
expect(client.port).to.be('80');
client.close();
});

it('should properly parse an IPv6 host without port (2/2)', function(done) {
var client = eio({ secure: true, host: '[::1]:' });
client.on('close', function() {
done();
});
expect(client.hostname).to.be('[::1]');
expect(client.port).to.be('443');
client.close();
});

it('should properly parse an IPv6 host with port', function(done) {
var client = eio({ host: '[::1]:8080' });
client.on('close', function() {
done();
});
expect(client.hostname).to.be('[::1]');
expect(client.port).to.be('8080');
client.close();
});

});

0 comments on commit f9d1bec

Please sign in to comment.