Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add user data #681

Merged
merged 1 commit into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions include/cinatra/coro_http_request.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,10 @@ class coro_http_request {
(aspect_data_.push_back(std::move(args)), ...);
}

void set_user_data(std::any data) { user_data_ = std::move(data); }

std::any get_user_data() { return user_data_; }

std::vector<std::string> &get_aspect_data() { return aspect_data_; }

std::unordered_map<std::string_view, std::string_view> get_cookies(
Expand Down Expand Up @@ -288,6 +292,9 @@ class coro_http_request {
if (!aspect_data_.empty()) {
aspect_data_.clear();
}
if (user_data_.has_value()) {
user_data_.reset();
}
}

std::unordered_map<std::string, std::string> params_;
Expand All @@ -300,5 +307,6 @@ class coro_http_request {
bool is_websocket_ = false;
std::vector<std::string> aspect_data_;
std::string cached_session_id_;
std::any user_data_;
};
} // namespace cinatra
17 changes: 16 additions & 1 deletion tests/test_cinatra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -547,13 +547,22 @@ TEST_CASE("test request https without init_ssl") {
struct add_data {
bool before(coro_http_request &req, coro_http_response &res) {
req.set_aspect_data("hello world");
auto val = std::make_shared<int>(42);
req.set_user_data(val);
return true;
}
};

struct add_more_data {
bool before(coro_http_request &req, coro_http_response &res) {
req.set_aspect_data(std::vector<std::string>{"test", "aspect"});
auto user_data = req.get_user_data();
CHECK(user_data.has_value());
auto val = std::any_cast<std::shared_ptr<int>>(user_data);
CHECK(*val == 42);
auto data = req.get_user_data();
val = std::any_cast<std::shared_ptr<int>>(data);
*val = 43;
return true;
}
};
Expand All @@ -570,6 +579,8 @@ struct auth_t {

struct dely_t {
bool before(coro_http_request &req, coro_http_response &res) {
auto user_data = req.get_user_data();
CHECK(!user_data.has_value());
res.set_status_and_content(status_type::unauthorized, "unauthorized");
return false;
}
Expand Down Expand Up @@ -603,9 +614,13 @@ TEST_CASE("test aspect") {
CHECK(val[0] == "test");
CHECK(val[1] == "aspect");
CHECK(!req.is_upgrade());
auto user_data = req.get_user_data();
CHECK(user_data.has_value());
auto val1 = std::any_cast<std::shared_ptr<int>>(user_data);
CHECK(*val1 == 43);
resp.set_status_and_content(status_type::ok, "ok");
},
add_more_data{});
add_data{}, add_more_data{});
server.set_http_handler<GET>(
"/auth",
[](coro_http_request &req, coro_http_response &resp) {
Expand Down
Loading