From 7e8a7c22f8dfcde7349c30e28c63b9412d60adf9 Mon Sep 17 00:00:00 2001 From: canepat <16927169+canepat@users.noreply.github.com> Date: Tue, 19 Mar 2024 15:21:06 +0100 Subject: [PATCH 1/3] rpc: simplify HTTP gzip compression handling rpc: fix Clang Tidy after 1914 --- silkworm/rpc/http/connection.cpp | 42 +++++++------------------------- silkworm/rpc/http/connection.hpp | 4 +-- 2 files changed, 10 insertions(+), 36 deletions(-) diff --git a/silkworm/rpc/http/connection.cpp b/silkworm/rpc/http/connection.cpp index a10ac547e5..04f32bc54c 100644 --- a/silkworm/rpc/http/connection.cpp +++ b/silkworm/rpc/http/connection.cpp @@ -44,7 +44,6 @@ namespace silkworm::rpc::http { static constexpr std::string_view kMaxAge{"600"}; static constexpr auto kMaxPayloadSize{30 * kMebi}; // 30MiB static constexpr std::array kAcceptedContentTypes{"application/json", "application/jsonrequest", "application/json-rpc"}; -static std::vector kSupportedCompressionList{"gzip"}; // specify the compression algo in priority level Connection::Connection(boost::asio::io_context& io_context, commands::RpcApi& api, @@ -183,23 +182,9 @@ Task Connection::handle_actual_request(const boost::beast::http::request Connection::handle_actual_request(const boost::beast::http::requestappend("\n"), boost::beast::http::status::ok, selected_compression); + co_await do_write(rsp_content->append("\n"), boost::beast::http::status::ok, accept_encoding); } } @@ -306,15 +291,15 @@ Task Connection::do_write(const std::string& content, boost::beast::http:: if (http_status == boost::beast::http::status::ok && !content_encoding.empty()) { // Positive response w/ compression required res.set(boost::beast::http::field::content_encoding, content_encoding); - std::string compressed_data; + std::string compressed_content; try { - compress_data(content, compressed_data); + compress(content, compressed_content); } catch (const std::exception& e) { SILK_ERROR << "Connection::do_write cannot compress exception: " << e.what(); throw; } - res.content_length(compressed_data.length()); - res.body() = std::move(compressed_data); + res.content_length(compressed_content.length()); + res.body() = std::move(compressed_content); } else { // Any negative response or positive response w/o compression if (!content_encoding.empty()) { @@ -340,7 +325,7 @@ Task Connection::do_write(const std::string& content, boost::beast::http:: co_return; } -void Connection::compress_data(const std::string& clear_data, std::string& compressed_data) { +void Connection::compress(const std::string& clear_data, std::string& compressed_data) { boost::iostreams::filtering_ostream out; out.push(boost::iostreams::gzip_compressor()); out.push(boost::iostreams::back_inserter(compressed_data)); @@ -415,15 +400,6 @@ void Connection::set_cors(boost::beast::http::response& res) { } } -std::string Connection::select_compression_algo(const std::string& requested_compression) { - for (std::string curr_compression : kSupportedCompressionList) { - if (requested_compression.find(curr_compression) != std::string::npos) { - return curr_compression; - } - } - return ""; -} - bool Connection::is_origin_allowed(const std::vector& allowed_origins, const std::string& origin) { if (allowed_origins.size() == 1 && allowed_origins[0] == "*") { return true; diff --git a/silkworm/rpc/http/connection.hpp b/silkworm/rpc/http/connection.hpp index fecd7f5419..51f4d284ef 100644 --- a/silkworm/rpc/http/connection.hpp +++ b/silkworm/rpc/http/connection.hpp @@ -91,9 +91,7 @@ class Connection : public StreamWriter { static std::string get_date_time(); - std::string select_compression_algo(const std::string& request_compression); - - void compress_data(const std::string& clear_data, std::string& compressed_data); + void compress(const std::string& clear_data, std::string& compressed_data); //! Socket for the connection. boost::asio::ip::tcp::socket socket_; From 795add2fbc13798a64ecc2bbb84c38382afc5c13 Mon Sep 17 00:00:00 2001 From: canepat <16927169+canepat@users.noreply.github.com> Date: Tue, 19 Mar 2024 16:16:53 +0100 Subject: [PATCH 2/3] remove unnecessary definition --- silkworm/rpc/http/connection.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/silkworm/rpc/http/connection.cpp b/silkworm/rpc/http/connection.cpp index 04f32bc54c..ffc46e4d75 100644 --- a/silkworm/rpc/http/connection.cpp +++ b/silkworm/rpc/http/connection.cpp @@ -16,10 +16,6 @@ #include "connection.hpp" -#ifndef ZLIB_CONST -#define ZLIB_CONST -#endif - #include #include #include From e4cca04614177e59052af8c15a739d17e7250961 Mon Sep 17 00:00:00 2001 From: canepat <16927169+canepat@users.noreply.github.com> Date: Tue, 19 Mar 2024 20:01:53 +0100 Subject: [PATCH 3/3] fix after review --- silkworm/rpc/http/connection.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/silkworm/rpc/http/connection.cpp b/silkworm/rpc/http/connection.cpp index ffc46e4d75..dc33c6125b 100644 --- a/silkworm/rpc/http/connection.cpp +++ b/silkworm/rpc/http/connection.cpp @@ -40,6 +40,7 @@ namespace silkworm::rpc::http { static constexpr std::string_view kMaxAge{"600"}; static constexpr auto kMaxPayloadSize{30 * kMebi}; // 30MiB static constexpr std::array kAcceptedContentTypes{"application/json", "application/jsonrequest", "application/json-rpc"}; +static constexpr auto kGzipEncoding{"gzip"}; Connection::Connection(boost::asio::io_context& io_context, commands::RpcApi& api, @@ -178,8 +179,9 @@ Task Connection::handle_actual_request(const boost::beast::http::request Connection::handle_actual_request(const boost::beast::http::requestappend("\n"), boost::beast::http::status::ok, accept_encoding); + co_await do_write(rsp_content->append("\n"), boost::beast::http::status::ok, gzip_encoding_requested ? kGzipEncoding : ""); } }