1#ifndef SOURCEMETA_CORE_HTTP_SYSTEM_H_
2#define SOURCEMETA_CORE_HTTP_SYSTEM_H_
4#ifndef SOURCEMETA_CORE_HTTP_EXPORT
5#include <sourcemeta/core/http_export.h>
8#include <sourcemeta/core/crypto.h>
9#include <sourcemeta/core/http_aws_sigv4.h>
10#include <sourcemeta/core/http_method.h>
11#include <sourcemeta/core/http_status.h>
12#include <sourcemeta/core/http_syntax.h>
13#include <sourcemeta/core/text.h>
25namespace sourcemeta::core {
31#pragma warning(disable : 4251 4275)
54 std::vector<std::pair<std::string, std::string>>
headers;
75 :
public std::runtime_error {
80 std::vector<std::string>
paths)
81 : std::runtime_error{message}, variable_{std::move(
variable)},
82 paths_{std::move(
paths)} {}
85 [[nodiscard]]
auto variable() const noexcept -> const std::
string & {
86 return this->variable_;
90 [[nodiscard]]
auto paths() const noexcept
91 -> const std::vector<std::
string> & {
96 std::string variable_;
97 std::vector<std::string> paths_;
122 : url_{std::move(url)}, method_{
method} {}
137 std::variant<std::string, SecureString>
data;
140 [[nodiscard]]
auto bytes() const -> std::string_view {
142 [](
const auto &value) -> std::string_view {
return value; },
159 this->headers_.emplace_back(std::move(name),
177 this->headers_.emplace_back(std::move(name),
193 [[nodiscard]]
auto header(
const std::string_view name)
const
194 -> std::optional<std::string_view> {
195 for (
const auto &[key, value] : this->headers_) {
197 return value.bytes();
205 [[nodiscard]]
auto headers() const noexcept -> const auto & {
206 return this->headers_;
222 Body{.data = std::move(data), .content_type = std::move(content_type)};
242 Body{.data = std::move(data), .content_type = std::move(content_type)};
248 this->follow_redirects_ = value;
254 this->maximum_redirects_ = value;
260 this->timeout_ = value;
268 this->connect_timeout_ = value;
274 this->maximum_response_size_ = value;
295 const std::string_view region,
296 const std::string_view service,
297 const std::chrono::system_clock::time_point moment =
298 std::chrono::system_clock::now())
313 std::variant<std::string, SecureString> data;
314 std::string content_type;
316 [[nodiscard]]
auto bytes()
const -> std::string_view {
318 [](
const auto &value) -> std::string_view {
return value; },
325 std::vector<std::pair<std::string, HeaderValue>> headers_;
326 std::optional<Body> body_;
327 bool follow_redirects_{
true};
328 std::size_t maximum_redirects_{20};
329 std::chrono::milliseconds timeout_{std::chrono::seconds{30}};
330 std::optional<std::chrono::milliseconds> connect_timeout_;
331 std::optional<std::size_t> maximum_response_size_;
335#pragma warning(default : 4251 4275)
HTTPSystemBackendError(const std::string &message, std::string variable, std::vector< std::string > paths)
Definition http_system.h:79
auto header(const std::string_view name) const -> std::optional< std::string_view >
Definition http_system.h:193
std::string body
The response body, owned by this result.
Definition http_system.h:56
auto follow_redirects(const bool value) -> HTTPSystemRequest &
Set whether to follow redirects, on by default.
Definition http_system.h:247
auto method(const HTTPMethod method) -> HTTPSystemRequest &
Set the request method.
Definition http_system.h:125
auto maximum_redirects(const std::size_t value) -> HTTPSystemRequest &
Set the maximum number of redirects to follow, 20 by default.
Definition http_system.h:253
auto timeout(const std::chrono::milliseconds value) -> HTTPSystemRequest &
Set the total request timeout, 30 seconds by default.
Definition http_system.h:259
auto headers() const noexcept -> const auto &
Get the request headers configured so far, in the order they were added.
Definition http_system.h:205
auto variable() const noexcept -> const std::string &
Get the name of the environment variable that overrides the backend path.
Definition http_system.h:85
auto header(std::string name, std::string value) -> HTTPSystemRequest &
Definition http_system.h:150
std::string url
The effective URL after any followed redirects.
Definition http_system.h:58
auto connect_timeout(const std::chrono::milliseconds value) -> HTTPSystemRequest &
Definition http_system.h:266
HTTPStatus status
The response status code.
Definition http_system.h:48
auto header(std::string name, SecureString value) -> HTTPSystemRequest &
Definition http_system.h:168
auto maximum_response_size(const std::size_t value) -> HTTPSystemRequest &
Abort with an error if the response body exceeds this number of bytes.
Definition http_system.h:273
auto paths() const noexcept -> const std::vector< std::string > &
Get the paths that were searched while looking for the backend.
Definition http_system.h:90
auto send() const -> HTTPResponse
auto body(SecureString data, std::string content_type) -> HTTPSystemRequest &
Definition http_system.h:231
std::vector< std::pair< std::string, std::string > > headers
Definition http_system.h:54
auto sign_aws_sigv4(const HTTPAWSCredentials &credentials, const std::string_view region, const std::string_view service, const std::chrono::system_clock::time_point moment=std::chrono::system_clock::now()) -> HTTPSystemRequest &
auto body(std::string data, std::string content_type) -> HTTPSystemRequest &
Definition http_system.h:212
HTTPSystemRequest(std::string url, const HTTPMethod method=HTTPMethod::GET)
Construct a request for the given URL and method.
Definition http_system.h:120
HTTPMethod
Definition http_method.h:12
auto http_field_line_has_forbidden_byte(const std::string_view value) noexcept -> bool
Definition http_syntax.h:196
@ GET
Transfer a current representation of the target resource.
Definition http_method.h:14
auto equals_ignore_case(const std::string_view left, const std::string_view right) noexcept -> bool
Definition text.h:838