Repository navigation
Expand file tree
/
Copy pathAdaUrl.cpp
More file actions
162 lines (147 loc) · 4.85 KB
/
Copy pathAdaUrl.cpp
File metadata and controls
162 lines (147 loc) · 4.85 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
// platform::ParseUrl, ParseModuleUrl and BlobStoreUrl over ada, for host
// tests configured with NS_ADA_DIR. A runtime implements them the same way
// over its own copy of ada.
#include <optional>
#include <string>
#include <string_view>
#include <utility>
#include "ada.h"
#include "ns/Platform.h"
namespace nativescript::common::platform {
std::optional<UrlParts> ParseUrl(std::string_view input,
std::string_view base) {
ada::result<ada::url_aggregator> url;
if (base.empty()) {
url = ada::parse<ada::url_aggregator>(input, nullptr);
} else {
auto baseUrl = ada::parse<ada::url_aggregator>(base, nullptr);
if (!baseUrl) {
return std::nullopt;
}
url = ada::parse<ada::url_aggregator>(input, &baseUrl.value());
}
if (!url) {
return std::nullopt;
}
UrlParts parts;
parts.href = std::string(url->get_href());
parts.protocol = std::string(url->get_protocol());
parts.host = std::string(url->get_host());
parts.pathname = std::string(url->get_pathname());
if (url->has_search()) {
const std::string_view search = url->get_search();
parts.query =
search.empty() ? std::string() : std::string(search.substr(1));
}
return parts;
}
} // namespace nativescript::common::platform
namespace {
bool HasHttpScheme(const ada::url_aggregator& url) {
return url.type == ada::scheme::type::HTTP ||
url.type == ada::scheme::type::HTTPS;
}
// The authority (userinfo, host, port) of an absolute or scheme-relative URL
// exactly as written, or empty when `url` does not spell one plainly.
std::string_view WrittenAuthority(std::string_view url) {
size_t start = 0;
if (url.substr(0, 2) == "//") {
start = 2;
} else {
const size_t colon = url.find(':');
if (colon == std::string_view::npos || url.substr(colon, 3) != "://") {
return std::string_view();
}
start = colon + 3;
}
const size_t end = url.find_first_of("/?#", start);
const std::string_view authority =
url.substr(start, end == std::string_view::npos ? end : end - start);
for (const char c : authority) {
if (static_cast<unsigned char>(c) <= 0x20 || c == '\\') {
return std::string_view();
}
}
return authority;
}
// A fast reject before the parse, which allocates: whether `input` starts
// with "blob:" as the parser reads a scheme, after leading C0 controls and
// spaces, in any ASCII case, with tabs and newlines anywhere ignored.
bool StartsWithBlobScheme(std::string_view input) {
size_t i = 0;
while (i < input.size() && static_cast<unsigned char>(input[i]) <= 0x20) {
i++;
}
for (const char want : std::string_view("blob:")) {
while (i < input.size() &&
(input[i] == '\t' || input[i] == '\n' || input[i] == '\r')) {
i++;
}
if (i == input.size()) {
return false;
}
const char c = input[i++];
if ((c >= 'A' && c <= 'Z' ? static_cast<char>(c + ('a' - 'A')) : c) !=
want) {
return false;
}
}
return true;
}
} // namespace
namespace nativescript::common::platform {
ModuleUrl ParseModuleUrl(std::string_view input, std::string_view base) {
ModuleUrl result;
ada::result<ada::url_aggregator> url;
if (base.empty()) {
url = ada::parse<ada::url_aggregator>(input, nullptr);
} else {
auto baseUrl = ada::parse<ada::url_aggregator>(base, nullptr);
if (!baseUrl) {
return result;
}
url = ada::parse<ada::url_aggregator>(input, &baseUrl.value());
}
if (!url) {
return result;
}
result.parsed = true;
result.http = HasHttpScheme(*url);
result.href = std::string(url->get_href());
if (!result.http) {
return result;
}
const std::string_view authority = WrittenAuthority(
base.empty() || input.substr(0, 2) == "//" ? input : base);
if (authority.empty()) {
return result;
}
const std::string_view href = url->get_href();
const size_t authorityStart = url->get_protocol().size() + 2;
const size_t pathnameStart = url->get_components().pathname_start;
if (href.substr(authorityStart, pathnameStart - authorityStart) ==
authority) {
return result;
}
// Everything after the authority comes from the parser's own href: its
// getters drop an empty query's '?' and an empty fragment's '#'.
std::string written(url->get_protocol());
written.append("//").append(authority).append(href.substr(pathnameStart));
auto reparsed = ada::parse<ada::url_aggregator>(written, nullptr);
if (reparsed && reparsed->get_href() == href) {
result.href = std::move(written);
}
return result;
}
std::string BlobStoreUrl(std::string_view input) {
if (!StartsWithBlobScheme(input)) {
return std::string();
}
auto url = ada::parse<ada::url_aggregator>(input, nullptr);
if (!url || url->get_protocol() != "blob:") {
return std::string();
}
url->set_hash("");
return std::string(url->get_href());
}
} // namespace nativescript::common::platform