[core] more robust max-age parsing

This commit is contained in:
Konstantin Käfer
2015-11-19 21:47:15 +01:00
parent 94e2d8a02c
commit b5f7cafab4
5 changed files with 128 additions and 5 deletions

View File

@@ -1,18 +1,18 @@
#include <mbgl/storage/http_request_base.hpp>
#include <mbgl/util/http_header.hpp>
#include <mbgl/util/chrono.hpp>
namespace mbgl {
int64_t HTTPRequestBase::parseCacheControl(const char *value) {
if (value) {
unsigned long long seconds = 0;
// TODO: cache-control may contain other information as well:
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9
if (std::sscanf(value, "max-age=%llu", &seconds) == 1) {
const auto cacheControl = http::CacheControl::parse(value);
if (cacheControl.maxAge) {
return std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now().time_since_epoch()).count() +
seconds;
*cacheControl.maxAge;
}
}