summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorbunnei <bunneidev@gmail.com>2017-08-27 01:02:03 +0200
committerbunnei <bunneidev@gmail.com>2017-08-27 01:45:41 +0200
commit7698567fc9b9d0b009264d5d8ab5babc3ea197d8 (patch)
treed263eaca71ee08c3f9b81441b42bcd0e35977510 /src
parentweb_backend: Fix asynchronous JSON post by spawning new thread. (diff)
downloadyuzu-7698567fc9b9d0b009264d5d8ab5babc3ea197d8.tar
yuzu-7698567fc9b9d0b009264d5d8ab5babc3ea197d8.tar.gz
yuzu-7698567fc9b9d0b009264d5d8ab5babc3ea197d8.tar.bz2
yuzu-7698567fc9b9d0b009264d5d8ab5babc3ea197d8.tar.lz
yuzu-7698567fc9b9d0b009264d5d8ab5babc3ea197d8.tar.xz
yuzu-7698567fc9b9d0b009264d5d8ab5babc3ea197d8.tar.zst
yuzu-7698567fc9b9d0b009264d5d8ab5babc3ea197d8.zip
Diffstat (limited to 'src')
-rw-r--r--src/web_service/web_backend.cpp42
1 files changed, 27 insertions, 15 deletions
diff --git a/src/web_service/web_backend.cpp b/src/web_service/web_backend.cpp
index a6070fc0f..d28a3f757 100644
--- a/src/web_service/web_backend.cpp
+++ b/src/web_service/web_backend.cpp
@@ -2,6 +2,10 @@
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
+#ifdef _WIN32
+#include <winsock.h>
+#endif
+
#include <cstdlib>
#include <thread>
#include <cpr/cpr.h>
@@ -12,18 +16,7 @@ namespace WebService {
static constexpr char API_VERSION[]{"1"};
-static void PostJsonAuthenticated(const std::string& url, const std::string& data,
- const std::string& username, const std::string& token) {
- cpr::Post(cpr::Url{url}, cpr::Body{data}, cpr::Header{{"Content-Type", "application/json"},
- {"x-username", username},
- {"x-token", token},
- {"api-version", API_VERSION}});
-}
-
-static void PostJsonAnonymous(const std::string& url, const std::string& data) {
- cpr::Post(cpr::Url{url}, cpr::Body{data},
- cpr::Header{{"Content-Type", "application/json"}, {"api-version", API_VERSION}});
-}
+static std::unique_ptr<cpr::Session> g_session;
void PostJson(const std::string& url, const std::string& data, bool allow_anonymous,
const std::string& username, const std::string& token) {
@@ -38,14 +31,33 @@ void PostJson(const std::string& url, const std::string& data, bool allow_anonym
return;
}
- // Post JSON asynchronously by spawning a new thread
+#ifdef _WIN32
+ // On Windows, CPR/libcurl does not properly initialize Winsock. The below code is used to
+ // initialize Winsock globally, which fixes this problem. Without this, only the first CPR
+ // session will properly be created, and subsequent ones will fail.
+ WSADATA wsa_data;
+ const int wsa_result{WSAStartup(MAKEWORD(2, 2), &wsa_data)};
+ if (wsa_result) {
+ LOG_CRITICAL(WebService, "WSAStartup failed: %d", wsa_result);
+ }
+#endif
+
+ // Built request header
+ cpr::Header header;
if (are_credentials_provided) {
// Authenticated request if credentials are provided
- std::thread{PostJsonAuthenticated, url, data, username, token}.detach();
+ header = {{"Content-Type", "application/json"},
+ {"x-username", username.c_str()},
+ {"x-token", token.c_str()},
+ {"api-version", API_VERSION}};
} else {
// Otherwise, anonymous request
- std::thread{PostJsonAnonymous, url, data}.detach();
+ header = cpr::Header{{"Content-Type", "application/json"}, {"api-version", API_VERSION}};
}
+
+ // Post JSON asynchronously
+ static cpr::AsyncResponse future;
+ future = cpr::PostAsync(cpr::Url{url.c_str()}, cpr::Body{data.c_str()}, header);
}
} // namespace WebService