This commit is contained in:
yhirose 2023-08-22 19:36:10 -04:00
parent afe627e7af
commit 6650632e7f
2 changed files with 21 additions and 32 deletions

View file

@ -487,8 +487,7 @@ struct Request {
bool has_header(const std::string &key) const; bool has_header(const std::string &key) const;
std::string get_header_value(const std::string &key, size_t id = 0) const; std::string get_header_value(const std::string &key, size_t id = 0) const;
template <typename T> uint64_t get_header_value_u64(const std::string &key, size_t id = 0) const;
T get_header_value(const std::string &key, size_t id = 0) const;
size_t get_header_value_count(const std::string &key) const; size_t get_header_value_count(const std::string &key) const;
void set_header(const std::string &key, const std::string &val); void set_header(const std::string &key, const std::string &val);
@ -520,8 +519,7 @@ struct Response {
bool has_header(const std::string &key) const; bool has_header(const std::string &key) const;
std::string get_header_value(const std::string &key, size_t id = 0) const; std::string get_header_value(const std::string &key, size_t id = 0) const;
template <typename T> uint64_t get_header_value_u64(const std::string &key, size_t id = 0) const;
T get_header_value(const std::string &key, size_t id = 0) const;
size_t get_header_value_count(const std::string &key) const; size_t get_header_value_count(const std::string &key) const;
void set_header(const std::string &key, const std::string &val); void set_header(const std::string &key, const std::string &val);
@ -988,8 +986,8 @@ public:
bool has_request_header(const std::string &key) const; bool has_request_header(const std::string &key) const;
std::string get_request_header_value(const std::string &key, std::string get_request_header_value(const std::string &key,
size_t id = 0) const; size_t id = 0) const;
template <typename T> uint64_t get_request_header_value_u64(const std::string &key,
T get_request_header_value(const std::string &key, size_t id = 0) const; size_t id = 0) const;
size_t get_request_header_value_count(const std::string &key) const; size_t get_request_header_value_count(const std::string &key) const;
private: private:
@ -1710,15 +1708,9 @@ inline void duration_to_sec_and_usec(const T &duration, U callback) {
callback(static_cast<time_t>(sec), static_cast<time_t>(usec)); callback(static_cast<time_t>(sec), static_cast<time_t>(usec));
} }
template <typename T> inline uint64_t get_header_value_u64(const Headers &headers,
inline T get_header_value(const Headers & /*headers*/, const std::string &key, size_t id,
const std::string & /*key*/, size_t /*id*/ = 0, uint64_t def) {
uint64_t /*def*/ = 0) {}
template <>
inline uint64_t get_header_value<uint64_t>(const Headers &headers,
const std::string &key, size_t id,
uint64_t def) {
auto rng = headers.equal_range(key); auto rng = headers.equal_range(key);
auto it = rng.first; auto it = rng.first;
std::advance(it, static_cast<ssize_t>(id)); std::advance(it, static_cast<ssize_t>(id));
@ -1730,14 +1722,14 @@ inline uint64_t get_header_value<uint64_t>(const Headers &headers,
} // namespace detail } // namespace detail
template <typename T> inline uint64_t Request::get_header_value_u64(const std::string &key,
inline T Request::get_header_value(const std::string &key, size_t id) const { size_t id) const {
return detail::get_header_value<T>(headers, key, id, 0); return detail::get_header_value_u64(headers, key, id, 0);
} }
template <typename T> inline uint64_t Response::get_header_value_u64(const std::string &key,
inline T Response::get_header_value(const std::string &key, size_t id) const { size_t id) const {
return detail::get_header_value<T>(headers, key, id, 0); return detail::get_header_value_u64(headers, key, id, 0);
} }
template <typename... Args> template <typename... Args>
@ -1906,10 +1898,9 @@ inline std::ostream &operator<<(std::ostream &os, const Error &obj) {
return os; return os;
} }
template <typename T> inline uint64_t Result::get_request_header_value_u64(const std::string &key,
inline T Result::get_request_header_value(const std::string &key, size_t id) const {
size_t id) const { return detail::get_header_value_u64(request_headers_, key, id, 0);
return detail::get_header_value<T>(request_headers_, key, id, 0);
} }
template <class Rep, class Period> template <class Rep, class Period>
@ -3894,7 +3885,7 @@ bool read_content(Stream &strm, T &x, size_t payload_max_length, int &status,
} else if (!has_header(x.headers, "Content-Length")) { } else if (!has_header(x.headers, "Content-Length")) {
ret = read_content_without_length(strm, out); ret = read_content_without_length(strm, out);
} else { } else {
auto len = get_header_value<uint64_t>(x.headers, "Content-Length"); auto len = get_header_value_u64(x.headers, "Content-Length", 0, 0);
if (len > payload_max_length) { if (len > payload_max_length) {
exceed_payload_max_length = true; exceed_payload_max_length = true;
skip_content_with_length(strm, len); skip_content_with_length(strm, len);

View file

@ -211,8 +211,7 @@ TEST(GetHeaderValueTest, DefaultValue) {
TEST(GetHeaderValueTest, DefaultValueInt) { TEST(GetHeaderValueTest, DefaultValueInt) {
Headers headers = {{"Dummy", "Dummy"}}; Headers headers = {{"Dummy", "Dummy"}};
auto val = auto val = detail::get_header_value_u64(headers, "Content-Length", 0, 100);
detail::get_header_value<uint64_t>(headers, "Content-Length", 0, 100);
EXPECT_EQ(100ull, val); EXPECT_EQ(100ull, val);
} }
@ -241,8 +240,7 @@ TEST(GetHeaderValueTest, SetContent) {
TEST(GetHeaderValueTest, RegularValueInt) { TEST(GetHeaderValueTest, RegularValueInt) {
Headers headers = {{"Content-Length", "100"}, {"Dummy", "Dummy"}}; Headers headers = {{"Content-Length", "100"}, {"Dummy", "Dummy"}};
auto val = auto val = detail::get_header_value_u64(headers, "Content-Length", 0, 0);
detail::get_header_value<uint64_t>(headers, "Content-Length", 0, 0);
EXPECT_EQ(100ull, val); EXPECT_EQ(100ull, val);
} }
@ -1016,7 +1014,7 @@ TEST(UrlWithSpace, Redirect_Online) {
auto res = cli.Get("/files/2595/310/Neat 1.4-17.jar"); auto res = cli.Get("/files/2595/310/Neat 1.4-17.jar");
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ(200, res->status); EXPECT_EQ(200, res->status);
EXPECT_EQ(18527U, res->get_header_value<uint64_t>("Content-Length")); EXPECT_EQ(18527U, res->get_header_value_u64("Content-Length"));
} }
#endif #endif
@ -3296,7 +3294,7 @@ TEST_F(ServerTest, PutLargeFileWithGzip2) {
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ(200, res->status); EXPECT_EQ(200, res->status);
EXPECT_EQ(LARGE_DATA, res->body); EXPECT_EQ(LARGE_DATA, res->body);
EXPECT_EQ(101942u, res.get_request_header_value<uint64_t>("Content-Length")); EXPECT_EQ(101942u, res.get_request_header_value_u64("Content-Length"));
EXPECT_EQ("gzip", res.get_request_header_value("Content-Encoding")); EXPECT_EQ("gzip", res.get_request_header_value("Content-Encoding"));
} }