From: LeonardoBizzoni Date: Tue, 13 Aug 2024 10:25:09 +0000 (+0200) Subject: Asynchronous http send X-Git-Url: http://git.leonardobizzoni.com/?a=commitdiff_plain;h=8acd6fd2e065a1c1c9a2cf49be39cddfbb126f18;p=http-lib Asynchronous http send --- diff --git a/src/http.h b/src/http.h index dd64421..f54a97a 100644 --- a/src/http.h +++ b/src/http.h @@ -1,15 +1,6 @@ #pragma once -#include -#include -#include -#include -#include - -#include -#include -#include - +#include #include "error.h" #include "method.h" #include "request.h" @@ -21,8 +12,12 @@ namespace http { std::expected send(Method, const RequestOpts& req); - std::expected connect_to(const std::string_view& domain_name, const uint16_t port = 80); + std::expected connect(const std::string_view& domain_name, const uint16_t port = 80); std::string build_request(Method method, const RequestOpts &req); std::expected read_raw_response(const int8_t socketfd); + + namespace async { + std::future> send(Method method, const RequestOpts &req); + } } // namespace http diff --git a/src/main.cpp b/src/main.cpp index 332547c..50c3bc8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,14 +1,24 @@ +#include + #include #include "http.h" #include "request.h" int main() { - auto resp1 = http::send(http::Method::GET, http::RequestOpts {.domain_name = "example.com", .accept="application/json"}); - // auto resp1 = http::sendreq(http::Method::POST, {.domain_name = "example.com", .body = "Hello, World!"}); + auto resp1 = + http::async::send(http::Method::GET, http::RequestOpts{.domain_name = "example.com", + .accept = "application/json"}); + // auto resp1 = http::sendreq(http::Method::POST, {.domain_name = "example.com", .body = "Hello, + // World!"}); + + std::cout << "Going to sleep" << std::endl; + sleep(1); + std::cout << "I woke up" << std::endl; - if (!resp1.has_value()) { - switch (resp1.error()) { + auto resp = resp1.get(); + if (!resp.has_value()) { + switch (resp.error()) { case http::Error::SocketCreation: { std::cout << "Socket creation" << std::endl; } break; @@ -26,6 +36,6 @@ int main() { } break; } } else { - std::cout << resp1.value() << std::endl; + std::cout << resp.value() << std::endl; } } diff --git a/src/send.cpp b/src/send.cpp index 54e7614..89df2c5 100644 --- a/src/send.cpp +++ b/src/send.cpp @@ -1,19 +1,23 @@ -#include #include +#include #include #include #include -#include +#include +#include + +#include "error.h" #include "http.h" #include "request.h" +#include "response.h" #define BUFFSIZE 1024 static std::unordered_map ip_map; namespace http { - std::expected connect_to(const std::string_view &domain_name, + std::expected connect(const std::string_view &domain_name, const uint16_t port) { struct addrinfo hints = {}, *addr_list; hints.ai_family = AF_UNSPEC; // Either IPv4 or IPv6 @@ -72,7 +76,7 @@ namespace http { } std::expected send(Method method, const RequestOpts &req) { - auto maybe_socketfd = connect_to(req.domain_name, req.port); + auto maybe_socketfd = connect(req.domain_name, req.port); if (!maybe_socketfd.has_value()) { return ERR(maybe_socketfd.error()); @@ -90,6 +94,12 @@ namespace http { return Response::build(maybe_response.value()); } + namespace async { + std::future> send(Method method, const RequestOpts &req) { + return std::async(::http::send, method, req); + } + } + std::string build_request(Method method, const RequestOpts &req) { std::stringstream ss; ss << method << " " << req.query << " HTTP/" << (int)req.version.major << "."