2021-03-26 23:18:51 +01:00
|
|
|
#ifndef CPR_BODY_H
|
|
|
|
#define CPR_BODY_H
|
|
|
|
|
2023-08-05 20:31:33 +02:00
|
|
|
#include <exception>
|
|
|
|
#include <fstream>
|
2021-03-26 23:18:51 +01:00
|
|
|
#include <initializer_list>
|
|
|
|
#include <string>
|
2023-08-05 20:31:33 +02:00
|
|
|
#include <vector>
|
2021-03-26 23:18:51 +01:00
|
|
|
|
2023-08-05 20:31:33 +02:00
|
|
|
#include "cpr/buffer.h"
|
2021-03-26 23:18:51 +01:00
|
|
|
#include "cpr/cprtypes.h"
|
2023-08-05 20:31:33 +02:00
|
|
|
#include "cpr/file.h"
|
2021-03-26 23:18:51 +01:00
|
|
|
|
|
|
|
namespace cpr {
|
|
|
|
|
|
|
|
class Body : public StringHolder<Body> {
|
|
|
|
public:
|
2023-08-05 20:31:33 +02:00
|
|
|
Body() = default;
|
2021-03-26 23:18:51 +01:00
|
|
|
// NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions)
|
2023-08-05 20:31:33 +02:00
|
|
|
Body(std::string body) : StringHolder<Body>(std::move(body)) {}
|
2021-03-26 23:18:51 +01:00
|
|
|
// NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions)
|
2023-08-05 20:31:33 +02:00
|
|
|
Body(std::string_view body) : StringHolder<Body>(body) {}
|
2021-03-26 23:18:51 +01:00
|
|
|
// NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions)
|
|
|
|
Body(const char* body) : StringHolder<Body>(body) {}
|
|
|
|
Body(const char* str, size_t len) : StringHolder<Body>(str, len) {}
|
|
|
|
Body(const std::initializer_list<std::string> args) : StringHolder<Body>(args) {}
|
2023-08-05 20:31:33 +02:00
|
|
|
// NOLINTNEXTLINE(google-explicit-constructor, cppcoreguidelines-pro-type-reinterpret-cast)
|
|
|
|
Body(const Buffer& buffer) : StringHolder<Body>(reinterpret_cast<const char*>(buffer.data), static_cast<size_t>(buffer.datalen)) {}
|
|
|
|
// NOLINTNEXTLINE(google-explicit-constructor)
|
|
|
|
Body(const File& file) {
|
|
|
|
std::ifstream is(file.filepath, std::ifstream::binary);
|
|
|
|
if (!is) {
|
|
|
|
throw std::invalid_argument("Can't open the file for HTTP request body!");
|
|
|
|
}
|
|
|
|
|
|
|
|
is.seekg(0, std::ios::end);
|
|
|
|
const std::streampos length = is.tellg();
|
|
|
|
is.seekg(0, std::ios::beg);
|
|
|
|
std::string buffer;
|
|
|
|
buffer.resize(static_cast<size_t>(length));
|
|
|
|
is.read(buffer.data(), length);
|
|
|
|
str_ = std::move(buffer);
|
|
|
|
}
|
2021-03-26 23:18:51 +01:00
|
|
|
Body(const Body& other) = default;
|
|
|
|
Body(Body&& old) noexcept = default;
|
|
|
|
~Body() override = default;
|
|
|
|
|
|
|
|
Body& operator=(Body&& old) noexcept = default;
|
|
|
|
Body& operator=(const Body& other) = default;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace cpr
|
|
|
|
|
|
|
|
#endif
|