diff options
| author | 2017-08-26 20:15:15 -0400 | |
|---|---|---|
| committer | 2017-08-26 20:15:15 -0400 | |
| commit | 22fc378fe9f3314b08d81ffaaf57fd8688e9e3cc (patch) | |
| tree | d263eaca71ee08c3f9b81441b42bcd0e35977510 /src/web_service/web_backend.cpp | |
| parent | SidebySide Layout (#2859) (diff) | |
| parent | web_backend: Fix CPR bug where Winsock is not properly initializing. (diff) | |
| download | yuzu-22fc378fe9f3314b08d81ffaaf57fd8688e9e3cc.tar.gz yuzu-22fc378fe9f3314b08d81ffaaf57fd8688e9e3cc.tar.xz yuzu-22fc378fe9f3314b08d81ffaaf57fd8688e9e3cc.zip | |
Merge pull request #2897 from bunnei/telemetry-ui
Telemetry UI and final touches
Diffstat (limited to 'src/web_service/web_backend.cpp')
| -rw-r--r-- | src/web_service/web_backend.cpp | 67 |
1 files changed, 39 insertions, 28 deletions
diff --git a/src/web_service/web_backend.cpp b/src/web_service/web_backend.cpp index 13e4555ac..d28a3f757 100644 --- a/src/web_service/web_backend.cpp +++ b/src/web_service/web_backend.cpp | |||
| @@ -2,51 +2,62 @@ | |||
| 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 | |||
| 16 | static std::string GetEnvironmentVariable(const char* name) { | ||
| 17 | const char* value{getenv(name)}; | ||
| 18 | if (value) { | ||
| 19 | return value; | ||
| 20 | } | ||
| 21 | return {}; | ||
| 22 | } | ||
| 23 | |||
| 24 | const std::string& GetUsername() { | ||
| 25 | static const std::string username{GetEnvironmentVariable(ENV_VAR_USERNAME)}; | ||
| 26 | return username; | ||
| 27 | } | ||
| 28 | 18 | ||
| 29 | const std::string& GetToken() { | 19 | static std::unique_ptr<cpr::Session> g_session; |
| 30 | static const std::string token{GetEnvironmentVariable(ENV_VAR_TOKEN)}; | ||
| 31 | return token; | ||
| 32 | } | ||
| 33 | 20 | ||
| 34 | void PostJson(const std::string& url, const std::string& data) { | 21 | void PostJson(const std::string& url, const std::string& data, bool allow_anonymous, |
| 22 | const std::string& username, const std::string& token) { | ||
| 35 | if (url.empty()) { | 23 | if (url.empty()) { |
| 36 | LOG_ERROR(WebService, "URL is invalid"); | 24 | LOG_ERROR(WebService, "URL is invalid"); |
| 37 | return; | 25 | return; |
| 38 | } | 26 | } |
| 39 | 27 | ||
| 40 | if (GetUsername().empty() || GetToken().empty()) { | 28 | const bool are_credentials_provided{!token.empty() && !username.empty()}; |
| 41 | LOG_ERROR(WebService, "Environment variables %s and %s must be set to POST JSON", | 29 | if (!allow_anonymous && !are_credentials_provided) { |
| 42 | ENV_VAR_USERNAME, ENV_VAR_TOKEN); | 30 | LOG_ERROR(WebService, "Credentials must be provided for authenticated requests"); |
| 43 | return; | 31 | return; |
| 44 | } | 32 | } |
| 45 | 33 | ||
| 46 | cpr::PostAsync(cpr::Url{url}, cpr::Body{data}, cpr::Header{{"Content-Type", "application/json"}, | 34 | #ifdef _WIN32 |
| 47 | {"x-username", GetUsername()}, | 35 | // On Windows, CPR/libcurl does not properly initialize Winsock. The below code is used to |
| 48 | {"x-token", GetToken()}, | 36 | // initialize Winsock globally, which fixes this problem. Without this, only the first CPR |
| 49 | {"api-version", API_VERSION}}); | 37 | // session will properly be created, and subsequent ones will fail. |
| 38 | WSADATA wsa_data; | ||
| 39 | const int wsa_result{WSAStartup(MAKEWORD(2, 2), &wsa_data)}; | ||
| 40 | if (wsa_result) { | ||
| 41 | LOG_CRITICAL(WebService, "WSAStartup failed: %d", wsa_result); | ||
| 42 | } | ||
| 43 | #endif | ||
| 44 | |||
| 45 | // Built request header | ||
| 46 | cpr::Header header; | ||
| 47 | if (are_credentials_provided) { | ||
| 48 | // Authenticated request if credentials are provided | ||
| 49 | header = {{"Content-Type", "application/json"}, | ||
| 50 | {"x-username", username.c_str()}, | ||
| 51 | {"x-token", token.c_str()}, | ||
| 52 | {"api-version", API_VERSION}}; | ||
| 53 | } else { | ||
| 54 | // Otherwise, anonymous request | ||
| 55 | header = cpr::Header{{"Content-Type", "application/json"}, {"api-version", API_VERSION}}; | ||
| 56 | } | ||
| 57 | |||
| 58 | // Post JSON asynchronously | ||
| 59 | static cpr::AsyncResponse future; | ||
| 60 | future = cpr::PostAsync(cpr::Url{url.c_str()}, cpr::Body{data.c_str()}, header); | ||
| 50 | } | 61 | } |
| 51 | 62 | ||
| 52 | } // namespace WebService | 63 | } // namespace WebService |