diff options
Diffstat (limited to 'src/web_service')
-rw-r--r-- | src/web_service/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/web_service/telemetry_json.cpp | 6 | ||||
-rw-r--r-- | src/web_service/telemetry_json.h | 30 | ||||
-rw-r--r-- | src/web_service/verify_login.cpp | 2 | ||||
-rw-r--r-- | src/web_service/web_backend.cpp | 56 | ||||
-rw-r--r-- | src/web_service/web_backend.h | 20 | ||||
-rw-r--r-- | src/web_service/web_result.h | 25 |
7 files changed, 81 insertions, 59 deletions
diff --git a/src/web_service/CMakeLists.txt b/src/web_service/CMakeLists.txt index 06ab7c59d..7e484b906 100644 --- a/src/web_service/CMakeLists.txt +++ b/src/web_service/CMakeLists.txt @@ -5,6 +5,7 @@ add_library(web_service STATIC verify_login.h web_backend.cpp web_backend.h + web_result.h ) create_target_directory_groups(web_service) diff --git a/src/web_service/telemetry_json.cpp b/src/web_service/telemetry_json.cpp index 7a480e33c..6215c914f 100644 --- a/src/web_service/telemetry_json.cpp +++ b/src/web_service/telemetry_json.cpp @@ -4,12 +4,14 @@ #include <nlohmann/json.hpp> #include "common/detached_tasks.h" -#include "common/web_result.h" #include "web_service/telemetry_json.h" #include "web_service/web_backend.h" +#include "web_service/web_result.h" namespace WebService { +namespace Telemetry = Common::Telemetry; + struct TelemetryJson::Impl { Impl(std::string host, std::string username, std::string token) : host{std::move(host)}, username{std::move(username)}, token{std::move(token)} {} @@ -123,7 +125,7 @@ bool TelemetryJson::SubmitTestcase() { Client client(impl->host, impl->username, impl->token); auto value = client.PostJson("/gamedb/testcase", content, false); - return value.result_code == Common::WebResult::Code::Success; + return value.result_code == WebResult::Code::Success; } } // namespace WebService diff --git a/src/web_service/telemetry_json.h b/src/web_service/telemetry_json.h index dfd202829..df51e00f8 100644 --- a/src/web_service/telemetry_json.h +++ b/src/web_service/telemetry_json.h @@ -14,25 +14,25 @@ namespace WebService { * Implementation of VisitorInterface that serialized telemetry into JSON, and submits it to the * yuzu web service */ -class TelemetryJson : public Telemetry::VisitorInterface { +class TelemetryJson : public Common::Telemetry::VisitorInterface { public: TelemetryJson(std::string host, std::string username, std::string token); ~TelemetryJson() override; - void Visit(const Telemetry::Field<bool>& field) override; - void Visit(const Telemetry::Field<double>& field) override; - void Visit(const Telemetry::Field<float>& field) override; - void Visit(const Telemetry::Field<u8>& field) override; - void Visit(const Telemetry::Field<u16>& field) override; - void Visit(const Telemetry::Field<u32>& field) override; - void Visit(const Telemetry::Field<u64>& field) override; - void Visit(const Telemetry::Field<s8>& field) override; - void Visit(const Telemetry::Field<s16>& field) override; - void Visit(const Telemetry::Field<s32>& field) override; - void Visit(const Telemetry::Field<s64>& field) override; - void Visit(const Telemetry::Field<std::string>& field) override; - void Visit(const Telemetry::Field<const char*>& field) override; - void Visit(const Telemetry::Field<std::chrono::microseconds>& field) override; + void Visit(const Common::Telemetry::Field<bool>& field) override; + void Visit(const Common::Telemetry::Field<double>& field) override; + void Visit(const Common::Telemetry::Field<float>& field) override; + void Visit(const Common::Telemetry::Field<u8>& field) override; + void Visit(const Common::Telemetry::Field<u16>& field) override; + void Visit(const Common::Telemetry::Field<u32>& field) override; + void Visit(const Common::Telemetry::Field<u64>& field) override; + void Visit(const Common::Telemetry::Field<s8>& field) override; + void Visit(const Common::Telemetry::Field<s16>& field) override; + void Visit(const Common::Telemetry::Field<s32>& field) override; + void Visit(const Common::Telemetry::Field<s64>& field) override; + void Visit(const Common::Telemetry::Field<std::string>& field) override; + void Visit(const Common::Telemetry::Field<const char*>& field) override; + void Visit(const Common::Telemetry::Field<std::chrono::microseconds>& field) override; void Complete() override; bool SubmitTestcase() override; diff --git a/src/web_service/verify_login.cpp b/src/web_service/verify_login.cpp index bfaa5b70a..ceb55ca6b 100644 --- a/src/web_service/verify_login.cpp +++ b/src/web_service/verify_login.cpp @@ -3,9 +3,9 @@ // Refer to the license.txt file included. #include <nlohmann/json.hpp> -#include "common/web_result.h" #include "web_service/verify_login.h" #include "web_service/web_backend.h" +#include "web_service/web_result.h" namespace WebService { diff --git a/src/web_service/web_backend.cpp b/src/web_service/web_backend.cpp index 09d1651ac..74e287045 100644 --- a/src/web_service/web_backend.cpp +++ b/src/web_service/web_backend.cpp @@ -6,13 +6,14 @@ #include <cstdlib> #include <mutex> #include <string> + #include <LUrlParser.h> #include <fmt/format.h> #include <httplib.h> -#include "common/common_types.h" + #include "common/logging/log.h" -#include "common/web_result.h" #include "web_service/web_backend.h" +#include "web_service/web_result.h" namespace WebService { @@ -33,17 +34,16 @@ struct Client::Impl { } /// A generic function handles POST, GET and DELETE request together - Common::WebResult GenericRequest(const std::string& method, const std::string& path, - const std::string& data, bool allow_anonymous, - const std::string& accept) { + WebResult GenericRequest(const std::string& method, const std::string& path, + const std::string& data, bool allow_anonymous, + const std::string& accept) { if (jwt.empty()) { UpdateJWT(); } if (jwt.empty() && !allow_anonymous) { LOG_ERROR(WebService, "Credentials must be provided for authenticated requests"); - return Common::WebResult{Common::WebResult::Code::CredentialsMissing, - "Credentials needed", ""}; + return WebResult{WebResult::Code::CredentialsMissing, "Credentials needed", ""}; } auto result = GenericRequest(method, path, data, accept, jwt); @@ -62,10 +62,10 @@ struct Client::Impl { * username + token is used if jwt is empty but username and token are * not empty anonymous if all of jwt, username and token are empty */ - Common::WebResult GenericRequest(const std::string& method, const std::string& path, - const std::string& data, const std::string& accept, - const std::string& jwt = "", const std::string& username = "", - const std::string& token = "") { + WebResult GenericRequest(const std::string& method, const std::string& path, + const std::string& data, const std::string& accept, + const std::string& jwt = "", const std::string& username = "", + const std::string& token = "") { if (cli == nullptr) { auto parsedUrl = LUrlParser::clParseURL::ParseURL(host); int port; @@ -81,12 +81,12 @@ struct Client::Impl { cli = std::make_unique<httplib::SSLClient>(parsedUrl.m_Host.c_str(), port); } else { LOG_ERROR(WebService, "Bad URL scheme {}", parsedUrl.m_Scheme); - return Common::WebResult{Common::WebResult::Code::InvalidURL, "Bad URL scheme", ""}; + return WebResult{WebResult::Code::InvalidURL, "Bad URL scheme", ""}; } } if (cli == nullptr) { LOG_ERROR(WebService, "Invalid URL {}", host + path); - return Common::WebResult{Common::WebResult::Code::InvalidURL, "Invalid URL", ""}; + return WebResult{WebResult::Code::InvalidURL, "Invalid URL", ""}; } cli->set_timeout_sec(TIMEOUT_SECONDS); @@ -106,7 +106,7 @@ struct Client::Impl { std::string(API_VERSION.begin(), API_VERSION.end())); if (method != "GET") { params.emplace(std::string("Content-Type"), std::string("application/json")); - }; + } httplib::Request request; request.method = method; @@ -118,29 +118,28 @@ struct Client::Impl { if (!cli->send(request, response)) { LOG_ERROR(WebService, "{} to {} returned null", method, host + path); - return Common::WebResult{Common::WebResult::Code::LibError, "Null response", ""}; + return WebResult{WebResult::Code::LibError, "Null response", ""}; } if (response.status >= 400) { LOG_ERROR(WebService, "{} to {} returned error status code: {}", method, host + path, response.status); - return Common::WebResult{Common::WebResult::Code::HttpError, - std::to_string(response.status), ""}; + return WebResult{WebResult::Code::HttpError, std::to_string(response.status), ""}; } auto content_type = response.headers.find("content-type"); if (content_type == response.headers.end()) { LOG_ERROR(WebService, "{} to {} returned no content", method, host + path); - return Common::WebResult{Common::WebResult::Code::WrongContent, "", ""}; + return WebResult{WebResult::Code::WrongContent, "", ""}; } if (content_type->second.find(accept) == std::string::npos) { LOG_ERROR(WebService, "{} to {} returned wrong content: {}", method, host + path, content_type->second); - return Common::WebResult{Common::WebResult::Code::WrongContent, "Wrong content", ""}; + return WebResult{WebResult::Code::WrongContent, "Wrong content", ""}; } - return Common::WebResult{Common::WebResult::Code::Success, "", response.body}; + return WebResult{WebResult::Code::Success, "", response.body}; } // Retrieve a new JWT from given username and token @@ -150,7 +149,7 @@ struct Client::Impl { } auto result = GenericRequest("POST", "/jwt/internal", "", "text/html", "", username, token); - if (result.result_code != Common::WebResult::Code::Success) { + if (result.result_code != WebResult::Code::Success) { LOG_ERROR(WebService, "UpdateJWT failed"); } else { std::lock_guard lock{jwt_cache.mutex}; @@ -180,29 +179,28 @@ Client::Client(std::string host, std::string username, std::string token) Client::~Client() = default; -Common::WebResult Client::PostJson(const std::string& path, const std::string& data, - bool allow_anonymous) { +WebResult Client::PostJson(const std::string& path, const std::string& data, bool allow_anonymous) { return impl->GenericRequest("POST", path, data, allow_anonymous, "application/json"); } -Common::WebResult Client::GetJson(const std::string& path, bool allow_anonymous) { +WebResult Client::GetJson(const std::string& path, bool allow_anonymous) { return impl->GenericRequest("GET", path, "", allow_anonymous, "application/json"); } -Common::WebResult Client::DeleteJson(const std::string& path, const std::string& data, - bool allow_anonymous) { +WebResult Client::DeleteJson(const std::string& path, const std::string& data, + bool allow_anonymous) { return impl->GenericRequest("DELETE", path, data, allow_anonymous, "application/json"); } -Common::WebResult Client::GetPlain(const std::string& path, bool allow_anonymous) { +WebResult Client::GetPlain(const std::string& path, bool allow_anonymous) { return impl->GenericRequest("GET", path, "", allow_anonymous, "text/plain"); } -Common::WebResult Client::GetImage(const std::string& path, bool allow_anonymous) { +WebResult Client::GetImage(const std::string& path, bool allow_anonymous) { return impl->GenericRequest("GET", path, "", allow_anonymous, "image/png"); } -Common::WebResult Client::GetExternalJWT(const std::string& audience) { +WebResult Client::GetExternalJWT(const std::string& audience) { return impl->GenericRequest("POST", fmt::format("/jwt/external/{}", audience), "", false, "text/html"); } diff --git a/src/web_service/web_backend.h b/src/web_service/web_backend.h index 04121f17e..81f58583c 100644 --- a/src/web_service/web_backend.h +++ b/src/web_service/web_backend.h @@ -7,12 +7,10 @@ #include <memory> #include <string> -namespace Common { -struct WebResult; -} - namespace WebService { +struct WebResult; + class Client { public: Client(std::string host, std::string username, std::string token); @@ -25,8 +23,7 @@ public: * @param allow_anonymous If true, allow anonymous unauthenticated requests. * @return the result of the request. */ - Common::WebResult PostJson(const std::string& path, const std::string& data, - bool allow_anonymous); + WebResult PostJson(const std::string& path, const std::string& data, bool allow_anonymous); /** * Gets JSON from the specified path. @@ -34,7 +31,7 @@ public: * @param allow_anonymous If true, allow anonymous unauthenticated requests. * @return the result of the request. */ - Common::WebResult GetJson(const std::string& path, bool allow_anonymous); + WebResult GetJson(const std::string& path, bool allow_anonymous); /** * Deletes JSON to the specified path. @@ -43,8 +40,7 @@ public: * @param allow_anonymous If true, allow anonymous unauthenticated requests. * @return the result of the request. */ - Common::WebResult DeleteJson(const std::string& path, const std::string& data, - bool allow_anonymous); + WebResult DeleteJson(const std::string& path, const std::string& data, bool allow_anonymous); /** * Gets a plain string from the specified path. @@ -52,7 +48,7 @@ public: * @param allow_anonymous If true, allow anonymous unauthenticated requests. * @return the result of the request. */ - Common::WebResult GetPlain(const std::string& path, bool allow_anonymous); + WebResult GetPlain(const std::string& path, bool allow_anonymous); /** * Gets an PNG image from the specified path. @@ -60,14 +56,14 @@ public: * @param allow_anonymous If true, allow anonymous unauthenticated requests. * @return the result of the request. */ - Common::WebResult GetImage(const std::string& path, bool allow_anonymous); + WebResult GetImage(const std::string& path, bool allow_anonymous); /** * Requests an external JWT for the specific audience provided. * @param audience the audience of the JWT requested. * @return the result of the request. */ - Common::WebResult GetExternalJWT(const std::string& audience); + WebResult GetExternalJWT(const std::string& audience); private: struct Impl; diff --git a/src/web_service/web_result.h b/src/web_service/web_result.h new file mode 100644 index 000000000..3aeeb5288 --- /dev/null +++ b/src/web_service/web_result.h @@ -0,0 +1,25 @@ +// Copyright 2018 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include <string> +#include "common/common_types.h" + +namespace WebService { +struct WebResult { + enum class Code : u32 { + Success, + InvalidURL, + CredentialsMissing, + LibError, + HttpError, + WrongContent, + NoWebservice, + }; + Code result_code; + std::string result_string; + std::string returned_data; +}; +} // namespace WebService |