diff options
-rw-r--r-- | src/cli/utils.cpp | 8 | ||||
-rw-r--r-- | src/lib/utils/http_util/http_util.cpp | 27 | ||||
-rw-r--r-- | src/lib/utils/http_util/http_util.h | 10 |
3 files changed, 32 insertions, 13 deletions
diff --git a/src/cli/utils.cpp b/src/cli/utils.cpp index 094665a00..161cc24bf 100644 --- a/src/cli/utils.cpp +++ b/src/cli/utils.cpp @@ -218,11 +218,15 @@ BOTAN_REGISTER_COMMAND("rng", RNG); class HTTP_Get final : public Command { public: - HTTP_Get() : Command("http_get url") {} + HTTP_Get() : Command("http_get --redirects=1 --timeout=3000 url") {} void go() override { - output() << Botan::HTTP::GET_sync(get_arg("url")) << "\n"; + const std::string url = get_arg("url"); + const std::chrono::milliseconds timeout(get_arg_sz("timeout")); + const size_t redirects = get_arg_sz("redirects"); + + output() << Botan::HTTP::GET_sync(url, redirects, timeout) << "\n"; } }; diff --git a/src/lib/utils/http_util/http_util.cpp b/src/lib/utils/http_util/http_util.cpp index 55dbd4df6..40e1dc976 100644 --- a/src/lib/utils/http_util/http_util.cpp +++ b/src/lib/utils/http_util/http_util.cpp @@ -25,13 +25,14 @@ namespace { * closes the socket. */ std::string http_transact(const std::string& hostname, - const std::string& message) + const std::string& message, + std::chrono::milliseconds timeout) { std::unique_ptr<OS::Socket> socket; try { - socket = OS::open_socket(hostname, "http", std::chrono::milliseconds(2500)); + socket = OS::open_socket(hostname, "http", timeout); if(!socket) throw Exception("No socket support enabled in build"); } @@ -205,10 +206,17 @@ Response http_sync(const std::string& verb, const std::string& url, const std::string& content_type, const std::vector<uint8_t>& body, - size_t allowable_redirects) + size_t allowable_redirects, + std::chrono::milliseconds timeout) { + auto transact_with_timeout = + [timeout](const std::string& hostname, const std::string& service) + { + return http_transact(hostname, service, timeout); + }; + return http_sync( - http_transact, + transact_with_timeout, verb, url, content_type, @@ -216,17 +224,20 @@ Response http_sync(const std::string& verb, allowable_redirects); } -Response GET_sync(const std::string& url, size_t allowable_redirects) +Response GET_sync(const std::string& url, + size_t allowable_redirects, + std::chrono::milliseconds timeout) { - return http_sync("GET", url, "", std::vector<uint8_t>(), allowable_redirects); + return http_sync("GET", url, "", std::vector<uint8_t>(), allowable_redirects, timeout); } Response POST_sync(const std::string& url, const std::string& content_type, const std::vector<uint8_t>& body, - size_t allowable_redirects) + size_t allowable_redirects, + std::chrono::milliseconds timeout) { - return http_sync("POST", url, content_type, body, allowable_redirects); + return http_sync("POST", url, content_type, body, allowable_redirects, timeout); } } diff --git a/src/lib/utils/http_util/http_util.h b/src/lib/utils/http_util/http_util.h index 14f168886..9edd3d983 100644 --- a/src/lib/utils/http_util/http_util.h +++ b/src/lib/utils/http_util/http_util.h @@ -14,6 +14,7 @@ #include <map> #include <string> #include <functional> +#include <chrono> namespace Botan { @@ -79,15 +80,18 @@ BOTAN_PUBLIC_API(2,0) Response http_sync(const std::string& verb, const std::string& url, const std::string& content_type, const std::vector<uint8_t>& body, - size_t allowable_redirects); + size_t allowable_redirects, + std::chrono::milliseconds timeout = std::chrono::milliseconds(3000)); BOTAN_PUBLIC_API(2,0) Response GET_sync(const std::string& url, - size_t allowable_redirects = 1); + size_t allowable_redirects = 1, + std::chrono::milliseconds timeout = std::chrono::milliseconds(3000)); BOTAN_PUBLIC_API(2,0) Response POST_sync(const std::string& url, const std::string& content_type, const std::vector<uint8_t>& body, - size_t allowable_redirects = 1); + size_t allowable_redirects = 1, + std::chrono::milliseconds timeout = std::chrono::milliseconds(3000)); BOTAN_PUBLIC_API(2,0) std::string url_encode(const std::string& url); |