Repository navigation
Expand file tree
/
Copy pathModuleUrl.cpp
More file actions
152 lines (138 loc) · 4.6 KB
/
Copy pathModuleUrl.cpp
File metadata and controls
152 lines (138 loc) · 4.6 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
// The URL functions of ns/Platform.h, over the runtime's ada.
#include "ModuleUrl.h"
#include <string_view>
#include <utility>
#include "ada/ada.h"
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
std::string HttpUrlOrigin(const std::string& url) {
auto parsed = ada::parse<ada::url_aggregator>(url, nullptr);
if (!parsed || !HasHttpScheme(*parsed)) {
return std::string();
}
return parsed->get_origin();
}
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 NormalizeHttpModuleUrl(const std::string& input) {
// Fast reject only: the parser drops leading C0 controls and spaces, after
// which an http(s) URL (or an HTTP key) starts with 'h' or 'H'.
size_t first = 0;
while (first < input.size() &&
static_cast<unsigned char>(input[first]) <= 0x20) {
first++;
}
if (first == input.size() || (input[first] != 'h' && input[first] != 'H')) {
return std::string();
}
const ModuleUrl url = ParseModuleUrl(input, std::string_view());
if (url.parsed) {
return url.http ? url.href : std::string();
}
return loader::IsHttpRegistryKey(input) ? input : std::string();
}
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