Skip to content

Commit 75b2c25

Browse files
authored
[coro_http_server]fix some (#917)
1 parent 6667c56 commit 75b2c25

File tree

5 files changed

+178
-147
lines changed

5 files changed

+178
-147
lines changed

include/ylt/reflection/member_count.hpp

Lines changed: 72 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ constexpr bool optional = !expected<T> && optional_impl<T>::value;
7474

7575
namespace internal {
7676
#if __cpp_concepts >= 201907L
77-
template <typename Type>
78-
concept tuple_size = requires(Type tuple) {
79-
std::tuple_size<remove_cvref_t<Type>>::value;
80-
};
77+
template <typename Type>
78+
concept tuple_size = requires(Type tuple) {
79+
std::tuple_size<remove_cvref_t<Type>>::value;
80+
};
8181
#else
8282
template <typename T, typename = void>
8383
struct tuple_size_impl : std::false_type {};
@@ -91,79 +91,79 @@ template <typename T>
9191
constexpr bool tuple_size = tuple_size_impl<T>::value;
9292
#endif
9393

94-
template <typename Type>
95-
constexpr inline bool is_compatible_v = false;
96-
97-
template <typename Type, uint64_t version>
98-
constexpr inline bool is_compatible_v<struct_pack::compatible<Type, version>> =
99-
true;
100-
101-
struct UniversalVectorType {
102-
template <typename T>
103-
operator std::vector<T>();
104-
};
94+
template <typename Type>
95+
constexpr inline bool is_compatible_v = false;
10596

106-
struct UniversalType {
107-
template <typename T>
108-
operator T();
109-
};
97+
template <typename Type, uint64_t version>
98+
constexpr inline bool
99+
is_compatible_v<struct_pack::compatible<Type, version>> = true;
110100

111-
struct UniversalIntegralType {
112-
template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
113-
operator T();
114-
};
101+
struct UniversalVectorType {
102+
template <typename T>
103+
operator std::vector<T>();
104+
};
115105

116-
struct UniversalNullptrType {
117-
operator std::nullptr_t();
118-
};
106+
struct UniversalType {
107+
template <typename T>
108+
operator T();
109+
};
119110

120-
struct UniversalOptionalType {
121-
template <typename U, typename = std::enable_if_t<optional<U>>>
122-
operator U();
123-
};
111+
struct UniversalIntegralType {
112+
template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
113+
operator T();
114+
};
124115

125-
struct UniversalCompatibleType {
126-
template <typename U, typename = std::enable_if_t<is_compatible_v<U>>>
127-
operator U();
128-
};
116+
struct UniversalNullptrType {
117+
operator std::nullptr_t();
118+
};
129119

130-
template <typename T, typename construct_param_t, typename = void,
131-
typename... Args>
132-
struct is_constructable_impl : std::false_type {};
133-
template <typename T, typename construct_param_t, typename... Args>
134-
struct is_constructable_impl<
135-
T, construct_param_t,
136-
std::void_t<decltype(T{{Args{}}..., {construct_param_t{}}})>, Args...>
137-
: std::true_type {};
120+
struct UniversalOptionalType {
121+
template <typename U, typename = std::enable_if_t<optional<U>>>
122+
operator U();
123+
};
138124

139-
template <typename T, typename construct_param_t, typename... Args>
140-
constexpr bool is_constructable =
141-
is_constructable_impl<T, construct_param_t, void, Args...>::value;
125+
struct UniversalCompatibleType {
126+
template <typename U, typename = std::enable_if_t<is_compatible_v<U>>>
127+
operator U();
128+
};
142129

143-
template <typename T, typename... Args>
144-
inline constexpr std::size_t members_count_impl() {
145-
if constexpr (is_constructable<T, UniversalVectorType, Args...>) {
146-
return members_count_impl<T, Args..., UniversalVectorType>();
147-
}
148-
else if constexpr (is_constructable<T, UniversalType, Args...>) {
149-
return members_count_impl<T, Args..., UniversalType>();
150-
}
151-
else if constexpr (is_constructable<T, UniversalOptionalType, Args...>) {
152-
return members_count_impl<T, Args..., UniversalOptionalType>();
153-
}
154-
else if constexpr (is_constructable<T, UniversalIntegralType, Args...>) {
155-
return members_count_impl<T, Args..., UniversalIntegralType>();
156-
}
157-
else if constexpr (is_constructable<T, UniversalNullptrType, Args...>) {
158-
return members_count_impl<T, Args..., UniversalNullptrType>();
130+
template <typename T, typename construct_param_t, typename = void,
131+
typename... Args>
132+
struct is_constructable_impl : std::false_type {};
133+
template <typename T, typename construct_param_t, typename... Args>
134+
struct is_constructable_impl<
135+
T, construct_param_t,
136+
std::void_t<decltype(T{{Args{}}..., {construct_param_t{}}})>, Args...>
137+
: std::true_type {};
138+
139+
template <typename T, typename construct_param_t, typename... Args>
140+
constexpr bool is_constructable =
141+
is_constructable_impl<T, construct_param_t, void, Args...>::value;
142+
143+
template <typename T, typename... Args>
144+
inline constexpr std::size_t members_count_impl() {
145+
if constexpr (is_constructable<T, UniversalVectorType, Args...>) {
146+
return members_count_impl<T, Args..., UniversalVectorType>();
147+
}
148+
else if constexpr (is_constructable<T, UniversalType, Args...>) {
149+
return members_count_impl<T, Args..., UniversalType>();
150+
}
151+
else if constexpr (is_constructable<T, UniversalOptionalType, Args...>) {
152+
return members_count_impl<T, Args..., UniversalOptionalType>();
153+
}
154+
else if constexpr (is_constructable<T, UniversalIntegralType, Args...>) {
155+
return members_count_impl<T, Args..., UniversalIntegralType>();
156+
}
157+
else if constexpr (is_constructable<T, UniversalNullptrType, Args...>) {
158+
return members_count_impl<T, Args..., UniversalNullptrType>();
159+
}
160+
else if constexpr (is_constructable<T, UniversalCompatibleType, Args...>) {
161+
return members_count_impl<T, Args..., UniversalCompatibleType>();
162+
}
163+
else {
164+
return sizeof...(Args);
165+
}
159166
}
160-
else if constexpr (is_constructable<T, UniversalCompatibleType, Args...>) {
161-
return members_count_impl<T, Args..., UniversalCompatibleType>();
162-
}
163-
else {
164-
return sizeof...(Args);
165-
}
166-
}
167167
} // namespace internal
168168

169169
template <typename T>
@@ -178,9 +178,12 @@ inline constexpr std::size_t members_count() {
178178
else if constexpr (internal::tuple_size<type>) {
179179
return std::tuple_size<type>::value;
180180
}
181-
else {
181+
else if constexpr (std::is_aggregate_v<type>) {
182182
return internal::members_count_impl<type>();
183183
}
184+
else {
185+
static_assert(!sizeof(T), "not supported type!");
186+
}
184187
}
185188

186189
template <typename T>

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:

0 commit comments

Comments
 (0)