Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
tls: only emit data after 'secure' event
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Feb 9, 2011
1 parent 38d8cd6 commit 9de5043
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions lib/tls.js
Expand Up @@ -8,7 +8,7 @@ var assert = require('assert').ok;

var debug;
if (process.env.NODE_DEBUG && /tls/.test(process.env.NODE_DEBUG)) {
debug = function() { util.error.apply(this, arguments); };
debug = function(a) { console.error("TLS: ", a); };
} else {
debug = function() { };
}
Expand Down Expand Up @@ -38,6 +38,7 @@ util.inherits(CryptoStream, stream.Stream);


CryptoStream.prototype.write = function(data /* , encoding, cb */) {
debug('CryptoStream.prototype.write called with <<<<' + data.toString() + '>>>');
if (!this.writable) {
throw new Error('CryptoStream is not writable');
}
Expand Down Expand Up @@ -69,13 +70,13 @@ CryptoStream.prototype.write = function(data /* , encoding, cb */) {


CryptoStream.prototype.pause = function() {
debug('paused cleartext');
debug('paused ' + (this == this.pair.cleartext ? 'cleartext' : 'encrypted'));
this._writeState = false;
};


CryptoStream.prototype.resume = function() {
debug('resumed cleartext');
debug('resume ' + (this == this.pair.cleartext ? 'cleartext' : 'encrypted'));
this._writeState = true;
this.pair._cycle();
};
Expand Down Expand Up @@ -220,6 +221,8 @@ CryptoStream.prototype._push = function() {
return;
}

this.pair._maybeInitFinished();

if (chunkBytes >= 0) {
bytesRead += chunkBytes;
}
Expand All @@ -238,6 +241,8 @@ CryptoStream.prototype._push = function() {

var chunk = pool.slice(0, bytesRead);

debug('emit "data" called with <<<<' + chunk.toString() + '>>>');

if (this._decoder) {
var string = this._decoder.write(chunk);
if (string.length) this.emit('data', string);
Expand Down Expand Up @@ -279,7 +284,7 @@ CryptoStream.prototype._pull = function() {
if (tmp === END_OF_FILE) {
// Sending EOF
if (this === this.pair.encrypted) {
debug('end encrypted');
debug('end encrypted ' + this.pair.fd);
this.pair.cleartext._destroyAfterPush = true;
} else {
// CleartextStream
Expand All @@ -306,6 +311,8 @@ CryptoStream.prototype._pull = function() {
return;
}

this.pair._maybeInitFinished();

if (rv === 0 || rv < 0) {
this._pending.unshift(tmp);
this._pendingCallbacks.unshift(cb);
Expand Down Expand Up @@ -489,16 +496,25 @@ SecurePair.prototype._cycle = function() {
return;
}

var established = this._secureEstablished;

this.encrypted._pull();
this.cleartext._pull();
this.cleartext._push();
this.encrypted._push();

if (!established && this._secureEstablished) {
// If we were not established but now we are, let's cycle again.
this._cycle();
}
};


SecurePair.prototype._maybeInitFinished = function() {
if (this._ssl && !this._secureEstablished && this._ssl.isInitFinished()) {
this._secureEstablished = true;
debug('secure established');
this.emit('secure');
this._cycle();
}
};

Expand Down Expand Up @@ -766,6 +782,7 @@ function pipe(pair, socket) {
pair.encrypted.pipe(socket);
socket.pipe(pair.encrypted);

pair.fd = socket.fd;
var cleartext = pair.cleartext;
cleartext.socket = socket;
cleartext.encrypted = pair.encrypted;
Expand Down

0 comments on commit 9de5043

Please sign in to comment.