diff options
Diffstat (limited to 'src/web_service/web_backend.cpp')
| -rw-r--r-- | src/web_service/web_backend.cpp | 138 |
1 files changed, 113 insertions, 25 deletions
diff --git a/src/web_service/web_backend.cpp b/src/web_service/web_backend.cpp index 13e4555ac..b17d82f9c 100644 --- a/src/web_service/web_backend.cpp +++ b/src/web_service/web_backend.cpp | |||
| @@ -2,51 +2,139 @@ | |||
| 2 | // Licensed under GPLv2 or any later version | 2 | // Licensed under GPLv2 or any later version |
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | #ifdef _WIN32 | ||
| 6 | #include <winsock.h> | ||
| 7 | #endif | ||
| 8 | |||
| 9 | #include <cstdlib> | ||
| 10 | #include <thread> | ||
| 5 | #include <cpr/cpr.h> | 11 | #include <cpr/cpr.h> |
| 6 | #include <stdlib.h> | ||
| 7 | #include "common/logging/log.h" | 12 | #include "common/logging/log.h" |
| 8 | #include "web_service/web_backend.h" | 13 | #include "web_service/web_backend.h" |
| 9 | 14 | ||
| 10 | namespace WebService { | 15 | namespace WebService { |
| 11 | 16 | ||
| 12 | static constexpr char API_VERSION[]{"1"}; | 17 | static constexpr char API_VERSION[]{"1"}; |
| 13 | static constexpr char ENV_VAR_USERNAME[]{"CITRA_WEB_SERVICES_USERNAME"}; | ||
| 14 | static constexpr char ENV_VAR_TOKEN[]{"CITRA_WEB_SERVICES_TOKEN"}; | ||
| 15 | 18 | ||
| 16 | static std::string GetEnvironmentVariable(const char* name) { | 19 | static std::unique_ptr<cpr::Session> g_session; |
| 17 | const char* value{getenv(name)}; | 20 | |
| 18 | if (value) { | 21 | void Win32WSAStartup() { |
| 19 | return value; | 22 | #ifdef _WIN32 |
| 23 | // On Windows, CPR/libcurl does not properly initialize Winsock. The below code is used to | ||
| 24 | // initialize Winsock globally, which fixes this problem. Without this, only the first CPR | ||
| 25 | // session will properly be created, and subsequent ones will fail. | ||
| 26 | WSADATA wsa_data; | ||
| 27 | const int wsa_result{WSAStartup(MAKEWORD(2, 2), &wsa_data)}; | ||
| 28 | if (wsa_result) { | ||
| 29 | LOG_CRITICAL(WebService, "WSAStartup failed: %d", wsa_result); | ||
| 20 | } | 30 | } |
| 21 | return {}; | 31 | #endif |
| 22 | } | 32 | } |
| 23 | 33 | ||
| 24 | const std::string& GetUsername() { | 34 | void PostJson(const std::string& url, const std::string& data, bool allow_anonymous, |
| 25 | static const std::string username{GetEnvironmentVariable(ENV_VAR_USERNAME)}; | 35 | const std::string& username, const std::string& token) { |
| 26 | return username; | 36 | if (url.empty()) { |
| 27 | } | 37 | LOG_ERROR(WebService, "URL is invalid"); |
| 38 | return; | ||
| 39 | } | ||
| 40 | |||
| 41 | const bool are_credentials_provided{!token.empty() && !username.empty()}; | ||
| 42 | if (!allow_anonymous && !are_credentials_provided) { | ||
| 43 | LOG_ERROR(WebService, "Credentials must be provided for authenticated requests"); | ||
| 44 | return; | ||
| 45 | } | ||
| 28 | 46 | ||
| 29 | const std::string& GetToken() { | 47 | Win32WSAStartup(); |
| 30 | static const std::string token{GetEnvironmentVariable(ENV_VAR_TOKEN)}; | 48 | |
| 31 | return token; | 49 | // Built request header |
| 50 | cpr::Header header; | ||
| 51 | if (are_credentials_provided) { | ||
| 52 | // Authenticated request if credentials are provided | ||
| 53 | header = {{"Content-Type", "application/json"}, | ||
| 54 | {"x-username", username.c_str()}, | ||
| 55 | {"x-token", token.c_str()}, | ||
| 56 | {"api-version", API_VERSION}}; | ||
| 57 | } else { | ||
| 58 | // Otherwise, anonymous request | ||
| 59 | header = cpr::Header{{"Content-Type", "application/json"}, {"api-version", API_VERSION}}; | ||
| 60 | } | ||
| 61 | |||
| 62 | // Post JSON asynchronously | ||
| 63 | static std::future<void> future; | ||
| 64 | future = cpr::PostCallback( | ||
| 65 | [](cpr::Response r) { | ||
| 66 | if (r.error) { | ||
| 67 | LOG_ERROR(WebService, "POST returned cpr error: %u:%s", | ||
| 68 | static_cast<u32>(r.error.code), r.error.message.c_str()); | ||
| 69 | return; | ||
| 70 | } | ||
| 71 | if (r.status_code >= 400) { | ||
| 72 | LOG_ERROR(WebService, "POST returned error status code: %u", r.status_code); | ||
| 73 | return; | ||
| 74 | } | ||
| 75 | if (r.header["content-type"].find("application/json") == std::string::npos) { | ||
| 76 | LOG_ERROR(WebService, "POST returned wrong content: %s", | ||
| 77 | r.header["content-type"].c_str()); | ||
| 78 | return; | ||
| 79 | } | ||
| 80 | }, | ||
| 81 | cpr::Url{url}, cpr::Body{data}, header); | ||
| 32 | } | 82 | } |
| 33 | 83 | ||
| 34 | void PostJson(const std::string& url, const std::string& data) { | 84 | template <typename T> |
| 85 | std::future<T> GetJson(std::function<T(const std::string&)> func, const std::string& url, | ||
| 86 | bool allow_anonymous, const std::string& username, | ||
| 87 | const std::string& token) { | ||
| 35 | if (url.empty()) { | 88 | if (url.empty()) { |
| 36 | LOG_ERROR(WebService, "URL is invalid"); | 89 | LOG_ERROR(WebService, "URL is invalid"); |
| 37 | return; | 90 | return std::async(std::launch::async, [func{std::move(func)}]() { return func(""); }); |
| 38 | } | 91 | } |
| 39 | 92 | ||
| 40 | if (GetUsername().empty() || GetToken().empty()) { | 93 | const bool are_credentials_provided{!token.empty() && !username.empty()}; |
| 41 | LOG_ERROR(WebService, "Environment variables %s and %s must be set to POST JSON", | 94 | if (!allow_anonymous && !are_credentials_provided) { |
| 42 | ENV_VAR_USERNAME, ENV_VAR_TOKEN); | 95 | LOG_ERROR(WebService, "Credentials must be provided for authenticated requests"); |
| 43 | return; | 96 | return std::async(std::launch::async, [func{std::move(func)}]() { return func(""); }); |
| 44 | } | 97 | } |
| 45 | 98 | ||
| 46 | cpr::PostAsync(cpr::Url{url}, cpr::Body{data}, cpr::Header{{"Content-Type", "application/json"}, | 99 | Win32WSAStartup(); |
| 47 | {"x-username", GetUsername()}, | 100 | |
| 48 | {"x-token", GetToken()}, | 101 | // Built request header |
| 49 | {"api-version", API_VERSION}}); | 102 | cpr::Header header; |
| 103 | if (are_credentials_provided) { | ||
| 104 | // Authenticated request if credentials are provided | ||
| 105 | header = {{"Content-Type", "application/json"}, | ||
| 106 | {"x-username", username.c_str()}, | ||
| 107 | {"x-token", token.c_str()}, | ||
| 108 | {"api-version", API_VERSION}}; | ||
| 109 | } else { | ||
| 110 | // Otherwise, anonymous request | ||
| 111 | header = cpr::Header{{"Content-Type", "application/json"}, {"api-version", API_VERSION}}; | ||
| 112 | } | ||
| 113 | |||
| 114 | // Get JSON asynchronously | ||
| 115 | return cpr::GetCallback( | ||
| 116 | [func{std::move(func)}](cpr::Response r) { | ||
| 117 | if (r.error) { | ||
| 118 | LOG_ERROR(WebService, "GET returned cpr error: %u:%s", | ||
| 119 | static_cast<u32>(r.error.code), r.error.message.c_str()); | ||
| 120 | return func(""); | ||
| 121 | } | ||
| 122 | if (r.status_code >= 400) { | ||
| 123 | LOG_ERROR(WebService, "GET returned error code: %u", r.status_code); | ||
| 124 | return func(""); | ||
| 125 | } | ||
| 126 | if (r.header["content-type"].find("application/json") == std::string::npos) { | ||
| 127 | LOG_ERROR(WebService, "GET returned wrong content: %s", | ||
| 128 | r.header["content-type"].c_str()); | ||
| 129 | return func(""); | ||
| 130 | } | ||
| 131 | return func(r.text); | ||
| 132 | }, | ||
| 133 | cpr::Url{url}, header); | ||
| 50 | } | 134 | } |
| 51 | 135 | ||
| 136 | template std::future<bool> GetJson(std::function<bool(const std::string&)> func, | ||
| 137 | const std::string& url, bool allow_anonymous, | ||
| 138 | const std::string& username, const std::string& token); | ||
| 139 | |||
| 52 | } // namespace WebService | 140 | } // namespace WebService |