summaryrefslogtreecommitdiff
path: root/src/web_service/web_backend.cpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/web_service/web_backend.cpp149
1 files changed, 149 insertions, 0 deletions
diff --git a/src/web_service/web_backend.cpp b/src/web_service/web_backend.cpp
new file mode 100644
index 000000000..787b0fbcb
--- /dev/null
+++ b/src/web_service/web_backend.cpp
@@ -0,0 +1,149 @@
1// Copyright 2017 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <cstdlib>
6#include <string>
7#include <thread>
8#include <LUrlParser.h>
9#include "common/logging/log.h"
10#include "common/web_result.h"
11#include "core/settings.h"
12#include "web_service/web_backend.h"
13
14namespace WebService {
15
16constexpr std::array<const char, 1> API_VERSION{'1'};
17
18constexpr u32 HTTP_PORT = 80;
19constexpr u32 HTTPS_PORT = 443;
20
21constexpr u32 TIMEOUT_SECONDS = 30;
22
23Client::JWTCache Client::jwt_cache{};
24
25Client::Client(const std::string& host, const std::string& username, const std::string& token)
26 : host(host), username(username), token(token) {
27 std::lock_guard<std::mutex> lock(jwt_cache.mutex);
28 if (username == jwt_cache.username && token == jwt_cache.token) {
29 jwt = jwt_cache.jwt;
30 }
31}
32
33Common::WebResult Client::GenericJson(const std::string& method, const std::string& path,
34 const std::string& data, const std::string& jwt,
35 const std::string& username, const std::string& token) {
36 if (cli == nullptr) {
37 auto parsedUrl = LUrlParser::clParseURL::ParseURL(host);
38 int port;
39 if (parsedUrl.m_Scheme == "http") {
40 if (!parsedUrl.GetPort(&port)) {
41 port = HTTP_PORT;
42 }
43 cli =
44 std::make_unique<httplib::Client>(parsedUrl.m_Host.c_str(), port, TIMEOUT_SECONDS);
45 } else if (parsedUrl.m_Scheme == "https") {
46 if (!parsedUrl.GetPort(&port)) {
47 port = HTTPS_PORT;
48 }
49 cli = std::make_unique<httplib::SSLClient>(parsedUrl.m_Host.c_str(), port,
50 TIMEOUT_SECONDS);
51 } else {
52 LOG_ERROR(WebService, "Bad URL scheme {}", parsedUrl.m_Scheme);
53 return Common::WebResult{Common::WebResult::Code::InvalidURL, "Bad URL scheme"};
54 }
55 }
56 if (cli == nullptr) {
57 LOG_ERROR(WebService, "Invalid URL {}", host + path);
58 return Common::WebResult{Common::WebResult::Code::InvalidURL, "Invalid URL"};
59 }
60
61 httplib::Headers params;
62 if (!jwt.empty()) {
63 params = {
64 {std::string("Authorization"), fmt::format("Bearer {}", jwt)},
65 };
66 } else if (!username.empty()) {
67 params = {
68 {std::string("x-username"), username},
69 {std::string("x-token"), token},
70 };
71 }
72
73 params.emplace(std::string("api-version"), std::string(API_VERSION.begin(), API_VERSION.end()));
74 if (method != "GET") {
75 params.emplace(std::string("Content-Type"), std::string("application/json"));
76 };
77
78 httplib::Request request;
79 request.method = method;
80 request.path = path;
81 request.headers = params;
82 request.body = data;
83
84 httplib::Response response;
85
86 if (!cli->send(request, response)) {
87 LOG_ERROR(WebService, "{} to {} returned null", method, host + path);
88 return Common::WebResult{Common::WebResult::Code::LibError, "Null response"};
89 }
90
91 if (response.status >= 400) {
92 LOG_ERROR(WebService, "{} to {} returned error status code: {}", method, host + path,
93 response.status);
94 return Common::WebResult{Common::WebResult::Code::HttpError,
95 std::to_string(response.status)};
96 }
97
98 auto content_type = response.headers.find("content-type");
99
100 if (content_type == response.headers.end()) {
101 LOG_ERROR(WebService, "{} to {} returned no content", method, host + path);
102 return Common::WebResult{Common::WebResult::Code::WrongContent, ""};
103 }
104
105 if (content_type->second.find("application/json") == std::string::npos &&
106 content_type->second.find("text/html; charset=utf-8") == std::string::npos) {
107 LOG_ERROR(WebService, "{} to {} returned wrong content: {}", method, host + path,
108 content_type->second);
109 return Common::WebResult{Common::WebResult::Code::WrongContent, "Wrong content"};
110 }
111 return Common::WebResult{Common::WebResult::Code::Success, "", response.body};
112}
113
114void Client::UpdateJWT() {
115 if (!username.empty() && !token.empty()) {
116 auto result = GenericJson("POST", "/jwt/internal", "", "", username, token);
117 if (result.result_code != Common::WebResult::Code::Success) {
118 LOG_ERROR(WebService, "UpdateJWT failed");
119 } else {
120 std::lock_guard<std::mutex> lock(jwt_cache.mutex);
121 jwt_cache.username = username;
122 jwt_cache.token = token;
123 jwt_cache.jwt = jwt = result.returned_data;
124 }
125 }
126}
127
128Common::WebResult Client::GenericJson(const std::string& method, const std::string& path,
129 const std::string& data, bool allow_anonymous) {
130 if (jwt.empty()) {
131 UpdateJWT();
132 }
133
134 if (jwt.empty() && !allow_anonymous) {
135 LOG_ERROR(WebService, "Credentials must be provided for authenticated requests");
136 return Common::WebResult{Common::WebResult::Code::CredentialsMissing, "Credentials needed"};
137 }
138
139 auto result = GenericJson(method, path, data, jwt);
140 if (result.result_string == "401") {
141 // Try again with new JWT
142 UpdateJWT();
143 result = GenericJson(method, path, data, jwt);
144 }
145
146 return result;
147}
148
149} // namespace WebService