mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-10-01 13:12:40 +07:00
Refactor SSLVerifierResponse to enum class and add get_param_values method to Request
This commit is contained in:
@@ -787,7 +787,7 @@ inline bool parse_url(const std::string &url, UrlComponents &uc) {
|
||||
|
||||
} // namespace detail
|
||||
|
||||
enum SSLVerifierResponse {
|
||||
enum class SSLVerifierResponse {
|
||||
// no decision has been made, use the built-in certificate verifier
|
||||
NoDecisionMade,
|
||||
// connection certificate is verified and accepted
|
||||
@@ -1293,6 +1293,7 @@ struct Request {
|
||||
|
||||
bool has_param(const std::string &key) const;
|
||||
std::string get_param_value(const std::string &key, size_t id = 0) const;
|
||||
std::vector<std::string> get_param_values(const std::string &key) const;
|
||||
size_t get_param_value_count(const std::string &key) const;
|
||||
|
||||
bool is_multipart_form_data() const;
|
||||
@@ -1696,6 +1697,9 @@ public:
|
||||
|
||||
Server &set_keep_alive_max_count(size_t count);
|
||||
Server &set_keep_alive_timeout(time_t sec);
|
||||
template <class Rep, class Period>
|
||||
Server &
|
||||
set_keep_alive_timeout(const std::chrono::duration<Rep, Period> &duration);
|
||||
|
||||
Server &set_read_timeout(time_t sec, time_t usec = 0);
|
||||
template <class Rep, class Period>
|
||||
@@ -9744,6 +9748,17 @@ inline std::string Request::get_param_value(const std::string &key,
|
||||
return std::string();
|
||||
}
|
||||
|
||||
inline std::vector<std::string>
|
||||
Request::get_param_values(const std::string &key) const {
|
||||
auto rng = params.equal_range(key);
|
||||
std::vector<std::string> values;
|
||||
values.reserve(static_cast<size_t>(std::distance(rng.first, rng.second)));
|
||||
for (auto it = rng.first; it != rng.second; ++it) {
|
||||
values.push_back(it->second);
|
||||
}
|
||||
return values;
|
||||
}
|
||||
|
||||
inline size_t Request::get_param_value_count(const std::string &key) const {
|
||||
auto r = params.equal_range(key);
|
||||
return static_cast<size_t>(std::distance(r.first, r.second));
|
||||
@@ -10921,6 +10936,15 @@ inline Server &Server::set_keep_alive_timeout(time_t sec) {
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <class Rep, class Period>
|
||||
inline Server &Server::set_keep_alive_timeout(
|
||||
const std::chrono::duration<Rep, Period> &duration) {
|
||||
detail::duration_to_sec_and_usec(duration, [&](time_t sec, time_t /*usec*/) {
|
||||
set_keep_alive_timeout(sec);
|
||||
});
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline Server &Server::set_read_timeout(time_t sec, time_t usec) {
|
||||
read_timeout_sec_ = sec;
|
||||
read_timeout_usec_ = usec;
|
||||
|
||||
@@ -3847,6 +3847,17 @@ protected:
|
||||
EXPECT_EQ("value2", req.get_param_value("array", 1));
|
||||
EXPECT_EQ("value3", req.get_param_value("array", 2));
|
||||
})
|
||||
.Get("/array-param-values",
|
||||
[&](const Request &req, Response & /*res*/) {
|
||||
auto values = req.get_param_values("array");
|
||||
EXPECT_EQ(3u, values.size());
|
||||
EXPECT_EQ("value1", values[0]);
|
||||
EXPECT_EQ("value2", values[1]);
|
||||
EXPECT_EQ("value3", values[2]);
|
||||
|
||||
auto empty = req.get_param_values("nonexistent");
|
||||
EXPECT_TRUE(empty.empty());
|
||||
})
|
||||
.Post("/validate-no-multiple-headers",
|
||||
[&](const Request &req, Response & /*res*/) {
|
||||
EXPECT_EQ(1u, req.get_header_value_count("Content-Length"));
|
||||
@@ -5972,6 +5983,13 @@ TEST_F(ServerTest, ArrayParam) {
|
||||
EXPECT_EQ(StatusCode::OK_200, res->status);
|
||||
}
|
||||
|
||||
TEST_F(ServerTest, ArrayParamValues) {
|
||||
auto res =
|
||||
cli_.Get("/array-param-values?array=value1&array=value2&array=value3");
|
||||
ASSERT_TRUE(res);
|
||||
EXPECT_EQ(StatusCode::OK_200, res->status);
|
||||
}
|
||||
|
||||
TEST_F(ServerTest, NoMultipleHeaders) {
|
||||
Headers headers = {{"Content-Length", "5"}};
|
||||
auto res = cli_.Post("/validate-no-multiple-headers", headers, "hello",
|
||||
|
||||
Reference in New Issue
Block a user