1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 08:47:17 +01:00
SqMod/vendor/CPR/include/cpr/multipart.h

80 lines
2.9 KiB
C
Raw Permalink Normal View History

2021-03-26 23:18:51 +01:00
#ifndef CPR_MULTIPART_H
#define CPR_MULTIPART_H
#include <cstdint>
#include <initializer_list>
#include <string>
#include <type_traits>
#include <vector>
namespace cpr {
struct File {
2021-07-02 16:44:48 +02:00
explicit File(std::string&& p_filepath) : filepath(std::move(p_filepath)) {}
explicit File(const std::string& p_filepath) : filepath(p_filepath) {}
2021-03-26 23:18:51 +01:00
const std::string filepath;
};
struct Buffer {
using data_t = const unsigned char*;
template <typename Iterator>
2021-07-02 16:44:48 +02:00
Buffer(Iterator begin, Iterator end, std::string&& p_filename)
2021-03-26 23:18:51 +01:00
// Ignored here since libcurl reqires a long.
// There is also no way around the reinterpret_cast.
// NOLINTNEXTLINE(google-runtime-int, cppcoreguidelines-pro-type-reinterpret-cast)
: data{reinterpret_cast<data_t>(&(*begin))}, datalen{static_cast<long>(
std::distance(begin, end))},
2021-07-02 16:44:48 +02:00
filename(std::move(p_filename)) {
2021-03-26 23:18:51 +01:00
is_random_access_iterator(begin, end);
static_assert(sizeof(*begin) == 1, "only byte buffers can be used");
}
template <typename Iterator>
typename std::enable_if<std::is_same<typename std::iterator_traits<Iterator>::iterator_category,
std::random_access_iterator_tag>::value>::type
is_random_access_iterator(Iterator /* begin */, Iterator /* end */) {}
data_t data;
// Ignored here since libcurl reqires a long:
// NOLINTNEXTLINE(google-runtime-int)
long datalen;
const std::string filename;
};
struct Part {
2021-07-02 16:44:48 +02:00
Part(const std::string& p_name, const std::string& p_value, const std::string& p_content_type = {})
: name{p_name}, value{p_value},
content_type{p_content_type}, is_file{false}, is_buffer{false} {}
Part(const std::string& p_name, const std::int32_t& p_value, const std::string& p_content_type = {})
: name{p_name}, value{std::to_string(p_value)},
content_type{p_content_type}, is_file{false}, is_buffer{false} {}
Part(const std::string& p_name, const File& file, const std::string& p_content_type = {})
: name{p_name}, value{file.filepath},
content_type{p_content_type}, is_file{true}, is_buffer{false} {}
Part(const std::string& p_name, const Buffer& buffer, const std::string& p_content_type = {})
: name{p_name}, value{buffer.filename}, content_type{p_content_type}, data{buffer.data},
2021-03-26 23:18:51 +01:00
datalen{buffer.datalen}, is_file{false}, is_buffer{true} {}
std::string name;
std::string value;
std::string content_type;
Buffer::data_t data{nullptr};
// Ignored here since libcurl reqires a long:
// NOLINTNEXTLINE(google-runtime-int)
long datalen{0};
bool is_file;
bool is_buffer;
};
class Multipart {
public:
Multipart(const std::initializer_list<Part>& parts);
std::vector<Part> parts;
};
} // namespace cpr
#endif