lib: move default address logic to net._listen2
When address is not provided to `server.listen()`, `_connectionKey` and error messages should include actual address and correct family. PR-URL: https://github.com/iojs/io.js/pull/539 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
This commit is contained in:
parent
3143d732f6
commit
8de89ec465
26
lib/net.js
26
lib/net.js
@ -1110,9 +1110,29 @@ Server.prototype._listen2 = function(address, port, addressType, backlog, fd) {
|
|||||||
|
|
||||||
// If there is not yet a handle, we need to create one and bind.
|
// If there is not yet a handle, we need to create one and bind.
|
||||||
// In the case of a server sent via IPC, we don't need to do this.
|
// In the case of a server sent via IPC, we don't need to do this.
|
||||||
if (!self._handle) {
|
if (self._handle) {
|
||||||
|
debug('_listen2: have a handle already');
|
||||||
|
} else {
|
||||||
debug('_listen2: create a handle');
|
debug('_listen2: create a handle');
|
||||||
var rval = createServerHandle(address, port, addressType, fd);
|
|
||||||
|
var rval = null;
|
||||||
|
|
||||||
|
if (!address && !util.isNumber(fd)) {
|
||||||
|
rval = createServerHandle('::', port, 6, fd);
|
||||||
|
|
||||||
|
if (util.isNumber(rval)) {
|
||||||
|
rval = null;
|
||||||
|
address = '0.0.0.0';
|
||||||
|
addressType = 4;
|
||||||
|
} else {
|
||||||
|
address = '::';
|
||||||
|
addressType = 6;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rval === null)
|
||||||
|
rval = createServerHandle(address, port, addressType, fd);
|
||||||
|
|
||||||
if (util.isNumber(rval)) {
|
if (util.isNumber(rval)) {
|
||||||
var error = exceptionWithHostPort(rval, 'listen', address, port);
|
var error = exceptionWithHostPort(rval, 'listen', address, port);
|
||||||
process.nextTick(function() {
|
process.nextTick(function() {
|
||||||
@ -1121,8 +1141,6 @@ Server.prototype._listen2 = function(address, port, addressType, backlog, fd) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
self._handle = rval;
|
self._handle = rval;
|
||||||
} else {
|
|
||||||
debug('_listen2: have a handle already');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
self._handle.onconnection = onconnection;
|
self._handle.onconnection = onconnection;
|
||||||
|
@ -18,12 +18,14 @@ server0.listen(function() {
|
|||||||
// No callback to listen(), assume we can bind in 100 ms
|
// No callback to listen(), assume we can bind in 100 ms
|
||||||
|
|
||||||
var address1;
|
var address1;
|
||||||
|
var connectionKey1;
|
||||||
var server1 = net.createServer(function(socket) { });
|
var server1 = net.createServer(function(socket) { });
|
||||||
|
|
||||||
server1.listen(common.PORT);
|
server1.listen(common.PORT);
|
||||||
|
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
address1 = server1.address();
|
address1 = server1.address();
|
||||||
|
connectionKey1 = server1._connectionKey;
|
||||||
console.log('address1 %j', address1);
|
console.log('address1 %j', address1);
|
||||||
server1.close();
|
server1.close();
|
||||||
}, 100);
|
}, 100);
|
||||||
@ -68,6 +70,15 @@ server4.listen(common.PORT + 3, 127, function() {
|
|||||||
process.on('exit', function() {
|
process.on('exit', function() {
|
||||||
assert.ok(address0.port > 100);
|
assert.ok(address0.port > 100);
|
||||||
assert.equal(common.PORT, address1.port);
|
assert.equal(common.PORT, address1.port);
|
||||||
|
|
||||||
|
var expectedConnectionKey1;
|
||||||
|
|
||||||
|
if (address1.family === 'IPv6')
|
||||||
|
expectedConnectionKey1 = '6::::' + address1.port;
|
||||||
|
else
|
||||||
|
expectedConnectionKey1 = '4:0.0.0.0:' + address1.port;
|
||||||
|
|
||||||
|
assert.equal(connectionKey1, expectedConnectionKey1);
|
||||||
assert.equal(common.PORT + 1, address2.port);
|
assert.equal(common.PORT + 1, address2.port);
|
||||||
assert.equal(common.PORT + 2, address3.port);
|
assert.equal(common.PORT + 2, address3.port);
|
||||||
assert.equal(common.PORT + 3, address4.port);
|
assert.equal(common.PORT + 3, address4.port);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user