From 7d9d6c576ad9230c654f4858653e7cfac3902a71 Mon Sep 17 00:00:00 2001 From: FearlessTobi Date: Sun, 30 Jul 2023 20:04:43 +0200 Subject: [PATCH] http_c: Split url for request httplib requires the url to be split in a host and path component in order for requests to be made. --- src/core/hle/service/http_c.cpp | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/core/hle/service/http_c.cpp b/src/core/hle/service/http_c.cpp index 094f908bad..b5d128819e 100644 --- a/src/core/hle/service/http_c.cpp +++ b/src/core/hle/service/http_c.cpp @@ -55,11 +55,31 @@ const ResultCode ERROR_WRONG_CERT_HANDLE = // 0xD8A0A0C9 const ResultCode ERROR_CERT_ALREADY_SET = // 0xD8A0A03D ResultCode(61, ErrorModule::HTTP, ErrorSummary::InvalidState, ErrorLevel::Permanent); +static std::pair splitUrl(const std::string& url) { + const std::string prefix = "://"; + const auto scheme = url.find(prefix); + const auto scheme_end = scheme == std::string::npos ? 0 : scheme + prefix.length(); + + const auto path_index = url.find("/", scheme_end); + std::string host; + std::string path; + if (path_index == std::string::npos) { + // If no path is specified after the host, set it to "/" + host = url; + path = "/"; + } else { + host = url.substr(0, path_index); + path = url.substr(path_index); + } + return std::make_pair(host, path); +} + void Context::MakeRequest() { ASSERT(state == RequestState::NotStarted); #ifdef ENABLE_WEB_SERVICE - std::unique_ptr client = std::make_unique(url.c_str()); + const auto& [host, path] = splitUrl(url.c_str()); + std::unique_ptr client = std::make_unique(host); SSL_CTX* ctx = client->ssl_context(); if (ctx) { if (auto client_cert = ssl_config.client_cert_ctx.lock()) { @@ -87,7 +107,7 @@ void Context::MakeRequest() { httplib::Request request; httplib::Error error; request.method = request_method_strings.at(method); - request.path = url; + request.path = path; // TODO(B3N30): Add post data body request.progress = [this](u64 current, u64 total) -> bool { // TODO(B3N30): Is there a state that shows response header are available @@ -101,7 +121,7 @@ void Context::MakeRequest() { } if (!client->send(request, response, error)) { - LOG_ERROR(Service_HTTP, "Request failed: {}", error); + LOG_ERROR(Service_HTTP, "Request failed: {}: {}", error, httplib::to_string(error)); state = RequestState::TimedOut; } else { LOG_DEBUG(Service_HTTP, "Request successful");