web_backend: Sync with Citra implementation
While porting https://github.com/citra-emu/citra/pull/7347, I noticed the code of yuzu was not up-to-date with the implementation from Citra.
This commit is contained in:
parent
4cccbe7989
commit
9ade941de1
1 changed files with 22 additions and 19 deletions
|
@ -32,9 +32,14 @@ struct Client::Impl {
|
||||||
Impl(std::string host_, std::string username_, std::string token_)
|
Impl(std::string host_, std::string username_, std::string token_)
|
||||||
: host{std::move(host_)}, username{std::move(username_)}, token{std::move(token_)} {
|
: host{std::move(host_)}, username{std::move(username_)}, token{std::move(token_)} {
|
||||||
std::scoped_lock lock{jwt_cache.mutex};
|
std::scoped_lock lock{jwt_cache.mutex};
|
||||||
if (username == jwt_cache.username && token == jwt_cache.token) {
|
if (this->username == jwt_cache.username && this->token == jwt_cache.token) {
|
||||||
jwt = jwt_cache.jwt;
|
jwt = jwt_cache.jwt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Normalize host expression
|
||||||
|
if (!this->host.empty() && this->host.back() == '/') {
|
||||||
|
static_cast<void>(this->host.pop_back());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A generic function handles POST, GET and DELETE request together
|
/// A generic function handles POST, GET and DELETE request together
|
||||||
|
@ -47,7 +52,7 @@ struct Client::Impl {
|
||||||
|
|
||||||
if (jwt.empty() && !allow_anonymous) {
|
if (jwt.empty() && !allow_anonymous) {
|
||||||
LOG_ERROR(WebService, "Credentials must be provided for authenticated requests");
|
LOG_ERROR(WebService, "Credentials must be provided for authenticated requests");
|
||||||
return WebResult{WebResult::Code::CredentialsMissing, "Credentials needed", ""};
|
return WebResult{WebResult::Code::CredentialsMissing, "Credentials needed"};
|
||||||
}
|
}
|
||||||
|
|
||||||
auto result = GenericRequest(method, path, data, accept, jwt);
|
auto result = GenericRequest(method, path, data, accept, jwt);
|
||||||
|
@ -71,18 +76,16 @@ struct Client::Impl {
|
||||||
const std::string& jwt_ = "", const std::string& username_ = "",
|
const std::string& jwt_ = "", const std::string& username_ = "",
|
||||||
const std::string& token_ = "") {
|
const std::string& token_ = "") {
|
||||||
if (cli == nullptr) {
|
if (cli == nullptr) {
|
||||||
cli = std::make_unique<httplib::Client>(host);
|
cli = std::make_unique<httplib::Client>(host.c_str());
|
||||||
|
cli->set_connection_timeout(TIMEOUT_SECONDS);
|
||||||
|
cli->set_read_timeout(TIMEOUT_SECONDS);
|
||||||
|
cli->set_write_timeout(TIMEOUT_SECONDS);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!cli->is_valid()) {
|
if (!cli->is_valid()) {
|
||||||
LOG_ERROR(WebService, "Client is invalid, skipping request!");
|
LOG_ERROR(WebService, "Invalid URL {}", host + path);
|
||||||
return {};
|
return WebResult{WebResult::Code::InvalidURL, "Invalid URL"};
|
||||||
}
|
}
|
||||||
|
|
||||||
cli->set_connection_timeout(TIMEOUT_SECONDS);
|
|
||||||
cli->set_read_timeout(TIMEOUT_SECONDS);
|
|
||||||
cli->set_write_timeout(TIMEOUT_SECONDS);
|
|
||||||
|
|
||||||
httplib::Headers params;
|
httplib::Headers params;
|
||||||
if (!jwt_.empty()) {
|
if (!jwt_.empty()) {
|
||||||
params = {
|
params = {
|
||||||
|
@ -107,32 +110,32 @@ struct Client::Impl {
|
||||||
request.headers = params;
|
request.headers = params;
|
||||||
request.body = data;
|
request.body = data;
|
||||||
|
|
||||||
httplib::Response response;
|
httplib::Result result = cli->send(request);
|
||||||
httplib::Error error;
|
|
||||||
|
|
||||||
if (!cli->send(request, response, error)) {
|
if (!result) {
|
||||||
LOG_ERROR(WebService, "{} to {} returned null (httplib Error: {})", method, host + path,
|
LOG_ERROR(WebService, "{} to {} returned null", method, host + path);
|
||||||
httplib::to_string(error));
|
return WebResult{WebResult::Code::LibError, "Null response"};
|
||||||
return WebResult{WebResult::Code::LibError, "Null response", ""};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
httplib::Response response = result.value();
|
||||||
|
|
||||||
if (response.status >= 400) {
|
if (response.status >= 400) {
|
||||||
LOG_ERROR(WebService, "{} to {} returned error status code: {}", method, host + path,
|
LOG_ERROR(WebService, "{} to {} returned error status code: {}", method, host + path,
|
||||||
response.status);
|
response.status);
|
||||||
return WebResult{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");
|
auto content_type = response.headers.find("content-type");
|
||||||
|
|
||||||
if (content_type == response.headers.end()) {
|
if (content_type == response.headers.end()) {
|
||||||
LOG_ERROR(WebService, "{} to {} returned no content", method, host + path);
|
LOG_ERROR(WebService, "{} to {} returned no content", method, host + path);
|
||||||
return WebResult{WebResult::Code::WrongContent, "", ""};
|
return WebResult{WebResult::Code::WrongContent, ""};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (content_type->second.find(accept) == std::string::npos) {
|
if (content_type->second.find(accept) == 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 WebResult{WebResult::Code::WrongContent, "Wrong content", ""};
|
return WebResult{WebResult::Code::WrongContent, "Wrong content"};
|
||||||
}
|
}
|
||||||
return WebResult{WebResult::Code::Success, "", response.body};
|
return WebResult{WebResult::Code::Success, "", response.body};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue