diff --git a/CMakeLists.txt b/CMakeLists.txt index 23f2a1f402..1e27b1caf1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -95,6 +95,7 @@ option(USE_SYSTEM_OPENSSL "Use the system OpenSSL libs (instead of the bundled L option(USE_SYSTEM_LIBUSB "Use the system libusb (instead of the bundled libusb)" OFF) option(USE_SYSTEM_CPP_JWT "Use the system cpp-jwt (instead of the bundled one)" OFF) option(USE_SYSTEM_SOUNDTOUCH "Use the system SoundTouch (instead of the bundled one)" OFF) +option(USE_SYSTEM_CPP_HTTPLIB "Use the system cpp-httplib (instead of the bundled one)" OFF) if (CITRA_USE_PRECOMPILED_HEADERS) message(STATUS "Using Precompiled Headers.") diff --git a/externals/CMakeLists.txt b/externals/CMakeLists.txt index 19da123ca7..ca5b4d4c8c 100644 --- a/externals/CMakeLists.txt +++ b/externals/CMakeLists.txt @@ -190,7 +190,7 @@ if (NOT OPENSSL_FOUND) set(LIBRESSL_SKIP_INSTALL ON CACHE BOOL "") set(OPENSSLDIR "/etc/ssl/") add_subdirectory(libressl EXCLUDE_FROM_ALL) - target_include_directories(ssl INTERFACE ./libressl/include) + target_include_directories(ssl SYSTEM INTERFACE ./libressl/include) target_compile_definitions(ssl PRIVATE -DHAVE_INET_NTOP) get_directory_property(OPENSSL_LIBRARIES DIRECTORY libressl @@ -199,8 +199,13 @@ endif() # httplib add_library(httplib INTERFACE) -target_include_directories(httplib INTERFACE ./httplib) -target_compile_options(httplib INTERFACE -DCPPHTTPLIB_OPENSSL_SUPPORT -DCPPHTTPLIB_NO_DEFAULT_USER_AGENT) +if(USE_SYSTEM_CPP_HTTPLIB) + find_package(CppHttp REQUIRED) + target_link_libraries(httplib SYSTEM INTERFACE cpp-httplib::cpp-httplib) +else() + target_include_directories(httplib SYSTEM INTERFACE ./httplib) +endif() +target_compile_options(httplib INTERFACE -DCPPHTTPLIB_OPENSSL_SUPPORT) target_link_libraries(httplib INTERFACE ${OPENSSL_LIBRARIES}) if(ANDROID) @@ -216,7 +221,7 @@ if (ENABLE_WEB_SERVICE) target_link_libraries(cpp-jwt INTERFACE cpp-jwt::cpp-jwt) else() add_library(cpp-jwt INTERFACE) - target_include_directories(cpp-jwt INTERFACE ./cpp-jwt/include) + target_include_directories(cpp-jwt SYSTEM INTERFACE ./cpp-jwt/include) target_compile_definitions(cpp-jwt INTERFACE CPP_JWT_USE_VENDORED_NLOHMANN_JSON) endif() endif() diff --git a/externals/cmake-modules/FindCppHttp.cmake b/externals/cmake-modules/FindCppHttp.cmake new file mode 100644 index 0000000000..b91cc9f6e1 --- /dev/null +++ b/externals/cmake-modules/FindCppHttp.cmake @@ -0,0 +1,35 @@ +if(NOT CppHttp_FOUND) + pkg_check_modules(HTTP_TMP cpp-httplib) + + find_path(CPP-HTTP_INCLUDE_DIR NAMES httplib.h + PATHS + ${HTTP_TMP_INCLUDE_DIRS} + /usr/include + /usr/local/include + ) + + find_library(CPP-HTTP_LIBRARIES NAMES cpp-httplib + PATHS + ${HTTP_TMP_LIBRARY_DIRS} + /usr/lib + /usr/local/lib + ) + + if(CPP-HTTP_INCLUDE_DIR AND CPP-HTTP_LIBRARIES) + set(CppHttp_FOUND TRUE CACHE INTERNAL "cpp-httplib found") + message(STATUS "Found cpp-httplib: ${CPP-HTTP_INCLUDE_DIR}, ${CPP-HTTP_LIBRARIES}") + else() + set(CppHttp_FOUND FALSE CACHE INTERNAL "cpp-httplib found") + message(STATUS "Cpp-httplib not found.") + endif() + +endif() + +if(CppHttp_FOUND AND NOT TARGET cpp-httplib::cpp-httplib) + add_library(cpp-httplib::cpp-httplib INTERFACE IMPORTED) + set_target_properties(cpp-httplib::cpp-httplib PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${CPP-HTTP_INCLUDE_DIR}" + INTERFACE_LINK_LIBRARIES "${CPP-HTTP_LIBRARIES}" + IMPORTED_LOCATION "${CPP-HTTP_LIBRARIES}" + ) +endif() diff --git a/externals/httplib/httplib.h b/externals/httplib/httplib.h index 89449452aa..c49e50e695 100644 --- a/externals/httplib/httplib.h +++ b/externals/httplib/httplib.h @@ -245,6 +245,12 @@ using socket_t = int; #undef X509_EXTENSIONS #undef PKCS7_SIGNER_INFO +// libressl will warn without this, which becomes an error. +#undef OCSP_REQUEST +#undef OCSP_RESPONSE +#undef PKCS7_ISSUER_AND_SERIAL +#undef __WINCRYPT_H__ + #ifdef _MSC_VER #pragma comment(lib, "crypt32.lib") #pragma comment(lib, "cryptui.lib") diff --git a/src/audio_core/dsp_interface.h b/src/audio_core/dsp_interface.h index f1b315e38d..f62980cdd8 100644 --- a/src/audio_core/dsp_interface.h +++ b/src/audio_core/dsp_interface.h @@ -14,7 +14,7 @@ #include "core/memory.h" namespace Service::DSP { -class DSP_DSP; +enum class InterruptType : u32; } // namespace Service::DSP namespace AudioCore { @@ -85,8 +85,9 @@ public: /// Returns a reference to the array backing DSP memory virtual std::array& GetDspMemory() = 0; - /// Sets the dsp class that we trigger interrupts for - virtual void SetServiceToInterrupt(std::weak_ptr dsp) = 0; + /// Sets the handler for the interrupts we trigger + virtual void SetInterruptHandler( + std::function handler) = 0; /// Loads the DSP program virtual void LoadComponent(std::span buffer) = 0; diff --git a/src/audio_core/hle/hle.cpp b/src/audio_core/hle/hle.cpp index f24176ca80..6b331b2774 100644 --- a/src/audio_core/hle/hle.cpp +++ b/src/audio_core/hle/hle.cpp @@ -34,8 +34,7 @@ SERIALIZE_EXPORT_IMPL(AudioCore::DspHle) -using InterruptType = Service::DSP::DSP_DSP::InterruptType; -using Service::DSP::DSP_DSP; +using InterruptType = Service::DSP::InterruptType; namespace AudioCore { @@ -71,7 +70,8 @@ public: std::array& GetDspMemory(); - void SetServiceToInterrupt(std::weak_ptr dsp); + void SetInterruptHandler( + std::function handler); private: void ResetPipes(); @@ -105,7 +105,7 @@ private: std::unique_ptr decoder{}; - std::weak_ptr dsp_dsp{}; + std::function interrupt_handler{}; template void serialize(Archive& ar, const unsigned int) { @@ -114,7 +114,8 @@ private: ar& dsp_memory.raw_memory; ar& sources; ar& mixers; - ar& dsp_dsp; + // interrupt_handler is function pointer and cant be serialised, fortunately though, it + // should be registerd before the game has started } friend class boost::serialization::access; }; @@ -320,10 +321,8 @@ void DspHle::Impl::PipeWrite(DspPipe pipe_number, std::span buffer) { pipe_data[static_cast(pipe_number)].resize(sizeof(value)); std::memcpy(pipe_data[static_cast(pipe_number)].data(), &value, sizeof(value)); } - auto dsp = dsp_dsp.lock(); - if (dsp) { - dsp->SignalInterrupt(InterruptType::Pipe, DspPipe::Binary); - } + + interrupt_handler(InterruptType::Pipe, DspPipe::Binary); break; } default: @@ -338,8 +337,9 @@ std::array& DspHle::Impl::GetDspMemory() { return dsp_memory.raw_memory; } -void DspHle::Impl::SetServiceToInterrupt(std::weak_ptr dsp) { - dsp_dsp = std::move(dsp); +void DspHle::Impl::SetInterruptHandler( + std::function handler) { + interrupt_handler = handler; } void DspHle::Impl::ResetPipes() { @@ -386,9 +386,7 @@ void DspHle::Impl::AudioPipeWriteStructAddresses() { WriteU16(DspPipe::Audio, addr); } // Signal that we have data on this pipe. - if (auto service = dsp_dsp.lock()) { - service->SignalInterrupt(InterruptType::Pipe, DspPipe::Audio); - } + interrupt_handler(InterruptType::Pipe, DspPipe::Audio); } size_t DspHle::Impl::CurrentRegionIndex() const { @@ -464,9 +462,7 @@ bool DspHle::Impl::Tick() { void DspHle::Impl::AudioTickCallback(s64 cycles_late) { if (Tick()) { // TODO(merry): Signal all the other interrupts as appropriate. - if (auto service = dsp_dsp.lock()) { - service->SignalInterrupt(InterruptType::Pipe, DspPipe::Audio); - } + interrupt_handler(InterruptType::Pipe, DspPipe::Audio); } // Reschedule recurrent event @@ -505,9 +501,10 @@ std::array& DspHle::GetDspMemory() { return impl->GetDspMemory(); } -void DspHle::SetServiceToInterrupt(std::weak_ptr dsp) { - impl->SetServiceToInterrupt(std::move(dsp)); -} +void DspHle::SetInterruptHandler( + std::function handler) { + impl->SetInterruptHandler(handler); +}; void DspHle::LoadComponent(std::span component_data) { // HLE doesn't need DSP program. Only log some info here diff --git a/src/audio_core/hle/hle.h b/src/audio_core/hle/hle.h index ea0577badf..aa2fd2c4fe 100644 --- a/src/audio_core/hle/hle.h +++ b/src/audio_core/hle/hle.h @@ -34,7 +34,8 @@ public: std::array& GetDspMemory() override; - void SetServiceToInterrupt(std::weak_ptr dsp) override; + void SetInterruptHandler( + std::function handler) override; void LoadComponent(std::span buffer) override; void UnloadComponent() override; diff --git a/src/audio_core/lle/lle.cpp b/src/audio_core/lle/lle.cpp index fd30f0c5ab..afcb9df3cc 100644 --- a/src/audio_core/lle/lle.cpp +++ b/src/audio_core/lle/lle.cpp @@ -408,29 +408,24 @@ std::array& DspLle::GetDspMemory() { return impl->teakra.GetDspMemory(); } -void DspLle::SetServiceToInterrupt(std::weak_ptr dsp) { - impl->teakra.SetRecvDataHandler(0, [this, dsp]() { +void DspLle::SetInterruptHandler( + std::function handler) { + impl->teakra.SetRecvDataHandler(0, [this, handler]() { if (!impl->loaded) return; std::lock_guard lock(HLE::g_hle_lock); - if (auto locked = dsp.lock()) { - locked->SignalInterrupt(Service::DSP::DSP_DSP::InterruptType::Zero, - static_cast(0)); - } + handler(Service::DSP::InterruptType::Zero, static_cast(0)); }); - impl->teakra.SetRecvDataHandler(1, [this, dsp]() { + impl->teakra.SetRecvDataHandler(1, [this, handler]() { if (!impl->loaded) return; std::lock_guard lock(HLE::g_hle_lock); - if (auto locked = dsp.lock()) { - locked->SignalInterrupt(Service::DSP::DSP_DSP::InterruptType::One, - static_cast(0)); - } + handler(Service::DSP::InterruptType::One, static_cast(0)); }); - auto ProcessPipeEvent = [this, dsp](bool event_from_data) { + auto ProcessPipeEvent = [this, handler](bool event_from_data) { if (!impl->loaded) return; @@ -456,10 +451,7 @@ void DspLle::SetServiceToInterrupt(std::weak_ptr dsp) { impl->GetPipeReadableSize(static_cast(pipe))); } else { std::lock_guard lock(HLE::g_hle_lock); - if (auto locked = dsp.lock()) { - locked->SignalInterrupt(Service::DSP::DSP_DSP::InterruptType::Pipe, - static_cast(pipe)); - } + handler(Service::DSP::InterruptType::Pipe, static_cast(pipe)); } } }; @@ -468,14 +460,6 @@ void DspLle::SetServiceToInterrupt(std::weak_ptr dsp) { impl->teakra.SetSemaphoreHandler([ProcessPipeEvent]() { ProcessPipeEvent(false); }); } -void DspLle::SetSemaphoreHandler(std::function handler) { - impl->teakra.SetSemaphoreHandler(handler); -} - -void DspLle::SetRecvDataHandler(u8 index, std::function handler) { - impl->teakra.SetRecvDataHandler(index, handler); -} - void DspLle::LoadComponent(std::span buffer) { impl->LoadComponent(buffer); } diff --git a/src/audio_core/lle/lle.h b/src/audio_core/lle/lle.h index 7aec28da42..bcc5e3c7d0 100644 --- a/src/audio_core/lle/lle.h +++ b/src/audio_core/lle/lle.h @@ -27,10 +27,8 @@ public: std::array& GetDspMemory() override; - void SetServiceToInterrupt(std::weak_ptr dsp) override; - - void SetSemaphoreHandler(std::function handler); - void SetRecvDataHandler(u8 index, std::function handler); + void SetInterruptHandler( + std::function handler) override; void LoadComponent(const std::span buffer) override; void UnloadComponent() override; diff --git a/src/common/string_util.h b/src/common/string_util.h index 74c342e5bb..2384e9c510 100644 --- a/src/common/string_util.h +++ b/src/common/string_util.h @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -84,6 +85,15 @@ std::string UTF16BufferToUTF8(const T& text) { return UTF16ToUTF8(buffer); } +/** + * Removes trailing null bytes from the string. + */ +inline void TruncateString(std::string& str) { + while (str.size() && str.back() == '\0') { + str.pop_back(); + } +} + /** * Creates a std::string from a fixed-size NUL-terminated char buffer. If the buffer isn't * NUL-terminated then the string ends at max_len characters. diff --git a/src/core/hle/kernel/hle_ipc.h b/src/core/hle/kernel/hle_ipc.h index f8c4a31a70..480c8920c7 100644 --- a/src/core/hle/kernel/hle_ipc.h +++ b/src/core/hle/kernel/hle_ipc.h @@ -213,6 +213,11 @@ public: return {cmd_buf[0]}; } + /// Returns the command ID from the IPC command buffer. + u16 CommandID() const { + return static_cast(CommandHeader().command_id.Value()); + } + /** * Returns the session through which this request was made. This can be used as a map key to * access per-client data on services. diff --git a/src/core/hle/service/dsp/dsp_dsp.cpp b/src/core/hle/service/dsp/dsp_dsp.cpp index 14c799c702..fd28925a02 100644 --- a/src/core/hle/service/dsp/dsp_dsp.cpp +++ b/src/core/hle/service/dsp/dsp_dsp.cpp @@ -12,7 +12,7 @@ #include "core/hle/service/dsp/dsp_dsp.h" using DspPipe = AudioCore::DspPipe; -using InterruptType = Service::DSP::DSP_DSP::InterruptType; +using InterruptType = Service::DSP::InterruptType; SERIALIZE_EXPORT_IMPL(Service::DSP::DSP_DSP) SERVICE_CONSTRUCT_IMPL(Service::DSP::DSP_DSP) @@ -235,7 +235,8 @@ void DSP_DSP::RegisterInterruptEvents(Kernel::HLERequestContext& ctx) { const u32 channel = rp.Pop(); auto event = rp.PopObject(); - ASSERT_MSG(interrupt < NUM_INTERRUPT_TYPE && channel < AudioCore::num_dsp_pipe, + ASSERT_MSG(interrupt < static_cast(InterruptType::Count) && + channel < AudioCore::num_dsp_pipe, "Invalid type or pipe: interrupt = {}, channel = {}", interrupt, channel); const InterruptType type = static_cast(interrupt); @@ -326,6 +327,9 @@ std::shared_ptr& DSP_DSP::GetInterruptEvent(InterruptType type, D ASSERT(pipe_index < AudioCore::num_dsp_pipe); return pipes[pipe_index]; } + case InterruptType::Count: + default: + break; } UNREACHABLE_MSG("Invalid interrupt type = {}", type); } @@ -401,7 +405,12 @@ void InstallInterfaces(Core::System& system) { auto& service_manager = system.ServiceManager(); auto dsp = std::make_shared(system); dsp->InstallAsService(service_manager); - system.DSP().SetServiceToInterrupt(std::move(dsp)); + system.DSP().SetInterruptHandler( + [dsp_ref = std::weak_ptr(dsp)](InterruptType type, DspPipe pipe) { + if (auto locked = dsp_ref.lock()) { + locked->SignalInterrupt(type, pipe); + } + }); } } // namespace Service::DSP diff --git a/src/core/hle/service/dsp/dsp_dsp.h b/src/core/hle/service/dsp/dsp_dsp.h index d580b3d00d..1eecb29cc7 100644 --- a/src/core/hle/service/dsp/dsp_dsp.h +++ b/src/core/hle/service/dsp/dsp_dsp.h @@ -19,15 +19,14 @@ class System; namespace Service::DSP { +/// There are three types of interrupts +enum class InterruptType : u32 { Zero = 0, One = 1, Pipe = 2, Count }; + class DSP_DSP final : public ServiceFramework { public: explicit DSP_DSP(Core::System& system); ~DSP_DSP(); - /// There are three types of interrupts - static constexpr std::size_t NUM_INTERRUPT_TYPE = 3; - enum class InterruptType : u32 { Zero = 0, One = 1, Pipe = 2 }; - /// Actual service implementation only has 6 'slots' for interrupts. static constexpr std::size_t max_number_of_interrupt_events = 6; diff --git a/src/core/hle/service/http/http_c.cpp b/src/core/hle/service/http/http_c.cpp index 45861d4d5d..b57a061e45 100644 --- a/src/core/hle/service/http/http_c.cpp +++ b/src/core/hle/service/http/http_c.cpp @@ -6,10 +6,12 @@ #include #include #include +#include #include #include #include "common/archives.h" #include "common/assert.h" +#include "common/string_util.h" #include "core/core.h" #include "core/file_sys/archive_ncch.h" #include "core/file_sys/file_backend.h" @@ -63,8 +65,26 @@ const ResultCode ERROR_WRONG_CERT_HANDLE = // 0xD8A0A0C9 const ResultCode ERROR_CERT_ALREADY_SET = // 0xD8A0A03D ResultCode(61, ErrorModule::HTTP, ErrorSummary::InvalidState, ErrorLevel::Permanent); -static ssize_t write_headers(httplib::Stream& strm, - const std::vector& headers) { +// Splits URL into its components. Example: https://citra-emu.org:443/index.html +// is_https: true; host: citra-emu.org; port: 443; path: /index.html +static URLInfo SplitUrl(const std::string& url) { + constexpr u16 default_http_port = 80; + constexpr u16 default_https_port = 443; + + const auto result = boost::urls::parse_uri(url); + const bool is_https = result->scheme_id() == boost::urls::scheme::https; + const auto port = result->port_number(); + const auto default_port = is_https ? default_https_port : default_http_port; + return URLInfo{ + .is_https = is_https, + .host = std::string{result->encoded_host()}, + .port = port == 0 ? default_port : port, + .path = std::string{result->encoded_path()}, + }; +} + +static ssize_t WriteHeaders(httplib::Stream& strm, + std::span headers) { ssize_t write_len = 0; for (const auto& x : headers) { auto len = strm.write_format("%s: %s\r\n", x.name.c_str(), x.value.c_str()); @@ -81,50 +101,50 @@ static ssize_t write_headers(httplib::Stream& strm, return write_len; } -static std::tuple SplitUrl(const std::string& url) { - const std::string prefix = "://"; - const auto scheme_end = url.find(prefix); - const auto prefix_end = scheme_end == std::string::npos ? 0 : scheme_end + prefix.length(); +static size_t HandleHeaderWrite(std::vector& pending_headers, + httplib::Stream& strm, httplib::Headers& httplib_headers) { + std::vector final_headers; + std::vector::iterator it_p; + httplib::Headers::iterator it_h; - bool is_https = scheme_end != std::string::npos && url.starts_with("https"); + auto find_pending_header = [&pending_headers](const std::string& str) { + return std::find_if(pending_headers.begin(), pending_headers.end(), + [&str](Context::RequestHeader& rh) { return rh.name == str; }); + }; - const auto path_index = url.find("/", prefix_end); - std::string host; - int port = -1; - std::string path; - if (path_index == std::string::npos) { - // If no path is specified after the host, set it to "/" - host = url.substr(prefix_end); - const auto port_start = host.find(":"); - if (port_start != std::string::npos) { - std::string port_str = host.substr(port_start + 1); - host = host.substr(0, port_start); - char* pEnd = NULL; - port = std::strtol(port_str.c_str(), &pEnd, 10); - if (*pEnd) { - port = -1; - } - } - path = "/"; + // Watch out for header ordering!! + // First: Host + it_p = find_pending_header("Host"); + if (it_p != pending_headers.end()) { + final_headers.push_back(Context::RequestHeader(it_p->name, it_p->value)); + pending_headers.erase(it_p); } else { - host = url.substr(prefix_end, path_index - prefix_end); - const auto port_start = host.find(":"); - if (port_start != std::string::npos) { - std::string port_str = host.substr(port_start + 1); - host = host.substr(0, port_start); - char* pEnd = NULL; - port = std::strtol(port_str.c_str(), &pEnd, 10); - if (*pEnd) { - port = -1; - } + it_h = httplib_headers.find("Host"); + if (it_h != httplib_headers.end()) { + final_headers.push_back(Context::RequestHeader(it_h->first, it_h->second)); } - path = url.substr(path_index); } - if (port == -1) { - port = is_https ? 443 : 80; + + // Second, user defined headers + // Third, Content-Type (optional, appended by MakeRequest) + for (const auto& header : pending_headers) { + final_headers.push_back(header); } - return std::make_tuple(host, port, path, is_https); -} + + // Fourth: Content-Length + it_p = find_pending_header("Content-Length"); + if (it_p != pending_headers.end()) { + final_headers.push_back(Context::RequestHeader(it_p->name, it_p->value)); + pending_headers.erase(it_p); + } else { + it_h = httplib_headers.find("Content-Length"); + if (it_h != httplib_headers.end()) { + final_headers.push_back(Context::RequestHeader(it_h->first, it_h->second)); + } + } + + return WriteHeaders(strm, final_headers); +}; void Context::MakeRequest() { ASSERT(state == RequestState::NotStarted); @@ -136,13 +156,12 @@ void Context::MakeRequest() { {RequestMethod::PutEmpty, "PUT"}, }; - const auto& [host, port, path, is_https] = SplitUrl(url.c_str()); + URLInfo url_info = SplitUrl(url); httplib::Request request; - httplib::Error error{-1}; std::vector pending_headers; request.method = request_method_strings.at(method); - request.path = path; + request.path = url_info.path; request.progress = [this](u64 current, u64 total) -> bool { // TODO(B3N30): Is there a state that shows response header are available @@ -151,56 +170,9 @@ void Context::MakeRequest() { return true; }; - // Watch out for header ordering! - auto header_writter = [&pending_headers](httplib::Stream& strm, - httplib::Headers& httplib_headers) { - std::vector final_headers; - std::vector::iterator it_p; - httplib::Headers::iterator it_h; - - auto find_pending_header = [&pending_headers](const std::string& str) { - return std::find_if(pending_headers.begin(), pending_headers.end(), - [&str](Context::RequestHeader& rh) { return rh.name == str; }); - }; - - // First: Host - it_p = find_pending_header("Host"); - if (it_p != pending_headers.end()) { - final_headers.push_back(Context::RequestHeader(it_p->name, it_p->value)); - pending_headers.erase(it_p); - } else { - it_h = httplib_headers.find("Host"); - if (it_h != httplib_headers.end()) { - final_headers.push_back(Context::RequestHeader(it_h->first, it_h->second)); - } - } - - // Second: Content-Type (optional, ignore httplib) - it_p = find_pending_header("Content-Type"); - if (it_p != pending_headers.end()) { - final_headers.push_back(Context::RequestHeader(it_p->name, it_p->value)); - pending_headers.erase(it_p); - } - - // Third: Content-Length - it_p = find_pending_header("Content-Length"); - if (it_p != pending_headers.end()) { - final_headers.push_back(Context::RequestHeader(it_p->name, it_p->value)); - pending_headers.erase(it_p); - } else { - it_h = httplib_headers.find("Content-Length"); - if (it_h != httplib_headers.end()) { - final_headers.push_back(Context::RequestHeader(it_h->first, it_h->second)); - } - } - - // Finally, user defined headers - for (const auto& header : pending_headers) { - final_headers.push_back(header); - } - - return write_headers(strm, final_headers); - }; + for (const auto& header : headers) { + pending_headers.push_back(header); + } if (!post_data.empty()) { pending_headers.push_back( @@ -209,68 +181,73 @@ void Context::MakeRequest() { boost::replace_all(request.body, "*", "%2A"); } - for (const auto& header : headers) { - pending_headers.push_back(header); - } - if (!post_data_raw.empty()) { request.body = post_data_raw; } state = RequestState::InProgress; - // Sadly, we have to duplicate code, the class hierarchy in httplib is not very useful... - if (is_https) { - X509* cert = nullptr; - EVP_PKEY* key = nullptr; - { - std::unique_ptr client; - if (uses_default_client_cert) { - const unsigned char* tmpCertPtr = clcert_data->certificate.data(); - const unsigned char* tmpKeyPtr = clcert_data->private_key.data(); - cert = d2i_X509(nullptr, &tmpCertPtr, (long)clcert_data->certificate.size()); - key = d2i_PrivateKey(EVP_PKEY_RSA, nullptr, &tmpKeyPtr, - (long)clcert_data->private_key.size()); - client = std::make_unique(host, port, cert, key); - } else { - if (auto client_cert = ssl_config.client_cert_ctx.lock()) { - const unsigned char* tmpCertPtr = client_cert->certificate.data(); - const unsigned char* tmpKeyPtr = client_cert->private_key.data(); - cert = d2i_X509(nullptr, &tmpCertPtr, (long)client_cert->certificate.size()); - key = d2i_PrivateKey(EVP_PKEY_RSA, nullptr, &tmpKeyPtr, - (long)client_cert->private_key.size()); - client = std::make_unique(host, port, cert, key); - } else { - client = std::make_unique(host, port); - } - } - - // TODO(B3N30): Check for SSLOptions-Bits and set the verify method accordingly - // https://www.3dbrew.org/wiki/SSL_Services#SSLOpt - // Hack: Since for now RootCerts are not implemented we set the VerifyMode to None. - client->enable_server_certificate_verification(false); - - client->set_header_writer(header_writter); - - if (!client->send(request, response, error)) { - LOG_ERROR(Service_HTTP, "Request failed: {}: {}", error, httplib::to_string(error)); - state = RequestState::TimedOut; - } else { - LOG_DEBUG(Service_HTTP, "Request successful"); - // TODO(B3N30): Verify this state on HW - state = RequestState::ReadyToDownloadContent; - } - } - if (cert) { - X509_free(cert); - } - if (key) { - EVP_PKEY_free(key); - } + if (url_info.is_https) { + MakeRequestSSL(request, url_info, pending_headers); } else { - std::unique_ptr client = std::make_unique(host, port); + MakeRequestNonSSL(request, url_info, pending_headers); + } +} - client->set_header_writer(header_writter); +void Context::MakeRequestNonSSL(httplib::Request& request, const URLInfo& url_info, + std::vector& pending_headers) { + httplib::Error error{-1}; + std::unique_ptr client = + std::make_unique(url_info.host, url_info.port); + + client->set_header_writer( + [&pending_headers](httplib::Stream& strm, httplib::Headers& httplib_headers) { + return HandleHeaderWrite(pending_headers, strm, httplib_headers); + }); + + if (!client->send(request, response, error)) { + LOG_ERROR(Service_HTTP, "Request failed: {}: {}", error, httplib::to_string(error)); + state = RequestState::TimedOut; + } else { + LOG_DEBUG(Service_HTTP, "Request successful"); + // TODO(B3N30): Verify this state on HW + state = RequestState::ReadyToDownloadContent; + } +} +void Context::MakeRequestSSL(httplib::Request& request, const URLInfo& url_info, + std::vector& pending_headers) { + httplib::Error error{-1}; + X509* cert = nullptr; + EVP_PKEY* key = nullptr; + { + std::unique_ptr client; + if (uses_default_client_cert) { + const unsigned char* tmpCertPtr = clcert_data->certificate.data(); + const unsigned char* tmpKeyPtr = clcert_data->private_key.data(); + cert = d2i_X509(nullptr, &tmpCertPtr, (long)clcert_data->certificate.size()); + key = d2i_PrivateKey(EVP_PKEY_RSA, nullptr, &tmpKeyPtr, + (long)clcert_data->private_key.size()); + client = std::make_unique(url_info.host, url_info.port, cert, key); + } else if (auto client_cert = ssl_config.client_cert_ctx.lock()) { + const unsigned char* tmpCertPtr = client_cert->certificate.data(); + const unsigned char* tmpKeyPtr = client_cert->private_key.data(); + cert = d2i_X509(nullptr, &tmpCertPtr, (long)client_cert->certificate.size()); + key = d2i_PrivateKey(EVP_PKEY_RSA, nullptr, &tmpKeyPtr, + (long)client_cert->private_key.size()); + client = std::make_unique(url_info.host, url_info.port, cert, key); + } else { + client = std::make_unique(url_info.host, url_info.port); + } + + // TODO(B3N30): Check for SSLOptions-Bits and set the verify method accordingly + // https://www.3dbrew.org/wiki/SSL_Services#SSLOpt + // Hack: Since for now RootCerts are not implemented we set the VerifyMode to None. + client->enable_server_certificate_verification(false); + + client->set_header_writer( + [&pending_headers](httplib::Stream& strm, httplib::Headers& httplib_headers) { + return HandleHeaderWrite(pending_headers, strm, httplib_headers); + }); if (!client->send(request, response, error)) { LOG_ERROR(Service_HTTP, "Request failed: {}: {}", error, httplib::to_string(error)); @@ -281,6 +258,12 @@ void Context::MakeRequest() { state = RequestState::ReadyToDownloadContent; } } + if (cert) { + X509_free(cert); + } + if (key) { + EVP_PKEY_free(key); + } } void HTTP_C::Initialize(Kernel::HLERequestContext& ctx) { @@ -358,11 +341,11 @@ void HTTP_C::BeginRequest(Kernel::HLERequestContext& ctx) { return; } - auto itr = contexts.find(context_handle); - ASSERT(itr != contexts.end()); + Context& http_context = GetContext(context_handle); // This should never happen in real hardware, but can happen on citra. - if (itr->second.uses_default_client_cert && !itr->second.clcert_data->init) { + if (http_context.uses_default_client_cert && !http_context.clcert_data->init) { + LOG_ERROR(Service_HTTP, "failed to begin HTTP request: client cert not found."); IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); rb.Push(ERROR_STATE_ERROR); return; @@ -375,9 +358,9 @@ void HTTP_C::BeginRequest(Kernel::HLERequestContext& ctx) { // Then there are 3? worker threads that pop the requests from the queue and send them // For now make every request async in it's own thread. - itr->second.request_future = - std::async(std::launch::async, &Context::MakeRequest, std::ref(itr->second)); - itr->second.current_copied_data = 0; + http_context.request_future = + std::async(std::launch::async, &Context::MakeRequest, std::ref(http_context)); + http_context.current_copied_data = 0; IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); rb.Push(RESULT_SUCCESS); @@ -393,11 +376,11 @@ void HTTP_C::BeginRequestAsync(Kernel::HLERequestContext& ctx) { return; } - auto itr = contexts.find(context_handle); - ASSERT(itr != contexts.end()); + Context& http_context = GetContext(context_handle); // This should never happen in real hardware, but can happen on citra. - if (itr->second.uses_default_client_cert && !itr->second.clcert_data->init) { + if (http_context.uses_default_client_cert && !http_context.clcert_data->init) { + LOG_ERROR(Service_HTTP, "failed to begin HTTP request: client cert not found."); IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); rb.Push(ERROR_STATE_ERROR); return; @@ -410,9 +393,9 @@ void HTTP_C::BeginRequestAsync(Kernel::HLERequestContext& ctx) { // Then there are 3? worker threads that pop the requests from the queue and send them // For now make every request async in it's own thread. - itr->second.request_future = - std::async(std::launch::async, &Context::MakeRequest, std::ref(itr->second)); - itr->second.current_copied_data = 0; + http_context.request_future = + std::async(std::launch::async, &Context::MakeRequest, std::ref(http_context)); + http_context.current_copied_data = 0; IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); rb.Push(RESULT_SUCCESS); @@ -462,10 +445,7 @@ void HTTP_C::ReceiveDataImpl(Kernel::HLERequestContext& ctx, bool timeout) { ctx.RunAsync( [async_data](Kernel::HLERequestContext& ctx) { - auto itr = async_data->own->contexts.find(async_data->context_handle); - ASSERT(itr != async_data->own->contexts.end()); - - Context& http_context = itr->second; + Context& http_context = async_data->own->GetContext(async_data->context_handle); if (async_data->timeout) { auto wait_res = http_context.request_future.wait_for( @@ -482,16 +462,12 @@ void HTTP_C::ReceiveDataImpl(Kernel::HLERequestContext& ctx, bool timeout) { return 1'000'000; }, [async_data](Kernel::HLERequestContext& ctx) { - IPC::RequestBuilder rb(ctx, static_cast(ctx.CommandHeader().command_id.Value()), 1, - 0); + IPC::RequestBuilder rb(ctx, ctx.CommandID(), 1, 0); if (async_data->async_res != RESULT_SUCCESS) { rb.Push(async_data->async_res); return; } - auto itr = async_data->own->contexts.find(async_data->context_handle); - ASSERT(itr != async_data->own->contexts.end()); - - Context& http_context = itr->second; + Context& http_context = async_data->own->GetContext(async_data->context_handle); size_t remaining_data = http_context.response.body.size() - http_context.current_copied_data; @@ -638,8 +614,7 @@ void HTTP_C::CancelConnection(Kernel::HLERequestContext& ctx) { return; } - auto itr = contexts.find(context_handle); - ASSERT(itr != contexts.end()); + [[maybe_unused]] Context& http_context = GetContext(context_handle); IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); rb.Push(RESULT_SUCCESS); @@ -667,10 +642,9 @@ void HTTP_C::AddRequestHeader(Kernel::HLERequestContext& ctx) { return; } - auto itr = contexts.find(context_handle); - ASSERT(itr != contexts.end()); + Context& http_context = GetContext(context_handle); - if (itr->second.state != RequestState::NotStarted) { + if (http_context.state != RequestState::NotStarted) { LOG_ERROR(Service_HTTP, "Tried to add a request header on a context that has already been started."); IPC::RequestBuilder rb = rp.MakeBuilder(1, 2); @@ -680,7 +654,7 @@ void HTTP_C::AddRequestHeader(Kernel::HLERequestContext& ctx) { return; } - itr->second.headers.emplace_back(name, value); + http_context.headers.emplace_back(name, value); IPC::RequestBuilder rb = rp.MakeBuilder(1, 2); rb.Push(RESULT_SUCCESS); @@ -709,10 +683,9 @@ void HTTP_C::AddPostDataAscii(Kernel::HLERequestContext& ctx) { return; } - auto itr = contexts.find(context_handle); - ASSERT(itr != contexts.end()); + Context& http_context = GetContext(context_handle); - if (itr->second.state != RequestState::NotStarted) { + if (http_context.state != RequestState::NotStarted) { LOG_ERROR(Service_HTTP, "Tried to add post data on a context that has already been started."); IPC::RequestBuilder rb = rp.MakeBuilder(1, 2); @@ -722,7 +695,7 @@ void HTTP_C::AddPostDataAscii(Kernel::HLERequestContext& ctx) { return; } - itr->second.post_data.emplace(name, value); + http_context.post_data.emplace(name, value); IPC::RequestBuilder rb = rp.MakeBuilder(1, 2); rb.Push(RESULT_SUCCESS); @@ -741,10 +714,9 @@ void HTTP_C::AddPostDataRaw(Kernel::HLERequestContext& ctx) { return; } - auto itr = contexts.find(context_handle); - ASSERT(itr != contexts.end()); + Context& http_context = GetContext(context_handle); - if (itr->second.state != RequestState::NotStarted) { + if (http_context.state != RequestState::NotStarted) { LOG_ERROR(Service_HTTP, "Tried to add post data on a context that has already been started."); IPC::RequestBuilder rb = rp.MakeBuilder(1, 2); @@ -754,8 +726,8 @@ void HTTP_C::AddPostDataRaw(Kernel::HLERequestContext& ctx) { return; } - itr->second.post_data_raw.resize(buffer.GetSize()); - buffer.Read(itr->second.post_data_raw.data(), 0, buffer.GetSize()); + http_context.post_data_raw.resize(buffer.GetSize()); + buffer.Read(http_context.post_data_raw.data(), 0, buffer.GetSize()); IPC::RequestBuilder rb = rp.MakeBuilder(1, 2); rb.Push(RESULT_SUCCESS); @@ -770,7 +742,7 @@ void HTTP_C::GetResponseHeader(Kernel::HLERequestContext& ctx) { u32 context_handle; u32 name_len; u32 value_max_len; - const std::vector* header_name; + std::span header_name; Kernel::MappedBuffer* value_buffer; }; std::shared_ptr async_data = std::make_shared(); @@ -779,7 +751,7 @@ void HTTP_C::GetResponseHeader(Kernel::HLERequestContext& ctx) { async_data->context_handle = rp.Pop(); async_data->name_len = rp.Pop(); async_data->value_max_len = rp.Pop(); - async_data->header_name = &rp.PopStaticBuffer(); + async_data->header_name = rp.PopStaticBuffer(); async_data->value_buffer = &rp.PopMappedBuffer(); if (!PerformStateChecks(ctx, rp, async_data->context_handle)) { @@ -788,24 +760,19 @@ void HTTP_C::GetResponseHeader(Kernel::HLERequestContext& ctx) { ctx.RunAsync( [async_data](Kernel::HLERequestContext& ctx) { - auto itr = async_data->own->contexts.find(async_data->context_handle); - ASSERT(itr != async_data->own->contexts.end()); - - itr->second.request_future.wait(); + Context& http_context = async_data->own->GetContext(async_data->context_handle); + http_context.request_future.wait(); return 0; }, [async_data](Kernel::HLERequestContext& ctx) { std::string header_name_str( - reinterpret_cast(async_data->header_name->data()), + reinterpret_cast(async_data->header_name.data()), async_data->name_len); - while (header_name_str.size() && header_name_str.back() == '\0') { - header_name_str.pop_back(); - } + Common::TruncateString(header_name_str); - auto itr = async_data->own->contexts.find(async_data->context_handle); - ASSERT(itr != async_data->own->contexts.end()); + Context& http_context = async_data->own->GetContext(async_data->context_handle); - auto& headers = itr->second.response.headers; + auto& headers = http_context.response.headers; u32 copied_size = 0; LOG_DEBUG(Service_HTTP, "header={}, max_len={}", header_name_str, @@ -822,13 +789,13 @@ void HTTP_C::GetResponseHeader(Kernel::HLERequestContext& ctx) { async_data->value_buffer->Write(header_value.data(), 0, header_value.size()); } else { LOG_DEBUG(Service_HTTP, "header={} not found", header_name_str); - IPC::RequestBuilder rb(ctx, 0x001E, 1, 2); + IPC::RequestBuilder rb(ctx, ctx.CommandID(), 1, 2); rb.Push(ERROR_HEADER_NOT_FOUND); rb.PushMappedBuffer(*async_data->value_buffer); return; } - IPC::RequestBuilder rb(ctx, 0x001E, 2, 2); + IPC::RequestBuilder rb(ctx, ctx.CommandID(), 2, 2); rb.Push(RESULT_SUCCESS); rb.Push(copied_size); rb.PushMappedBuffer(*async_data->value_buffer); @@ -874,11 +841,10 @@ void HTTP_C::GetResponseStatusCodeImpl(Kernel::HLERequestContext& ctx, bool time ctx.RunAsync( [async_data](Kernel::HLERequestContext& ctx) { - auto itr = async_data->own->contexts.find(async_data->context_handle); - ASSERT(itr != async_data->own->contexts.end()); + Context& http_context = async_data->own->GetContext(async_data->context_handle); if (async_data->timeout) { - auto wait_res = itr->second.request_future.wait_for( + auto wait_res = http_context.request_future.wait_for( std::chrono::nanoseconds(async_data->timeout_nanos)); if (wait_res == std::future_status::timeout) { LOG_DEBUG(Service_HTTP, "Status code: {}", "timeout"); @@ -887,26 +853,23 @@ void HTTP_C::GetResponseStatusCodeImpl(Kernel::HLERequestContext& ctx, bool time ErrorLevel::Permanent); } } else { - itr->second.request_future.wait(); + http_context.request_future.wait(); } return 0; }, [async_data](Kernel::HLERequestContext& ctx) { if (async_data->async_res != RESULT_SUCCESS) { - IPC::RequestBuilder rb( - ctx, static_cast(ctx.CommandHeader().command_id.Value()), 1, 0); + IPC::RequestBuilder rb(ctx, ctx.CommandID(), 1, 0); rb.Push(async_data->async_res); return; } - auto itr = async_data->own->contexts.find(async_data->context_handle); - ASSERT(itr != async_data->own->contexts.end()); + Context& http_context = async_data->own->GetContext(async_data->context_handle); - const u32 response_code = itr->second.response.status; + const u32 response_code = http_context.response.status; LOG_DEBUG(Service_HTTP, "Status code: {}, response_code={}", "good", response_code); - IPC::RequestBuilder rb(ctx, static_cast(ctx.CommandHeader().command_id.Value()), 2, - 0); + IPC::RequestBuilder rb(ctx, ctx.CommandID(), 2, 0); rb.Push(RESULT_SUCCESS); rb.Push(response_code); }); @@ -940,7 +903,7 @@ void HTTP_C::AddDefaultCert(Kernel::HLERequestContext& ctx) { void HTTP_C::SetDefaultClientCert(Kernel::HLERequestContext& ctx) { IPC::RequestParser rp(ctx); const Context::Handle context_handle = rp.Pop(); - const u32 client_cert_id = rp.Pop(); + const ClientCertID client_cert_id = static_cast(rp.Pop()); LOG_DEBUG(Service_HTTP, "client_cert_id={}", client_cert_id); @@ -948,17 +911,16 @@ void HTTP_C::SetDefaultClientCert(Kernel::HLERequestContext& ctx) { return; } - auto itr = contexts.find(context_handle); - ASSERT(itr != contexts.end()); + Context& http_context = GetContext(context_handle); - if (client_cert_id != 0x40) { + if (client_cert_id != ClientCertID::Default) { IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); rb.Push(ERROR_WRONG_CERT_ID); return; } - itr->second.uses_default_client_cert = true; - itr->second.clcert_data = &GetClCertA(); + http_context.uses_default_client_cert = true; + http_context.clcert_data = &GetClCertA(); IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); rb.Push(RESULT_SUCCESS); @@ -976,8 +938,7 @@ void HTTP_C::SetClientCertContext(Kernel::HLERequestContext& ctx) { return; } - auto http_context_itr = contexts.find(context_handle); - ASSERT(http_context_itr != contexts.end()); + Context& http_context = GetContext(context_handle); auto cert_context_itr = client_certs.find(client_cert_handle); if (cert_context_itr == client_certs.end()) { @@ -987,7 +948,7 @@ void HTTP_C::SetClientCertContext(Kernel::HLERequestContext& ctx) { return; } - if (http_context_itr->second.ssl_config.client_cert_ctx.lock()) { + if (http_context.ssl_config.client_cert_ctx.lock()) { LOG_ERROR(Service_HTTP, "Tried to set a client cert to a context that already has a client cert"); IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); @@ -995,7 +956,7 @@ void HTTP_C::SetClientCertContext(Kernel::HLERequestContext& ctx) { return; } - if (http_context_itr->second.state != RequestState::NotStarted) { + if (http_context.state != RequestState::NotStarted) { LOG_ERROR(Service_HTTP, "Tried to set a client cert on a context that has already been started."); IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); @@ -1004,7 +965,7 @@ void HTTP_C::SetClientCertContext(Kernel::HLERequestContext& ctx) { return; } - http_context_itr->second.ssl_config.client_cert_ctx = cert_context_itr->second; + http_context.ssl_config.client_cert_ctx = cert_context_itr->second; IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); rb.Push(RESULT_SUCCESS); } @@ -1016,8 +977,7 @@ void HTTP_C::GetSSLError(Kernel::HLERequestContext& ctx) { LOG_WARNING(Service_HTTP, "(STUBBED) called, context_handle={}, unk={}", context_handle, unk); - auto http_context_itr = contexts.find(context_handle); - ASSERT(http_context_itr != contexts.end()); + [[maybe_unused]] Context& http_context = GetContext(context_handle); IPC::RequestBuilder rb = rp.MakeBuilder(2, 0); rb.Push(RESULT_SUCCESS); @@ -1103,7 +1063,7 @@ void HTTP_C::OpenDefaultClientCertContext(Kernel::HLERequestContext& ctx) { return; } - constexpr u8 default_cert_id = 0x40; + constexpr u8 default_cert_id = static_cast(ClientCertID::Default); if (cert_id != default_cert_id) { LOG_ERROR(Service_HTTP, "called with invalid cert_id {}", cert_id); IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); @@ -1210,28 +1170,27 @@ void HTTP_C::GetDownloadSizeState(Kernel::HLERequestContext& ctx) { return; } - auto itr = contexts.find(context_handle); - ASSERT(itr != contexts.end()); + Context& http_context = GetContext(context_handle); // On the real console, the current downloaded progress and the total size of the content gets // returned. Since we do not support chunked downloads on the host, always return the content // length if the download is complete and 0 otherwise. u32 content_length = 0; - const bool is_complete = itr->second.request_future.wait_for(std::chrono::milliseconds(0)) == + const bool is_complete = http_context.request_future.wait_for(std::chrono::milliseconds(0)) == std::future_status::ready; if (is_complete) { - const auto& headers = itr->second.response.headers; + const auto& headers = http_context.response.headers; const auto& it = headers.find("Content-Length"); if (it != headers.end()) { content_length = std::stoi(it->second); } } - LOG_DEBUG(Service_HTTP, "current={}, total={}", itr->second.current_copied_data, + LOG_DEBUG(Service_HTTP, "current={}, total={}", http_context.current_copied_data, content_length); IPC::RequestBuilder rb = rp.MakeBuilder(3, 0); rb.Push(RESULT_SUCCESS); - rb.Push(static_cast(itr->second.current_copied_data)); + rb.Push(static_cast(http_context.current_copied_data)); rb.Push(content_length); } diff --git a/src/core/hle/service/http/http_c.h b/src/core/hle/service/http/http_c.h index 722e5e93ae..07547d05ea 100644 --- a/src/core/hle/service/http/http_c.h +++ b/src/core/hle/service/http/http_c.h @@ -57,6 +57,17 @@ enum class RequestState : u8 { TimedOut = 0xA, // Request timed out? }; +enum class ClientCertID : u32 { + Default = 0x40, // Default client cert +}; + +struct URLInfo { + bool is_https; + std::string host; + int port; + std::string path; +}; + /// Represents a client certificate along with its private key, stored as a byte array of DER data. /// There can only be at most one client certificate context attached to an HTTP context at any /// given time. @@ -130,8 +141,6 @@ public: Context(const Context&) = delete; Context& operator=(const Context&) = delete; - void MakeRequest(); - struct Proxy { std::string url; std::string username; @@ -211,6 +220,12 @@ public: size_t current_copied_data; bool uses_default_client_cert{}; httplib::Response response; + + void MakeRequest(); + void MakeRequestNonSSL(httplib::Request& request, const URLInfo& url_info, + std::vector& pending_headers); + void MakeRequestSSL(httplib::Request& request, const URLInfo& url_info, + std::vector& pending_headers); }; struct SessionData : public Kernel::SessionRequestHandler::SessionDataBase { @@ -595,6 +610,13 @@ private: /// Global list of HTTP contexts currently opened. std::unordered_map contexts; + // Get context from its handle + inline Context& GetContext(const Context::Handle& handle) { + auto it = contexts.find(handle); + ASSERT(it != contexts.end()); + return it->second; + } + /// Global list of ClientCert contexts currently opened. std::unordered_map> client_certs; diff --git a/src/tests/audio_core/hle/hle.cpp b/src/tests/audio_core/hle/hle.cpp index 07d5e04af7..7b77209f98 100644 --- a/src/tests/audio_core/hle/hle.cpp +++ b/src/tests/audio_core/hle/hle.cpp @@ -25,7 +25,7 @@ TEST_CASE("DSP LLE vs HLE", "[audio_core][hle]") { AudioCore::DspHle hle(hle_memory, hle_core_timing); AudioCore::DspLle lle(lle_memory, lle_core_timing, true); - // Initialiase LLE + // Initialise LLE { FileUtil::SetUserPath(); // see tests/audio_core/lle/lle.cpp for details on dspaudio.cdc @@ -41,25 +41,15 @@ TEST_CASE("DSP LLE vs HLE", "[audio_core][hle]") { std::vector firm_file_buf(firm_file.GetSize()); firm_file.ReadArray(firm_file_buf.data(), firm_file_buf.size()); lle.LoadComponent(firm_file_buf); - lle.SetSemaphoreHandler([&lle]() { - u16 slot = lle.RecvData(2); - u16 side = slot % 2; - u16 pipe = slot / 2; - fmt::print("SetSemaphoreHandler slot={}\n", slot); - if (pipe > 15) - return; - if (side != 0) - return; - if (pipe == 0) { - // pipe 0 is for debug. 3DS automatically drains this pipe and discards the - // data - lle.PipeRead(static_cast(pipe), - lle.GetPipeReadableSize(static_cast(pipe))); - } + lle.SetInterruptHandler([](Service::DSP::InterruptType type, AudioCore::DspPipe pipe) { + fmt::print("LLE SetInterruptHandler type={} pipe={}\n", type, pipe); + }); + } + // Initialise HLE + { + hle.SetInterruptHandler([](Service::DSP::InterruptType type, AudioCore::DspPipe pipe) { + fmt::print("HLE SetInterruptHandler type={} pipe={}\n", type, pipe); }); - lle.SetRecvDataHandler(0, []() { fmt::print("SetRecvDataHandler 0\n"); }); - lle.SetRecvDataHandler(1, []() { fmt::print("SetRecvDataHandler 1\n"); }); - lle.SetRecvDataHandler(2, []() { fmt::print("SetRecvDataHandler 2\n"); }); } SECTION("Initialise Audio Pipe") { diff --git a/src/tests/audio_core/lle/lle.cpp b/src/tests/audio_core/lle/lle.cpp index d445542106..e4a85e68c3 100644 --- a/src/tests/audio_core/lle/lle.cpp +++ b/src/tests/audio_core/lle/lle.cpp @@ -37,26 +37,11 @@ TEST_CASE("DSP LLE Sanity", "[audio_core][lle]") { std::vector firm_file_buf(firm_file.GetSize()); firm_file.ReadArray(firm_file_buf.data(), firm_file_buf.size()); lle.LoadComponent(firm_file_buf); + + lle.SetInterruptHandler([](Service::DSP::InterruptType type, AudioCore::DspPipe pipe) { + fmt::print("SetInterruptHandler type={} pipe={}\n", type, pipe); + }); } - lle.SetSemaphoreHandler([&lle]() { - u16 slot = lle.RecvData(2); - u16 side = slot % 2; - u16 pipe = slot / 2; - fmt::print("SetSemaphoreHandler slot={}\n", slot); - if (pipe > 15) - return; - if (side != 0) - return; - if (pipe == 0) { - // pipe 0 is for debug. 3DS automatically drains this pipe and discards the - // data - lle.PipeRead(static_cast(pipe), - lle.GetPipeReadableSize(static_cast(pipe))); - } - }); - lle.SetRecvDataHandler(0, []() { fmt::print("SetRecvDataHandler 0\n"); }); - lle.SetRecvDataHandler(1, []() { fmt::print("SetRecvDataHandler 1\n"); }); - lle.SetRecvDataHandler(2, []() { fmt::print("SetRecvDataHandler 2\n"); }); SECTION("Initialise Audio Pipe") { std::vector buffer(4, 0); buffer[0] = 0;