summaryrefslogtreecommitdiff
path: root/platform/default
diff options
context:
space:
mode:
authorKonstantin Käfer <mail@kkaefer.com>2018-01-24 08:34:05 -0800
committerKonstantin Käfer <mail@kkaefer.com>2018-01-25 13:45:47 -0800
commitc148a7a0616568b0ae82ab8dc89c1c096af10537 (patch)
tree678bc978cb108a21f9ed22a6b88d33952b0083e7 /platform/default
parentda7759a273c096117e513b249741355865f9af8e (diff)
downloadqtlocation-mapboxgl-c148a7a0616568b0ae82ab8dc89c1c096af10537.tar.gz
[core] align implementations of local and asset file source
Diffstat (limited to 'platform/default')
-rw-r--r--platform/default/asset_file_source.cpp27
-rw-r--r--platform/default/default_file_source.cpp12
-rw-r--r--platform/default/local_file_source.cpp17
3 files changed, 30 insertions, 26 deletions
diff --git a/platform/default/asset_file_source.cpp b/platform/default/asset_file_source.cpp
index 54dbb8d0f6..3063bf88a0 100644
--- a/platform/default/asset_file_source.cpp
+++ b/platform/default/asset_file_source.cpp
@@ -10,6 +10,12 @@
#include <sys/types.h>
#include <sys/stat.h>
+namespace {
+
+const std::string assetProtocol = "asset://";
+
+} // namespace
+
namespace mbgl {
class AssetFileSource::Impl {
@@ -19,18 +25,17 @@ public:
}
void request(const std::string& url, ActorRef<FileSourceRequest> req) {
- std::string path;
+ Response response;
- if (url.size() <= 8 || url[8] == '/') {
- // This is an empty or absolute path.
- path = mbgl::util::percentDecode(url.substr(8));
- } else {
- // This is a relative path. Prefix with the application root.
- path = root + "/" + mbgl::util::percentDecode(url.substr(8));
+ if (!acceptsURL(url)) {
+ response.error = std::make_unique<Response::Error>(Response::Error::Reason::Other,
+ "Invalid asset URL");
+ req.invoke(&FileSourceRequest::setResponse, response);
+ return;
}
- Response response;
-
+ // Cut off the protocol and prefix with path.
+ const auto path = root + "/" + mbgl::util::percentDecode(url.substr(assetProtocol.size()));
struct stat buf;
int result = stat(path.c_str(), &buf);
@@ -69,4 +74,8 @@ std::unique_ptr<AsyncRequest> AssetFileSource::request(const Resource& resource,
return std::move(req);
}
+bool AssetFileSource::acceptsURL(const std::string& url) {
+ return std::equal(assetProtocol.begin(), assetProtocol.end(), url.begin());
+}
+
} // namespace mbgl
diff --git a/platform/default/default_file_source.cpp b/platform/default/default_file_source.cpp
index 608b782ab9..cb602995a4 100644
--- a/platform/default/default_file_source.cpp
+++ b/platform/default/default_file_source.cpp
@@ -14,16 +14,6 @@
#include <cassert>
-namespace {
-
-const std::string assetProtocol = "asset://";
-
-bool isAssetURL(const std::string& url) {
- return std::equal(assetProtocol.begin(), assetProtocol.end(), url.begin());
-}
-
-} // namespace
-
namespace mbgl {
class DefaultFileSource::Impl {
@@ -118,7 +108,7 @@ public:
ref.invoke(&FileSourceRequest::setResponse, res);
};
- if (isAssetURL(resource.url)) {
+ if (AssetFileSource::acceptsURL(resource.url)) {
//Asset request
tasks[req] = assetFileSource->request(resource, callback);
} else if (LocalFileSource::acceptsURL(resource.url)) {
diff --git a/platform/default/local_file_source.cpp b/platform/default/local_file_source.cpp
index 21803d0935..0635e86d80 100644
--- a/platform/default/local_file_source.cpp
+++ b/platform/default/local_file_source.cpp
@@ -16,8 +16,7 @@
namespace {
-const char* protocol = "file://";
-const std::size_t protocolLength = 7;
+const std::string fileProtocol = "file://";
} // namespace
@@ -28,11 +27,17 @@ public:
Impl(ActorRef<Impl>) {}
void request(const std::string& url, ActorRef<FileSourceRequest> req) {
- // Cut off the protocol
- std::string path = mbgl::util::percentDecode(url.substr(protocolLength));
-
Response response;
+ if (!acceptsURL(url)) {
+ response.error = std::make_unique<Response::Error>(Response::Error::Reason::Other,
+ "Invalid file URL");
+ req.invoke(&FileSourceRequest::setResponse, response);
+ return;
+ }
+
+ // Cut off the protocol and prefix with path.
+ const auto path = mbgl::util::percentDecode(url.substr(fileProtocol.size()));
struct stat buf;
int result = stat(path.c_str(), &buf);
@@ -70,7 +75,7 @@ std::unique_ptr<AsyncRequest> LocalFileSource::request(const Resource& resource,
}
bool LocalFileSource::acceptsURL(const std::string& url) {
- return url.compare(0, protocolLength, protocol) == 0;
+ return std::equal(fileProtocol.begin(), fileProtocol.end(), url.begin());
}
} // namespace mbgl