summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gyp/mbgl-ios.gypi3
-rw-r--r--gyp/mbgl-linux.gypi3
-rw-r--r--gyp/mbgl-osx.gypi3
-rw-r--r--include/mbgl/platform/platform.hpp2
-rw-r--r--include/mbgl/storage/asset_request.hpp27
-rw-r--r--include/mbgl/storage/file_request.hpp27
m---------ios/mapbox-gl-cocoa0
-rw-r--r--linux/main.cpp4
-rw-r--r--macosx/main.mm4
-rw-r--r--platform/darwin/application_root.mm18
-rw-r--r--platform/default/application_root.cpp14
-rw-r--r--platform/default/asset_request_libuv.cpp (renamed from platform/default/file_request_libuv.cpp)67
-rw-r--r--src/mbgl/storage/caching_http_file_source.cpp6
13 files changed, 113 insertions, 65 deletions
diff --git a/gyp/mbgl-ios.gypi b/gyp/mbgl-ios.gypi
index da05619a34..bd96486d69 100644
--- a/gyp/mbgl-ios.gypi
+++ b/gyp/mbgl-ios.gypi
@@ -39,8 +39,9 @@
'../platform/darwin/log_nslog.mm',
'../platform/darwin/string_nsstring.mm',
'../platform/darwin/http_request_baton_cocoa.mm',
+ '../platform/darwin/application_root.mm',
'../platform/darwin/image.mm',
- '../platform/default/file_request_libuv.cpp',
+ '../platform/default/asset_request_libuv.cpp',
],
'include_dirs': [
'../include',
diff --git a/gyp/mbgl-linux.gypi b/gyp/mbgl-linux.gypi
index 51df45ecc7..d9268d9200 100644
--- a/gyp/mbgl-linux.gypi
+++ b/gyp/mbgl-linux.gypi
@@ -34,8 +34,9 @@
'../platform/default/shader_cache_tmp.cpp',
'../platform/default/log_stderr.cpp',
'../platform/default/string_stdlib.cpp',
- '../platform/default/file_request_libuv.cpp',
+ '../platform/default/asset_request_libuv.cpp',
'../platform/default/http_request_baton_curl.cpp',
+ '../platform/default/application_root.cpp',
'../platform/default/image.cpp',
'../platform/default/image_reader.cpp',
'../platform/default/png_reader.cpp',
diff --git a/gyp/mbgl-osx.gypi b/gyp/mbgl-osx.gypi
index cc6db2eb20..0cc29f6e73 100644
--- a/gyp/mbgl-osx.gypi
+++ b/gyp/mbgl-osx.gypi
@@ -14,8 +14,9 @@
'../platform/darwin/log_nslog.mm',
'../platform/darwin/string_nsstring.mm',
'../platform/darwin/http_request_baton_cocoa.mm',
+ '../platform/darwin/application_root.mm',
'../platform/darwin/image.mm',
- '../platform/default/file_request_libuv.cpp',
+ '../platform/default/asset_request_libuv.cpp',
],
'include_dirs': [
'../include',
diff --git a/include/mbgl/platform/platform.hpp b/include/mbgl/platform/platform.hpp
index 9edcc23ccd..02c612c833 100644
--- a/include/mbgl/platform/platform.hpp
+++ b/include/mbgl/platform/platform.hpp
@@ -23,6 +23,8 @@ std::string defaultCacheDatabase();
// Returns the path to the default shader cache on this system.
std::string defaultShaderCache();
+std::string applicationRoot();
+
// Shows an alpha image with the specified dimensions in a named window.
void show_debug_image(std::string name, const char *data, size_t width, size_t height);
diff --git a/include/mbgl/storage/asset_request.hpp b/include/mbgl/storage/asset_request.hpp
new file mode 100644
index 0000000000..3114d41ad2
--- /dev/null
+++ b/include/mbgl/storage/asset_request.hpp
@@ -0,0 +1,27 @@
+#ifndef MBGL_STORAGE_ASSET_REQUEST
+#define MBGL_STORAGE_ASSET_REQUEST
+
+#include <mbgl/storage/base_request.hpp>
+
+namespace mbgl {
+
+typedef struct uv_loop_s uv_loop_t;
+
+struct AssetRequestBaton;
+
+class AssetRequest : public BaseRequest {
+public:
+ AssetRequest(const std::string &path, uv_loop_t *loop);
+ ~AssetRequest();
+
+ void cancel();
+
+private:
+ AssetRequestBaton *ptr = nullptr;
+
+ friend struct AssetRequestBaton;
+};
+
+}
+
+#endif
diff --git a/include/mbgl/storage/file_request.hpp b/include/mbgl/storage/file_request.hpp
deleted file mode 100644
index 3de2d5b60d..0000000000
--- a/include/mbgl/storage/file_request.hpp
+++ /dev/null
@@ -1,27 +0,0 @@
-#ifndef MBGL_STORAGE_FILE_REQUEST
-#define MBGL_STORAGE_FILE_REQUEST
-
-#include <mbgl/storage/base_request.hpp>
-
-namespace mbgl {
-
-typedef struct uv_loop_s uv_loop_t;
-
-struct FileRequestBaton;
-
-class FileRequest : public BaseRequest {
-public:
- FileRequest(const std::string &path, uv_loop_t *loop);
- ~FileRequest();
-
- void cancel();
-
-private:
- FileRequestBaton *ptr = nullptr;
-
- friend struct FileRequestBaton;
-};
-
-}
-
-#endif
diff --git a/ios/mapbox-gl-cocoa b/ios/mapbox-gl-cocoa
-Subproject 1533d3288f5678d13bc550b43fb7dbb6ecdfa29
+Subproject 27442b6816ff7df9054f0898968471283ca773c
diff --git a/linux/main.cpp b/linux/main.cpp
index c5b2569ba3..e2965a8ea7 100644
--- a/linux/main.cpp
+++ b/linux/main.cpp
@@ -49,7 +49,7 @@ int main(int argc, char *argv[]) {
// handle fullscreen_flag
break;
case 's':
- style = std::string("file://") + std::string(optarg);
+ style = std::string("asset://") + std::string(optarg);
default:
break;
}
@@ -83,7 +83,7 @@ int main(int argc, char *argv[]) {
// Load style
if (style.empty())
- style = std::string("file://") + uv::cwd() + std::string("/styles/bright-v6.json");
+ style = std::string("asset://") + std::string("styles/bright-v6.json");
map.setStyleURL(style);
diff --git a/macosx/main.mm b/macosx/main.mm
index 228b05f0b4..39ddde10d0 100644
--- a/macosx/main.mm
+++ b/macosx/main.mm
@@ -102,9 +102,7 @@ int main() {
if (accessToken) fileSource.setAccessToken([accessToken cStringUsingEncoding:[NSString defaultCStringEncoding]]);
// Load style
- const std::string path([[[NSBundle mainBundle] pathForResource:@"bright-v6" ofType:@"json" inDirectory:@"styles/"] UTF8String]);
-
- map.setStyleURL(std::string("file://") + path);
+ map.setStyleURL("asset://styles/bright-v6.json");
int ret = view.run();
diff --git a/platform/darwin/application_root.mm b/platform/darwin/application_root.mm
new file mode 100644
index 0000000000..19b872c54d
--- /dev/null
+++ b/platform/darwin/application_root.mm
@@ -0,0 +1,18 @@
+#import <Foundation/Foundation.h>
+
+#include <mbgl/platform/platform.hpp>
+
+namespace mbgl {
+namespace platform {
+
+// Returns the path to the default shader cache on this system.
+std::string applicationRoot() {
+ static const std::string root = []() -> std::string {
+ NSString *path = [[[NSBundle mainBundle] resourceURL] path];
+ return {[path cStringUsingEncoding : NSUTF8StringEncoding],
+ [path lengthOfBytesUsingEncoding:NSUTF8StringEncoding]};
+ }();
+ return root;
+}
+}
+}
diff --git a/platform/default/application_root.cpp b/platform/default/application_root.cpp
new file mode 100644
index 0000000000..e6f73e0211
--- /dev/null
+++ b/platform/default/application_root.cpp
@@ -0,0 +1,14 @@
+#include <mbgl/platform/platform.hpp>
+
+#include <mbgl/util/uv.hpp>
+
+namespace mbgl {
+namespace platform {
+
+// Returns the path to the default cache database on this system.
+std::string applicationRoot() {
+ return uv::cwd();
+}
+
+}
+}
diff --git a/platform/default/file_request_libuv.cpp b/platform/default/asset_request_libuv.cpp
index f874bed8d2..202e39967e 100644
--- a/platform/default/file_request_libuv.cpp
+++ b/platform/default/asset_request_libuv.cpp
@@ -1,5 +1,6 @@
-#include <mbgl/storage/file_request.hpp>
+#include <mbgl/storage/asset_request.hpp>
#include <mbgl/storage/response.hpp>
+#include <mbgl/platform/platform.hpp>
#include <mbgl/util/std.hpp>
#include <uv.h>
@@ -8,9 +9,9 @@
namespace mbgl {
-struct FileRequestBaton {
- FileRequestBaton(FileRequest *request_, const std::string &path, uv_loop_t *loop);
- ~FileRequestBaton();
+struct AssetRequestBaton {
+ AssetRequestBaton(AssetRequest *request_, const std::string &path, uv_loop_t *loop);
+ ~AssetRequestBaton();
void cancel();
static void file_opened(uv_fs_t *req);
@@ -21,7 +22,7 @@ struct FileRequestBaton {
static void cleanup(uv_fs_t *req);
const std::thread::id thread_id;
- FileRequest *request = nullptr;
+ AssetRequest *request = nullptr;
uv_fs_t req;
uv_file fd = -1;
bool canceled = false;
@@ -29,16 +30,16 @@ struct FileRequestBaton {
uv_buf_t buffer;
};
-FileRequestBaton::FileRequestBaton(FileRequest *request_, const std::string &path, uv_loop_t *loop)
+AssetRequestBaton::AssetRequestBaton(AssetRequest *request_, const std::string &path, uv_loop_t *loop)
: thread_id(std::this_thread::get_id()), request(request_) {
req.data = this;
uv_fs_open(loop, &req, path.c_str(), O_RDONLY, S_IRUSR, file_opened);
}
-FileRequestBaton::~FileRequestBaton() {
+AssetRequestBaton::~AssetRequestBaton() {
}
-void FileRequestBaton::cancel() {
+void AssetRequestBaton::cancel() {
canceled = true;
// uv_cancel fails frequently when the request has already been started.
@@ -47,8 +48,8 @@ void FileRequestBaton::cancel() {
uv_cancel((uv_req_t *)&req);
}
-void FileRequestBaton::notify_error(uv_fs_t *req) {
- FileRequestBaton *ptr = (FileRequestBaton *)req->data;
+void AssetRequestBaton::notify_error(uv_fs_t *req) {
+ AssetRequestBaton *ptr = (AssetRequestBaton *)req->data;
assert(ptr->thread_id == std::this_thread::get_id());
if (ptr->request && req->result < 0 && !ptr->canceled && req->result != UV_ECANCELED) {
@@ -63,8 +64,8 @@ void FileRequestBaton::notify_error(uv_fs_t *req) {
}
}
-void FileRequestBaton::file_opened(uv_fs_t *req) {
- FileRequestBaton *ptr = (FileRequestBaton *)req->data;
+void AssetRequestBaton::file_opened(uv_fs_t *req) {
+ AssetRequestBaton *ptr = (AssetRequestBaton *)req->data;
assert(ptr->thread_id == std::this_thread::get_id());
if (req->result < 0) {
@@ -78,7 +79,7 @@ void FileRequestBaton::file_opened(uv_fs_t *req) {
uv_fs_req_cleanup(req);
if (ptr->canceled || !ptr->request) {
- // Either the FileRequest object has been destructed, or the
+ // Either the AssetRequest object has been destructed, or the
// request was canceled.
uv_fs_close(req->loop, req, fd, file_closed);
} else {
@@ -88,8 +89,8 @@ void FileRequestBaton::file_opened(uv_fs_t *req) {
}
}
-void FileRequestBaton::file_stated(uv_fs_t *req) {
- FileRequestBaton *ptr = (FileRequestBaton *)req->data;
+void AssetRequestBaton::file_stated(uv_fs_t *req) {
+ AssetRequestBaton *ptr = (AssetRequestBaton *)req->data;
assert(ptr->thread_id == std::this_thread::get_id());
if (req->result != 0 || ptr->canceled || !ptr->request) {
@@ -135,8 +136,8 @@ void FileRequestBaton::file_stated(uv_fs_t *req) {
}
}
-void FileRequestBaton::file_read(uv_fs_t *req) {
- FileRequestBaton *ptr = (FileRequestBaton *)req->data;
+void AssetRequestBaton::file_read(uv_fs_t *req) {
+ AssetRequestBaton *ptr = (AssetRequestBaton *)req->data;
assert(ptr->thread_id == std::this_thread::get_id());
if (req->result < 0 || ptr->canceled || !ptr->request) {
@@ -157,8 +158,8 @@ void FileRequestBaton::file_read(uv_fs_t *req) {
uv_fs_close(req->loop, req, ptr->fd, file_closed);
}
-void FileRequestBaton::file_closed(uv_fs_t *req) {
- assert(((FileRequestBaton *)req->data)->thread_id == std::this_thread::get_id());
+void AssetRequestBaton::file_closed(uv_fs_t *req) {
+ assert(((AssetRequestBaton *)req->data)->thread_id == std::this_thread::get_id());
if (req->result < 0) {
// Closing the file failed. But there isn't anything we can do.
@@ -167,8 +168,8 @@ void FileRequestBaton::file_closed(uv_fs_t *req) {
cleanup(req);
}
-void FileRequestBaton::cleanup(uv_fs_t *req) {
- FileRequestBaton *ptr = (FileRequestBaton *)req->data;
+void AssetRequestBaton::cleanup(uv_fs_t *req) {
+ AssetRequestBaton *ptr = (AssetRequestBaton *)req->data;
assert(ptr->thread_id == std::this_thread::get_id());
if (ptr->request) {
@@ -180,17 +181,29 @@ void FileRequestBaton::cleanup(uv_fs_t *req) {
}
-FileRequest::FileRequest(const std::string &path_, uv_loop_t *loop)
- : BaseRequest(path_), ptr(new FileRequestBaton(this, path, loop)) {
+AssetRequest::AssetRequest(const std::string &path_, uv_loop_t *loop)
+ : BaseRequest(path_) {
+ if (!path.empty() && path[0] == '/') {
+ // This is an absolute path. We don't allow this. Note that this is not a way to absolutely
+ // prevent access to resources outside the application bundle; e.g. there could be symlinks
+ // in the application bundle that link to outside. We don't care about these.
+ response = util::make_unique<Response>();
+ response->code = 403;
+ response->message = "Path is outside the application bundle";
+ notify();
+ } else {
+ // Note: The AssetRequestBaton object is deleted in AssetRequestBaton::cleanup().
+ ptr = new AssetRequestBaton(this, platform::applicationRoot() + "/" + path, loop);
+ }
}
-void FileRequest::cancel() {
+void AssetRequest::cancel() {
assert(thread_id == std::this_thread::get_id());
if (ptr) {
ptr->cancel();
- // When deleting a FileRequest object with a uv_fs_* call is in progress, we are making sure
+ // When deleting a AssetRequest object with a uv_fs_* call is in progress, we are making sure
// that the callback doesn't accidentally reference this object again.
ptr->request = nullptr;
ptr = nullptr;
@@ -199,11 +212,11 @@ void FileRequest::cancel() {
notify();
}
-FileRequest::~FileRequest() {
+AssetRequest::~AssetRequest() {
assert(thread_id == std::this_thread::get_id());
cancel();
- // Note: The FileRequestBaton object is deleted in FileRequestBaton::cleanup().
+ // Note: The AssetRequestBaton object is deleted in AssetRequestBaton::cleanup().
}
}
diff --git a/src/mbgl/storage/caching_http_file_source.cpp b/src/mbgl/storage/caching_http_file_source.cpp
index ddd65118fb..eed3bdd051 100644
--- a/src/mbgl/storage/caching_http_file_source.cpp
+++ b/src/mbgl/storage/caching_http_file_source.cpp
@@ -1,5 +1,5 @@
#include <mbgl/storage/caching_http_file_source.hpp>
-#include <mbgl/storage/file_request.hpp>
+#include <mbgl/storage/asset_request.hpp>
#include <mbgl/storage/http_request.hpp>
#include <mbgl/storage/sqlite_store.hpp>
#include <mbgl/util/uv-messenger.h>
@@ -95,8 +95,8 @@ std::unique_ptr<Request> CachingHTTPFileSource::request(ResourceType type, const
}
if (!req) {
- if (url.substr(0, 7) == "file://") {
- req = std::make_shared<FileRequest>(url.substr(7), loop);
+ if (url.substr(0, 8) == "asset://") {
+ req = std::make_shared<AssetRequest>(url.substr(8), loop);
} else {
req = std::make_shared<HTTPRequest>(type, url, loop, store);
}