net: cleanup connect logic
Separates out the lookup logic for net.Socket. In the event the `host` property is an IP address, the lookup is skipped. PR-URL: https://github.com/iojs/io.js/pull/1505 Reviewed-By: Chris Dickinson <christopher.s.dickinson@gmail.com> Reviewed-By: Yosuke Furukawa <yosuke.furukawa@gmail.com>
This commit is contained in:
parent
3d3083b91f
commit
1bef717476
31
lib/net.js
31
lib/net.js
@ -881,15 +881,18 @@ Socket.prototype.connect = function(options, cb) {
|
|||||||
connect(self, options.path);
|
connect(self, options.path);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
lookupAndConnect(self, options);
|
||||||
|
}
|
||||||
|
return self;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
function lookupAndConnect(self, options) {
|
||||||
const dns = require('dns');
|
const dns = require('dns');
|
||||||
var host = options.host || 'localhost';
|
var host = options.host || 'localhost';
|
||||||
var port = 0;
|
var port = options.port;
|
||||||
var localAddress = options.localAddress;
|
var localAddress = options.localAddress;
|
||||||
var localPort = options.localPort;
|
var localPort = options.localPort;
|
||||||
var dnsopts = {
|
|
||||||
family: options.family,
|
|
||||||
hints: 0
|
|
||||||
};
|
|
||||||
|
|
||||||
if (localAddress && !exports.isIP(localAddress))
|
if (localAddress && !exports.isIP(localAddress))
|
||||||
throw new TypeError('localAddress must be a valid IP: ' + localAddress);
|
throw new TypeError('localAddress must be a valid IP: ' + localAddress);
|
||||||
@ -897,7 +900,6 @@ Socket.prototype.connect = function(options, cb) {
|
|||||||
if (localPort && typeof localPort !== 'number')
|
if (localPort && typeof localPort !== 'number')
|
||||||
throw new TypeError('localPort should be a number: ' + localPort);
|
throw new TypeError('localPort should be a number: ' + localPort);
|
||||||
|
|
||||||
port = options.port;
|
|
||||||
if (typeof port !== 'undefined') {
|
if (typeof port !== 'undefined') {
|
||||||
if (typeof port !== 'number' && typeof port !== 'string')
|
if (typeof port !== 'number' && typeof port !== 'string')
|
||||||
throw new TypeError('port should be a number or string: ' + port);
|
throw new TypeError('port should be a number or string: ' + port);
|
||||||
@ -906,6 +908,19 @@ Socket.prototype.connect = function(options, cb) {
|
|||||||
}
|
}
|
||||||
port |= 0;
|
port |= 0;
|
||||||
|
|
||||||
|
// If host is an IP, skip performing a lookup
|
||||||
|
// TODO(evanlucas) should we hot path this for localhost?
|
||||||
|
var addressType = exports.isIP(host);
|
||||||
|
if (addressType) {
|
||||||
|
connect(self, host, port, addressType, localAddress, localPort);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var dnsopts = {
|
||||||
|
family: options.family,
|
||||||
|
hints: 0
|
||||||
|
};
|
||||||
|
|
||||||
if (dnsopts.family !== 4 && dnsopts.family !== 6)
|
if (dnsopts.family !== 4 && dnsopts.family !== 6)
|
||||||
dnsopts.hints = dns.ADDRCONFIG | dns.V4MAPPED;
|
dnsopts.hints = dns.ADDRCONFIG | dns.V4MAPPED;
|
||||||
|
|
||||||
@ -936,9 +951,7 @@ Socket.prototype.connect = function(options, cb) {
|
|||||||
localPort);
|
localPort);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return self;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
function connectErrorNT(self, err, options) {
|
function connectErrorNT(self, err, options) {
|
||||||
|
18
test/parallel/test-net-dns-lookup-skip.js
Normal file
18
test/parallel/test-net-dns-lookup-skip.js
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
var common = require('../common');
|
||||||
|
var assert = require('assert');
|
||||||
|
var net = require('net');
|
||||||
|
|
||||||
|
function check(addressType) {
|
||||||
|
var server = net.createServer(function(client) {
|
||||||
|
client.end();
|
||||||
|
server.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
var address = addressType === 4 ? '127.0.0.1' : '::1';
|
||||||
|
server.listen(common.PORT, address, function() {
|
||||||
|
net.connect(common.PORT, address).on('lookup', assert.fail);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
check(4);
|
||||||
|
common.hasIPv6 && check(6);
|
@ -9,7 +9,7 @@ var server = net.createServer(function(client) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
server.listen(common.PORT, '127.0.0.1', function() {
|
server.listen(common.PORT, '127.0.0.1', function() {
|
||||||
net.connect(common.PORT, '127.0.0.1').on('lookup', function(err, ip, type) {
|
net.connect(common.PORT, 'localhost').on('lookup', function(err, ip, type) {
|
||||||
assert.equal(err, null);
|
assert.equal(err, null);
|
||||||
assert.equal(ip, '127.0.0.1');
|
assert.equal(ip, '127.0.0.1');
|
||||||
assert.equal(type, '4');
|
assert.equal(type, '4');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user