]> git.leonardobizzoni.com Git - http-lib/commitdiff
Renamed `sendred` → `send` + clean up
authorLeonardoBizzoni <leo2002714@gmail.com>
Tue, 13 Aug 2024 10:02:32 +0000 (12:02 +0200)
committerLeonardoBizzoni <leo2002714@gmail.com>
Tue, 13 Aug 2024 10:02:32 +0000 (12:02 +0200)
CMakeLists.txt
src/http.h
src/main.cpp
src/method.cpp
src/request.h
src/response.cpp
src/send.cpp

index 0abad883bd823044e65ddcfc501d1803f13a6d30..162043ddc3aa574bdd1cca636eda8c293fed8e12 100644 (file)
@@ -23,6 +23,7 @@ add_executable(${PROJECT_NAME} ${SRC})
 
 target_sources(${PROJECT_NAME} PRIVATE ${PLATFORM_SRC})
 target_compile_definitions(${PROJECT_NAME} PRIVATE ROOTDIR="${CMAKE_SOURCE_DIR}")
+target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wextra -Wpedantic -Werror)
 
 # Set output directories
 set_target_properties(${PROJECT_NAME} PROPERTIES
index 8e1204c8937b2aba747480fdc2d9600c5535bd7f..dd644211c7b6f106f690875f412420ab46dcc947 100644 (file)
@@ -19,7 +19,7 @@
 #define NEW_LINE std::string_view("\r\n")
 
 namespace http {
-  std::expected<Response, Error> sendreq(Method, const RequestOpts& req);
+  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);
 
index f3f5b6902906c5da93e01fdcdcc77c17351ab9ea..332547c921e65c2e4f83cc613379d97b3bb0c33a 100644 (file)
@@ -1,9 +1,10 @@
 #include <iostream>
 
 #include "http.h"
+#include "request.h"
 
 int main() {
-  auto resp1 = http::sendreq(http::Method::GET, {.domain_name = "example.com"});
+  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!"});
 
   if (!resp1.has_value()) {
index afd1ac6d5e1c05364be457f1fe109bfa571383d0..5e2851c8a62859273c193fee37577701748fd90d 100644 (file)
@@ -1,5 +1,7 @@
 #include "method.h"
 
+#include <utility>
+
 std::ostream &operator<<(std::ostream &os, const http::Method &method) {
   using namespace http;
 
@@ -34,5 +36,7 @@ std::ostream &operator<<(std::ostream &os, const http::Method &method) {
     case Method::UPDATE: {
       return os << "UPDATE";
     } break;
+    default:
+      std::unreachable();
   }
 }
index 25c368119ffa300830436a102c67282d560c6656..e6c168f0d78e56108e68039b6d8444164e57bad7 100644 (file)
@@ -4,7 +4,7 @@
 #include <string_view>
 
 namespace http {
-  struct http_version{
+  struct http_version {
     uint8_t major = 1;
     uint8_t minor = 1;
   };
@@ -17,6 +17,6 @@ namespace http {
     std::string_view accept = "*/*";
     std::string_view body = "";
 
-    http_version version;
+    http_version version = {.major = 1, .minor = 1};
   };
 }  // namespace http
index ab8edc46c494f444078474fb660a5a10bcd460b7..e1d3f92b0764e9b2718bb466f661315078003ff9 100644 (file)
@@ -68,7 +68,7 @@ std::ostream &operator<<(std::ostream &os, const http::Response &resp) {
   os << "HTTP version: " << (int)resp.version.major << "." << (int)resp.version.minor << "\n";
   os << "Status: " << resp.status;
 
-  for (const auto entry : resp.fields) {
+  for (const auto &entry : resp.fields) {
     os << "\n" << entry.first << ": " << entry.second;
   }
 
index 88f239a59ce84a5a2ef5a4c949e431d36ec5ecb1..54e7614a918dcba8c8b34600fec862a38e5251cd 100644 (file)
@@ -15,7 +15,7 @@ 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,
                                          const uint16_t port) {
-    struct addrinfo hints = {0}, *addr_list;
+    struct addrinfo hints = {}, *addr_list;
     hints.ai_family = AF_UNSPEC;      // Either IPv4 or IPv6
     hints.ai_socktype = SOCK_STREAM;  // TCP only
 
@@ -71,7 +71,7 @@ namespace http {
     return remote_socketfd;
   }
 
-  std::expected<Response, Error> sendreq(Method method, const RequestOpts &req) {
+  std::expected<Response, Error> send(Method method, const RequestOpts &req) {
     auto maybe_socketfd = connect_to(req.domain_name, req.port);
 
     if (!maybe_socketfd.has_value()) {
@@ -79,7 +79,7 @@ namespace http {
     }
 
     std::string msg = build_request(method, req);
-    send((int)maybe_socketfd.value(), msg.c_str(), msg.size(), 0);
+    ::send((int)maybe_socketfd.value(), msg.c_str(), msg.size(), 0);
 
     auto maybe_response = read_raw_response(maybe_socketfd.value());
     if (!maybe_response.has_value()) {