@@ -103,12 +103,12 @@ Task<bool> Connection::do_read() {
103103 auto gzip_encoding_requested = accept_encoding.contains (kGzipEncoding ) && http_compression_;
104104
105105 RequestData request_data{
106- .request_keep_alive_ = parser.get ().keep_alive (),
107- .request_http_version_ = parser.get ().version (),
108- .gzip_encoding_requested_ = gzip_encoding_requested,
109- .vary_ = req[boost::beast::http::field::vary],
110- .origin_ = req[boost::beast::http::field::origin],
111- .method_ = req.method (),
106+ .request_keep_alive = parser.get ().keep_alive (),
107+ .request_http_version = parser.get ().version (),
108+ .gzip_encoding_requested = gzip_encoding_requested,
109+ .vary = req[boost::beast::http::field::vary],
110+ .origin = req[boost::beast::http::field::origin],
111+ .method = req.method (),
112112 };
113113
114114 if (boost::beast::websocket::is_upgrade (parser.get ())) {
@@ -153,7 +153,7 @@ Task<void> Connection::handle_request(const RequestWithStringBody& req, RequestD
153153}
154154
155155Task<void > Connection::handle_preflight (const RequestWithStringBody& req, RequestData& request_data) {
156- boost::beast::http::response<boost::beast::http::string_body> res{boost::beast::http::status::no_content, request_data.request_http_version_ };
156+ boost::beast::http::response<boost::beast::http::string_body> res{boost::beast::http::status::no_content, request_data.request_http_version };
157157 std::string vary = req[boost::beast::http::field::vary];
158158
159159 if (vary.empty ()) {
@@ -193,7 +193,7 @@ Task<void> Connection::handle_actual_request(const RequestWithStringBody& req, R
193193 co_return ;
194194 }
195195
196- if (http_compression_ && !accept_encoding.empty () && !accept_encoding.contains (kIdentity ) && !request_data.gzip_encoding_requested_ ) {
196+ if (http_compression_ && !accept_encoding.empty () && !accept_encoding.contains (kIdentity ) && !request_data.gzip_encoding_requested ) {
197197 co_await do_write (" unsupported requested compression\n " , boost::beast::http::status::unsupported_media_type, request_data, kGzipEncoding );
198198 co_return ;
199199 }
@@ -220,9 +220,10 @@ Task<void> Connection::handle_actual_request(const RequestWithStringBody& req, R
220220 request_map_.emplace (request_id_, std::move (request_data));
221221 auto rsp_content = co_await handler_->handle (req.body (), request_id_);
222222 if (rsp_content) {
223- // no streaming api
224- co_await do_write (rsp_content->append (" \n " ), boost::beast::http::status::ok, request_data, request_data.gzip_encoding_requested_ ? kGzipEncoding : " " , request_data.gzip_encoding_requested_ );
225- auto it = request_map_.find (request_id_);
223+ // no streaming
224+ const auto & req_data = request_map_.at (request_id_);
225+ co_await do_write (rsp_content->append (" \n " ), boost::beast::http::status::ok, req_data, req_data.gzip_encoding_requested ? kGzipEncoding : " " , req_data.gzip_encoding_requested );
226+ const auto it = request_map_.find (request_id_);
226227 if (it != request_map_.end ()) {
227228 request_map_.erase (it);
228229 }
@@ -233,12 +234,12 @@ Task<void> Connection::handle_actual_request(const RequestWithStringBody& req, R
233234// ! Write chunked response headers
234235Task<void > Connection::create_chunk_header (RequestData& request_data) {
235236 try {
236- boost::beast::http::response<boost::beast::http::empty_body> rsp{boost::beast::http::status::ok, request_data.request_http_version_ };
237+ boost::beast::http::response<boost::beast::http::empty_body> rsp{boost::beast::http::status::ok, request_data.request_http_version };
237238 rsp.set (boost::beast::http::field::content_type, " application/json" );
238239 rsp.set (boost::beast::http::field::date, get_date_time ());
239240 rsp.chunked (true );
240241
241- if (request_data.gzip_encoding_requested_ ) {
242+ if (request_data.gzip_encoding_requested ) {
242243 rsp.set (boost::beast::http::field::content_encoding, kGzipEncoding );
243244 }
244245
@@ -258,47 +259,49 @@ Task<void> Connection::create_chunk_header(RequestData& request_data) {
258259}
259260
260261Task<void > Connection::open_stream (uint64_t request_id) {
261- auto request_data_it = request_map_.find (request_id);
262+ const auto request_data_it = request_map_.find (request_id);
262263 if (request_data_it == request_map_.end ()) {
263264 SILK_ERROR << " Connection::open_stream request_id not found: " << request_id;
264- co_return ;
265+ SILKWORM_ASSERT ( false ) ;
265266 }
266267 auto & request_data = request_data_it->second ;
267268
268269 // add chunking supports
269- request_data.chunk_ = std::make_unique<Chunker>();
270+ request_data.chunk = std::make_unique<Chunker>();
270271
271272 co_return ;
272273}
273274
274275Task<void > Connection::close_stream (uint64_t request_id) {
275- auto request_data_it = request_map_.find (request_id);
276+ const auto request_data_it = request_map_.find (request_id);
276277 if (request_data_it == request_map_.end ()) {
277278 SILK_ERROR << " Connection::close_stream request_id not found: " << request_id;
278- co_return ;
279+ SILKWORM_ASSERT ( false ) ;
279280 }
280281 auto & request_data = request_data_it->second ;
281282
282283 try {
283- // get chunk remainder and flush it
284- auto [chunk, first_chunk] = request_data.chunk_ ->get_remainder ();
284+ // Get remaining chunk and flush it
285+ auto [chunk, first_chunk] = request_data.chunk ->get_remainder ();
285286 if (first_chunk) {
286287 if (!chunk.empty ()) {
287- // it is first chunk so send full msg without chunking
288- co_await do_write (chunk, boost::beast::http::status::ok, request_data, request_data.gzip_encoding_requested_ ? kGzipEncoding : " " , /* to_be_compressed */ false ); // data already compressed if nec
288+ // If it is the first chunk, send without chunking
289+ co_await do_write (chunk, boost::beast::http::status::ok, request_data, request_data.gzip_encoding_requested ? kGzipEncoding : " " , /* to_be_compressed */ false ); // data already compressed if nec
289290 }
290291 } else {
291- // already a chunk is generated
292+ // A previous chunk was already generated
292293 if (!chunk.empty ()) {
293- // send new one
294+ // Send the new one
294295 co_await send_chunk (chunk);
295296 }
296297 co_await boost::asio::async_write (socket_, boost::beast::http::make_chunk_last (), boost::asio::use_awaitable);
297298 }
298299 } catch (const boost::system::system_error& se) {
300+ request_map_.erase (request_data_it);
299301 SILK_TRACE << " Connection::close system_error: " << se.what ();
300302 throw ;
301303 } catch (const std::exception& e) {
304+ request_map_.erase (request_data_it);
302305 SILK_ERROR << " Connection::close exception: " << e.what ();
303306 throw ;
304307 }
@@ -309,10 +312,10 @@ Task<void> Connection::close_stream(uint64_t request_id) {
309312
310313// ! Write chunked response content to the underlying socket
311314Task<size_t > Connection::write (uint64_t request_id, std::string_view content, bool last) {
312- auto request_data_it = request_map_.find (request_id);
315+ const auto request_data_it = request_map_.find (request_id);
313316 if (request_data_it == request_map_.end ()) {
314317 SILK_ERROR << " Connection::write request_id not found: " << request_id;
315- co_return 0 ;
318+ SILKWORM_ASSERT ( false ) ;
316319 }
317320 auto & request_data = request_data_it->second ;
318321
@@ -321,19 +324,19 @@ Task<size_t> Connection::write(uint64_t request_id, std::string_view content, bo
321324 response.append (" \n " );
322325 }
323326
324- if (request_data.gzip_encoding_requested_ ) {
327+ if (request_data.gzip_encoding_requested ) {
325328 std::string compressed_content;
326- co_await compress (response. data () , compressed_content);
329+ co_await compress (response, compressed_content);
327330 // queued compressed buffer
328- request_data.chunk_ ->queue_data (std::move ( compressed_content) );
331+ request_data.chunk ->queue_data (compressed_content);
329332 } else {
330333 // queued clear buffer
331- request_data.chunk_ ->queue_data (std::move ( response) );
334+ request_data.chunk ->queue_data (response);
332335 }
333336
334337 // until completed chunk are present
335- while (request_data.chunk_ ->has_chunks ()) {
336- auto [complete_chunk, first_chunk] = request_data.chunk_ ->get_complete_chunk ();
338+ while (request_data.chunk ->has_chunks ()) {
339+ auto [complete_chunk, first_chunk] = request_data.chunk ->get_complete_chunk ();
337340
338341 if (first_chunk) {
339342 co_await create_chunk_header (request_data);
@@ -360,10 +363,10 @@ Task<size_t> Connection::send_chunk(const std::string& content) {
360363 co_return bytes_transferred;
361364}
362365
363- Task<void > Connection::do_write (const std::string& content, boost::beast::http::status http_status, RequestData& request_data, std::string_view content_encoding, bool to_be_compressed) {
366+ Task<void > Connection::do_write (const std::string& content, boost::beast::http::status http_status, const RequestData& request_data, std::string_view content_encoding, bool to_be_compressed) {
364367 try {
365368 SILK_TRACE << " Connection::do_write response: " << http_status << " content: " << content;
366- boost::beast::http::response<boost::beast::http::string_body> res{http_status, request_data.request_http_version_ };
369+ boost::beast::http::response<boost::beast::http::string_body> res{http_status, request_data.request_http_version };
367370
368371 if (http_status != boost::beast::http::status::ok) {
369372 res.set (boost::beast::http::field::content_type, " text/plain" );
@@ -373,7 +376,7 @@ Task<void> Connection::do_write(const std::string& content, boost::beast::http::
373376
374377 res.set (boost::beast::http::field::date, get_date_time ());
375378 res.erase (boost::beast::http::field::host);
376- res.keep_alive (request_data.request_keep_alive_ );
379+ res.keep_alive (request_data.request_keep_alive );
377380 if (http_status == boost::beast::http::status::ok && !content_encoding.empty ()) {
378381 // Positive response w/ compression required
379382 res.set (boost::beast::http::field::content_encoding, content_encoding);
@@ -385,7 +388,7 @@ Task<void> Connection::do_write(const std::string& content, boost::beast::http::
385388 res.body () = std::move (compressed_content);
386389 } else {
387390 res.content_length (content.size ());
388- res.body () = std::move ( content) ;
391+ res.body () = content;
389392 }
390393
391394 } else {
@@ -394,7 +397,7 @@ Task<void> Connection::do_write(const std::string& content, boost::beast::http::
394397 res.set (boost::beast::http::field::accept_encoding, content_encoding); // Indicate the supported encoding
395398 }
396399 res.content_length (content.size ());
397- res.body () = std::move ( content) ;
400+ res.body () = content;
398401 }
399402
400403 set_cors<boost::beast::http::string_body>(res, request_data);
@@ -463,30 +466,30 @@ Connection::AuthorizationResult Connection::is_request_authorized(const RequestW
463466}
464467
465468template <class Body >
466- void Connection::set_cors (boost::beast::http::response<Body>& res, RequestData& request_data) {
467- if (request_data.vary_ .empty ()) {
469+ void Connection::set_cors (boost::beast::http::response<Body>& res, const RequestData& request_data) {
470+ if (request_data.vary .empty ()) {
468471 res.set (boost::beast::http::field::vary, " Origin" );
469472 } else {
470- auto vary{request_data.vary_ };
473+ auto vary{request_data.vary };
471474 res.set (boost::beast::http::field::vary, vary.append (" Origin" ));
472475 }
473476
474- if (request_data.origin_ .empty ()) {
477+ if (request_data.origin .empty ()) {
475478 return ;
476479 }
477480
478- if (!is_origin_allowed (allowed_origins_, request_data.origin_ )) {
481+ if (!is_origin_allowed (allowed_origins_, request_data.origin )) {
479482 return ;
480483 }
481484
482- if (!is_method_allowed (request_data.method_ )) {
485+ if (!is_method_allowed (request_data.method )) {
483486 return ;
484487 }
485488
486489 if (allowed_origins_.at (0 ) == " *" ) {
487490 res.set (boost::beast::http::field::access_control_allow_origin, " *" );
488491 } else {
489- res.set (boost::beast::http::field::access_control_allow_origin, request_data.origin_ );
492+ res.set (boost::beast::http::field::access_control_allow_origin, request_data.origin );
490493 }
491494}
492495
0 commit comments