From a0c55dfe0968dd48d80383e97b2198346ffb0dce Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Wed, 8 Dec 2010 11:39:57 -0800 Subject: [PATCH] TLS: don't use events when control hasn't been inverted --- lib/tls.js | 44 +++++++++++++---------------------- test/simple/test-tls-server-verify.js | 1 - 2 files changed, 16 insertions(+), 29 deletions(-) diff --git a/lib/tls.js b/lib/tls.js index c7d2a99..025e462 100644 --- a/lib/tls.js +++ b/lib/tls.js @@ -138,33 +138,6 @@ function SecurePair(credentials, isServer, requestCert, rejectUnauthorized) { self._destroy(err); }; - this.cleartext.on('end', function(err) { - debug('clearIn end'); - if (!self._done) { - self._ssl.shutdown(); - self._cycle(); - } - self._destroy(err); - }); - - this.cleartext.on('close', function() { - debug('source close'); - self.emit('close'); - self._destroy(); - }); - - this.cleartext.on('drain', function() { - debug('source drain'); - self._cycle(); - self.encrypted.resume(); - }); - - this.encrypted.on('drain', function() { - debug('target drain'); - self._cycle(); - self.cleartext.resume(); - }); - process.nextTick(function() { self._ssl.start(); self._cycle(); @@ -267,6 +240,7 @@ SecurePair.prototype._cycle = function() { // pair.encrypted.write(d) // }); // + var encPending = this._encInPending.length > 0; while (this._encInPending.length > 0) { tmp = this._encInPending.shift(); @@ -285,11 +259,19 @@ SecurePair.prototype._cycle = function() { assert(rv === tmp.length); } + // If we've cleared all of incoming encrypted data, emit drain. + if (encPending && this._encInPending.length === 0) { + debug('encrypted drain'); + this.encrypted.emit('drain'); + } + + // Pull in any clear data coming from the application. // This arrives via some code like this: // // pair.cleartext.write("hello world"); // + var clearPending = this._clearInPending.length > 0; while (this._clearInPending.length > 0) { tmp = this._clearInPending.shift(); try { @@ -307,6 +289,13 @@ SecurePair.prototype._cycle = function() { assert(rv === tmp.length); } + // If we've cleared all of incoming cleartext data, emit drain. + if (clearPending && this._clearInPending.length === 0) { + debug('cleartext drain'); + this.cleartext.emit('drain'); + } + + // Move decrypted, clear data out into the application. // From the user's perspective this occurs as a 'data' event // on the pair.cleartext. @@ -361,7 +350,6 @@ SecurePair.prototype._destroy = function(err) { this._ssl = null; this.encrypted.emit('close'); this.cleartext.emit('close'); - this.emit('end', err); } this._cycle(); }; diff --git a/test/simple/test-tls-server-verify.js b/test/simple/test-tls-server-verify.js index 2c10b5b..bdfe053 100644 --- a/test/simple/test-tls-server-verify.js +++ b/test/simple/test-tls-server-verify.js @@ -112,7 +112,6 @@ function runClient (options, cb) { // To test use: openssl s_client -connect localhost:8000 var client = spawn('openssl', args); - console.error(args); var out = ''; -- 2.7.4