From: LeonardoBizzoni Date: Tue, 13 Aug 2024 12:44:43 +0000 (+0200) Subject: Return error if invalid response instead of defaulting X-Git-Url: http://git.leonardobizzoni.com/?a=commitdiff_plain;h=7a32c1fa432700dda5604b224ede30037268d0b5;p=http-lib Return error if invalid response instead of defaulting --- diff --git a/src/error.h b/src/error.h index 64c4009..1c2f2d5 100644 --- a/src/error.h +++ b/src/error.h @@ -7,5 +7,8 @@ namespace http { DNSResolution, ServerNotFound, InvalidRead, + InvalidResponse, + InvalidResponseHTTPVersion, + InvalidResponseStatusCode, }; } diff --git a/src/main.cpp b/src/main.cpp index 42294ce..66a11bc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,13 +1,10 @@ -#include - #include #include "http.h" -#include "request.h" int main() { - auto resp1 = http::async::send(http::Method::GET, - http::RequestOpts{.domain_name = "google.com", .query = "/prova"}); + auto resp1 = http::async::send(http::Method::GET, {.domain_name = "example.com", + .version = {1, 1}}); auto resp = resp1.get(); if (!resp.has_value()) { @@ -27,6 +24,15 @@ int main() { case http::Error::InvalidRead: { std::cout << "Invalid read" << std::endl; } break; + case http::Error::InvalidResponse: { + std::cout << "Invalid response" << std::endl; + } break; + case http::Error::InvalidResponseHTTPVersion: { + std::cout << "Invalid response http version" << std::endl; + } break; + case http::Error::InvalidResponseStatusCode: { + std::cout << "Invalid response status code" << std::endl; + } break; } } else { std::cout << resp.value() << std::endl; diff --git a/src/response.cpp b/src/response.cpp index cdc9d77..cc81be1 100644 --- a/src/response.cpp +++ b/src/response.cpp @@ -1,11 +1,16 @@ #include #include #include +#include #include "http.h" namespace http { std::expected Response::build(std::string_view raw_response) noexcept { + if (raw_response.empty()) { + return ERR(Error::InvalidResponse); + } + Response resp; auto lines_view = raw_response | std::views::split(NEW_LINE); @@ -22,14 +27,14 @@ namespace http { resp.version = {.major = (uint8_t)std::stoi(major.data()), .minor = (uint8_t)std::stoi(minor.data())}; } catch (...) { - resp.version = {.major = 0, .minor = 0}; + return ERR(Error::InvalidResponseHTTPVersion); } try { std::string_view status(*++word_iter); resp.status = status_map[std::stoi(status.data())]; } catch (...) { - resp.status = Status::PARSE_ERROR; + return ERR(Error::InvalidResponseStatusCode); } for (std::string_view line; lines_view_iter != lines_view.end() && @@ -55,8 +60,6 @@ std::ostream &operator<<(std::ostream &os, const http::Status &status) { using namespace http; switch (status) { - case Status::PARSE_ERROR: - return os << (uint16_t)status << " - Parse Error"; case Status::CONTINUE: return os << (uint16_t)status << " - Continue"; case Status::SWITCHING_PROTOCOLS: @@ -181,9 +184,8 @@ std::ostream &operator<<(std::ostream &os, const http::Status &status) { return os << (uint16_t)status << " - Not Extended"; case Status::NETWORK_AUTHENTICATION_REQUIRED: return os << (uint16_t)status << " - Network Authentication Required"; - default: - return os << (uint16_t)status << " - Unknown Status"; + std::unreachable(); } } diff --git a/src/response.h b/src/response.h index 7979834..2c54b62 100644 --- a/src/response.h +++ b/src/response.h @@ -11,7 +11,6 @@ namespace http { enum class Status { - PARSE_ERROR = 0, CONTINUE = 100, SWITCHING_PROTOCOLS = 101, PROCESSING = 102,