Commit ec4b2e04 authored by Jay Edgar's avatar Jay Edgar Committed by Facebook Github Bot

Use a static exception where it makes sense

Summary: Don't constanly rebuild exceptions that are always the same.

Reviewed By: jrahman

Differential Revision: D16016383

fbshipit-source-id: b0b24ed39e6c41f95c61369c90a3ac83abfd1a66
parent 7b691d80
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
#include <memory> #include <memory>
#include <folly/Format.h> #include <folly/Format.h>
#include <folly/Indestructible.h>
#include <folly/SocketAddress.h> #include <folly/SocketAddress.h>
#include <folly/SpinLock.h> #include <folly/SpinLock.h>
#include <folly/io/Cursor.h> #include <folly/io/Cursor.h>
...@@ -348,8 +349,9 @@ void AsyncSSLSocket::closeNow() { ...@@ -348,8 +349,9 @@ void AsyncSSLSocket::closeNow() {
DestructorGuard dg(this); DestructorGuard dg(this);
invokeHandshakeErr(AsyncSocketException( static const Indestructible<AsyncSocketException> ex(
AsyncSocketException::END_OF_FILE, "SSL connection closed locally")); AsyncSocketException::END_OF_FILE, "SSL connection closed locally");
invokeHandshakeErr(*ex);
// Close the socket. // Close the socket.
AsyncSocket::closeNow(); AsyncSocket::closeNow();
...@@ -446,16 +448,16 @@ void AsyncSSLSocket::invalidState(HandshakeCB* callback) { ...@@ -446,16 +448,16 @@ void AsyncSSLSocket::invalidState(HandshakeCB* callback) {
assert(!handshakeTimeout_.isScheduled()); assert(!handshakeTimeout_.isScheduled());
sslState_ = STATE_ERROR; sslState_ = STATE_ERROR;
AsyncSocketException ex( static const Indestructible<AsyncSocketException> ex(
AsyncSocketException::INVALID_STATE, AsyncSocketException::INVALID_STATE,
"sslAccept() called with socket in invalid state"); "sslAccept() called with socket in invalid state");
handshakeEndTime_ = std::chrono::steady_clock::now(); handshakeEndTime_ = std::chrono::steady_clock::now();
if (callback) { if (callback) {
callback->handshakeErr(this, ex); callback->handshakeErr(this, *ex);
} }
failHandshake(__func__, ex); failHandshake(__func__, *ex);
} }
void AsyncSSLSocket::sslAccept( void AsyncSSLSocket::sslAccept(
...@@ -616,10 +618,10 @@ void AsyncSSLSocket::timeoutExpired( ...@@ -616,10 +618,10 @@ void AsyncSSLSocket::timeoutExpired(
} else if (state_ == StateEnum::CONNECTING) { } else if (state_ == StateEnum::CONNECTING) {
assert(sslState_ == STATE_CONNECTING); assert(sslState_ == STATE_CONNECTING);
DestructorGuard dg(this); DestructorGuard dg(this);
AsyncSocketException ex( static const Indestructible<AsyncSocketException> ex(
AsyncSocketException::TIMED_OUT, AsyncSocketException::TIMED_OUT,
"Fallback connect timed out during TFO"); "Fallback connect timed out during TFO");
failHandshake(__func__, ex); failHandshake(__func__, *ex);
} else { } else {
assert( assert(
state_ == StateEnum::ESTABLISHED && state_ == StateEnum::ESTABLISHED &&
...@@ -778,19 +780,19 @@ void AsyncSSLSocket::sslConn( ...@@ -778,19 +780,19 @@ void AsyncSSLSocket::sslConn(
ssl_.reset(ctx_->createSSL()); ssl_.reset(ctx_->createSSL());
} catch (std::exception& e) { } catch (std::exception& e) {
sslState_ = STATE_ERROR; sslState_ = STATE_ERROR;
AsyncSocketException ex( static const Indestructible<AsyncSocketException> ex(
AsyncSocketException::INTERNAL_ERROR, AsyncSocketException::INTERNAL_ERROR,
"error calling SSLContext::createSSL()"); "error calling SSLContext::createSSL()");
LOG(ERROR) << "AsyncSSLSocket::sslConn(this=" << this << ", fd=" << fd_ LOG(ERROR) << "AsyncSSLSocket::sslConn(this=" << this << ", fd=" << fd_
<< "): " << e.what(); << "): " << e.what();
return failHandshake(__func__, ex); return failHandshake(__func__, *ex);
} }
if (!setupSSLBio()) { if (!setupSSLBio()) {
sslState_ = STATE_ERROR; sslState_ = STATE_ERROR;
AsyncSocketException ex( static const Indestructible<AsyncSocketException> ex(
AsyncSocketException::INTERNAL_ERROR, "error creating SSL bio"); AsyncSocketException::INTERNAL_ERROR, "error creating SSL bio");
return failHandshake(__func__, ex); return failHandshake(__func__, *ex);
} }
applyVerificationOptions(ssl_); applyVerificationOptions(ssl_);
...@@ -1079,9 +1081,9 @@ void AsyncSSLSocket::restartSSLAccept() { ...@@ -1079,9 +1081,9 @@ void AsyncSSLSocket::restartSSLAccept() {
} }
if (sslState_ == STATE_ERROR) { if (sslState_ == STATE_ERROR) {
// go straight to fail if timeout expired during lookup // go straight to fail if timeout expired during lookup
AsyncSocketException ex( static const Indestructible<AsyncSocketException> ex(
AsyncSocketException::TIMED_OUT, "SSL accept timed out"); AsyncSocketException::TIMED_OUT, "SSL accept timed out");
failHandshake(__func__, ex); failHandshake(__func__, *ex);
return; return;
} }
sslState_ = STATE_ACCEPTING; sslState_ = STATE_ACCEPTING;
...@@ -1100,19 +1102,19 @@ void AsyncSSLSocket::handleAccept() noexcept { ...@@ -1100,19 +1102,19 @@ void AsyncSSLSocket::handleAccept() noexcept {
ssl_.reset(ctx_->createSSL()); ssl_.reset(ctx_->createSSL());
} catch (std::exception& e) { } catch (std::exception& e) {
sslState_ = STATE_ERROR; sslState_ = STATE_ERROR;
AsyncSocketException ex( static const Indestructible<AsyncSocketException> ex(
AsyncSocketException::INTERNAL_ERROR, AsyncSocketException::INTERNAL_ERROR,
"error calling SSLContext::createSSL()"); "error calling SSLContext::createSSL()");
LOG(ERROR) << "AsyncSSLSocket::handleAccept(this=" << this LOG(ERROR) << "AsyncSSLSocket::handleAccept(this=" << this
<< ", fd=" << fd_ << "): " << e.what(); << ", fd=" << fd_ << "): " << e.what();
return failHandshake(__func__, ex); return failHandshake(__func__, *ex);
} }
if (!setupSSLBio()) { if (!setupSSLBio()) {
sslState_ = STATE_ERROR; sslState_ = STATE_ERROR;
AsyncSocketException ex( static const Indestructible<AsyncSocketException> ex(
AsyncSocketException::INTERNAL_ERROR, "error creating write bio"); AsyncSocketException::INTERNAL_ERROR, "error creating write bio");
return failHandshake(__func__, ex); return failHandshake(__func__, *ex);
} }
SSL_set_ex_data(ssl_.get(), getSSLExDataIndex(), this); SSL_set_ex_data(ssl_.get(), getSSLExDataIndex(), this);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment