diff options
author | bunnei <bunneidev@gmail.com> | 2019-11-09 20:50:31 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-11-09 20:50:31 +0100 |
commit | 8714d40a776f655c644392bc2454ace98064144e (patch) | |
tree | d272c2bb1d28ea072d40a9c89b7b510b56229525 /src/web_service/web_backend.cpp | |
parent | Merge pull request #3082 from ReinUsesLisp/fix-lockers (diff) | |
parent | web-service: Port citra's updated web_backend code. (diff) | |
download | yuzu-8714d40a776f655c644392bc2454ace98064144e.tar yuzu-8714d40a776f655c644392bc2454ace98064144e.tar.gz yuzu-8714d40a776f655c644392bc2454ace98064144e.tar.bz2 yuzu-8714d40a776f655c644392bc2454ace98064144e.tar.lz yuzu-8714d40a776f655c644392bc2454ace98064144e.tar.xz yuzu-8714d40a776f655c644392bc2454ace98064144e.tar.zst yuzu-8714d40a776f655c644392bc2454ace98064144e.zip |
Diffstat (limited to 'src/web_service/web_backend.cpp')
-rw-r--r-- | src/web_service/web_backend.cpp | 52 |
1 files changed, 34 insertions, 18 deletions
diff --git a/src/web_service/web_backend.cpp b/src/web_service/web_backend.cpp index dc149d2ed..6683f459f 100644 --- a/src/web_service/web_backend.cpp +++ b/src/web_service/web_backend.cpp @@ -2,10 +2,12 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include <array> #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" @@ -16,10 +18,10 @@ namespace WebService { constexpr std::array<const char, 1> API_VERSION{'1'}; -constexpr u32 HTTP_PORT = 80; -constexpr u32 HTTPS_PORT = 443; +constexpr int HTTP_PORT = 80; +constexpr int HTTPS_PORT = 443; -constexpr u32 TIMEOUT_SECONDS = 30; +constexpr std::size_t TIMEOUT_SECONDS = 30; struct Client::Impl { Impl(std::string host, std::string username, std::string token) @@ -31,8 +33,9 @@ struct Client::Impl { } /// A generic function handles POST, GET and DELETE request together - Common::WebResult GenericJson(const std::string& method, const std::string& path, - const std::string& data, bool allow_anonymous) { + Common::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(); } @@ -43,11 +46,11 @@ struct Client::Impl { "Credentials needed"}; } - auto result = GenericJson(method, path, data, jwt); + auto result = GenericRequest(method, path, data, accept, jwt); if (result.result_string == "401") { // Try again with new JWT UpdateJWT(); - result = GenericJson(method, path, data, jwt); + result = GenericRequest(method, path, data, accept, jwt); } return result; @@ -56,12 +59,13 @@ struct Client::Impl { /** * A generic function with explicit authentication method specified * JWT is used if the jwt parameter is not empty - * 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 + * 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 GenericJson(const std::string& method, const std::string& path, - const std::string& data, const std::string& jwt = "", - const std::string& username = "", const std::string& token = "") { + 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 = "") { if (cli == nullptr) { auto parsedUrl = LUrlParser::clParseURL::ParseURL(host); int port; @@ -132,8 +136,7 @@ struct Client::Impl { return Common::WebResult{Common::WebResult::Code::WrongContent, ""}; } - if (content_type->second.find("application/json") == std::string::npos && - content_type->second.find("text/html; charset=utf-8") == std::string::npos) { + 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"}; @@ -147,7 +150,7 @@ struct Client::Impl { return; } - auto result = GenericJson("POST", "/jwt/internal", "", "", username, token); + auto result = GenericRequest("POST", "/jwt/internal", "", "text/html", "", username, token); if (result.result_code != Common::WebResult::Code::Success) { LOG_ERROR(WebService, "UpdateJWT failed"); } else { @@ -180,16 +183,29 @@ Client::~Client() = default; Common::WebResult Client::PostJson(const std::string& path, const std::string& data, bool allow_anonymous) { - return impl->GenericJson("POST", path, data, allow_anonymous); + return impl->GenericRequest("POST", path, data, allow_anonymous, "application/json"); } Common::WebResult Client::GetJson(const std::string& path, bool allow_anonymous) { - return impl->GenericJson("GET", path, "", 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) { - return impl->GenericJson("DELETE", path, data, allow_anonymous); + return impl->GenericRequest("DELETE", path, data, allow_anonymous, "application/json"); +} + +Common::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) { + return impl->GenericRequest("GET", path, "", allow_anonymous, "image/png"); +} + +Common::WebResult Client::GetExternalJWT(const std::string& audience) { + return impl->GenericRequest("POST", fmt::format("/jwt/external/{}", audience), "", false, + "text/html"); } } // namespace WebService |