From 97f7c0645118ffb3844fec67345ad0ae7e71f6a9 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Thu, 3 Feb 2011 12:17:26 -0800 Subject: [PATCH] TLS: fix throttling Re-enable test-https-large-response.js Closes GH-614. --- lib/tls.js | 42 +++++++++++++++---- .../test-https-large-response.js | 0 test/simple/test-tls-throttle.js | 13 ++++-- 3 files changed, 44 insertions(+), 11 deletions(-) rename test/{disabled => pummel}/test-https-large-response.js (100%) diff --git a/lib/tls.js b/lib/tls.js index b7db6efc48..6204a9cb5f 100644 --- a/lib/tls.js +++ b/lib/tls.js @@ -152,6 +152,9 @@ CryptoStream.prototype.end = function(d) { this._pending.push(END_OF_FILE); this._pendingCallbacks.push(null); + // If this is an encrypted stream then we need to disable further 'data' + // events. + this.writable = false; this.pair._cycle(); @@ -169,6 +172,18 @@ CryptoStream.prototype.destroy = function(err) { }; +CryptoStream.prototype._done = function() { + this._doneFlag = true; + + if (this.pair.cleartext._doneFlag && + this.pair.encrypted._doneFlag && + !this.pair._done) { + // If both streams are done: + this.pair._destroy(); + } +}; + + CryptoStream.prototype.fd = -1; CryptoStream.prototype.__defineGetter__('readyState', net.Socket.prototype.__lookupGetter__('readyState')); @@ -186,6 +201,11 @@ CryptoStream.prototype.__defineGetter__('readyState', // }); // CryptoStream.prototype._push = function() { + if (this == this.pair.encrypted && !this.writable) { + // If the encrypted side got EOF, we do not attempt + // to write out data anymore. + return; + } while (this._writeState == true) { var bytesRead = 0; @@ -211,7 +231,7 @@ CryptoStream.prototype._push = function() { // Bail out if we didn't read any data. if (bytesRead == 0) { if (this._pendingBytes() == 0 && this._destroyAfterPush) { - this.destroy(); + this._done(); } return; } @@ -256,17 +276,23 @@ CryptoStream.prototype._pull = function() { if (tmp === END_OF_FILE) { // Sending EOF - debug('end'); - this.pair._ssl.shutdown(); + if (this === this.pair.encrypted) { + debug('end encrypted'); + this.pair.cleartext._destroyAfterPush = true; + } else { + // CleartextStream + assert(this === this.pair.cleartext); + debug('end cleartext'); - // TODO check if we get EAGAIN From shutdown, would have to do it - // again. should unshift END_OF_FILE back onto pending and wait for - // next cycle. + this.pair._ssl.shutdown(); + // TODO check if we get EAGAIN From shutdown, would have to do it + // again. should unshift END_OF_FILE back onto pending and wait for + // next cycle. + } this.pair.encrypted._destroyAfterPush = true; - this.pair._cycle(); - //this.pair._destroy(); + this._done() return; } diff --git a/test/disabled/test-https-large-response.js b/test/pummel/test-https-large-response.js similarity index 100% rename from test/disabled/test-https-large-response.js rename to test/pummel/test-https-large-response.js diff --git a/test/simple/test-tls-throttle.js b/test/simple/test-tls-throttle.js index 8d009ec966..ff0e5c35aa 100644 --- a/test/simple/test-tls-throttle.js +++ b/test/simple/test-tls-throttle.js @@ -34,6 +34,7 @@ server.listen(common.PORT, function() { var client = tls.connect(common.PORT); client.on('data', function(d) { + process.stdout.write('.'); recvCount += d.length; client.pause(); @@ -44,18 +45,24 @@ server.listen(common.PORT, function() { client.on('close', function() { + console.error('close'); server.close(); clearTimeout(timeout); }); }); -var timeout = setTimeout(function() { - process.exit(1); -}, 10*1000); +function displayCounts() { + console.log('body.length: %d', body.length); + console.log(' recvCount: %d', recvCount); +} + + +var timeout = setTimeout(displayCounts, 10*1000); process.on('exit', function() { + displayCounts(); assert.equal(1, connections); assert.equal(body.length, recvCount); });