aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/utils
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2017-12-16 14:04:25 -0500
committerJack Lloyd <[email protected]>2017-12-17 14:59:21 -0500
commit3679787e87482f96164f1fab4320d9ecacf1c6b9 (patch)
tree63d7d1e424a8576de8bec7e687a72d1135c1ada9 /src/lib/utils
parentdc8355ad610634e98c59700540a52523da1ca0d7 (diff)
Add an overall timeout to the HTTP request
Diffstat (limited to 'src/lib/utils')
-rw-r--r--src/lib/utils/http_util/http_util.cpp8
1 files changed, 8 insertions, 0 deletions
diff --git a/src/lib/utils/http_util/http_util.cpp b/src/lib/utils/http_util/http_util.cpp
index 40e1dc976..f5f9c0213 100644
--- a/src/lib/utils/http_util/http_util.cpp
+++ b/src/lib/utils/http_util/http_util.cpp
@@ -30,6 +30,8 @@ std::string http_transact(const std::string& hostname,
{
std::unique_ptr<OS::Socket> socket;
+ const std::chrono::system_clock::time_point start_time = std::chrono::system_clock::now();
+
try
{
socket = OS::open_socket(hostname, "http", timeout);
@@ -45,6 +47,9 @@ std::string http_transact(const std::string& hostname,
socket->write(cast_char_ptr_to_uint8(message.data()),
message.size());
+ if(std::chrono::system_clock::now() - start_time > timeout)
+ throw HTTP_Error("Timeout during writing message body");
+
std::ostringstream oss;
std::vector<uint8_t> buf(BOTAN_DEFAULT_BUFFER_SIZE);
while(true)
@@ -53,6 +58,9 @@ std::string http_transact(const std::string& hostname,
if(got == 0) // EOF
break;
+ if(std::chrono::system_clock::now() - start_time > timeout)
+ throw HTTP_Error("Timeout while reading message body");
+
oss.write(cast_uint8_ptr_to_char(buf.data()),
static_cast<std::streamsize>(got));
}