TLS: don't use events when control hasn't been inverted
authorRyan Dahl <ry@tinyclouds.org>
Wed, 8 Dec 2010 19:39:57 +0000 (11:39 -0800)
committerRyan Dahl <ry@tinyclouds.org>
Wed, 8 Dec 2010 19:46:19 +0000 (11:46 -0800)
lib/tls.js
test/simple/test-tls-server-verify.js

index c7d2a99..025e462 100644 (file)
@@ -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();
 };
index 2c10b5b..bdfe053 100644 (file)
@@ -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 = '';