[core] Move parseCacheControl() up in the class hierarchy

Avoid duplicating it on all the ports.
This commit is contained in:
Thiago Marcos P. Santos
2015-10-26 17:07:10 +02:00
parent 75dec6ffac
commit c65f809651
5 changed files with 24 additions and 47 deletions

View File

@@ -2,7 +2,6 @@
#include <mbgl/storage/http_request_base.hpp>
#include <mbgl/storage/resource.hpp>
#include <mbgl/storage/response.hpp>
#include <mbgl/util/chrono.hpp>
#include <mbgl/platform/log.hpp>
#include <mbgl/android/jni.hpp>
@@ -248,21 +247,6 @@ void HTTPAndroidRequest::finish() {
delete this;
}
int64_t 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) {
return std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now().time_since_epoch()).count() +
seconds;
}
}
return 0;
}
void HTTPAndroidRequest::onResponse(int code, std::string message, std::string etag, std::string modified, std::string cacheControl, std::string expires, std::string body) {
if (!response) {
response = std::make_unique<Response>();

View File

@@ -198,21 +198,6 @@ void HTTPNSURLRequest::cancel() {
}
}
int64_t 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) {
return std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now().time_since_epoch()).count() +
seconds;
}
}
return 0;
}
void HTTPNSURLRequest::handleResult(NSData *data, NSURLResponse *res, NSError *error) {
if (error) {
if ([error code] == NSURLErrorCancelled) {

View File

@@ -2,7 +2,6 @@
#include <mbgl/storage/http_request_base.hpp>
#include <mbgl/storage/resource.hpp>
#include <mbgl/storage/response.hpp>
#include <mbgl/util/chrono.hpp>
#include <mbgl/platform/log.hpp>
#include <mbgl/util/time.hpp>
@@ -541,21 +540,6 @@ size_t headerMatches(const char *const header, const char *const buffer, const s
return i == headerLength ? i : std::string::npos;
}
int64_t 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) {
return std::chrono::duration_cast<std::chrono::seconds>(
SystemClock::now().time_since_epoch()).count() +
seconds;
}
}
return 0;
}
size_t HTTPCURLRequest::headerCallback(char *const buffer, const size_t size, const size_t nmemb, void *userp) {
assert(userp);
auto baton = reinterpret_cast<HTTPCURLRequest *>(userp);

View File

@@ -0,0 +1,22 @@
#include <mbgl/storage/http_request_base.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) {
return std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now().time_since_epoch()).count() +
seconds;
}
}
return 0;
}
}

View File

@@ -46,6 +46,8 @@ public:
virtual void retry() = 0;
protected:
static int64_t parseCacheControl(const char *value);
bool cancelled;
};