Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion core/libs/prx/libSceHttp2/Export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,15 @@ int APS5_VABI sceHttp2SetRequestNoContentLength(int id) {
return Update(id, __func__, [&](auto& settings) { settings.noContentLength = true; });
}

extern "C" void Http2SetLookupHook_nid_no_patch(Http2::LookupHook hook) {
Http2::LookupHookSlot().store(hook, std::memory_order_release);
}

int APS5_VABI sceHttp2SetResolveRetry(int id, int32_t retry) {
return Update(id, __func__, [&](auto&) { if (retry != 0) NotImplemented_nid_no_patch("sceHttp2SetResolveRetry"); });
return Update(id, __func__, [&](auto& settings) {
if (retry < 0) NotImplemented_nid_no_patch("sceHttp2SetResolveRetry");
settings.resolveRetry = retry;
});
}

int APS5_VABI sceHttp2WebSocketCreateRequest(std::int32_t templateId, const char* url, Http2WebSocketTextCallback textCallback, Http2WebSocketDataCallback dataCallback, Http2WebSocketCloseCallback closeCallback, Http2WebSocketErrorCallback errorCallback, void* receiveBuffer, std::size_t receiveBufferSize, void* userData, const void* option) {
Expand Down
11 changes: 7 additions & 4 deletions core/libs/prx/libSceHttp2/Transport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ class Resolver {
hints.ai_family = AF_UNSPEC;
addrinfo* raw = nullptr;
const auto service = std::to_string(job->port);
if (const auto hook = LookupHookSlot().load(std::memory_order_acquire)) Require(hook(job->host.c_str()) == 0, "sceHttp2: DNS failed; console error mapping is unknown");
const int result = getaddrinfo(job->host.c_str(), service.c_str(), &hints, &raw);
std::unique_ptr<addrinfo, decltype(&freeaddrinfo)> addresses(raw, freeaddrinfo);
Require(result == 0, "sceHttp2: DNS failed; console error mapping is unknown");
Expand Down Expand Up @@ -323,16 +324,18 @@ struct Request::State {
Require(startup >= 0, "sceHttp2: network initialization failed; console error mapping is unknown");
Require(sceNetSocketClose(startup) == 0, "sceHttp2: network initialization socket close failed");
#endif
const auto deadline = Deadline(settings.resolveTimeout);
const auto job = Dns().Submit(url);
{
std::shared_ptr<Lookup> job;
for (std::int32_t attempt = 0;; ++attempt) {
const auto deadline = Deadline(settings.resolveTimeout);
job = Dns().Submit(url);
std::unique_lock lock(job->mutex);
while (!job->complete) {
Check(deadline);
job->changed.wait_for(lock, std::chrono::milliseconds(5));
}
Check(deadline);
if (job->failure) std::rethrow_exception(job->failure);
if (!job->failure) break;
if (attempt >= settings.resolveRetry) std::rethrow_exception(job->failure);
}
const auto connectDeadline = Deadline(settings.connectTimeout);
int result = 0;
Expand Down
10 changes: 10 additions & 0 deletions core/libs/prx/libSceHttp2/Transport.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@

namespace Http2 {

using LookupHook = int (*)(const char* host);

inline std::atomic<LookupHook>& LookupHookSlot() {
static std::atomic<LookupHook> hook{nullptr};
return hook;
}

struct Settings {
std::string userAgent;
std::vector<std::pair<std::string, std::string>> headers;
Expand All @@ -20,6 +27,7 @@ struct Settings {
std::uint32_t verifyFlags = 0x3d;
std::uint32_t timeout = 120000000;
std::uint32_t connectTimeout = 30000000;
std::int32_t resolveRetry = 0;
std::uint32_t sendTimeout = 120000000;
std::uint32_t recvTimeout = 120000000;
std::uint32_t resolveTimeout = 30000000;
Expand Down Expand Up @@ -57,3 +65,5 @@ std::shared_ptr<Capacity> CreateCapacity_nid_no_patch(int maximum);
std::shared_ptr<Request> CreateRequest_nid_no_patch(int sslContext, std::shared_ptr<Capacity> capacity, Settings settings, std::string method, std::string url);

}

extern "C" void Http2SetLookupHook_nid_no_patch(Http2::LookupHook hook);
3 changes: 2 additions & 1 deletion core/libs/tests/GuestHttp2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ int main() {
Require(Throws([&] { sceHttp2SetCookieBox(tmpl, box); }));
Require(sceHttp2CookieFlush(box) == 0);
Require(sceHttp2SetResolveRetry(tmpl, 0) == 0);
Require(Throws([&] { sceHttp2SetResolveRetry(tmpl, 3); }));
Require(sceHttp2SetResolveRetry(tmpl, 3) == 0);
Require(Throws([&] { sceHttp2SetResolveRetry(tmpl, -1); }));
Require(sceHttp2SetRequestNoContentLength(request) == 0);
Require(Throws([&] { sceHttp2AddRequestHeader(request, "bad:name", "value", 0); }));
Require(Throws([&] { sceHttp2AddRequestHeader(request, "x-header", "value\r\ninjected: yes", 0); }));
Expand Down
40 changes: 40 additions & 0 deletions core/libs/tests/GuestHttp2Transport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "SceTypes.hpp"
#include "prx/libkernel/include/Api.hpp"
#include "prx/libSceHttp2/include/Http2.hpp"
#include "prx/libSceHttp2/Transport.hpp"
#include "prx/libSceNet/include/Net.hpp"
#include "prx/libSceSsl/include/Ssl.hpp"
#include "prx/libSceSsl/SslTypes.hpp"
Expand Down Expand Up @@ -308,6 +309,7 @@ void Success(int ssl, Mode mode, bool disableCa = false) {
if (disableCa) Require(sceHttp2SslDisableOption(client.tmpl, 0x20) == 0, "disable known CA check");
if (mode == Mode::Normal && !disableCa) Require(sceHttp2SslEnableOption(client.tmpl, 0x4) == 0, "verify localhost certificate name");
Require(sceHttp2AddRequestHeader(client.tmpl, "X-Inherited", "original", 0) == 0, "template header");
Require(sceHttp2SetResolveRetry(client.tmpl, 2) == 0, "resolve retries");
const std::string upload = mode == Mode::Large ? std::string(150000, 'u') : std::string{};
client.Create(server.Url(mode == Mode::Normal && !disableCa ? "localhost" : "127.0.0.1"), mode == Mode::Head ? "HEAD" : upload.empty() ? "GET" : "POST", upload.size());
Require(sceHttp2AddRequestHeader(client.tmpl, "X-Inherited", "changed", 0) == 0, "template mutation");
Expand Down Expand Up @@ -480,6 +482,43 @@ void Refused(int ssl) {
Require(sceHttp2GetStatusCode(client.request, &status) == static_cast<int>(0x8041013d) && status == 123, "failed status untouched");
}

std::atomic<int> lookups{0};
std::atomic<int> failedLookups{0};

int CountedLookup(const char*) {
return ++lookups <= failedLookups.load() ? -1 : 0;
}

void Retries(int ssl) {
std::fprintf(stderr, "HTTP2 resolve retry scenarios\n");
Http2SetLookupHook_nid_no_patch(CountedLookup);
{
Server server(Mode::Normal);
Client client(ssl);
Require(sceHttp2SetResolveRetry(client.tmpl, 2) == 0, "two resolve retries");
client.Create(server.Url());
lookups = 0;
failedLookups = 2;
Require(sceHttp2SendRequest(client.request, nullptr, 0) == 0, "a lookup that fails twice succeeds on its second retry");
Require(lookups == 3, "two failed lookups and the third one were made");
std::array<char, 64> buffer;
std::string body;
for (int count; (count = sceHttp2ReadData(client.request, buffer.data(), buffer.size())) > 0;) body.append(buffer.data(), static_cast<std::size_t>(count));
Require(body == "response body", "body after retried lookups");
server.Join();
}
for (const int retry : {0, 1}) {
Client client(ssl);
Require(sceHttp2SetResolveRetry(client.tmpl, retry) == 0, "resolve retry count");
client.Create("https://127.0.0.1:1/");
lookups = 0;
failedLookups = 100;
Require(Throws([&] { sceHttp2SendRequest(client.request, nullptr, 0); }), "every lookup failing fails the request");
Require(lookups == retry + 1, "a request makes its retry count plus one lookups before failing");
}
Http2SetLookupHook_nid_no_patch(nullptr);
}

void Capacity(int ssl) {
Server server(Mode::StallBody);
Client client(ssl, 1);
Expand Down Expand Up @@ -556,6 +595,7 @@ int main(int argc, char** argv) {
}
Refused(ssl);
Capacity(ssl);
Retries(ssl);
const int untrusted = sceSslInit_nid_postfix(8 * 1024 * 1024);
Require(untrusted > 0, "untrusted SSL context");
Failure(untrusted, Mode::Normal);
Expand Down
4 changes: 2 additions & 2 deletions docs/dev/debt/libSceHttp2.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
- [Default TLS trust](../../../core/libs/prx/libSceHttp2/Transport.cpp) (libSceHttp2) - HTTPS connections also trust the system CA list, `/<sandbox word>/common/cert/CA_LIST.cer`, served by libkernel from the host trust store, after the title's own libSceSsl certificates. RetroArch (PPSA99169, [updater](https://github.com/mihawk-99/PS5_RetroArch/blob/7268510b12fe7461342e0cc4497c87a0910c1055/src/webui_update.cpp#L302-L312)), PS5LM (PPSA99575) and Porpoise (PPSA99764) send version-3 requests to public `https://` servers with no loaded certificate, verify callback or verification option, which pass the known-CA check only if the console trusts public roots by default. That the console's roots are exactly this file is not measured

- [libSceHttp2 transport](../../../core/libs/prx/libSceHttp2/Transport.cpp) - only synchronous HTTPS requests with version 3 and ALPN `h2` are supported. HTTP/1.x, cleartext HTTP/2, negotiation fallback, connection reuse, multiplexing across requests, CONNECT, response trailers, automatic redirects and authentication challenges throw. Version values, method strings, header overwrite/append modes, status/read/content-length signatures and timeout units follow [SharpProspero](https://github.com/SvenGDK/SharpProspero/blob/5dfdb4b0b166e83dc9ae275001afb695f12c7174/src/SharpProspero/Interop/Net/Http2.cs); wire framing and negotiation follow [RFC 9113 sections 3, 5, 6 and 8](https://www.rfc-editor.org/rfc/rfc9113.html). [prosper](https://github.com/mattias800/prosper/blob/b1bec57186a0/prosper/src/hle/net/hle_http2.cpp) instead records the method argument as an integer; the string signature follows SharpProspero. These are public compatibility contracts, not console measurements.
- [libSceHttp2 error paths](../../../core/libs/prx/libSceHttp2/Transport.cpp) - cancellation, deadlines, DNS/TLS/protocol failures, malformed inputs, concurrent calls on one request and exhausted connection slots throw because their console error mappings are unknown. Socket connect errors propagate the libSceNet result, following the module-analysis report in [prosper #2894](https://github.com/mattias800/prosper/issues/2894); the report does not establish every exported error path. A request uses one connection; a context's concurrency cap counts connections until EOF, failure or deletion. Native pooling, scheduling, validation order and teardown semantics remain unverified. DNS uses two host workers and a queue of eight; cancellation/deadlines abandon the caller's wait, not libc's lookup. Library unload can wait for a host lookup to return.
- [libSceHttp2 TLS settings](../../../core/libs/prx/libSceHttp2/Export.cpp) - minimum SSL versions outside 2 to 5, nonnull SSL callbacks and flags outside `0x3d` throw. Supported certificate-check bits follow SharpProspero and use libSceSsl's TLS 1.2 backend and CA context; native defaults and error translation are unverified. Settings on object kinds other than templates/requests throw, except for the request options below; native object-kind restrictions are otherwise unverified. Settings become immutable when transport is created. Zero timeout values, nonzero resolve retries, gzip inflation and connection-slot wait timeouts throw. Host HTTP allocations are not charged to the context pool, so `sceHttp2GetMemoryPoolStats` reports a live context's pool size with zero peak and current use. PPSA01494, PPSA30839 and PPSA31162 read the three sizes for memory logging without checking the result, and stopped at startup while it threw.
- [libSceHttp2 error paths](../../../core/libs/prx/libSceHttp2/Transport.cpp) - cancellation, deadlines, DNS/TLS/protocol failures, malformed inputs, concurrent calls on one request and exhausted connection slots throw because their console error mappings are unknown. Socket connect errors propagate the libSceNet result, following the module-analysis report in [prosper #2894](https://github.com/mattias800/prosper/issues/2894); the report does not establish every exported error path. A request uses one connection; a context's concurrency cap counts connections until EOF, failure or deletion. Native pooling, scheduling, validation order and teardown semantics remain unverified. DNS uses two host workers and a queue of eight; cancellation/deadlines abandon the caller's wait, not libc's lookup. Library unload can wait for a host lookup to return. A resolve retry resubmits a failed lookup with a new timeout; a timed-out lookup isn't retried. The console's retry semantics are unverified.
- [libSceHttp2 TLS settings](../../../core/libs/prx/libSceHttp2/Export.cpp) - minimum SSL versions outside 2 to 5, nonnull SSL callbacks and flags outside `0x3d` throw. Supported certificate-check bits follow SharpProspero and use libSceSsl's TLS 1.2 backend and CA context; native defaults and error translation are unverified. Settings on object kinds other than templates/requests throw, except for the request options below; native object-kind restrictions are otherwise unverified. Settings become immutable when transport is created. Zero timeout values, negative resolve retries, gzip inflation and connection-slot wait timeouts throw; a failed lookup is retried up to the set count. Host HTTP allocations are not charged to the context pool, so `sceHttp2GetMemoryPoolStats` reports a live context's pool size with zero peak and current use. PPSA01494, PPSA30839 and PPSA31162 read the three sizes for memory logging without checking the result, and stopped at startup while it threw.
- [sceHttp2SetMinSslVersion](../../../core/libs/prx/libSceHttp2/Export.cpp) - libSceHttp2's own range and validation order are not measured: versions 2 (SSL 3.0) to 5 (TLS 1.2) are accepted, the range `sceSslSetMinSslVersion` takes on a 13.40 console ([libSceSsl](libSceSsl.md)). The floor is not kept: libSceSsl negotiates TLS 1.2 only, which meets every accepted floor, so a server limited to versions older than TLS 1.2 fails the handshake whatever the floor; what a lower floor does with such a server on a console is not measured.
- [libSceHttp2 request options](../../../core/libs/prx/libSceHttp2/Export.cpp) - extends the TLS settings entry: the auth and redirect flags, headers and the auth-info and pre-send callbacks live in `Http2::Settings`, are copied into each new request and become immutable once transport exists; the getters still read them then. Accepted kinds, defaults, copying, header modes, error codes and their order follow the 13.42 module run in an emulator ([#10536](https://github.com/boykopovar/AnyPS5/pull/10536)), not a console. A null out-pointer to `sceHttp2GetAuthEnabled` is written through on that module; here it throws. The pre-send callback type is unknown. The transport never calls either callback, and the auth cache flush only validates the context
- [sceHttp2GetAllResponseHeaders](../../../core/libs/prx/libSceHttp2/Export.cpp) throws: the serialization, size and lifetime of a decoded HTTP/2 header block have not been established. Status and Content-Length fields are decoded; absent Content-Length reports `NotFound`, following SharpProspero.
Expand Down
Loading