parse cache-control and last-modified headers

This commit is contained in:
Konstantin Käfer
2014-08-25 16:56:56 +02:00
parent 28446bed24
commit b94635f704
8 changed files with 121 additions and 15 deletions

26
src/platform/response.cpp Normal file
View File

@@ -0,0 +1,26 @@
#include <mbgl/platform/response.hpp>
#include <curl/curl.h>
#include <cstdio>
namespace mbgl {
namespace platform {
void Response::setCacheControl(const char *value) {
int 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=%u", &seconds) == 1) {
if (std::time(&expires) != -1) {
expires += seconds;
}
}
}
void Response::setLastModified(const char *value) {
modified = curl_getdate(value, nullptr);
}
}
}

View File

@@ -32,6 +32,7 @@ void FileSource::createSchema() {
"`url` TEXT PRIMARY KEY NOT NULL,"
"`code` INTEGER NOT NULL,"
"`type` INTEGER NOT NULL,"
"`modified` INTEGER,"
"`expires` INTEGER,"
"`data` BLOB,"
"`compressed` INTEGER NOT NULL DEFAULT 0"
@@ -143,7 +144,7 @@ void FileSource::saveFile(ResourceType type, const std::string &url, platform::R
sqlite3_stmt *stmt = nullptr;
int err;
err = sqlite3_prepare_v2(db, "REPLACE INTO `http_cache` (`url`, `code`, `type`, `data`, `compressed`) VALUES(?, ?, ?, ?, ?)", -1, &stmt, nullptr);
err = sqlite3_prepare_v2(db, "REPLACE INTO `http_cache` (`url`, `code`, `type`, `modified`, `expires`, `data`, `compressed`) VALUES(?, ?, ?, ?, ?, ?, ?)", -1, &stmt, nullptr);
if (err != SQLITE_OK) {
Log::Warning(Event::Database, "%s: %s", sqlite3_errstr(err), sqlite3_errmsg(db));
return;
@@ -179,15 +180,35 @@ void FileSource::saveFile(ResourceType type, const std::string &url, platform::R
return;
}
err = sqlite3_bind_int(stmt, 4, res->modified);
if (err != SQLITE_OK) {
Log::Warning(Event::Database, "%s: %s", sqlite3_errstr(err), sqlite3_errmsg(db));
err = sqlite3_finalize(stmt);
if (err != SQLITE_OK) {
Log::Warning(Event::Database, "%s: %s", sqlite3_errstr(err), sqlite3_errmsg(db));
}
return;
}
err = sqlite3_bind_int(stmt, 5, res->expires);
if (err != SQLITE_OK) {
Log::Warning(Event::Database, "%s: %s", sqlite3_errstr(err), sqlite3_errmsg(db));
err = sqlite3_finalize(stmt);
if (err != SQLITE_OK) {
Log::Warning(Event::Database, "%s: %s", sqlite3_errstr(err), sqlite3_errmsg(db));
}
return;
}
bool compressed = false;
switch (type) {
case ResourceType::Image:
err = sqlite3_bind_blob(stmt, 4, res->body.data(), res->body.size(), SQLITE_STATIC);
err = sqlite3_bind_blob(stmt, 6, res->body.data(), res->body.size(), SQLITE_STATIC);
break;
default:
const std::string *data = new std::string(std::move(util::compress(res->body)));
compressed = true;
err = sqlite3_bind_blob(stmt, 4, data->data(), data->size(), [](void *p) { delete (std::string *)p; });
err = sqlite3_bind_blob(stmt, 6, data->data(), data->size(), [](void *p) { delete (std::string *)p; });
break;
}
if (err != SQLITE_OK) {
@@ -199,7 +220,7 @@ void FileSource::saveFile(ResourceType type, const std::string &url, platform::R
return;
}
err = sqlite3_bind_int(stmt, 5, compressed);
err = sqlite3_bind_int(stmt, 7, compressed);
if (err != SQLITE_OK) {
Log::Warning(Event::Database, "%s: %s", sqlite3_errstr(err), sqlite3_errmsg(db));
err = sqlite3_finalize(stmt);