@@ -294,7 +294,42 @@ Handle<Value> SecureContext::Close(const Arguments& args) {
294294#endif
295295
296296
297- int Connection::HandleError (const char * func, int rv, bool ignore_error) {
297+ int Connection::HandleBIOError (BIO *bio,
298+ const char * func,
299+ int rv,
300+ bool ignore_error) {
301+ if (rv >= 0 ) return rv;
302+
303+ int retry = BIO_should_retry (bio);
304+
305+ if (BIO_should_write (bio)) {
306+ DEBUG_PRINT (" [%p] BIO: %s want write. should retry %d\n " , ssl_, func, retry);
307+ return 0 ;
308+
309+ } else if (BIO_should_read (bio)) {
310+ DEBUG_PRINT (" [%p] BIO: %s want read. should retry %d\n " , ssl_, func, retry);
311+ return 0 ;
312+
313+ } else {
314+ static char ssl_error_buf[512 ];
315+ ERR_error_string_n (rv, ssl_error_buf, sizeof (ssl_error_buf));
316+
317+ if (!ignore_error) {
318+ HandleScope scope;
319+ Local<Value> e = Exception::Error (String::New (ssl_error_buf));
320+ handle_->Set (String::New (" error" ), e);
321+ }
322+
323+ DEBUG_PRINT (" [%p] BIO: %s failed: (%d) %s\n " , ssl_, func, rv, ssl_error_buf);
324+
325+ return rv;
326+ }
327+
328+ return 0 ;
329+ }
330+
331+
332+ int Connection::HandleSSLError (const char * func, int rv, bool ignore_error) {
298333 if (rv >= 0 ) return rv;
299334
300335 int err = SSL_get_error (ssl_, rv);
@@ -510,8 +545,8 @@ Handle<Value> Connection::EncIn(const Arguments& args) {
510545 String::New (" Length is extends beyond buffer" )));
511546 }
512547
513- int bytes_written = BIO_write (ss->bio_read_ , ( char *) buffer_data + off, len);
514- ss->HandleError ( " BIO_write" , bytes_written);
548+ int bytes_written = BIO_write (ss->bio_read_ , buffer_data + off, len);
549+ ss->HandleBIOError (ss-> bio_read_ , " BIO_write" , bytes_written);
515550 ss->SetShutdownFlags ();
516551
517552 return scope.Close (Integer::New (bytes_written));
@@ -554,17 +589,17 @@ Handle<Value> Connection::ClearOut(const Arguments& args) {
554589
555590 if (ss->is_server_ ) {
556591 rv = SSL_accept (ss->ssl_ );
557- ss->HandleError (" SSL_accept:ClearOut" , rv);
592+ ss->HandleSSLError (" SSL_accept:ClearOut" , rv);
558593 } else {
559594 rv = SSL_connect (ss->ssl_ );
560- ss->HandleError (" SSL_connect:ClearOut" , rv);
595+ ss->HandleSSLError (" SSL_connect:ClearOut" , rv);
561596 }
562597
563598 if (rv < 0 ) return scope.Close (Integer::New (rv));
564599 }
565600
566- int bytes_read = SSL_read (ss->ssl_ , ( char *) buffer_data + off, len);
567- ss->HandleError (" SSL_read:ClearOut" , bytes_read);
601+ int bytes_read = SSL_read (ss->ssl_ , buffer_data + off, len);
602+ ss->HandleSSLError (" SSL_read:ClearOut" , bytes_read);
568603 ss->SetShutdownFlags ();
569604
570605 return scope.Close (Integer::New (bytes_read));
@@ -622,9 +657,9 @@ Handle<Value> Connection::EncOut(const Arguments& args) {
622657 String::New (" Length is extends beyond buffer" )));
623658 }
624659
625- int bytes_read = BIO_read (ss->bio_write_ , ( char *) buffer_data + off, len);
660+ int bytes_read = BIO_read (ss->bio_write_ , buffer_data + off, len);
626661
627- ss->HandleError ( " BIO_read:EncOut" , bytes_read, true );
662+ ss->HandleBIOError (ss-> bio_write_ , " BIO_read:EncOut" , bytes_read, true );
628663 ss->SetShutdownFlags ();
629664
630665 return scope.Close (Integer::New (bytes_read));
@@ -666,18 +701,18 @@ Handle<Value> Connection::ClearIn(const Arguments& args) {
666701 int rv;
667702 if (ss->is_server_ ) {
668703 rv = SSL_accept (ss->ssl_ );
669- ss->HandleError (" SSL_accept:ClearIn" , rv);
704+ ss->HandleSSLError (" SSL_accept:ClearIn" , rv);
670705 } else {
671706 rv = SSL_connect (ss->ssl_ );
672- ss->HandleError (" SSL_connect:ClearIn" , rv);
707+ ss->HandleSSLError (" SSL_connect:ClearIn" , rv);
673708 }
674709
675710 if (rv < 0 ) return scope.Close (Integer::New (rv));
676711 }
677712
678- int bytes_written = SSL_write (ss->ssl_ , ( char *) buffer_data + off, len);
713+ int bytes_written = SSL_write (ss->ssl_ , buffer_data + off, len);
679714
680- ss->HandleError (" SSL_write:ClearIn" , bytes_written);
715+ ss->HandleSSLError (" SSL_write:ClearIn" , bytes_written);
681716 ss->SetShutdownFlags ();
682717
683718 return scope.Close (Integer::New (bytes_written));
@@ -766,10 +801,10 @@ Handle<Value> Connection::Start(const Arguments& args) {
766801 int rv;
767802 if (ss->is_server_ ) {
768803 rv = SSL_accept (ss->ssl_ );
769- ss->HandleError (" SSL_accept:Start" , rv);
804+ ss->HandleSSLError (" SSL_accept:Start" , rv);
770805 } else {
771806 rv = SSL_connect (ss->ssl_ );
772- ss->HandleError (" SSL_connect:Start" , rv);
807+ ss->HandleSSLError (" SSL_connect:Start" , rv);
773808 }
774809
775810 return scope.Close (Integer::New (rv));
@@ -787,7 +822,7 @@ Handle<Value> Connection::Shutdown(const Arguments& args) {
787822 if (ss->ssl_ == NULL ) return False ();
788823 int rv = SSL_shutdown (ss->ssl_ );
789824
790- ss->HandleError (" SSL_shutdown" , rv);
825+ ss->HandleSSLError (" SSL_shutdown" , rv);
791826 ss->SetShutdownFlags ();
792827
793828 return scope.Close (Integer::New (rv));
0 commit comments