@@ -217,12 +217,16 @@ Task<void> Connection::handle_actual_request(const RequestWithStringBody& req, R
217217 co_return ;
218218 }
219219
220+ request_map_.emplace (request_id_, std::move (request_data));
220221 auto rsp_content = co_await handler_->handle (req.body (), request_id_);
221222 if (rsp_content) {
222223 // no streaming
223- 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 );
224- } else {
225- request_map_.emplace (request_id_, std::move (request_data));
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_);
227+ if (it != request_map_.end ()) {
228+ request_map_.erase (it);
229+ }
226230 }
227231 request_id_++;
228232}
@@ -255,10 +259,10 @@ Task<void> Connection::create_chunk_header(RequestData& request_data) {
255259}
256260
257261Task<void > Connection::open_stream (uint64_t request_id) {
258- auto request_data_it = request_map_.find (request_id);
262+ const auto request_data_it = request_map_.find (request_id);
259263 if (request_data_it == request_map_.end ()) {
260264 SILK_ERROR << " Connection::open_stream request_id not found: " << request_id;
261- co_return ;
265+ SILKWORM_ASSERT ( false ) ;
262266 }
263267 auto & request_data = request_data_it->second ;
264268
@@ -269,15 +273,15 @@ Task<void> Connection::open_stream(uint64_t request_id) {
269273}
270274
271275Task<void > Connection::close_stream (uint64_t request_id) {
272- auto request_data_it = request_map_.find (request_id);
276+ const auto request_data_it = request_map_.find (request_id);
273277 if (request_data_it == request_map_.end ()) {
274278 SILK_ERROR << " Connection::close_stream request_id not found: " << request_id;
275- co_return ;
279+ SILKWORM_ASSERT ( false ) ;
276280 }
277281 auto & request_data = request_data_it->second ;
278282
279283 try {
280- // Get remianing chunk and flush it
284+ // Get remaining chunk and flush it
281285 auto [chunk, first_chunk] = request_data.chunk ->get_remainder ();
282286 if (first_chunk) {
283287 if (!chunk.empty ()) {
@@ -293,9 +297,11 @@ Task<void> Connection::close_stream(uint64_t request_id) {
293297 co_await boost::asio::async_write (socket_, boost::beast::http::make_chunk_last (), boost::asio::use_awaitable);
294298 }
295299 } catch (const boost::system::system_error& se) {
300+ request_map_.erase (request_data_it);
296301 SILK_TRACE << " Connection::close system_error: " << se.what ();
297302 throw ;
298303 } catch (const std::exception& e) {
304+ request_map_.erase (request_data_it);
299305 SILK_ERROR << " Connection::close exception: " << e.what ();
300306 throw ;
301307 }
@@ -306,10 +312,10 @@ Task<void> Connection::close_stream(uint64_t request_id) {
306312
307313// ! Write chunked response content to the underlying socket
308314Task<size_t > Connection::write (uint64_t request_id, std::string_view content, bool last) {
309- auto request_data_it = request_map_.find (request_id);
315+ const auto request_data_it = request_map_.find (request_id);
310316 if (request_data_it == request_map_.end ()) {
311317 SILK_ERROR << " Connection::write request_id not found: " << request_id;
312- co_return 0 ;
318+ SILKWORM_ASSERT ( false ) ;
313319 }
314320 auto & request_data = request_data_it->second ;
315321
@@ -357,7 +363,7 @@ Task<size_t> Connection::send_chunk(const std::string& content) {
357363 co_return bytes_transferred;
358364}
359365
360- 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) {
361367 try {
362368 SILK_TRACE << " Connection::do_write response: " << http_status << " content: " << content;
363369 boost::beast::http::response<boost::beast::http::string_body> res{http_status, request_data.request_http_version };
@@ -460,7 +466,7 @@ Connection::AuthorizationResult Connection::is_request_authorized(const RequestW
460466}
461467
462468template <class Body >
463- void Connection::set_cors (boost::beast::http::response<Body>& res, RequestData& request_data) {
469+ void Connection::set_cors (boost::beast::http::response<Body>& res, const RequestData& request_data) {
464470 if (request_data.vary .empty ()) {
465471 res.set (boost::beast::http::field::vary, " Origin" );
466472 } else {
0 commit comments