From 6650632e7fd7ccffe2a6ac263445bf3ea0393cb3 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 22 Aug 2023 19:36:10 -0400 Subject: [PATCH] Fix #1638 --- httplib.h | 43 +++++++++++++++++-------------------------- test/test.cc | 10 ++++------ 2 files changed, 21 insertions(+), 32 deletions(-) diff --git a/httplib.h b/httplib.h index fd7f7b7..71b4c12 100644 --- a/httplib.h +++ b/httplib.h @@ -487,8 +487,7 @@ struct Request { bool has_header(const std::string &key) const; std::string get_header_value(const std::string &key, size_t id = 0) const; - template - T get_header_value(const std::string &key, size_t id = 0) const; + uint64_t get_header_value_u64(const std::string &key, size_t id = 0) const; size_t get_header_value_count(const std::string &key) const; 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; std::string get_header_value(const std::string &key, size_t id = 0) const; - template - T get_header_value(const std::string &key, size_t id = 0) const; + uint64_t get_header_value_u64(const std::string &key, size_t id = 0) const; size_t get_header_value_count(const std::string &key) const; 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; std::string get_request_header_value(const std::string &key, size_t id = 0) const; - template - T get_request_header_value(const std::string &key, size_t id = 0) const; + uint64_t get_request_header_value_u64(const std::string &key, + size_t id = 0) const; size_t get_request_header_value_count(const std::string &key) const; private: @@ -1710,15 +1708,9 @@ inline void duration_to_sec_and_usec(const T &duration, U callback) { callback(static_cast(sec), static_cast(usec)); } -template -inline T get_header_value(const Headers & /*headers*/, - const std::string & /*key*/, size_t /*id*/ = 0, - uint64_t /*def*/ = 0) {} - -template <> -inline uint64_t get_header_value(const Headers &headers, - const std::string &key, size_t id, - uint64_t def) { +inline uint64_t get_header_value_u64(const Headers &headers, + const std::string &key, size_t id, + uint64_t def) { auto rng = headers.equal_range(key); auto it = rng.first; std::advance(it, static_cast(id)); @@ -1730,14 +1722,14 @@ inline uint64_t get_header_value(const Headers &headers, } // namespace detail -template -inline T Request::get_header_value(const std::string &key, size_t id) const { - return detail::get_header_value(headers, key, id, 0); +inline uint64_t Request::get_header_value_u64(const std::string &key, + size_t id) const { + return detail::get_header_value_u64(headers, key, id, 0); } -template -inline T Response::get_header_value(const std::string &key, size_t id) const { - return detail::get_header_value(headers, key, id, 0); +inline uint64_t Response::get_header_value_u64(const std::string &key, + size_t id) const { + return detail::get_header_value_u64(headers, key, id, 0); } template @@ -1906,10 +1898,9 @@ inline std::ostream &operator<<(std::ostream &os, const Error &obj) { return os; } -template -inline T Result::get_request_header_value(const std::string &key, - size_t id) const { - return detail::get_header_value(request_headers_, key, id, 0); +inline uint64_t Result::get_request_header_value_u64(const std::string &key, + size_t id) const { + return detail::get_header_value_u64(request_headers_, key, id, 0); } template @@ -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")) { ret = read_content_without_length(strm, out); } else { - auto len = get_header_value(x.headers, "Content-Length"); + auto len = get_header_value_u64(x.headers, "Content-Length", 0, 0); if (len > payload_max_length) { exceed_payload_max_length = true; skip_content_with_length(strm, len); diff --git a/test/test.cc b/test/test.cc index 217991b..e702e36 100644 --- a/test/test.cc +++ b/test/test.cc @@ -211,8 +211,7 @@ TEST(GetHeaderValueTest, DefaultValue) { TEST(GetHeaderValueTest, DefaultValueInt) { Headers headers = {{"Dummy", "Dummy"}}; - auto val = - detail::get_header_value(headers, "Content-Length", 0, 100); + auto val = detail::get_header_value_u64(headers, "Content-Length", 0, 100); EXPECT_EQ(100ull, val); } @@ -241,8 +240,7 @@ TEST(GetHeaderValueTest, SetContent) { TEST(GetHeaderValueTest, RegularValueInt) { Headers headers = {{"Content-Length", "100"}, {"Dummy", "Dummy"}}; - auto val = - detail::get_header_value(headers, "Content-Length", 0, 0); + auto val = detail::get_header_value_u64(headers, "Content-Length", 0, 0); EXPECT_EQ(100ull, val); } @@ -1016,7 +1014,7 @@ TEST(UrlWithSpace, Redirect_Online) { auto res = cli.Get("/files/2595/310/Neat 1.4-17.jar"); ASSERT_TRUE(res); EXPECT_EQ(200, res->status); - EXPECT_EQ(18527U, res->get_header_value("Content-Length")); + EXPECT_EQ(18527U, res->get_header_value_u64("Content-Length")); } #endif @@ -3296,7 +3294,7 @@ TEST_F(ServerTest, PutLargeFileWithGzip2) { ASSERT_TRUE(res); EXPECT_EQ(200, res->status); EXPECT_EQ(LARGE_DATA, res->body); - EXPECT_EQ(101942u, res.get_request_header_value("Content-Length")); + EXPECT_EQ(101942u, res.get_request_header_value_u64("Content-Length")); EXPECT_EQ("gzip", res.get_request_header_value("Content-Encoding")); }