Fix net.Socket.connect argument parsing

Fixes #1251.
This commit is contained in:
koichik 2011-07-05 00:34:58 +09:00
Родитель bd0baf2338
Коммит c60cdcda4e
3 изменённых файлов: 34 добавлений и 15 удалений

Просмотреть файл

@ -700,9 +700,14 @@ Socket.prototype.connect = function() {
self._connecting = true; // set false in doConnect
self.writable = true;
var lastArg = arguments[arguments.length - 1];
if (typeof lastArg == 'function') {
self.addListener('connect', lastArg);
var host;
if (typeof arguments[1] === 'function') {
self.on('connect', arguments[1]);
} else {
host = arguments[1];
if (typeof arguments[2] === 'function') {
self.on('connect', arguments[2]);
}
}
var port = toPort(arguments[0]);
@ -715,7 +720,7 @@ Socket.prototype.connect = function() {
doConnect(self, arguments[0]);
} else {
// TCP
require('dns').lookup(arguments[1], function(err, ip, addressType) {
require('dns').lookup(host, function(err, ip, addressType) {
if (err) {
self.emit('error', err);
} else {

Просмотреть файл

@ -24,9 +24,9 @@ exports.createServer = function() {
};
exports.connect = exports.createConnection = function(port, host /* [cb] */ ) {
exports.connect = exports.createConnection = function(port /* [host], [cb] */) {
var s = new Socket();
s.connect(port, host, arguments[2]);
s.connect(port, arguments[1], arguments[2]);
return s;
};
@ -340,7 +340,7 @@ function connectip(self, port, ip) {
}
Socket.prototype.connect = function(port, host /* [cb] */) {
Socket.prototype.connect = function(port /* [host], [cb] */) {
var self = this;
if (this.destroyed) {
@ -348,8 +348,14 @@ Socket.prototype.connect = function(port, host /* [cb] */) {
initSocketHandle(this);
}
if (typeof arguments[2] === 'function') {
self.on('connect', arguments[2]);
var host;
if (typeof arguments[1] === 'function') {
self.on('connect', arguments[1]);
} else {
host = arguments[1];
if (typeof arguments[2] === 'function') {
self.on('connect', arguments[2]);
}
}
timers.active(this);

Просмотреть файл

@ -24,19 +24,27 @@ var assert = require('assert');
var net = require('net');
var tcpPort = common.PORT;
var connectHappened = false;
var clientConnected = 0;
var serverConnected = 0;
var server = net.createServer(function(socket) {
server.close();
socket.end();
if (++serverConnected === 4) {
server.close();
}
});
server.listen(tcpPort, 'localhost', function() {
var client = net.createConnection(tcpPort, 'localhost', function() {
connectHappened = true;
});
function cb() {
++clientConnected;
}
net.createConnection(tcpPort).on('connect', cb);
net.createConnection(tcpPort, 'localhost').on('connect', cb);
net.createConnection(tcpPort, cb);
net.createConnection(tcpPort, 'localhost', cb);
});
process.on('exit', function () {
assert.ok(connectHappened);
assert.equal(clientConnected, 4);
});