build: Fix web service functionality. (#6903)

This commit is contained in:
Steveice10 2023-08-19 17:34:35 -07:00 committed by GitHub
parent 6ddf4b241f
commit b2092de871
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 15 additions and 19 deletions

View file

@ -458,6 +458,8 @@ add_library(citra_core STATIC
mmio.h
movie.cpp
movie.h
nus_download.cpp
nus_download.h
perf_stats.cpp
perf_stats.h
precompiled_headers.h

View file

@ -31,9 +31,7 @@
#include "core/hle/service/fs/fs_user.h"
#include "core/loader/loader.h"
#include "core/loader/smdh.h"
#ifdef ENABLE_WEB_SERVICE
#include "web_service/nus_download.h"
#endif
#include "core/nus_download.h"
namespace Service::AM {
@ -463,7 +461,6 @@ InstallStatus InstallCIA(const std::string& path,
}
InstallStatus InstallFromNus(u64 title_id, int version) {
#ifdef ENABLE_WEB_SERVICE
LOG_DEBUG(Service_AM, "Downloading {:X}", title_id);
CIAFile install_file{GetTitleMediaType(title_id)};
@ -472,7 +469,7 @@ InstallStatus InstallFromNus(u64 title_id, int version) {
if (version != -1) {
path += fmt::format(".{}", version);
}
auto tmd_response = WebService::NUS::Download(path);
auto tmd_response = Core::NUS::Download(path);
if (!tmd_response) {
LOG_ERROR(Service_AM, "Failed to download tmd for {:016X}", title_id);
return InstallStatus::ErrorFileNotFound;
@ -481,7 +478,7 @@ InstallStatus InstallFromNus(u64 title_id, int version) {
tmd.Load(*tmd_response);
path = fmt::format("/ccs/download/{:016X}/cetk", title_id);
auto cetk_response = WebService::NUS::Download(path);
auto cetk_response = Core::NUS::Download(path);
if (!cetk_response) {
LOG_ERROR(Service_AM, "Failed to download cetk for {:016X}", title_id);
return InstallStatus::ErrorFileNotFound;
@ -492,7 +489,7 @@ InstallStatus InstallFromNus(u64 title_id, int version) {
for (std::size_t i = 0; i < content_count; ++i) {
const std::string filename = fmt::format("{:08x}", tmd.GetContentIDByIndex(i));
path = fmt::format("/ccs/download/{:016X}/{}", title_id, filename);
const auto temp_response = WebService::NUS::Download(path);
const auto temp_response = Core::NUS::Download(path);
if (!temp_response) {
LOG_ERROR(Service_AM, "Failed to download content for {:016X}", title_id);
return InstallStatus::ErrorFileNotFound;
@ -550,9 +547,6 @@ InstallStatus InstallFromNus(u64 title_id, int version) {
return result;
}
return InstallStatus::Success;
#else
return InstallStatus::ErrorFileNotFound;
#endif
}
u64 GetTitleUpdateId(u64 title_id) {

49
src/core/nus_download.cpp Normal file
View file

@ -0,0 +1,49 @@
// Copyright 2020 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include <memory>
#include <httplib.h>
#include "common/logging/log.h"
#include "core/nus_download.h"
namespace Core::NUS {
std::optional<std::vector<u8>> Download(const std::string& path) {
constexpr auto HOST = "http://nus.cdn.c.shop.nintendowifi.net";
std::unique_ptr<httplib::Client> client = std::make_unique<httplib::Client>(HOST);
if (client == nullptr) {
LOG_ERROR(WebService, "Invalid URL {}{}", HOST, path);
return {};
}
httplib::Request request{
.method = "GET",
.path = path,
// Needed when httplib is included on android
.matches = httplib::Match(),
};
client->set_follow_location(true);
const auto result = client->send(request);
if (!result) {
LOG_ERROR(WebService, "GET to {}{} returned null", HOST, path);
return {};
}
const auto& response = result.value();
if (response.status >= 400) {
LOG_ERROR(WebService, "GET to {}{} returned error status code: {}", HOST, path,
response.status);
return {};
}
if (!response.headers.contains("content-type")) {
LOG_ERROR(WebService, "GET to {}{} returned no content", HOST, path);
return {};
}
return std::vector<u8>(response.body.begin(), response.body.end());
}
} // namespace Core::NUS

15
src/core/nus_download.h Normal file
View file

@ -0,0 +1,15 @@
// Copyright 2020 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#pragma once
#include <optional>
#include <vector>
#include "common/common_types.h"
namespace Core::NUS {
std::optional<std::vector<u8>> Download(const std::string& path);
} // namespace Core::NUS