131 lines
4.4 KiB
C++
131 lines
4.4 KiB
C++
#pragma once
|
|
|
|
#include <algorithm>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <curl/curl.h>
|
|
#include <functional>
|
|
#include <memory>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <vector>
|
|
|
|
#include <memory_resource>
|
|
|
|
#include <rublon/error.hpp>
|
|
#include <rublon/utils.hpp>
|
|
|
|
#include <tl/expected.hpp>
|
|
|
|
namespace rublon {
|
|
|
|
namespace {
|
|
size_t WriteMemoryCallback(void * contents, size_t size, size_t nmemb, void * userp) {
|
|
const size_t realsize = size * nmemb;
|
|
reinterpret_cast< std::pmr::string * >(userp)->append(static_cast< const char * >(contents), realsize);
|
|
return realsize;
|
|
}
|
|
} // namespace
|
|
|
|
struct Request {
|
|
std::pmr::memory_resource * _mr;
|
|
|
|
std::pmr::map< std::pmr::string, std::pmr::string > headers;
|
|
std::pmr::string body;
|
|
|
|
public:
|
|
Request(std::pmr::memory_resource * mr) : _mr{mr}, headers{_mr}, body{_mr} {};
|
|
|
|
Request(const Request & res) = delete;
|
|
Request & operator=(const Request & res) = delete;
|
|
|
|
Request(Request && res) = delete;
|
|
Request & operator=(Request &&) = delete;
|
|
|
|
std::pmr::memory_resource * get_allocator() const noexcept {
|
|
return _mr;
|
|
}
|
|
};
|
|
|
|
struct Response {
|
|
std::pmr::memory_resource * _mr;
|
|
|
|
std::pmr::map< std::pmr::string, std::pmr::string > headers;
|
|
std::pmr::string body;
|
|
|
|
public:
|
|
Response(std::pmr::memory_resource * mr) : _mr{mr}, headers{_mr}, body{_mr} {};
|
|
|
|
Response(const Response & res) = delete;
|
|
Response & operator=(const Response & res) = delete;
|
|
|
|
Response(Response && res) noexcept = default;
|
|
Response & operator=(Response && res) noexcept = default;
|
|
|
|
std::pmr::memory_resource * get_allocator() const noexcept {
|
|
return _mr;
|
|
}
|
|
};
|
|
|
|
class CURL {
|
|
std::unique_ptr< ::CURL, void (*)(::CURL *) > curl;
|
|
|
|
public:
|
|
CURL() : curl{std::unique_ptr< ::CURL, void (*)(::CURL *) >(curl_easy_init(), curl_easy_cleanup)} {}
|
|
|
|
tl::expected< Response, HttpError > request(std::string_view uri, const Request & request) const {
|
|
memory::MonotonicStackResource< 8 * 1024 > stackResource;
|
|
|
|
std::pmr::string response_data{&stackResource};
|
|
response_data.reserve(6000);
|
|
|
|
/// TODO this can be done on stack using pmr
|
|
auto curl_headers = std::unique_ptr< curl_slist, void (*)(curl_slist *) >(nullptr, curl_slist_free_all);
|
|
std::for_each(request.headers.begin(), request.headers.end(), [&](auto header) {
|
|
log(LogLevel::Debug, "%s header: %s: %s", "CURL", header.first.c_str(), header.second.c_str());
|
|
curl_headers.reset(curl_slist_append(curl_headers.release(), (header.first + ": " + header.second).c_str()));
|
|
});
|
|
|
|
curl_easy_setopt(curl.get(), CURLOPT_VERBOSE, 1);
|
|
curl_easy_setopt(curl.get(), CURLOPT_URL, uri.data());
|
|
curl_easy_setopt(curl.get(), CURLOPT_HTTPHEADER, curl_headers.get());
|
|
curl_easy_setopt(curl.get(), CURLOPT_POST, 1);
|
|
curl_easy_setopt(curl.get(), CURLOPT_POSTFIELDS, request.body.data());
|
|
curl_easy_setopt(curl.get(), CURLOPT_POSTFIELDSIZE, static_cast< u_int32_t >(request.body.size()));
|
|
curl_easy_setopt(curl.get(), CURLOPT_HEADER, 1);
|
|
curl_easy_setopt(curl.get(), CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
|
|
curl_easy_setopt(curl.get(), CURLOPT_WRITEDATA, &response_data);
|
|
|
|
log(LogLevel::Debug, "%s Request send, uri:%s body:\n%s\n", "CURL", uri.data(), request.body.c_str());
|
|
const auto res = curl_easy_perform(curl.get());
|
|
|
|
if(res != CURLE_OK) {
|
|
log(LogLevel::Error, "%s No response from Rublon server err:{%s}", "CURL", curl_easy_strerror(res));
|
|
return tl::unexpected{HttpError{HttpError::Timeout, 0}};
|
|
}
|
|
|
|
long http_code = 0;
|
|
curl_easy_getinfo(curl.get(), CURLINFO_RESPONSE_CODE, http_code);
|
|
|
|
if(http_code >= 400) {
|
|
log(LogLevel::Error, "%s response with code %d ", "CURL", http_code);
|
|
return tl::unexpected{HttpError{HttpError::Error, http_code}};
|
|
}
|
|
|
|
log(LogLevel::Debug, "Response:\n%s\n", response_data.c_str());
|
|
|
|
long size;
|
|
curl_easy_getinfo(curl.get(), CURLINFO_HEADER_SIZE, &size);
|
|
|
|
Response response{memory::default_resource()};
|
|
|
|
details::headers(response_data, response.headers);
|
|
response.body = response_data.substr(size);
|
|
|
|
return response;
|
|
}
|
|
};
|
|
|
|
} // namespace rublon
|