summaryrefslogtreecommitdiff
path: root/src/web_service/web_backend.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/web_service/web_backend.cpp')
-rw-r--r--src/web_service/web_backend.cpp31
1 files changed, 17 insertions, 14 deletions
diff --git a/src/web_service/web_backend.cpp b/src/web_service/web_backend.cpp
index dff380cca..fdf3ac846 100644
--- a/src/web_service/web_backend.cpp
+++ b/src/web_service/web_backend.cpp
@@ -32,9 +32,14 @@ struct Client::Impl {
32 Impl(std::string host_, std::string username_, std::string token_) 32 Impl(std::string host_, std::string username_, std::string token_)
33 : host{std::move(host_)}, username{std::move(username_)}, token{std::move(token_)} { 33 : host{std::move(host_)}, username{std::move(username_)}, token{std::move(token_)} {
34 std::scoped_lock lock{jwt_cache.mutex}; 34 std::scoped_lock lock{jwt_cache.mutex};
35 if (username == jwt_cache.username && token == jwt_cache.token) { 35 if (this->username == jwt_cache.username && this->token == jwt_cache.token) {
36 jwt = jwt_cache.jwt; 36 jwt = jwt_cache.jwt;
37 } 37 }
38
39 // Normalize host expression
40 if (!this->host.empty() && this->host.back() == '/') {
41 static_cast<void>(this->host.pop_back());
42 }
38 } 43 }
39 44
40 /// A generic function handles POST, GET and DELETE request together 45 /// A generic function handles POST, GET and DELETE request together
@@ -71,18 +76,16 @@ struct Client::Impl {
71 const std::string& jwt_ = "", const std::string& username_ = "", 76 const std::string& jwt_ = "", const std::string& username_ = "",
72 const std::string& token_ = "") { 77 const std::string& token_ = "") {
73 if (cli == nullptr) { 78 if (cli == nullptr) {
74 cli = std::make_unique<httplib::Client>(host); 79 cli = std::make_unique<httplib::Client>(host.c_str());
80 cli->set_connection_timeout(TIMEOUT_SECONDS);
81 cli->set_read_timeout(TIMEOUT_SECONDS);
82 cli->set_write_timeout(TIMEOUT_SECONDS);
75 } 83 }
76
77 if (!cli->is_valid()) { 84 if (!cli->is_valid()) {
78 LOG_ERROR(WebService, "Client is invalid, skipping request!"); 85 LOG_ERROR(WebService, "Invalid URL {}", host + path);
79 return {}; 86 return WebResult{WebResult::Code::InvalidURL, "Invalid URL", ""};
80 } 87 }
81 88
82 cli->set_connection_timeout(TIMEOUT_SECONDS);
83 cli->set_read_timeout(TIMEOUT_SECONDS);
84 cli->set_write_timeout(TIMEOUT_SECONDS);
85
86 httplib::Headers params; 89 httplib::Headers params;
87 if (!jwt_.empty()) { 90 if (!jwt_.empty()) {
88 params = { 91 params = {
@@ -107,15 +110,15 @@ struct Client::Impl {
107 request.headers = params; 110 request.headers = params;
108 request.body = data; 111 request.body = data;
109 112
110 httplib::Response response; 113 httplib::Result result = cli->send(request);
111 httplib::Error error;
112 114
113 if (!cli->send(request, response, error)) { 115 if (!result) {
114 LOG_ERROR(WebService, "{} to {} returned null (httplib Error: {})", method, host + path, 116 LOG_ERROR(WebService, "{} to {} returned null", method, host + path);
115 httplib::to_string(error));
116 return WebResult{WebResult::Code::LibError, "Null response", ""}; 117 return WebResult{WebResult::Code::LibError, "Null response", ""};
117 } 118 }
118 119
120 httplib::Response response = result.value();
121
119 if (response.status >= 400) { 122 if (response.status >= 400) {
120 LOG_ERROR(WebService, "{} to {} returned error status code: {}", method, host + path, 123 LOG_ERROR(WebService, "{} to {} returned error status code: {}", method, host + path,
121 response.status); 124 response.status);