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: negotiation and validation of HTTP compression scheme #1914

Merged
merged 5 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
61 changes: 53 additions & 8 deletions silkworm/rpc/http/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <jwt-cpp/jwt.h>
#include <jwt-cpp/traits/nlohmann-json/defaults.h>

#include <silkworm/core/common/assert.hpp>
#include <silkworm/infra/common/log.hpp>
#include <silkworm/rpc/common/util.hpp>

Expand All @@ -42,6 +43,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 std::vector<std::string> SupportedCompressionList{"gzip"}; // specify the compression algo in priority level
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that we need to support just one compression scheme (i.e. gzip), it's better to avoid handling a list of schemes: we will do it in the future if necessary. No need to do the changes here, next PR is fine


Connection::Connection(boost::asio::io_context& io_context,
commands::RpcApi& api,
Expand Down Expand Up @@ -174,6 +176,31 @@ Task<void> Connection::handle_actual_request(const boost::beast::http::request<b
co_return;
}

const auto accept_encoding = req[boost::beast::http::field::accept_encoding];
if (!http_compression_ && !accept_encoding.empty()) {
co_await do_write("unsupported compression\n", boost::beast::http::status::unsupported_media_type, "identity");
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 : SupportedCompressionList) {
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 (!is_method_allowed(req.method())) {
co_await do_write("method not allowed\n", boost::beast::http::status::method_not_allowed);
co_return;
Expand All @@ -200,11 +227,10 @@ Task<void> Connection::handle_actual_request(const boost::beast::http::request<b
vary_ = req[boost::beast::http::field::vary];
origin_ = req[boost::beast::http::field::origin];
method_ = req.method();
auto encoding = req[boost::beast::http::field::accept_encoding];

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, !encoding.empty());
co_await do_write(rsp_content->append("\n"), boost::beast::http::status::ok, selected_compression);
}
}

Expand Down Expand Up @@ -262,7 +288,7 @@ Task<std::size_t> Connection::write(std::string_view content, bool /*last*/) {
co_return bytes_transferred;
}

Task<void> Connection::do_write(const std::string& content, boost::beast::http::status http_status, bool compress) {
Task<void> Connection::do_write(const std::string& content, boost::beast::http::status http_status, const std::string& compression_algo) {
try {
SILK_TRACE << "Connection::do_write response: " << http_status << " content: " << content;
boost::beast::http::response<boost::beast::http::string_body> res{http_status, request_http_version_};
Expand All @@ -276,19 +302,23 @@ Task<void> Connection::do_write(const std::string& content, boost::beast::http::
res.set(boost::beast::http::field::date, get_date_time());
res.erase(boost::beast::http::field::host);
res.keep_alive(request_keep_alive_);
if (compress) {
const std::string compression_type = "gzip";
res.set(boost::beast::http::field::content_encoding, compression_type);
if (http_status == boost::beast::http::status::ok && !compression_algo.empty()) {
// in case of compression
res.set(boost::beast::http::field::content_encoding, compression_algo);
std::string compressed_data;
try {
compress_data(content, compressed_data);
compress_data(content, compressed_data, compression_algo);
} catch (const std::exception& e) {
SILK_ERROR << "Connection::compress_data exception: " << e.what();
throw;
}
res.content_length(compressed_data.length());
res.body() = std::move(compressed_data);
} else {
// in case of fail response or postive without compression
if (!compression_algo.empty()) {
res.set(boost::beast::http::field::accept_encoding, compression_algo); // set response to indicates the supported algo
}
res.content_length(content.size());
res.body() = content;
}
Expand All @@ -309,9 +339,15 @@ 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_data(const std::string& clear_data, std::string& compressed_data, const std::string& compression_algo) {
z_stream strm;

assert(compression_algo == "gzip");

if (compression_algo != "gzip") {
throw std::runtime_error("unsupported compression algo");
}

std::memset(&strm, 0, sizeof(strm));
int ret = Z_OK;
ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 15 | 16, 8, Z_DEFAULT_STRATEGY);
Expand Down Expand Up @@ -407,6 +443,15 @@ 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 : SupportedCompressionList) {
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
6 changes: 4 additions & 2 deletions silkworm/rpc/http/connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,13 @@ class Connection : public StreamWriter {
Task<bool> do_read();

//! Perform an asynchronous write operation.
Task<void> do_write(const std::string& content, boost::beast::http::status http_status, bool compress = false);
Task<void> do_write(const std::string& content, boost::beast::http::status http_status, const std::string& content_encoding = "");

static std::string get_date_time();

void compress_data(const std::string& clear_data, std::string& compressed_data);
std::string select_compression_algo(const std::string& request_compression);

void compress_data(const std::string& clear_data, std::string& compressed_data, const std::string& compression_algo);

//! Socket for the connection.
boost::asio::ip::tcp::socket socket_;
Expand Down
Loading