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
#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);
#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()) {
#include "method.h"
+#include <utility>
+
std::ostream &operator<<(std::ostream &os, const http::Method &method) {
using namespace http;
case Method::UPDATE: {
return os << "UPDATE";
} break;
+ default:
+ std::unreachable();
}
}
#include <string_view>
namespace http {
- struct http_version{
+ struct http_version {
uint8_t major = 1;
uint8_t minor = 1;
};
std::string_view accept = "*/*";
std::string_view body = "";
- http_version version;
+ http_version version = {.major = 1, .minor = 1};
};
} // namespace http
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;
}
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
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()) {
}
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()) {