mirror of
https://github.com/BlackMATov/curly.hpp.git
synced 2025-12-15 12:19:47 +07:00
move some code to header
This commit is contained in:
@@ -6,34 +6,38 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <cctype>
|
||||||
|
#include <cassert>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
|
#include <chrono>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
#include <map>
|
|
||||||
#include <chrono>
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <utility>
|
||||||
#include <vector>
|
#include <algorithm>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
|
||||||
|
#include <map>
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
namespace curly_hpp
|
namespace curly_hpp
|
||||||
{
|
{
|
||||||
class exception final : public std::runtime_error {
|
class request;
|
||||||
public:
|
class response;
|
||||||
explicit exception(const char* what);
|
class request_builder;
|
||||||
explicit exception(const std::string& what);
|
|
||||||
};
|
|
||||||
|
|
||||||
struct icase_string_compare final {
|
using http_code_t = std::uint16_t;
|
||||||
using is_transparent = void;
|
|
||||||
bool operator()(
|
using time_sec_t = std::chrono::seconds;
|
||||||
std::string_view l,
|
using time_ms_t = std::chrono::milliseconds;
|
||||||
std::string_view r) const noexcept;
|
using time_point_t = std::chrono::steady_clock::time_point;
|
||||||
};
|
|
||||||
|
|
||||||
enum class methods {
|
enum class methods {
|
||||||
put,
|
put,
|
||||||
@@ -42,16 +46,6 @@ namespace curly_hpp
|
|||||||
post
|
post
|
||||||
};
|
};
|
||||||
|
|
||||||
using http_code_t = std::uint16_t;
|
|
||||||
using headers_t = std::map<std::string, std::string, icase_string_compare>;
|
|
||||||
|
|
||||||
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<void(request)>;
|
|
||||||
|
|
||||||
class upload_handler {
|
class upload_handler {
|
||||||
public:
|
public:
|
||||||
virtual ~upload_handler() {}
|
virtual ~upload_handler() {}
|
||||||
@@ -65,10 +59,44 @@ namespace curly_hpp
|
|||||||
virtual std::size_t write(const char* src, std::size_t size) = 0;
|
virtual std::size_t write(const char* src, std::size_t size) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
using callback_t = std::function<void(request)>;
|
||||||
using uploader_uptr = std::unique_ptr<upload_handler>;
|
using uploader_uptr = std::unique_ptr<upload_handler>;
|
||||||
using downloader_uptr = std::unique_ptr<download_handler>;
|
using downloader_uptr = std::unique_ptr<download_handler>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
namespace curly_hpp
|
||||||
{
|
{
|
||||||
class content_t final {
|
class content_t final {
|
||||||
@@ -81,15 +109,31 @@ namespace curly_hpp
|
|||||||
content_t(const content_t&) = default;
|
content_t(const content_t&) = default;
|
||||||
content_t& operator=(const content_t&) = default;
|
content_t& operator=(const content_t&) = default;
|
||||||
|
|
||||||
content_t(std::string_view data);
|
content_t(std::string_view data)
|
||||||
content_t(std::vector<char> data) noexcept;
|
: data_(data.cbegin(), data.cend() ) {}
|
||||||
|
|
||||||
std::size_t size() const noexcept;
|
content_t(std::vector<char> data) noexcept
|
||||||
std::vector<char>& data() noexcept;
|
: data_(std::move(data)) {}
|
||||||
const std::vector<char>& data() const noexcept;
|
|
||||||
|
|
||||||
std::string as_string_copy() const;
|
std::size_t size() const noexcept {
|
||||||
std::string_view as_string_view() const noexcept;
|
return data_.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<char>& data() noexcept {
|
||||||
|
return data_;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::vector<char>& 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:
|
private:
|
||||||
std::vector<char> data_;
|
std::vector<char> data_;
|
||||||
};
|
};
|
||||||
@@ -107,10 +151,16 @@ namespace curly_hpp
|
|||||||
response(const response&) = delete;
|
response(const response&) = delete;
|
||||||
response& operator=(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;
|
bool is_http_error() const noexcept {
|
||||||
http_code_t http_code() const noexcept;
|
return http_code_ >= 400u;
|
||||||
|
}
|
||||||
|
|
||||||
|
http_code_t http_code() const noexcept {
|
||||||
|
return http_code_;
|
||||||
|
}
|
||||||
public:
|
public:
|
||||||
content_t content;
|
content_t content;
|
||||||
headers_t headers;
|
headers_t headers;
|
||||||
|
|||||||
@@ -6,13 +6,8 @@
|
|||||||
|
|
||||||
#include <curly.hpp/curly.hpp>
|
#include <curly.hpp/curly.hpp>
|
||||||
|
|
||||||
#include <cctype>
|
|
||||||
#include <cassert>
|
|
||||||
#include <cstring>
|
|
||||||
|
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <queue>
|
#include <queue>
|
||||||
#include <algorithm>
|
|
||||||
#include <condition_variable>
|
#include <condition_variable>
|
||||||
|
|
||||||
#ifndef NOMINMAX
|
#ifndef NOMINMAX
|
||||||
@@ -213,96 +208,6 @@ namespace
|
|||||||
std::unique_ptr<curl_state> curl_state::self_;
|
std::unique_ptr<curl_state> 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<char> data) noexcept
|
|
||||||
: data_(std::move(data)) {}
|
|
||||||
|
|
||||||
std::size_t content_t::size() const noexcept {
|
|
||||||
return data_.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<char>& content_t::data() noexcept {
|
|
||||||
return data_;
|
|
||||||
}
|
|
||||||
|
|
||||||
const std::vector<char>& 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
|
// request
|
||||||
|
|||||||
Reference in New Issue
Block a user