@@ -217,12 +217,16 @@ Task<void> Connection::handle_actual_request(const RequestWithStringBody& req, R
217
217
co_return ;
218
218
}
219
219
220
+ request_map_.emplace (request_id_, std::move (request_data));
220
221
auto rsp_content = co_await handler_->handle (req.body (), request_id_);
221
222
if (rsp_content) {
222
223
// 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
+ }
226
230
}
227
231
request_id_++;
228
232
}
@@ -255,10 +259,10 @@ Task<void> Connection::create_chunk_header(RequestData& request_data) {
255
259
}
256
260
257
261
Task<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);
259
263
if (request_data_it == request_map_.end ()) {
260
264
SILK_ERROR << " Connection::open_stream request_id not found: " << request_id;
261
- co_return ;
265
+ SILKWORM_ASSERT ( false ) ;
262
266
}
263
267
auto & request_data = request_data_it->second ;
264
268
@@ -269,15 +273,15 @@ Task<void> Connection::open_stream(uint64_t request_id) {
269
273
}
270
274
271
275
Task<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);
273
277
if (request_data_it == request_map_.end ()) {
274
278
SILK_ERROR << " Connection::close_stream request_id not found: " << request_id;
275
- co_return ;
279
+ SILKWORM_ASSERT ( false ) ;
276
280
}
277
281
auto & request_data = request_data_it->second ;
278
282
279
283
try {
280
- // Get remianing chunk and flush it
284
+ // Get remaining chunk and flush it
281
285
auto [chunk, first_chunk] = request_data.chunk ->get_remainder ();
282
286
if (first_chunk) {
283
287
if (!chunk.empty ()) {
@@ -293,9 +297,11 @@ Task<void> Connection::close_stream(uint64_t request_id) {
293
297
co_await boost::asio::async_write (socket_, boost::beast::http::make_chunk_last (), boost::asio::use_awaitable);
294
298
}
295
299
} catch (const boost::system::system_error& se) {
300
+ request_map_.erase (request_data_it);
296
301
SILK_TRACE << " Connection::close system_error: " << se.what ();
297
302
throw ;
298
303
} catch (const std::exception& e) {
304
+ request_map_.erase (request_data_it);
299
305
SILK_ERROR << " Connection::close exception: " << e.what ();
300
306
throw ;
301
307
}
@@ -306,10 +312,10 @@ Task<void> Connection::close_stream(uint64_t request_id) {
306
312
307
313
// ! Write chunked response content to the underlying socket
308
314
Task<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);
310
316
if (request_data_it == request_map_.end ()) {
311
317
SILK_ERROR << " Connection::write request_id not found: " << request_id;
312
- co_return 0 ;
318
+ SILKWORM_ASSERT ( false ) ;
313
319
}
314
320
auto & request_data = request_data_it->second ;
315
321
@@ -357,7 +363,7 @@ Task<size_t> Connection::send_chunk(const std::string& content) {
357
363
co_return bytes_transferred;
358
364
}
359
365
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) {
361
367
try {
362
368
SILK_TRACE << " Connection::do_write response: " << http_status << " content: " << content;
363
369
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
460
466
}
461
467
462
468
template <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) {
464
470
if (request_data.vary .empty ()) {
465
471
res.set (boost::beast::http::field::vary, " Origin" );
466
472
} else {
0 commit comments