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

Net: Defer reading until listeners could be added #1496

Closed
wants to merge 1 commit into from
Closed
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
25 changes: 17 additions & 8 deletions lib/_tls_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,20 @@ function onocspresponse(resp) {
this.emit('OCSPResponse', resp);
}

function initRead(tls, wrapped) {
// If we were destroyed already don't bother reading
if (!tls._handle)
return;

// Socket already has some buffered data - emulate receiving it
if (wrapped && wrapped._readableState && wrapped._readableState.length) {
var buf;
while ((buf = wrapped.read()) !== null)
tls._handle.receive(buf);
}

tls.read(0);
}

/**
* Provides a wrap of socket stream to do encrypted communication.
Expand Down Expand Up @@ -257,7 +271,9 @@ function TLSSocket(socket, options) {
// starting the flow of the data
this.readable = true;
this.writable = true;
this.read(0);

// Read on next tick so the caller has a chance to setup listeners
process.nextTick(initRead, this, socket);
}
util.inherits(TLSSocket, net.Socket);
exports.TLSSocket = TLSSocket;
Expand Down Expand Up @@ -416,13 +432,6 @@ TLSSocket.prototype._init = function(socket, wrap) {
if (options.handshakeTimeout > 0)
this.setTimeout(options.handshakeTimeout, this._handleTimeout);

// Socket already has some buffered data - emulate receiving it
if (socket && socket._readableState && socket._readableState.length) {
var buf;
while ((buf = socket.read()) !== null)
ssl.receive(buf);
}

if (socket instanceof net.Socket) {
this._parent = socket;

Expand Down
46 changes: 46 additions & 0 deletions test/parallel/test-tls-delayed-attach-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';
var common = require('../common');
Copy link
Contributor

Choose a reason for hiding this comment

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

A 'use strict'; now needs to be added here with the new linter.

var assert = require('assert');

if (!common.hasCrypto) {
console.log('1..0 # Skipped: missing crypto');
process.exit();
}
var tls = require('tls');
var fs = require('fs');
var net = require('net');

var bonkers = new Buffer(1024);
bonkers.fill(42);

var receivedError = false;
var options = {
key: fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem'),
cert: fs.readFileSync(common.fixturesDir + '/keys/agent1-cert.pem')
};

var server = net.createServer(function(c) {
setTimeout(function() {
var s = new tls.TLSSocket(c, {
isServer: true,
secureContext: tls.createSecureContext(options)
});

s.on('_tlsError', function() {
receivedError = true;
});

s.on('close', function() {
server.close();
s.destroy();
});
}, 200);
}).listen(common.PORT, function() {
var c = net.connect({port: common.PORT}, function() {
c.write(bonkers);
});
});

process.on('exit', function() {
assert.ok(receivedError);
});
3 changes: 0 additions & 3 deletions test/parallel/test-tls-delayed-attach.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ var net = require('net');

var sent = 'hello world';
var received = '';
var ended = 0;

Copy link
Member

Choose a reason for hiding this comment

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

Why?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

var options = {
key: fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem'),
Expand All @@ -32,7 +31,6 @@ var server = net.createServer(function(c) {
});

s.on('end', function() {
ended++;
server.close();
s.destroy();
});
Expand All @@ -47,5 +45,4 @@ var server = net.createServer(function(c) {

process.on('exit', function() {
assert.equal(received, sent);
assert.equal(ended, 1);
});