From 7fb90e3226d100dd89f726f10ffdf2040494b242 Mon Sep 17 00:00:00 2001 From: Simon Shillaker Date: Thu, 12 Aug 2021 14:21:55 +0000 Subject: [PATCH] Remove HTTP utils --- include/faabric/util/http.h | 26 ------------ src/util/CMakeLists.txt | 1 - src/util/http.cpp | 78 ----------------------------------- tests/test/util/test_http.cpp | 77 ---------------------------------- 4 files changed, 182 deletions(-) delete mode 100644 include/faabric/util/http.h delete mode 100644 src/util/http.cpp delete mode 100644 tests/test/util/test_http.cpp diff --git a/include/faabric/util/http.h b/include/faabric/util/http.h deleted file mode 100644 index 89d13907b..000000000 --- a/include/faabric/util/http.h +++ /dev/null @@ -1,26 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -#define HTTP_FILE_TIMEOUT 20000 - -using namespace Pistache; - -namespace faabric::util { -std::vector readFileFromUrl(const std::string& url); - -std::vector readFileFromUrlWithHeader( - const std::string& url, - const std::shared_ptr& header); - -class FaabricHttpException : public faabric::util::FaabricException -{ - public: - explicit FaabricHttpException(std::string message) - : FaabricException(std::move(message)) - {} -}; -} diff --git a/src/util/CMakeLists.txt b/src/util/CMakeLists.txt index 56dd984d5..e509c30a8 100644 --- a/src/util/CMakeLists.txt +++ b/src/util/CMakeLists.txt @@ -12,7 +12,6 @@ set(LIB_FILES files.cpp func.cpp gids.cpp - http.cpp json.cpp latch.cpp logging.cpp diff --git a/src/util/http.cpp b/src/util/http.cpp deleted file mode 100644 index 0f7cbafd7..000000000 --- a/src/util/http.cpp +++ /dev/null @@ -1,78 +0,0 @@ -#include -#include -#include - -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -using namespace Pistache; - -namespace faabric::util { -std::vector readFileFromUrl(const std::string& url) -{ - return readFileFromUrlWithHeader(url, nullptr); -} - -std::vector readFileFromUrlWithHeader( - const std::string& url, - const std::shared_ptr& header) -{ - Http::Client client; - client.init(); - - auto rb = client.get(url); - - if (header != nullptr) { - rb.header(header); - } - - // Set up the request and callbacks - Async::Promise resp = - rb.timeout(std::chrono::milliseconds(HTTP_FILE_TIMEOUT)).send(); - - std::stringstream out; - Http::Code respCode; - bool success = true; - resp.then( - [&](Http::Response response) { - respCode = response.code(); - if (respCode == Http::Code::Ok) { - auto body = response.body(); - out << body; - } else { - success = false; - } - }, - [&](std::exception_ptr exc) { - PrintException excPrinter; - excPrinter(exc); - success = false; - }); - - // Make calls synchronous - Async::Barrier barrier(resp); - std::chrono::milliseconds timeout(HTTP_FILE_TIMEOUT); - barrier.wait_for(timeout); - - client.shutdown(); - - // Check the response - if (!success) { - std::string msg = - fmt::format("Error reading file from {} ({})", url, respCode); - throw FaabricHttpException(msg); - } - - return faabric::util::stringToBytes(out.str()); -} -} diff --git a/tests/test/util/test_http.cpp b/tests/test/util/test_http.cpp deleted file mode 100644 index 567f4ed41..000000000 --- a/tests/test/util/test_http.cpp +++ /dev/null @@ -1,77 +0,0 @@ -#include "faabric/util/bytes.h" -#include "faabric/util/config.h" -#include - -#include -#include -#include -#include - -using namespace faabric::util; - -#define DUMMY_CONTENTS "blahblahblahblah" - -namespace tests { - -struct DummyHandler : public Http::Handler -{ - HTTP_PROTOTYPE(DummyHandler) - - void onRequest(const Http::Request&, Http::ResponseWriter writer) override - { - writer.send(Http::Code::Ok, DUMMY_CONTENTS); - } -}; - -TEST_CASE("Test reading from a URL", "[util]") -{ - auto conf = faabric::util::getSystemConfig(); - - // Start a dummy server - const Pistache::Address address("localhost", Pistache::Port(0)); - - Http::Endpoint server(address); - auto flags = Tcp::Options::ReuseAddr; - auto serverOpts = Http::Endpoint::options().flags(flags); - server.init(serverOpts); - server.setHandler(Http::make_handler()); - server.serveThreaded(); - - const std::string url = "localhost:" + server.getPort().toString(); - - std::vector actualBytes = faabric::util::readFileFromUrl(url); - std::vector expectedBytes = - faabric::util::stringToBytes(DUMMY_CONTENTS); - - REQUIRE(actualBytes == expectedBytes); - - server.shutdown(); -} - -TEST_CASE("Test reading from bad URLs", "[util]") -{ - std::string url; - std::string expectedMessage; - - SECTION("500 error") - { - url = "httpstat.us/500"; - expectedMessage = "Error reading file from httpstat.us/500 (500)"; - } - SECTION("203 code") - { - url = "httpstat.us/203"; - expectedMessage = "Error reading file from httpstat.us/203 (203)"; - } - - bool exceptionThrown = false; - try { - faabric::util::readFileFromUrl(url); - } catch (faabric::util::FaabricHttpException& ex) { - exceptionThrown = true; - REQUIRE(ex.what() == expectedMessage); - } - - REQUIRE(exceptionThrown); -} -}