Repository navigation
Expand file tree
/
Copy pathUri.cpp
More file actions
359 lines (317 loc) · 13.7 KB
/
Copy pathUri.cpp
File metadata and controls
359 lines (317 loc) · 13.7 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#include <algorithm>
#include <cctype>
#include <charconv>
#include <cstdint>
#include <cstddef>
#include <cstring>
#include <string>
#include <string_view>
#include "SceTypes.hpp"
#include "prx/libc/include/General.hpp"
#include "prx/libSceHttp/src/HttpErrors.hpp"
namespace {
constexpr uint32_t URI_BUILD_WITH_SCHEME = 0x01;
constexpr uint32_t URI_BUILD_WITH_HOSTNAME = 0x02;
constexpr uint32_t URI_BUILD_WITH_PORT = 0x04;
constexpr uint32_t URI_BUILD_WITH_PATH = 0x08;
constexpr uint32_t URI_BUILD_WITH_USERNAME = 0x10;
constexpr uint32_t URI_BUILD_WITH_PASSWORD = 0x20;
constexpr uint32_t URI_BUILD_WITH_QUERY = 0x40;
constexpr uint32_t URI_BUILD_WITH_FRAGMENT = 0x80;
constexpr size_t URI_MAX_LENGTH = 0x3FFF;
struct UriParts {
bool opaque = false;
std::string_view scheme;
std::string_view username;
std::string_view password;
std::string_view hostname;
std::string path;
std::string_view query;
std::string_view fragment;
uint16_t port = 0;
};
bool equalsIgnoreCase(std::string_view left, std::string_view right) {
if (left.size() != right.size()) return false;
for (size_t i = 0; i < left.size(); ++i) {
if (std::tolower(static_cast<unsigned char>(left[i])) != std::tolower(static_cast<unsigned char>(right[i]))) return false;
}
return true;
}
uint16_t defaultPort(std::string_view scheme) {
if (equalsIgnoreCase(scheme, "http")) return 80;
if (equalsIgnoreCase(scheme, "https")) return 443;
return 0;
}
bool isHostCharacter(char c) {
return std::isalnum(static_cast<unsigned char>(c)) || c == '-' || c == '.' || c == '_';
}
std::string removeDotSegments(std::string_view input) {
std::string output;
while (!input.empty()) {
if (input.starts_with("../")) input.remove_prefix(3);
else if (input.starts_with("./")) input.remove_prefix(2);
else if (input.starts_with("/./")) input.remove_prefix(2);
else if (input == "/.") input = "/";
else if (input.starts_with("/../") || input == "/..") {
input = input.size() == 3 ? std::string_view("/") : input.substr(3);
const size_t slash = output.rfind('/');
output.erase(slash == std::string::npos ? 0 : slash);
} else if (input == "." || input == "..") input = {};
else {
const size_t end = input.find('/', 1);
const size_t length = end == std::string_view::npos ? input.size() : end;
output.append(input.substr(0, length));
input.remove_prefix(length);
}
}
return output;
}
size_t schemeLength(std::string_view uri) {
size_t schemeEnd = 0;
if (!uri.empty() && std::isalpha(static_cast<unsigned char>(uri[0]))) {
schemeEnd = 1;
while (schemeEnd < uri.size() && (std::isalnum(static_cast<unsigned char>(uri[schemeEnd])) || uri[schemeEnd] == '+'
|| uri[schemeEnd] == '-' || uri[schemeEnd] == '.')) ++schemeEnd;
}
return schemeEnd > 0 && schemeEnd < uri.size() && uri[schemeEnd] == ':' ? schemeEnd : 0;
}
bool isOpaque(std::string_view uri) {
const size_t schemeEnd = schemeLength(uri);
return !uri.substr(schemeEnd == 0 ? 0 : schemeEnd + 1).starts_with("//");
}
int parseUri(std::string_view uri, UriParts& parts) {
if (const size_t schemeEnd = schemeLength(uri); schemeEnd != 0) {
parts.scheme = uri.substr(0, schemeEnd);
uri.remove_prefix(schemeEnd + 1);
}
parts.opaque = !uri.starts_with("//");
if (!parts.opaque) uri.remove_prefix(2);
std::string_view authority = uri.substr(0, uri.find_first_of("/?#"));
uri.remove_prefix(authority.size());
const size_t at = authority.rfind('@');
if (at != std::string_view::npos) {
const std::string_view userinfo = authority.substr(0, at);
const size_t colon = userinfo.find(':');
parts.username = userinfo.substr(0, colon);
if (colon != std::string_view::npos) parts.password = userinfo.substr(colon + 1);
authority.remove_prefix(at + 1);
}
std::string_view port;
if (authority.starts_with('[')) {
const size_t close = authority.find(']');
if (close == std::string_view::npos) return ERROR_INVALID_URL;
parts.hostname = authority.substr(1, close - 1);
authority.remove_prefix(close + 1);
if (!authority.empty() && authority[0] != ':') return ERROR_INVALID_URL;
if (!authority.empty()) port = authority.substr(1);
for (const char c : parts.hostname) {
if (!isHostCharacter(c) && c != ':') return ERROR_INVALID_URL;
}
} else {
const size_t colon = authority.find(':');
parts.hostname = authority.substr(0, colon);
if (colon != std::string_view::npos) port = authority.substr(colon + 1);
for (const char c : parts.hostname) {
if (!isHostCharacter(c)) return ERROR_INVALID_URL;
}
}
if (port.size() > 5) return ERROR_INVALID_URL;
uint32_t portValue = 0;
for (const char c : port) {
if (!std::isdigit(static_cast<unsigned char>(c))) return ERROR_INVALID_URL;
portValue = portValue * 10 + static_cast<uint32_t>(c - '0');
}
if (portValue > 0xFFFF) return ERROR_INVALID_URL;
parts.port = port.empty() ? defaultPort(parts.scheme) : static_cast<uint16_t>(portValue);
const std::string_view path = uri.substr(0, uri.find_first_of("?#"));
parts.path = removeDotSegments(path);
uri.remove_prefix(path.size());
if (uri.starts_with('?')) {
parts.query = uri.substr(0, uri.find('#'));
uri.remove_prefix(parts.query.size());
}
parts.fragment = uri;
return 0;
}
size_t poolSize(const UriParts& parts) {
const std::string_view fields[] = {parts.scheme, parts.username, parts.password, parts.hostname, parts.path, parts.query, parts.fragment};
size_t size = 0;
for (const std::string_view field : fields) size += field.size() + 1;
return size;
}
int copyOut(const std::string& value, char* out, size_t* require, size_t prepare) {
if (require) *require = value.size() + 1;
if (!out) return 0;
if (prepare < value.size() + 1) return ERROR_OUT_OF_MEMORY;
std::memcpy(out, value.c_str(), value.size() + 1);
return 0;
}
}
extern "C" {
int APS5_VABI sceHttpUriBuild(char* out, size_t* require, size_t prepare, const SceHttpUriElement* src_element, uint32_t option) {
if (!src_element) return ERROR_INVALID_URL;
if (!out && !require) return ERROR_INVALID_VALUE;
auto field = [](const char* value) { return value ? std::string_view(value) : std::string_view(); };
const std::string_view scheme = field(src_element->scheme);
const std::string_view username = option & URI_BUILD_WITH_USERNAME ? field(src_element->username) : std::string_view();
const std::string_view password = option & URI_BUILD_WITH_PASSWORD ? field(src_element->password) : std::string_view();
const std::string_view hostname = option & URI_BUILD_WITH_HOSTNAME ? field(src_element->hostname) : std::string_view();
std::string uri;
if (option & URI_BUILD_WITH_SCHEME && !scheme.empty()) uri.append(scheme).push_back(':');
if (!src_element->opaque) uri.append("//");
uri.append(username);
if (!password.empty()) uri.append(":").append(password);
if (!username.empty() || !password.empty()) uri.push_back('@');
if (hostname.find(':') != std::string_view::npos) uri.append("[").append(hostname).append("]");
else uri.append(hostname);
if (option & URI_BUILD_WITH_PORT && src_element->port != 0 && src_element->port != defaultPort(scheme)) {
uri.append(":").append(std::to_string(src_element->port));
}
if (option & URI_BUILD_WITH_PATH) uri.append(field(src_element->path));
if (option & URI_BUILD_WITH_QUERY) uri.append(field(src_element->query));
if (option & URI_BUILD_WITH_FRAGMENT) uri.append(field(src_element->fragment));
return copyOut(uri, out, require, prepare);
}
int APS5_VABI sceHttpUriEscape(char* out, size_t* require, size_t prepare, const char* in) {
if (!in) return ERROR_INVALID_VALUE;
if (!out && !require) return ERROR_INVALID_VALUE;
static constexpr char hex[] = "0123456789ABCDEF";
std::string escaped;
for (const char* c = in; *c; ++c) {
const auto value = static_cast<unsigned char>(*c);
if (std::isalnum(value) || value == '-' || value == '_' || value == '.' || value == '~') {
escaped.push_back(*c);
} else {
escaped.push_back('%');
escaped.push_back(hex[value >> 4]);
escaped.push_back(hex[value & 0xF]);
}
}
return copyOut(escaped, out, require, prepare);
}
int APS5_VABI sceHttpUriUnescape(char* out, size_t* require, size_t prepare, const char* in) {
if (!in) return ERROR_INVALID_VALUE;
const std::string_view input(in);
std::string decoded;
for (size_t i = 0; i < input.size(); ++i) {
if (input[i] == '%' && input.size() - i >= 3) {
unsigned int value = 0;
const auto result = std::from_chars(input.data() + i + 1, input.data() + i + 3, value, 16);
if (result.ec == std::errc{} && result.ptr == input.data() + i + 3) {
decoded.push_back(static_cast<char>(value));
i += 2;
continue;
}
}
decoded.push_back(input[i]);
}
return copyOut(decoded, out, require, prepare);
}
int APS5_VABI sceHttpUriMerge(char* merged_url, const char* url, const char* relative_uri, size_t* require, size_t prepare, uint32_t option) {
if (option != 0 || !url || !relative_uri) return ERROR_INVALID_VALUE;
UriParts base;
if (const int result = parseUri(url, base); result != 0) return result;
const bool relativeOpaque = isOpaque(relative_uri);
if (UriParts relative; !relativeOpaque) {
if (const int result = parseUri(relative_uri, relative); result != 0) return result;
}
const size_t urlLength = strnlen(url, URI_MAX_LENGTH);
const size_t relativeLength = strnlen(relative_uri, URI_MAX_LENGTH);
const size_t size = poolSize(base) + 2 + (urlLength + relativeLength) * 2;
if (require) *require = size;
if (!merged_url) return 0;
if (prepare < size) return ERROR_OUT_OF_MEMORY;
if (!relativeOpaque) {
std::strncpy(merged_url, relative_uri, size);
merged_url[size - 1] = '\0';
if (require) *require = relativeLength + 1;
return 0;
}
std::string path = base.path;
const size_t slash = path.rfind('/');
if (slash == std::string::npos) path.push_back('/');
else path.erase(slash + 1);
if (relative_uri[0] == '/') path.clear();
path.append(relative_uri, std::min(relativeLength, URI_MAX_LENGTH - std::min(path.size(), URI_MAX_LENGTH)));
std::string scheme(base.scheme), username(base.username), password(base.password), hostname(base.hostname);
SceHttpUriElement element;
element.opaque = base.opaque;
element.scheme = scheme.data();
element.username = username.data();
element.password = password.data();
element.hostname = hostname.data();
element.path = path.data();
element.port = base.port;
return sceHttpUriBuild(merged_url, nullptr, prepare - (urlLength + relativeLength + 1), &element,
URI_BUILD_WITH_SCHEME | URI_BUILD_WITH_HOSTNAME | URI_BUILD_WITH_PORT | URI_BUILD_WITH_PATH | URI_BUILD_WITH_USERNAME | URI_BUILD_WITH_PASSWORD);
}
int APS5_VABI sceHttpUriParse(SceHttpUriElement* out, const char* src_url, void* pool, size_t* require, size_t prepare) {
if (!src_url) return ERROR_INVALID_URL;
const bool write = out && pool;
if (!write && !require) return ERROR_INVALID_VALUE;
UriParts parts;
if (const int result = parseUri(src_url, parts); result != 0) return result;
const std::string_view fields[] = {parts.scheme, parts.username, parts.password, parts.hostname, parts.path, parts.query, parts.fragment};
const size_t size = poolSize(parts);
if (require) *require = size;
if (!write) return 0;
if (prepare < size) return ERROR_OUT_OF_MEMORY;
char* next = static_cast<char*>(pool);
char* strings[7];
for (size_t i = 0; i < 7; ++i) {
strings[i] = next;
if (!fields[i].empty()) std::memcpy(next, fields[i].data(), fields[i].size());
next[fields[i].size()] = '\0';
next += fields[i].size() + 1;
}
*out = {};
out->opaque = parts.opaque;
out->scheme = strings[0];
out->username = strings[1];
out->password = strings[2];
out->hostname = strings[3];
out->path = strings[4];
out->query = strings[5];
out->fragment = strings[6];
out->port = parts.port;
return 0;
}
int APS5_VABI sceHttpUriSweepPath(char* dst, const char* src, size_t srcSize) {
if (srcSize == 0) return 0;
if (!dst || !src) return ERROR_INVALID_VALUE;
const size_t length = srcSize - 1;
if (length == 0 || src[0] != '/') {
std::memcpy(dst, src, length);
dst[length] = '\0';
return 0;
}
dst[0] = '/';
dst[1] = '\0';
size_t end = 0;
size_t pos = 1;
while (pos < length) {
if (src[pos] == '.' && pos + 1 < length && src[pos + 1] == '/') {
pos += 2;
continue;
}
if (src[pos] == '.' && pos + 2 < length && src[pos + 1] == '.' && src[pos + 2] == '/') {
if (end != 0) {
dst[end] = '\0';
end = static_cast<size_t>(std::strrchr(dst, '/') - dst);
dst[end + 1] = '\0';
}
pos += 3;
continue;
}
size_t count = length - pos;
if (const void* slash = std::memchr(src + pos, '/', length - pos)) {
count = static_cast<size_t>(static_cast<const char*>(slash) + 1 - (src + pos));
}
std::memcpy(dst + end + 1, src + pos, count);
dst[end + 1 + count] = '\0';
end += count;
pos += count;
}
return 0;
}
}