Handle string ports properly in server.listen

Parse port as an integer when possible in server.listen.  In addition to
fixing server.url output, this avoids some weird socket reuse behavior
when mixing string and integer types passed to net.listen.

Fix mcavage/node-ldapjs#196
This commit is contained in:
Patrick Mooney 2014-06-06 12:21:05 -05:00
parent d6780e30cb
commit 5a6e781293
2 changed files with 19 additions and 2 deletions

View File

@ -665,6 +665,10 @@ Server.prototype.listen = function (port, host, callback) {
callback = host;
host = '0.0.0.0';
}
if (typeof (port) === 'string' && /^[0-9]+$/.test(port)) {
// Disambiguate between string ports and file paths
port = parseInt(port, 10);
}
var self = this;
function _callback() {

View File

@ -14,6 +14,8 @@ var BIND_PW = 'secret';
var SUFFIX = 'dc=test';
var SERVER_PORT = process.env.SERVER_PORT || 1389;
var ldap;
var Attribute;
var Change;
@ -44,23 +46,34 @@ test('basic create', function (t) {
});
test('listen on unix/named socket', { timeout: 1000 }, function (t) {
t.plan(1);
server = ldap.createServer();
sock = getSock();
server.listen(sock, function () {
t.ok(true);
server.close();
t.end();
});
});
test('listen on static port', { timeout: 1000 }, function (t) {
server = ldap.createServer();
server.listen(SERVER_PORT, '127.0.0.1', function () {
var addr = server.address();
t.equal(addr.port, parseInt(SERVER_PORT, 10));
t.equals(server.url, 'ldap://127.0.0.1:' + SERVER_PORT);
server.close();
t.end();
});
});
test('listen on ephemeral port', { timeout: 1000 }, function (t) {
t.plan(2);
server = ldap.createServer();
server.listen(0, 'localhost', function () {
var addr = server.address();
t.ok(addr.port > 0);
t.ok(addr.port < 65535);
server.close();
t.end();
});
});