diff options
author | Arttu Tarkiainen <arttu.tarkiainen@qt.io> | 2024-03-07 15:15:14 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2024-03-11 08:05:36 +0000 |
commit | e0018367d85e34d1a8afdc487f51813501c18391 (patch) | |
tree | e57bfe1a8c218c5cfe0615af7d34c4ab8cc6c8d2 | |
parent | 072454d5beff3da3480f36912d24bbbefaee1116 (diff) |
HttpClient: include HTTP status codes in logging prints
Also fix some duplicate prints and improve existing messages.
We cannot make the >= 400 codes return error status from HttpClient
(set CURLOPT_FAILONERROR=1) yet, as the server currently returns a
JSON response body which may contain a more detailed error code that
can be converted to string and passed to client application.
Change-Id: Ia28e28e90810a166da79e7c02d4203c3b8887a62
Reviewed-by: Iikka Eklund <iikka.eklund@qt.io>
(cherry picked from commit 8f3df6834fb9b6e23542d7f8e7aaa8d1b18d4a71)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r-- | src/libs/qlicenseservice/httpclient.cpp | 24 | ||||
-rw-r--r-- | src/libs/qlicenseservice/httpclient.h | 2 | ||||
-rw-r--r-- | src/libs/qlicenseservice/licenser.cpp | 5 |
3 files changed, 20 insertions, 11 deletions
diff --git a/src/libs/qlicenseservice/httpclient.cpp b/src/libs/qlicenseservice/httpclient.cpp index 5c54aa8..b7ab54f 100644 --- a/src/libs/qlicenseservice/httpclient.cpp +++ b/src/libs/qlicenseservice/httpclient.cpp @@ -19,6 +19,7 @@ namespace QLicenseService { HttpResult::HttpResult(HttpRequest *request) : m_request(request) , m_deleteRequest(false) + , m_code(-1) { } @@ -38,6 +39,11 @@ std::string HttpResult::reply() const return m_reply; } +long HttpResult::code() const +{ + return m_code; +} + CURLcode HttpResult::error() const { return m_error; @@ -65,7 +71,6 @@ HttpRequest::HttpRequest(const std::string &url, const std::string &authKey) , m_forceHttps(false) , m_headers(nullptr) { - logDebug("Send to URL: %s", m_url.c_str() ); if (!authKey.empty()) { // Add auth key in headers const std::string auth = "Authorization: " + authKey; @@ -98,7 +103,6 @@ HttpResult *HttpRequest::doRequest(const std::string &payload) utils::sleepMillisecs(m_delay); } - logDebug("Payload to send:\n%s", payload.c_str()); utils::setBlockSignalsMask(); HttpResult *result = new HttpResult(this); @@ -117,9 +121,12 @@ HttpResult *HttpRequest::doRequest(const std::string &payload) curl_easy_setopt(m_curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS); if (!payload.empty()) { + logDebug("POST to URL: %s", m_url.c_str() ); + logDebug("Payload to send:\n%s", payload.c_str()); curl_easy_setopt(m_curl, CURLOPT_HTTPPOST, 1); curl_easy_setopt(m_curl, CURLOPT_POSTFIELDS, payload.c_str()); } else { + logDebug("GET to URL: %s", m_url.c_str() ); curl_easy_setopt(m_curl, CURLOPT_HTTPGET, 1); // for server ping } @@ -131,6 +138,8 @@ HttpResult *HttpRequest::doRequest(const std::string &payload) // get it result->m_error = curl_easy_perform(m_curl); + curl_easy_getinfo(m_curl, CURLINFO_RESPONSE_CODE, &result->m_code); + // check for errors if (result->m_error != CURLE_OK) { logError("HTTP transfer failed, URL: %s", m_url.c_str()); @@ -140,8 +149,10 @@ HttpResult *HttpRequest::doRequest(const std::string &payload) return result; } - logDebug("%d bytes retrieved from license server:\n%s", - result->m_reply.length(), result->m_reply.c_str()); + + logDebug("[%d] %d bytes retrieved from license server:\n%s", + result->code(), result->m_reply.length(), result->m_reply.c_str()); + if (onResultReady) onResultReady(result->m_reply); @@ -222,7 +233,6 @@ bool HttpClient::sendReceive(std::string &reply, const std::string &payload, con // If server URL is not given as param, we use the default std::string requestUrl = server.empty() ? m_serverUrl : server; requestUrl += accessPoint; - logDebug("Send/receive from URL: %s", requestUrl.c_str()); HttpRequest request(requestUrl, authKey); request.setForceHttps(m_forceHttps); @@ -258,10 +268,8 @@ Status HttpClient::receive(uint16_t &clientId, std::string &reply) } result->setDeleteRequest(true); - if (result->error() != CURLE_OK) { - logError("HTTP request failed: %s", result->errorString().c_str()); + if (result->error() != CURLE_OK) return Status::BAD_CONNECTION; - } reply = result->reply(); return Status::SUCCESS; diff --git a/src/libs/qlicenseservice/httpclient.h b/src/libs/qlicenseservice/httpclient.h index 53b0daa..285e576 100644 --- a/src/libs/qlicenseservice/httpclient.h +++ b/src/libs/qlicenseservice/httpclient.h @@ -35,6 +35,7 @@ public: HttpRequest *request() const; std::string reply() const; + long code() const; CURLcode error() const; std::string errorString() const; @@ -49,6 +50,7 @@ private: HttpRequest *const m_request; bool m_deleteRequest; + long m_code; std::string m_reply; CURLcode m_error; }; diff --git a/src/libs/qlicenseservice/licenser.cpp b/src/libs/qlicenseservice/licenser.cpp index a80bf4a..864ee1f 100644 --- a/src/libs/qlicenseservice/licenser.cpp +++ b/src/libs/qlicenseservice/licenser.cpp @@ -276,9 +276,8 @@ int Licenser::processReservation(ClientHandler *client, uint32_t delay) logWarn("No client stored with id: %d", requestId); return 0; } - if (reply.empty()) { - logWarn("No response from server"); - } + if (reply.empty()) + logWarn("No response body from server"); // No further actions on shutdown if (m_state == DaemonState::Stopped) |