diff options
59 files changed, 2561 insertions, 2246 deletions
diff --git a/gyp/mbgl-ios.gypi b/gyp/mbgl-ios.gypi index b1a55c8176..2bc9ec718d 100644 --- a/gyp/mbgl-ios.gypi +++ b/gyp/mbgl-ios.gypi @@ -47,7 +47,10 @@ '../src', ], 'xcode_settings': { - 'OTHER_CPLUSPLUSFLAGS': [ '<@(uv_cflags)' ], + 'OTHER_CPLUSPLUSFLAGS': [ + '<@(uv_cflags)', + '-I<(boost_root)/include', + ], }, 'direct_dependent_settings': { 'include_dirs': [ diff --git a/gyp/mbgl-osx.gypi b/gyp/mbgl-osx.gypi index 98b66e62b7..49135fd6c4 100644 --- a/gyp/mbgl-osx.gypi +++ b/gyp/mbgl-osx.gypi @@ -12,7 +12,7 @@ '../platform/osx/cache_database_application_support.mm', '../platform/darwin/log_nslog.mm', '../platform/darwin/string_nsstring.mm', - '../platform/darwin/http_request_baton_cocoa.mm', + '../platform/darwin/http_request_cocoa.mm', '../platform/darwin/application_root.mm', '../platform/darwin/image.mm', '../platform/default/asset_request_libuv.cpp', @@ -22,7 +22,10 @@ '../src', ], 'xcode_settings': { - 'OTHER_CPLUSPLUSFLAGS': [ '<@(uv_cflags)' ], + 'OTHER_CPLUSPLUSFLAGS': [ + '<@(uv_cflags)', + '-I<(boost_root)/include', + ], }, 'direct_dependent_settings': { 'include_dirs': [ diff --git a/include/mbgl/map/map.hpp b/include/mbgl/map/map.hpp index 62f0d62014..876b7b7207 100644 --- a/include/mbgl/map/map.hpp +++ b/include/mbgl/map/map.hpp @@ -123,6 +123,10 @@ public: void startRotating(); void stopRotating(); + // API + void setAccessToken(const std::string &token); + const std::string &getAccessToken() const; + // Debug void setDebug(bool value); void toggleDebug(); @@ -160,7 +164,10 @@ private: Mode mode = Mode::None; +public: // TODO: make private again std::unique_ptr<uv::loop> loop; + +private: std::unique_ptr<uv::worker> workers; std::thread thread; std::unique_ptr<uv::async> asyncTerminate; @@ -214,6 +221,7 @@ private: std::string styleURL; std::string styleJSON = ""; std::vector<std::string> classes; + std::string accessToken; std::chrono::steady_clock::duration defaultTransitionDuration; diff --git a/include/mbgl/storage/asset_request.hpp b/include/mbgl/storage/asset_request.hpp deleted file mode 100644 index 3114d41ad2..0000000000 --- a/include/mbgl/storage/asset_request.hpp +++ /dev/null @@ -1,27 +0,0 @@ -#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/caching_http_file_source.hpp b/include/mbgl/storage/caching_http_file_source.hpp deleted file mode 100644 index 655afa6396..0000000000 --- a/include/mbgl/storage/caching_http_file_source.hpp +++ /dev/null @@ -1,62 +0,0 @@ -#ifndef MBGL_STORAGE_CACHING_HTTP_FILE_SOURCE -#define MBGL_STORAGE_CACHING_HTTP_FILE_SOURCE - -#include <mbgl/storage/file_source.hpp> - -#include <thread> -#include <unordered_map> - -typedef struct uv_messenger_s uv_messenger_t; - -namespace mbgl { - -class BaseRequest; -class SQLiteStore; - -class CachingHTTPFileSource : public FileSource { -public: - CachingHTTPFileSource(const std::string &path_); - virtual ~CachingHTTPFileSource(); - - // Stores and checks the libuv loop for requests - void setLoop(uv_loop_t*); - bool hasLoop(); - void clearLoop(); - - // Set the base path/URL for relative requests - void setBase(std::string); - - // Set the Mapbox API access token - void setAccessToken(std::string); - - // Get the Mapbox API access token - std::string getAccessToken() const; - - std::unique_ptr<Request> request(ResourceType type, const std::string &url); - - void prepare(std::function<void()> fn); - - // Call this when the network status reachability changed. - void setReachability(bool reachable); - -private: - std::thread::id threadId; - - // Mapbox API access token. - std::string accessToken; - - // Path to the cache database. - std::string path; - - // Stores a URL that is used as a base for loading resources with relative path. - std::string base; - - std::unordered_map<std::string, std::weak_ptr<BaseRequest>> pending; - util::ptr<SQLiteStore> store; - uv_loop_t *loop = nullptr; - uv_messenger_t *queue = nullptr; -}; - -} - -#endif diff --git a/include/mbgl/storage/default/asset_request.hpp b/include/mbgl/storage/default/asset_request.hpp new file mode 100644 index 0000000000..c582c025fb --- /dev/null +++ b/include/mbgl/storage/default/asset_request.hpp @@ -0,0 +1,24 @@ +#ifndef MBGL_STORAGE_DEFAULT_ASSET_REQUEST +#define MBGL_STORAGE_DEFAULT_ASSET_REQUEST + +#include "shared_request_base.hpp" + +namespace mbgl { + +class AssetRequest : public SharedRequestBase { +public: + AssetRequest(DefaultFileSource *source, const Resource &resource); + + void start(uv_loop_t *loop, std::unique_ptr<Response> response = nullptr); + void cancel(); + +private: + ~AssetRequest(); + void *ptr = nullptr; + + friend class AssetRequestImpl; +}; + +} + +#endif diff --git a/include/mbgl/storage/default/default_file_source.hpp b/include/mbgl/storage/default/default_file_source.hpp new file mode 100644 index 0000000000..189ff7b9ee --- /dev/null +++ b/include/mbgl/storage/default/default_file_source.hpp @@ -0,0 +1,60 @@ +#ifndef MBGL_STORAGE_DEFAULT_DEFAULT_FILE_SOURCE +#define MBGL_STORAGE_DEFAULT_DEFAULT_FILE_SOURCE + +#include <mbgl/storage/file_source.hpp> +#include <mbgl/storage/file_cache.hpp> + +#include <set> +#include <unordered_map> +#include <thread> + +namespace mapbox { namespace util { template<typename... Types> class variant; } } + +namespace mbgl { + +namespace util { template <typename T> class AsyncQueue; } + +class SharedRequestBase; + +class DefaultFileSource : public FileSource { +public: + DefaultFileSource(FileCache *cache); + DefaultFileSource(FileCache *cache, uv_loop_t *loop); + ~DefaultFileSource(); + + Request *request(const Resource &resource, uv_loop_t *loop, Callback callback); + void cancel(Request *request); + void request(const Resource &resource, Callback callback); + + enum class CacheHint : uint8_t { Full, Refresh, No }; + void notify(SharedRequestBase *sharedRequest, const std::set<Request *> &observers, + std::shared_ptr<const Response> response, FileCache::Hint hint); + +private: + struct ActionDispatcher; + struct AddRequestAction; + struct RemoveRequestAction; + struct ResultAction; + struct StopAction; + using Action = + mapbox::util::variant<AddRequestAction, RemoveRequestAction, ResultAction, StopAction>; + using Queue = util::AsyncQueue<Action>; + + void process(AddRequestAction &action); + void process(RemoveRequestAction &action); + void process(ResultAction &action); + void process(StopAction &action); + + SharedRequestBase *find(const Resource &resource); + + std::unordered_map<Resource, SharedRequestBase *> pending; + + uv_loop_t *loop = nullptr; + FileCache *cache = nullptr; + Queue *queue = nullptr; + std::thread thread; +}; + +} + +#endif diff --git a/include/mbgl/storage/default/http_context.hpp b/include/mbgl/storage/default/http_context.hpp new file mode 100644 index 0000000000..18f17ef0cb --- /dev/null +++ b/include/mbgl/storage/default/http_context.hpp @@ -0,0 +1,117 @@ +#ifndef MBGL_STORAGE_DEFAULT_HTTP_CONTEXT +#define MBGL_STORAGE_DEFAULT_HTTP_CONTEXT + +#include <mbgl/storage/network_status.hpp> +#include <mbgl/util/noncopyable.hpp> +#include <mbgl/util/std.hpp> +#include <mbgl/util/util.hpp> +#include <mbgl/util/uv.hpp> + +#include <uv.h> +#include <pthread.h> + +#include <map> +#include <cassert> +#include <set> + +namespace mbgl { + +class HTTPRequest; + +// This is a template class that provides a per-thread Context object. It can be used by HTTP +// implementations to store global state. It also implements the NetworkStatus mechanism and +// triggers immediate retries on all requests waiting for network status changes. + +template <typename Context> +class HTTPContext : private util::noncopyable { +protected: + MBGL_STORE_THREAD(tid) + using Map = std::map<uv_loop_t *, std::unique_ptr<Context>>; + +public: + static Context *Get(uv_loop_t *loop); + +private: + static pthread_key_t key; + static pthread_once_t once; + +public: + HTTPContext(uv_loop_t *loop); + ~HTTPContext(); + + void addRequest(HTTPRequest *request); + void removeRequest(HTTPRequest *baton); + +public: + uv_loop_t *loop; + + // Will be fired when the network status becomes reachable. + uv_async_t *reachability = nullptr; + + // A list of all pending HTTPRequestImpls that we need to notify when the network status + // changes. + std::set<HTTPRequest *> requests; +}; + +template <typename Context> +Context *HTTPContext<Context>::Get(uv_loop_t *loop) { + pthread_once(&once, []() { + pthread_key_create(&key, [](void *ptr) { + assert(ptr); + delete reinterpret_cast<Map *>(ptr); + }); + }); + auto contexts = reinterpret_cast<Map *>(pthread_getspecific(key)); + if (!contexts) { + contexts = new Map(); + pthread_setspecific(key, contexts); + } + + // Now find a HTTPContext that matches the requested loop. + auto it = contexts->find(loop); + if (it == contexts->end()) { + auto result = contexts->emplace(loop, util::make_unique<Context>(loop)); + assert(result.second); // Make sure it was actually inserted. + return result.first->second.get(); + } else { + return it->second.get(); + } +} + +template <typename Context> +HTTPContext<Context>::HTTPContext(uv_loop_t *loop_) : loop(loop_) { + reachability = new uv_async_t; + reachability->data = this; + uv_async_init(loop, reachability, [](uv_async_t *async, int) { + for (auto request : reinterpret_cast<Context *>(async->data)->requests) { + request->retryImmediately(); + } + }); + // Allow the loop to quit even though this handle is still active. + uv_unref(reinterpret_cast<uv_handle_t *>(reachability)); + NetworkStatus::Subscribe(reachability); +} + +template <typename Context> +HTTPContext<Context>::~HTTPContext() { + MBGL_VERIFY_THREAD(tid); + + assert(requests.empty()); + + NetworkStatus::Unsubscribe(reachability); + uv::close(reachability); +} + +template <typename Context> +void HTTPContext<Context>::addRequest(HTTPRequest *request) { + requests.insert(request); +} + +template <typename Context> +void HTTPContext<Context>::removeRequest(HTTPRequest *request) { + requests.erase(request); +} + +} + +#endif diff --git a/include/mbgl/storage/default/http_request.hpp b/include/mbgl/storage/default/http_request.hpp new file mode 100644 index 0000000000..914e622f13 --- /dev/null +++ b/include/mbgl/storage/default/http_request.hpp @@ -0,0 +1,26 @@ +#ifndef MBGL_STORAGE_DEFAULT_HTTP_REQUEST +#define MBGL_STORAGE_DEFAULT_HTTP_REQUEST + +#include "shared_request_base.hpp" + +namespace mbgl { + +class HTTPRequest : public SharedRequestBase { +public: + HTTPRequest(DefaultFileSource *source, const Resource &resource); + + void start(uv_loop_t *loop, std::unique_ptr<Response> response = nullptr); + void cancel(); + + void retryImmediately(); + +private: + ~HTTPRequest(); + void *ptr = nullptr; + + friend class HTTPRequestImpl; +}; + +} + +#endif diff --git a/include/mbgl/storage/default/request.hpp b/include/mbgl/storage/default/request.hpp new file mode 100644 index 0000000000..0ed544093c --- /dev/null +++ b/include/mbgl/storage/default/request.hpp @@ -0,0 +1,46 @@ +#ifndef MBGL_STORAGE_DEFAULT_REQUEST +#define MBGL_STORAGE_DEFAULT_REQUEST + +#include <mbgl/storage/resource.hpp> + +#include <mbgl/util/util.hpp> +#include <mbgl/util/noncopyable.hpp> + +#include <functional> + +typedef struct uv_async_s uv_async_t; +typedef struct uv_loop_s uv_loop_t; + +namespace mbgl { + +class Response; + +class Request : private util::noncopyable { + MBGL_STORE_THREAD(tid) + +public: + using Callback = std::function<void(const Response &)>; + Request(const Resource &resource, uv_loop_t *loop, Callback callback); + ~Request(); + +public: + // May be called from any thread. + void notify(const std::shared_ptr<const Response> &response); + void destruct(); + + // May be called only from the thread the Request was created in. + void cancel(); + +private: + uv_async_t *notify_async = nullptr; + uv_async_t *destruct_async = nullptr; + Callback callback; + std::shared_ptr<const Response> response; + +public: + const Resource resource; +}; + +} + +#endif diff --git a/include/mbgl/storage/default/shared_request_base.hpp b/include/mbgl/storage/default/shared_request_base.hpp new file mode 100644 index 0000000000..8591d66ba3 --- /dev/null +++ b/include/mbgl/storage/default/shared_request_base.hpp @@ -0,0 +1,89 @@ +#ifndef MBGL_STORAGE_DEFAULT_SHARED_REQUEST_BASE +#define MBGL_STORAGE_DEFAULT_SHARED_REQUEST_BASE + +#include <mbgl/storage/resource.hpp> +#include <mbgl/storage/file_cache.hpp> +#include <mbgl/util/util.hpp> +#include <mbgl/util/noncopyable.hpp> + +#include "default_file_source.hpp" + +#include <string> +#include <set> +#include <cassert> + +typedef struct uv_loop_s uv_loop_t; + +namespace mbgl { + +class Request; +class Response; +class DefaultFileSource; + +class SharedRequestBase : private util::noncopyable { +protected: + MBGL_STORE_THREAD(tid) + +public: + SharedRequestBase(DefaultFileSource *source_, const Resource &resource_) + : resource(resource_), source(source_) {} + + virtual void start(uv_loop_t *loop, std::unique_ptr<Response> response = nullptr) = 0; + virtual void cancel() = 0; + + void notify(std::unique_ptr<Response> response, FileCache::Hint hint) { + MBGL_VERIFY_THREAD(tid); + + if (source) { + source->notify(this, observers, std::shared_ptr<const Response>(response.release()), + hint); + } + } + + void subscribe(Request *request) { + MBGL_VERIFY_THREAD(tid); + + observers.insert(request); + } + + void unsubscribeAll() { + MBGL_VERIFY_THREAD(tid); + + source = nullptr; + observers.clear(); + cancel(); + } + + void unsubscribe(Request *request) { + MBGL_VERIFY_THREAD(tid); + + observers.erase(request); + + if (observers.empty()) { + // There are no observers anymore. We are initiating cancelation. + if (source) { + // First, remove this SharedRequestBase from the source. + source->notify(this, observers, nullptr, FileCache::Hint::No); + } + + // Then, initiate cancelation of this request + cancel(); + } + } + +protected: + virtual ~SharedRequestBase() { + MBGL_VERIFY_THREAD(tid); + } + +public: + const Resource resource; + +private: + DefaultFileSource *source = nullptr; + std::set<Request *> observers; +}; + +} + +#endif diff --git a/include/mbgl/storage/default/sqlite_cache.hpp b/include/mbgl/storage/default/sqlite_cache.hpp new file mode 100644 index 0000000000..8f2746561c --- /dev/null +++ b/include/mbgl/storage/default/sqlite_cache.hpp @@ -0,0 +1,52 @@ +#ifndef MBGL_STORAGE_DEFAULT_SQLITE_CACHE +#define MBGL_STORAGE_DEFAULT_SQLITE_CACHE + +#include <mbgl/storage/file_cache.hpp> + +#include <string> +#include <thread> + +typedef struct uv_loop_s uv_loop_t; + +namespace mapbox { namespace util { template<typename... Types> class variant; } } +namespace mapbox { namespace sqlite { class Database; class Statement; } } + +namespace mbgl { + +namespace util { template <typename T> class AsyncQueue; } + +class SQLiteCache : public FileCache { + struct GetAction; + struct PutAction; + struct RefreshAction; + struct StopAction; + using Action = mapbox::util::variant<GetAction, PutAction, RefreshAction, StopAction>; + using Queue = util::AsyncQueue<Action>; + +public: + SQLiteCache(const std::string &path = ":memory:"); + ~SQLiteCache(); + + void get(const Resource &resource, std::function<void(std::unique_ptr<Response>)> callback); + void put(const Resource &resource, std::shared_ptr<const Response> response, Hint hint); + +private: + struct ActionDispatcher; + void process(GetAction &action); + void process(PutAction &action); + void process(RefreshAction &action); + void process(StopAction &action); + + void createDatabase(); + + const std::string path; + uv_loop_t *loop = nullptr; + Queue *queue = nullptr; + std::thread thread; + std::unique_ptr<::mapbox::sqlite::Database> db; + std::unique_ptr<::mapbox::sqlite::Statement> getStmt, putStmt, refreshStmt; +}; + +} + +#endif diff --git a/include/mbgl/storage/file_cache.hpp b/include/mbgl/storage/file_cache.hpp new file mode 100644 index 0000000000..38a82cc237 --- /dev/null +++ b/include/mbgl/storage/file_cache.hpp @@ -0,0 +1,26 @@ +#ifndef MBGL_STORAGE_FILE_CACHE +#define MBGL_STORAGE_FILE_CACHE + +#include <mbgl/util/noncopyable.hpp> + +#include <functional> + +namespace mbgl { + +struct Resource; +class Response; + +class FileCache : private util::noncopyable { +public: + virtual ~FileCache() = default; + + enum class Hint : uint8_t { Full, Refresh, No }; + + virtual void get(const Resource &resource, + std::function<void(std::unique_ptr<Response>)> callback) = 0; + virtual void put(const Resource &resource, std::shared_ptr<const Response> response, Hint hint) = 0; +}; + +} + +#endif diff --git a/include/mbgl/storage/file_source.hpp b/include/mbgl/storage/file_source.hpp index 23a1462ae8..8517d6e4a6 100644 --- a/include/mbgl/storage/file_source.hpp +++ b/include/mbgl/storage/file_source.hpp @@ -1,29 +1,38 @@ #ifndef MBGL_STORAGE_FILE_SOURCE #define MBGL_STORAGE_FILE_SOURCE +#include "response.hpp" +#include "resource.hpp" + #include <mbgl/util/noncopyable.hpp> -#include <mbgl/storage/resource_type.hpp> -#include <mbgl/storage/request.hpp> +#include <mbgl/util/std.hpp> +#include <mbgl/util/util.hpp> -#include <string> #include <functional> typedef struct uv_loop_s uv_loop_t; - namespace mbgl { -class FileSource : public util::noncopyable { +class Request; + +class FileSource : private util::noncopyable { +protected: + MBGL_STORE_THREAD(tid) + public: virtual ~FileSource() = default; - virtual void setLoop(uv_loop_t*) = 0; - virtual bool hasLoop() = 0; - virtual void clearLoop() = 0; + using Callback = std::function<void(const Response &)>; + + // These can be called from any thread. The callback will be invoked in the loop. + // You can only cancel a request from the same thread it was created in. + virtual Request *request(const Resource &resource, uv_loop_t *loop, Callback callback) = 0; + virtual void cancel(Request *request) = 0; - virtual void setBase(std::string) = 0; - virtual std::unique_ptr<Request> request(ResourceType type, const std::string &url) = 0; - virtual void prepare(std::function<void()> fn) = 0; + // These can be called from any thread. The callback will be invoked in an arbitrary other thread. + // You cannot cancel these requests. + virtual void request(const Resource &resource, Callback callback) = 0; }; } diff --git a/include/mbgl/storage/http_request_baton.hpp b/include/mbgl/storage/http_request_baton.hpp deleted file mode 100644 index 11abfb71d4..0000000000 --- a/include/mbgl/storage/http_request_baton.hpp +++ /dev/null @@ -1,74 +0,0 @@ -#ifndef MBGL_STORAGE_HTTP_REQUEST_BATON -#define MBGL_STORAGE_HTTP_REQUEST_BATON - -#include <mbgl/storage/response.hpp> -#include <mbgl/util/ptr.hpp> - -#include <string> -#include <thread> - -typedef struct uv_async_s uv_async_t; - -namespace mbgl { - -class HTTPRequest; - -enum class HTTPResponseType : int8_t { - // This error probably won't be resolved by retrying anytime soon. We are giving up. - PermanentError = -5, - - // This error might be resolved by waiting some time (e.g. server issues). - // We are going to do an exponential back-off and will try again in a few seconds. - TemporaryError = -4, - - // This error was caused by a temporary error and it is likely that it will be resolved - // immediately. We are going to try again right away. This is like the TemporaryError, except - // that we will not perform exponential back-off. - SingularError = -3, - - // This error might be resolved once the network reachability status changes. - // We are going to watch the network status for changes and will retry as soon as the operating - // system notifies us of a network status change. - ConnectionError = -2, - - // The request was canceled programatically. - Canceled = -1, - - // The request is still in progress. - Unknown = 0, - - // The request returned data successfully. We retrieved and decoded the data successfully. - Successful = 1, - - // The request confirmed that the data wasn't changed. We already have the data. - NotModified = 2, -}; - -struct HTTPRequestBaton { - HTTPRequestBaton(const std::string &path); - ~HTTPRequestBaton(); - - const std::thread::id threadId; - const std::string path; - - HTTPRequest *request = nullptr; - uv_async_t *async = nullptr; - - HTTPResponseType type = HTTPResponseType::Unknown; - std::unique_ptr<Response> response; - - // Implementation specific use. - void *ptr = nullptr; - - // IMPLEMENT THESE 3 PLATFORM SPECIFIC FUNCTIONS: - - // Begin the HTTP request. Platform-specific implementation. - static void start(const util::ptr<HTTPRequestBaton> &ptr); - - // This will be called to stop/cancel the HTTP request (if possible). Platform-specific implementation. - static void stop(const util::ptr<HTTPRequestBaton> &ptr); -}; - -} - -#endif diff --git a/include/mbgl/storage/network_status.hpp b/include/mbgl/storage/network_status.hpp new file mode 100644 index 0000000000..cac2ae193b --- /dev/null +++ b/include/mbgl/storage/network_status.hpp @@ -0,0 +1,25 @@ +#ifndef MBGL_STORAGE_NETWORK_STATUS +#define MBGL_STORAGE_NETWORK_STATUS + +#include <mutex> +#include <set> + +typedef struct uv_async_s uv_async_t; + +namespace mbgl { + +class NetworkStatus { +public: + static void Reachable(); + + static void Subscribe(uv_async_t *async); + static void Unsubscribe(uv_async_t *async); + +private: + static std::mutex mtx; + static std::set<uv_async_t *> observers; +}; + +} + +#endif
\ No newline at end of file diff --git a/include/mbgl/storage/request.hpp b/include/mbgl/storage/request.hpp deleted file mode 100644 index 845c9a6dad..0000000000 --- a/include/mbgl/storage/request.hpp +++ /dev/null @@ -1,41 +0,0 @@ -#ifndef MBGL_STORAGE_REQUEST -#define MBGL_STORAGE_REQUEST - -#include <mbgl/storage/request_callback.hpp> -#include <mbgl/storage/response.hpp> - -#include <mbgl/util/ptr.hpp> - -#include <thread> -#include <forward_list> - -typedef struct uv_loop_s uv_loop_t; - -namespace mbgl { - -class BaseRequest; - -class Request { -private: - Request(const Request &) = delete; - Request(Request &&) = delete; - Request& operator=(const Request &) = delete; - Request& operator=(Request &&) = delete; - -public: - Request(const util::ptr<BaseRequest> &base); - ~Request(); - - void onload(CompletedCallback cb); - void oncancel(AbortedCallback cb); - void cancel(); - -private: - const std::thread::id thread_id; - util::ptr<BaseRequest> base; - std::forward_list<Callback *> callbacks; -}; - -} - -#endif
\ No newline at end of file diff --git a/include/mbgl/storage/request_callback.hpp b/include/mbgl/storage/request_callback.hpp deleted file mode 100644 index 01427bd96d..0000000000 --- a/include/mbgl/storage/request_callback.hpp +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef MBGL_STORAGE_REQUEST_CALLBACK -#define MBGL_STORAGE_REQUEST_CALLBACK - -#include <mbgl/util/variant.hpp> - -#include <functional> - -namespace mbgl { - -class Response; - -using CompletedCallback = std::function<void(const Response &)>; -using AbortedCallback = std::function<void()>; - -using Callback = mapbox::util::variant< - CompletedCallback, - AbortedCallback ->; - -} - -#endif diff --git a/include/mbgl/storage/resource.hpp b/include/mbgl/storage/resource.hpp new file mode 100644 index 0000000000..e499f84a28 --- /dev/null +++ b/include/mbgl/storage/resource.hpp @@ -0,0 +1,39 @@ +#ifndef MBGL_STORAGE_RESOURCE +#define MBGL_STORAGE_RESOURCE + +#include <string> +#include <functional> + +namespace mbgl { + +struct Resource { + enum Kind : uint8_t { + Unknown = 0, + Tile = 1, + Glyphs = 2, + Image = 3, + JSON = 4, + }; + + const Kind kind; + const std::string url; + + inline bool operator==(const Resource &res) const { + return kind == res.kind && url == res.url; + } +}; + +} + +// Specialize std::hash for use in std::unordered_map +namespace std { + +template<> struct hash<mbgl::Resource> { + std::size_t operator()(mbgl::Resource const& r) const { + return std::hash<std::string>()(r.url) ^ (std::hash<uint8_t>()(r.kind) << 1); + } +}; + +} + +#endif diff --git a/include/mbgl/storage/resource_type.hpp b/include/mbgl/storage/resource_type.hpp deleted file mode 100644 index b7204a9fa1..0000000000 --- a/include/mbgl/storage/resource_type.hpp +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef MBGL_STORAGE_RESOURCE_TYPE -#define MBGL_STORAGE_RESOURCE_TYPE - -#include <cstdint> - -namespace mbgl { - -enum class ResourceType : uint8_t { - Unknown = 0, - Tile = 1, - Glyphs = 2, - Image = 3, - JSON = 4 -}; - -} - -#endif diff --git a/include/mbgl/storage/response.hpp b/include/mbgl/storage/response.hpp index 9357ad3c63..cf22d9002b 100644 --- a/include/mbgl/storage/response.hpp +++ b/include/mbgl/storage/response.hpp @@ -2,25 +2,21 @@ #define MBGL_STORAGE_RESPONSE #include <string> -#include <ctime> namespace mbgl { - - class Response { public: - long code = 0; + enum Status : bool { Error, Successful }; + + Status status = Error; + std::string message; int64_t modified = 0; int64_t expires = 0; std::string etag; std::string data; - - std::string message; - - static int64_t parseCacheControl(const char *value); }; } -#endif
\ No newline at end of file +#endif diff --git a/include/mbgl/util/async_queue.hpp b/include/mbgl/util/async_queue.hpp new file mode 100644 index 0000000000..b3eaabc319 --- /dev/null +++ b/include/mbgl/util/async_queue.hpp @@ -0,0 +1,95 @@ +#ifndef MBGL_UTIL_ASYNC_QUEUE +#define MBGL_UTIL_ASYNC_QUEUE + +#include "std.hpp" + +#include <uv.h> + +#include <thread> +#include <mutex> +#include <functional> +#include <queue> +#include <string> + + +#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10 +#define UV_ASYNC_PARAMS(handle) uv_async_t *handle, int +#else +#define UV_ASYNC_PARAMS(handle) uv_async_t *handle +#endif + +namespace mbgl { +namespace util { + +template <typename T> +class AsyncQueue { +public: + AsyncQueue(uv_loop_t *loop, std::function<void(T &)> fn) : + callback(fn) { + async.data = this; + uv_async_init(loop, &async, [](UV_ASYNC_PARAMS(handle)) { + auto q = reinterpret_cast<AsyncQueue *>(handle->data); + q->process(); + }); + } + + void send(T &&data) { + { + std::lock_guard<std::mutex> lock(mutex); + queue.push(util::make_unique<T>(std::move(data))); + } + uv_async_send(&async); + } + + void send(std::unique_ptr<T> data) { + { + std::lock_guard<std::mutex> lock(mutex); + queue.push(std::move(data)); + } + uv_async_send(&async); + } + + void stop() { + uv_close((uv_handle_t *)&async, [](uv_handle_t *handle) { + delete reinterpret_cast<AsyncQueue *>(handle->data); + }); + } + + void ref() { + uv_ref((uv_handle_t *)&async); + } + + void unref() { + uv_unref((uv_handle_t *)&async); + } + +private: + ~AsyncQueue() { + } + + void process() { + std::unique_ptr<T> item; + while (true) { + mutex.lock(); + if (queue.empty()) { + mutex.unlock(); + break; + } + item = std::move(queue.front()); + queue.pop(); + mutex.unlock(); + callback(*item); + } + } + +private: + std::mutex mutex; + uv_async_t async; + std::queue<std::unique_ptr<T>> queue; + std::function<void(T &)> callback; +}; + +} +} + +#endif diff --git a/include/mbgl/util/util.hpp b/include/mbgl/util/util.hpp new file mode 100644 index 0000000000..bf5dad3c01 --- /dev/null +++ b/include/mbgl/util/util.hpp @@ -0,0 +1,15 @@ +#ifndef MBGL_UTIL_UTIL +#define MBGL_UTIL_UTIL + +#include <thread> + +#ifndef NDEBUG +#include <thread> +#define MBGL_STORE_THREAD(tid) const std::thread::id tid = std::this_thread::get_id(); +#define MBGL_VERIFY_THREAD(tid) assert(tid == std::this_thread::get_id()); +#else +#define MBGL_STORE_THREAD(tid) +#define MBGL_VERIFY_THREAD(tid) +#endif + +#endif diff --git a/include/mbgl/util/uv.hpp b/include/mbgl/util/uv.hpp index f59037c1d8..85f93e78bd 100644 --- a/include/mbgl/util/uv.hpp +++ b/include/mbgl/util/uv.hpp @@ -3,10 +3,11 @@ #include <string> +typedef struct uv_handle_s uv_handle_t; typedef struct uv_async_s uv_async_t; typedef struct uv_timer_s uv_timer_t; -typedef struct uv_handle_s uv_handle_t; typedef struct uv_loop_s uv_loop_t; +typedef struct uv_fs_s uv_fs_t; namespace uv { @@ -19,6 +20,15 @@ class worker; class mutex; class cond; +const char *getFileRequestError(uv_fs_t *req); + +template <typename T> +void close(T *specific) { + uv_close(reinterpret_cast<uv_handle_t *>(specific), [](uv_handle_t *generic) { + delete reinterpret_cast<T *>(generic); + }); +} + } #endif diff --git a/include/mbgl/util/variant.hpp b/include/mbgl/util/variant.hpp index 2de195cd69..411f1918d5 100644 --- a/include/mbgl/util/variant.hpp +++ b/include/mbgl/util/variant.hpp @@ -10,7 +10,7 @@ #include <iosfwd> #include <string> -#include <mbgl/util/recursive_wrapper.hpp> +#include "recursive_wrapper.hpp" #ifdef _MSC_VER // http://msdn.microsoft.com/en-us/library/z8y1yy88.aspx @@ -34,7 +34,19 @@ // translates to 100 #define VARIANT_VERSION (VARIANT_MAJOR_VERSION*100000) + (VARIANT_MINOR_VERSION*100) + (VARIANT_PATCH_VERSION) -namespace mapbox { namespace util { namespace detail { +namespace mapbox { namespace util { + +// static visitor +template <typename R = void> +struct static_visitor +{ + using result_type = R; +protected: + static_visitor() {} + ~static_visitor() {} +}; + +namespace detail { static constexpr std::size_t invalid_value = std::size_t(-1); @@ -109,18 +121,38 @@ struct select_type<0, T, Types...> using type = T; }; -} // namespace detail -// static visitor -template <typename R = void> -struct static_visitor +template <typename T, typename R = void> +struct enable_if_type { using type = R; }; + +template <typename F, typename V, typename Enable = void> +struct result_of_unary_visit { - using result_type = R; -protected: - static_visitor() {} - ~static_visitor() {} + using type = typename std::result_of<F(V&)>::type; }; +template <typename F, typename V> +struct result_of_unary_visit<F, V, typename enable_if_type<typename F::result_type>::type > +{ + using type = typename F::result_type; +}; + +template <typename F, typename V, class Enable = void> +struct result_of_binary_visit +{ + using type = typename std::result_of<F(V&,V&)>::type; +}; + + +template <typename F, typename V> +struct result_of_binary_visit<F, V, typename enable_if_type<typename F::result_type>::type > +{ + using type = typename F::result_type; +}; + + +} // namespace detail + template <std::size_t arg1, std::size_t ... others> struct static_max; @@ -225,7 +257,7 @@ struct dispatcher; template <typename F, typename V, typename T, typename...Types> struct dispatcher<F, V, T, Types...> { - using result_type = typename F::result_type; + using result_type = typename detail::result_of_unary_visit<F, V>::type; VARIANT_INLINE static result_type apply_const(V const& v, F f) { if (v.get_type_index() == sizeof...(Types)) @@ -254,7 +286,7 @@ struct dispatcher<F, V, T, Types...> template<typename F, typename V> struct dispatcher<F, V> { - using result_type = typename F::result_type; + using result_type = typename detail::result_of_unary_visit<F, V>::type; VARIANT_INLINE static result_type apply_const(V const&, F) { throw std::runtime_error(std::string("unary dispatch: FAIL ") + typeid(V).name()); @@ -273,7 +305,7 @@ struct binary_dispatcher_rhs; template <typename F, typename V, typename T0, typename T1, typename...Types> struct binary_dispatcher_rhs<F, V, T0, T1, Types...> { - using result_type = typename F::result_type; + using result_type = typename detail::result_of_binary_visit<F, V>::type; VARIANT_INLINE static result_type apply_const(V const& lhs, V const& rhs, F f) { if (rhs.get_type_index() == sizeof...(Types)) // call binary functor @@ -305,7 +337,7 @@ struct binary_dispatcher_rhs<F, V, T0, T1, Types...> template<typename F, typename V, typename T> struct binary_dispatcher_rhs<F, V, T> { - using result_type = typename F::result_type; + using result_type = typename detail::result_of_binary_visit<F, V>::type; VARIANT_INLINE static result_type apply_const(V const&, V const&, F) { throw std::runtime_error("binary dispatch: FAIL"); @@ -323,7 +355,7 @@ struct binary_dispatcher_lhs; template <typename F, typename V, typename T0, typename T1, typename...Types> struct binary_dispatcher_lhs<F, V, T0, T1, Types...> { - using result_type = typename F::result_type; + using result_type = typename detail::result_of_binary_visit<F, V>::type; VARIANT_INLINE static result_type apply_const(V const& lhs, V const& rhs, F f) { if (lhs.get_type_index() == sizeof...(Types)) // call binary functor @@ -353,7 +385,7 @@ struct binary_dispatcher_lhs<F, V, T0, T1, Types...> template<typename F, typename V, typename T> struct binary_dispatcher_lhs<F, V, T> { - using result_type = typename F::result_type; + using result_type = typename detail::result_of_binary_visit<F, V>::type; VARIANT_INLINE static result_type apply_const(V const&, V const&, F) { throw std::runtime_error("binary dispatch: FAIL"); @@ -371,7 +403,7 @@ struct binary_dispatcher; template <typename F, typename V, typename T, typename...Types> struct binary_dispatcher<F, V, T, Types...> { - using result_type = typename F::result_type; + using result_type = typename detail::result_of_binary_visit<F, V>::type; VARIANT_INLINE static result_type apply_const(V const& v0, V const& v1, F f) { if (v0.get_type_index() == sizeof...(Types)) @@ -416,7 +448,7 @@ struct binary_dispatcher<F, V, T, Types...> template<typename F, typename V> struct binary_dispatcher<F, V> { - using result_type = typename F::result_type; + using result_type = typename detail::result_of_binary_visit<F, V>::type; VARIANT_INLINE static result_type apply_const(V const&, V const&, F) { throw std::runtime_error("binary dispatch: FAIL"); @@ -448,7 +480,7 @@ struct less_comp }; template <typename Variant, typename Comp> -class comparer : public static_visitor<bool> +class comparer { public: explicit comparer(Variant const& lhs) noexcept @@ -467,7 +499,7 @@ private: // operator<< helper template <typename Out> -class printer : public static_visitor<> +class printer { public: explicit printer(Out & out) diff --git a/ios/mapbox-gl-cocoa b/ios/mapbox-gl-cocoa -Subproject 905bca9394a6e4d6a89dcf0e10b4e25105fbe64 +Subproject 37c9f726752674f87758cd351316413f978f160 diff --git a/macosx/main.mm b/macosx/main.mm index 277f8c44d2..6956f04d6d 100644 --- a/macosx/main.mm +++ b/macosx/main.mm @@ -3,7 +3,9 @@ #include <mbgl/platform/darwin/log_nslog.hpp> #include <mbgl/platform/darwin/Reachability.h> #include <mbgl/platform/default/glfw_view.hpp> -#include <mbgl/storage/caching_http_file_source.hpp> +#include <mbgl/storage/default/default_file_source.hpp> +#include <mbgl/storage/default/sqlite_cache.hpp> +#include <mbgl/storage/network_status.hpp> #import <Foundation/Foundation.h> @@ -74,9 +76,10 @@ int main() { mbgl::Log::Set<mbgl::NSLogBackend>(); GLFWView view; - mbgl::CachingHTTPFileSource fileSource(mbgl::platform::defaultCacheDatabase()); + + mbgl::SQLiteCache cache(mbgl::platform::defaultCacheDatabase()); + mbgl::DefaultFileSource fileSource(&cache); mbgl::Map map(view, fileSource); - mbgl::CachingHTTPFileSource *fileSourcePtr = &fileSource; URLHandler *handler = [[URLHandler alloc] init]; [handler setMap:&map]; @@ -86,7 +89,7 @@ int main() { // Notify map object when network reachability status changes. Reachability* reachability = [Reachability reachabilityForInternetConnection]; reachability.reachableBlock = ^(Reachability *) { - fileSourcePtr->setReachability(true); + mbgl::NetworkStatus::Reachable(); }; [reachability startNotifier]; @@ -99,7 +102,7 @@ int main() { // Set access token if present NSString *accessToken = [[NSProcessInfo processInfo] environment][@"MAPBOX_ACCESS_TOKEN"]; if (!accessToken) mbgl::Log::Warning(mbgl::Event::Setup, "No access token set. Mapbox vector tiles won't work."); - if (accessToken) fileSource.setAccessToken([accessToken cStringUsingEncoding:[NSString defaultCStringEncoding]]); + if (accessToken) map.setAccessToken([accessToken cStringUsingEncoding:[NSString defaultCStringEncoding]]); // Load style map.setStyleURL("asset://styles/bright-v7.json"); diff --git a/platform/darwin/http_request_baton_cocoa.mm b/platform/darwin/http_request_baton_cocoa.mm deleted file mode 100644 index 4a59837e32..0000000000 --- a/platform/darwin/http_request_baton_cocoa.mm +++ /dev/null @@ -1,155 +0,0 @@ -#include <mbgl/storage/http_request_baton.hpp> -#include <mbgl/util/std.hpp> -#include <mbgl/util/parsedate.h> -#include <mbgl/util/time.hpp> -#include <mbgl/util/version.hpp> - -#include <uv.h> - -#include <mbgl/util/uv.hpp> - -#import <Foundation/Foundation.h> - -namespace mbgl { - -dispatch_once_t request_initialize = 0; -NSURLSession *session = nullptr; - -NSString *userAgent = nil; - -void HTTPRequestBaton::start(const util::ptr<HTTPRequestBaton> &ptr) { - assert(std::this_thread::get_id() == ptr->threadId); - - // Starts the request. - util::ptr<HTTPRequestBaton> baton = ptr; - - dispatch_once(&request_initialize, ^{ - NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration]; - sessionConfig.timeoutIntervalForResource = 30; - sessionConfig.HTTPMaximumConnectionsPerHost = 8; - sessionConfig.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData; - sessionConfig.URLCache = nil; - - session = [NSURLSession sessionWithConfiguration:sessionConfig]; - - // Write user agent string - NSDictionary *systemVersion = [NSDictionary dictionaryWithContentsOfFile:@"/System/Library/CoreServices/SystemVersion.plist"]; - userAgent = [NSString stringWithFormat:@"MapboxGL/%d.%d.%d (+https://mapbox.com/mapbox-gl/; %s; %@ %@)", - version::major, version::minor, version::patch, version::revision, - [systemVersion objectForKey:@"ProductName"], - [systemVersion objectForKey:@"ProductVersion"] - ]; - }); - - NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@(baton->path.c_str())]]; - if (baton->response) { - if (!baton->response->etag.empty()) { - [request addValue:@(baton->response->etag.c_str()) forHTTPHeaderField:@"If-None-Match"]; - } else if (baton->response->modified) { - const std::string time = util::rfc1123(baton->response->modified); - [request addValue:@(time.c_str()) forHTTPHeaderField:@"If-Modified-Since"]; - } - } - - [request addValue:userAgent forHTTPHeaderField:@"User-Agent"]; - - NSURLSessionDataTask *task = [session dataTaskWithRequest:request - completionHandler:^(NSData *data, NSURLResponse *res, NSError *error) { - if (error) { - if ([error code] == NSURLErrorCancelled) { - // The response code remains at 0 to indicate cancelation. - // In addition, we don't need any response object. - baton->response.reset(); - baton->type = HTTPResponseType::Canceled; - } else { - // TODO: Use different codes for host not found, timeout, invalid URL etc. - // These can be categorized in temporary and permanent errors. - baton->response = util::make_unique<Response>(); - baton->response->code = [(NSHTTPURLResponse *)res statusCode]; - baton->response->message = [[error localizedDescription] UTF8String]; - - switch ([error code]) { - case NSURLErrorBadServerResponse: // 5xx errors - baton->type = HTTPResponseType::TemporaryError; - break; - - case NSURLErrorTimedOut: - case NSURLErrorUserCancelledAuthentication: - baton->type = HTTPResponseType::SingularError; // retry immediately - break; - - case NSURLErrorNetworkConnectionLost: - case NSURLErrorCannotFindHost: - case NSURLErrorCannotConnectToHost: - case NSURLErrorDNSLookupFailed: - case NSURLErrorNotConnectedToInternet: - case NSURLErrorInternationalRoamingOff: - case NSURLErrorCallIsActive: - case NSURLErrorDataNotAllowed: - baton->type = HTTPResponseType::ConnectionError; - break; - - default: - baton->type = HTTPResponseType::PermanentError; - } - } - } else if ([res isKindOfClass:[NSHTTPURLResponse class]]) { - const long code = [(NSHTTPURLResponse *)res statusCode]; - if (code == 304) { - // Assume a Response object already exists. - assert(baton->response); - } else { - baton->response = util::make_unique<Response>(); - baton->response->code = code; - baton->response->data = {(const char *)[data bytes], [data length]}; - } - - if (code == 304) { - baton->type = HTTPResponseType::NotModified; - } else if (code == 200) { - baton->type = HTTPResponseType::Successful; - } else { - baton->type = HTTPResponseType::PermanentError; - } - - NSDictionary *headers = [(NSHTTPURLResponse *)res allHeaderFields]; - NSString *cache_control = [headers objectForKey:@"Cache-Control"]; - if (cache_control) { - baton->response->expires = Response::parseCacheControl([cache_control UTF8String]); - } - - NSString *last_modified = [headers objectForKey:@"Last-Modified"]; - if (last_modified) { - baton->response->modified = parse_date([last_modified UTF8String]); - } - - NSString *etag = [headers objectForKey:@"ETag"]; - if (etag) { - baton->response->etag = [etag UTF8String]; - } - } else { - // This should never happen. - baton->type = HTTPResponseType::PermanentError; - baton->response = util::make_unique<Response>(); - baton->response->code = -1; - baton->response->message = "response class is not NSHTTPURLResponse"; - } - - uv_async_send(baton->async); - }]; - - [task resume]; - - baton->ptr = const_cast<void *>(CFBridgingRetain(task)); -} - -void HTTPRequestBaton::stop(const util::ptr<HTTPRequestBaton> &ptr) { - assert(std::this_thread::get_id() == ptr->threadId); - assert(ptr->ptr); - - NSURLSessionDataTask *task = CFBridgingRelease(ptr->ptr); - ptr->ptr = nullptr; - [task cancel]; -} - -} diff --git a/platform/darwin/http_request_cocoa.mm b/platform/darwin/http_request_cocoa.mm new file mode 100644 index 0000000000..163d4cf2db --- /dev/null +++ b/platform/darwin/http_request_cocoa.mm @@ -0,0 +1,388 @@ +#include <mbgl/storage/default/http_request.hpp> +#include <mbgl/storage/default/http_context.hpp> +#include <mbgl/storage/response.hpp> +#include <mbgl/util/uv.hpp> + +#include <mbgl/util/time.hpp> +#include <mbgl/util/parsedate.h> + +#import <Foundation/Foundation.h> + +#include <map> +#include <cassert> + +dispatch_once_t request_initialize = 0; +NSURLSession *session = nullptr; +NSString *userAgent = nil; + +namespace mbgl { + +enum class ResponseStatus : uint8_t { + // This error probably won't be resolved by retrying anytime soon. We are giving up. + PermanentError, + + // This error might be resolved by waiting some time (e.g. server issues). + // We are going to do an exponential back-off and will try again in a few seconds. + TemporaryError, + + // This error was caused by a temporary error and it is likely that it will be resolved + // immediately. We are going to try again right away. This is like the TemporaryError, except + // that we will not perform exponential back-off. + SingularError, + + // This error might be resolved once the network reachability status changes. + // We are going to watch the network status for changes and will retry as soon as the + // operating system notifies us of a network status change. + ConnectionError, + + // The request was canceled mid-way. + Canceled, + + // The request returned data successfully. We retrieved and decoded the data successfully. + Successful, + + // The request confirmed that the data wasn't changed. We already have the data. + NotModified, +}; + +// ------------------------------------------------------------------------------------------------- + +class HTTPCocoaContext; + +class HTTPRequestImpl { +public: + HTTPRequestImpl(HTTPRequest *request, uv_loop_t *loop, std::unique_ptr<Response> response); + ~HTTPRequestImpl(); + + void cancel(); + + void start(); + void handleResult(NSData *data, NSURLResponse *res, NSError *error); + void handleResponse(); + + void retry(uint64_t timeout); + void retryImmediately(); + static void restart(uv_timer_t *timer, int); + +private: + HTTPCocoaContext *context = nullptr; + HTTPRequest *request = nullptr; + NSURLSessionDataTask *task = nullptr; + std::unique_ptr<Response> response; + std::unique_ptr<Response> existingResponse; + ResponseStatus status = ResponseStatus::PermanentError; + uv_async_t *async = nullptr; + int attempts = 0; + uv_timer_t *timer = nullptr; + enum : bool { PreemptImmediately, ExponentialBackoff } strategy = PreemptImmediately; + + static const int maxAttempts = 4; +}; + +// ------------------------------------------------------------------------------------------------- + +class HTTPCocoaContext : public HTTPContext<HTTPCocoaContext> { +public: + HTTPCocoaContext(uv_loop_t *loop); +}; + +template<> pthread_key_t HTTPContext<HTTPCocoaContext>::key{}; +template<> pthread_once_t HTTPContext<HTTPCocoaContext>::once = PTHREAD_ONCE_INIT; + +HTTPCocoaContext::HTTPCocoaContext(uv_loop_t *loop_) : HTTPContext(loop_) {} + +// ------------------------------------------------------------------------------------------------- + +HTTPRequestImpl::HTTPRequestImpl(HTTPRequest *request_, uv_loop_t *loop, + std::unique_ptr<Response> existingResponse_) + : context(HTTPCocoaContext::Get(loop)), + request(request_), + existingResponse(std::move(existingResponse_)), + async(new uv_async_t) { + assert(request); + context->addRequest(request); + + async->data = this; + uv_async_init(loop, async, [](uv_async_t *as, int) { + auto impl = reinterpret_cast<HTTPRequestImpl *>(as->data); + impl->handleResponse(); + }); + + start(); +} + +void HTTPRequestImpl::start() { + assert(!task); + + attempts++; + + @autoreleasepool { + NSMutableURLRequest *req = [[NSMutableURLRequest alloc] + initWithURL:[NSURL URLWithString:@(request->resource.url.c_str())]]; + if (existingResponse) { + if (!existingResponse->etag.empty()) { + [req addValue:@(existingResponse->etag.c_str()) forHTTPHeaderField:@"If-None-Match"]; + } else if (existingResponse->modified) { + const std::string time = util::rfc1123(existingResponse->modified); + [req addValue:@(time.c_str()) forHTTPHeaderField:@"If-Modified-Since"]; + } + } + + [req addValue:userAgent forHTTPHeaderField:@"User-Agent"]; + + task = [session dataTaskWithRequest:req + completionHandler:^(NSData *data, NSURLResponse *res, + NSError *error) { handleResult(data, res, error); }]; + [req release]; + [task resume]; + } +} + +void HTTPRequestImpl::handleResponse() { + task = nullptr; + + if (request) { + if (status == ResponseStatus::TemporaryError && attempts < maxAttempts) { + strategy = ExponentialBackoff; + return retry((1 << (attempts - 1)) * 1000); + } else if (status == ResponseStatus::ConnectionError && attempts < maxAttempts) { + // By default, we will retry every 30 seconds (network change notification will + // preempt the timeout). + strategy = PreemptImmediately; + return retry(30000); + } + + // Actually return the response. + if (status == ResponseStatus::NotModified) { + request->notify(std::move(response), FileCache::Hint::Refresh); + } else { + request->notify(std::move(response), FileCache::Hint::Full); + } + + context->removeRequest(request); + delete request; + request = nullptr; + } + + delete this; +} + +void HTTPRequestImpl::cancel() { + context->removeRequest(request); + request = nullptr; + + [task cancel]; + task = nullptr; +} + +HTTPRequestImpl::~HTTPRequestImpl() { + assert(!task); + assert(async); + + uv::close(async); + + if (request) { + context->removeRequest(request); + request->ptr = nullptr; + } +} + +int64_t 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) { + return std::chrono::duration_cast<std::chrono::seconds>( + std::chrono::system_clock::now().time_since_epoch()).count() + + seconds; + } + } + + return 0; +} + +void HTTPRequestImpl::handleResult(NSData *data, NSURLResponse *res, NSError *error) { + if (error) { + if ([error code] == NSURLErrorCancelled) { + status = ResponseStatus::Canceled; + } else { + // TODO: Use different codes for host not found, timeout, invalid URL etc. + // These can be categorized in temporary and permanent errors. + response = util::make_unique<Response>(); + response->status = Response::Error; + response->message = [[error localizedDescription] UTF8String]; + + switch ([error code]) { + case NSURLErrorBadServerResponse: // 5xx errors + status = ResponseStatus::TemporaryError; + break; + + case NSURLErrorTimedOut: + case NSURLErrorUserCancelledAuthentication: + status = ResponseStatus::SingularError; // retry immediately + break; + + case NSURLErrorNetworkConnectionLost: + case NSURLErrorCannotFindHost: + case NSURLErrorCannotConnectToHost: + case NSURLErrorDNSLookupFailed: + case NSURLErrorNotConnectedToInternet: + case NSURLErrorInternationalRoamingOff: + case NSURLErrorCallIsActive: + case NSURLErrorDataNotAllowed: + status = ResponseStatus::ConnectionError; + break; + + default: + status = ResponseStatus::PermanentError; + } + } + } else if ([res isKindOfClass:[NSHTTPURLResponse class]]) { + const long responseCode = [(NSHTTPURLResponse *)res statusCode]; + + response = util::make_unique<Response>(); + response->data = {(const char *)[data bytes], [data length]}; + + NSDictionary *headers = [(NSHTTPURLResponse *)res allHeaderFields]; + NSString *cache_control = [headers objectForKey:@"Cache-Control"]; + if (cache_control) { + response->expires = parseCacheControl([cache_control UTF8String]); + } + + NSString *expires = [headers objectForKey:@"Expires"]; + if (expires) { + response->expires = parse_date([expires UTF8String]); + } + + NSString *last_modified = [headers objectForKey:@"Last-Modified"]; + if (last_modified) { + response->modified = parse_date([last_modified UTF8String]); + } + + NSString *etag = [headers objectForKey:@"ETag"]; + if (etag) { + response->etag = [etag UTF8String]; + } + + if (responseCode == 304) { + if (existingResponse) { + // We're going to reuse the old response object, but need to copy over the new + // expires value (if possible). + std::swap(response, existingResponse); + if (existingResponse->expires) { + response->expires = existingResponse->expires; + } + status = ResponseStatus::NotModified; + } else { + // This is an unsolicited 304 response and should only happen on malfunctioning + // HTTP servers. It likely doesn't include any data, but we don't have much options. + response->status = Response::Successful; + status = ResponseStatus::Successful; + } + } else if (responseCode == 200) { + response->status = Response::Successful; + status = ResponseStatus::Successful; + } else if (responseCode >= 500 && responseCode < 600) { + // Server errors may be temporary, so back off exponentially. + response->status = Response::Error; + response->message = "HTTP status code " + std::to_string(responseCode); + status = ResponseStatus::TemporaryError; + } else { + // We don't know how to handle any other errors, so declare them as permanently failing. + response->status = Response::Error; + response->message = "HTTP status code " + std::to_string(responseCode); + status = ResponseStatus::PermanentError; + } + } else { + // This should never happen. + status = ResponseStatus::PermanentError; + response = util::make_unique<Response>(); + response->status = Response::Error; + response->message = "response class is not NSHTTPURLResponse"; + } + + uv_async_send(async); +} + +void HTTPRequestImpl::retry(uint64_t timeout) { + response.reset(); + + assert(!timer); + timer = new uv_timer_t; + timer->data = this; + uv_timer_init(async->loop, timer); + uv_timer_start(timer, restart, timeout, 0); +} + +void HTTPRequestImpl::retryImmediately() { + // All batons get notified when the network status changed, but some of them + // might not actually wait for the network to become available again. + if (timer && strategy == PreemptImmediately) { + // Triggers the timer upon the next event loop iteration. + uv_timer_stop(timer); + uv_timer_start(timer, restart, 0, 0); + } +} + +void HTTPRequestImpl::restart(uv_timer_t *timer, int) { + // Restart the request. + auto impl = reinterpret_cast<HTTPRequestImpl *>(timer->data); + + // Get rid of the timer. + impl->timer = nullptr; + uv::close(timer); + + impl->start(); +} + +// ------------------------------------------------------------------------------------------------- + +HTTPRequest::HTTPRequest(DefaultFileSource *source, const Resource &resource) + : SharedRequestBase(source, resource) { + // Global initialization. + dispatch_once(&request_initialize, ^{ + NSURLSessionConfiguration *sessionConfig = + [NSURLSessionConfiguration defaultSessionConfiguration]; + sessionConfig.timeoutIntervalForResource = 30; + sessionConfig.HTTPMaximumConnectionsPerHost = 8; + sessionConfig.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData; + sessionConfig.URLCache = nil; + + session = [NSURLSession sessionWithConfiguration:sessionConfig]; + + // Write user agent string + userAgent = @"MapboxGL"; + }); +} + +HTTPRequest::~HTTPRequest() { + MBGL_VERIFY_THREAD(tid); + + if (ptr) { + reinterpret_cast<HTTPRequestImpl *>(ptr)->cancel(); + } +} + +void HTTPRequest::start(uv_loop_t *loop, std::unique_ptr<Response> response) { + MBGL_VERIFY_THREAD(tid); + + assert(!ptr); + ptr = new HTTPRequestImpl(this, loop, std::move(response)); +} + +void HTTPRequest::retryImmediately() { + MBGL_VERIFY_THREAD(tid); + + if (ptr) { + reinterpret_cast<HTTPRequestImpl *>(ptr)->retryImmediately(); + } +} + +void HTTPRequest::cancel() { + MBGL_VERIFY_THREAD(tid); + + delete this; +} + +} diff --git a/platform/default/asset_request_libuv.cpp b/platform/default/asset_request_libuv.cpp index 0e0b7280a7..36342bc876 100644 --- a/platform/default/asset_request_libuv.cpp +++ b/platform/default/asset_request_libuv.cpp @@ -1,19 +1,26 @@ -#include <mbgl/storage/asset_request.hpp> +#include <mbgl/storage/default/asset_request.hpp> #include <mbgl/storage/response.hpp> -#include <mbgl/platform/platform.hpp> #include <mbgl/util/std.hpp> +#include <mbgl/util/util.hpp> +#include <mbgl/util/uv.hpp> #include <uv.h> +#include <boost/algorithm/string.hpp> -#include <limits> +#include <cassert> + + +namespace algo = boost::algorithm; namespace mbgl { -struct AssetRequestBaton { - AssetRequestBaton(AssetRequest *request_, const std::string &path, uv_loop_t *loop); - ~AssetRequestBaton(); +class AssetRequestImpl { + MBGL_STORE_THREAD(tid) + +public: + AssetRequestImpl(AssetRequest *request, uv_loop_t *loop); + ~AssetRequestImpl(); - void cancel(); static void fileOpened(uv_fs_t *req); static void fileStated(uv_fs_t *req); static void fileRead(uv_fs_t *req); @@ -21,52 +28,32 @@ struct AssetRequestBaton { static void notifyError(uv_fs_t *req); static void cleanup(uv_fs_t *req); - const std::thread::id threadId; + AssetRequest *request = nullptr; + bool canceled = false; uv_fs_t req; uv_file fd = -1; - bool canceled = false; - std::string body; uv_buf_t buffer; + std::unique_ptr<Response> response; }; -AssetRequestBaton::AssetRequestBaton(AssetRequest *request_, const std::string &path, uv_loop_t *loop) - : threadId(std::this_thread::get_id()), request(request_) { - req.data = this; - uv_fs_open(loop, &req, path.c_str(), O_RDONLY, S_IRUSR, fileOpened); -} - -AssetRequestBaton::~AssetRequestBaton() { -} - -void AssetRequestBaton::cancel() { - canceled = true; +AssetRequestImpl::~AssetRequestImpl() { + MBGL_VERIFY_THREAD(tid); - // uv_cancel fails frequently when the request has already been started. - // In that case, we have to let it complete and check the canceled bool - // instead. - uv_cancel((uv_req_t *)&req); + if (request) { + request->ptr = nullptr; + } } -void AssetRequestBaton::notifyError(uv_fs_t *req) { - AssetRequestBaton *ptr = reinterpret_cast<AssetRequestBaton *>(req->data); - assert(std::this_thread::get_id() == ptr->threadId); - - if (ptr->request && req->result < 0 && !ptr->canceled && req->result != UV_ECANCELED) { - ptr->request->response = util::make_unique<Response>(); - ptr->request->response->code = req->result == UV_ENOENT ? 404 : 500; -#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10 - ptr->request->response->message = uv_strerror(uv_last_error(req->loop)); -#else - ptr->request->response->message = uv_strerror(int(req->result)); -#endif - ptr->request->notify(); - } +AssetRequestImpl::AssetRequestImpl(AssetRequest *request_, uv_loop_t *loop) : request(request_) { + req.data = this; + uv_fs_open(loop, &req, (request->resource.url.substr(8)).c_str(), O_RDONLY, S_IRUSR, fileOpened); } -void AssetRequestBaton::fileOpened(uv_fs_t *req) { - AssetRequestBaton *ptr = reinterpret_cast<AssetRequestBaton *>(req->data); - assert(std::this_thread::get_id() == ptr->threadId); +void AssetRequestImpl::fileOpened(uv_fs_t *req) { + assert(req->data); + auto self = reinterpret_cast<AssetRequestImpl *>(req->data); + MBGL_VERIFY_THREAD(self->tid); if (req->result < 0) { // Opening failed or was canceled. There isn't much left we can do. @@ -78,88 +65,93 @@ void AssetRequestBaton::fileOpened(uv_fs_t *req) { // We're going to reuse this handle, so we need to cleanup first. uv_fs_req_cleanup(req); - if (ptr->canceled || !ptr->request) { - // Either the AssetRequest object has been destructed, or the - // request was canceled. + if (self->canceled) { + // The request was canceled. uv_fs_close(req->loop, req, fd, fileClosed); } else { - ptr->fd = fd; + self->fd = fd; uv_fs_fstat(req->loop, req, fd, fileStated); } } } -void AssetRequestBaton::fileStated(uv_fs_t *req) { - AssetRequestBaton *ptr = reinterpret_cast<AssetRequestBaton *>(req->data); - assert(std::this_thread::get_id() == ptr->threadId); +void AssetRequestImpl::fileStated(uv_fs_t *req) { + assert(req->data); + auto self = reinterpret_cast<AssetRequestImpl *>(req->data); + MBGL_VERIFY_THREAD(self->tid); - if (req->result != 0 || ptr->canceled || !ptr->request) { + if (req->result != 0 || self->canceled) { // Stating failed or was canceled. We already have an open file handle // though, which we'll have to close. notifyError(req); uv_fs_req_cleanup(req); - uv_fs_close(req->loop, req, ptr->fd, fileClosed); + uv_fs_close(req->loop, req, self->fd, fileClosed); } else { #if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10 - const uv_statbuf_t *stat = static_cast<const uv_statbuf_t *>(req->ptr); + auto stat = static_cast<const uv_statbuf_t *>(req->ptr); #else - const uv_stat_t *stat = static_cast<const uv_stat_t *>(req->ptr); + auto stat = static_cast<const uv_stat_t *>(req->ptr); #endif if (stat->st_size > std::numeric_limits<int>::max()) { // File is too large for us to open this way because uv_buf's only support unsigned // ints as maximum size. - if (ptr->request) { - ptr->request->response = util::make_unique<Response>(); - ptr->request->response->code = UV_EFBIG; + auto response = util::make_unique<Response>(); + response->status = Response::Error; #if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10 - ptr->request->response->message = uv_strerror(uv_err_t {UV_EFBIG, 0}); + response->message = uv_strerror(uv_err_t {UV_EFBIG, 0}); #else - ptr->request->response->message = uv_strerror(UV_EFBIG); + response->message = uv_strerror(UV_EFBIG); #endif - ptr->request->notify(); - } + assert(self->request); + self->request->notify(std::move(response), FileCache::Hint::No); + delete self->request; uv_fs_req_cleanup(req); - uv_fs_close(req->loop, req, ptr->fd, fileClosed); + uv_fs_close(req->loop, req, self->fd, fileClosed); } else { - const unsigned int size = (unsigned int)(stat->st_size); - ptr->body.resize(size); - ptr->buffer = uv_buf_init(const_cast<char *>(ptr->body.data()), size); + self->response = util::make_unique<Response>(); + self->response->modified = stat->st_mtimespec.tv_sec; + self->response->etag = std::to_string(stat->st_ino); + const auto size = (unsigned int)(stat->st_size); + self->response->data.resize(size); + self->buffer = uv_buf_init(const_cast<char *>(self->response->data.data()), size); uv_fs_req_cleanup(req); #if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10 - uv_fs_read(req->loop, req, ptr->fd, ptr->buffer.base, ptr->buffer.len, -1, fileRead); + uv_fs_read(req->loop, req, self->fd, self->buffer.base, self->buffer.len, -1, fileRead); #else - uv_fs_read(req->loop, req, ptr->fd, &ptr->buffer, 1, 0, fileRead); + uv_fs_read(req->loop, req, self->fd, &self->buffer, 1, 0, fileRead); #endif } } } -void AssetRequestBaton::fileRead(uv_fs_t *req) { - AssetRequestBaton *ptr = reinterpret_cast<AssetRequestBaton *>(req->data); - assert(std::this_thread::get_id() == ptr->threadId); +void AssetRequestImpl::fileRead(uv_fs_t *req) { + assert(req->data); + auto self = reinterpret_cast<AssetRequestImpl *>(req->data); + MBGL_VERIFY_THREAD(self->tid); - if (req->result < 0 || ptr->canceled || !ptr->request) { - // Reading failed or was canceled. We already have an open file handle + if (req->result < 0 || self->canceled) { + // Stating failed or was canceled. We already have an open file handle // though, which we'll have to close. notifyError(req); } else { // File was successfully read. - if (ptr->request) { - ptr->request->response = util::make_unique<Response>(); - ptr->request->response->code = 200; - ptr->request->response->data = std::move(ptr->body); - ptr->request->notify(); - } + self->response->status = Response::Successful; + assert(self->request); + self->request->notify(std::move(self->response), FileCache::Hint::No); + delete self->request; } uv_fs_req_cleanup(req); - uv_fs_close(req->loop, req, ptr->fd, fileClosed); + uv_fs_close(req->loop, req, self->fd, fileClosed); } -void AssetRequestBaton::fileClosed(uv_fs_t *req) { - assert(std::this_thread::get_id() == (reinterpret_cast<AssetRequestBaton *>(req->data))->threadId); +void AssetRequestImpl::fileClosed(uv_fs_t *req) { + assert(req->data); + auto self = reinterpret_cast<AssetRequestImpl *>(req->data); + MBGL_VERIFY_THREAD(self->tid); + (void(self)); // Silence unused variable error in Release mode if (req->result < 0) { // Closing the file failed. But there isn't anything we can do. @@ -168,56 +160,70 @@ void AssetRequestBaton::fileClosed(uv_fs_t *req) { cleanup(req); } -void AssetRequestBaton::cleanup(uv_fs_t *req) { - AssetRequestBaton *ptr = reinterpret_cast<AssetRequestBaton *>(req->data); - assert(std::this_thread::get_id() == ptr->threadId); - - if (ptr->request) { - ptr->request->ptr = nullptr; +void AssetRequestImpl::notifyError(uv_fs_t *req) { + assert(req->data); + auto self = reinterpret_cast<AssetRequestImpl *>(req->data); + MBGL_VERIFY_THREAD(self->tid); + + if (req->result < 0 && !self->canceled && req->result != UV_ECANCELED) { + auto response = util::make_unique<Response>(); + response->status = Response::Error; + response->message = uv::getFileRequestError(req); + assert(self->request); + self->request->notify(std::move(response), FileCache::Hint::No); + delete self->request; } +} +void AssetRequestImpl::cleanup(uv_fs_t *req) { + assert(req->data); + auto self = reinterpret_cast<AssetRequestImpl *>(req->data); + MBGL_VERIFY_THREAD(self->tid); uv_fs_req_cleanup(req); - delete ptr; - ptr = nullptr; + delete self; } +// ------------------------------------------------------------------------------------------------- -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); - } +AssetRequest::AssetRequest(DefaultFileSource *source, const Resource &resource) + : SharedRequestBase(source, resource) { + assert(algo::starts_with(resource.url, "asset://")); } -void AssetRequest::cancel() { - assert(std::this_thread::get_id() == threadId); +AssetRequest::~AssetRequest() { + MBGL_VERIFY_THREAD(tid); if (ptr) { - ptr->cancel(); - - // 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; + reinterpret_cast<AssetRequestImpl *>(ptr)->request = nullptr; } +} + +void AssetRequest::start(uv_loop_t *loop, std::unique_ptr<Response> response) { + MBGL_VERIFY_THREAD(tid); + + // We're ignoring the existing response if any. + (void(response)); - notify(); + assert(!ptr); + ptr = new AssetRequestImpl(this, loop); + // Note: the AssetRequestImpl deletes itself. } -AssetRequest::~AssetRequest() { - assert(std::this_thread::get_id() == threadId); - cancel(); +void AssetRequest::cancel() { + MBGL_VERIFY_THREAD(tid); - // Note: The AssetRequestBaton object is deleted in AssetRequestBaton::cleanup(). + if (ptr) { + reinterpret_cast<AssetRequestImpl *>(ptr)->canceled = true; + + // uv_cancel fails frequently when the request has already been started. + // In that case, we have to let it complete and check the canceled bool + // instead. The cancelation callback will delete the AssetRequest object. + uv_cancel((uv_req_t *)&reinterpret_cast<AssetRequestImpl *>(ptr)->req); + } else { + // This request is canceled before we called start. We're safe to delete + // ourselves now. + delete this; + } } } diff --git a/platform/default/cache_database_tmp.cpp b/platform/default/cache_database_tmp.cpp deleted file mode 100644 index 6132ace692..0000000000 --- a/platform/default/cache_database_tmp.cpp +++ /dev/null @@ -1,12 +0,0 @@ -#include <mbgl/platform/platform.hpp> - -namespace mbgl { -namespace platform { - -// Returns the path to the default cache database on this system. -std::string defaultCacheDatabase() { - return "/tmp/mbgl-cache.db"; -} - -} -} diff --git a/platform/default/http_request_baton_curl.cpp b/platform/default/http_request_baton_curl.cpp deleted file mode 100644 index 9e8cf64716..0000000000 --- a/platform/default/http_request_baton_curl.cpp +++ /dev/null @@ -1,629 +0,0 @@ -#include <mbgl/mbgl.hpp> -#include <mbgl/storage/http_request_baton.hpp> -#include <mbgl/util/uv-messenger.h> -#include <mbgl/util/time.hpp> -#include <mbgl/util/string.hpp> -#include <mbgl/util/std.hpp> -#include <mbgl/util/version.hpp> - -#ifdef __ANDROID__ - #include <mbgl/android/jni.hpp> - #include <zip.h> - #include <openssl/ssl.h> -#endif - -#include <uv.h> -#include <curl/curl.h> - -#include <sys/utsname.h> - -#include <queue> -#include <cassert> -#include <cstring> -#include <thread> - - -// Check curl library version. -const static bool curl_version_check = []() { - const auto version = curl_version_info(CURLVERSION_NOW); - if (version->version_num != LIBCURL_VERSION_NUM) { - throw std::runtime_error(mbgl::util::sprintf<96>( - "libcurl version mismatch: headers report %d.%d.%d, but library reports %d.%d.%d", - (LIBCURL_VERSION_NUM >> 16) & 0xFF, (LIBCURL_VERSION_NUM >> 8) & 0xFF, LIBCURL_VERSION_NUM & 0xFF, - (version->version_num >> 16) & 0xFF, (version->version_num >> 8) & 0xFF, version->version_num & 0xFF)); - } - return true; -}(); - - -// This file contains code from http://curl.haxx.se/libcurl/c/multi-uv.html: - -/*************************************************************************** - * _ _ ____ _ - * Project ___| | | | _ \| | - * / __| | | | |_) | | - * | (__| |_| | _ <| |___ - * \___|\___/|_| \_\_____| - * - * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al. - * - * This software is licensed as described in the file COPYING, which - * you should have received as part of this distribution. The terms - * are also available at http://curl.haxx.se/docs/copyright.html. - * - * You may opt to use, copy, modify, merge, publish, distribute and/or sell - * copies of the Software, and permit persons to whom the Software is - * furnished to do so, under the terms of the COPYING file. - * - * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY - * KIND, either express or implied. - * - ***************************************************************************/ - -/* Example application code using the multi socket interface to download - multiple files at once, but instead of using curl_multi_perform and - curl_multi_wait, which uses select(), we use libuv. - It supports epoll, kqueue, etc. on unixes and fast IO completion ports on - Windows, which means, it should be very fast on all platforms.. - - Written by Clemens Gruber, based on an outdated example from uvbook and - some tests from libuv. - - Requires libuv and (of course) libcurl. - - See http://nikhilm.github.com/uvbook/ for more information on libuv. -*/ - -// Handles the request thread + messaging to the thread. -static uv_once_t once; -static uv_loop_t *loop = nullptr; -static uv_messenger_t start_messenger; -static uv_messenger_t stop_messenger; -static uv_thread_t thread; -static std::thread::id thread_id; - -// Used as the CURL timer function to periodically check for socket updates. -static uv_timer_t timeout; - -// CURL multi handle that we use to request multiple URLs at the same time, without having to block -// and spawn threads. -static CURLM *multi = nullptr; - -// CURL share handles are used for sharing session state (e.g.) -static uv_mutex_t share_mutex; -static CURLSH *share = nullptr; - -// A queue that we use for storing resuable CURL easy handles to avoid creating and destroying them -// all the time. -static std::queue<CURL *> handles; - -namespace mbgl { - -struct Context { - const util::ptr<HTTPRequestBaton> baton; - CURL *handle = nullptr; - curl_slist *headers = nullptr; - - Context(const util::ptr<HTTPRequestBaton> &baton_) : baton(baton_) { - assert(baton); - baton->ptr = this; - - if (!handles.empty()) { - handle = handles.front(); - handles.pop(); - } else { - handle = curl_easy_init(); - } - } - - ~Context() { - baton->ptr = nullptr; - - if (headers) { - curl_slist_free_all(headers); - headers = nullptr; - } - - CURLMcode error = curl_multi_remove_handle(multi, handle); - if (error != CURLM_OK) { - baton->response = util::make_unique<Response>(); - baton->response->code = -1; - baton->response->message = curl_multi_strerror(error); - } - - curl_easy_setopt(handle, CURLOPT_PRIVATE, nullptr); - curl_easy_reset(handle); - handles.push(handle); - handle = nullptr; - - if (baton->async) { - uv_async_send(baton->async); - baton->async = nullptr; - } - } -}; - -struct Socket { -private: - uv_poll_t poll_handle; - -public: - const curl_socket_t sockfd = 0; - -public: - Socket(curl_socket_t sockfd_) : sockfd(sockfd_) { - uv_poll_init_socket(loop, &poll_handle, sockfd); - poll_handle.data = this; - } - - void start(int events, uv_poll_cb cb) { - uv_poll_start(&poll_handle, events, cb); - } - - void stop() { - assert(poll_handle.data); - poll_handle.data = nullptr; - uv_poll_stop(&poll_handle); - uv_close((uv_handle_t *)&poll_handle, [](uv_handle_t *handle) { - delete (Socket *)handle->data; - }); - } - -private: - // Make the destructor private to ensure that we can only close the Socket - // with stop(), and disallow manual deletion. - ~Socket() { - assert(!poll_handle.data); - } -}; - -// Locks the CURL share handle -void curl_share_lock(CURL *, curl_lock_data, curl_lock_access, void *) { - uv_mutex_lock(&share_mutex); -} - -// Unlocks the CURL share handle -void curl_share_unlock(CURL *, curl_lock_data, void *) { - uv_mutex_unlock(&share_mutex); -} - -void check_multi_info() { - CURLMsg *message = nullptr; - int pending = 0; - - while ((message = curl_multi_info_read(multi, &pending))) { - switch (message->msg) { - case CURLMSG_DONE: { - Context *context = nullptr; - curl_easy_getinfo(message->easy_handle, CURLINFO_PRIVATE, (char *)&context); - assert(context); - - auto baton = context->baton; - - // This request is complete. We are removing the pointer to the CURL easy handle again - // to prevent this request from getting canceled. - context->baton->ptr = nullptr; - - // Add human-readable error code - if (message->data.result != CURLE_OK) { - baton->response->message = curl_easy_strerror(message->data.result); - baton->response->code = -1; - - switch (message->data.result) { - case CURLE_COULDNT_RESOLVE_PROXY: - case CURLE_COULDNT_RESOLVE_HOST: - case CURLE_COULDNT_CONNECT: - baton->type = HTTPResponseType::ConnectionError; - break; - - case CURLE_OPERATION_TIMEDOUT: - baton->type = HTTPResponseType::TemporaryError; - break; - - default: - baton->type = HTTPResponseType::PermanentError; - } - } else { - long code = 0; - curl_easy_getinfo(message->easy_handle, CURLINFO_RESPONSE_CODE, &code); - - if (code != 304) { - baton->response->code = code; - } - - if (code == 304) { - baton->type = HTTPResponseType::NotModified; - } else if (code == 200) { - baton->type = HTTPResponseType::Successful; - } else if (code >= 500 && code < 600) { - baton->type = HTTPResponseType::TemporaryError; - } else if (code >= 400 && code < 500) { - baton->type = HTTPResponseType::PermanentError; - } else { - assert(!"code must be either 200 or 304"); - } - } - - delete context; - } break; - - default: - // This should never happen, because there are no other message types. - throw std::runtime_error("CURLMSG returned unknown message type"); - } - } -} - -void curl_perform(uv_poll_t *req, int /* status */, int events) { - int flags = 0; - - uv_timer_stop(&timeout); - - if (events & UV_READABLE) { - flags |= CURL_CSELECT_IN; - } - if (events & UV_WRITABLE) { - flags |= CURL_CSELECT_OUT; - } - - Socket *context = (Socket *)req->data; - int running_handles = 0; - curl_multi_socket_action(multi, context->sockfd, flags, &running_handles); - - check_multi_info(); -} - -int handle_socket(CURL * /* handle */, curl_socket_t s, int action, void * /* userp */, void *socketp) { - Socket *socket = (Socket *)socketp; - if (!socket && action != CURL_POLL_REMOVE) { - socket = new Socket(s); - curl_multi_assign(multi, s, (void *)socket); - } - - switch (action) { - case CURL_POLL_IN: - socket->start(UV_READABLE, curl_perform); - break; - case CURL_POLL_OUT: - socket->start(UV_WRITABLE, curl_perform); - break; - case CURL_POLL_REMOVE: - if (socket) { - socket->stop(); - curl_multi_assign(multi, s, NULL); - } - break; - default: - abort(); - } - - return 0; -} - -#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10 -void on_timeout(uv_timer_t * /* req */, int /* status */) { -#else -void on_timeout(uv_timer_t * /* req */) { -#endif - int running_handles; - CURLMcode error = curl_multi_socket_action(multi, CURL_SOCKET_TIMEOUT, 0, &running_handles); - if (error != CURLM_OK) { - throw std::runtime_error(std::string("CURL multi error: ") + curl_multi_strerror(error)); - } - - check_multi_info(); -} - -void start_timeout(CURLM * /* multi */, long timeout_ms, void * /* userp */) { - if (timeout_ms <= 0) { - timeout_ms = 1; /* 0 means directly call socket_action, but we'll do it in a bit */ - } - uv_timer_start(&timeout, on_timeout, timeout_ms, 0); -} - -void thread_init(void *) { -#ifdef __APPLE__ - pthread_setname_np("CURL"); -#endif - thread_id = std::this_thread::get_id(); - - if (curl_global_init(CURL_GLOBAL_ALL)) { - throw std::runtime_error("Could not init cURL"); - } - - uv_timer_init(loop, &timeout); - - CURLSHcode share_error; - share = curl_share_init(); - - share_error = curl_share_setopt(share, CURLSHOPT_LOCKFUNC, curl_share_lock); - if (share_error != CURLSHE_OK) { - throw std::runtime_error(std::string("CURL share error: ") + curl_share_strerror(share_error)); - } - - share_error = curl_share_setopt(share, CURLSHOPT_UNLOCKFUNC, curl_share_unlock); - if (share_error != CURLSHE_OK) { - throw std::runtime_error(std::string("CURL share error: ") + curl_share_strerror(share_error)); - } - - CURLMcode multi_error; - multi = curl_multi_init(); - - multi_error = curl_multi_setopt(multi, CURLMOPT_SOCKETFUNCTION, handle_socket); - if (multi_error != CURLM_OK) { - throw std::runtime_error(std::string("CURL multi error: ") + curl_multi_strerror(multi_error)); - } - multi_error = curl_multi_setopt(multi, CURLMOPT_TIMERFUNCTION, start_timeout); - if (multi_error != CURLM_OK) { - throw std::runtime_error(std::string("CURL multi error: ") + curl_multi_strerror(multi_error)); - } - - // Main event loop. This will not return until the request loop is terminated. - uv_run(loop, UV_RUN_DEFAULT); - - curl_multi_cleanup(multi); - multi = nullptr; - - curl_share_cleanup(share); - share = nullptr; - - thread_id = std::thread::id(); -} - -// This function is called when we have new data for a request. We just append it to the string -// containing the previous data. -size_t curl_write_cb(void *const contents, const size_t size, const size_t nmemb, void *const userp) { - auto &response = *(std::unique_ptr<Response> *)userp; - assert(response); - response->data.append((char *)contents, size * nmemb); - return size * nmemb; -} - -// Compares the beginning of the (non-zero-terminated!) data buffer with the (zero-terminated!) -// header string. If the data buffer contains the header string at the beginning, it returns -// the length of the header string == begin of the value, otherwise it returns npos. -// The comparison of the header is ASCII-case-insensitive. -size_t header_matches(const char *const header, const char *const buffer, const size_t length) { - const size_t header_length = strlen(header); - if (length < header_length) return std::string::npos; - size_t i = 0; - while (i < length && i < header_length && std::tolower(buffer[i]) == header[i]) { - i++; - } - return i == header_length ? i : std::string::npos; -} - -size_t curl_header_cb(char * const buffer, const size_t size, const size_t nmemb, void *const userp) { - const size_t length = size * nmemb; - - auto &response = *(std::unique_ptr<Response> *)userp; - assert(response); - - size_t begin = std::string::npos; - if ((begin = header_matches("last-modified: ", buffer, length)) != std::string::npos) { - // Always overwrite the modification date; We might already have a value here from the - // Date header, but this one is more accurate. - const std::string value { buffer + begin, length - begin - 2 }; // remove \r\n - response->modified = curl_getdate(value.c_str(), nullptr); - } else if ((begin = header_matches("etag: ", buffer, length)) != std::string::npos) { - response->etag = { buffer + begin, length - begin - 2 }; // remove \r\n - } else if ((begin = header_matches("cache-control: ", buffer, length)) != std::string::npos) { - const std::string value { buffer + begin, length - begin - 2 }; // remove \r\n - response->expires = Response::parseCacheControl(value.c_str()); - } - - return length; -} - -// This function is called to load the CA bundle -// from http://curl.haxx.se/libcurl/c/cacertinmem.html -#ifdef __ANDROID__ -static CURLcode sslctx_function(CURL */*curl*/, void *sslctx, void */*parm*/) { - - int error = 0; - struct zip *apk = zip_open(mbgl::android::apkPath.c_str(), 0, &error); - if (apk == nullptr) { - return CURLE_SSL_CACERT_BADFILE; - } - - struct zip_file *apkFile = zip_fopen(apk, "assets/ca-bundle.crt", ZIP_FL_NOCASE); - if (apkFile == nullptr) { - zip_close(apk); - apk = nullptr; - return CURLE_SSL_CACERT_BADFILE; - } - - struct zip_stat stat; - if (zip_stat(apk, "assets/ca-bundle.crt", ZIP_FL_NOCASE, &stat) != 0) { - zip_fclose(apkFile); - apkFile = nullptr; - zip_close(apk); - apk = nullptr; - return CURLE_SSL_CACERT_BADFILE; - } - - if (stat.size > std::numeric_limits<int>::max()) { - zip_fclose(apkFile); - apkFile = nullptr; - zip_close(apk); - apk = nullptr; - return CURLE_SSL_CACERT_BADFILE; - } - - const std::unique_ptr<char[]> pem = util::make_unique<char[]>(stat.size); - - if (static_cast<zip_uint64_t>(zip_fread(apkFile, reinterpret_cast<void *>(pem.get()), stat.size)) != stat.size) { - zip_fclose(apkFile); - apkFile = nullptr; - zip_close(apk); - apk = nullptr; - return CURLE_SSL_CACERT_BADFILE; - } - - // get a pointer to the X509 certificate store (which may be empty!) - X509_STORE *store = SSL_CTX_get_cert_store((SSL_CTX *)sslctx); - if (store == nullptr) { - return CURLE_SSL_CACERT_BADFILE; - } - - // get a BIO - BIO *bio = BIO_new_mem_buf(pem.get(), static_cast<int>(stat.size)); - if (bio == nullptr) { - store = nullptr; - return CURLE_SSL_CACERT_BADFILE; - } - - // use it to read the PEM formatted certificate from memory into an X509 - // structure that SSL can use - X509 *cert = nullptr; - while (PEM_read_bio_X509(bio, &cert, 0, nullptr) != nullptr) { - if (cert == nullptr) { - BIO_free(bio); - bio = nullptr; - store = nullptr; - return CURLE_SSL_CACERT_BADFILE; - } - - // add our certificate to this store - if (X509_STORE_add_cert(store, cert) == 0) { - X509_free(cert); - cert = nullptr; - BIO_free(bio); - bio = nullptr; - store = nullptr; - return CURLE_SSL_CACERT_BADFILE; - } - - X509_free(cert); - cert = nullptr; - } - - // decrease reference counts - BIO_free(bio); - bio = nullptr; - - zip_fclose(apkFile); - apkFile = nullptr; - zip_close(apk); - apk = nullptr; - - // all set to go - return CURLE_OK; -} -#endif - -std::string buildUserAgentString() { -#ifdef __ANDROID__ - return util::sprintf<128>("MapboxGL/%d.%d.%d (+https://mapbox.com/mapbox-gl/; %s; %s %s)", - version::major, version::minor, version::patch, version::revision, "Android", mbgl::android::androidRelease.c_str()); -#else - utsname name; - uname(&name); - return util::sprintf<128>("MapboxGL/%d.%d.%d (+https://mapbox.com/mapbox-gl/; %s; %s %s)", - version::major, version::minor, version::patch, version::revision, name.sysname, name.release); -#endif -} - -// This function must run in the CURL thread. -void start_request(void *const ptr) { - assert(std::this_thread::get_id() == thread_id); - static const std::string userAgent = buildUserAgentString(); - - // The Context object stores information that we need to retain throughout the request, such - // as the actual CURL easy handle, the baton, and the list of headers. The Context itself is - // stored in both the CURL easy handle's PRIVATE field, and the baton's `ptr` field. - auto context = new Context(*(util::ptr<HTTPRequestBaton> *)ptr); - delete (util::ptr<HTTPRequestBaton> *)ptr; - - if (context->baton->response) { - if (!context->baton->response->etag.empty()) { - const std::string header = std::string("If-None-Match: ") + context->baton->response->etag; - context->headers = curl_slist_append(context->headers, header.c_str()); - } else if (context->baton->response->modified) { - const std::string time = - std::string("If-Modified-Since: ") + util::rfc1123(context->baton->response->modified); - context->headers = curl_slist_append(context->headers, time.c_str()); - } - } - - if (context->headers) { - curl_easy_setopt(context->handle, CURLOPT_HTTPHEADER, context->headers); - } - - if (!context->baton->response) { - context->baton->response = util::make_unique<Response>(); - } - - // Carry on the shared pointer in the private information of the CURL handle. - curl_easy_setopt(context->handle, CURLOPT_PRIVATE, context); -#ifndef __ANDROID__ - curl_easy_setopt(context->handle, CURLOPT_CAINFO, "ca-bundle.crt"); -#else - curl_easy_setopt(context->handle, CURLOPT_SSLCERTTYPE, "PEM"); - curl_easy_setopt(context->handle, CURLOPT_SSL_CTX_FUNCTION, sslctx_function); -#endif - curl_easy_setopt(context->handle, CURLOPT_FOLLOWLOCATION, 1); - curl_easy_setopt(context->handle, CURLOPT_URL, context->baton->path.c_str()); - curl_easy_setopt(context->handle, CURLOPT_WRITEFUNCTION, curl_write_cb); - curl_easy_setopt(context->handle, CURLOPT_WRITEDATA, &context->baton->response); - curl_easy_setopt(context->handle, CURLOPT_HEADERFUNCTION, curl_header_cb); - curl_easy_setopt(context->handle, CURLOPT_HEADERDATA, &context->baton->response); - curl_easy_setopt(context->handle, CURLOPT_ACCEPT_ENCODING, "gzip, deflate"); - curl_easy_setopt(context->handle, CURLOPT_USERAGENT, userAgent.c_str()); - curl_easy_setopt(context->handle, CURLOPT_SHARE, share); - - // Start requesting the information. - curl_multi_add_handle(multi, context->handle); -} - -// This function must run in the CURL thread. -void stop_request(void *const ptr) { - assert(std::this_thread::get_id() == thread_id); - auto baton = *(util::ptr<HTTPRequestBaton> *)ptr; - delete (util::ptr<HTTPRequestBaton> *)ptr; - assert(baton); - - if (baton->async) { - baton->type = HTTPResponseType::Canceled; - - assert(baton->ptr); - - // We can still stop the request because it is still in progress. - delete (Context *)baton->ptr; - assert(!baton->ptr); - } else { - // If the async handle is gone, it means that the actual request has been completed before - // we got a chance to cancel it. In this case, this is a no-op. It is likely that - // the pointer below is the last lifeline of the HTTPRequestBaton. This means we're going - // to delete the HTTPRequestBaton in the current (CURL) thread. - } -} - -void create_thread() { - uv_mutex_init(&share_mutex); -#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10 - loop = uv_loop_new(); -#else - loop = new uv_loop_t; - uv_loop_init(loop); -#endif - uv_messenger_init(loop, &start_messenger, start_request); - uv_messenger_init(loop, &stop_messenger, stop_request); - uv_thread_create(&thread, thread_init, nullptr); -} - -// This function must be run from the main thread (== where the HTTPRequestBaton was created) -void HTTPRequestBaton::start(const util::ptr<HTTPRequestBaton> &ptr) { - assert(std::this_thread::get_id() == ptr->threadId); - uv_once(&once, create_thread); - uv_messenger_send(&start_messenger, new util::ptr<HTTPRequestBaton>(ptr)); -} - -// This function must be run from the main thread (== where the HTTPRequestBaton was created) -void HTTPRequestBaton::stop(const util::ptr<HTTPRequestBaton> &ptr) { - assert(std::this_thread::get_id() == ptr->threadId); - uv_once(&once, create_thread); - uv_messenger_send(&stop_messenger, new util::ptr<HTTPRequestBaton>(ptr)); -} - -} diff --git a/platform/default/http_request_curl.cpp b/platform/default/http_request_curl.cpp new file mode 100644 index 0000000000..42764e4927 --- /dev/null +++ b/platform/default/http_request_curl.cpp @@ -0,0 +1,640 @@ +#include <mbgl/storage/default/http_request.hpp> +#include <mbgl/storage/default/http_context.hpp> +#include <mbgl/storage/response.hpp> + +#include <mbgl/util/time.hpp> + +#include <curl/curl.h> + +#include <queue> +#include <map> +#include <cassert> + + +void handleError(CURLMcode code) { + if (code != CURLM_OK) { + throw std::runtime_error(std::string("CURL multi error: ") + curl_multi_strerror(code)); + } +} + +void handleError(CURLcode code) { + if (code != CURLE_OK) { + throw std::runtime_error(std::string("CURL easy error: ") + curl_easy_strerror(code)); + } +} + +namespace mbgl { + +enum class ResponseStatus : int8_t { + // This error probably won't be resolved by retrying anytime soon. We are giving up. + PermanentError, + + // This error might be resolved by waiting some time (e.g. server issues). + // We are going to do an exponential back-off and will try again in a few seconds. + TemporaryError, + + // This error might be resolved once the network reachability status changes. + // We are going to watch the network status for changes and will retry as soon as the + // operating system notifies us of a network status change. + ConnectionError, + + // The request returned data successfully. We retrieved and decoded the data successfully. + Successful, + + // The request confirmed that the data wasn't changed. We already have the data. + NotModified, +}; + +class HTTPRequestImpl; + +class HTTPCURLContext : public HTTPContext<HTTPCURLContext> { + +public: + HTTPCURLContext(uv_loop_t *loop); + ~HTTPCURLContext(); + + static int handleSocket(CURL *handle, curl_socket_t s, int action, void *userp, void *socketp); + static void perform(uv_poll_t *req, int status, int events); + static int startTimeout(CURLM *multi, long timeout_ms, void *userp); +#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10 + static void onTimeout(uv_timer_t *req, int status); +#else + static void onTimeout(uv_timer_t *req); +#endif + + CURL *getHandle(); + void returnHandle(CURL *handle); + void checkMultiInfo(); + +public: + // Used as the CURL timer function to periodically check for socket updates. + uv_timer_t *timeout = nullptr; + + // CURL multi handle that we use to request multiple URLs at the same time, without having to + // block and spawn threads. + CURLM *multi = nullptr; + + // CURL share handles are used for sharing session state (e.g.) + CURLSH *share = nullptr; + + // A queue that we use for storing resuable CURL easy handles to avoid creating and destroying + // them all the time. + std::queue<CURL *> handles; +}; + + +class HTTPRequestImpl { + MBGL_STORE_THREAD(tid) + +public: + HTTPRequestImpl(HTTPRequest *request, uv_loop_t *loop, std::unique_ptr<Response> response); + ~HTTPRequestImpl(); + + void handleResult(CURLcode code); + void abandon(); + void retryImmediately(); + +private: + static size_t headerCallback(char *const buffer, const size_t size, const size_t nmemb, void *userp); + static size_t writeCallback(void *const contents, const size_t size, const size_t nmemb, void *userp); + + void retry(uint64_t timeout); + static void restart(uv_timer_t *timer, int); + void finish(ResponseStatus status); + void start(); + +private: + HTTPCURLContext *context = nullptr; + HTTPRequest *request = nullptr; + + // Will store the current response. + std::unique_ptr<Response> response; + + // In case of revalidation requests, this will store the old response. + std::unique_ptr<Response> existingResponse; + + CURL *handle = nullptr; + curl_slist *headers = nullptr; + + uv_timer_t *timer = nullptr; + enum : bool { PreemptImmediately, ExponentialBackoff } strategy = PreemptImmediately; + int attempts = 0; + + static const int maxAttempts = 4; +}; + + + +struct Socket { +private: + uv_poll_t poll; + +public: + HTTPCURLContext *context = nullptr; + const curl_socket_t sockfd = 0; + +public: + Socket(HTTPCURLContext *context_, curl_socket_t sockfd_) : context(context_), sockfd(sockfd_) { + assert(context); + uv_poll_init_socket(context->loop, &poll, sockfd); + poll.data = this; + } + + void start(int events, uv_poll_cb cb) { + uv_poll_start(&poll, events, cb); + } + + void stop() { + assert(poll.data); + uv_poll_stop(&poll); + uv_close((uv_handle_t *)&poll, [](uv_handle_t *handle) { + assert(handle->data); + delete reinterpret_cast<Socket *>(handle->data); + }); + } + +private: + // Make the destructor private to ensure that we can only close the Socket + // with stop(), and disallow manual deletion. + ~Socket() = default; +}; + +// ------------------------------------------------------------------------------------------------- + +template<> pthread_key_t HTTPContext<HTTPCURLContext>::key{}; +template<> pthread_once_t HTTPContext<HTTPCURLContext>::once = PTHREAD_ONCE_INIT; + +HTTPCURLContext::HTTPCURLContext(uv_loop_t *loop_) : HTTPContext(loop_) { + if (curl_global_init(CURL_GLOBAL_ALL)) { + throw std::runtime_error("Could not init cURL"); + } + + timeout = new uv_timer_t; + timeout->data = this; + uv_timer_init(loop, timeout); + + share = curl_share_init(); + + multi = curl_multi_init(); + handleError(curl_multi_setopt(multi, CURLMOPT_SOCKETFUNCTION, handleSocket)); + handleError(curl_multi_setopt(multi, CURLMOPT_SOCKETDATA, this)); + handleError(curl_multi_setopt(multi, CURLMOPT_TIMERFUNCTION, startTimeout)); + handleError(curl_multi_setopt(multi, CURLMOPT_TIMERDATA, this)); +} + +HTTPCURLContext::~HTTPCURLContext() { + curl_multi_cleanup(multi); + multi = nullptr; + + curl_share_cleanup(share); + share = nullptr; + + uv_timer_stop(timeout); + uv::close(timeout); +} + +CURL *HTTPCURLContext::getHandle() { + if (!handles.empty()) { + auto handle = handles.front(); + handles.pop(); + return handle; + } else { + return curl_easy_init(); + } +} + +void HTTPCURLContext::returnHandle(CURL *handle) { + curl_easy_reset(handle); + handles.push(handle); +} + +void HTTPCURLContext::checkMultiInfo() { + MBGL_VERIFY_THREAD(tid); + CURLMsg *message = nullptr; + int pending = 0; + + while ((message = curl_multi_info_read(multi, &pending))) { + switch (message->msg) { + case CURLMSG_DONE: { + HTTPRequestImpl *baton = nullptr; + curl_easy_getinfo(message->easy_handle, CURLINFO_PRIVATE, (char *)&baton); + assert(baton); + baton->handleResult(message->data.result); + } break; + + default: + // This should never happen, because there are no other message types. + throw std::runtime_error("CURLMsg returned unknown message type"); + } + } +} + +void HTTPCURLContext::perform(uv_poll_t *req, int /* status */, int events) { + assert(req->data); + auto socket = reinterpret_cast<Socket *>(req->data); + auto context = socket->context; + MBGL_VERIFY_THREAD(context->tid); + + int flags = 0; + + if (events & UV_READABLE) { + flags |= CURL_CSELECT_IN; + } + if (events & UV_WRITABLE) { + flags |= CURL_CSELECT_OUT; + } + + + int running_handles = 0; + curl_multi_socket_action(context->multi, socket->sockfd, flags, &running_handles); + context->checkMultiInfo(); +} + +int HTTPCURLContext::handleSocket(CURL * /* handle */, curl_socket_t s, int action, void *userp, + void *socketp) { + auto socket = reinterpret_cast<Socket *>(socketp); + assert(userp); + auto context = reinterpret_cast<HTTPCURLContext *>(userp); + MBGL_VERIFY_THREAD(context->tid); + + if (!socket && action != CURL_POLL_REMOVE) { + socket = new Socket(context, s); + curl_multi_assign(context->multi, s, (void *)socket); + } + + switch (action) { + case CURL_POLL_IN: + socket->start(UV_READABLE, perform); + break; + case CURL_POLL_OUT: + socket->start(UV_WRITABLE, perform); + break; + case CURL_POLL_REMOVE: + if (socket) { + socket->stop(); + curl_multi_assign(context->multi, s, nullptr); + } + break; + default: + throw std::runtime_error("Unhandled CURL socket action"); + } + + return 0; +} + +#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10 +void HTTPCURLContext::onTimeout(uv_timer_t *req, int /* status */) { +#else +void HTTPCURLContext::onTimeout(uv_timer_t *req) { +#endif + assert(req->data); + auto context = reinterpret_cast<HTTPCURLContext *>(req->data); + MBGL_VERIFY_THREAD(context->tid); + int running_handles; + CURLMcode error = curl_multi_socket_action(context->multi, CURL_SOCKET_TIMEOUT, 0, &running_handles); + if (error != CURLM_OK) { + throw std::runtime_error(std::string("CURL multi error: ") + curl_multi_strerror(error)); + } + context->checkMultiInfo(); +} + +int HTTPCURLContext::startTimeout(CURLM * /* multi */, long timeout_ms, void *userp) { + assert(userp); + auto context = reinterpret_cast<HTTPCURLContext *>(userp); + MBGL_VERIFY_THREAD(context->tid); + if (timeout_ms < 0) { + // A timeout of 0 ms means that the timer will invoked in the next loop iteration. + timeout_ms = 0; + } + uv_timer_stop(context->timeout); + uv_timer_start(context->timeout, onTimeout, timeout_ms, 0); + return 0; +} + +// ------------------------------------------------------------------------------------------------- + +HTTPRequestImpl::HTTPRequestImpl(HTTPRequest *request_, uv_loop_t *loop, std::unique_ptr<Response> response_) + : context(HTTPCURLContext::Get(loop)), + request(request_), + existingResponse(std::move(response_)), + handle(context->getHandle()) { + assert(request); + context->addRequest(request); + + // If there's already a response, set the correct etags/modified headers to make sure we are + // getting a 304 response if possible. This avoids redownloading unchanged data. + if (existingResponse) { + if (!existingResponse->etag.empty()) { + const std::string header = std::string("If-None-Match: ") + existingResponse->etag; + headers = curl_slist_append(headers, header.c_str()); + } else if (existingResponse->modified) { + const std::string time = + std::string("If-Modified-Since: ") + util::rfc1123(existingResponse->modified); + headers = curl_slist_append(headers, time.c_str()); + } + } + + if (headers) { + curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers); + } + + handleError(curl_easy_setopt(handle, CURLOPT_PRIVATE, this)); + handleError(curl_easy_setopt(handle, CURLOPT_CAINFO, "ca-bundle.crt")); + handleError(curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1)); + handleError(curl_easy_setopt(handle, CURLOPT_URL, request->resource.url.c_str())); + handleError(curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, writeCallback)); + handleError(curl_easy_setopt(handle, CURLOPT_WRITEDATA, this)); + handleError(curl_easy_setopt(handle, CURLOPT_HEADERFUNCTION, headerCallback)); + handleError(curl_easy_setopt(handle, CURLOPT_HEADERDATA, this)); + handleError(curl_easy_setopt(handle, CURLOPT_ACCEPT_ENCODING, "gzip, deflate")); + handleError(curl_easy_setopt(handle, CURLOPT_USERAGENT, "MapboxGL/1.0")); + handleError(curl_easy_setopt(handle, CURLOPT_SHARE, context->share)); + + start(); +} + +void HTTPRequestImpl::abandon() { + if (request) { + context->removeRequest(request); + request = nullptr; + } +} + +void HTTPRequestImpl::start() { + // Count up the attempts. + attempts++; + + // Start requesting the information. + handleError(curl_multi_add_handle(context->multi, handle)); +} + +HTTPRequestImpl::~HTTPRequestImpl() { + MBGL_VERIFY_THREAD(tid); + + if (request) { + context->removeRequest(request); + request->ptr = nullptr; + } + + handleError(curl_multi_remove_handle(context->multi, handle)); + context->returnHandle(handle); + handle = nullptr; + + if (timer) { + // Stop the backoff timer to avoid re-triggering this request. + uv_timer_stop(timer); + uv::close(timer); + timer = nullptr; + } + + if (headers) { + curl_slist_free_all(headers); + headers = nullptr; + } +} + +// This function is called when we have new data for a request. We just append it to the string +// containing the previous data. +size_t HTTPRequestImpl::writeCallback(void *const contents, const size_t size, const size_t nmemb, void *userp) { + assert(userp); + auto impl = reinterpret_cast<HTTPRequestImpl *>(userp); + MBGL_VERIFY_THREAD(impl->tid); + + if (!impl->response) { + impl->response = util::make_unique<Response>(); + } + + impl->response->data.append((char *)contents, size * nmemb); + return size * nmemb; +} + +// Compares the beginning of the (non-zero-terminated!) data buffer with the (zero-terminated!) +// header string. If the data buffer contains the header string at the beginning, it returns +// the length of the header string == begin of the value, otherwise it returns npos. +// The comparison of the header is ASCII-case-insensitive. +size_t headerMatches(const char *const header, const char *const buffer, const size_t length) { + const size_t headerLength = strlen(header); + if (length < headerLength) { + return std::string::npos; + } + size_t i = 0; + while (i < length && i < headerLength && std::tolower(buffer[i]) == std::tolower(header[i])) { + i++; + } + return i == headerLength ? i : std::string::npos; +} + +int64_t 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) { + return std::chrono::duration_cast<std::chrono::seconds>( + std::chrono::system_clock::now().time_since_epoch()).count() + + seconds; + } + } + + return 0; +} + +size_t HTTPRequestImpl::headerCallback(char *const buffer, const size_t size, const size_t nmemb, void *userp) { + assert(userp); + auto baton = reinterpret_cast<HTTPRequestImpl *>(userp); + MBGL_VERIFY_THREAD(baton->tid); + + if (!baton->response) { + baton->response = util::make_unique<Response>(); + } + + const size_t length = size * nmemb; + size_t begin = std::string::npos; + if ((begin = headerMatches("last-modified: ", buffer, length)) != std::string::npos) { + // Always overwrite the modification date; We might already have a value here from the + // Date header, but this one is more accurate. + const std::string value { buffer + begin, length - begin - 2 }; // remove \r\n + baton->response->modified = curl_getdate(value.c_str(), nullptr); + } else if ((begin = headerMatches("etag: ", buffer, length)) != std::string::npos) { + baton->response->etag = { buffer + begin, length - begin - 2 }; // remove \r\n + } else if ((begin = headerMatches("cache-control: ", buffer, length)) != std::string::npos) { + const std::string value { buffer + begin, length - begin - 2 }; // remove \r\n + baton->response->expires = parseCacheControl(value.c_str()); + } else if ((begin = headerMatches("expires: ", buffer, length)) != std::string::npos) { + const std::string value { buffer + begin, length - begin - 2 }; // remove \r\n + baton->response->expires = curl_getdate(value.c_str(), nullptr); + } + + return length; +} + + +void HTTPRequestImpl::retry(uint64_t timeout) { + handleError(curl_multi_remove_handle(context->multi, handle)); + + response.reset(); + + assert(!timer); + timer = new uv_timer_t; + timer->data = this; + uv_timer_init(context->loop, timer); + uv_timer_start(timer, restart, timeout, 0); +} + +void HTTPRequestImpl::retryImmediately() { + // All batons get notified when the network status changed, but some of them + // might not actually wait for the network to become available again. + if (timer && strategy == PreemptImmediately) { + // Triggers the timer upon the next event loop iteration. + uv_timer_stop(timer); + uv_timer_start(timer, restart, 0, 0); + } +} + +void HTTPRequestImpl::restart(uv_timer_t *timer, int) { + // Restart the request. + auto baton = reinterpret_cast<HTTPRequestImpl *>(timer->data); + + // Get rid of the timer. + baton->timer = nullptr; + uv::close(timer); + + baton->start(); +} + +void HTTPRequestImpl::finish(ResponseStatus status) { + if (status == ResponseStatus::TemporaryError && attempts < maxAttempts) { + strategy = ExponentialBackoff; + return retry((1 << (attempts - 1)) * 1000); + } else if (status == ResponseStatus::ConnectionError && attempts < maxAttempts) { + // By default, we will retry every 30 seconds (network change notification will + // preempt the timeout). + strategy = PreemptImmediately; + return retry(30000); + } + + // Actually return the response. + if (status == ResponseStatus::NotModified) { + request->notify(std::move(response), FileCache::Hint::Refresh); + } else { + request->notify(std::move(response), FileCache::Hint::Full); + } + + delete request; + delete this; +} + +void HTTPRequestImpl::handleResult(CURLcode code) { + MBGL_VERIFY_THREAD(tid); + + if (!request) { + // In this case, it doesn't make sense to even process the response even further since + // the request was canceled anyway. + delete this; + return; + } + + // Make sure a response object exists in case we haven't got any headers + // or content. + if (!response) { + response = util::make_unique<Response>(); + } + + // Add human-readable error code + if (code != CURLE_OK) { + response->message = curl_easy_strerror(code); + + switch (code) { + case CURLE_COULDNT_RESOLVE_PROXY: + case CURLE_COULDNT_RESOLVE_HOST: + case CURLE_COULDNT_CONNECT: + response->status = Response::Error; + return finish(ResponseStatus::ConnectionError); + + case CURLE_OPERATION_TIMEDOUT: + response->status = Response::Error; + return finish(ResponseStatus::TemporaryError); + + default: + response->status = Response::Error; + return finish(ResponseStatus::PermanentError); + } + } else { + long responseCode = 0; + curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &responseCode); + + if (responseCode == 304) { + if (existingResponse) { + // We're going to reuse the old response object, but need to copy over the new + // expires value (if possible). + std::swap(response, existingResponse); + if (existingResponse->expires) { + response->expires = existingResponse->expires; + } + return finish(ResponseStatus::NotModified); + } else { + // This is an unsolicited 304 response and should only happen on malfunctioning + // HTTP servers. It likely doesn't include any data, but we don't have much options. + response->status = Response::Successful; + return finish(ResponseStatus::Successful); + } + } else if (responseCode == 200) { + response->status = Response::Successful; + return finish(ResponseStatus::Successful); + } else if (responseCode >= 500 && responseCode < 600) { + // Server errors may be temporary, so back off exponentially. + response->status = Response::Error; + response->message = "HTTP status code " + std::to_string(responseCode); + return finish(ResponseStatus::TemporaryError); + } else { + // We don't know how to handle any other errors, so declare them as permanently failing. + response->status = Response::Error; + response->message = "HTTP status code " + std::to_string(responseCode); + return finish(ResponseStatus::PermanentError); + } + } + + throw std::runtime_error("Response hasn't been handled"); +} + +// ------------------------------------------------------------------------------------------------- + +HTTPRequest::HTTPRequest(DefaultFileSource *source, const Resource &resource) + : SharedRequestBase(source, resource) { +} + +HTTPRequest::~HTTPRequest() { + MBGL_VERIFY_THREAD(tid); + + if (ptr) { + reinterpret_cast<HTTPRequestImpl *>(ptr)->abandon(); + } +} + +void HTTPRequest::start(uv_loop_t *loop, std::unique_ptr<Response> response) { + MBGL_VERIFY_THREAD(tid); + + assert(!ptr); + ptr = new HTTPRequestImpl(this, loop, std::move(response)); +} + +void HTTPRequest::retryImmediately() { + MBGL_VERIFY_THREAD(tid); + + if (ptr) { + reinterpret_cast<HTTPRequestImpl *>(ptr)->retryImmediately(); + } +} + +void HTTPRequest::cancel() { + MBGL_VERIFY_THREAD(tid); + + if (ptr) { + delete reinterpret_cast<HTTPRequestImpl *>(ptr); + ptr = nullptr; + } + + delete this; +} + +} diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp index 3359dd317f..112499a485 100644 --- a/src/mbgl/map/map.cpp +++ b/src/mbgl/map/map.cpp @@ -24,6 +24,7 @@ #include <mbgl/platform/log.hpp> #include <mbgl/util/string.hpp> #include <mbgl/util/uv.hpp> +#include <mbgl/util/mapbox.hpp> #include <algorithm> #include <iostream> @@ -127,9 +128,7 @@ Map::~Map() { } uv::worker &Map::getWorker() { - if (!workers) { - workers = util::make_unique<uv::worker>(**loop, 4, "Tile Worker"); - } + assert(workers); return *workers; } @@ -153,8 +152,6 @@ void Map::start(bool startPaused) { workers.reset(); activeSources.clear(); - fileSource.clearLoop(); - terminating = true; // Closes all open handles on the loop. This means that the loop will automatically terminate. @@ -195,6 +192,8 @@ void Map::start(bool startPaused) { pthread_setname_np("Map"); #endif + workers = util::make_unique<uv::worker>(**loop, 4, "Tile Worker"); + run(); #ifndef NDEBUG @@ -301,7 +300,6 @@ void Map::run() { mapThread = std::thread::id(); #endif mode = Mode::None; - fileSource.clearLoop(); } view.deactivate(); @@ -386,13 +384,14 @@ void Map::setStyleJSON(std::string newStyleJSON, const std::string &base) { style = std::make_shared<Style>(); } + style->base = base; style->loadJSON((const uint8_t *)styleJSON.c_str()); style->cascadeClasses(classes); - fileSource.setBase(base); - glyphStore->setURL(style->glyph_url); - style->setDefaultTransitionDuration(defaultTransitionDuration); + const std::string glyphURL = util::mapbox::normalizeGlyphsURL(style->glyph_url, getAccessToken()); + glyphStore->setURL(glyphURL); + update(); } @@ -559,6 +558,15 @@ void Map::stopRotating() { update(); } +#pragma mark - Access Token + +void Map::setAccessToken(const std::string &token) { + accessToken = token; +} + +const std::string &Map::getAccessToken() const { + return accessToken; +} #pragma mark - Toggles @@ -674,20 +682,16 @@ void Map::updateTiles() { source->source->update(*this, getWorker(), style, *glyphAtlas, *glyphStore, *spriteAtlas, getSprite(), - *texturePool, fileSource, [this](){ update(); }); + *texturePool, fileSource, ***loop, [this](){ update(); }); } } void Map::prepare() { - if (!fileSource.hasLoop()) { - fileSource.setLoop(**loop); - } - if (!style) { style = std::make_shared<Style>(); - fileSource.request(ResourceType::JSON, styleURL)->onload([&](const Response &res) { - if (res.code == 200) { + fileSource.request({ Resource::Kind::JSON, styleURL}, **loop, [&](const Response &res) { + if (res.status == Response::Successful) { // Calculate the base const size_t pos = styleURL.rfind('/'); std::string base = ""; @@ -697,7 +701,7 @@ void Map::prepare() { setStyleJSON(res.data, base); } else { - Log::Error(Event::Setup, "loading style failed: %ld (%s)", res.code, res.message.c_str()); + Log::Error(Event::Setup, "loading style failed: %s", res.message.c_str()); } }); } diff --git a/src/mbgl/map/raster_tile_data.cpp b/src/mbgl/map/raster_tile_data.cpp index 6fac7862e7..84e9bb236a 100644 --- a/src/mbgl/map/raster_tile_data.cpp +++ b/src/mbgl/map/raster_tile_data.cpp @@ -5,8 +5,8 @@ using namespace mbgl; -RasterTileData::RasterTileData(Tile::ID const& id_, TexturePool& texturePool, const SourceInfo& source_) - : TileData(id_, source_), +RasterTileData::RasterTileData(Tile::ID const& id_, TexturePool& texturePool, const SourceInfo& source_, FileSource& fileSource_) + : TileData(id_, source_, fileSource_), bucket(texturePool, properties) { } diff --git a/src/mbgl/map/raster_tile_data.hpp b/src/mbgl/map/raster_tile_data.hpp index 42070d9c61..7f338056f5 100644 --- a/src/mbgl/map/raster_tile_data.hpp +++ b/src/mbgl/map/raster_tile_data.hpp @@ -16,7 +16,7 @@ class RasterTileData : public TileData { friend class TileParser; public: - RasterTileData(Tile::ID const& id, TexturePool&, const SourceInfo&); + RasterTileData(Tile::ID const& id, TexturePool&, const SourceInfo&, FileSource &); ~RasterTileData(); virtual void parse(); diff --git a/src/mbgl/map/source.cpp b/src/mbgl/map/source.cpp index 798cd41d1d..f23bcaa14a 100644 --- a/src/mbgl/map/source.cpp +++ b/src/mbgl/map/source.cpp @@ -15,6 +15,7 @@ #include <mbgl/geometry/glyph_atlas.hpp> #include <mbgl/style/style_layer.hpp> #include <mbgl/platform/log.hpp> +#include <mbgl/util/uv_detail.hpp> #include <mbgl/map/vector_tile_data.hpp> #include <mbgl/map/raster_tile_data.hpp> @@ -39,8 +40,9 @@ void Source::load(Map& map, FileSource& fileSource) { util::ptr<Source> source = shared_from_this(); - fileSource.request(ResourceType::JSON, info.url)->onload([source, &map](const Response &res) { - if (res.code != 200) { + const std::string url = util::mapbox::normalizeSourceURL(info.url, map.getAccessToken()); + fileSource.request({ Resource::Kind::JSON, url }, **map.loop, [source, &map](const Response &res) { + if (res.status != Response::Successful) { Log::Warning(Event::General, "failed to load source TileJSON"); return; } @@ -155,7 +157,7 @@ TileData::State Source::addTile(Map& map, uv::worker& worker, util::ptr<Style> style, GlyphAtlas& glyphAtlas, GlyphStore& glyphStore, SpriteAtlas& spriteAtlas, util::ptr<Sprite> sprite, - FileSource& fileSource, TexturePool& texturePool, + FileSource& fileSource, uv_loop_t &loop, TexturePool& texturePool, const Tile::ID& id, std::function<void ()> callback) { const TileData::State state = hasTile(id); @@ -188,14 +190,14 @@ TileData::State Source::addTile(Map& map, uv::worker& worker, new_tile.data = std::make_shared<VectorTileData>(normalized_id, map.getMaxZoom(), style, glyphAtlas, glyphStore, spriteAtlas, sprite, - texturePool, info); + texturePool, info, fileSource); } else if (info.type == SourceType::Raster) { - new_tile.data = std::make_shared<RasterTileData>(normalized_id, texturePool, info); + new_tile.data = std::make_shared<RasterTileData>(normalized_id, texturePool, info, fileSource); } else { throw std::runtime_error("source type not implemented"); } - new_tile.data->request(worker, fileSource, map.getState().getPixelRatio(), callback); + new_tile.data->request(worker, loop, map.getState().getPixelRatio(), callback); tile_data.emplace(new_tile.data->id, new_tile.data); } @@ -286,7 +288,7 @@ void Source::update(Map& map, uv::worker& worker, util::ptr<Style> style, GlyphAtlas& glyphAtlas, GlyphStore& glyphStore, SpriteAtlas& spriteAtlas, util::ptr<Sprite> sprite, - TexturePool& texturePool, FileSource& fileSource, + TexturePool& texturePool, FileSource& fileSource, uv_loop_t& loop, std::function<void ()> callback) { if (!loaded || map.getTime() <= updated) return; @@ -310,7 +312,7 @@ void Source::update(Map& map, uv::worker& worker, const TileData::State state = addTile(map, worker, style, glyphAtlas, glyphStore, spriteAtlas, sprite, - fileSource, texturePool, + fileSource, loop, texturePool, id, callback); if (state != TileData::State::parsed) { diff --git a/src/mbgl/map/source.hpp b/src/mbgl/map/source.hpp index f0023afa09..3649837a58 100644 --- a/src/mbgl/map/source.hpp +++ b/src/mbgl/map/source.hpp @@ -39,7 +39,7 @@ public: util::ptr<Style>, GlyphAtlas&, GlyphStore&, SpriteAtlas&, util::ptr<Sprite>, - TexturePool&, FileSource&, + TexturePool&, FileSource&, uv_loop_t& loop, std::function<void ()> callback); void updateMatrices(const mat4 &projMatrix, const TransformState &transform); @@ -63,7 +63,7 @@ private: util::ptr<Style>, GlyphAtlas&, GlyphStore&, SpriteAtlas&, util::ptr<Sprite>, - FileSource&, TexturePool&, + FileSource&, uv_loop_t &, TexturePool&, const Tile::ID&, std::function<void ()> callback); diff --git a/src/mbgl/map/sprite.cpp b/src/mbgl/map/sprite.cpp index c1f71e59d9..a3e264b762 100644 --- a/src/mbgl/map/sprite.cpp +++ b/src/mbgl/map/sprite.cpp @@ -52,6 +52,7 @@ Sprite::operator bool() const { // The reason this isn't part of the constructor is that calling shared_from_this() in // the constructor fails. void Sprite::load(FileSource& fileSource) { + if (!valid) { // Treat a non-existent sprite as a successfully loaded empty sprite. loadedImage = true; @@ -62,26 +63,26 @@ void Sprite::load(FileSource& fileSource) { util::ptr<Sprite> sprite = shared_from_this(); - fileSource.request(ResourceType::JSON, jsonURL)->onload([sprite](const Response &res) { - if (res.code == 200) { + fileSource.request({ Resource::Kind::JSON, jsonURL }, [sprite](const Response &res) { + if (res.status == Response::Successful) { sprite->body = res.data; sprite->parseJSON(); sprite->complete(); } else { - Log::Warning(Event::Sprite, "Failed to load sprite info: Error %d: %s", res.code, res.message.c_str()); + Log::Warning(Event::Sprite, "Failed to load sprite info: %s", res.message.c_str()); if (!sprite->future.valid()) { sprite->promise.set_exception(std::make_exception_ptr(std::runtime_error(res.message))); } } }); - fileSource.request(ResourceType::Image, spriteURL)->onload([sprite](const Response &res) { - if (res.code == 200) { + fileSource.request({ Resource::Kind::Image, spriteURL }, [sprite](const Response &res) { + if (res.status == Response::Successful) { sprite->image = res.data; sprite->parseImage(); sprite->complete(); } else { - Log::Warning(Event::Sprite, "Failed to load sprite image: Error %d: %s", res.code, res.message.c_str()); + Log::Warning(Event::Sprite, "Failed to load sprite image: Error %s", res.message.c_str()); if (!sprite->future.valid()) { sprite->promise.set_exception(std::make_exception_ptr(std::runtime_error(res.message))); } diff --git a/src/mbgl/map/tile_data.cpp b/src/mbgl/map/tile_data.cpp index f89ff15baf..9d48041239 100644 --- a/src/mbgl/map/tile_data.cpp +++ b/src/mbgl/map/tile_data.cpp @@ -6,29 +6,33 @@ #include <mbgl/util/string.hpp> #include <mbgl/storage/file_source.hpp> #include <mbgl/util/uv_detail.hpp> +#include <mbgl/platform/log.hpp> using namespace mbgl; -TileData::TileData(Tile::ID const& id_, const SourceInfo& source_) +TileData::TileData(Tile::ID const& id_, const SourceInfo& source_, FileSource& fileSource_) : id(id_), name(id), state(State::initial), source(source_), + fileSource(fileSource_), debugBucket(debugFontBuffer) { // Initialize tile debug coordinates debugFontBuffer.addText(name.c_str(), 50, 200, 5); } TileData::~TileData() { - cancel(); + if (req) { + fileSource.cancel(req); + } } const std::string TileData::toString() const { return std::string { "[tile " } + name + "]"; } -void TileData::request(uv::worker& worker, FileSource& fileSource, - float pixelRatio, std::function<void ()> callback) { +void TileData::request(uv::worker &worker, uv_loop_t &loop, + float pixelRatio, std::function<void()> callback) { if (source.tiles.empty()) return; @@ -51,8 +55,7 @@ void TileData::request(uv::worker& worker, FileSource& fileSource, // Note: Somehow this feels slower than the change to request_http() std::weak_ptr<TileData> weak_tile = shared_from_this(); - req = fileSource.request(ResourceType::Tile, url); - req->onload([weak_tile, url, callback, &worker](const Response &res) { + req = fileSource.request({ Resource::Kind::Tile, url }, &loop, [weak_tile, url, callback, &worker](const Response &res) { util::ptr<TileData> tile = weak_tile.lock(); if (!tile || tile->state == State::obsolete) { // noop. Tile is obsolete and we're now just waiting for the refcount @@ -61,9 +64,9 @@ void TileData::request(uv::worker& worker, FileSource& fileSource, } // Clear the request object. - tile->req.reset(); + tile->req = nullptr; - if (res.code == 200) { + if (res.status == Response::Successful) { tile->state = State::loaded; tile->data = res.data; @@ -71,9 +74,7 @@ void TileData::request(uv::worker& worker, FileSource& fileSource, // Schedule tile parsing in another thread tile->reparse(worker, callback); } else { -#if defined(DEBUG) - fprintf(stderr, "[%s] tile loading failed: %ld, %s\n", url.c_str(), res.code, res.message.c_str()); -#endif + Log::Error(Event::HttpRequest, "[%s] tile loading failed: %s", url.c_str(), res.message.c_str()); } }); } @@ -81,10 +82,10 @@ void TileData::request(uv::worker& worker, FileSource& fileSource, void TileData::cancel() { if (state != State::obsolete) { state = State::obsolete; - if (req) { - req->cancel(); - req.reset(); - } + } + if (req) { + fileSource.cancel(req); + req = nullptr; } } diff --git a/src/mbgl/map/tile_data.hpp b/src/mbgl/map/tile_data.hpp index 1ae215b204..a83a4648dd 100644 --- a/src/mbgl/map/tile_data.hpp +++ b/src/mbgl/map/tile_data.hpp @@ -18,6 +18,8 @@ namespace uv { class worker; } +typedef struct uv_loop_s uv_loop_t; + namespace mbgl { class Map; @@ -46,10 +48,10 @@ public: }; public: - TileData(Tile::ID const& id, const SourceInfo&); + TileData(Tile::ID const& id, const SourceInfo&, FileSource&); ~TileData(); - void request(uv::worker&, FileSource&, float pixelRatio, std::function<void ()> callback); + void request(uv::worker&, uv_loop_t&, float pixelRatio, std::function<void ()> callback); void reparse(uv::worker&, std::function<void ()> callback); void cancel(); const std::string toString() const; @@ -71,9 +73,10 @@ public: public: const SourceInfo& source; + FileSource& fileSource; protected: - std::unique_ptr<Request> req; + Request *req = nullptr; std::string data; // Contains the tile ID string for painting debug information. diff --git a/src/mbgl/map/vector_tile_data.cpp b/src/mbgl/map/vector_tile_data.cpp index 06782057f6..ca98d2695b 100644 --- a/src/mbgl/map/vector_tile_data.cpp +++ b/src/mbgl/map/vector_tile_data.cpp @@ -5,6 +5,7 @@ #include <mbgl/style/style_layer.hpp> #include <mbgl/style/style_bucket.hpp> #include <mbgl/geometry/glyph_atlas.hpp> +#include <mbgl/platform/log.hpp> using namespace mbgl; @@ -13,8 +14,8 @@ VectorTileData::VectorTileData(Tile::ID const& id_, GlyphAtlas& glyphAtlas_, GlyphStore& glyphStore_, SpriteAtlas& spriteAtlas_, util::ptr<Sprite> sprite_, TexturePool& texturePool_, - const SourceInfo& source_) - : TileData(id_, source_), + const SourceInfo& source_, FileSource &fileSource_) + : TileData(id_, source_, fileSource_), glyphAtlas(glyphAtlas_), glyphStore(glyphStore_), spriteAtlas(spriteAtlas_), @@ -44,10 +45,8 @@ void VectorTileData::parse() { texturePool); parser.parse(); } catch (const std::exception& ex) { -#if defined(DEBUG) - fprintf(stderr, "[%p] exception [%d/%d/%d]... failed: %s\n", this, id.z, id.x, id.y, ex.what()); -#endif - cancel(); + Log::Error(Event::ParseTile, "Parsing [%d/%d/%d] failed: %s", id.z, id.x, id.y, ex.what()); + state = State::obsolete; return; } diff --git a/src/mbgl/map/vector_tile_data.hpp b/src/mbgl/map/vector_tile_data.hpp index b9bf55a1b3..31318003af 100644 --- a/src/mbgl/map/vector_tile_data.hpp +++ b/src/mbgl/map/vector_tile_data.hpp @@ -36,7 +36,7 @@ public: GlyphAtlas&, GlyphStore&, SpriteAtlas&, util::ptr<Sprite>, TexturePool&, - const SourceInfo&); + const SourceInfo&, FileSource &); ~VectorTileData(); virtual void parse(); diff --git a/src/mbgl/storage/base_request.cpp b/src/mbgl/storage/base_request.cpp deleted file mode 100644 index 510bd7bf1c..0000000000 --- a/src/mbgl/storage/base_request.cpp +++ /dev/null @@ -1,87 +0,0 @@ -#include <mbgl/storage/base_request.hpp> -#include <mbgl/storage/response.hpp> -#include <mbgl/storage/request.hpp> -#include <mbgl/util/std.hpp> - -#include <uv.h> - -#include <cassert> - -namespace mbgl { - -template <typename T, typename ...Args> -void invoke(const std::forward_list<std::unique_ptr<Callback>> &list, Args&& ...args) { - for (const std::unique_ptr<Callback> &callback : list) { - assert(callback); - if (callback->is<T>()) { - callback->get<T>()(::std::forward<Args>(args)...); - } - } -} - -BaseRequest::BaseRequest(const std::string &path_) : threadId(std::this_thread::get_id()), path(path_) { -} - -// A base request can only be "canceled" by destroying the object. In that case, we'll have to -// notify all cancel callbacks. -BaseRequest::~BaseRequest() { - assert(std::this_thread::get_id() == threadId); - notify(); -} - -void BaseRequest::retryImmediately() { - // no-op. override in child class. -} - -void BaseRequest::notify() { - assert(std::this_thread::get_id() == threadId); - - // The parameter exists solely so that any calls to ->remove() - // are not going to cause deallocation of this object while this call is in progress. - util::ptr<BaseRequest> retain = self; - - // Swap the lists so that it's safe for callbacks to call ->cancel() - // on the request object, which would modify the list. - const std::forward_list<std::unique_ptr<Callback>> list = std::move(callbacks); - callbacks.clear(); - - if (response) { - invoke<CompletedCallback>(list, *response); - } else { - invoke<AbortedCallback>(list); - } - - self.reset(); -} - -Callback *BaseRequest::add(Callback &&callback, const util::ptr<BaseRequest> &request) { - assert(std::this_thread::get_id() == threadId); - assert(this == request.get()); - - if (response) { - // We already have a response. Notify right away. - if (callback.is<CompletedCallback>()) { - callback.get<CompletedCallback>()(*response); - } else { - // We already know that this request was successful. The AbortedCallback will be discarded - // here since it would never be called. - } - return nullptr; - } else { - self = request; - callbacks.push_front(util::make_unique<Callback>(std::move(callback))); - return callbacks.front().get(); - } -} - -void BaseRequest::remove(Callback *callback) { - assert(std::this_thread::get_id() == threadId); - callbacks.remove_if([=](const std::unique_ptr<Callback> &cb) { - return cb.get() == callback; - }); - if (callbacks.empty()) { - self.reset(); - } -} - -} diff --git a/src/mbgl/storage/base_request.hpp b/src/mbgl/storage/base_request.hpp deleted file mode 100644 index 5119c343e9..0000000000 --- a/src/mbgl/storage/base_request.hpp +++ /dev/null @@ -1,62 +0,0 @@ -#ifndef MBGL_STORAGE_BASE_REQUEST -#define MBGL_STORAGE_BASE_REQUEST - -#include <mbgl/storage/request_callback.hpp> -#include <mbgl/util/ptr.hpp> - -#include <string> -#include <forward_list> -#include <functional> -#include <thread> - -typedef struct uv_loop_s uv_loop_t; -typedef struct uv_async_s uv_async_t; - -namespace mbgl { - -class Response; -class Request; - -class BaseRequest { -private: - // Make noncopyable and immovable - BaseRequest(const BaseRequest &) = delete; - BaseRequest(BaseRequest &&) = delete; - BaseRequest& operator=(const BaseRequest &) = delete; - BaseRequest& operator=(BaseRequest &&) = delete; - -public: - BaseRequest(const std::string &path); - virtual ~BaseRequest(); - - Callback *add(Callback &&callback, const util::ptr<BaseRequest> &request); - void remove(Callback *callback); - - // Must be called by subclasses when a valid Response object is available. It will notify - // all listeners. - void notify(); - - // This function is called when the request ought to be stopped. Any subclass must make sure this - // is also called in its destructor. Calling this function repeatedly must be safe. - // This function must call notify(). - virtual void cancel() = 0; - - // This function is called when the request should be reattempted immediately. This is typically - // reaction to a network status change. - virtual void retryImmediately(); - -public: - const std::thread::id threadId; - const std::string path; - std::unique_ptr<Response> response; - -protected: - // This object may hold a shared_ptr to itself. It does this to prevent destruction of this object - // while a request is in progress. - util::ptr<BaseRequest> self; - std::forward_list<std::unique_ptr<Callback>> callbacks; -}; - -} - -#endif diff --git a/src/mbgl/storage/caching_http_file_source.cpp b/src/mbgl/storage/caching_http_file_source.cpp deleted file mode 100644 index 634a56f9c4..0000000000 --- a/src/mbgl/storage/caching_http_file_source.cpp +++ /dev/null @@ -1,136 +0,0 @@ -#include <mbgl/storage/caching_http_file_source.hpp> -#include <mbgl/storage/asset_request.hpp> -#include <mbgl/storage/http_request.hpp> -#include <mbgl/storage/sqlite_store.hpp> -#include <mbgl/storage/asset_request.hpp> -#include <mbgl/util/uv-messenger.h> -#include <mbgl/util/mapbox.hpp> -#include <mbgl/util/std.hpp> - -#include <uv.h> - -namespace mbgl { - -CachingHTTPFileSource::CachingHTTPFileSource(const std::string &path_) - : path(path_) {} - -CachingHTTPFileSource::~CachingHTTPFileSource() { -} - -void CachingHTTPFileSource::setLoop(uv_loop_t* loop_) { - assert(!loop); - - threadId = std::this_thread::get_id(); - store = !path.empty() ? util::ptr<SQLiteStore>(new SQLiteStore(loop_, path)) : nullptr; - loop = loop_; - queue = new uv_messenger_t; - - uv_messenger_init(loop, queue, [](void *ptr) { - std::unique_ptr<std::function<void()>> fn { reinterpret_cast<std::function<void()> *>(ptr) }; - (*fn)(); - }); - uv_unref((uv_handle_t *)&queue->async); -} - -bool CachingHTTPFileSource::hasLoop() { - return loop; -} - -void CachingHTTPFileSource::clearLoop() { - assert(std::this_thread::get_id() == threadId); - assert(loop); - - uv_messenger_stop(queue, [](uv_messenger_t *msgr) { - delete msgr; - }); - - util::ptr<BaseRequest> req; - - // Send a cancel() message to all requests that we are still holding. - for (const std::pair<std::string, std::weak_ptr<BaseRequest>> &pair : pending) { - if ((req = pair.second.lock())) { - req->cancel(); - } - } - - store.reset(); - - loop = nullptr; -} - -void CachingHTTPFileSource::setBase(std::string value) { - // TODO: Make threadsafe. - base.swap(value); -} - -void CachingHTTPFileSource::setAccessToken(std::string value) { - // TODO: Make threadsafe. - accessToken.swap(value); -} - -std::string CachingHTTPFileSource::getAccessToken() const { - return accessToken; -} - -std::unique_ptr<Request> CachingHTTPFileSource::request(ResourceType type, const std::string& url_) { - assert(std::this_thread::get_id() == threadId); - - std::string url = url_; - - // Make URL absolute. - const size_t separator = url.find("://"); - if (separator == std::string::npos) { - url = base + url; - } - - // Normalize mapbox:// URLs. - switch (type) { - case ResourceType::Glyphs: - url = util::mapbox::normalizeGlyphsURL(url, accessToken); - default: - url = util::mapbox::normalizeSourceURL(url, accessToken); - } - - util::ptr<BaseRequest> req; - - // First, try to find an existing Request object. - auto it = pending.find(url); - if (it != pending.end()) { - req = it->second.lock(); - } - - if (!req) { - 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); - } - - pending.emplace(url, req); - } - - return util::make_unique<Request>(req); -} - -void CachingHTTPFileSource::prepare(std::function<void()> fn) { - if (std::this_thread::get_id() == threadId) { - fn(); - } else { - uv_messenger_send(queue, new std::function<void()>(std::move(fn))); - } -} - -void CachingHTTPFileSource::setReachability(bool reachable) { - if (reachable && loop) { - prepare([this]() { - util::ptr<BaseRequest> req; - for (const std::pair<std::string, std::weak_ptr<BaseRequest>> &pair : pending) { - if ((req = pair.second.lock())) { - req->retryImmediately(); - } - } - }); - } -} - -} diff --git a/src/mbgl/storage/default_file_source.cpp b/src/mbgl/storage/default_file_source.cpp new file mode 100644 index 0000000000..05d87e474b --- /dev/null +++ b/src/mbgl/storage/default_file_source.cpp @@ -0,0 +1,239 @@ +#include <mbgl/storage/default/default_file_source.hpp> +#include <mbgl/storage/default/request.hpp> +#include <mbgl/storage/default/asset_request.hpp> +#include <mbgl/storage/default/http_request.hpp> + +#include <mbgl/storage/response.hpp> + +#include <mbgl/util/async_queue.hpp> +#include <mbgl/util/util.hpp> + +#include <mbgl/util/variant.hpp> + +#include <boost/algorithm/string.hpp> + +#include <thread> +#include <algorithm> +#include <cassert> + + +namespace algo = boost::algorithm; + +namespace mbgl { + +struct DefaultFileSource::ActionDispatcher { + DefaultFileSource &fileSource; + template <typename T> void operator()(T &t) { fileSource.process(t); } +}; + +struct DefaultFileSource::AddRequestAction { + Request *const request; +}; + +struct DefaultFileSource::RemoveRequestAction { + Request *const request; +}; + +struct DefaultFileSource::ResultAction { + const Resource resource; + std::unique_ptr<Response> response; +}; + +struct DefaultFileSource::StopAction { +}; + + +DefaultFileSource::DefaultFileSource(FileCache *cache_) + : loop(uv_loop_new()), + cache(cache_), + queue(new Queue(loop, [this](Action &action) { + mapbox::util::apply_visitor(ActionDispatcher{*this}, action); + })), + thread([this]() { +#ifdef __APPLE__ + pthread_setname_np("FileSource"); +#endif + uv_run(loop, UV_RUN_DEFAULT); + }) { +} + +DefaultFileSource::DefaultFileSource(FileCache *cache_, uv_loop_t *loop_) + : loop(loop_), + cache(cache_), + queue(new Queue(loop, [this](Action &action) { + mapbox::util::apply_visitor(ActionDispatcher{*this}, action); + })) { + // Make sure that the queue doesn't block the loop from exiting. + queue->unref(); +} + +DefaultFileSource::~DefaultFileSource() { + MBGL_VERIFY_THREAD(tid); + + if (thread.joinable()) { + if (queue) { + queue->send(StopAction{ }); + } + thread.join(); + uv_loop_delete(loop); + } else { + // Assume that the loop we received is running in the current thread. + StopAction action {}; + process(action); + } +} + +SharedRequestBase *DefaultFileSource::find(const Resource &resource) { + // We're using a set of pointers here instead of a map between url and SharedRequestBase because + // we need to find the requests both by pointer and by URL. Given that the number of requests + // is generally very small (typically < 10 at a time), hashing by URL incurs too much overhead + // anyway. + const auto it = pending.find(resource); + if (it != pending.end()) { + return it->second; + } + return nullptr; +} + +Request *DefaultFileSource::request(const Resource &resource, uv_loop_t *l, Callback callback) { + auto request = new Request(resource, l, std::move(callback)); + + // This function can be called from any thread. Make sure we're executing the actual call in the + // file source loop by sending it over the queue. It will be processed in processAction(). + queue->send(AddRequestAction{ request }); + return request; +} + +void DefaultFileSource::request(const Resource &resource, Callback callback) { + auto request = new Request(resource, nullptr, std::move(callback)); + + // This function can be called from any thread. Make sure we're executing the actual call in the + // file source loop by sending it over the queue. It will be processed in processAction(). + queue->send(AddRequestAction{ request }); +} + +void DefaultFileSource::cancel(Request *request) { + request->cancel(); + + // This function can be called from any thread. Make sure we're executing the actual call in the + // file source loop by sending it over the queue. It will be processed in processAction(). + queue->send(RemoveRequestAction{ request }); +} + +void DefaultFileSource::process(AddRequestAction &action) { + const Resource &resource = action.request->resource; + + // We're adding a new Request. + SharedRequestBase *sharedRequest = find(resource); + if (!sharedRequest) { + // There is no request for this URL yet. Create a new one and start it. + if (algo::starts_with(resource.url, "asset://")) { + sharedRequest = new AssetRequest(this, resource); + } else { + sharedRequest = new HTTPRequest(this, resource); + } + + // Make sure the loop stays alive when we're not running the file source in it's own thread. + if (!thread.joinable() && pending.empty()) { + queue->ref(); + } + + const bool inserted = pending.emplace(resource, sharedRequest).second; + assert(inserted); + (void (inserted)); // silence unused variable warning on Release builds. + + // But first, we're going to start querying the database if it exists. + if (!cache) { + sharedRequest->start(loop); + } else { + // Otherwise, first check the cache for existing data so that we can potentially + // revalidate the information without having to redownload everything. + cache->get(resource, [this, resource](std::unique_ptr<Response> response) { + queue->send(ResultAction { resource, std::move(response) }); + }); + } + } + sharedRequest->subscribe(action.request); +} + +void DefaultFileSource::process(RemoveRequestAction &action) { + SharedRequestBase *sharedRequest = find(action.request->resource); + if (sharedRequest) { + // If the number of dependent requests of the SharedRequestBase drops to zero, the + // unsubscribe callback triggers the removal of the SharedRequestBase pointer from the list + // of pending requests and initiates cancelation. + sharedRequest->unsubscribe(action.request); + } else { + // There is no request for this URL anymore. Likely, the request already completed + // before we got around to process the cancelation request. + } + + // Send a message back to the requesting thread and notify it that this request has been + // canceled and is now safe to be deleted. + action.request->destruct(); +} + +void DefaultFileSource::process(ResultAction &action) { + SharedRequestBase *sharedRequest = find(action.resource); + if (sharedRequest) { + if (action.response) { + // This entry was stored in the cache. Now determine if we need to revalidate. + const int64_t now = std::chrono::duration_cast<std::chrono::seconds>( + std::chrono::system_clock::now().time_since_epoch()).count(); + if (action.response->expires > now) { + // The response is fresh. We're good to notify the caller. + sharedRequest->notify(std::move(action.response), FileCache::Hint::No); + sharedRequest->cancel(); + return; + } else { + // The cached response is stale. Now run the real request. + sharedRequest->start(loop, std::move(action.response)); + } + } else { + // There is no response. Now run the real request. + sharedRequest->start(loop); + } + } else { + // There is no request for this URL anymore. Likely, the request was canceled + // before we got around to process the cache result. + } +} + +void DefaultFileSource::process(StopAction &) { + // Cancel all remaining requests. + for (auto it : pending) { + it.second->unsubscribeAll(); + } + pending.clear(); + + assert(queue); + queue->stop(); + queue = nullptr; +} + +void DefaultFileSource::notify(SharedRequestBase *sharedRequest, + const std::set<Request *> &observers, + std::shared_ptr<const Response> response, FileCache::Hint hint) { + // First, remove the request, since it might be destructed at any point now. + assert(find(sharedRequest->resource) == sharedRequest); + pending.erase(sharedRequest->resource); + + if (response) { + if (cache) { + // Store response in database + cache->put(sharedRequest->resource, response, hint); + } + + // Notify all observers. + for (auto it : observers) { + it->notify(response); + } + } + + if (!thread.joinable() && pending.empty()) { + // When there are no pending requests, we're going to allow the queue to stop. + queue->unref(); + } +} + +} diff --git a/src/mbgl/storage/http_request.cpp b/src/mbgl/storage/http_request.cpp deleted file mode 100644 index 57e6c260ef..0000000000 --- a/src/mbgl/storage/http_request.cpp +++ /dev/null @@ -1,280 +0,0 @@ -#include <mbgl/storage/http_request.hpp> -#include <mbgl/storage/sqlite_store.hpp> -#include <mbgl/storage/http_request_baton.hpp> -#include <mbgl/platform/log.hpp> - -#include <uv.h> - -#include <cassert> -#include <chrono> - -namespace mbgl { - -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wdisabled-macro-expansion" -#pragma clang diagnostic ignored "-Wexit-time-destructors" -#pragma clang diagnostic ignored "-Wglobal-constructors" - -struct CacheRequestBaton { - HTTPRequest *request = nullptr; - std::string path; - util::ptr<SQLiteStore> store; -}; - -HTTPRequest::HTTPRequest(ResourceType type_, const std::string &path_, uv_loop_t *loop_, util::ptr<SQLiteStore> store_) - : BaseRequest(path_), threadId(std::this_thread::get_id()), loop(loop_), store(store_), type(type_) { - if (store) { - startCacheRequest(); - } else { - startHTTPRequest(nullptr); - } -} - -void HTTPRequest::startCacheRequest() { - assert(std::this_thread::get_id() == threadId); - - cacheBaton = new CacheRequestBaton; - cacheBaton->request = this; - cacheBaton->path = path; - cacheBaton->store = store; - store->get(path, [](std::unique_ptr<Response> &&response_, void *ptr) { - // Wrap in a unique_ptr, so it'll always get auto-destructed. - std::unique_ptr<CacheRequestBaton> baton((CacheRequestBaton *)ptr); - if (baton->request) { - baton->request->cacheBaton = nullptr; - baton->request->handleCacheResponse(std::move(response_)); - } - }, cacheBaton); -} - -void HTTPRequest::handleCacheResponse(std::unique_ptr<Response> &&res) { - assert(std::this_thread::get_id() == threadId); - - if (res) { - // This entry was stored in the cache. Now determine if we need to revalidate. - const int64_t now = std::chrono::duration_cast<std::chrono::seconds>( - std::chrono::system_clock::now().time_since_epoch()).count(); - if (res->expires > now) { - response = std::move(res); - notify(); - // Note: after calling notify(), the request object may cease to exist. - // This HTTPRequest is completed. - return; - } else { - // TODO: notify with preliminary results. - } - } - - startHTTPRequest(std::move(res)); -} - -void HTTPRequest::startHTTPRequest(std::unique_ptr<Response> &&res) { - assert(std::this_thread::get_id() == threadId); - assert(!httpBaton); - - httpBaton = std::make_shared<HTTPRequestBaton>(path); - httpBaton->request = this; - httpBaton->async = new uv_async_t; - httpBaton->response = std::move(res); - httpBaton->async->data = new util::ptr<HTTPRequestBaton>(httpBaton); - -#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10 - uv_async_init(loop, httpBaton->async, [](uv_async_t *async, int) { -#else - uv_async_init(loop, httpBaton->async, [](uv_async_t *async) { -#endif - util::ptr<HTTPRequestBaton> &baton = *(util::ptr<HTTPRequestBaton> *)async->data; - - if (baton->request) { - HTTPRequest *request = baton->request; - request->httpBaton.reset(); - baton->request = nullptr; - request->handleHTTPResponse(baton->type, std::move(baton->response)); - } - - delete (util::ptr<HTTPRequestBaton> *)async->data; - uv_close((uv_handle_t *)async, [](uv_handle_t *handle) { - uv_async_t *async_handle = (uv_async_t *)handle; - delete async_handle; - }); - }); - attempts++; - HTTPRequestBaton::start(httpBaton); -} - - - -void HTTPRequest::handleHTTPResponse(HTTPResponseType responseType, std::unique_ptr<Response> &&res) { - assert(std::this_thread::get_id() == threadId); - assert(!httpBaton); - assert(!response); - - switch (responseType) { - // This error was caused by a temporary error and it is likely that it will be resolved - // immediately. We are going to try again right away. This is like the TemporaryError, - // except that we will not perform exponential back-off. - case HTTPResponseType::SingularError: - if (attempts >= 4) { - // Report as error after 4 attempts. - response = std::move(res); - notify(); - } else if (attempts >= 2) { - // Switch to the back-off algorithm after the second failure. - retryHTTPRequest(std::move(res), (1 << attempts) * 1000); - return; - } else { - startHTTPRequest(std::move(res)); - } - break; - - // This error might be resolved by waiting some time (e.g. server issues). - // We are going to do an exponential back-off and will try again in a few seconds. - case HTTPResponseType::TemporaryError: - if (attempts >= 4) { - // Report error back after it failed completely. - response = std::move(res); - notify(); - } else { - retryHTTPRequest(std::move(res), (1 << attempts) * 1000); - } - break; - - // This error might be resolved once the network reachability status changes. - // We are going to watch the network status for changes and will retry as soon as the - // operating system notifies us of a network status change. - case HTTPResponseType::ConnectionError: - - if (attempts >= 4) { - // Report error back after it failed completely. - response = std::move(res); - notify(); - } else { - // By default, we will retry every 60 seconds. - retryHTTPRequest(std::move(res), 60000); - } - break; - - // The request was canceled programatically. - case HTTPResponseType::Canceled: - response.reset(); - notify(); - break; - - // This error probably won't be resolved by retrying anytime soon. We are giving up. - case HTTPResponseType::PermanentError: - response = std::move(res); - notify(); - break; - - // The request returned data successfully. We retrieved and decoded the data successfully. - case HTTPResponseType::Successful: - if (store) { - store->put(path, type, *res); - } - response = std::move(res); - notify(); - break; - - // The request confirmed that the data wasn't changed. We already have the data. - case HTTPResponseType::NotModified: - if (store) { - store->updateExpiration(path, res->expires); - } - response = std::move(res); - notify(); - break; - - default: - assert(!"Response wasn't set"); - break; - } -} - -using RetryBaton = std::pair<HTTPRequest *, std::unique_ptr<Response>>; - -void HTTPRequest::retryHTTPRequest(std::unique_ptr<Response> &&res, uint64_t timeout) { - assert(std::this_thread::get_id() == threadId); - assert(!backoffTimer); - backoffTimer = new uv_timer_t(); - uv_timer_init(loop, backoffTimer); - backoffTimer->data = new RetryBaton(this, std::move(res)); - -#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10 - uv_timer_start(backoffTimer, [](uv_timer_t *timer, int) { -#else - uv_timer_start(backoffTimer, [](uv_timer_t *timer) { -#endif - std::unique_ptr<RetryBaton> pair { static_cast<RetryBaton *>(timer->data) }; - pair->first->startHTTPRequest(std::move(pair->second)); - pair->first->backoffTimer = nullptr; - uv_timer_stop(timer); - uv_close((uv_handle_t *)timer, [](uv_handle_t *handle) { delete (uv_timer_t *)handle; }); - }, timeout, 0); -} - -void HTTPRequest::removeHTTPBaton() { - assert(std::this_thread::get_id() == threadId); - if (httpBaton) { - httpBaton->request = nullptr; - HTTPRequestBaton::stop(httpBaton); - httpBaton.reset(); - } -} - -void HTTPRequest::removeCacheBaton() { - assert(std::this_thread::get_id() == threadId); - if (cacheBaton) { - // Make sre that this object doesn't accidentally get accessed when it is destructed before - // the callback returned. They are being run in the same thread, so just setting it to - // null is sufficient. - // Note: We don't manually delete the CacheRequestBaton since it'll be deleted by the - // callback. - cacheBaton->request = nullptr; - cacheBaton = nullptr; - } -} - -void HTTPRequest::removeBackoffTimer() { - assert(std::this_thread::get_id() == threadId); - if (backoffTimer) { - delete static_cast<RetryBaton *>(backoffTimer->data); - uv_timer_stop(backoffTimer); - uv_close((uv_handle_t *)backoffTimer, [](uv_handle_t *handle) { delete (uv_timer_t *)handle; }); - backoffTimer = nullptr; - } -} - -void HTTPRequest::retryImmediately() { - assert(std::this_thread::get_id() == threadId); - if (!cacheBaton && !httpBaton) { - if (backoffTimer) { - // Retry immediately. - uv_timer_stop(backoffTimer); - std::unique_ptr<RetryBaton> pair { static_cast<RetryBaton *>(backoffTimer->data) }; - assert(pair->first == this); - startHTTPRequest(std::move(pair->second)); - uv_close((uv_handle_t *)backoffTimer, [](uv_handle_t *handle) { delete (uv_timer_t *)handle; }); - backoffTimer = nullptr; - } else { - assert(!"We should always have a backoffTimer when there are no batons"); - } - } -} - -void HTTPRequest::cancel() { - assert(std::this_thread::get_id() == threadId); - removeCacheBaton(); - removeHTTPBaton(); - removeBackoffTimer(); - notify(); -} - - -HTTPRequest::~HTTPRequest() { - assert(std::this_thread::get_id() == threadId); - cancel(); -} - -#pragma clang diagnostic pop - -} diff --git a/src/mbgl/storage/http_request.hpp b/src/mbgl/storage/http_request.hpp deleted file mode 100644 index 7cc72101d5..0000000000 --- a/src/mbgl/storage/http_request.hpp +++ /dev/null @@ -1,58 +0,0 @@ -#ifndef MBGL_STORAGE_HTTP_REQUEST -#define MBGL_STORAGE_HTTP_REQUEST - -#include <mbgl/storage/resource_type.hpp> -#include <mbgl/storage/base_request.hpp> -#include <mbgl/storage/http_request_baton.hpp> - -#include <string> -#include <memory> -#include <cassert> -#include <thread> - -typedef struct uv_loop_s uv_loop_t; -typedef struct uv_timer_s uv_timer_t; - -namespace mbgl { - -struct CacheRequestBaton; -struct HTTPRequestBaton; -struct CacheEntry; -class SQLiteStore; - -class HTTPRequest : public BaseRequest { -public: - HTTPRequest(ResourceType type, const std::string &path, uv_loop_t *loop, util::ptr<SQLiteStore> store); - ~HTTPRequest(); - - void cancel(); - void retryImmediately(); - -private: - void startCacheRequest(); - void handleCacheResponse(std::unique_ptr<Response> &&response); - void startHTTPRequest(std::unique_ptr<Response> &&res); - void handleHTTPResponse(HTTPResponseType responseType, std::unique_ptr<Response> &&response); - - void retryHTTPRequest(std::unique_ptr<Response> &&res, uint64_t timeout); - - void removeCacheBaton(); - void removeHTTPBaton(); - void removeBackoffTimer(); - -private: - const std::thread::id threadId; - uv_loop_t *const loop; - CacheRequestBaton *cacheBaton = nullptr; - util::ptr<HTTPRequestBaton> httpBaton; - uv_timer_t *backoffTimer = nullptr; - util::ptr<SQLiteStore> store; - const ResourceType type; - uint8_t attempts = 0; - - friend struct HTTPRequestBaton; -}; - -} - -#endif diff --git a/src/mbgl/storage/http_request_baton.cpp b/src/mbgl/storage/http_request_baton.cpp deleted file mode 100644 index d781a3bdf4..0000000000 --- a/src/mbgl/storage/http_request_baton.cpp +++ /dev/null @@ -1,12 +0,0 @@ -#include <mbgl/storage/http_request_baton.hpp> -#include <uv.h> - -namespace mbgl { - -HTTPRequestBaton::HTTPRequestBaton(const std::string &path_) : threadId(std::this_thread::get_id()), path(path_) { -} - -HTTPRequestBaton::~HTTPRequestBaton() { -} - -} diff --git a/src/mbgl/storage/network_status.cpp b/src/mbgl/storage/network_status.cpp new file mode 100644 index 0000000000..04b6937d94 --- /dev/null +++ b/src/mbgl/storage/network_status.cpp @@ -0,0 +1,32 @@ +#include <mbgl/storage/network_status.hpp> + +#include <uv.h> + +// Example: Allocate a reachability object +// Reachability* reach = [Reachability reachabilityForInternetConnection]; +// reach.reachableBlock = ^(Reachability* reach) { NetworkStatus::Reachable(); }; +// [reach startNotifier]; + +namespace mbgl { + +std::mutex NetworkStatus::mtx; +std::set<uv_async_t *> NetworkStatus::observers; + +void NetworkStatus::Subscribe(uv_async_t *async) { + std::lock_guard<std::mutex> lock(NetworkStatus::mtx); + observers.insert(async); +} + +void NetworkStatus::Unsubscribe(uv_async_t *async) { + std::lock_guard<std::mutex> lock(NetworkStatus::mtx); + observers.erase(async); +} + +void NetworkStatus::Reachable() { + std::lock_guard<std::mutex> lock(NetworkStatus::mtx); + for (auto async : observers) { + uv_async_send(async); + } +} + +} diff --git a/src/mbgl/storage/request.cpp b/src/mbgl/storage/request.cpp index 39fbd36789..c771acb929 100644 --- a/src/mbgl/storage/request.cpp +++ b/src/mbgl/storage/request.cpp @@ -1,49 +1,86 @@ -#include <mbgl/storage/request.hpp> -#include <mbgl/storage/base_request.hpp> +#include <mbgl/storage/default/request.hpp> + +#include <mbgl/storage/response.hpp> + +#include <mbgl/util/util.hpp> +#include <mbgl/util/uv.hpp> #include <uv.h> #include <cassert> +#include <functional> namespace mbgl { -Request::Request(const util::ptr<BaseRequest> &base_) - : thread_id(std::this_thread::get_id()), base(base_) { -} +// Note: This requires that loop is running in the current thread (or not yet running). +Request::Request(const Resource &resource_, uv_loop_t *loop, Callback callback_) + : callback(callback_), resource(resource_) { + // When there is no loop supplied (== nullptr), the callback will be fired in an arbitrary + // thread (the thread notify() is called from) rather than kicking back to the calling thread. + if (loop) { + notify_async = new uv_async_t; + notify_async->data = this; + uv_async_init(loop, notify_async, [](uv_async_t *async, int) { + auto request = reinterpret_cast<Request *>(async->data); + uv::close(async); -Request::~Request() { - assert(thread_id == std::this_thread::get_id()); + if (!request->destruct_async) { + // We haven't created a cancel request, so we can safely delete this Request object + // since it won't be accessed in the future. + assert(request->response); + request->callback(*request->response); + delete request; + } else { + // Otherwise, we're waiting for for the destruct notification to be delivered in order + // to delete the Request object. We're doing this since we can't know whether the + // DefaultFileSource is still sending a cancel event, which means this object must still + // exist. + } + }); + } } -void Request::onload(CompletedCallback cb) { - assert(thread_id == std::this_thread::get_id()); - if (base) { - Callback *callback = base->add(std::move(cb), base); - if (callback) { - callbacks.push_front(callback); - } +Request::~Request() { + if (notify_async) { + // Request objects can be destructed in other threads when the user didn't supply a loop. + MBGL_VERIFY_THREAD(tid) } } -void Request::oncancel(AbortedCallback cb) { - assert(thread_id == std::this_thread::get_id()); - if (base) { - Callback *callback = base->add(std::move(cb), base); - if (callback) { - callbacks.push_front(callback); - } +void Request::notify(const std::shared_ptr<const Response> &response_) { + response = response_; + if (notify_async) { + uv_async_send(notify_async); + } else { + assert(response); + callback(*response); + delete this; } } void Request::cancel() { - assert(thread_id == std::this_thread::get_id()); - if (base) { - for (Callback *callback : callbacks) { - base->remove(callback); - } - base.reset(); + MBGL_VERIFY_THREAD(tid) + assert(notify_async); + assert(!destruct_async); + destruct_async = new uv_async_t; + destruct_async->data = this; + uv_async_init(notify_async->loop, destruct_async, [](uv_async_t *async, int) { + // The destruct_async will be invoked *after* the notify_async callback has already run. + auto request = reinterpret_cast<Request *>(async->data); + uv::close(async); + delete request; + }); +} + +// This gets called from the FileSource thread, and will only ever be invoked after cancel() was called +// in the original requesting thread. +void Request::destruct() { + if (notify_async) { + notify(nullptr); } - callbacks.clear(); + + assert(destruct_async); + uv_async_send(destruct_async); } } diff --git a/src/mbgl/storage/response.cpp b/src/mbgl/storage/response.cpp deleted file mode 100644 index a08a6d31ce..0000000000 --- a/src/mbgl/storage/response.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#include <mbgl/storage/response.hpp> - -#include <chrono> - -namespace mbgl { - -int64_t Response::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) { - return std::chrono::duration_cast<std::chrono::seconds>( - std::chrono::system_clock::now().time_since_epoch()).count() + - seconds; - } - } - - return -1; -} - -} diff --git a/src/mbgl/storage/sqlite_cache.cpp b/src/mbgl/storage/sqlite_cache.cpp new file mode 100644 index 0000000000..7a0fd21ce6 --- /dev/null +++ b/src/mbgl/storage/sqlite_cache.cpp @@ -0,0 +1,255 @@ +#include <mbgl/storage/default/sqlite_cache.hpp> +#include <mbgl/storage/default/request.hpp> +#include <mbgl/storage/response.hpp> + +#include <mbgl/util/util.hpp> +#include <mbgl/util/async_queue.hpp> +#include <mbgl/util/variant.hpp> + +#include "../util/sqlite3.hpp" +#include "../util/compression.hpp" + +#include <uv.h> + +#include <cassert> + +namespace mbgl { + +std::string removeAccessTokenFromURL(const std::string &url) { + const size_t token_start = url.find("access_token="); + // Ensure that token exists, isn't at the front and is preceded by either & or ?. + if (token_start == std::string::npos || token_start == 0 || !(url[token_start - 1] == '&' || url[token_start - 1] == '?')) { + return url; + } + + const size_t token_end = url.find_first_of('&', token_start); + if (token_end == std::string::npos) { + // The token is the last query argument. We slice away the "&access_token=..." part + return url.substr(0, token_start - 1); + } else { + // We slice away the "access_token=...&" part. + return url.substr(0, token_start) + url.substr(token_end + 1); + } +} + +std::string convertMapboxDomainsToProtocol(const std::string &url) { + const size_t protocol_separator = url.find("://"); + if (protocol_separator == std::string::npos) { + return url; + } + + const std::string protocol = url.substr(0, protocol_separator); + if (!(protocol == "http" || protocol == "https")) { + return url; + } + + const size_t domain_begin = protocol_separator + 3; + const size_t path_separator = url.find("/", domain_begin); + if (path_separator == std::string::npos) { + return url; + } + + const std::string domain = url.substr(domain_begin, path_separator - domain_begin); + if (domain.find(".tiles.mapbox.com") != std::string::npos) { + return "mapbox://" + url.substr(path_separator + 1); + } else { + return url; + } +} + +std::string unifyMapboxURLs(const std::string &url) { + return removeAccessTokenFromURL(convertMapboxDomainsToProtocol(url)); +} + + +using namespace mapbox::sqlite; + +struct SQLiteCache::GetAction { + const Resource resource; + const std::function<void(std::unique_ptr<Response>)> callback; +}; + +struct SQLiteCache::PutAction { + const Resource resource; + const std::shared_ptr<const Response> response; +}; + +struct SQLiteCache::RefreshAction { + const Resource resource; + const int64_t expires; +}; + +struct SQLiteCache::StopAction { +}; + +struct SQLiteCache::ActionDispatcher { + SQLiteCache &cache; + template <typename T> void operator()(T &t) { cache.process(t); } +}; + +SQLiteCache::SQLiteCache(const std::string &path_) + : path(path_), + loop(uv_loop_new()), + queue(new Queue(loop, [this](Action &action) { + mapbox::util::apply_visitor(ActionDispatcher{ *this }, action); + })), + thread([this]() { +#ifdef __APPLE__ + pthread_setname_np("SQLite Cache"); +#endif + uv_run(loop, UV_RUN_DEFAULT); + }) +{ +} + +SQLiteCache::~SQLiteCache() { + if (thread.joinable()) { + if (queue) { + queue->send(StopAction{ }); + } + thread.join(); + uv_loop_delete(loop); + } +} + + +void SQLiteCache::get(const Resource &resource, std::function<void(std::unique_ptr<Response>)> callback) { + // Can be called from any thread, but most likely from the file source thread. + // Will try to load the URL from the SQLite database and call the callback when done. + // Note that the callback is probably going to invoked from another thread, so the caller + // must make sure that it can run in that thread. + assert(queue); + queue->send(GetAction{ resource, callback }); +} + +void SQLiteCache::put(const Resource &resource, std::shared_ptr<const Response> response, Hint hint) { + // Can be called from any thread, but most likely from the file source thread. We are either + // storing a new response or updating the currently stored response, potentially setting a new + // expiry date. + assert(queue); + assert(response); + + if (hint == Hint::Full) { + queue->send(PutAction{ resource, response }); + } else if (hint == Hint::Refresh) { + queue->send(RefreshAction{ resource, response->expires }); + } +} + +void SQLiteCache::createDatabase() { + db = util::make_unique<Database>(path.c_str(), ReadWrite | Create); + + db->exec("CREATE TABLE IF NOT EXISTS `http_cache` (" + " `url` TEXT PRIMARY KEY NOT NULL," + " `status` INTEGER NOT NULL," // The response status (Successful or Error). + " `kind` INTEGER NOT NULL," // The kind of file. + " `modified` INTEGER," // Timestamp when the file was last modified. + " `etag` TEXT," + " `expires` INTEGER," // Timestamp when the server says the file expires. + " `data` BLOB," + " `compressed` INTEGER NOT NULL DEFAULT 0" // Whether the data is compressed. + ");" + "CREATE INDEX IF NOT EXISTS `http_cache_kind_idx` ON `http_cache` (`kind`);"); +} + +void SQLiteCache::process(GetAction &action) { + // This is called in the SQLite event loop. + if (!db) { + createDatabase(); + } + + if (!getStmt) { + // Initialize the statement 0 1 + getStmt = util::make_unique<Statement>(db->prepare("SELECT `status`, `modified`, " + // 2 3 4 5 1 + "`etag`, `expires`, `data`, `compressed` FROM `http_cache` WHERE `url` = ?")); + } else { + getStmt->reset(); + } + + const std::string unifiedURL = unifyMapboxURLs(action.resource.url); + getStmt->bind(1, unifiedURL.c_str()); + if (getStmt->run()) { + // There is data. + auto response = util::make_unique<Response>(); + response->status = Response::Status(getStmt->get<int>(0)); + response->modified = getStmt->get<int64_t>(1); + response->etag = getStmt->get<std::string>(2); + response->expires = getStmt->get<int64_t>(3); + response->data = getStmt->get<std::string>(4); + if (getStmt->get<int>(5)) { // == compressed + response->data = util::decompress(response->data); + } + action.callback(std::move(response)); + } else { + // There is no data. + action.callback(nullptr); + } +} + +void SQLiteCache::process(PutAction &action) { + if (!db) { + createDatabase(); + } + + if (!putStmt) { + putStmt = util::make_unique<Statement>(db->prepare("REPLACE INTO `http_cache` (" + // 1 2 3 4 5 6 7 8 + "`url`, `status`, `kind`, `modified`, `etag`, `expires`, `data`, `compressed`" + ") VALUES(?, ?, ?, ?, ?, ?, ?, ?)")); + } else { + putStmt->reset(); + } + + const std::string unifiedURL = unifyMapboxURLs(action.resource.url); + putStmt->bind(1 /* url */, unifiedURL.c_str()); + putStmt->bind(2 /* status */, int(action.response->status)); + putStmt->bind(3 /* kind */, int(action.resource.kind)); + putStmt->bind(4 /* modified */, action.response->modified); + putStmt->bind(5 /* etag */, action.response->etag.c_str()); + putStmt->bind(6 /* expires */, action.response->expires); + + std::string data; + if (action.resource.kind != Resource::Image) { + // Do not compress images, since they are typically compressed already. + data = util::compress(action.response->data); + } + + if (!data.empty() && data.size() < action.response->data.size()) { + // Store the compressed data when it is smaller than the original + // uncompressed data. + putStmt->bind(7 /* data */, data, false); // do not retain the string internally. + putStmt->bind(8 /* compressed */, true); + } else { + putStmt->bind(7 /* data */, action.response->data, false); // do not retain the string internally. + putStmt->bind(8 /* compressed */, false); + } + + putStmt->run(); +} + +void SQLiteCache::process(RefreshAction &action) { + if (!db) { + createDatabase(); + } + + if (!refreshStmt) { + refreshStmt = util::make_unique<Statement>( // 1 2 + db->prepare("UPDATE `http_cache` SET `expires` = ? WHERE `url` = ?")); + } else { + refreshStmt->reset(); + } + + const std::string unifiedURL = unifyMapboxURLs(action.resource.url); + refreshStmt->bind(1, int64_t(action.expires)); + refreshStmt->bind(2, unifiedURL.c_str()); + refreshStmt->run(); +} + +void SQLiteCache::process(StopAction &) { + assert(queue); + queue->stop(); + queue = nullptr; +} + +} diff --git a/src/mbgl/storage/sqlite_store.cpp b/src/mbgl/storage/sqlite_store.cpp deleted file mode 100644 index d382921dec..0000000000 --- a/src/mbgl/storage/sqlite_store.cpp +++ /dev/null @@ -1,228 +0,0 @@ -#include <mbgl/storage/sqlite_store.hpp> -#include <mbgl/util/compression.hpp> -#include <mbgl/util/sqlite3.hpp> -#include <mbgl/util/std.hpp> - -#include <mbgl/util/uv-worker.h> - -#include <cassert> - -using namespace mapbox::sqlite; - -std::string removeAccessTokenFromURL(const std::string &url) { - const size_t token_start = url.find("access_token="); - // Ensure that token exists, isn't at the front and is preceded by either & or ?. - if (token_start == std::string::npos || token_start == 0 || !(url[token_start - 1] == '&' || url[token_start - 1] == '?')) { - return url; - } - - const size_t token_end = url.find_first_of('&', token_start); - if (token_end == std::string::npos) { - // The token is the last query argument. We slice away the "&access_token=..." part - return url.substr(0, token_start - 1); - } else { - // We slice away the "access_token=...&" part. - return url.substr(0, token_start) + url.substr(token_end + 1); - } -} - -std::string convertMapboxDomainsToProtocol(const std::string &url) { - const size_t protocol_separator = url.find("://"); - if (protocol_separator == std::string::npos) { - return url; - } - - const std::string protocol = url.substr(0, protocol_separator); - if (!(protocol == "http" || protocol == "https")) { - return url; - } - - const size_t domain_begin = protocol_separator + 3; - const size_t path_separator = url.find("/", domain_begin); - if (path_separator == std::string::npos) { - return url; - } - - const std::string domain = url.substr(domain_begin, path_separator - domain_begin); - if (domain.find(".tiles.mapbox.com") != std::string::npos) { - return "mapbox://" + url.substr(path_separator + 1); - } else { - return url; - } -} - -std::string unifyMapboxURLs(const std::string &url) { - return removeAccessTokenFromURL(convertMapboxDomainsToProtocol(url)); -} - -namespace mbgl { - -SQLiteStore::SQLiteStore(uv_loop_t *loop, const std::string &path) - : thread_id(std::this_thread::get_id()), - db(std::make_shared<Database>(path.c_str(), ReadWrite | Create)) { - createSchema(); - worker = new uv_worker_t; - uv_worker_init(worker, loop, 1, "SQLite"); -} - -SQLiteStore::~SQLiteStore() { - // Nothing to do. This function needs to be here because we're forward-declaring - // Database, so we need the actual definition here to be able to properly destruct it. - if (worker) { - uv_worker_close(worker, [](uv_worker_t *worker_handle) { - delete worker_handle; - }); - } -} - -void SQLiteStore::createSchema() { - if (!db || !*db) { - return; - } - - db->exec("CREATE TABLE IF NOT EXISTS `http_cache` (" - " `url` TEXT PRIMARY KEY NOT NULL," - " `code` INTEGER NOT NULL," - " `type` INTEGER NOT NULL," - " `modified` INTEGER," - " `etag` TEXT," - " `expires` INTEGER," - " `data` BLOB," - " `compressed` INTEGER NOT NULL DEFAULT 0" - ");" - "CREATE INDEX IF NOT EXISTS `http_cache_type_idx` ON `http_cache` (`type`);"); -} - -struct GetBaton { - util::ptr<Database> db; - std::string path; - ResourceType type; - void *ptr = nullptr; - SQLiteStore::GetCallback callback = nullptr; - std::unique_ptr<Response> response; -}; - -void SQLiteStore::get(const std::string &path, GetCallback callback, void *ptr) { - assert(std::this_thread::get_id() == thread_id); - if (!db || !*db) { - if (callback) { - callback(nullptr, ptr); - } - return; - } - - GetBaton *get_baton = new GetBaton; - get_baton->db = db; - get_baton->path = path; - get_baton->ptr = ptr; - get_baton->callback = callback; - - uv_worker_send(worker, get_baton, [](void *data) { - GetBaton *baton = (GetBaton *)data; - const std::string url = unifyMapboxURLs(baton->path); - // 0 1 2 - Statement stmt = baton->db->prepare("SELECT `code`, `type`, `modified`, " - // 3 4 5 6 - "`etag`, `expires`, `data`, `compressed` FROM `http_cache` WHERE `url` = ?"); - - stmt.bind(1, url.c_str()); - if (stmt.run()) { - // There is data. - baton->response = util::make_unique<Response>(); - - baton->response->code = stmt.get<int>(0); - baton->type = ResourceType(stmt.get<int>(1)); - baton->response->modified = stmt.get<int64_t>(2); - baton->response->etag = stmt.get<std::string>(3); - baton->response->expires = stmt.get<int64_t>(4); - baton->response->data = stmt.get<std::string>(5); - if (stmt.get<int>(6)) { // == compressed - baton->response->data = util::decompress(baton->response->data); - } - } else { - // There is no data. - // This is a noop. - } - }, [](void *data) { - std::unique_ptr<GetBaton> baton { (GetBaton *)data }; - if (baton->callback) { - baton->callback(std::move(baton->response), baton->ptr); - } - }); -} - - -struct PutBaton { - util::ptr<Database> db; - std::string path; - ResourceType type; - Response response; -}; - -void SQLiteStore::put(const std::string &path, ResourceType type, const Response &response) { - assert(std::this_thread::get_id() == thread_id); - if (!db) return; - - PutBaton *put_baton = new PutBaton; - put_baton->db = db; - put_baton->path = path; - put_baton->type = type; - put_baton->response = response; - - uv_worker_send(worker, put_baton, [](void *data) { - PutBaton *baton = (PutBaton *)data; - const std::string url = unifyMapboxURLs(baton->path); - Statement stmt = baton->db->prepare("REPLACE INTO `http_cache` (" - // 1 2 3 4 5 6 7 8 - "`url`, `code`, `type`, `modified`, `etag`, `expires`, `data`, `compressed`" - ") VALUES(?, ?, ?, ?, ?, ?, ?, ?)"); - stmt.bind(1, url.c_str()); - stmt.bind(2, int(baton->response.code)); - stmt.bind(3, int(baton->type)); - stmt.bind(4, baton->response.modified); - stmt.bind(5, baton->response.etag.c_str()); - stmt.bind(6, baton->response.expires); - - if (baton->type == ResourceType::Image) { - stmt.bind(7, baton->response.data, false); // do not retain the string internally. - stmt.bind(8, false); - } else { - stmt.bind(7, util::compress(baton->response.data), true); // retain the string internally. - stmt.bind(8, true); - } - - stmt.run(); - }, [](void *data) { - delete (PutBaton *)data; - }); -} - -struct ExpirationBaton { - util::ptr<Database> db; - std::string path; - int64_t expires; -}; - -void SQLiteStore::updateExpiration(const std::string &path, int64_t expires) { - assert(std::this_thread::get_id() == thread_id); - if (!db || !*db) return; - - ExpirationBaton *expiration_baton = new ExpirationBaton; - expiration_baton->db = db; - expiration_baton->path = path; - expiration_baton->expires = expires; - - uv_worker_send(worker, expiration_baton, [](void *data) { - ExpirationBaton *baton = (ExpirationBaton *)data; - const std::string url = unifyMapboxURLs(baton->path); - Statement stmt = // 1 2 - baton->db->prepare("UPDATE `http_cache` SET `expires` = ? WHERE `url` = ?"); - stmt.bind<int64_t>(1, baton->expires); - stmt.bind(2, url.c_str()); - stmt.run(); - }, [](void *data) { - delete (ExpirationBaton *)data; - }); -} - -} diff --git a/src/mbgl/storage/sqlite_store.hpp b/src/mbgl/storage/sqlite_store.hpp deleted file mode 100644 index 988eca2597..0000000000 --- a/src/mbgl/storage/sqlite_store.hpp +++ /dev/null @@ -1,49 +0,0 @@ -#ifndef MBGL_STORAGE_SQLITE_STORE -#define MBGL_STORAGE_SQLITE_STORE - -#include <mbgl/storage/response.hpp> -#include <mbgl/storage/resource_type.hpp> -#include <mbgl/util/ptr.hpp> - -#include <uv.h> - -#include <string> -#include <thread> - -typedef struct uv_worker_s uv_worker_t; - -namespace mapbox { -namespace sqlite { -class Database; -} -} - -namespace mbgl { - -class SQLiteStore { -public: - SQLiteStore(uv_loop_t *loop, const std::string &path); - ~SQLiteStore(); - - typedef void (*GetCallback)(std::unique_ptr<Response> &&entry, void *ptr); - - void get(const std::string &path, GetCallback cb, void *ptr); - void put(const std::string &path, ResourceType type, const Response &entry); - void updateExpiration(const std::string &path, int64_t expires); - -private: - void createSchema(); - void closeDatabase(); - static void runGet(uv_work_t *req); - static void runPut(uv_work_t *req); - static void deliverResult(uv_work_t *req, int status); - -private: - const std::thread::id thread_id; - util::ptr<mapbox::sqlite::Database> db; - uv_worker_t *worker = nullptr; -}; - -} - -#endif diff --git a/src/mbgl/style/style.hpp b/src/mbgl/style/style.hpp index af00f9710b..5517a56a71 100644 --- a/src/mbgl/style/style.hpp +++ b/src/mbgl/style/style.hpp @@ -43,6 +43,7 @@ public: util::ptr<StyleLayerGroup> layers; std::vector<std::string> appliedClasses; std::string glyph_url; + std::string base; private: std::string sprite_url; diff --git a/src/mbgl/text/glyph_store.cpp b/src/mbgl/text/glyph_store.cpp index 2f5db180fd..0d9e70d556 100644 --- a/src/mbgl/text/glyph_store.cpp +++ b/src/mbgl/text/glyph_store.cpp @@ -148,28 +148,23 @@ GlyphPBF::GlyphPBF(const std::string &glyphURL, const std::string &fontStack, Gl }); // The prepare call jumps back to the main thread. - fileSource.prepare([&, url] { - auto request = fileSource.request(ResourceType::Glyphs, url); - request->onload([&, url](const Response &res) { - if (res.code != 200) { - // Something went wrong with loading the glyph pbf. Pass on the error to the future listeners. - const std::string msg = std::string { "[ERROR] failed to load glyphs (" } + util::toString(res.code) + "): " + res.message; - promise.set_exception(std::make_exception_ptr(std::runtime_error(msg))); - } else { - // Transfer the data to the GlyphSet and signal its availability. - // Once it is available, the caller will need to call parse() to actually - // parse the data we received. We are not doing this here since this callback is being - // called from another (unknown) thread. - data = res.data; - promise.set_value(*this); - } - }); - request->oncancel([&]() { - promise.set_exception(std::make_exception_ptr(std::runtime_error("Loading glyphs was canceled"))); - }); + fileSource.request({ Resource::Kind::Glyphs, url }, [&, url](const Response &res) { + if (res.status != Response::Successful) { + // Something went wrong with loading the glyph pbf. Pass on the error to the future listeners. + const std::string msg = std::string { "[ERROR] failed to load glyphs: " } + res.message; + promise.set_exception(std::make_exception_ptr(std::runtime_error(msg))); + } else { + // Transfer the data to the GlyphSet and signal its availability. + // Once it is available, the caller will need to call parse() to actually + // parse the data we received. We are not doing this here since this callback is being + // called from another (unknown) thread. + data = res.data; + promise.set_value(*this); + } }); } + std::shared_future<GlyphPBF &> GlyphPBF::getFuture() { return future; } diff --git a/src/mbgl/util/uv.cpp b/src/mbgl/util/uv.cpp index 7aa5bad0cf..a993e6b962 100644 --- a/src/mbgl/util/uv.cpp +++ b/src/mbgl/util/uv.cpp @@ -22,4 +22,12 @@ std::string cwd() { #endif } +const char *getFileRequestError(uv_fs_t *req) { +#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10 + return uv_strerror(uv_last_error(req->loop)); +#else + return uv_strerror(int(req->result)); +#endif +} + } |