1 #include <mbgl/util/url.hpp>
2 #include <mbgl/util/token.hpp>
3
4 #include <iomanip>
5 #include <sstream>
6 #include <string>
7 #include <cstdlib>
8 #include <algorithm>
9 #include <cstring>
10
11 namespace {
12
13 // std::alnum etc. suffer from locale-dependence.
14
isAlphaCharacter(char c)15 inline bool isAlphaCharacter(char c) {
16 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
17 }
18
isAlphaNumericCharacter(char c)19 inline bool isAlphaNumericCharacter(char c) {
20 return isAlphaCharacter(c) || (c >= '0' && c <= '9');
21 }
22
isSchemeCharacter(char c)23 inline bool isSchemeCharacter(char c) {
24 return isAlphaNumericCharacter(c) || c == '-' || c == '+' || c == '.';
25 }
26
27 } // namespace
28
29 namespace mbgl {
30 namespace util {
31
percentEncode(const std::string & input)32 std::string percentEncode(const std::string& input) {
33 std::ostringstream encoded;
34
35 encoded.fill('0');
36 encoded << std::hex;
37
38 for (auto c : input) {
39 if (isAlphaNumericCharacter(c) || c == '-' || c == '_' || c == '.' || c == '~') {
40 encoded << c;
41 } else {
42 encoded << '%' << std::setw(2) << int(c);
43 }
44 }
45
46 return encoded.str();
47 }
48
percentDecode(const std::string & input)49 std::string percentDecode(const std::string& input) {
50 std::string decoded;
51
52 auto it = input.begin();
53 const auto end = input.end();
54 char hex[3] = "00";
55
56 while (it != end) {
57 auto cur = std::find(it, end, '%');
58 decoded.append(it, cur);
59 it = cur;
60 if (cur != end) {
61 it += input.copy(hex, 2, cur - input.begin() + 1) + 1;
62 decoded += static_cast<char>(std::strtoul(hex, nullptr, 16));
63 }
64 }
65
66 return decoded;
67 }
68
URL(const std::string & str)69 URL::URL(const std::string& str)
70 : query([&]() -> Segment {
71 const auto hashPos = str.find('#');
72 const auto queryPos = str.find('?');
73 if (queryPos == std::string::npos || hashPos < queryPos) {
74 return { hashPos != std::string::npos ? hashPos : str.size(), 0 };
75 }
76 return { queryPos, (hashPos != std::string::npos ? hashPos : str.size()) - queryPos };
77 }()),
__anon14e840fe0302() 78 scheme([&]() -> Segment {
79 if (str.empty() || !isAlphaCharacter(str.front())) return { 0, 0 };
80 size_t schemeEnd = 0;
81 while (schemeEnd < query.first && isSchemeCharacter(str[schemeEnd])) ++schemeEnd;
82 return { 0, str[schemeEnd] == ':' ? schemeEnd : 0 };
83 }()),
__anon14e840fe0402() 84 domain([&]() -> Segment {
85 auto domainPos = scheme.first + scheme.second;
86 while (domainPos < query.first && (str[domainPos] == ':' || str[domainPos] == '/')) {
87 ++domainPos;
88 }
89 const bool isData = str.compare(scheme.first, scheme.second, "data") == 0;
90 const auto endPos = str.find(isData ? ',' : '/', domainPos);
91 return { domainPos, std::min(query.first, endPos) - domainPos };
92 }()),
__anon14e840fe0502() 93 path([&]() -> Segment {
94 auto pathPos = domain.first + domain.second;
95 const bool isData = str.compare(scheme.first, scheme.second, "data") == 0;
96 if (isData) {
97 // Skip comma
98 pathPos++;
99 }
100 return { pathPos, query.first - pathPos };
101 }()) {
102 }
103
Path(const std::string & str,const size_t pos,const size_t count)104 Path::Path(const std::string& str, const size_t pos, const size_t count)
105 : directory([&]() -> Segment {
106 // Finds the string between pos and the first /, if it exists
107 const auto endPos = count == std::string::npos ? str.size() : pos + count;
108 const auto slashPos = str.rfind('/', endPos);
109 return { pos, slashPos == std::string::npos || slashPos < pos ? 0 : slashPos + 1 - pos };
110 }()),
__anon14e840fe0702() 111 extension([&]() -> Segment {
112 auto dotPos = str.rfind('.', pos + count);
113 const auto endPos = count == std::string::npos ? str.size() : pos + count;
114 // Count a preceding @2x to the file extension as well.
115 const char* factor = "@2x";
116 const size_t factorLen = strlen(factor);
117 if (dotPos >= factorLen && dotPos < endPos &&
118 str.compare(dotPos - factorLen, factorLen, factor) == 0) {
119 dotPos -= factorLen;
120 }
121 if (dotPos == std::string::npos || dotPos < directory.first + directory.second) {
122 return { endPos, 0 };
123 }
124 return { dotPos, endPos - dotPos };
125 }()),
__anon14e840fe0802() 126 filename([&]() -> Segment {
127 const auto filePos = directory.first + directory.second;
128 return { filePos, extension.first - filePos };
129 }()) {
130 }
131
transformURL(const std::string & tpl,const std::string & str,const URL & url)132 std::string transformURL(const std::string& tpl, const std::string& str, const URL& url) {
133 auto result = util::replaceTokens(tpl, [&](const std::string& token) -> optional<std::string> {
134 if (token == "path") {
135 return str.substr(url.path.first, url.path.second);
136 } else if (token == "domain") {
137 return str.substr(url.domain.first, url.domain.second);
138 } else if (token == "scheme") {
139 return str.substr(url.scheme.first, url.scheme.second);
140 } else if (token == "directory") {
141 const Path path(str, url.path.first, url.path.second);
142 return str.substr(path.directory.first, path.directory.second);
143 } else if (token == "filename") {
144 const Path path(str, url.path.first, url.path.second);
145 return str.substr(path.filename.first, path.filename.second);
146 } else if (token == "extension") {
147 const Path path(str, url.path.first, url.path.second);
148 return str.substr(path.extension.first, path.extension.second);
149 } else {
150 return {};
151 }
152 });
153
154 // Append the query string if it exists.
155 if (url.query.second > 1) {
156 const auto amp = result.find('?') != std::string::npos ? result.size() : std::string::npos;
157 result.append(str, url.query.first, url.query.second);
158 // Transform the question mark to an ampersand if we had a query string previously.
159 if (amp < result.size()) {
160 result[amp] = '&';
161 }
162 }
163 return result;
164 }
165
166 } // namespace util
167 } // namespace mbgl
168