diff options
author | Konstantin Käfer <mail@kkaefer.com> | 2016-05-13 18:24:37 +0200 |
---|---|---|
committer | Konstantin Käfer <mail@kkaefer.com> | 2016-05-18 12:40:22 +0200 |
commit | 8ee222c45e31413b03d1c83006cf4eadc0e6d3a7 (patch) | |
tree | 2394a4a3cd216eb3d2d6a80b6a8ad3f94337eeda /platform/default/default_file_source.cpp | |
parent | c5fc7f1980814a1063773e6c876dad66b7909adf (diff) | |
download | qtlocation-mapboxgl-8ee222c45e31413b03d1c83006cf4eadc0e6d3a7.tar.gz |
[core] allow creating optional requests
Introduces "optional" requests. These should be fulfilled by the FileSource only there's a low-cost/easy way to obtain the data (e.g. from a local cache). If the data for an optional request cannot be found, it *must* return a Response object with a NotFound error.
Traditional "required" requests still work the same way, with one change: If you set any prior* field in the Resource (i.e. priorModified, priorEtag, or priorExpires), the DefaultFileSource assumes that you already have the cache value and will not consult the cache before performing the request. If a prior cache lookup didn't turn up any data, and you therefore don't have an Etag or Modified value, you can still skip the cache by setting priorExpires. This will of course always result in a non-conditional HTTP request.
Diffstat (limited to 'platform/default/default_file_source.cpp')
-rw-r--r-- | platform/default/default_file_source.cpp | 36 |
1 files changed, 25 insertions, 11 deletions
diff --git a/platform/default/default_file_source.cpp b/platform/default/default_file_source.cpp index afcb63e945..9d369210f8 100644 --- a/platform/default/default_file_source.cpp +++ b/platform/default/default_file_source.cpp @@ -82,21 +82,35 @@ public: } void request(AsyncRequest* req, Resource resource, Callback callback) { - auto offlineResponse = offlineDatabase.get(resource); - Resource revalidation = resource; - if (offlineResponse) { - revalidation.priorModified = offlineResponse->modified; - revalidation.priorExpires = offlineResponse->expires; - revalidation.priorEtag = offlineResponse->etag; - callback(*offlineResponse); + const bool hasPrior = resource.priorEtag || resource.priorModified || resource.priorExpires; + if (!hasPrior || resource.necessity == Resource::Optional) { + auto offlineResponse = offlineDatabase.get(resource); + + if (resource.necessity == Resource::Optional && !offlineResponse) { + // Ensure there's always a response that we can send, so the caller knows that + // there's no optional data available in the cache. + offlineResponse.emplace(); + offlineResponse->noContent = true; + offlineResponse->error = std::make_unique<Response::Error>( + Response::Error::Reason::NotFound, "Not found in offline database"); + } + + if (offlineResponse) { + revalidation.priorModified = offlineResponse->modified; + revalidation.priorExpires = offlineResponse->expires; + revalidation.priorEtag = offlineResponse->etag; + callback(*offlineResponse); + } } - tasks[req] = onlineFileSource.request(revalidation, [=] (Response onlineResponse) { - this->offlineDatabase.put(revalidation, onlineResponse); - callback(onlineResponse); - }); + if (resource.necessity == Resource::Required) { + tasks[req] = onlineFileSource.request(revalidation, [=] (Response onlineResponse) { + this->offlineDatabase.put(revalidation, onlineResponse); + callback(onlineResponse); + }); + } } void cancel(AsyncRequest* req) { |