summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar FearlessTobi2024-02-04 16:51:52 +0100
committerGravatar FearlessTobi2024-02-04 16:51:52 +0100
commit9ade941de121eb5476e1a3bff4dcc4e57cd2b16d (patch)
tree67e9f811cdd49c36fd3a0badf35e236238088de4 /src
parentMerge pull request #12892 from liamwhite/serialization-stuff (diff)
downloadyuzu-9ade941de121eb5476e1a3bff4dcc4e57cd2b16d.tar.gz
yuzu-9ade941de121eb5476e1a3bff4dcc4e57cd2b16d.tar.xz
yuzu-9ade941de121eb5476e1a3bff4dcc4e57cd2b16d.zip
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.
Diffstat (limited to 'src')
-rw-r--r--src/web_service/web_backend.cpp41
1 files changed, 22 insertions, 19 deletions
diff --git a/src/web_service/web_backend.cpp b/src/web_service/web_backend.cpp
index dff380cca..f41d8af0e 100644
--- a/src/web_service/web_backend.cpp
+++ b/src/web_service/web_backend.cpp
@@ -32,9 +32,14 @@ struct Client::Impl {
32 Impl(std::string host_, std::string username_, std::string token_) 32 Impl(std::string host_, std::string username_, std::string token_)
33 : host{std::move(host_)}, username{std::move(username_)}, token{std::move(token_)} { 33 : host{std::move(host_)}, username{std::move(username_)}, token{std::move(token_)} {
34 std::scoped_lock lock{jwt_cache.mutex}; 34 std::scoped_lock lock{jwt_cache.mutex};
35 if (username == jwt_cache.username && token == jwt_cache.token) { 35 if (this->username == jwt_cache.username && this->token == jwt_cache.token) {
36 jwt = jwt_cache.jwt; 36 jwt = jwt_cache.jwt;
37 } 37 }
38
39 // Normalize host expression
40 if (!this->host.empty() && this->host.back() == '/') {
41 static_cast<void>(this->host.pop_back());
42 }
38 } 43 }
39 44
40 /// A generic function handles POST, GET and DELETE request together 45 /// A generic function handles POST, GET and DELETE request together
@@ -47,7 +52,7 @@ struct Client::Impl {
47 52
48 if (jwt.empty() && !allow_anonymous) { 53 if (jwt.empty() && !allow_anonymous) {
49 LOG_ERROR(WebService, "Credentials must be provided for authenticated requests"); 54 LOG_ERROR(WebService, "Credentials must be provided for authenticated requests");
50 return WebResult{WebResult::Code::CredentialsMissing, "Credentials needed", ""}; 55 return WebResult{WebResult::Code::CredentialsMissing, "Credentials needed"};
51 } 56 }
52 57
53 auto result = GenericRequest(method, path, data, accept, jwt); 58 auto result = GenericRequest(method, path, data, accept, jwt);
@@ -71,18 +76,16 @@ struct Client::Impl {
71 const std::string& jwt_ = "", const std::string& username_ = "", 76 const std::string& jwt_ = "", const std::string& username_ = "",
72 const std::string& token_ = "") { 77 const std::string& token_ = "") {
73 if (cli == nullptr) { 78 if (cli == nullptr) {
74 cli = std::make_unique<httplib::Client>(host); 79 cli = std::make_unique<httplib::Client>(host.c_str());
80 cli->set_connection_timeout(TIMEOUT_SECONDS);
81 cli->set_read_timeout(TIMEOUT_SECONDS);
82 cli->set_write_timeout(TIMEOUT_SECONDS);
75 } 83 }
76
77 if (!cli->is_valid()) { 84 if (!cli->is_valid()) {
78 LOG_ERROR(WebService, "Client is invalid, skipping request!"); 85 LOG_ERROR(WebService, "Invalid URL {}", host + path);
79 return {}; 86 return WebResult{WebResult::Code::InvalidURL, "Invalid URL"};
80 } 87 }
81 88
82 cli->set_connection_timeout(TIMEOUT_SECONDS);
83 cli->set_read_timeout(TIMEOUT_SECONDS);
84 cli->set_write_timeout(TIMEOUT_SECONDS);
85
86 httplib::Headers params; 89 httplib::Headers params;
87 if (!jwt_.empty()) { 90 if (!jwt_.empty()) {
88 params = { 91 params = {
@@ -107,32 +110,32 @@ struct Client::Impl {
107 request.headers = params; 110 request.headers = params;
108 request.body = data; 111 request.body = data;
109 112
110 httplib::Response response; 113 httplib::Result result = cli->send(request);
111 httplib::Error error;
112 114
113 if (!cli->send(request, response, error)) { 115 if (!result) {
114 LOG_ERROR(WebService, "{} to {} returned null (httplib Error: {})", method, host + path, 116 LOG_ERROR(WebService, "{} to {} returned null", method, host + path);
115 httplib::to_string(error)); 117 return WebResult{WebResult::Code::LibError, "Null response"};
116 return WebResult{WebResult::Code::LibError, "Null response", ""};
117 } 118 }
118 119
120 httplib::Response response = result.value();
121
119 if (response.status >= 400) { 122 if (response.status >= 400) {
120 LOG_ERROR(WebService, "{} to {} returned error status code: {}", method, host + path, 123 LOG_ERROR(WebService, "{} to {} returned error status code: {}", method, host + path,
121 response.status); 124 response.status);
122 return WebResult{WebResult::Code::HttpError, std::to_string(response.status), ""}; 125 return WebResult{WebResult::Code::HttpError, std::to_string(response.status)};
123 } 126 }
124 127
125 auto content_type = response.headers.find("content-type"); 128 auto content_type = response.headers.find("content-type");
126 129
127 if (content_type == response.headers.end()) { 130 if (content_type == response.headers.end()) {
128 LOG_ERROR(WebService, "{} to {} returned no content", method, host + path); 131 LOG_ERROR(WebService, "{} to {} returned no content", method, host + path);
129 return WebResult{WebResult::Code::WrongContent, "", ""}; 132 return WebResult{WebResult::Code::WrongContent, ""};
130 } 133 }
131 134
132 if (content_type->second.find(accept) == std::string::npos) { 135 if (content_type->second.find(accept) == std::string::npos) {
133 LOG_ERROR(WebService, "{} to {} returned wrong content: {}", method, host + path, 136 LOG_ERROR(WebService, "{} to {} returned wrong content: {}", method, host + path,
134 content_type->second); 137 content_type->second);
135 return WebResult{WebResult::Code::WrongContent, "Wrong content", ""}; 138 return WebResult{WebResult::Code::WrongContent, "Wrong content"};
136 } 139 }
137 return WebResult{WebResult::Code::Success, "", response.body}; 140 return WebResult{WebResult::Code::Success, "", response.body};
138 } 141 }