mirror of
https://github.com/yhirose/cpp-httplib
synced 2024-11-21 14:29:10 -07:00
Merge branch 'MannyClicks-patch-2'
This commit is contained in:
commit
77536acef7
2 changed files with 13 additions and 11 deletions
14
httplib.h
14
httplib.h
|
@ -781,11 +781,13 @@ inline const char *get_header_value(const Headers &headers, const char *key,
|
|||
return def;
|
||||
}
|
||||
|
||||
inline int get_header_value_int(const Headers &headers, const char *key,
|
||||
int def = 0) {
|
||||
auto it = headers.find(key);
|
||||
if (it != headers.end()) { return std::stoi(it->second); }
|
||||
return def;
|
||||
inline uint64_t get_header_value_uint64(const Headers &headers, const char *key,
|
||||
int def = 0) {
|
||||
auto it = headers.find(key);
|
||||
if (it != headers.end()) {
|
||||
return std::strtoull(it->second.data(), nullptr, 10);
|
||||
}
|
||||
return def;
|
||||
}
|
||||
|
||||
inline bool read_headers(Stream &strm, Headers &headers) {
|
||||
|
@ -881,7 +883,7 @@ inline bool read_content_chunked(Stream &strm, std::string &out) {
|
|||
template <typename T>
|
||||
bool read_content(Stream &strm, T &x, Progress progress = Progress()) {
|
||||
if (has_header(x.headers, "Content-Length")) {
|
||||
auto len = get_header_value_int(x.headers, "Content-Length", 0);
|
||||
auto len = get_header_value_uint64(x.headers, "Content-Length", 0);
|
||||
if (len == 0) {
|
||||
const auto &encoding =
|
||||
get_header_value(x.headers, "Transfer-Encoding", 0, "");
|
||||
|
|
10
test/test.cc
10
test/test.cc
|
@ -72,8 +72,8 @@ TEST(GetHeaderValueTest, DefaultValue) {
|
|||
|
||||
TEST(GetHeaderValueTest, DefaultValueInt) {
|
||||
Headers headers = {{"Dummy", "Dummy"}};
|
||||
auto val = detail::get_header_value_int(headers, "Content-Length", 100);
|
||||
EXPECT_EQ(100, val);
|
||||
auto val = detail::get_header_value_uint64(headers, "Content-Length", 100);
|
||||
EXPECT_EQ(100ull, val);
|
||||
}
|
||||
|
||||
TEST(GetHeaderValueTest, RegularValue) {
|
||||
|
@ -84,8 +84,8 @@ TEST(GetHeaderValueTest, RegularValue) {
|
|||
|
||||
TEST(GetHeaderValueTest, RegularValueInt) {
|
||||
Headers headers = {{"Content-Length", "100"}, {"Dummy", "Dummy"}};
|
||||
auto val = detail::get_header_value_int(headers, "Content-Length", 0);
|
||||
EXPECT_EQ(100, val);
|
||||
auto val = detail::get_header_value_uint64(headers, "Content-Length", 0);
|
||||
EXPECT_EQ(100ull, val);
|
||||
}
|
||||
|
||||
TEST(GetHeaderValueTest, Range) {
|
||||
|
@ -474,7 +474,7 @@ protected:
|
|||
EXPECT_EQ("value3", req.get_param_value("array", 2));
|
||||
})
|
||||
.Post("/validate-no-multiple-headers",
|
||||
[&](const Request &req, Response &res) {
|
||||
[&](const Request &req, Response & /*res*/) {
|
||||
EXPECT_EQ(1u, req.get_header_value_count("Content-Length"));
|
||||
EXPECT_EQ("5", req.get_header_value("Content-Length"));
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue