Header entries should be case insensitive

This commit is contained in:
King_DuckZ 2020-09-04 02:29:26 +01:00
parent 9dffbf811c
commit d1573c6218

View file

@ -21,6 +21,8 @@
#include "duckhandy/int_conv.hpp"
#include "api_nap_exception.hpp"
#include <optional>
#include <algorithm>
#include <cctype>
namespace sjd = simdjson::dom;
namespace sj = simdjson;
@ -28,22 +30,30 @@ namespace sj = simdjson;
namespace oro {
namespace {
bool equal (std::string_view a, std::string_view b) {
return a.size() == b.size() and std::equal(
a.begin(), a.end(),
b.begin(),
[](auto c1, auto c2) {return std::tolower(c1) == std::tolower(c2); }
);
}
Header to_header (const nap::HttpResponse& resp) {
using dhandy::int_conv;
Header ret;
for (const auto& entry : resp.header) {
if (entry.first == "Date")
if (equal(entry.first, "Date"))
ret.date = from_header_timestamp(std::string(entry.second));
else if (entry.first == "X-RateLimit-Limit")
else if (equal(entry.first, "X-RateLimit-Limit"))
ret.rate_limit = int_conv<decltype(ret.rate_limit)>(entry.second);
else if (entry.first == "X-RateLimit-Remaining")
else if (equal(entry.first, "X-RateLimit-Remaining"))
ret.rate_limit_remaining = int_conv<decltype(ret.rate_limit_remaining)>(entry.second);
else if (entry.first == "X-RateLimit-Reset")
else if (equal(entry.first, "X-RateLimit-Reset"))
ret.rate_limit_reset = int_conv<decltype(ret.rate_limit_reset)>(entry.second);
else if (entry.first == "Retry-After")
else if (equal(entry.first, "Retry-After"))
ret.retry_after = int_conv<decltype(ret.retry_after)>(entry.second);
else if (entry.first == "Server")
else if (equal(entry.first, "Server"))
ret.server = std::string(entry.second);
}
return ret;