diff options
Diffstat (limited to 'src/mbgl')
-rw-r--r-- | src/mbgl/map/sprite.cpp | 5 | ||||
-rw-r--r-- | src/mbgl/storage/default_file_source.cpp | 5 | ||||
-rw-r--r-- | src/mbgl/util/mapbox.cpp | 83 | ||||
-rw-r--r-- | src/mbgl/util/mapbox.hpp | 1 |
4 files changed, 66 insertions, 28 deletions
diff --git a/src/mbgl/map/sprite.cpp b/src/mbgl/map/sprite.cpp index 84fbce6069..d5628b05b2 100644 --- a/src/mbgl/map/sprite.cpp +++ b/src/mbgl/map/sprite.cpp @@ -9,6 +9,7 @@ #include <mbgl/util/raster.hpp> #include <mbgl/util/thread.hpp> #include <mbgl/util/uv_detail.hpp> +#include <mbgl/util/mapbox.hpp> #include <rapidjson/document.h> @@ -49,7 +50,7 @@ Sprite::Sprite(const std::string& baseUrl, float pixelRatio_) loader = std::make_unique<Loader>(); FileSource* fs = util::ThreadContext::getFileSource(); - loader->jsonRequest = fs->request({ Resource::Kind::JSON, jsonURL }, util::RunLoop::getLoop(), + loader->jsonRequest = fs->request({ Resource::Kind::SpriteJSON, jsonURL }, util::RunLoop::getLoop(), [this, jsonURL](const Response& res) { loader->jsonRequest = nullptr; if (res.status == Response::Successful) { @@ -65,7 +66,7 @@ Sprite::Sprite(const std::string& baseUrl, float pixelRatio_) }); loader->spriteRequest = - fs->request({ Resource::Kind::Image, spriteURL }, util::RunLoop::getLoop(), + fs->request({ Resource::Kind::SpriteImage, spriteURL }, util::RunLoop::getLoop(), [this, spriteURL](const Response& res) { loader->spriteRequest = nullptr; if (res.status == Response::Successful) { diff --git a/src/mbgl/storage/default_file_source.cpp b/src/mbgl/storage/default_file_source.cpp index 12db28c981..c33728db15 100644 --- a/src/mbgl/storage/default_file_source.cpp +++ b/src/mbgl/storage/default_file_source.cpp @@ -55,6 +55,11 @@ Request* DefaultFileSource::request(const Resource& resource, url = util::mapbox::normalizeGlyphsURL(resource.url, accessToken); break; + case Resource::Kind::SpriteImage: + case Resource::Kind::SpriteJSON: + url = util::mapbox::normalizeSpriteURL(resource.url, accessToken); + break; + default: url = resource.url; } diff --git a/src/mbgl/util/mapbox.cpp b/src/mbgl/util/mapbox.cpp index 05e778dfb3..dee0e0f8f4 100644 --- a/src/mbgl/util/mapbox.cpp +++ b/src/mbgl/util/mapbox.cpp @@ -1,59 +1,90 @@ #include <mbgl/util/mapbox.hpp> #include <stdexcept> +#include <vector> namespace mbgl { namespace util { namespace mapbox { -const std::string mapbox = "mapbox://"; +const std::string protocol = "mapbox://"; +const std::string baseURL = "https://api.mapbox.com/"; -std::string normalizeURL(const std::string& url, const std::string& pathPrefix, const std::string& accessToken) { - if (accessToken.empty()) - throw std::runtime_error("You must provide a Mapbox API access token for Mapbox tile sources"); +bool isMapboxURL(const std::string& url) { + return url.compare(0, protocol.length(), protocol) == 0; +} - return std::string("https://api.tiles.mapbox.com") - + pathPrefix - + url.substr(mapbox.length()) - + "?access_token=" - + accessToken; +std::vector<std::string> getMapboxURLPathname(const std::string& url) { + std::vector<std::string> pathname; + std::size_t startIndex = protocol.length(); + while (startIndex < url.length()) { + std::size_t endIndex = url.find("/", startIndex); + if (endIndex == std::string::npos) endIndex = url.length(); + pathname.push_back(url.substr(startIndex, endIndex - startIndex)); + startIndex = endIndex + 1; + } + return pathname; } std::string normalizeSourceURL(const std::string& url, const std::string& accessToken) { - if (url.compare(0, mapbox.length(), mapbox) != 0) + if (!isMapboxURL(url)) { return url; + } - std::string result = normalizeURL(url + ".json", "/v4/", accessToken); - - // TileJSON requests need a secure flag appended to their URLs so - // that the server knows to send SSL-ified resource references. - result += "&secure"; + if (accessToken.empty()) { + throw std::runtime_error("You must provide a Mapbox API access token for Mapbox tile sources"); + } - return result; + return baseURL + "v4/" + url.substr(protocol.length()) + ".json?access_token=" + accessToken + "&secure"; } std::string normalizeStyleURL(const std::string& url, const std::string& accessToken) { - if (url.compare(0, mapbox.length(), mapbox) != 0) + if (!isMapboxURL(url)) { + return url; + } + + std::vector<std::string> pathname = getMapboxURLPathname(url); + std::string user = pathname[1]; + std::string id = pathname[2]; + bool isDraft = pathname.size() > 3; + return baseURL + "styles/v1/" + user + "/" + id + (isDraft ? "/draft" : "") + "?access_token=" + accessToken; +} + +std::string normalizeSpriteURL(const std::string& url, const std::string& accessToken) { + if (!isMapboxURL(url)) { return url; + } - const std::string user = url.substr(mapbox.length(), url.find('.') - mapbox.length()); + std::vector<std::string> pathname = getMapboxURLPathname(url); + std::string user = pathname[1]; + bool isDraft = pathname.size() > 3; - return normalizeURL(url, "/styles/v1/" + user + "/", accessToken); + std::string id, extension; + if (isDraft) { + size_t index = pathname[3].find_first_of("@."); + id = pathname[2]; + extension = pathname[3].substr(index); + } else { + size_t index = pathname[2].find_first_of("@."); + id = pathname[2].substr(0, index); + extension = pathname[2].substr(index); + } + + return baseURL + "styles/v1/" + user + "/" + id + "/" + (isDraft ? "draft/" : "") + "sprite" + extension + "?access_token=" + accessToken; } std::string normalizeGlyphsURL(const std::string& url, const std::string& accessToken) { - if (url.compare(0, mapbox.length(), mapbox) != 0) { + if (!isMapboxURL(url)) { return url; - } else { - const std::string prefix = "mapbox://fonts/"; - const std::string user = url.substr(prefix.length(), url.find("/", prefix.length()) - prefix.length()); - return normalizeURL("mapbox://" + user + "/{fontstack}/{range}.pbf", "/fonts/v1/", accessToken); } + + std::vector<std::string> pathname = getMapboxURLPathname(url); + std::string user = pathname[1]; + return baseURL + "fonts/v1/" + user + "/{fontstack}/{range}.pbf?access_token=" + accessToken; } std::string normalizeTileURL(const std::string& url, const std::string& sourceURL, SourceType sourceType) { - if (sourceURL.empty() || sourceURL.compare(0, mapbox.length(), mapbox) != 0 || - sourceType != SourceType::Raster) { + if (sourceURL.empty() || !isMapboxURL(sourceURL) || sourceType != SourceType::Raster) { return url; } diff --git a/src/mbgl/util/mapbox.hpp b/src/mbgl/util/mapbox.hpp index 86ea349b8c..9c24dda0e6 100644 --- a/src/mbgl/util/mapbox.hpp +++ b/src/mbgl/util/mapbox.hpp @@ -10,6 +10,7 @@ namespace mapbox { std::string normalizeSourceURL(const std::string& url, const std::string& accessToken); std::string normalizeStyleURL(const std::string& url, const std::string& accessToken); +std::string normalizeSpriteURL(const std::string& url, const std::string& accessToken); std::string normalizeGlyphsURL(const std::string& url, const std::string& accessToken); std::string normalizeTileURL(const std::string& url, const std::string& sourceURL, SourceType sourceType); |