summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Hartig <james.hartig@grooveshark.com>2015-03-10 16:48:19 -0400
committercjihrig <cjihrig@gmail.com>2015-03-16 14:35:50 -0400
commit8c38b07252d86f4eef91344f8df4f0c3e38bca35 (patch)
tree264eb154cf607f777364ca95be9b1cbd5d2b0c60
parenta995a6a776f1b2d01946702190c6ff837c338577 (diff)
downloadnode-8c38b07252d86f4eef91344f8df4f0c3e38bca35.tar.gz
net: use cached peername to resolve remote fields
Allows socket.remote* properties to still be accessed even after the socket is closed. Fixes: https://github.com/joyent/node/issues/9287 PR-URL: https://github.com/joyent/node/pull/9366 Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
-rw-r--r--lib/net.js7
-rw-r--r--test/simple/test-net-remote-address-port.js12
2 files changed, 16 insertions, 3 deletions
diff --git a/lib/net.js b/lib/net.js
index 668155bed..2b0afa578 100644
--- a/lib/net.js
+++ b/lib/net.js
@@ -579,10 +579,10 @@ function onread(nread, buffer) {
Socket.prototype._getpeername = function() {
- if (!this._handle || !this._handle.getpeername) {
- return {};
- }
if (!this._peername) {
+ if (!this._handle || !this._handle.getpeername) {
+ return {};
+ }
var out = {};
var err = this._handle.getpeername(out);
if (err) return {}; // FIXME(bnoordhuis) Throw?
@@ -868,6 +868,7 @@ Socket.prototype.connect = function(options, cb) {
this._writableState.errorEmitted = false;
this.destroyed = false;
this._handle = null;
+ this._peername = null;
}
var self = this;
diff --git a/test/simple/test-net-remote-address-port.js b/test/simple/test-net-remote-address-port.js
index 0cfe47afe..9fc53ffe3 100644
--- a/test/simple/test-net-remote-address-port.js
+++ b/test/simple/test-net-remote-address-port.js
@@ -41,6 +41,10 @@ var server = net.createServer(function(socket) {
socket.on('end', function() {
if (++conns_closed == 2) server.close();
});
+ socket.on('close', function() {
+ assert.notEqual(-1, remoteAddrCandidates.indexOf(socket.remoteAddress));
+ assert.notEqual(-1, remoteFamilyCandidates.indexOf(socket.remoteFamily));
+ });
socket.resume();
});
@@ -53,12 +57,20 @@ server.listen(common.PORT, 'localhost', function() {
assert.equal(common.PORT, client.remotePort);
client.end();
});
+ client.on('close', function() {
+ assert.notEqual(-1, remoteAddrCandidates.indexOf(client.remoteAddress));
+ assert.notEqual(-1, remoteFamilyCandidates.indexOf(client.remoteFamily));
+ });
client2.on('connect', function() {
assert.notEqual(-1, remoteAddrCandidates.indexOf(client2.remoteAddress));
assert.notEqual(-1, remoteFamilyCandidates.indexOf(client2.remoteFamily));
assert.equal(common.PORT, client2.remotePort);
client2.end();
});
+ client2.on('close', function() {
+ assert.notEqual(-1, remoteAddrCandidates.indexOf(client2.remoteAddress));
+ assert.notEqual(-1, remoteFamilyCandidates.indexOf(client2.remoteFamily));
+ });
});
process.on('exit', function() {