Skip to content

Commit 17a2139

Browse files
authored
[coro_http_client][feat]add user data (#871)
1 parent d027389 commit 17a2139

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

include/ylt/standalone/cinatra/coro_http_request.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,10 @@ class coro_http_request {
248248
(aspect_data_.push_back(std::move(args)), ...);
249249
}
250250

251+
void set_user_data(std::any data) { user_data_ = std::move(data); }
252+
253+
std::any get_user_data() { return user_data_; }
254+
251255
std::vector<std::string> &get_aspect_data() { return aspect_data_; }
252256

253257
std::unordered_map<std::string_view, std::string_view> get_cookies(
@@ -288,6 +292,9 @@ class coro_http_request {
288292
if (!aspect_data_.empty()) {
289293
aspect_data_.clear();
290294
}
295+
if (user_data_.has_value()) {
296+
user_data_.reset();
297+
}
291298
}
292299

293300
std::unordered_map<std::string, std::string> params_;
@@ -300,5 +307,6 @@ class coro_http_request {
300307
bool is_websocket_ = false;
301308
std::vector<std::string> aspect_data_;
302309
std::string cached_session_id_;
310+
std::any user_data_;
303311
};
304312
} // namespace cinatra

src/coro_http/tests/test_cinatra.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,13 +538,22 @@ TEST_CASE("test request https without init_ssl") {
538538
struct add_data {
539539
bool before(coro_http_request &req, coro_http_response &res) {
540540
req.set_aspect_data("hello world");
541+
auto val = std::make_shared<int>(42);
542+
req.set_user_data(val);
541543
return true;
542544
}
543545
};
544546

545547
struct add_more_data {
546548
bool before(coro_http_request &req, coro_http_response &res) {
547549
req.set_aspect_data(std::vector<std::string>{"test", "aspect"});
550+
auto user_data = req.get_user_data();
551+
CHECK(user_data.has_value());
552+
auto val = std::any_cast<std::shared_ptr<int>>(user_data);
553+
CHECK(*val == 42);
554+
auto data = req.get_user_data();
555+
val = std::any_cast<std::shared_ptr<int>>(data);
556+
*val = 43;
548557
return true;
549558
}
550559
};
@@ -561,6 +570,8 @@ struct auth_t {
561570

562571
struct dely_t {
563572
bool before(coro_http_request &req, coro_http_response &res) {
573+
auto user_data = req.get_user_data();
574+
CHECK(!user_data.has_value());
564575
res.set_status_and_content(status_type::unauthorized, "unauthorized");
565576
return false;
566577
}
@@ -594,9 +605,13 @@ TEST_CASE("test aspect") {
594605
CHECK(val[0] == "test");
595606
CHECK(val[1] == "aspect");
596607
CHECK(!req.is_upgrade());
608+
auto user_data = req.get_user_data();
609+
CHECK(user_data.has_value());
610+
auto val1 = std::any_cast<std::shared_ptr<int>>(user_data);
611+
CHECK(*val1 == 43);
597612
resp.set_status_and_content(status_type::ok, "ok");
598613
},
599-
add_more_data{});
614+
add_data{}, add_more_data{});
600615
server.set_http_handler<GET>(
601616
"/auth",
602617
[](coro_http_request &req, coro_http_response &resp) {

src/metric/tests/test_metric.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1964,9 +1964,12 @@ TEST_CASE("test metric manager clean expired label") {
19641964
CHECK(c->label_value_count() == 2);
19651965
CHECK(summary->label_value_count() == 1);
19661966
CHECK(h->label_value_count() == 1);
1967-
std::this_thread::sleep_for(std::chrono::seconds(2));
1967+
std::this_thread::sleep_for(std::chrono::seconds(3));
19681968
c->inc({"/index"});
19691969
size_t count = c->label_value_count();
1970+
if (count != 1) {
1971+
std::this_thread::sleep_for(std::chrono::seconds(2));
1972+
}
19701973
CHECK(count == 1);
19711974
auto ct1 = summary->label_value_count();
19721975
CHECK(ct1 == 0);

0 commit comments

Comments
 (0)