diff --git a/src/mbgl/map/tile_data.cpp b/src/mbgl/map/tile_data.cpp index b8f317a07..7b379e9fe 100644 --- a/src/mbgl/map/tile_data.cpp +++ b/src/mbgl/map/tile_data.cpp @@ -38,7 +38,8 @@ void TileData::request(uv::worker &worker, float pixelRatio, std::function std::string { + url = util::mapbox::normalizeTileURL(url, source.url, source.type); + url = util::replaceTokens(url, [&](const std::string &token) -> std::string { if (token == "z") return util::toString(id.z); if (token == "x") return util::toString(id.x); if (token == "y") return util::toString(id.y); diff --git a/src/mbgl/util/mapbox.cpp b/src/mbgl/util/mapbox.cpp index f89ed6c00..d5b467246 100644 --- a/src/mbgl/util/mapbox.cpp +++ b/src/mbgl/util/mapbox.cpp @@ -39,23 +39,27 @@ std::string normalizeGlyphsURL(const std::string& url, const std::string& access return normalizeURL(url, accessToken); } -std::string normalizeTileURL(const std::string& url, const std::string& sourceURL) { - if (sourceURL.empty() || sourceURL.compare(0, mapbox.length(), mapbox) != 0) +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) { return url; + } std::string::size_type queryIdx = url.rfind("?"); // Trim off the right end but never touch anything before the extension dot. std::string urlSansParams((queryIdx == std::string::npos) ? url : url.substr(0, queryIdx)); - while (!urlSansParams.empty() && isdigit(urlSansParams.back())) + while (!urlSansParams.empty() && isdigit(urlSansParams.back())) { urlSansParams.pop_back(); + } - std::string::size_type extensionIdx = urlSansParams.length() - 4; - if (extensionIdx <= 0) - return url; - std::string extension = urlSansParams.substr(extensionIdx); - if (extension.compare(".png") != 0 && extension.compare(".jpg") != 0) + std::string::size_type basenameIdx = url.rfind("/"); + std::string::size_type extensionIdx = url.rfind("."); + if (basenameIdx == std::string::npos || extensionIdx == std::string::npos || + basenameIdx > extensionIdx) { + // No file extension: probably not a file name we can tack a ratio onto. return url; + } std::string normalizedURL(url); normalizedURL.insert(extensionIdx, "{ratio}"); diff --git a/src/mbgl/util/mapbox.hpp b/src/mbgl/util/mapbox.hpp index 68b821eab..8ad92c0b4 100644 --- a/src/mbgl/util/mapbox.hpp +++ b/src/mbgl/util/mapbox.hpp @@ -2,6 +2,7 @@ #define MBGL_UTIL_MAPBOX #include +#include namespace mbgl { namespace util { @@ -9,7 +10,7 @@ namespace mapbox { std::string normalizeSourceURL(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); +std::string normalizeTileURL(const std::string& url, const std::string& sourceURL, SourceType sourceType); } } diff --git a/test/miscellaneous/mapbox.cpp b/test/miscellaneous/mapbox.cpp index 62344e016..e82478874 100644 --- a/test/miscellaneous/mapbox.cpp +++ b/test/miscellaneous/mapbox.cpp @@ -19,12 +19,15 @@ TEST(Mapbox, GlyphsURL) { TEST(Mapbox, TileURL) { try { - EXPECT_EQ(mbgl::util::mapbox::normalizeTileURL("http://path.png/tile.png", "mapbox://user.map"), "http://path.png/tile{ratio}.png"); - EXPECT_EQ(mbgl::util::mapbox::normalizeTileURL("http://path.png/tile.png32", "mapbox://user.map"), "http://path.png/tile{ratio}.png32"); - EXPECT_EQ(mbgl::util::mapbox::normalizeTileURL("http://path.png/tile.png70", "mapbox://user.map"), "http://path.png/tile{ratio}.png70"); - EXPECT_EQ(mbgl::util::mapbox::normalizeTileURL("http://path.png/tile.png?access_token=foo", "mapbox://user.map"), "http://path.png/tile{ratio}.png?access_token=foo"); - EXPECT_EQ(mbgl::util::mapbox::normalizeTileURL("http://path.png", "http://path"), "http://path.png"); - EXPECT_EQ(mbgl::util::mapbox::normalizeTileURL("http://path.png", ""), "http://path.png"); + EXPECT_EQ(mbgl::util::mapbox::normalizeTileURL("http://path.png/tile.png", "mapbox://user.map", SourceType::Raster), "http://path.png/tile{ratio}.png"); + EXPECT_EQ(mbgl::util::mapbox::normalizeTileURL("http://path.png/tile.png32", "mapbox://user.map", SourceType::Raster), "http://path.png/tile{ratio}.png32"); + EXPECT_EQ(mbgl::util::mapbox::normalizeTileURL("http://path.png/tile.png70", "mapbox://user.map", SourceType::Raster), "http://path.png/tile{ratio}.png70"); + EXPECT_EQ(mbgl::util::mapbox::normalizeTileURL("http://path.png/tile.png?access_token=foo", "mapbox://user.map", SourceType::Raster), "http://path.png/tile{ratio}.png?access_token=foo"); + EXPECT_EQ(mbgl::util::mapbox::normalizeTileURL("http://path.png", "http://path", SourceType::Raster), "http://path.png"); + EXPECT_EQ(mbgl::util::mapbox::normalizeTileURL("http://path.png", "", SourceType::Raster), "http://path.png"); + EXPECT_EQ(mbgl::util::mapbox::normalizeTileURL("http://path.png/tile.png", "mapbox://user.map", SourceType::Vector), "http://path.png/tile.png"); + EXPECT_EQ(mbgl::util::mapbox::normalizeTileURL("http://path.png/tile.pbf", "mapbox://user.map", SourceType::Raster), "http://path.png/tile{ratio}.pbf"); + EXPECT_EQ(mbgl::util::mapbox::normalizeTileURL("http://path.png/tile.pbf", "mapbox://user.map", SourceType::Vector), "http://path.png/tile.pbf"); } catch (const std::regex_error& e) { std::cout << "regex_error caught: " << e.what() << '\n'; std::cout << e.code() << '\n'; @@ -57,5 +60,6 @@ TEST(Mapbox, TileURL) { default: break; } + throw e; } }