Merge pull request #4030 from sigiesec/test-coverage-3

Problem: code duplicated between handshake and out_event
This commit is contained in:
Luca Boccassi 2020-09-04 16:24:28 +01:00 committed by GitHub
commit 04ac547c20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 22 deletions

View File

@ -133,6 +133,11 @@ void zmq::wss_engine_t::out_event ()
if (_established) if (_established)
return ws_engine_t::out_event (); return ws_engine_t::out_event ();
do_handshake ();
}
bool zmq::wss_engine_t::do_handshake ()
{
int rc = gnutls_handshake (_tls_session); int rc = gnutls_handshake (_tls_session);
reset_pollout (); reset_pollout ();
@ -140,42 +145,28 @@ void zmq::wss_engine_t::out_event ()
if (rc == GNUTLS_E_SUCCESS) { if (rc == GNUTLS_E_SUCCESS) {
start_ws_handshake (); start_ws_handshake ();
_established = true; _established = true;
return; return false;
} else if (rc == GNUTLS_E_AGAIN) { } else if (rc == GNUTLS_E_AGAIN) {
int direction = gnutls_record_get_direction (_tls_session); int direction = gnutls_record_get_direction (_tls_session);
if (direction == 1) if (direction == 1)
set_pollout (); set_pollout ();
return; return false;
} else if (rc == GNUTLS_E_INTERRUPTED } else if (rc == GNUTLS_E_INTERRUPTED
|| rc == GNUTLS_E_WARNING_ALERT_RECEIVED) { || rc == GNUTLS_E_WARNING_ALERT_RECEIVED) {
return; return false;
} else { } else {
error (zmq::i_engine::connection_error); error (zmq::i_engine::connection_error);
return; return false;
} }
return true;
} }
bool zmq::wss_engine_t::handshake () bool zmq::wss_engine_t::handshake ()
{ {
if (!_established) { if (!_established) {
int rc = gnutls_handshake (_tls_session); if (!do_handshake ()) {
if (rc == GNUTLS_E_SUCCESS) {
start_ws_handshake ();
_established = true;
return false;
} else if (rc == GNUTLS_E_AGAIN) {
int direction = gnutls_record_get_direction (_tls_session);
if (direction == 1)
set_pollout ();
return false;
} else if (rc == GNUTLS_E_INTERRUPTED
|| rc == GNUTLS_E_WARNING_ALERT_RECEIVED) {
return false;
} else {
error (zmq::i_engine::connection_error);
return false; return false;
} }
} }

View File

@ -57,8 +57,9 @@ class wss_engine_t : public ws_engine_t
int read (void *data, size_t size_); int read (void *data, size_t size_);
int write (const void *data_, size_t size_); int write (const void *data_, size_t size_);
private: private:
bool do_handshake ();
bool _established; bool _established;
gnutls_certificate_credentials_t _tls_client_cred; gnutls_certificate_credentials_t _tls_client_cred;
gnutls_session_t _tls_session; gnutls_session_t _tls_session;