]> git.leonardobizzoni.com Git - http-lib/commitdiff
Asynchronous http send
authorLeonardoBizzoni <leo2002714@gmail.com>
Tue, 13 Aug 2024 10:25:09 +0000 (12:25 +0200)
committerLeonardoBizzoni <leo2002714@gmail.com>
Tue, 13 Aug 2024 10:25:09 +0000 (12:25 +0200)
src/http.h
src/main.cpp
src/send.cpp

index dd644211c7b6f106f690875f412420ab46dcc947..f54a97ab196ba894451100e3e926007dbc237cc2 100644 (file)
@@ -1,15 +1,6 @@
 #pragma once
 
-#include <arpa/inet.h>
-#include <netdb.h>
-#include <sys/socket.h>
-#include <unistd.h>
-#include <fcntl.h>
-
-#include <cstdint>
-#include <expected>
-#include <string_view>
-
+#include <future>
 #include "error.h"
 #include "method.h"
 #include "request.h"
 namespace http {
   std::expected<Response, Error> send(Method, const RequestOpts& req);
 
-  std::expected<int8_t, Error> connect_to(const std::string_view& domain_name, const uint16_t port = 80);
+  std::expected<int8_t, Error> connect(const std::string_view& domain_name, const uint16_t port = 80);
 
   std::string build_request(Method method, const RequestOpts &req);
   std::expected<std::string, Error> read_raw_response(const int8_t socketfd);
+
+  namespace async {
+    std::future<std::expected<Response, Error>> send(Method method, const RequestOpts &req);
+  }
 }  // namespace http
index 332547c921e65c2e4f83cc613379d97b3bb0c33a..50c3bc86068364c23265b6e56c70225d45ce3d8e 100644 (file)
@@ -1,14 +1,24 @@
+#include <unistd.h>
+
 #include <iostream>
 
 #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;
   }
 }
index 54e7614a918dcba8c8b34600fec862a38e5251cd..89df2c5df490eea42391337be62a5566bbdea046 100644 (file)
@@ -1,19 +1,23 @@
-#include <algorithm>
 #include <cstdint>
+#include <expected>
 #include <iostream>
 #include <sstream>
 #include <unordered_map>
-#include <vector>
 
+#include <netdb.h>
+#include <unistd.h>
+
+#include "error.h"
 #include "http.h"
 #include "request.h"
+#include "response.h"
 
 #define BUFFSIZE 1024
 
 static std::unordered_map<std::string, struct addrinfo> ip_map;
 
 namespace http {
-  std::expected<int8_t, Error> connect_to(const std::string_view &domain_name,
+  std::expected<int8_t, Error> 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<Response, Error> 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<std::expected<Response, Error>> 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 << "."