summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
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 @@
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
5#include <cstdlib> 9#include <cstdlib>
6#include <thread> 10#include <thread>
7#include <cpr/cpr.h> 11#include <cpr/cpr.h>
@@ -12,18 +16,7 @@ namespace WebService {
12 16
13static constexpr char API_VERSION[]{"1"}; 17static constexpr char API_VERSION[]{"1"};
14 18
15static void PostJsonAuthenticated(const std::string& url, const std::string& data, 19static std::unique_ptr<cpr::Session> g_session;
16 const std::string& username, const std::string& token) {
17 cpr::Post(cpr::Url{url}, cpr::Body{data}, cpr::Header{{"Content-Type", "application/json"},
18 {"x-username", username},
19 {"x-token", token},
20 {"api-version", API_VERSION}});
21}
22
23static void PostJsonAnonymous(const std::string& url, const std::string& data) {
24 cpr::Post(cpr::Url{url}, cpr::Body{data},
25 cpr::Header{{"Content-Type", "application/json"}, {"api-version", API_VERSION}});
26}
27 20
28void PostJson(const std::string& url, const std::string& data, bool allow_anonymous, 21void PostJson(const std::string& url, const std::string& data, bool allow_anonymous,
29 const std::string& username, const std::string& token) { 22 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
38 return; 31 return;
39 } 32 }
40 33
41 // Post JSON asynchronously by spawning a new thread 34#ifdef _WIN32
35 // On Windows, CPR/libcurl does not properly initialize Winsock. The below code is used to
36 // initialize Winsock globally, which fixes this problem. Without this, only the first CPR
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;
42 if (are_credentials_provided) { 47 if (are_credentials_provided) {
43 // Authenticated request if credentials are provided 48 // Authenticated request if credentials are provided
44 std::thread{PostJsonAuthenticated, url, data, username, token}.detach(); 49 header = {{"Content-Type", "application/json"},
50 {"x-username", username.c_str()},
51 {"x-token", token.c_str()},
52 {"api-version", API_VERSION}};
45 } else { 53 } else {
46 // Otherwise, anonymous request 54 // Otherwise, anonymous request
47 std::thread{PostJsonAnonymous, url, data}.detach(); 55 header = cpr::Header{{"Content-Type", "application/json"}, {"api-version", API_VERSION}};
48 } 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);
49} 61}
50 62
51} // namespace WebService 63} // namespace WebService