Repository navigation
Expand file tree
/
Copy pathFakeUrl.cpp
More file actions
305 lines (284 loc) · 8.34 KB
/
Copy pathFakeUrl.cpp
File metadata and controls
305 lines (284 loc) · 8.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// platform::ParseUrl, ParseModuleUrl and BlobStoreUrl for host tests built
// without ada (see NS_ADA_DIR in
// CMakeLists.txt): enough of the WHATWG URL parser for the URLs the tests use.
// No IDNA or IPv6.
#include <cctype>
#include <optional>
#include <string>
#include <string_view>
#include <vector>
#include "ns/Platform.h"
namespace {
using nativescript::common::platform::UrlParts;
bool IsSpecial(const std::string& scheme) {
return scheme == "file" || scheme == "http" || scheme == "https" ||
scheme == "ws" || scheme == "wss" || scheme == "ftp";
}
void Encode(std::string& out, char ch, bool inPath, bool specialQuery = false) {
static constexpr char kHex[] = "0123456789ABCDEF";
const auto c = static_cast<unsigned char>(ch);
bool encode = c < 0x20 || c >= 0x7F || c == ' ' || c == '"' || c == '<' ||
c == '>' || c == '#';
if (inPath) {
encode = encode || c == '?' || c == '^' || c == '`' || c == '{' || c == '}';
} else if (specialQuery) {
encode = encode || c == '\'';
}
if (encode) {
out += '%';
out += kHex[c >> 4];
out += kHex[c & 0xF];
} else {
out += ch;
}
}
bool EqualsIgnoringCase(std::string_view a, std::string_view b) {
if (a.size() != b.size()) {
return false;
}
for (size_t i = 0; i < a.size(); i++) {
if (std::tolower(static_cast<unsigned char>(a[i])) !=
std::tolower(static_cast<unsigned char>(b[i]))) {
return false;
}
}
return true;
}
// The URL standard's single- and double-dot path segments, which include
// their percent-encoded spellings.
bool IsSingleDot(std::string_view segment) {
return segment == "." || EqualsIgnoringCase(segment, "%2e");
}
bool IsDoubleDot(std::string_view segment) {
return segment == ".." || EqualsIgnoringCase(segment, ".%2e") ||
EqualsIgnoringCase(segment, "%2e.") ||
EqualsIgnoringCase(segment, "%2e%2e");
}
// `path` (starting with '/') with its dot segments removed and its bytes
// percent-encoded.
std::string SerializePath(std::string_view path) {
std::vector<std::string> segments;
size_t start = 1;
while (start <= path.size()) {
size_t end = path.find('/', start);
if (end == std::string_view::npos) {
end = path.size();
}
const std::string_view segment = path.substr(start, end - start);
const bool last = end == path.size();
if (IsDoubleDot(segment)) {
if (!segments.empty()) {
segments.pop_back();
}
if (last) {
segments.emplace_back();
}
} else if (IsSingleDot(segment)) {
if (last) {
segments.emplace_back();
}
} else {
segments.emplace_back(segment);
}
start = end + 1;
}
std::string out;
for (const std::string& segment : segments) {
out += '/';
for (const char c : segment) {
Encode(out, c, true);
}
}
return out.empty() ? "/" : out;
}
struct Pieces {
std::string scheme;
bool hasAuthority = false;
// A non-special URL with neither an authority nor a '/' after its scheme
// (`app:x.mjs`): its path is kept as written.
bool opaque = false;
std::string host;
std::string path;
std::optional<std::string> query;
};
std::optional<Pieces> ParseAbsolute(std::string_view input) {
const size_t colon = input.find(':');
if (colon == std::string_view::npos || colon == 0 ||
!std::isalpha(static_cast<unsigned char>(input[0]))) {
return std::nullopt;
}
Pieces pieces;
for (size_t i = 0; i < colon; i++) {
const char c = input[i];
if (!std::isalnum(static_cast<unsigned char>(c)) && c != '+' && c != '-' &&
c != '.') {
return std::nullopt;
}
pieces.scheme +=
static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
}
std::string rest(input.substr(colon + 1));
const bool special = IsSpecial(pieces.scheme);
if (special) {
for (char& c : rest) {
if (c == '\\') {
c = '/';
}
}
}
rest = rest.substr(0, rest.find('#'));
const size_t q = rest.find('?');
if (q != std::string::npos) {
pieces.query = rest.substr(q + 1);
rest = rest.substr(0, q);
}
if (rest.rfind("//", 0) == 0) {
pieces.hasAuthority = true;
const size_t slash = rest.find('/', 2);
pieces.host =
rest.substr(2, slash == std::string::npos ? slash : slash - 2);
rest = slash == std::string::npos ? std::string() : rest.substr(slash);
for (char& c : pieces.host) {
c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
}
if (pieces.scheme == "file" &&
(pieces.host.find(':') != std::string::npos)) {
return std::nullopt;
}
if (pieces.scheme == "file" && pieces.host == "localhost") {
pieces.host.clear();
}
} else if (special && pieces.scheme != "file") {
return std::nullopt;
}
if (!special && !pieces.hasAuthority && (rest.empty() || rest[0] != '/')) {
pieces.opaque = true;
pieces.path = rest;
return pieces;
}
if (rest.empty() || rest[0] != '/') {
rest = "/" + rest;
}
pieces.path = rest;
return pieces;
}
UrlParts Serialize(const Pieces& pieces) {
UrlParts parts;
parts.protocol = pieces.scheme + ":";
parts.host = pieces.host;
const bool emptyPath =
!IsSpecial(pieces.scheme) && pieces.path == "/" && pieces.hasAuthority;
if (pieces.opaque) {
// The C0 control percent-encode set.
for (const char c : pieces.path) {
const auto byte = static_cast<unsigned char>(c);
if (byte < 0x20 || byte >= 0x7F) {
Encode(parts.pathname, c, false);
} else {
parts.pathname += c;
}
}
} else {
parts.pathname = emptyPath ? std::string() : SerializePath(pieces.path);
}
if (pieces.query) {
std::string query;
for (const char c : *pieces.query) {
Encode(query, c, false, IsSpecial(pieces.scheme));
}
parts.query = query;
}
parts.href = parts.protocol;
if (pieces.hasAuthority || pieces.scheme == "file") {
parts.href += "//" + parts.host;
}
parts.href += parts.pathname;
if (parts.query) {
parts.href += "?" + *parts.query;
}
return parts;
}
} // namespace
namespace nativescript::common::platform {
std::optional<UrlParts> ParseUrl(std::string_view rawInput,
std::string_view base) {
std::string input;
for (const char c : rawInput) {
if (c != '\t' && c != '\n' && c != '\r') {
input += c;
}
}
std::optional<Pieces> absolute = ParseAbsolute(input);
if (absolute || base.empty()) {
if (!absolute) {
return std::nullopt;
}
return Serialize(*absolute);
}
std::optional<Pieces> resolved = ParseAbsolute(base);
if (!resolved || resolved->opaque) {
return std::nullopt;
}
std::string relative(input.substr(0, input.find('#')));
if (IsSpecial(resolved->scheme)) {
for (char& c : relative) {
if (c == '\\') {
c = '/';
}
}
}
if (relative.rfind("//", 0) == 0) {
const std::optional<Pieces> pieces =
ParseAbsolute(resolved->scheme + ":" + relative);
if (!pieces) {
return std::nullopt;
}
return Serialize(*pieces);
}
std::optional<std::string> query;
const size_t q = relative.find('?');
if (q != std::string::npos) {
query = relative.substr(q + 1);
relative = relative.substr(0, q);
}
if (relative.empty()) {
if (!query) {
query = resolved->query;
}
} else if (relative[0] == '/') {
resolved->path = relative;
} else {
resolved->path =
resolved->path.substr(0, resolved->path.find_last_of('/') + 1) +
relative;
}
resolved->query = query;
return Serialize(*resolved);
}
} // namespace nativescript::common::platform
namespace nativescript::common::platform {
ModuleUrl ParseModuleUrl(std::string_view input, std::string_view base) {
ModuleUrl result;
const std::optional<UrlParts> parts = ParseUrl(input, base);
if (!parts) {
return result;
}
result.parsed = true;
result.http = parts->protocol == "http:" || parts->protocol == "https:";
result.href = parts->href;
return result;
}
std::string BlobStoreUrl(std::string_view input) {
if (input.size() < 5) {
return std::string();
}
std::string scheme(input.substr(0, 5));
for (char& c : scheme) {
c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
}
if (scheme != "blob:") {
return std::string();
}
return scheme + std::string(input.substr(5, input.find('#') - 5));
}
} // namespace nativescript::common::platform