Skip to content

Commit c9857f0

Browse files
committed
fix some
1 parent 6667c56 commit c9857f0

File tree

3 files changed

+102
-75
lines changed

3 files changed

+102
-75
lines changed

include/ylt/standalone/cinatra/coro_http_connection.hpp

Lines changed: 67 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -220,82 +220,83 @@ class coro_http_connection
220220
co_await router_.route_coro(coro_handler, request_, response_, key);
221221
}
222222
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+
}
225241
}
226242
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_);
239253
}
240254
else {
241255
response_.set_status(status_type::not_found);
242256
}
243257
}
244258
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+
}
260273
}
261274
}
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_,
271282
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_);
275286
is_matched_regex_router = true;
276287
}
277288
}
278289
}
279290
}
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_);
296297
}
297-
// not found
298-
if (!is_matched_regex_router) {
298+
else {
299+
// not found
299300
response_.set_status(status_type::not_found);
300301
}
301302
}
@@ -389,6 +390,11 @@ class coro_http_connection
389390
}
390391
}
391392

393+
if (!keep_alive_) {
394+
// now in io thread, so can close socket immediately.
395+
close();
396+
}
397+
392398
response_.clear();
393399
request_.clear();
394400
buffers_.clear();
@@ -423,11 +429,6 @@ class coro_http_connection
423429
co_return false;
424430
}
425431

426-
if (!keep_alive_) {
427-
// now in io thread, so can close socket immediately.
428-
close();
429-
}
430-
431432
co_return true;
432433
}
433434

@@ -478,11 +479,6 @@ class coro_http_connection
478479
co_return false;
479480
}
480481

481-
if (!keep_alive_) {
482-
// now in io thread, so can close socket immediately.
483-
close();
484-
}
485-
486482
co_return true;
487483
}
488484

@@ -719,15 +715,15 @@ class coro_http_connection
719715
} break;
720716
case cinatra::ws_frame_type::WS_PING_FRAME: {
721717
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);
723719
if (ec) {
724720
close();
725721
result.ec = ec;
726722
}
727723
} break;
728724
case cinatra::ws_frame_type::WS_PONG_FRAME: {
729725
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);
731727
result.ec = ec;
732728
} break;
733729
default:

src/coro_http/tests/test_cinatra.cpp

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,34 @@ TEST_CASE("test invalid http body size") {
406406
}
407407
}
408408

409+
TEST_CASE("test default handler, loweset priority") {
410+
coro_http_server server(2, 8443);
411+
server.set_http_handler<HEAD, POST, GET>(
412+
"/check/:id", [](request &req, response &resp) {
413+
resp.set_status_and_content(status_type::ok, "check ok");
414+
});
415+
server.set_http_handler<HEAD, POST, GET>(
416+
"/test", [](request &req, response &resp) {
417+
resp.set_status_and_content(status_type::ok, "test ok");
418+
});
419+
server.set_default_handler(
420+
[](request &req, response &resp) -> async_simple::coro::Lazy<void> {
421+
resp.set_status_and_content(status_type::ok, "default");
422+
co_return;
423+
});
424+
server.async_start();
425+
426+
coro_http_client client{};
427+
auto result = client.get("http://127.0.0.1:8443/check/12");
428+
CHECK(result.resp_body == "check ok");
429+
430+
result = client.get("http://127.0.0.1:8443/test");
431+
CHECK(result.resp_body == "test ok");
432+
433+
result = client.get("http://127.0.0.1:8443/dummy");
434+
CHECK(result.resp_body == "default");
435+
}
436+
409437
bool create_file(std::string_view filename, size_t file_size = 1024) {
410438
std::ofstream out(filename.data(), std::ios::binary);
411439
if (!out.is_open()) {
@@ -983,7 +1011,7 @@ TEST_CASE("test out buffer and async upload ") {
9831011

9841012
auto ss = std::make_shared<std::stringstream>();
9851013
*ss << "hello world";
986-
1014+
client.add_header("Connection", "close");
9871015
if (flag == upload_type::send_file) {
9881016
result = co_await client.async_upload("http://127.0.0.1:9000/more"sv,
9891017
http_method::POST, ss);
@@ -1408,7 +1436,7 @@ TEST_CASE("test out io_contex server") {
14081436

14091437
TEST_CASE("test coro_http_client async_http_connect") {
14101438
coro_http_client client{};
1411-
cinatra::coro_http_client::config conf{.req_timeout_duration = 60s};
1439+
cinatra::coro_http_client::config conf{.req_timeout_duration = 3s};
14121440
client.init_config(conf);
14131441
auto r = async_simple::coro::syncAwait(
14141442
client.async_http_connect("http://www.baidu.com"));

src/coro_http/tests/test_coro_http_server.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -990,10 +990,13 @@ TEST_CASE("test websocket") {
990990
CHECK(data.resp_body == "test_ws");
991991
co_await client.write_websocket("PING", opcode::ping);
992992
data = co_await client.read_websocket();
993-
CHECK(data.resp_body == "pong");
993+
CHECK(data.resp_body == "PING");
994+
co_await client.write_websocket("", opcode::ping);
995+
data = co_await client.read_websocket();
996+
CHECK(data.resp_body == "");
994997
co_await client.write_websocket("PONG", opcode::pong);
995998
data = co_await client.read_websocket();
996-
CHECK(data.resp_body == "ping");
999+
CHECK(data.resp_body == "PONG");
9971000
co_await client.write_websocket_close("normal close");
9981001
data = co_await client.read_websocket();
9991002
CHECK(data.resp_body == "normal close");

0 commit comments

Comments
 (0)