summaryrefslogtreecommitdiff
path: root/src/mbgl/util/http_header.cpp
blob: da68b4a79078f4b5305af0f461f9b4c49efafaa1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <mbgl/util/http_header.hpp>

#include <mbgl/util/string.hpp>

#include <regex>

namespace mbgl {
namespace http {

CacheControl CacheControl::parse(const std::string& value) {
    std::regex maxAgeRegex(R"((?:[^"]*"[^"]*[^\\]")*[^"]*max-age[\s]*=[\s]*([\d]+).*)");
    std::smatch maxAgeMatch;

    CacheControl result;
    result.mustRevalidate = value.find("must-revalidate") != std::string::npos;
    if (std::regex_match(value, maxAgeMatch, maxAgeRegex) && maxAgeMatch.size() == 2) {
        result.maxAge = ::atoll(maxAgeMatch[1].str().c_str());
    }
    return result;
}

optional<Timestamp> CacheControl::toTimePoint() const {
    return maxAge ? util::now() + Seconds(*maxAge) : optional<Timestamp>{};
}

optional<Timestamp> parseRetryHeaders(const optional<std::string>& retryAfter,
                                      const optional<std::string>& xRateLimitReset) {
    if (retryAfter) {
        try {
            auto secs = std::chrono::seconds(std::stoi(*retryAfter));
            return std::chrono::time_point_cast<Seconds>(std::chrono::system_clock::now() + secs);
        } catch (...) {
            return util::parseTimestamp((*retryAfter).c_str());
        }
    } else if (xRateLimitReset) {
        try {
            return util::parseTimestamp(std::stoi(*xRateLimitReset));
        } catch (...) {
            return {};
        }
    }

    return {};
}

} // namespace http
} // namespace mbgl