diff --git a/headers/curly.hpp/curly.hpp b/headers/curly.hpp/curly.hpp index 2d150f4..7d276d2 100644 --- a/headers/curly.hpp/curly.hpp +++ b/headers/curly.hpp/curly.hpp @@ -6,34 +6,38 @@ #pragma once +#include +#include +#include + #include #include #include +#include #include -#include -#include #include -#include -#include +#include +#include #include #include +#include +#include +#include + namespace curly_hpp { - class exception final : public std::runtime_error { - public: - explicit exception(const char* what); - explicit exception(const std::string& what); - }; + class request; + class response; + class request_builder; - struct icase_string_compare final { - using is_transparent = void; - bool operator()( - std::string_view l, - std::string_view r) const noexcept; - }; + using http_code_t = std::uint16_t; + + using time_sec_t = std::chrono::seconds; + using time_ms_t = std::chrono::milliseconds; + using time_point_t = std::chrono::steady_clock::time_point; enum class methods { put, @@ -42,16 +46,6 @@ namespace curly_hpp post }; - using http_code_t = std::uint16_t; - using headers_t = std::map; - - using time_sec_t = std::chrono::seconds; - using time_ms_t = std::chrono::milliseconds; - using time_point_t = std::chrono::steady_clock::time_point; - - class request; - using callback_t = std::function; - class upload_handler { public: virtual ~upload_handler() {} @@ -65,10 +59,44 @@ namespace curly_hpp virtual std::size_t write(const char* src, std::size_t size) = 0; }; + using callback_t = std::function; using uploader_uptr = std::unique_ptr; using downloader_uptr = std::unique_ptr; } +namespace curly_hpp +{ + class exception final : public std::runtime_error { + public: + explicit exception(const char* what) + : std::runtime_error(what) {} + + explicit exception(const std::string& what) + : std::runtime_error(what) {} + }; +} + +namespace curly_hpp +{ + namespace detail + { + struct icase_string_compare final { + using is_transparent = void; + bool operator()(std::string_view l, std::string_view r) const noexcept { + return std::lexicographical_compare( + l.begin(), l.end(), r.begin(), r.end(), + [](const char lc, const char rc) noexcept { + return std::tolower(lc) < std::tolower(rc); + }); + } + }; + } + + using headers_t = std::map< + std::string, std::string, + detail::icase_string_compare>; +} + namespace curly_hpp { class content_t final { @@ -81,15 +109,31 @@ namespace curly_hpp content_t(const content_t&) = default; content_t& operator=(const content_t&) = default; - content_t(std::string_view data); - content_t(std::vector data) noexcept; + content_t(std::string_view data) + : data_(data.cbegin(), data.cend() ) {} - std::size_t size() const noexcept; - std::vector& data() noexcept; - const std::vector& data() const noexcept; + content_t(std::vector data) noexcept + : data_(std::move(data)) {} - std::string as_string_copy() const; - std::string_view as_string_view() const noexcept; + std::size_t size() const noexcept { + return data_.size(); + } + + std::vector& data() noexcept { + return data_; + } + + const std::vector& data() const noexcept { + return data_; + } + + std::string as_string_copy() const { + return {data_.data(), data_.size()}; + } + + std::string_view as_string_view() const noexcept { + return {data_.data(), data_.size()}; + } private: std::vector data_; }; @@ -107,10 +151,16 @@ namespace curly_hpp response(const response&) = delete; response& operator=(const response&) = delete; - explicit response(http_code_t c) noexcept; + explicit response(http_code_t c) noexcept + : http_code_(c) {} - bool is_http_error() const noexcept; - http_code_t http_code() const noexcept; + bool is_http_error() const noexcept { + return http_code_ >= 400u; + } + + http_code_t http_code() const noexcept { + return http_code_; + } public: content_t content; headers_t headers; diff --git a/sources/curly.hpp/curly.cpp b/sources/curly.hpp/curly.cpp index c5d4d80..f76dd26 100644 --- a/sources/curly.hpp/curly.cpp +++ b/sources/curly.hpp/curly.cpp @@ -6,13 +6,8 @@ #include -#include -#include -#include - #include #include -#include #include #ifndef NOMINMAX @@ -213,96 +208,6 @@ namespace std::unique_ptr curl_state::self_; } -// ----------------------------------------------------------------------------- -// -// exception -// -// ----------------------------------------------------------------------------- - -namespace curly_hpp -{ - exception::exception(const char* what) - : std::runtime_error(what) {} - - exception::exception(const std::string& what) - : std::runtime_error(what) {} -} - -// ----------------------------------------------------------------------------- -// -// icase_string_compare -// -// ----------------------------------------------------------------------------- - -namespace curly_hpp -{ - bool icase_string_compare::operator()( - std::string_view l, - std::string_view r) const noexcept - { - return std::lexicographical_compare( - l.begin(), l.end(), r.begin(), r.end(), - [](const auto lc, const auto rc) noexcept { - return std::tolower(lc) < std::tolower(rc); - }); - } -} - -// ----------------------------------------------------------------------------- -// -// content_t -// -// ----------------------------------------------------------------------------- - -namespace curly_hpp -{ - content_t::content_t(std::string_view data) - : data_(data.cbegin(), data.cend() ) {} - - content_t::content_t(std::vector data) noexcept - : data_(std::move(data)) {} - - std::size_t content_t::size() const noexcept { - return data_.size(); - } - - std::vector& content_t::data() noexcept { - return data_; - } - - const std::vector& content_t::data() const noexcept { - return data_; - } - - std::string content_t::as_string_copy() const { - return {data_.data(), data_.size()}; - } - - std::string_view content_t::as_string_view() const noexcept { - return {data_.data(), data_.size()}; - } -} - -// ----------------------------------------------------------------------------- -// -// response -// -// ----------------------------------------------------------------------------- - -namespace curly_hpp -{ - response::response(http_code_t c) noexcept - : http_code_(c) {} - - bool response::is_http_error() const noexcept { - return http_code_ >= 400u; - } - - http_code_t response::http_code() const noexcept { - return http_code_; - } -} - // ----------------------------------------------------------------------------- // // request