From 17a564ab81094117db24fb340f9b167973c67df2 Mon Sep 17 00:00:00 2001 From: SONY-STRING Date: Wed, 3 Jul 2024 19:34:13 +0900 Subject: [PATCH] fix: add error message for request timeouts and add timed_out member variable to https_client to check for request timeouts --- include/dpp/httpsclient.h | 5 +++++ src/dpp/httpsclient.cpp | 7 ++++++- src/dpp/queues.cpp | 5 ++++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/include/dpp/httpsclient.h b/include/dpp/httpsclient.h index 7884baab39..0090c68af4 100644 --- a/include/dpp/httpsclient.h +++ b/include/dpp/httpsclient.h @@ -230,6 +230,11 @@ class DPP_EXPORT https_client : public ssl_client { http_state get_state(); public: + /** + * @brief If true the response timed out while waiting + */ + bool timed_out; + /** * @brief Connect to a specific HTTP(S) server and complete a request. * diff --git a/src/dpp/httpsclient.cpp b/src/dpp/httpsclient.cpp index b18675cd65..29598cd375 100644 --- a/src/dpp/httpsclient.cpp +++ b/src/dpp/httpsclient.cpp @@ -42,7 +42,8 @@ https_client::https_client(const std::string &hostname, uint16_t port, const st request_headers(extra_headers), status(0), http_protocol(protocol), - timeout(request_timeout) + timeout(request_timeout), + timed_out(false) { nonblocking = false; timeout = time(nullptr) + request_timeout; @@ -313,6 +314,10 @@ http_state https_client::get_state() { void https_client::one_second_timer() { if ((this->sfd == SOCKET_ERROR || time(nullptr) >= timeout) && this->state != HTTPS_DONE) { + /* if and only if response is timed out */ + if (this->sfd != SOCKET_ERROR) { + timed_out = true; + } keepalive = false; this->close(); } diff --git a/src/dpp/queues.cpp b/src/dpp/queues.cpp index b179edc747..b6d691210a 100644 --- a/src/dpp/queues.cpp +++ b/src/dpp/queues.cpp @@ -178,7 +178,10 @@ http_request_completion_t http_request::run(cluster* owner) { try { https_client cli(hci.hostname, hci.port, _url, request_verb[method], multipart.body, headers, !hci.is_ssl, request_timeout, protocol); rv.latency = dpp::utility::time_f() - start; - if (cli.get_status() < 100) { + if (cli.timed_out) { + rv.error = h_connection; + owner->log(ll_error, "HTTP(S) error on " + hci.scheme + " connection to " + hci.hostname + ":" + std::to_string(hci.port) + ": Timed out while waiting for the response"); + } else if (cli.get_status() < 100) { rv.error = h_connection; owner->log(ll_error, "HTTP(S) error on " + hci.scheme + " connection to " + hci.hostname + ":" + std::to_string(hci.port) + ": Malformed HTTP response"); } else {