@@ -103,12 +103,12 @@ Task<bool> Connection::do_read() {
103
103
auto gzip_encoding_requested = accept_encoding.contains (kGzipEncoding ) && http_compression_;
104
104
105
105
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 (),
112
112
};
113
113
114
114
if (boost::beast::websocket::is_upgrade (parser.get ())) {
@@ -153,7 +153,7 @@ Task<void> Connection::handle_request(const RequestWithStringBody& req, RequestD
153
153
}
154
154
155
155
Task<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 };
157
157
std::string vary = req[boost::beast::http::field::vary];
158
158
159
159
if (vary.empty ()) {
@@ -193,7 +193,7 @@ Task<void> Connection::handle_actual_request(const RequestWithStringBody& req, R
193
193
co_return ;
194
194
}
195
195
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 ) {
197
197
co_await do_write (" unsupported requested compression\n " , boost::beast::http::status::unsupported_media_type, request_data, kGzipEncoding );
198
198
co_return ;
199
199
}
@@ -220,9 +220,10 @@ Task<void> Connection::handle_actual_request(const RequestWithStringBody& req, R
220
220
request_map_.emplace (request_id_, std::move (request_data));
221
221
auto rsp_content = co_await handler_->handle (req.body (), request_id_);
222
222
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_);
226
227
if (it != request_map_.end ()) {
227
228
request_map_.erase (it);
228
229
}
@@ -233,12 +234,12 @@ Task<void> Connection::handle_actual_request(const RequestWithStringBody& req, R
233
234
// ! Write chunked response headers
234
235
Task<void > Connection::create_chunk_header (RequestData& request_data) {
235
236
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 };
237
238
rsp.set (boost::beast::http::field::content_type, " application/json" );
238
239
rsp.set (boost::beast::http::field::date, get_date_time ());
239
240
rsp.chunked (true );
240
241
241
- if (request_data.gzip_encoding_requested_ ) {
242
+ if (request_data.gzip_encoding_requested ) {
242
243
rsp.set (boost::beast::http::field::content_encoding, kGzipEncoding );
243
244
}
244
245
@@ -258,47 +259,49 @@ Task<void> Connection::create_chunk_header(RequestData& request_data) {
258
259
}
259
260
260
261
Task<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);
262
263
if (request_data_it == request_map_.end ()) {
263
264
SILK_ERROR << " Connection::open_stream request_id not found: " << request_id;
264
- co_return ;
265
+ SILKWORM_ASSERT ( false ) ;
265
266
}
266
267
auto & request_data = request_data_it->second ;
267
268
268
269
// add chunking supports
269
- request_data.chunk_ = std::make_unique<Chunker>();
270
+ request_data.chunk = std::make_unique<Chunker>();
270
271
271
272
co_return ;
272
273
}
273
274
274
275
Task<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);
276
277
if (request_data_it == request_map_.end ()) {
277
278
SILK_ERROR << " Connection::close_stream request_id not found: " << request_id;
278
- co_return ;
279
+ SILKWORM_ASSERT ( false ) ;
279
280
}
280
281
auto & request_data = request_data_it->second ;
281
282
282
283
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 ();
285
286
if (first_chunk) {
286
287
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
289
290
}
290
291
} else {
291
- // already a chunk is generated
292
+ // A previous chunk was already generated
292
293
if (!chunk.empty ()) {
293
- // send new one
294
+ // Send the new one
294
295
co_await send_chunk (chunk);
295
296
}
296
297
co_await boost::asio::async_write (socket_, boost::beast::http::make_chunk_last (), boost::asio::use_awaitable);
297
298
}
298
299
} catch (const boost::system::system_error& se) {
300
+ request_map_.erase (request_data_it);
299
301
SILK_TRACE << " Connection::close system_error: " << se.what ();
300
302
throw ;
301
303
} catch (const std::exception& e) {
304
+ request_map_.erase (request_data_it);
302
305
SILK_ERROR << " Connection::close exception: " << e.what ();
303
306
throw ;
304
307
}
@@ -309,10 +312,10 @@ Task<void> Connection::close_stream(uint64_t request_id) {
309
312
310
313
// ! Write chunked response content to the underlying socket
311
314
Task<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);
313
316
if (request_data_it == request_map_.end ()) {
314
317
SILK_ERROR << " Connection::write request_id not found: " << request_id;
315
- co_return 0 ;
318
+ SILKWORM_ASSERT ( false ) ;
316
319
}
317
320
auto & request_data = request_data_it->second ;
318
321
@@ -321,19 +324,19 @@ Task<size_t> Connection::write(uint64_t request_id, std::string_view content, bo
321
324
response.append (" \n " );
322
325
}
323
326
324
- if (request_data.gzip_encoding_requested_ ) {
327
+ if (request_data.gzip_encoding_requested ) {
325
328
std::string compressed_content;
326
- co_await compress (response. data () , compressed_content);
329
+ co_await compress (response, compressed_content);
327
330
// queued compressed buffer
328
- request_data.chunk_ ->queue_data (std::move ( compressed_content) );
331
+ request_data.chunk ->queue_data (compressed_content);
329
332
} else {
330
333
// queued clear buffer
331
- request_data.chunk_ ->queue_data (std::move ( response) );
334
+ request_data.chunk ->queue_data (response);
332
335
}
333
336
334
337
// 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 ();
337
340
338
341
if (first_chunk) {
339
342
co_await create_chunk_header (request_data);
@@ -360,10 +363,10 @@ Task<size_t> Connection::send_chunk(const std::string& content) {
360
363
co_return bytes_transferred;
361
364
}
362
365
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) {
364
367
try {
365
368
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 };
367
370
368
371
if (http_status != boost::beast::http::status::ok) {
369
372
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::
373
376
374
377
res.set (boost::beast::http::field::date, get_date_time ());
375
378
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 );
377
380
if (http_status == boost::beast::http::status::ok && !content_encoding.empty ()) {
378
381
// Positive response w/ compression required
379
382
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::
385
388
res.body () = std::move (compressed_content);
386
389
} else {
387
390
res.content_length (content.size ());
388
- res.body () = std::move ( content) ;
391
+ res.body () = content;
389
392
}
390
393
391
394
} else {
@@ -394,7 +397,7 @@ Task<void> Connection::do_write(const std::string& content, boost::beast::http::
394
397
res.set (boost::beast::http::field::accept_encoding, content_encoding); // Indicate the supported encoding
395
398
}
396
399
res.content_length (content.size ());
397
- res.body () = std::move ( content) ;
400
+ res.body () = content;
398
401
}
399
402
400
403
set_cors<boost::beast::http::string_body>(res, request_data);
@@ -463,30 +466,30 @@ Connection::AuthorizationResult Connection::is_request_authorized(const RequestW
463
466
}
464
467
465
468
template <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 ()) {
468
471
res.set (boost::beast::http::field::vary, " Origin" );
469
472
} else {
470
- auto vary{request_data.vary_ };
473
+ auto vary{request_data.vary };
471
474
res.set (boost::beast::http::field::vary, vary.append (" Origin" ));
472
475
}
473
476
474
- if (request_data.origin_ .empty ()) {
477
+ if (request_data.origin .empty ()) {
475
478
return ;
476
479
}
477
480
478
- if (!is_origin_allowed (allowed_origins_, request_data.origin_ )) {
481
+ if (!is_origin_allowed (allowed_origins_, request_data.origin )) {
479
482
return ;
480
483
}
481
484
482
- if (!is_method_allowed (request_data.method_ )) {
485
+ if (!is_method_allowed (request_data.method )) {
483
486
return ;
484
487
}
485
488
486
489
if (allowed_origins_.at (0 ) == " *" ) {
487
490
res.set (boost::beast::http::field::access_control_allow_origin, " *" );
488
491
} 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 );
490
493
}
491
494
}
492
495
0 commit comments