@@ -220,82 +220,83 @@ class coro_http_connection
220
220
co_await router_.route_coro (coro_handler, request_, response_, key);
221
221
}
222
222
else {
223
- if (default_handler_) {
224
- co_await default_handler_ (request_, response_);
223
+ bool is_exist = false ;
224
+ bool is_coro_exist = false ;
225
+ bool is_matched_regex_router = false ;
226
+ std::function<void (coro_http_request & req,
227
+ coro_http_response & resp)>
228
+ handler;
229
+ std::string method_str{parser_.method ()};
230
+ std::string url_path = method_str;
231
+ url_path.append (" " ).append (parser_.url ());
232
+ std::tie (is_exist, handler, request_.params_ ) =
233
+ router_.get_router_tree ()->get (url_path, method_str);
234
+ if (is_exist) {
235
+ if (handler) {
236
+ (handler)(request_, response_);
237
+ }
238
+ else {
239
+ response_.set_status (status_type::not_found);
240
+ }
225
241
}
226
242
else {
227
- bool is_exist = false ;
228
- std::function<void (coro_http_request & req,
229
- coro_http_response & resp)>
230
- handler;
231
- std::string method_str{parser_.method ()};
232
- std::string url_path = method_str;
233
- url_path.append (" " ).append (parser_.url ());
234
- std::tie (is_exist, handler, request_.params_ ) =
235
- router_.get_router_tree ()->get (url_path, method_str);
236
- if (is_exist) {
237
- if (handler) {
238
- (handler)(request_, response_);
243
+ std::function<async_simple::coro::Lazy<void >(
244
+ coro_http_request & req, coro_http_response & resp)>
245
+ coro_handler;
246
+
247
+ std::tie (is_coro_exist, coro_handler, request_.params_ ) =
248
+ router_.get_coro_router_tree ()->get_coro (url_path, method_str);
249
+
250
+ if (is_coro_exist) {
251
+ if (coro_handler) {
252
+ co_await coro_handler (request_, response_);
239
253
}
240
254
else {
241
255
response_.set_status (status_type::not_found);
242
256
}
243
257
}
244
258
else {
245
- bool is_coro_exist = false ;
246
- std::function<async_simple::coro::Lazy<void >(
247
- coro_http_request & req, coro_http_response & resp)>
248
- coro_handler;
249
-
250
- std::tie (is_coro_exist, coro_handler, request_.params_ ) =
251
- router_.get_coro_router_tree ()->get_coro (url_path,
252
- method_str);
253
-
254
- if (is_coro_exist) {
255
- if (coro_handler) {
256
- co_await coro_handler (request_, response_);
257
- }
258
- else {
259
- response_.set_status (status_type::not_found);
259
+ // coro regex router
260
+ auto coro_regex_handlers = router_.get_coro_regex_handlers ();
261
+ if (coro_regex_handlers.size () != 0 ) {
262
+ for (auto &pair : coro_regex_handlers) {
263
+ std::string coro_regex_key{key};
264
+
265
+ if (std::regex_match (coro_regex_key, request_.matches_ ,
266
+ std::get<0 >(pair))) {
267
+ auto coro_handler = std::get<1 >(pair);
268
+ if (coro_handler) {
269
+ co_await coro_handler (request_, response_);
270
+ is_matched_regex_router = true ;
271
+ }
272
+ }
260
273
}
261
274
}
262
- else {
263
- bool is_matched_regex_router = false ;
264
- // coro regex router
265
- auto coro_regex_handlers = router_.get_coro_regex_handlers ();
266
- if (coro_regex_handlers.size () != 0 ) {
267
- for (auto &pair : coro_regex_handlers) {
268
- std::string coro_regex_key{key};
269
-
270
- if (std::regex_match (coro_regex_key, request_.matches_ ,
275
+ // regex router
276
+ if (!is_matched_regex_router) {
277
+ auto regex_handlers = router_.get_regex_handlers ();
278
+ if (regex_handlers.size () != 0 ) {
279
+ for (auto &pair : regex_handlers) {
280
+ std::string regex_key{key};
281
+ if (std::regex_match (regex_key, request_.matches_ ,
271
282
std::get<0 >(pair))) {
272
- auto coro_handler = std::get<1 >(pair);
273
- if (coro_handler ) {
274
- co_await coro_handler (request_, response_);
283
+ auto handler = std::get<1 >(pair);
284
+ if (handler ) {
285
+ (handler) (request_, response_);
275
286
is_matched_regex_router = true ;
276
287
}
277
288
}
278
289
}
279
290
}
280
- // regex router
281
- if (!is_matched_regex_router) {
282
- auto regex_handlers = router_.get_regex_handlers ();
283
- if (regex_handlers.size () != 0 ) {
284
- for (auto &pair : regex_handlers) {
285
- std::string regex_key{key};
286
- if (std::regex_match (regex_key, request_.matches_ ,
287
- std::get<0 >(pair))) {
288
- auto handler = std::get<1 >(pair);
289
- if (handler) {
290
- (handler)(request_, response_);
291
- is_matched_regex_router = true ;
292
- }
293
- }
294
- }
295
- }
291
+ }
292
+ // radix route -> radix coro route -> regex coro -> regex ->
293
+ // default -> not found
294
+ if (!is_matched_regex_router) {
295
+ if (default_handler_) {
296
+ co_await default_handler_ (request_, response_);
296
297
}
297
- // not found
298
- if (!is_matched_regex_router) {
298
+ else {
299
+ // not found
299
300
response_.set_status (status_type::not_found);
300
301
}
301
302
}
@@ -389,6 +390,11 @@ class coro_http_connection
389
390
}
390
391
}
391
392
393
+ if (!keep_alive_) {
394
+ // now in io thread, so can close socket immediately.
395
+ close ();
396
+ }
397
+
392
398
response_.clear ();
393
399
request_.clear ();
394
400
buffers_.clear ();
@@ -423,11 +429,6 @@ class coro_http_connection
423
429
co_return false ;
424
430
}
425
431
426
- if (!keep_alive_) {
427
- // now in io thread, so can close socket immediately.
428
- close ();
429
- }
430
-
431
432
co_return true ;
432
433
}
433
434
@@ -478,11 +479,6 @@ class coro_http_connection
478
479
co_return false ;
479
480
}
480
481
481
- if (!keep_alive_) {
482
- // now in io thread, so can close socket immediately.
483
- close ();
484
- }
485
-
486
482
co_return true ;
487
483
}
488
484
@@ -719,15 +715,15 @@ class coro_http_connection
719
715
} break ;
720
716
case cinatra::ws_frame_type::WS_PING_FRAME: {
721
717
result.data = {payload.data (), payload.size ()};
722
- auto ec = co_await write_websocket (" pong " , opcode::pong);
718
+ auto ec = co_await write_websocket (result. data , opcode::pong);
723
719
if (ec) {
724
720
close ();
725
721
result.ec = ec;
726
722
}
727
723
} break ;
728
724
case cinatra::ws_frame_type::WS_PONG_FRAME: {
729
725
result.data = {payload.data (), payload.size ()};
730
- auto ec = co_await write_websocket (" ping " , opcode::ping);
726
+ auto ec = co_await write_websocket (result. data , opcode::ping);
731
727
result.ec = ec;
732
728
} break ;
733
729
default :
0 commit comments