diff --git a/CMakeLists.txt b/CMakeLists.txt index a0b4a862..9c5cd6d4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,7 +61,7 @@ target_sources(clio PRIVATE src/subscriptions/SubscriptionManager.cpp ## RPC src/rpc/Errors.cpp - src/rpc/RPC.cpp + src/rpc/Factories.cpp src/rpc/RPCHelpers.cpp src/rpc/Counters.cpp src/rpc/WorkQueue.cpp diff --git a/src/rpc/RPC.cpp b/src/rpc/Factories.cpp similarity index 99% rename from src/rpc/RPC.cpp rename to src/rpc/Factories.cpp index 51e0b88b..0ce6ec93 100644 --- a/src/rpc/RPC.cpp +++ b/src/rpc/Factories.cpp @@ -18,7 +18,7 @@ //============================================================================== #include -#include +#include #include #include #include diff --git a/src/rpc/RPC.h b/src/rpc/Factories.h similarity index 100% rename from src/rpc/RPC.h rename to src/rpc/Factories.h diff --git a/src/webserver/HttpBase.h b/src/webserver/HttpBase.h index d7d38767..8bc9d7ee 100644 --- a/src/webserver/HttpBase.h +++ b/src/webserver/HttpBase.h @@ -40,7 +40,7 @@ #include #include
#include -#include +#include #include #include #include @@ -101,7 +101,7 @@ class HttpBase : public util::Taggable self_.derived().stream(), *sp, boost::beast::bind_front_handler( - &HttpBase::on_write, self_.derived().shared_from_this(), sp->need_eof())); + &HttpBase::onWrite, self_.derived().shared_from_this(), sp->need_eof())); } }; @@ -199,7 +199,7 @@ public: } void - do_read() + doRead() { if (dead()) return; @@ -215,17 +215,17 @@ public: derived().stream(), buffer_, req_, - boost::beast::bind_front_handler(&HttpBase::on_read, derived().shared_from_this())); + boost::beast::bind_front_handler(&HttpBase::onRead, derived().shared_from_this())); } void - on_read(boost::beast::error_code ec, std::size_t bytes_transferred) + onRead(boost::beast::error_code ec, std::size_t bytes_transferred) { boost::ignore_unused(bytes_transferred); // This means they closed the connection if (ec == http::error::end_of_stream) - return derived().do_close(); + return derived().doClose(); if (ec) return httpFail(ec, "read"); @@ -253,9 +253,9 @@ public: // Disable the timeout. // The websocket::stream uses its own timeout settings. boost::beast::get_lowest_layer(derived().stream()).expires_never(); - return make_websocket_session( + return make_WebsocketSession( ioc_, - derived().release_stream(), + derived().releaseStream(), derived().ip(), std::move(req_), std::move(buffer_), @@ -282,7 +282,7 @@ public: if (not rpcEngine_->post( [this, ip, session](boost::asio::yield_context yield) { - handle_request( + handleRequest( yield, std::move(req_), lambda_, @@ -309,7 +309,7 @@ public: } void - on_write(bool close, boost::beast::error_code ec, std::size_t bytes_transferred) + onWrite(bool close, boost::beast::error_code ec, std::size_t bytes_transferred) { boost::ignore_unused(bytes_transferred); @@ -320,14 +320,14 @@ public: { // This means we should close the connection, usually because // the response indicated the "Connection: close" semantic. - return derived().do_close(); + return derived().doClose(); } // We're done with the response so delete it res_ = nullptr; // Read another request - do_read(); + doRead(); } }; @@ -337,7 +337,7 @@ public: // caller to pass a generic lambda for receiving the response. template void -handle_request( +handleRequest( boost::asio::yield_context& yc, boost::beast::http::request>&& req, Send&& send, diff --git a/src/webserver/HttpSession.h b/src/webserver/HttpSession.h index 8a42f25d..4bf2eef0 100644 --- a/src/webserver/HttpSession.h +++ b/src/webserver/HttpSession.h @@ -79,8 +79,9 @@ public: { return stream_; } + boost::beast::tcp_stream - release_stream() + releaseStream() { return std::move(stream_); } @@ -99,11 +100,11 @@ public: // on the I/O objects in this HttpSession. Although not strictly // necessary for single-threaded contexts, this example code is written // to be thread-safe by default. - net::dispatch(stream_.get_executor(), boost::beast::bind_front_handler(&HttpBase::do_read, shared_from_this())); + net::dispatch(stream_.get_executor(), boost::beast::bind_front_handler(&HttpBase::doRead, shared_from_this())); } void - do_close() + doClose() { // Send a TCP shutdown boost::beast::error_code ec; diff --git a/src/webserver/Listener.h b/src/webserver/Listener.h index 9700888a..05f37efa 100644 --- a/src/webserver/Listener.h +++ b/src/webserver/Listener.h @@ -94,11 +94,11 @@ public: // Set the timeout. boost::beast::get_lowest_layer(stream_).expires_after(std::chrono::seconds(30)); // Detect a TLS handshake - async_detect_ssl(stream_, buffer_, boost::beast::bind_front_handler(&Detector::on_detect, shared_from_this())); + async_detect_ssl(stream_, buffer_, boost::beast::bind_front_handler(&Detector::onDetect, shared_from_this())); } void - on_detect(boost::beast::error_code ec, bool result) + onDetect(boost::beast::error_code ec, bool result) { if (ec) return fail(ec, "detect"); @@ -141,7 +141,7 @@ public: }; void -make_websocket_session( +make_WebsocketSession( boost::asio::io_context& ioc, boost::beast::tcp_stream stream, std::optional const& ip, @@ -172,7 +172,7 @@ make_websocket_session( } void -make_websocket_session( +make_WebsocketSession( boost::asio::io_context& ioc, boost::beast::ssl_stream stream, std::optional const& ip, @@ -275,20 +275,20 @@ public: void run() { - do_accept(); + doAccept(); } private: void - do_accept() + doAccept() { // The new connection gets its own strand acceptor_.async_accept( - net::make_strand(ioc_), boost::beast::bind_front_handler(&Listener::on_accept, shared_from_this())); + net::make_strand(ioc_), boost::beast::bind_front_handler(&Listener::onAccept, shared_from_this())); } void - on_accept(boost::beast::error_code ec, tcp::socket socket) + onAccept(boost::beast::error_code ec, tcp::socket socket) { if (!ec) { @@ -309,7 +309,7 @@ private: } // Accept another connection - do_accept(); + doAccept(); } }; diff --git a/src/webserver/PlainWsSession.h b/src/webserver/PlainWsSession.h index a18c90b7..ecd8cd5c 100644 --- a/src/webserver/PlainWsSession.h +++ b/src/webserver/PlainWsSession.h @@ -26,7 +26,7 @@ #include #include -#include +#include #include #include @@ -121,6 +121,7 @@ public: , ip_(ip) { } + WsUpgrader( boost::asio::io_context& ioc, boost::beast::tcp_stream&& stream, @@ -158,12 +159,12 @@ public: // thread-safe by default. net::dispatch( - http_.get_executor(), boost::beast::bind_front_handler(&WsUpgrader::do_upgrade, shared_from_this())); + http_.get_executor(), boost::beast::bind_front_handler(&WsUpgrader::doUpgrade, shared_from_this())); } private: void - do_upgrade() + doUpgrade() { parser_.emplace(); @@ -174,11 +175,11 @@ private: // Set the timeout. boost::beast::get_lowest_layer(http_).expires_after(std::chrono::seconds(30)); - on_upgrade(); + onUpgrade(); } void - on_upgrade() + onUpgrade() { // See if it is a WebSocket Upgrade if (!websocket::is_upgrade(req_)) diff --git a/src/webserver/Ssl.h b/src/webserver/Ssl.h deleted file mode 100644 index 1ef4bd9f..00000000 --- a/src/webserver/Ssl.h +++ /dev/null @@ -1,59 +0,0 @@ -//------------------------------------------------------------------------------ -/* - This file is part of clio: https://github.com/XRPLF/clio - Copyright (c) 2022, the clio developers. - - Permission to use, copy, modify, and distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ -//============================================================================== - -#pragma once - -#include - -#include -#include - -namespace ssl = boost::asio::ssl; - -static std::optional -parse_certs(const char* certFilename, const char* keyFilename) -{ - std::ifstream readCert(certFilename, std::ios::in | std::ios::binary); - if (!readCert) - return {}; - - std::stringstream contents; - contents << readCert.rdbuf(); - readCert.close(); - std::string cert = contents.str(); - - std::ifstream readKey(keyFilename, std::ios::in | std::ios::binary); - if (!readKey) - return {}; - - contents.str(""); - contents << readKey.rdbuf(); - readKey.close(); - std::string key = contents.str(); - - ssl::context ctx{ssl::context::tlsv12}; - - ctx.set_options(boost::asio::ssl::context::default_workarounds | boost::asio::ssl::context::no_sslv2); - - ctx.use_certificate_chain(boost::asio::buffer(cert.data(), cert.size())); - - ctx.use_private_key(boost::asio::buffer(key.data(), key.size()), boost::asio::ssl::context::file_format::pem); - - return ctx; -} diff --git a/src/webserver/SslHttpSession.h b/src/webserver/SslHttpSession.h index 22908089..9e50127d 100644 --- a/src/webserver/SslHttpSession.h +++ b/src/webserver/SslHttpSession.h @@ -81,7 +81,7 @@ public: return stream_; } boost::beast::ssl_stream - release_stream() + releaseStream() { return std::move(stream_); } @@ -108,33 +108,33 @@ public: self->stream_.async_handshake( ssl::stream_base::server, self->buffer_.data(), - boost::beast::bind_front_handler(&SslHttpSession::on_handshake, self)); + boost::beast::bind_front_handler(&SslHttpSession::onHandshake, self)); }); } void - on_handshake(boost::beast::error_code ec, std::size_t bytes_used) + onHandshake(boost::beast::error_code ec, std::size_t bytes_used) { if (ec) return httpFail(ec, "handshake"); buffer_.consume(bytes_used); - do_read(); + doRead(); } void - do_close() + doClose() { // Set the timeout. boost::beast::get_lowest_layer(stream_).expires_after(std::chrono::seconds(30)); // Perform the SSL shutdown - stream_.async_shutdown(boost::beast::bind_front_handler(&SslHttpSession::on_shutdown, shared_from_this())); + stream_.async_shutdown(boost::beast::bind_front_handler(&SslHttpSession::onShutdown, shared_from_this())); } void - on_shutdown(boost::beast::error_code ec) + onShutdown(boost::beast::error_code ec) { if (ec) return httpFail(ec, "shutdown"); diff --git a/src/webserver/SslWsSession.h b/src/webserver/SslWsSession.h index 8836f0b6..8f473939 100644 --- a/src/webserver/SslWsSession.h +++ b/src/webserver/SslWsSession.h @@ -59,6 +59,7 @@ public: , ws_(std::move(stream)) { } + boost::beast::websocket::stream>& ws() { @@ -115,6 +116,7 @@ public: , dosGuard_(dosGuard) { } + SslWsUpgrader( boost::asio::io_context& ioc, boost::beast::ssl_stream stream, @@ -152,12 +154,12 @@ public: boost::beast::get_lowest_layer(https_).expires_after(std::chrono::seconds(30)); net::dispatch( - https_.get_executor(), boost::beast::bind_front_handler(&SslWsUpgrader::do_upgrade, shared_from_this())); + https_.get_executor(), boost::beast::bind_front_handler(&SslWsUpgrader::doUpgrade, shared_from_this())); } private: void - on_handshake(boost::beast::error_code ec, std::size_t bytes_used) + onHandshake(boost::beast::error_code ec, std::size_t bytes_used) { if (ec) return logError(ec, "handshake"); @@ -165,11 +167,11 @@ private: // Consume the portion of the buffer used by the handshake buffer_.consume(bytes_used); - do_upgrade(); + doUpgrade(); } void - do_upgrade() + doUpgrade() { parser_.emplace(); @@ -180,11 +182,11 @@ private: // Set the timeout. boost::beast::get_lowest_layer(https_).expires_after(std::chrono::seconds(30)); - on_upgrade(); + onUpgrade(); } void - on_upgrade() + onUpgrade() { // See if it is a WebSocket Upgrade if (!websocket::is_upgrade(req_)) diff --git a/src/webserver/WsBase.h b/src/webserver/WsBase.h index 1cec2d60..a4972ea9 100644 --- a/src/webserver/WsBase.h +++ b/src/webserver/WsBase.h @@ -24,7 +24,7 @@ #include #include #include -#include +#include #include #include #include @@ -185,16 +185,16 @@ public: } void - do_write() + doWrite() { sending_ = true; derived().ws().async_write( net::buffer(messages_.front()->data(), messages_.front()->size()), - boost::beast::bind_front_handler(&WsSession::on_write, derived().shared_from_this())); + boost::beast::bind_front_handler(&WsSession::onWrite, derived().shared_from_this())); } void - on_write(boost::system::error_code ec, std::size_t) + onWrite(boost::system::error_code ec, std::size_t) { if (ec) { @@ -204,17 +204,17 @@ public: { messages_.pop(); sending_ = false; - maybe_send_next(); + maybeSendNext(); } } void - maybe_send_next() + maybeSendNext() { if (ec_ || sending_ || messages_.empty()) return; - do_write(); + doWrite(); } void @@ -223,7 +223,7 @@ public: net::dispatch( derived().ws().get_executor(), [this, self = derived().shared_from_this(), msg = std::move(msg)]() { messages_.push(std::move(msg)); - maybe_send_next(); + maybeSendNext(); }); } @@ -246,11 +246,11 @@ public: })); derived().ws().async_accept( - req, boost::beast::bind_front_handler(&WsSession::on_accept, this->shared_from_this())); + req, boost::beast::bind_front_handler(&WsSession::onAccept, this->shared_from_this())); } void - on_accept(boost::beast::error_code ec) + onAccept(boost::beast::error_code ec) { if (ec) return wsFail(ec, "accept"); @@ -258,11 +258,11 @@ public: perfLog_.info() << tag() << "accepting new connection"; // Read a message - do_read(); + doRead(); } void - do_read() + doRead() { if (dead()) return; @@ -272,11 +272,11 @@ public: buffer_.consume(buffer_.size()); // Read a message into our buffer derived().ws().async_read( - buffer_, boost::beast::bind_front_handler(&WsSession::on_read, this->shared_from_this())); + buffer_, boost::beast::bind_front_handler(&WsSession::onRead, this->shared_from_this())); } void - handle_request(boost::json::object const&& request, boost::json::value const& id, boost::asio::yield_context& yield) + handleRequest(boost::json::object const&& request, boost::json::value const& id, boost::asio::yield_context& yield) { auto ip = derived().ip(); if (!ip) @@ -371,7 +371,7 @@ public: } void - on_read(boost::beast::error_code ec, std::size_t bytes_transferred) + onRead(boost::beast::error_code ec, std::size_t bytes_transferred) { boost::ignore_unused(bytes_transferred); @@ -431,12 +431,12 @@ public: if (not rpcEngine_->post( [self = shared_from_this(), req = std::move(request), id](boost::asio::yield_context yield) { - self->handle_request(std::move(req), id, yield); + self->handleRequest(std::move(req), id, yield); }, ip.value())) sendError(RPC::RippledError::rpcTOO_BUSY, id, request); } - do_read(); + doRead(); } }; diff --git a/unittests/rpc/BaseTests.cpp b/unittests/rpc/BaseTests.cpp index 44b70a9d..a97b65a1 100644 --- a/unittests/rpc/BaseTests.cpp +++ b/unittests/rpc/BaseTests.cpp @@ -19,7 +19,7 @@ #include -#include +#include #include #include #include diff --git a/unittests/rpc/ErrorTests.cpp b/unittests/rpc/ErrorTests.cpp index 86b0e46e..8b913c8a 100644 --- a/unittests/rpc/ErrorTests.cpp +++ b/unittests/rpc/ErrorTests.cpp @@ -17,7 +17,7 @@ */ //============================================================================== -#include +#include #include #include diff --git a/unittests/rpc/handlers/impl/FakesAndMocks.h b/unittests/rpc/handlers/impl/FakesAndMocks.h index 2bc5c832..7ac4646b 100644 --- a/unittests/rpc/handlers/impl/FakesAndMocks.h +++ b/unittests/rpc/handlers/impl/FakesAndMocks.h @@ -19,7 +19,7 @@ #pragma once -#include +#include #include #include