From b28ade1ee8dfd3ba7ed85d43f3f18880fbf2c102 Mon Sep 17 00:00:00 2001 From: Castor215 <132155746+Castor215@users.noreply.github.com> Date: Tue, 3 Oct 2023 09:19:18 +0100 Subject: [PATCH 01/17] externals: mark cpp-jwt headers as SYSTEM (#7033) --- externals/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/externals/CMakeLists.txt b/externals/CMakeLists.txt index 25f864e0fb..b81b3fe9ba 100644 --- a/externals/CMakeLists.txt +++ b/externals/CMakeLists.txt @@ -216,7 +216,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() From e0373d679cbd61ba7f9482f61e5fa74c4df816d0 Mon Sep 17 00:00:00 2001 From: PabloMK7 Date: Tue, 3 Oct 2023 17:49:19 +0200 Subject: [PATCH 02/17] Disable treat errors as warnings for httplib --- src/core/hle/service/http/http_c.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/core/hle/service/http/http_c.h b/src/core/hle/service/http/http_c.h index 722e5e93ae..d2e0c45ee6 100644 --- a/src/core/hle/service/http/http_c.h +++ b/src/core/hle/service/http/http_c.h @@ -20,7 +20,23 @@ #if defined(__ANDROID__) #include #endif +#if defined(_WIN32) +#ifdef _MSC_VER +#pragma warning(push) +#pragma warning(default : all) +#else +#pragma GCC diagnostic push +#pragma GCC diagnostic warning "-Werror" +#endif +#endif #include +#if defined(_WIN32) +#ifdef _MSC_VER +#pragma warning(pop) +#else +#pragma GCC diagnostic pop +#endif +#endif #include "core/hle/ipc_helpers.h" #include "core/hle/kernel/shared_memory.h" #include "core/hle/service/service.h" From 0b11a2b79089c90316fe58eab99c84ec83fae444 Mon Sep 17 00:00:00 2001 From: PabloMK7 Date: Tue, 3 Oct 2023 18:23:50 +0200 Subject: [PATCH 03/17] Fix defines --- src/core/hle/service/http/http_c.h | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/src/core/hle/service/http/http_c.h b/src/core/hle/service/http/http_c.h index d2e0c45ee6..aaaf7f4439 100644 --- a/src/core/hle/service/http/http_c.h +++ b/src/core/hle/service/http/http_c.h @@ -20,23 +20,14 @@ #if defined(__ANDROID__) #include #endif -#if defined(_WIN32) -#ifdef _MSC_VER -#pragma warning(push) -#pragma warning(default : all) -#else +#if defined(_WIN32) && !defined(_MSC_VER) #pragma GCC diagnostic push #pragma GCC diagnostic warning "-Werror" #endif -#endif #include -#if defined(_WIN32) -#ifdef _MSC_VER -#pragma warning(pop) -#else +#if defined(_WIN32) && !defined(_MSC_VER) #pragma GCC diagnostic pop #endif -#endif #include "core/hle/ipc_helpers.h" #include "core/hle/kernel/shared_memory.h" #include "core/hle/service/service.h" From 62ee5e2cdc45971db612ca4acf4e93294f714bd5 Mon Sep 17 00:00:00 2001 From: PabloMK7 Date: Tue, 3 Oct 2023 19:31:47 +0200 Subject: [PATCH 04/17] Remove pragmas that do nothing and mark as SYSTEM --- externals/CMakeLists.txt | 2 +- src/core/hle/service/http/http_c.h | 7 ------- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/externals/CMakeLists.txt b/externals/CMakeLists.txt index 25f864e0fb..d86e6e34e4 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 diff --git a/src/core/hle/service/http/http_c.h b/src/core/hle/service/http/http_c.h index aaaf7f4439..722e5e93ae 100644 --- a/src/core/hle/service/http/http_c.h +++ b/src/core/hle/service/http/http_c.h @@ -20,14 +20,7 @@ #if defined(__ANDROID__) #include #endif -#if defined(_WIN32) && !defined(_MSC_VER) -#pragma GCC diagnostic push -#pragma GCC diagnostic warning "-Werror" -#endif #include -#if defined(_WIN32) && !defined(_MSC_VER) -#pragma GCC diagnostic pop -#endif #include "core/hle/ipc_helpers.h" #include "core/hle/kernel/shared_memory.h" #include "core/hle/service/service.h" From 3476fd456570a9d7245804656623ae345e7aabbf Mon Sep 17 00:00:00 2001 From: PabloMK7 Date: Tue, 3 Oct 2023 21:40:29 +0200 Subject: [PATCH 05/17] Make httplib system --- externals/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/externals/CMakeLists.txt b/externals/CMakeLists.txt index d86e6e34e4..64c7c489fb 100644 --- a/externals/CMakeLists.txt +++ b/externals/CMakeLists.txt @@ -199,7 +199,7 @@ endif() # httplib add_library(httplib INTERFACE) -target_include_directories(httplib INTERFACE ./httplib) +target_include_directories(httplib SYSTEM INTERFACE ./httplib) target_compile_options(httplib INTERFACE -DCPPHTTPLIB_OPENSSL_SUPPORT) target_link_libraries(httplib INTERFACE ${OPENSSL_LIBRARIES}) From bf5c707a88c2d068e6774311aab8836f776e5906 Mon Sep 17 00:00:00 2001 From: PabloMK7 Date: Tue, 3 Oct 2023 23:10:20 +0200 Subject: [PATCH 06/17] Try to fix issue from httplib --- externals/httplib/httplib.h | 6 ++++++ 1 file changed, 6 insertions(+) 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") From 0ce956ba000031c202a1ba296264d71009ce6934 Mon Sep 17 00:00:00 2001 From: Castor215 <132155746+Castor215@users.noreply.github.com> Date: Wed, 4 Oct 2023 14:41:13 +0100 Subject: [PATCH 07/17] externals: allow users to use system cpp-httplib (#7034) --- CMakeLists.txt | 1 + externals/CMakeLists.txt | 7 ++++- externals/cmake-modules/FindCppHttp.cmake | 35 +++++++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 externals/cmake-modules/FindCppHttp.cmake 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 b81b3fe9ba..7e3a0e7d7d 100644 --- a/externals/CMakeLists.txt +++ b/externals/CMakeLists.txt @@ -199,7 +199,12 @@ endif() # httplib add_library(httplib INTERFACE) -target_include_directories(httplib INTERFACE ./httplib) +if(USE_SYSTEM_CPP_HTTPLIB) + find_package(CppHttp REQUIRED) + target_link_libraries(httplib INTERFACE cpp-httplib::cpp-httplib) +else() + target_include_directories(httplib INTERFACE ./httplib) +endif() target_compile_options(httplib INTERFACE -DCPPHTTPLIB_OPENSSL_SUPPORT) target_link_libraries(httplib INTERFACE ${OPENSSL_LIBRARIES}) 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() From 72ff0c53375b487c5a95ba9702d391a19c629b88 Mon Sep 17 00:00:00 2001 From: SachinVin <26602104+SachinVin@users.noreply.github.com> Date: Wed, 4 Oct 2023 19:14:59 +0530 Subject: [PATCH 08/17] AudioCore: Refactor DSP interrupt handling (#7026) --- src/audio_core/dsp_interface.h | 7 +++--- src/audio_core/hle/hle.cpp | 37 +++++++++++++--------------- src/audio_core/hle/hle.h | 3 ++- src/audio_core/lle/lle.cpp | 32 ++++++------------------ src/audio_core/lle/lle.h | 6 ++--- src/core/hle/service/dsp/dsp_dsp.cpp | 15 ++++++++--- src/core/hle/service/dsp/dsp_dsp.h | 7 +++--- src/tests/audio_core/hle/hle.cpp | 28 +++++++-------------- src/tests/audio_core/lle/lle.cpp | 23 +++-------------- 9 files changed, 61 insertions(+), 97 deletions(-) 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/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/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; From 5fb455425ce62536454f0bfdb661a88ddaf23511 Mon Sep 17 00:00:00 2001 From: PabloMK7 Date: Wed, 4 Oct 2023 21:59:47 +0200 Subject: [PATCH 09/17] Apply suggestions --- src/core/hle/service/http/http_c.cpp | 302 +++++++++++++-------------- src/core/hle/service/http/http_c.h | 11 + 2 files changed, 161 insertions(+), 152 deletions(-) diff --git a/src/core/hle/service/http/http_c.cpp b/src/core/hle/service/http/http_c.cpp index 45861d4d5d..0eed95c4a4 100644 --- a/src/core/hle/service/http/http_c.cpp +++ b/src/core/hle/service/http/http_c.cpp @@ -63,25 +63,16 @@ 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) { - 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()); - if (len < 0) { - return len; - } - write_len += len; - } - auto len = strm.write("\r\n"); - if (len < 0) { - return len; - } - write_len += len; - return write_len; -} +struct URLInfo { + bool is_https; + std::string host; + int port; + std::string path; +}; -static std::tuple SplitUrl(const std::string& url) { +// 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) { 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(); @@ -91,6 +82,8 @@ static std::tuple SplitUrl(const std::strin const auto path_index = url.find("/", prefix_end); std::string host; int port = -1; + constexpr int default_http_port = 80; + constexpr int default_https_port = 443; std::string path; if (path_index == std::string::npos) { // If no path is specified after the host, set it to "/" @@ -121,11 +114,79 @@ static std::tuple SplitUrl(const std::strin path = url.substr(path_index); } if (port == -1) { - port = is_https ? 443 : 80; + port = is_https ? default_https_port : default_http_port; } - return std::make_tuple(host, port, path, is_https); + return URLInfo{.is_https = is_https, .host = host, .port = port, .path = path}; } +static ssize_t WriteHeaders(httplib::Stream& strm, + const std::vector& 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()); + if (len < 0) { + return len; + } + write_len += len; + } + auto len = strm.write("\r\n"); + if (len < 0) { + return len; + } + write_len += len; + return write_len; +} + +static size_t HeaderWriteHandler(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; + + 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 WriteHeaders(strm, final_headers); +}; + void Context::MakeRequest() { ASSERT(state == RequestState::NotStarted); @@ -136,13 +197,13 @@ void Context::MakeRequest() { {RequestMethod::PutEmpty, "PUT"}, }; - const auto& [host, port, path, is_https] = SplitUrl(url.c_str()); + URLInfo url_info = SplitUrl(url.c_str()); 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,55 +212,10 @@ void Context::MakeRequest() { return true; }; - // Watch out for header ordering! + // 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); + return HeaderWriteHandler(pending_headers, strm, httplib_headers); }; if (!post_data.empty()) { @@ -220,7 +236,7 @@ void Context::MakeRequest() { state = RequestState::InProgress; // Sadly, we have to duplicate code, the class hierarchy in httplib is not very useful... - if (is_https) { + if (url_info.is_https) { X509* cert = nullptr; EVP_PKEY* key = nullptr; { @@ -231,18 +247,18 @@ void Context::MakeRequest() { 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); + 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 { - 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); - } + client = std::make_unique(url_info.host, url_info.port); } // TODO(B3N30): Check for SSLOptions-Bits and set the verify method accordingly @@ -268,7 +284,8 @@ void Context::MakeRequest() { EVP_PKEY_free(key); } } else { - std::unique_ptr client = std::make_unique(host, port); + std::unique_ptr client = + std::make_unique(url_info.host, url_info.port); client->set_header_writer(header_writter); @@ -358,11 +375,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 +392,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 +410,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 +427,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 +479,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( @@ -488,10 +502,7 @@ void HTTP_C::ReceiveDataImpl(Kernel::HLERequestContext& ctx, bool timeout) { 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 +649,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,8 +677,7 @@ void HTTP_C::AddRequestHeader(Kernel::HLERequestContext& ctx) { return; } - auto itr = contexts.find(context_handle); - ASSERT(itr != contexts.end()); + [[maybe_unused]] Context& http_context = GetContext(context_handle); if (itr->second.state != RequestState::NotStarted) { LOG_ERROR(Service_HTTP, @@ -709,10 +718,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 +730,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 +749,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 +761,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 +777,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 +786,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 = std::span(rp.PopStaticBuffer()); async_data->value_buffer = &rp.PopMappedBuffer(); if (!PerformStateChecks(ctx, rp, async_data->context_handle)) { @@ -788,24 +795,21 @@ 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(); } - 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, @@ -874,11 +878,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,7 +890,7 @@ void HTTP_C::GetResponseStatusCodeImpl(Kernel::HLERequestContext& ctx, bool time ErrorLevel::Permanent); } } else { - itr->second.request_future.wait(); + http_context.request_future.wait(); } return 0; }, @@ -899,10 +902,9 @@ void HTTP_C::GetResponseStatusCodeImpl(Kernel::HLERequestContext& ctx, bool time 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, @@ -940,7 +942,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 = rp.Pop(); LOG_DEBUG(Service_HTTP, "client_cert_id={}", client_cert_id); @@ -948,17 +950,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 +977,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 +987,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 +995,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 +1004,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 +1016,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 +1102,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 +1209,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..370889ee0f 100644 --- a/src/core/hle/service/http/http_c.h +++ b/src/core/hle/service/http/http_c.h @@ -57,6 +57,10 @@ enum class RequestState : u8 { TimedOut = 0xA, // Request timed out? }; +enum class ClientCertID : u32 { + Default = 0x40, // Default client cert +}; + /// 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. @@ -595,6 +599,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; From 0689a4a22f5d1ee369332f6c3d59087c2f12e1eb Mon Sep 17 00:00:00 2001 From: PabloMK7 Date: Wed, 4 Oct 2023 22:03:35 +0200 Subject: [PATCH 10/17] Fix header ordering --- src/core/hle/service/http/http_c.cpp | 32 +++++++++++----------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/src/core/hle/service/http/http_c.cpp b/src/core/hle/service/http/http_c.cpp index 0eed95c4a4..8d3de3d911 100644 --- a/src/core/hle/service/http/http_c.cpp +++ b/src/core/hle/service/http/http_c.cpp @@ -137,8 +137,8 @@ static ssize_t WriteHeaders(httplib::Stream& strm, return write_len; } -static size_t HeaderWriteHandler(std::vector& pending_headers, - httplib::Stream& strm, httplib::Headers& httplib_headers) { +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; @@ -148,6 +148,7 @@ static size_t HeaderWriteHandler(std::vector& pending_he [&str](Context::RequestHeader& rh) { return rh.name == str; }); }; + // Watch out for header ordering!! // First: Host it_p = find_pending_header("Host"); if (it_p != pending_headers.end()) { @@ -160,14 +161,13 @@ static size_t HeaderWriteHandler(std::vector& pending_he } } - // 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); + // Second, user defined headers + // Third, Content-Type (optional, appended by MakeRequest) + for (const auto& header : pending_headers) { + final_headers.push_back(header); } - // Third: Content-Length + // 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)); @@ -179,11 +179,6 @@ static size_t HeaderWriteHandler(std::vector& pending_he } } - // Finally, user defined headers - for (const auto& header : pending_headers) { - final_headers.push_back(header); - } - return WriteHeaders(strm, final_headers); }; @@ -212,12 +207,15 @@ void Context::MakeRequest() { return true; }; - // Watch out for header ordering!! auto header_writter = [&pending_headers](httplib::Stream& strm, httplib::Headers& httplib_headers) { - return HeaderWriteHandler(pending_headers, strm, httplib_headers); + return HandleHeaderWrite(pending_headers, strm, httplib_headers); }; + for (const auto& header : headers) { + pending_headers.push_back(header); + } + if (!post_data.empty()) { pending_headers.push_back( Context::RequestHeader("Content-Type", "application/x-www-form-urlencoded")); @@ -225,10 +223,6 @@ 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; } From 0fa535775487464164bc84773b5205f7578738e4 Mon Sep 17 00:00:00 2001 From: PabloMK7 Date: Wed, 4 Oct 2023 22:20:29 +0200 Subject: [PATCH 11/17] Fix compilation issue --- src/core/hle/service/http/http_c.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/core/hle/service/http/http_c.cpp b/src/core/hle/service/http/http_c.cpp index 8d3de3d911..b389dc8529 100644 --- a/src/core/hle/service/http/http_c.cpp +++ b/src/core/hle/service/http/http_c.cpp @@ -671,9 +671,9 @@ void HTTP_C::AddRequestHeader(Kernel::HLERequestContext& ctx) { return; } - [[maybe_unused]] Context& http_context = GetContext(context_handle); + 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); @@ -683,7 +683,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); From 3ccbe657b152144bf9a9c867b4bf29be89399c8d Mon Sep 17 00:00:00 2001 From: PabloMK7 Date: Wed, 4 Oct 2023 22:31:14 +0200 Subject: [PATCH 12/17] Create and use ctx.CommandID() --- src/core/hle/kernel/hle_ipc.h | 5 +++++ src/core/hle/service/http/http_c.cpp | 13 +++++-------- 2 files changed, 10 insertions(+), 8 deletions(-) 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/http/http_c.cpp b/src/core/hle/service/http/http_c.cpp index b389dc8529..d0dbf410ba 100644 --- a/src/core/hle/service/http/http_c.cpp +++ b/src/core/hle/service/http/http_c.cpp @@ -490,8 +490,7 @@ 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; @@ -820,13 +819,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); @@ -890,8 +889,7 @@ void HTTP_C::GetResponseStatusCodeImpl(Kernel::HLERequestContext& ctx, bool time }, [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; } @@ -901,8 +899,7 @@ void HTTP_C::GetResponseStatusCodeImpl(Kernel::HLERequestContext& ctx, bool time 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); }); From 937ffb267c4c371405ec8910650ee8dbe020e079 Mon Sep 17 00:00:00 2001 From: PabloMK7 Date: Wed, 4 Oct 2023 22:53:42 +0200 Subject: [PATCH 13/17] Add and use Common::TruncateString --- src/common/string_util.h | 10 ++++++++++ src/core/hle/service/http/http_c.cpp | 5 ++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/common/string_util.h b/src/common/string_util.h index 74c342e5bb..20cfec770d 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. + */ +[[nodiscard]] 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/service/http/http_c.cpp b/src/core/hle/service/http/http_c.cpp index d0dbf410ba..4776cbc73f 100644 --- a/src/core/hle/service/http/http_c.cpp +++ b/src/core/hle/service/http/http_c.cpp @@ -10,6 +10,7 @@ #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" @@ -796,9 +797,7 @@ void HTTP_C::GetResponseHeader(Kernel::HLERequestContext& ctx) { std::string header_name_str( 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); Context& http_context = async_data->own->GetContext(async_data->context_handle); From 6f98457888bf85a6975b960af34f802450fab8e1 Mon Sep 17 00:00:00 2001 From: PabloMK7 Date: Wed, 4 Oct 2023 22:57:00 +0200 Subject: [PATCH 14/17] Apply more suggestions --- src/core/hle/service/http/http_c.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/core/hle/service/http/http_c.cpp b/src/core/hle/service/http/http_c.cpp index 4776cbc73f..64ef1cd722 100644 --- a/src/core/hle/service/http/http_c.cpp +++ b/src/core/hle/service/http/http_c.cpp @@ -117,11 +117,16 @@ static URLInfo SplitUrl(const std::string& url) { if (port == -1) { port = is_https ? default_https_port : default_http_port; } - return URLInfo{.is_https = is_https, .host = host, .port = port, .path = path}; + return URLInfo{ + .is_https = is_https, + .host = host, + .port = port, + .path = path, + }; } static ssize_t WriteHeaders(httplib::Stream& strm, - const std::vector& headers) { + 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()); @@ -193,7 +198,7 @@ void Context::MakeRequest() { {RequestMethod::PutEmpty, "PUT"}, }; - URLInfo url_info = SplitUrl(url.c_str()); + URLInfo url_info = SplitUrl(url); httplib::Request request; httplib::Error error{-1}; From 3f640ae662df86fd3def1d4a63f6225a96204916 Mon Sep 17 00:00:00 2001 From: PabloMK7 Date: Thu, 5 Oct 2023 13:01:50 +0200 Subject: [PATCH 15/17] Apply suggestions --- src/common/string_util.h | 2 +- src/core/hle/service/http/http_c.cpp | 59 ++++++---------------------- 2 files changed, 13 insertions(+), 48 deletions(-) diff --git a/src/common/string_util.h b/src/common/string_util.h index 20cfec770d..2384e9c510 100644 --- a/src/common/string_util.h +++ b/src/common/string_util.h @@ -88,7 +88,7 @@ std::string UTF16BufferToUTF8(const T& text) { /** * Removes trailing null bytes from the string. */ -[[nodiscard]] void TruncateString(std::string& str) { +inline void TruncateString(std::string& str) { while (str.size() && str.back() == '\0') { str.pop_back(); } diff --git a/src/core/hle/service/http/http_c.cpp b/src/core/hle/service/http/http_c.cpp index 64ef1cd722..5bcfbae2b7 100644 --- a/src/core/hle/service/http/http_c.cpp +++ b/src/core/hle/service/http/http_c.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include "common/archives.h" @@ -74,54 +75,18 @@ struct URLInfo { // 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) { - 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(); + constexpr u16 default_http_port = 80; + constexpr u16 default_https_port = 443; - bool is_https = scheme_end != std::string::npos && url.starts_with("https"); - - const auto path_index = url.find("/", prefix_end); - std::string host; - int port = -1; - constexpr int default_http_port = 80; - constexpr int default_https_port = 443; - 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 = "/"; - } 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; - } - } - path = url.substr(path_index); - } - if (port == -1) { - port = is_https ? default_https_port : default_http_port; - } + 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 = host, - .port = port, - .path = path, + .host = std::string{result->encoded_host()}, + .port = port == 0 ? default_port : port, + .path = std::string{result->encoded_path()}, }; } @@ -785,7 +750,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 = std::span(rp.PopStaticBuffer()); + async_data->header_name = rp.PopStaticBuffer(); async_data->value_buffer = &rp.PopMappedBuffer(); if (!PerformStateChecks(ctx, rp, async_data->context_handle)) { @@ -937,7 +902,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 ClientCertID client_cert_id = rp.Pop(); + const ClientCertID client_cert_id = static_cast(rp.Pop()); LOG_DEBUG(Service_HTTP, "client_cert_id={}", client_cert_id); From 13418013a4657e9083a862f03e08c6926b968094 Mon Sep 17 00:00:00 2001 From: PabloMK7 Date: Thu, 5 Oct 2023 14:16:08 +0200 Subject: [PATCH 16/17] Fix compilation --- src/core/hle/service/http/http_c.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/hle/service/http/http_c.cpp b/src/core/hle/service/http/http_c.cpp index 5bcfbae2b7..3451ed7d7b 100644 --- a/src/core/hle/service/http/http_c.cpp +++ b/src/core/hle/service/http/http_c.cpp @@ -6,7 +6,7 @@ #include #include #include -#include +#include #include #include #include "common/archives.h" From b8c3f0648b635ffdb1827d381c926de4179f7ca4 Mon Sep 17 00:00:00 2001 From: PabloMK7 Date: Thu, 5 Oct 2023 14:48:02 +0200 Subject: [PATCH 17/17] Apply suggestions --- src/core/hle/service/http/http_c.cpp | 127 ++++++++++++++------------- src/core/hle/service/http/http_c.h | 15 +++- 2 files changed, 77 insertions(+), 65 deletions(-) diff --git a/src/core/hle/service/http/http_c.cpp b/src/core/hle/service/http/http_c.cpp index 3451ed7d7b..b57a061e45 100644 --- a/src/core/hle/service/http/http_c.cpp +++ b/src/core/hle/service/http/http_c.cpp @@ -65,13 +65,6 @@ const ResultCode ERROR_WRONG_CERT_HANDLE = // 0xD8A0A0C9 const ResultCode ERROR_CERT_ALREADY_SET = // 0xD8A0A03D ResultCode(61, ErrorModule::HTTP, ErrorSummary::InvalidState, ErrorLevel::Permanent); -struct URLInfo { - bool is_https; - std::string host; - int port; - std::string path; -}; - // 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) { @@ -166,7 +159,6 @@ void Context::MakeRequest() { 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 = url_info.path; @@ -178,11 +170,6 @@ void Context::MakeRequest() { return true; }; - auto header_writter = [&pending_headers](httplib::Stream& strm, - httplib::Headers& httplib_headers) { - return HandleHeaderWrite(pending_headers, strm, httplib_headers); - }; - for (const auto& header : headers) { pending_headers.push_back(header); } @@ -200,59 +187,67 @@ void Context::MakeRequest() { state = RequestState::InProgress; - // Sadly, we have to duplicate code, the class hierarchy in httplib is not very useful... if (url_info.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(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(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); - } + MakeRequestSSL(request, url_info, pending_headers); } else { - std::unique_ptr client = - std::make_unique(url_info.host, url_info.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)); @@ -263,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) { diff --git a/src/core/hle/service/http/http_c.h b/src/core/hle/service/http/http_c.h index 370889ee0f..07547d05ea 100644 --- a/src/core/hle/service/http/http_c.h +++ b/src/core/hle/service/http/http_c.h @@ -61,6 +61,13 @@ 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. @@ -134,8 +141,6 @@ public: Context(const Context&) = delete; Context& operator=(const Context&) = delete; - void MakeRequest(); - struct Proxy { std::string url; std::string username; @@ -215,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 {