From e4d4aaa9663f688404e5a3b82e47ddcbcec4906b Mon Sep 17 00:00:00 2001 From: LeonardoBizzoni Date: Tue, 13 Aug 2024 12:02:32 +0200 Subject: [PATCH] =?utf8?q?Renamed=20`sendred`=20=E2=86=92=20`send`=20+=20c?= =?utf8?q?lean=20up?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 1 + src/http.h | 2 +- src/main.cpp | 3 ++- src/method.cpp | 4 ++++ src/request.h | 4 ++-- src/response.cpp | 2 +- src/send.cpp | 6 +++--- 7 files changed, 14 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0abad88..162043d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/src/http.h b/src/http.h index 8e1204c..dd64421 100644 --- a/src/http.h +++ b/src/http.h @@ -19,7 +19,7 @@ #define NEW_LINE std::string_view("\r\n") namespace http { - std::expected sendreq(Method, const RequestOpts& req); + std::expected send(Method, const RequestOpts& req); std::expected connect_to(const std::string_view& domain_name, const uint16_t port = 80); diff --git a/src/main.cpp b/src/main.cpp index f3f5b69..332547c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,9 +1,10 @@ #include #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()) { diff --git a/src/method.cpp b/src/method.cpp index afd1ac6..5e2851c 100644 --- a/src/method.cpp +++ b/src/method.cpp @@ -1,5 +1,7 @@ #include "method.h" +#include + 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(); } } diff --git a/src/request.h b/src/request.h index 25c3681..e6c168f 100644 --- a/src/request.h +++ b/src/request.h @@ -4,7 +4,7 @@ #include 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 diff --git a/src/response.cpp b/src/response.cpp index ab8edc4..e1d3f92 100644 --- a/src/response.cpp +++ b/src/response.cpp @@ -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; } diff --git a/src/send.cpp b/src/send.cpp index 88f239a..54e7614 100644 --- a/src/send.cpp +++ b/src/send.cpp @@ -15,7 +15,7 @@ static std::unordered_map ip_map; namespace http { std::expected 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 sendreq(Method method, const RequestOpts &req) { + std::expected 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()) { -- 2.52.0