diff options
author | Konstantin Käfer <mail@kkaefer.com> | 2014-08-25 16:56:56 +0200 |
---|---|---|
committer | Konstantin Käfer <mail@kkaefer.com> | 2014-09-24 16:12:03 +0200 |
commit | b94635f70430c3659cd57596e49649d376284473 (patch) | |
tree | 8d3957bf7c26c8fffeb7f961e0826ab625d5052a /src | |
parent | 28446bed24689b2bac7c8bfbd37741a7fa4fa6be (diff) | |
download | qtlocation-mapboxgl-b94635f70430c3659cd57596e49649d376284473.tar.gz |
parse cache-control and last-modified headers
Diffstat (limited to 'src')
-rw-r--r-- | src/platform/response.cpp | 26 | ||||
-rw-r--r-- | src/util/filesource.cpp | 29 |
2 files changed, 51 insertions, 4 deletions
diff --git a/src/platform/response.cpp b/src/platform/response.cpp new file mode 100644 index 0000000000..f2117c8894 --- /dev/null +++ b/src/platform/response.cpp @@ -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); +} + +} +} diff --git a/src/util/filesource.cpp b/src/util/filesource.cpp index d626092bce..8c14f6e4e0 100644 --- a/src/util/filesource.cpp +++ b/src/util/filesource.cpp @@ -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); |