Skip to content

Commit 9de5043

Browse files
committed
tls: only emit data after 'secure' event
1 parent 38d8cd6 commit 9de5043

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

lib/tls.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var assert = require('assert').ok;
88

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

3939

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

7071

7172
CryptoStream.prototype.pause = function() {
72-
debug('paused cleartext');
73+
debug('paused ' + (this == this.pair.cleartext ? 'cleartext' : 'encrypted'));
7374
this._writeState = false;
7475
};
7576

7677

7778
CryptoStream.prototype.resume = function() {
78-
debug('resumed cleartext');
79+
debug('resume ' + (this == this.pair.cleartext ? 'cleartext' : 'encrypted'));
7980
this._writeState = true;
8081
this.pair._cycle();
8182
};
@@ -220,6 +221,8 @@ CryptoStream.prototype._push = function() {
220221
return;
221222
}
222223

224+
this.pair._maybeInitFinished();
225+
223226
if (chunkBytes >= 0) {
224227
bytesRead += chunkBytes;
225228
}
@@ -238,6 +241,8 @@ CryptoStream.prototype._push = function() {
238241

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

244+
debug('emit "data" called with <<<<' + chunk.toString() + '>>>');
245+
241246
if (this._decoder) {
242247
var string = this._decoder.write(chunk);
243248
if (string.length) this.emit('data', string);
@@ -279,7 +284,7 @@ CryptoStream.prototype._pull = function() {
279284
if (tmp === END_OF_FILE) {
280285
// Sending EOF
281286
if (this === this.pair.encrypted) {
282-
debug('end encrypted');
287+
debug('end encrypted ' + this.pair.fd);
283288
this.pair.cleartext._destroyAfterPush = true;
284289
} else {
285290
// CleartextStream
@@ -306,6 +311,8 @@ CryptoStream.prototype._pull = function() {
306311
return;
307312
}
308313

314+
this.pair._maybeInitFinished();
315+
309316
if (rv === 0 || rv < 0) {
310317
this._pending.unshift(tmp);
311318
this._pendingCallbacks.unshift(cb);
@@ -489,16 +496,25 @@ SecurePair.prototype._cycle = function() {
489496
return;
490497
}
491498

499+
var established = this._secureEstablished;
500+
492501
this.encrypted._pull();
493502
this.cleartext._pull();
494503
this.cleartext._push();
495504
this.encrypted._push();
496505

506+
if (!established && this._secureEstablished) {
507+
// If we were not established but now we are, let's cycle again.
508+
this._cycle();
509+
}
510+
};
511+
512+
513+
SecurePair.prototype._maybeInitFinished = function() {
497514
if (this._ssl && !this._secureEstablished && this._ssl.isInitFinished()) {
498515
this._secureEstablished = true;
499516
debug('secure established');
500517
this.emit('secure');
501-
this._cycle();
502518
}
503519
};
504520

@@ -766,6 +782,7 @@ function pipe(pair, socket) {
766782
pair.encrypted.pipe(socket);
767783
socket.pipe(pair.encrypted);
768784

785+
pair.fd = socket.fd;
769786
var cleartext = pair.cleartext;
770787
cleartext.socket = socket;
771788
cleartext.encrypted = pair.encrypted;

0 commit comments

Comments
 (0)