diff options
Diffstat (limited to 'src/web_service/web_backend.cpp')
| -rw-r--r-- | src/web_service/web_backend.cpp | 140 |
1 files changed, 0 insertions, 140 deletions
diff --git a/src/web_service/web_backend.cpp b/src/web_service/web_backend.cpp deleted file mode 100644 index b17d82f9c..000000000 --- a/src/web_service/web_backend.cpp +++ /dev/null | |||
| @@ -1,140 +0,0 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #ifdef _WIN32 | ||
| 6 | #include <winsock.h> | ||
| 7 | #endif | ||
| 8 | |||
| 9 | #include <cstdlib> | ||
| 10 | #include <thread> | ||
| 11 | #include <cpr/cpr.h> | ||
| 12 | #include "common/logging/log.h" | ||
| 13 | #include "web_service/web_backend.h" | ||
| 14 | |||
| 15 | namespace WebService { | ||
| 16 | |||
| 17 | static constexpr char API_VERSION[]{"1"}; | ||
| 18 | |||
| 19 | static std::unique_ptr<cpr::Session> g_session; | ||
| 20 | |||
| 21 | void Win32WSAStartup() { | ||
| 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); | ||
| 30 | } | ||
| 31 | #endif | ||
| 32 | } | ||
| 33 | |||
| 34 | void PostJson(const std::string& url, const std::string& data, bool allow_anonymous, | ||
| 35 | const std::string& username, const std::string& token) { | ||
| 36 | if (url.empty()) { | ||
| 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 | } | ||
| 46 | |||
| 47 | Win32WSAStartup(); | ||
| 48 | |||
| 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); | ||
| 82 | } | ||
| 83 | |||
| 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) { | ||
| 88 | if (url.empty()) { | ||
| 89 | LOG_ERROR(WebService, "URL is invalid"); | ||
| 90 | return std::async(std::launch::async, [func{std::move(func)}]() { return func(""); }); | ||
| 91 | } | ||
| 92 | |||
| 93 | const bool are_credentials_provided{!token.empty() && !username.empty()}; | ||
| 94 | if (!allow_anonymous && !are_credentials_provided) { | ||
| 95 | LOG_ERROR(WebService, "Credentials must be provided for authenticated requests"); | ||
| 96 | return std::async(std::launch::async, [func{std::move(func)}]() { return func(""); }); | ||
| 97 | } | ||
| 98 | |||
| 99 | Win32WSAStartup(); | ||
| 100 | |||
| 101 | // Built request header | ||
| 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); | ||
| 134 | } | ||
| 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 | |||
| 140 | } // namespace WebService | ||