From da0c6579fa467a7f768e9bbce26289a742743186 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 31 Aug 2024 17:07:48 -0400 Subject: [PATCH] Breaking Change! get_header_ methods on Request and Response now take a default value. --- httplib.h | 48 ++++++++++++++++++++++++++++-------------------- test/test.cc | 8 ++++---- 2 files changed, 32 insertions(+), 24 deletions(-) diff --git a/httplib.h b/httplib.h index 7ef5826..c777991 100644 --- a/httplib.h +++ b/httplib.h @@ -565,8 +565,10 @@ struct Request { #endif bool has_header(const std::string &key) const; - std::string 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; + std::string get_header_value(const std::string &key, const char *def = "", + size_t id = 0) const; + uint64_t get_header_value_u64(const std::string &key, uint64_t def = 0, + 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); @@ -597,8 +599,10 @@ struct Response { std::string location; // Redirect location bool has_header(const std::string &key) const; - std::string 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; + std::string get_header_value(const std::string &key, const char *def = "", + size_t id = 0) const; + uint64_t get_header_value_u64(const std::string &key, uint64_t def = 0, + 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); @@ -1091,9 +1095,10 @@ public: // Request Headers bool has_request_header(const std::string &key) const; std::string get_request_header_value(const std::string &key, + const char *def = "", size_t id = 0) const; uint64_t get_request_header_value_u64(const std::string &key, - size_t id = 0) const; + uint64_t def = 0, size_t id = 0) const; size_t get_request_header_value_count(const std::string &key) const; private: @@ -1914,8 +1919,8 @@ inline void duration_to_sec_and_usec(const T &duration, U callback) { } inline uint64_t get_header_value_u64(const Headers &headers, - const std::string &key, size_t id, - uint64_t def) { + const std::string &key, uint64_t def, + size_t id) { auto rng = headers.equal_range(key); auto it = rng.first; std::advance(it, static_cast(id)); @@ -1928,13 +1933,13 @@ inline uint64_t get_header_value_u64(const Headers &headers, } // namespace detail 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); + uint64_t def, size_t id) const { + return detail::get_header_value_u64(headers, key, def, id); } 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); + uint64_t def, size_t id) const { + return detail::get_header_value_u64(headers, key, def, id); } template @@ -2119,8 +2124,9 @@ inline std::ostream &operator<<(std::ostream &os, const Error &obj) { } inline uint64_t Result::get_request_header_value_u64(const std::string &key, + uint64_t def, size_t id) const { - return detail::get_header_value_u64(request_headers_, key, id, 0); + return detail::get_header_value_u64(request_headers_, key, def, id); } template @@ -2220,7 +2226,7 @@ socket_t create_client_socket( time_t write_timeout_usec, const std::string &intf, Error &error); const char *get_header_value(const Headers &headers, const std::string &key, - size_t id = 0, const char *def = nullptr); + const char *def, size_t id); std::string params_to_query_str(const Params ¶ms); @@ -3948,8 +3954,8 @@ inline bool has_header(const Headers &headers, const std::string &key) { } inline const char *get_header_value(const Headers &headers, - const std::string &key, size_t id, - const char *def) { + const std::string &key, const char *def, + size_t id) { auto rng = headers.equal_range(key); auto it = rng.first; std::advance(it, static_cast(id)); @@ -4146,7 +4152,7 @@ inline bool read_content_chunked(Stream &strm, T &x, inline bool is_chunked_transfer_encoding(const Headers &headers) { return compare_case_ignore( - get_header_value(headers, "Transfer-Encoding", 0, ""), "chunked"); + get_header_value(headers, "Transfer-Encoding", "", 0), "chunked"); } template @@ -5489,8 +5495,8 @@ inline bool Request::has_header(const std::string &key) const { } inline std::string Request::get_header_value(const std::string &key, - size_t id) const { - return detail::get_header_value(headers, key, id, ""); + const char *def, size_t id) const { + return detail::get_header_value(headers, key, def, id); } inline size_t Request::get_header_value_count(const std::string &key) const { @@ -5554,8 +5560,9 @@ inline bool Response::has_header(const std::string &key) const { } inline std::string Response::get_header_value(const std::string &key, + const char *def, size_t id) const { - return detail::get_header_value(headers, key, id, ""); + return detail::get_header_value(headers, key, def, id); } inline size_t Response::get_header_value_count(const std::string &key) const { @@ -5640,8 +5647,9 @@ inline bool Result::has_request_header(const std::string &key) const { } inline std::string Result::get_request_header_value(const std::string &key, + const char *def, size_t id) const { - return detail::get_header_value(request_headers_, key, id, ""); + return detail::get_header_value(request_headers_, key, def, id); } inline size_t diff --git a/test/test.cc b/test/test.cc index 33ef30b..e13d3b9 100644 --- a/test/test.cc +++ b/test/test.cc @@ -467,25 +467,25 @@ TEST(ParseMultipartBoundaryTest, ValueWithQuotesAndCharset) { TEST(GetHeaderValueTest, DefaultValue) { Headers headers = {{"Dummy", "Dummy"}}; - auto val = detail::get_header_value(headers, "Content-Type", 0, "text/plain"); + auto val = detail::get_header_value(headers, "Content-Type", "text/plain", 0); EXPECT_STREQ("text/plain", val); } TEST(GetHeaderValueTest, DefaultValueInt) { Headers headers = {{"Dummy", "Dummy"}}; - auto val = detail::get_header_value_u64(headers, "Content-Length", 0, 100); + auto val = detail::get_header_value_u64(headers, "Content-Length", 100, 0); EXPECT_EQ(100ull, val); } TEST(GetHeaderValueTest, RegularValue) { Headers headers = {{"Content-Type", "text/html"}, {"Dummy", "Dummy"}}; - auto val = detail::get_header_value(headers, "Content-Type", 0, "text/plain"); + auto val = detail::get_header_value(headers, "Content-Type", "text/plain", 0); EXPECT_STREQ("text/html", val); } TEST(GetHeaderValueTest, RegularValueWithDifferentCase) { Headers headers = {{"Content-Type", "text/html"}, {"Dummy", "Dummy"}}; - auto val = detail::get_header_value(headers, "content-type", 0, "text/plain"); + auto val = detail::get_header_value(headers, "content-type", "text/plain", 0); EXPECT_STREQ("text/html", val); }