mirror of
https://github.com/yuzu-emu/yuzu.git
synced 2024-09-15 00:55:33 +00:00
web-service: Port citra's updated web_backend code.
This commit is contained in:
parent
883eb1a1a1
commit
8dc9f35baf
@ -2,10 +2,12 @@
|
|||||||
// Licensed under GPLv2 or any later version
|
// Licensed under GPLv2 or any later version
|
||||||
// Refer to the license.txt file included.
|
// Refer to the license.txt file included.
|
||||||
|
|
||||||
|
#include <array>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <LUrlParser.h>
|
#include <LUrlParser.h>
|
||||||
|
#include <fmt/format.h>
|
||||||
#include <httplib.h>
|
#include <httplib.h>
|
||||||
#include "common/common_types.h"
|
#include "common/common_types.h"
|
||||||
#include "common/logging/log.h"
|
#include "common/logging/log.h"
|
||||||
@ -16,10 +18,10 @@ namespace WebService {
|
|||||||
|
|
||||||
constexpr std::array<const char, 1> API_VERSION{'1'};
|
constexpr std::array<const char, 1> API_VERSION{'1'};
|
||||||
|
|
||||||
constexpr u32 HTTP_PORT = 80;
|
constexpr int HTTP_PORT = 80;
|
||||||
constexpr u32 HTTPS_PORT = 443;
|
constexpr int HTTPS_PORT = 443;
|
||||||
|
|
||||||
constexpr u32 TIMEOUT_SECONDS = 30;
|
constexpr std::size_t TIMEOUT_SECONDS = 30;
|
||||||
|
|
||||||
struct Client::Impl {
|
struct Client::Impl {
|
||||||
Impl(std::string host, std::string username, std::string token)
|
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
|
/// A generic function handles POST, GET and DELETE request together
|
||||||
Common::WebResult GenericJson(const std::string& method, const std::string& path,
|
Common::WebResult GenericRequest(const std::string& method, const std::string& path,
|
||||||
const std::string& data, bool allow_anonymous) {
|
const std::string& data, bool allow_anonymous,
|
||||||
|
const std::string& accept) {
|
||||||
if (jwt.empty()) {
|
if (jwt.empty()) {
|
||||||
UpdateJWT();
|
UpdateJWT();
|
||||||
}
|
}
|
||||||
@ -43,11 +46,11 @@ struct Client::Impl {
|
|||||||
"Credentials needed"};
|
"Credentials needed"};
|
||||||
}
|
}
|
||||||
|
|
||||||
auto result = GenericJson(method, path, data, jwt);
|
auto result = GenericRequest(method, path, data, accept, jwt);
|
||||||
if (result.result_string == "401") {
|
if (result.result_string == "401") {
|
||||||
// Try again with new JWT
|
// Try again with new JWT
|
||||||
UpdateJWT();
|
UpdateJWT();
|
||||||
result = GenericJson(method, path, data, jwt);
|
result = GenericRequest(method, path, data, accept, jwt);
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
@ -56,12 +59,13 @@ struct Client::Impl {
|
|||||||
/**
|
/**
|
||||||
* A generic function with explicit authentication method specified
|
* A generic function with explicit authentication method specified
|
||||||
* JWT is used if the jwt parameter is not empty
|
* 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
|
* username + token is used if jwt is empty but username and token are
|
||||||
* anonymous if all of jwt, username and token are empty
|
* not empty anonymous if all of jwt, username and token are empty
|
||||||
*/
|
*/
|
||||||
Common::WebResult GenericJson(const std::string& method, const std::string& path,
|
Common::WebResult GenericRequest(const std::string& method, const std::string& path,
|
||||||
const std::string& data, const std::string& jwt = "",
|
const std::string& data, const std::string& accept,
|
||||||
const std::string& username = "", const std::string& token = "") {
|
const std::string& jwt = "", const std::string& username = "",
|
||||||
|
const std::string& token = "") {
|
||||||
if (cli == nullptr) {
|
if (cli == nullptr) {
|
||||||
auto parsedUrl = LUrlParser::clParseURL::ParseURL(host);
|
auto parsedUrl = LUrlParser::clParseURL::ParseURL(host);
|
||||||
int port;
|
int port;
|
||||||
@ -132,8 +136,7 @@ struct Client::Impl {
|
|||||||
return Common::WebResult{Common::WebResult::Code::WrongContent, ""};
|
return Common::WebResult{Common::WebResult::Code::WrongContent, ""};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (content_type->second.find("application/json") == std::string::npos &&
|
if (content_type->second.find(accept) == std::string::npos) {
|
||||||
content_type->second.find("text/html; charset=utf-8") == std::string::npos) {
|
|
||||||
LOG_ERROR(WebService, "{} to {} returned wrong content: {}", method, host + path,
|
LOG_ERROR(WebService, "{} to {} returned wrong content: {}", method, host + path,
|
||||||
content_type->second);
|
content_type->second);
|
||||||
return Common::WebResult{Common::WebResult::Code::WrongContent, "Wrong content"};
|
return Common::WebResult{Common::WebResult::Code::WrongContent, "Wrong content"};
|
||||||
@ -147,7 +150,7 @@ struct Client::Impl {
|
|||||||
return;
|
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) {
|
if (result.result_code != Common::WebResult::Code::Success) {
|
||||||
LOG_ERROR(WebService, "UpdateJWT failed");
|
LOG_ERROR(WebService, "UpdateJWT failed");
|
||||||
} else {
|
} else {
|
||||||
@ -180,16 +183,29 @@ Client::~Client() = default;
|
|||||||
|
|
||||||
Common::WebResult Client::PostJson(const std::string& path, const std::string& data,
|
Common::WebResult Client::PostJson(const std::string& path, const std::string& data,
|
||||||
bool allow_anonymous) {
|
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) {
|
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,
|
Common::WebResult Client::DeleteJson(const std::string& path, const std::string& data,
|
||||||
bool allow_anonymous) {
|
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
|
} // namespace WebService
|
||||||
|
@ -46,6 +46,29 @@ public:
|
|||||||
Common::WebResult DeleteJson(const std::string& path, const std::string& data,
|
Common::WebResult DeleteJson(const std::string& path, const std::string& data,
|
||||||
bool allow_anonymous);
|
bool allow_anonymous);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a plain string from the specified path.
|
||||||
|
* @param path the URL segment after the host address.
|
||||||
|
* @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);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets an PNG image from the specified path.
|
||||||
|
* @param path the URL segment after the host address.
|
||||||
|
* @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);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct Impl;
|
struct Impl;
|
||||||
std::unique_ptr<Impl> impl;
|
std::unique_ptr<Impl> impl;
|
||||||
|
Loading…
Reference in New Issue
Block a user