@@ -8,7 +8,7 @@ var assert = require('assert').ok;
8
8
9
9
var debug ;
10
10
if ( process . env . NODE_DEBUG && / t l s / . test ( process . env . NODE_DEBUG ) ) {
11
- debug = function ( ) { util . error . apply ( this , arguments ) ; } ;
11
+ debug = function ( a ) { console . error ( "TLS: " , a ) ; } ;
12
12
} else {
13
13
debug = function ( ) { } ;
14
14
}
@@ -38,6 +38,7 @@ util.inherits(CryptoStream, stream.Stream);
38
38
39
39
40
40
CryptoStream . prototype . write = function ( data /* , encoding, cb */ ) {
41
+ debug ( 'CryptoStream.prototype.write called with <<<<' + data . toString ( ) + '>>>' ) ;
41
42
if ( ! this . writable ) {
42
43
throw new Error ( 'CryptoStream is not writable' ) ;
43
44
}
@@ -69,13 +70,13 @@ CryptoStream.prototype.write = function(data /* , encoding, cb */) {
69
70
70
71
71
72
CryptoStream . prototype . pause = function ( ) {
72
- debug ( 'paused cleartext' ) ;
73
+ debug ( 'paused ' + ( this == this . pair . cleartext ? 'cleartext' : 'encrypted' ) ) ;
73
74
this . _writeState = false ;
74
75
} ;
75
76
76
77
77
78
CryptoStream . prototype . resume = function ( ) {
78
- debug ( 'resumed cleartext' ) ;
79
+ debug ( 'resume ' + ( this == this . pair . cleartext ? 'cleartext' : 'encrypted' ) ) ;
79
80
this . _writeState = true ;
80
81
this . pair . _cycle ( ) ;
81
82
} ;
@@ -220,6 +221,8 @@ CryptoStream.prototype._push = function() {
220
221
return ;
221
222
}
222
223
224
+ this . pair . _maybeInitFinished ( ) ;
225
+
223
226
if ( chunkBytes >= 0 ) {
224
227
bytesRead += chunkBytes ;
225
228
}
@@ -238,6 +241,8 @@ CryptoStream.prototype._push = function() {
238
241
239
242
var chunk = pool . slice ( 0 , bytesRead ) ;
240
243
244
+ debug ( 'emit "data" called with <<<<' + chunk . toString ( ) + '>>>' ) ;
245
+
241
246
if ( this . _decoder ) {
242
247
var string = this . _decoder . write ( chunk ) ;
243
248
if ( string . length ) this . emit ( 'data' , string ) ;
@@ -279,7 +284,7 @@ CryptoStream.prototype._pull = function() {
279
284
if ( tmp === END_OF_FILE ) {
280
285
// Sending EOF
281
286
if ( this === this . pair . encrypted ) {
282
- debug ( 'end encrypted' ) ;
287
+ debug ( 'end encrypted ' + this . pair . fd ) ;
283
288
this . pair . cleartext . _destroyAfterPush = true ;
284
289
} else {
285
290
// CleartextStream
@@ -306,6 +311,8 @@ CryptoStream.prototype._pull = function() {
306
311
return ;
307
312
}
308
313
314
+ this . pair . _maybeInitFinished ( ) ;
315
+
309
316
if ( rv === 0 || rv < 0 ) {
310
317
this . _pending . unshift ( tmp ) ;
311
318
this . _pendingCallbacks . unshift ( cb ) ;
@@ -489,16 +496,25 @@ SecurePair.prototype._cycle = function() {
489
496
return ;
490
497
}
491
498
499
+ var established = this . _secureEstablished ;
500
+
492
501
this . encrypted . _pull ( ) ;
493
502
this . cleartext . _pull ( ) ;
494
503
this . cleartext . _push ( ) ;
495
504
this . encrypted . _push ( ) ;
496
505
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 ( ) {
497
514
if ( this . _ssl && ! this . _secureEstablished && this . _ssl . isInitFinished ( ) ) {
498
515
this . _secureEstablished = true ;
499
516
debug ( 'secure established' ) ;
500
517
this . emit ( 'secure' ) ;
501
- this . _cycle ( ) ;
502
518
}
503
519
} ;
504
520
@@ -766,6 +782,7 @@ function pipe(pair, socket) {
766
782
pair . encrypted . pipe ( socket ) ;
767
783
socket . pipe ( pair . encrypted ) ;
768
784
785
+ pair . fd = socket . fd ;
769
786
var cleartext = pair . cleartext ;
770
787
cleartext . socket = socket ;
771
788
cleartext . encrypted = pair . encrypted ;
0 commit comments