Merge pull request #810 from adriadam10/feat/http-uri-merge

feat(libSceHttp): implement sceHttpUriMerge
This commit is contained in:
Dean
2026-10-07 10:05:32 +13:00
committed by GitHub
3 changed files with 118 additions and 4 deletions
+65 -4
View File
@@ -1,3 +1,4 @@
#include <algorithm>
#include <cctype>
#include <charconv>
#include <cstdint>
@@ -19,6 +20,7 @@ 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;
@@ -72,14 +74,23 @@ std::string removeDotSegments(std::string_view input) {
return output;
}
int parseUri(std::string_view uri, UriParts& parts) {
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;
}
if (schemeEnd > 0 && schemeEnd < uri.size() && uri[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);
}
@@ -140,6 +151,13 @@ int parseUri(std::string_view uri, UriParts& parts) {
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;
@@ -218,6 +236,50 @@ int APS5_VABI sceHttpUriUnescape(char* out, size_t* require, size_t prepare, con
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;
@@ -227,8 +289,7 @@ int APS5_VABI sceHttpUriParse(SceHttpUriElement* out, const char* src_url, void*
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};
size_t size = 0;
for (const std::string_view field : fields) size += field.size() + 1;
const size_t size = poolSize(parts);
if (require) *require = size;
if (!write) return 0;
if (prepare < size) return ERROR_OUT_OF_MEMORY;
+52
View File
@@ -7,6 +7,7 @@
extern "C" {
int APS5_VABI sceHttpUriParse(SceHttpUriElement*, const char*, void*, std::size_t*, std::size_t);
int APS5_VABI sceHttpUriMerge(char*, const char*, const char*, std::size_t*, std::size_t, std::uint32_t);
int APS5_VABI sceHttpSetInflateGZIPEnabled(int, int);
int APS5_VABI sceHttpUriBuild(char*, std::size_t*, std::size_t, const SceHttpUriElement*, std::uint32_t);
int APS5_VABI sceHttpUriEscape(char*, std::size_t*, std::size_t, const char*);
@@ -126,6 +127,57 @@ int main() {
Require(sceHttpUriUnescape(decodedBytes, &required, sizeof(decodedBytes), encodedBytes) == 0);
Require(required == sizeof(bytes) && std::memcmp(bytes, decodedBytes, sizeof(bytes)) == 0);
const char* base = "http://foo.com/foo/index.html";
const std::size_t baseMergeSize = 5 + 1 + 1 + 8 + 16 + 1 + 1 + 2;
char merged[512];
auto merge = [&](const char* mergeBase, const char* relative, const char* expected) {
std::memset(merged, 'Z', sizeof(merged));
return sceHttpUriMerge(merged, mergeBase, relative, &required, sizeof(merged), 0) == 0 && Equal(merged, expected);
};
Require(merge(base, "./default.html", "http://foo.com/foo/./default.html"));
Require(required == baseMergeSize + 2 * (29 + 14));
Require(merge(base, "../sibling.html", "http://foo.com/foo/../sibling.html"));
Require(merge(base, "", "http://foo.com/foo/"));
Require(merge(base, "/root.html", "http://foo.com/root.html"));
Require(merge(base, "a?q=1#f", "http://foo.com/foo/a?q=1#f"));
Require(merge("https://u:p@foo.com:8443/a/b?x=1#top", "c", "https://u:p@foo.com:8443/a/c"));
Require(merge("http://foo.com", "x", "http://foo.com/x"));
Require(merge("http://foo.com:80/", "x", "http://foo.com/x"));
Require(merge("http://foo.com/a/b", "mailto:x", "http://foo.com/a/mailto:x"));
Require(merge(base, "a%20b.html", "http://foo.com/foo/a%20b.html"));
Require(merge(base, "~user/x", "http://foo.com/foo/~user/x"));
Require(merge(base, "file(1).png", "http://foo.com/foo/file(1).png"));
Require(merge(base, "a+b=c;d!e", "http://foo.com/foo/a+b=c;d!e"));
Require(merge(base, "http://bar.com/other", "http://bar.com/other") && required == 21);
const std::size_t absoluteSize = baseMergeSize + 2 * (29 + 20);
for (std::size_t i = 21; i < absoluteSize; ++i) Require(merged[i] == '\0');
Require(merged[absoluteSize] == 'Z');
Require(merge(base, "//bar.com/x", "//bar.com/x") && required == 12);
required = 0;
Require(sceHttpUriMerge(nullptr, base, "./default.html", &required, 0, 0) == 0);
Require(required == baseMergeSize + 2 * (29 + 14));
Require(sceHttpUriMerge(nullptr, base, "http://bar.com/other", &required, 0, 0) == 0);
Require(required == absoluteSize);
Require(sceHttpUriMerge(nullptr, base, "x", nullptr, 0, 0) == 0);
std::memset(merged, 'Z', sizeof(merged));
Require(sceHttpUriMerge(merged, base, "./default.html", &required, baseMergeSize + 2 * (29 + 14) - 1, 0) == outOfMemory);
Require(required == baseMergeSize + 2 * (29 + 14) && merged[0] == 'Z');
Require(sceHttpUriMerge(merged, base, "http://bar.com/other", &required, absoluteSize - 1, 0) == outOfMemory);
Require(merged[0] == 'Z');
Require(sceHttpUriMerge(merged, base, "./default.html", nullptr, baseMergeSize + 2 * (29 + 14), 0) == 0);
Require(Equal(merged, "http://foo.com/foo/./default.html"));
required = 123;
Require(sceHttpUriMerge(merged, nullptr, "./x", &required, sizeof(merged), 0) == invalidValue);
Require(sceHttpUriMerge(merged, base, nullptr, &required, sizeof(merged), 0) == invalidValue);
Require(sceHttpUriMerge(merged, base, "./x", &required, sizeof(merged), 1) == invalidValue);
Require(sceHttpUriMerge(nullptr, nullptr, nullptr, &required, 0, 1) == invalidValue);
Require(sceHttpUriMerge(merged, "http://bad host/", "./x", &required, sizeof(merged), 0) == invalidUrl);
Require(sceHttpUriMerge(merged, base, "http://bad host/", &required, sizeof(merged), 0) == invalidUrl);
Require(required == 123);
HttpEpollHandle epoll = nullptr;
Require(sceHttpCreateEpoll(1, nullptr) == invalidValue);
Require(sceHttpCreateEpoll(1, &epoll) == 0 && epoll != nullptr);
+1
View File
@@ -110,6 +110,7 @@ Throughout the project, every function at every stage either **does exactly what
- [NGS2 custom submixer rack](../../core/libs/prx/libSceNgs2.native/src/Custom.cpp) (libSceNgs2.native, rack 0x4002) - structure layouts from [shadPS4](https://github.com/shadps4-emu/shadPS4)'s `ngs2_custom.h`; module id 0x1f for UserFx2, the voice parameter id `0x40001f00 | module index`, the process flags (1 on the first process call after setup, 2 after a parameter change, 0 otherwise) and the call order (setup per voice at rack creation, process after the inputs are mixed, cleanup at destruction) come only from [KytyPS5](https://github.com/KytyPS5/KytyPS5). Only UserFx2 modules on a single buffer are accepted: other module ids, more buffers, a module control handler, different input and output channel counts, setup flags and default rack options throw. Each module's state is a separate buffer, not the `state_offset` slice of the voice state, and `sceNgs2VoiceGetState` still throws for this rack. As in KytyPS5, process runs only on grains where an input produced samples, so an effect tail stops with the input and a pending flag 2 waits for the next audible grain
- [sceHttpParseResponseHeader](../../core/libs/prx/libSceHttp/Export.cpp) (libSceHttp) - signature follows [SharpProspero](https://github.com/SvenGDK/SharpProspero/blob/main/src/SharpProspero/Interop/Net/Http.cs); parsing and error codes follow [shadPS4's reverse-engineered PS4 library](https://github.com/shadps4-emu/shadPS4/blob/main/src/core/libraries/network/http.cpp), not confirmed on a PS5 title or hardware. Folded values retain their line breaks and whitespace; an empty first line starts the value scan after its line feed. A consumed byte count above `INT_MAX` throws instead of wrapping.
- [sceHttpParseStatusLine](../../core/libs/prx/libSceHttp/src/Parse.cpp) (libSceHttp) - behaviour and error codes follow the [shadPS4](https://github.com/shadps4-emu/shadPS4) reimplementation of the PS4 library, not confirmed on a PS5 title; unlike it, no byte past `lineLen` is read
- [sceHttpUriMerge](../../core/libs/prx/libSceHttp/src/Uri.cpp) (libSceHttp) - signature follows [SharpProspero](https://github.com/SvenGDK/SharpProspero/blob/main/src/SharpProspero/Interop/Net/Http.cs); validation, size formula, absolute/relative split and path join follow the disassembly-like body in [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/main/src/core/libraries/network/http.cpp), not confirmed on a PS5 title or hardware. The base URL goes through our `sceHttpUriParse`, which removes dot segments from the base path, while the relative part is appended unswept; base query and fragment are dropped. The relative reference is only classified by its scheme and a leading `//`, and only references with `//` are validated; as in shadPS4, `//host/x` is returned unchanged instead of taking the base scheme, a reference with a scheme but no `//` (`mailto:x`) is joined onto the base directory, and `?q` and `#f` are appended to the base directory instead of the base path.
- [sceHttpUriUnescape](../../core/libs/prx/libSceHttp/src/Uri.cpp) (libSceHttp) - signature follows [SharpProspero](https://github.com/SvenGDK/SharpProspero/blob/main/src/SharpProspero/Interop/Net/Http.cs); percent decoding, invalid sequences and error codes follow [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/main/src/core/libraries/network/http.cpp), not confirmed on a PS5 title or hardware. Size includes the final NUL; malformed escapes and `+` remain unchanged, and decoding runs once.
- [sceHttpUriSweepPath](../../core/libs/prx/libSceHttp/src/Uri.cpp) (libSceHttp) - behaviour follows the [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/main/src/core/libraries/network/http.cpp) reimplementation of the PS4 library and its tests (including the PS4 SDK example), not confirmed on a PS5 title or hardware. It is not RFC 3986 5.2.4: a path that doesn't start with `/` is copied unchanged and a trailing `.` or `..` without a slash is kept. Unlike shadPS4, nothing at or past `srcSize - 1` is read, and `srcSize == 1` writes only the terminator
- [sceHttpSetResponseHeaderMaxSize, sceHttpRedirectCacheFlush, sceHttpsUnloadCert](../../core/libs/prx/libSceHttp/Export.cpp) (libSceHttp) - signatures follow [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/main/src/core/libraries/network/http.cpp) (PS4); no PS5 reference has them