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