#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
+#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;
} break;
}
} else {
- std::cout << resp1.value() << std::endl;
+ std::cout << resp.value() << std::endl;
}
}
-#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
}
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());
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 << "."