Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: allow server.listen({ port: "1234" }) #1116

Merged
merged 1 commit into from
Mar 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,15 @@ function connect(self, address, port, addressType, localAddress, localPort) {
}


// Check that the port number is not NaN when coerced to a number,
// is an integer and that it falls within the legal range of port numbers.
function isLegalPort(port) {
if (typeof port === 'string' && port.trim() === '')
return false;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cjihrig What was the motivation for disallowing an empty string?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty string and strings of 1 or more spaces become 0 when the + operator is applied. Since we want to allow zero, I had to specifically check for that case.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, that much I get. It's not really clear to me though why undefined is allowed (and interpreted as 0) but not a blank string.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@misterdjules and I had a fair amount of discussion about this. undefined is allowed because the argument is optional and needs a default value. The empty string means you are explicitly passing in an "incorrect" value. I actually wasn't the one to add the original checks. However, I moved the validation, so I sort of took ownership of it when the bug (port zero was no longer allowed) was reported against 0.12.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, thanks for the explanation. It feels kind of quirky but I don't have a strong enough opinion to file a PR for it. :-)

return +port === (port >>> 0) && port >= 0 && port <= 0xFFFF;
}


Socket.prototype.connect = function(options, cb) {
if (this.write !== Socket.prototype.write)
this.write = Socket.prototype.write;
Expand Down Expand Up @@ -896,16 +905,14 @@ Socket.prototype.connect = function(options, cb) {
if (localPort && typeof localPort !== 'number')
throw new TypeError('localPort should be a number: ' + localPort);

if (typeof options.port === 'number')
port = options.port;
else if (typeof options.port === 'string')
port = options.port.trim() === '' ? -1 : +options.port;
else if (options.port !== undefined)
throw new TypeError('port should be a number or string: ' + options.port);

if (port < 0 || port > 65535 || isNaN(port))
throw new RangeError('port should be >= 0 and < 65536: ' +
options.port);
port = options.port;
if (typeof port !== 'undefined') {
if (typeof port !== 'number' && typeof port !== 'string')
throw new TypeError('port should be a number or string: ' + port);
if (!isLegalPort(port))
throw new RangeError('port should be >= 0 and < 65536: ' + port);
}
port |= 0;

if (dnsopts.family !== 4 && dnsopts.family !== 6)
dnsopts.hints = dns.ADDRCONFIG | dns.V4MAPPED;
Expand Down Expand Up @@ -1266,11 +1273,16 @@ Server.prototype.listen = function() {
if (h.backlog)
backlog = h.backlog;

if (typeof h.port === 'number') {
if (typeof h.port === 'number' || typeof h.port === 'string' ||
(typeof h.port === 'undefined' && 'port' in h)) {
// Undefined is interpreted as zero (random port) for consistency
// with net.connect().
if (typeof h.port !== 'undefined' && !isLegalPort(h.port))
throw new RangeError('port should be >= 0 and < 65536: ' + h.port);
if (h.host)
listenAfterLookup(h.port, h.host, backlog, h.exclusive);
listenAfterLookup(h.port | 0, h.host, backlog, h.exclusive);
else
listen(self, null, h.port, 4, backlog, undefined, h.exclusive);
listen(self, null, h.port | 0, 4, backlog, undefined, h.exclusive);
} else if (h.path && isPipeName(h.path)) {
var pipeName = self._pipeName = h.path;
listen(self, pipeName, -1, -1, backlog, undefined, h.exclusive);
Expand Down
26 changes: 26 additions & 0 deletions test/parallel/test-net-listen-port-option.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var common = require('../common');
var assert = require('assert');
var net = require('net');

function close() { this.close(); }
net.Server().listen({ port: undefined }, close);
net.Server().listen({ port: '' + common.PORT }, close);

[ 'nan',
-1,
123.456,
0x10000,
1 / 0,
-1 / 0,
'+Infinity',
'-Infinity' ].forEach(function(port) {
assert.throws(function() {
net.Server().listen({ port: port }, assert.fail);
}, /port should be >= 0 and < 65536/i);
});

[null, true, false].forEach(function(port) {
assert.throws(function() {
net.Server().listen({ port: port }, assert.fail);
}, /invalid listen argument/i);
});