summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorKonstantin Käfer <mail@kkaefer.com>2015-11-19 21:47:15 +0100
committerKonstantin Käfer <mail@kkaefer.com>2015-11-20 14:39:44 +0100
commitb5f7cafab45471bd7d714fb2972e33603a4baaae (patch)
treeaff2a9125627105e1bd977105df5175f332b89dc /src
parent94e2d8a02c321380e650d055e7e7d6d6ed1c529d (diff)
downloadqtlocation-mapboxgl-b5f7cafab45471bd7d714fb2972e33603a4baaae.tar.gz
[core] more robust max-age parsing
Diffstat (limited to 'src')
-rw-r--r--src/mbgl/storage/http_request_base.cpp10
-rw-r--r--src/mbgl/util/http_header.cpp29
-rw-r--r--src/mbgl/util/http_header.hpp22
3 files changed, 56 insertions, 5 deletions
diff --git a/src/mbgl/storage/http_request_base.cpp b/src/mbgl/storage/http_request_base.cpp
index 2da453ead8..f9e40c4f2b 100644
--- a/src/mbgl/storage/http_request_base.cpp
+++ b/src/mbgl/storage/http_request_base.cpp
@@ -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;
}
}
diff --git a/src/mbgl/util/http_header.cpp b/src/mbgl/util/http_header.cpp
new file mode 100644
index 0000000000..00aee151c8
--- /dev/null
+++ b/src/mbgl/util/http_header.cpp
@@ -0,0 +1,29 @@
+#include <mbgl/util/http_header.hpp>
+
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wunknown-pragmas"
+#pragma GCC diagnostic ignored "-Wunused-parameter"
+#pragma GCC diagnostic ignored "-Wshadow"
+#include <boost/spirit/include/qi.hpp>
+#include <boost/spirit/include/phoenix_core.hpp>
+#include <boost/spirit/include/phoenix_operator.hpp>
+#pragma GCC diagnostic pop
+
+namespace mbgl {
+namespace http {
+
+CacheControl CacheControl::parse(const std::string& value) {
+ namespace qi = boost::spirit::qi;
+ namespace phoenix = boost::phoenix;
+
+ CacheControl result;
+ qi::phrase_parse(value.begin(), value.end(), (
+ (qi::lit("must-revalidate") [ phoenix::ref(result.mustRevalidate) = true ]) |
+ (qi::lit("max-age") >> '=' >> qi::ulong_long [ phoenix::ref(result.maxAge) = qi::_1 ]) |
+ (*(('"' >> *(('\\' >> qi::char_) | (qi::char_ - '"')) >> '"') | (qi::char_ - '"' - ',')))
+ ) % ',', qi::ascii::space);
+ return result;
+}
+
+} // namespace http
+} // namespace mbgl
diff --git a/src/mbgl/util/http_header.hpp b/src/mbgl/util/http_header.hpp
new file mode 100644
index 0000000000..bd1f809cee
--- /dev/null
+++ b/src/mbgl/util/http_header.hpp
@@ -0,0 +1,22 @@
+#ifndef MBGL_UTIL_HTTP_HEADER
+#define MBGL_UTIL_HTTP_HEADER
+
+#include <mapbox/optional.hpp>
+
+#include <string>
+
+namespace mbgl {
+namespace http {
+
+class CacheControl {
+public:
+ static CacheControl parse(const std::string&);
+
+ mapbox::util::optional<uint64_t> maxAge;
+ bool mustRevalidate = false;
+};
+
+} // namespace http
+} // namespace mbgl
+
+#endif