@@ -62,7 +62,6 @@ TLSWrap::TLSWrap(Environment* env,
62
62
stream_(stream),
63
63
enc_in_(nullptr ),
64
64
enc_out_(nullptr ),
65
- clear_in_(nullptr ),
66
65
write_size_(0 ),
67
66
started_(false ),
68
67
established_(false ),
@@ -95,8 +94,6 @@ TLSWrap::TLSWrap(Environment* env,
95
94
TLSWrap::~TLSWrap () {
96
95
enc_in_ = nullptr ;
97
96
enc_out_ = nullptr ;
98
- delete clear_in_;
99
- clear_in_ = nullptr ;
100
97
101
98
sc_ = nullptr ;
102
99
@@ -119,11 +116,6 @@ TLSWrap::~TLSWrap() {
119
116
}
120
117
121
118
122
- void TLSWrap::MakePending () {
123
- write_callback_scheduled_ = true ;
124
- }
125
-
126
-
127
119
bool TLSWrap::InvokeQueued (int status, const char * error_str) {
128
120
if (!write_callback_scheduled_)
129
121
return false ;
@@ -183,10 +175,6 @@ void TLSWrap::InitSSL() {
183
175
// Unexpected
184
176
ABORT ();
185
177
}
186
-
187
- // Initialize ring for queud clear data
188
- clear_in_ = new crypto::NodeBIO ();
189
- clear_in_->AssignEnvironment (env ());
190
178
}
191
179
192
180
@@ -302,14 +290,14 @@ void TLSWrap::EncOut() {
302
290
303
291
// Split-off queue
304
292
if (established_ && current_write_ != nullptr )
305
- MakePending () ;
293
+ write_callback_scheduled_ = true ;
306
294
307
295
if (ssl_ == nullptr )
308
296
return ;
309
297
310
298
// No data to write
311
299
if (BIO_pending (enc_out_) == 0 ) {
312
- if (clear_in_-> Length () == 0 )
300
+ if (pending_cleartext_input_. empty () )
313
301
InvokeQueued (0 );
314
302
return ;
315
303
}
@@ -496,21 +484,24 @@ bool TLSWrap::ClearIn() {
496
484
if (ssl_ == nullptr )
497
485
return false ;
498
486
487
+ std::vector<uv_buf_t > buffers;
488
+ buffers.swap (pending_cleartext_input_);
489
+
499
490
crypto::MarkPopErrorOnReturn mark_pop_error_on_return;
500
491
492
+ size_t i;
501
493
int written = 0 ;
502
- while (clear_in_-> Length () > 0 ) {
503
- size_t avail = 0 ;
504
- char * data = clear_in_-> Peek (&avail) ;
494
+ for (i = 0 ; i < buffers. size (); ++i ) {
495
+ size_t avail = buffers[i]. len ;
496
+ char * data = buffers[i]. base ;
505
497
written = SSL_write (ssl_, data, avail);
506
498
CHECK (written == -1 || written == static_cast <int >(avail));
507
499
if (written == -1 )
508
500
break ;
509
- clear_in_->Read (nullptr , avail);
510
501
}
511
502
512
503
// All written
513
- if (clear_in_-> Length () == 0 ) {
504
+ if (i == buffers. size () ) {
514
505
CHECK_GE (written, 0 );
515
506
return true ;
516
507
}
@@ -520,9 +511,15 @@ bool TLSWrap::ClearIn() {
520
511
std::string error_str;
521
512
Local<Value> arg = GetSSLError (written, &err, &error_str);
522
513
if (!arg.IsEmpty ()) {
523
- MakePending () ;
514
+ write_callback_scheduled_ = true ;
524
515
InvokeQueued (UV_EPROTO, error_str.c_str ());
525
- clear_in_->Reset ();
516
+ } else {
517
+ // Push back the not-yet-written pending buffers into their queue.
518
+ // This can be skipped in the error case because no further writes
519
+ // would succeed anyway.
520
+ pending_cleartext_input_.insert (pending_cleartext_input_.end (),
521
+ &buffers[i],
522
+ &buffers[buffers.size ()]);
526
523
}
527
524
528
525
return false ;
@@ -615,14 +612,6 @@ int TLSWrap::DoWrite(WriteWrap* w,
615
612
return 0 ;
616
613
}
617
614
618
- // Process enqueued data first
619
- if (!ClearIn ()) {
620
- // If there're still data to process - enqueue current one
621
- for (i = 0 ; i < count; i++)
622
- clear_in_->Write (bufs[i].base , bufs[i].len );
623
- return 0 ;
624
- }
625
-
626
615
if (ssl_ == nullptr ) {
627
616
ClearError ();
628
617
error_ = " Write after DestroySSL" ;
@@ -645,9 +634,9 @@ int TLSWrap::DoWrite(WriteWrap* w,
645
634
if (!arg.IsEmpty ())
646
635
return UV_EPROTO;
647
636
648
- // No errors, queue rest
649
- for (; i < count; i++)
650
- clear_in_-> Write (bufs[i]. base , bufs[i]. len );
637
+ pending_cleartext_input_. insert (pending_cleartext_input_. end (),
638
+ &bufs[i],
639
+ & bufs[count] );
651
640
}
652
641
653
642
// Try writing data immediately
@@ -817,17 +806,14 @@ void TLSWrap::DestroySSL(const FunctionCallbackInfo<Value>& args) {
817
806
TLSWrap* wrap;
818
807
ASSIGN_OR_RETURN_UNWRAP (&wrap, args.Holder ());
819
808
820
- // Move all writes to pending
821
- wrap->MakePending () ;
809
+ // If there is a write happening, mark it as finished.
810
+ wrap->write_callback_scheduled_ = true ;
822
811
823
812
// And destroy
824
813
wrap->InvokeQueued (UV_ECANCELED, " Canceled because of SSL destruction" );
825
814
826
815
// Destroy the SSL structure and friends
827
816
wrap->SSLWrap <TLSWrap>::DestroySSL ();
828
-
829
- delete wrap->clear_in_ ;
830
- wrap->clear_in_ = nullptr ;
831
817
}
832
818
833
819
@@ -927,7 +913,7 @@ void TLSWrap::GetWriteQueueSize(const FunctionCallbackInfo<Value>& info) {
927
913
TLSWrap* wrap;
928
914
ASSIGN_OR_RETURN_UNWRAP (&wrap, info.This ());
929
915
930
- if (wrap->clear_in_ == nullptr ) {
916
+ if (wrap->ssl_ == nullptr ) {
931
917
info.GetReturnValue ().Set (0 );
932
918
return ;
933
919
}
0 commit comments