web_backend.cpp 5.2 KB

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