Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rpcdaemon: simplify HTTP gzip compression handling #1923

Merged
merged 3 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 9 additions & 37 deletions silkworm/rpc/http/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@

#include "connection.hpp"

#ifndef ZLIB_CONST
#define ZLIB_CONST
#endif

#include <array>
#include <exception>
#include <string_view>
Expand All @@ -44,7 +40,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<std::string> kSupportedCompressionList{"gzip"}; // specify the compression algo in priority level

canepat marked this conversation as resolved.
Show resolved Hide resolved
Connection::Connection(boost::asio::io_context& io_context,
commands::RpcApi& api,
Expand Down Expand Up @@ -183,23 +178,9 @@ Task<void> Connection::handle_actual_request(const boost::beast::http::request<b
co_return;
}

std::string selected_compression = "";
if (http_compression_ && !accept_encoding.empty()) {
selected_compression = select_compression_algo(accept_encoding);
if (selected_compression.empty()) {
std::string complete_list;
bool first = true;
for (std::string curr_compression : kSupportedCompressionList) {
if (first) {
first = false;
} else {
complete_list += ", ";
}
complete_list += curr_compression;
}
co_await do_write("unsupported requested compression\n", boost::beast::http::status::unsupported_media_type, complete_list);
co_return;
}
if (http_compression_ && !accept_encoding.empty() && accept_encoding != "gzip") {
canepat marked this conversation as resolved.
Show resolved Hide resolved
co_await do_write("unsupported requested compression\n", boost::beast::http::status::unsupported_media_type, "gzip");
co_return;
}

if (!is_method_allowed(req.method())) {
Expand Down Expand Up @@ -231,7 +212,7 @@ Task<void> Connection::handle_actual_request(const boost::beast::http::request<b

auto rsp_content = co_await request_handler_.handle(req.body());
if (rsp_content) {
co_await do_write(rsp_content->append("\n"), boost::beast::http::status::ok, selected_compression);
co_await do_write(rsp_content->append("\n"), boost::beast::http::status::ok, accept_encoding);
canepat marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down Expand Up @@ -306,15 +287,15 @@ Task<void> 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()) {
Expand All @@ -340,7 +321,7 @@ Task<void> 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));
Expand Down Expand Up @@ -415,15 +396,6 @@ void Connection::set_cors(boost::beast::http::response<Body>& 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<std::string>& allowed_origins, const std::string& origin) {
if (allowed_origins.size() == 1 && allowed_origins[0] == "*") {
return true;
Expand Down
4 changes: 1 addition & 3 deletions silkworm/rpc/http/connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand Down
Loading