Code cleanup
This commit is contained in:
parent
978a4f6345
commit
9720ef8c34
52
httplib.h
52
httplib.h
@ -321,6 +321,8 @@ make_unique(std::size_t n) {
|
|||||||
return std::unique_ptr<T>(new RT[n]);
|
return std::unique_ptr<T>(new RT[n]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace case_ignore {
|
||||||
|
|
||||||
inline unsigned char to_lower(int c) {
|
inline unsigned char to_lower(int c) {
|
||||||
const static unsigned char table[256] = {
|
const static unsigned char table[256] = {
|
||||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
|
||||||
@ -345,28 +347,36 @@ inline unsigned char to_lower(int c) {
|
|||||||
return table[(unsigned char)(char)c];
|
return table[(unsigned char)(char)c];
|
||||||
}
|
}
|
||||||
|
|
||||||
struct case_ignore_equal {
|
inline bool equal(const std::string &a, const std::string &b) {
|
||||||
bool operator()(const std::string &s1, const std::string &s2) const {
|
return a.size() == b.size() &&
|
||||||
return s1.size() == s2.size() &&
|
std::equal(a.begin(), a.end(), b.begin(),
|
||||||
std::equal(s1.begin(), s1.end(), s2.begin(), [](char a, char b) {
|
[](char a, char b) { return to_lower(a) == to_lower(b); });
|
||||||
return to_lower(a) == to_lower(b);
|
}
|
||||||
});
|
|
||||||
|
struct equal_to {
|
||||||
|
bool operator()(const std::string &a, const std::string &b) const {
|
||||||
|
return equal(a, b);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct case_ignore_hash {
|
struct hash {
|
||||||
size_t operator()(const std::string &key) const {
|
size_t operator()(const std::string &key) const {
|
||||||
return hash_core(key.data(), key.size(), 0);
|
return hash_core(key.data(), key.size(), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr size_t hash_core(const char *s, size_t l, size_t h) const {
|
size_t hash_core(const char *s, size_t l, size_t h) const {
|
||||||
return (l == 0)
|
return (l == 0) ? h
|
||||||
? h
|
|
||||||
: hash_core(s + 1, l - 1,
|
: hash_core(s + 1, l - 1,
|
||||||
(h * 33) ^ static_cast<unsigned char>(to_lower(*s)));
|
// Unsets the 6 high bits of h, therefore no
|
||||||
|
// overflow happens
|
||||||
|
(((std::numeric_limits<size_t>::max)() >> 6) &
|
||||||
|
h * 33) ^
|
||||||
|
static_cast<unsigned char>(to_lower(*s)));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
}; // namespace case_ignore
|
||||||
|
|
||||||
// This is based on
|
// This is based on
|
||||||
// "http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4189".
|
// "http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4189".
|
||||||
|
|
||||||
@ -473,8 +483,8 @@ enum StatusCode {
|
|||||||
};
|
};
|
||||||
|
|
||||||
using Headers =
|
using Headers =
|
||||||
std::unordered_multimap<std::string, std::string, detail::case_ignore_hash,
|
std::unordered_multimap<std::string, std::string, detail::case_ignore::hash,
|
||||||
detail::case_ignore_equal>;
|
detail::case_ignore::equal_to>;
|
||||||
|
|
||||||
using Params = std::multimap<std::string, std::string>;
|
using Params = std::multimap<std::string, std::string>;
|
||||||
using Match = std::smatch;
|
using Match = std::smatch;
|
||||||
@ -3996,14 +4006,6 @@ inline const char *get_header_value(const Headers &headers,
|
|||||||
return def;
|
return def;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool compare_case_ignore(const std::string &a, const std::string &b) {
|
|
||||||
if (a.size() != b.size()) { return false; }
|
|
||||||
for (size_t i = 0; i < b.size(); i++) {
|
|
||||||
if (to_lower(a[i]) != to_lower(b[i])) { return false; }
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
inline bool parse_header(const char *beg, const char *end, T fn) {
|
inline bool parse_header(const char *beg, const char *end, T fn) {
|
||||||
// Skip trailing spaces and tabs.
|
// Skip trailing spaces and tabs.
|
||||||
@ -4031,7 +4033,7 @@ inline bool parse_header(const char *beg, const char *end, T fn) {
|
|||||||
if (!key_len) { return false; }
|
if (!key_len) { return false; }
|
||||||
|
|
||||||
auto key = std::string(beg, key_end);
|
auto key = std::string(beg, key_end);
|
||||||
auto val = compare_case_ignore(key, "Location")
|
auto val = case_ignore::equal(key, "Location")
|
||||||
? std::string(p, end)
|
? std::string(p, end)
|
||||||
: decode_url(std::string(p, end), false);
|
: decode_url(std::string(p, end), false);
|
||||||
|
|
||||||
@ -4196,7 +4198,7 @@ inline bool read_content_chunked(Stream &strm, T &x,
|
|||||||
}
|
}
|
||||||
|
|
||||||
inline bool is_chunked_transfer_encoding(const Headers &headers) {
|
inline bool is_chunked_transfer_encoding(const Headers &headers) {
|
||||||
return compare_case_ignore(
|
return case_ignore::equal(
|
||||||
get_header_value(headers, "Transfer-Encoding", "", 0), "chunked");
|
get_header_value(headers, "Transfer-Encoding", "", 0), "chunked");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -4835,7 +4837,9 @@ private:
|
|||||||
const std::string &b) const {
|
const std::string &b) const {
|
||||||
if (a.size() < b.size()) { return false; }
|
if (a.size() < b.size()) { return false; }
|
||||||
for (size_t i = 0; i < b.size(); i++) {
|
for (size_t i = 0; i < b.size(); i++) {
|
||||||
if (to_lower(a[i]) != to_lower(b[i])) { return false; }
|
if (case_ignore::to_lower(a[i]) != case_ignore::to_lower(b[i])) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user