1616
1717#include " connection.hpp"
1818
19- #ifndef ZLIB_CONST
20- #define ZLIB_CONST
21- #endif
22-
2319#include < array>
2420#include < exception>
2521#include < string_view>
@@ -44,7 +40,7 @@ namespace silkworm::rpc::http {
4440static constexpr std::string_view kMaxAge {" 600" };
4541static constexpr auto kMaxPayloadSize {30 * kMebi }; // 30MiB
4642static constexpr std::array kAcceptedContentTypes {" application/json" , " application/jsonrequest" , " application/json-rpc" };
47- static std::vector<std::string> kSupportedCompressionList {" gzip" }; // specify the compression algo in priority level
43+ static constexpr auto kGzipEncoding {" gzip" };
4844
4945Connection::Connection (boost::asio::io_context& io_context,
5046 commands::RpcApi& api,
@@ -183,23 +179,10 @@ Task<void> Connection::handle_actual_request(const boost::beast::http::request<b
183179 co_return ;
184180 }
185181
186- std::string selected_compression = " " ;
187- if (http_compression_ && !accept_encoding.empty ()) {
188- selected_compression = select_compression_algo (accept_encoding);
189- if (selected_compression.empty ()) {
190- std::string complete_list;
191- bool first = true ;
192- for (std::string curr_compression : kSupportedCompressionList ) {
193- if (first) {
194- first = false ;
195- } else {
196- complete_list += " , " ;
197- }
198- complete_list += curr_compression;
199- }
200- co_await do_write (" unsupported requested compression\n " , boost::beast::http::status::unsupported_media_type, complete_list);
201- co_return ;
202- }
182+ const bool gzip_encoding_requested{accept_encoding.contains (kGzipEncoding )};
183+ if (http_compression_ && !accept_encoding.empty () && !gzip_encoding_requested) {
184+ co_await do_write (" unsupported requested compression\n " , boost::beast::http::status::unsupported_media_type, kGzipEncoding );
185+ co_return ;
203186 }
204187
205188 if (!is_method_allowed (req.method ())) {
@@ -231,7 +214,7 @@ Task<void> Connection::handle_actual_request(const boost::beast::http::request<b
231214
232215 auto rsp_content = co_await request_handler_.handle (req.body ());
233216 if (rsp_content) {
234- co_await do_write (rsp_content->append (" \n " ), boost::beast::http::status::ok, selected_compression );
217+ co_await do_write (rsp_content->append (" \n " ), boost::beast::http::status::ok, gzip_encoding_requested ? kGzipEncoding : " " );
235218 }
236219}
237220
@@ -306,15 +289,15 @@ Task<void> Connection::do_write(const std::string& content, boost::beast::http::
306289 if (http_status == boost::beast::http::status::ok && !content_encoding.empty ()) {
307290 // Positive response w/ compression required
308291 res.set (boost::beast::http::field::content_encoding, content_encoding);
309- std::string compressed_data ;
292+ std::string compressed_content ;
310293 try {
311- compress_data (content, compressed_data );
294+ compress (content, compressed_content );
312295 } catch (const std::exception& e) {
313296 SILK_ERROR << " Connection::do_write cannot compress exception: " << e.what ();
314297 throw ;
315298 }
316- res.content_length (compressed_data .length ());
317- res.body () = std::move (compressed_data );
299+ res.content_length (compressed_content .length ());
300+ res.body () = std::move (compressed_content );
318301 } else {
319302 // Any negative response or positive response w/o compression
320303 if (!content_encoding.empty ()) {
@@ -340,7 +323,7 @@ Task<void> Connection::do_write(const std::string& content, boost::beast::http::
340323 co_return ;
341324}
342325
343- void Connection::compress_data (const std::string& clear_data, std::string& compressed_data) {
326+ void Connection::compress (const std::string& clear_data, std::string& compressed_data) {
344327 boost::iostreams::filtering_ostream out;
345328 out.push (boost::iostreams::gzip_compressor ());
346329 out.push (boost::iostreams::back_inserter (compressed_data));
@@ -415,15 +398,6 @@ void Connection::set_cors(boost::beast::http::response<Body>& res) {
415398 }
416399}
417400
418- std::string Connection::select_compression_algo (const std::string& requested_compression) {
419- for (std::string curr_compression : kSupportedCompressionList ) {
420- if (requested_compression.find (curr_compression) != std::string::npos) {
421- return curr_compression;
422- }
423- }
424- return " " ;
425- }
426-
427401bool Connection::is_origin_allowed (const std::vector<std::string>& allowed_origins, const std::string& origin) {
428402 if (allowed_origins.size () == 1 && allowed_origins[0 ] == " *" ) {
429403 return true ;
0 commit comments