10#include <boost/beast/http/message.hpp>
11#include <boost/beast/http/string_body.hpp>
12#include <boost/beast/http/empty_body.hpp>
13#include <promise-cpp/promise.hpp>
15#ifdef ROAR_ENABLE_NLOHMANN_JSON
16# include <nlohmann/json.hpp>
27 template <
typename BodyT>
39 explicit Response(boost::beast::http::response<BodyT>&& res)
45 template <
typename... Forwards>
47 :
response_{std::piecewise_construct, std::forward_as_tuple(std::forward<Forwards>(forwards)...)}
103 template <
typename T>
106#ifdef ROAR_ENABLE_NLOHMANN_JSON
107 if constexpr (std::is_same_v<std::decay_t<T>, nlohmann::json>)
108 response_.body() = std::forward<T>(toAssign).dump();
111 response_.body() = std::forward<T>(toAssign);
144 return setHeader(boost::beast::http::field::content_type, type);
148 if (
auto it =
response_.find(boost::beast::http::field::content_type); it != std::end(
response_))
149 return std::string{it->value()};
155 return setHeader(boost::beast::http::field::content_length, std::to_string(length));
159 if (
auto it =
response_.find(boost::beast::http::field::content_length); it != std::end(
response_))
160 return std::stoull(std::string{it->value()});
189 auto at(boost::beast::http::field field)
const
204 auto erase(boost::beast::http::field field)
208 auto find(boost::beast::http::field field)
const
216 bool hasField(boost::beast::http::field field)
const
240 void set(boost::beast::http::field field, std::string
const& value)
261 return setHeader(boost::beast::http::field::content_type, type);
275 std::optional<std::string>
getHeader(boost::beast::http::field field)
const
278 return std::string{it->value()};
305 auto joinList(std::vector<std::string>
const& list)
307 return list.empty() ? std::string{}
309 std::next(std::begin(list)),
312 [](std::string accum, std::string
const& elem) {
313 return std::move(accum) +
"," + elem;
325 template <
typename RequestBodyT>
332 boost::beast::http::field::access_control_allow_origin,
333 cors->allowedOrigin(std::string{req[boost::beast::http::field::origin]}));
336 if (req.method() == boost::beast::http::verb::options)
338 std::string requestedMethod;
339 if (req.find(boost::beast::http::field::access_control_request_method) != std::end(req))
340 requestedMethod = std::string{req[boost::beast::http::field::access_control_request_method]};
342 if (
const auto&& allowedMethods = cors->methodAllowSelection({std::move(requestedMethod)});
343 !allowedMethods.empty())
344 response_.set(boost::beast::http::field::access_control_allow_methods,
joinList(allowedMethods));
347 std::vector<std::string> requestedHeaders;
348 if (req.find(boost::beast::http::field::access_control_request_headers) != std::end(req))
352 if (
const auto&& allowedHeaders = cors->headerAllowSelection(requestedHeaders); !allowedHeaders.empty())
353 response_.set(boost::beast::http::field::access_control_allow_headers,
joinList(allowedHeaders));
355 response_.set(boost::beast::http::field::access_control_allow_headers,
"*");
357 if (cors->allowCredentials.has_value())
359 boost::beast::http::field::access_control_allow_credentials,
360 *cors->allowCredentials ?
"true" :
"false");
362 if (!cors->exposeHeaders.empty())
365 boost::beast::http::field::access_control_expose_headers,
367 std::next(std::begin(cors->exposeHeaders)),
368 std::end(cors->exposeHeaders),
369 std::string{boost::beast::http::to_string(cors->exposeHeaders.front())},
370 [](std::string accum,
auto const& field) {
371 return std::move(accum) +
"," + std::string{boost::beast::http::to_string(field)};
380 response_.
set(boost::beast::http::field::access_control_allow_origin,
"*");
381 response_.set(boost::beast::http::field::access_control_allow_methods,
"*");
382 response_.set(boost::beast::http::field::access_control_allow_headers,
"*");
383 response_.set(boost::beast::http::field::access_control_allow_credentials,
"true");
390 boost::beast::http::response<BodyT>&&
response() &&
392 return std::move(response_);
std::string toSetCookieString() const
Definition cookie.cpp:159
This class extends the boost::beast::http::request<BodyT> with additional convenience.
Definition request.hpp:52
std::vector< std::string > splitCommaSeperatedHeaderEntry(FieldT &&field) const
Retrieves a header which value is a typically comma seperated list as a vector of string.
Definition request.hpp:208
Definition response.hpp:29
Response()
Definition response.hpp:33
auto body() const
Definition response.hpp:115
Response & contentType(std::string const &type)
For setting of the content type.
Definition response.hpp:142
auto version() const
Definition response.hpp:248
auto target() const
Definition response.hpp:244
std::optional< std::string > contentType() const
Definition response.hpp:146
bool hasKeepAlive() const
Definition response.hpp:220
std::optional< std::size_t > contentLength() const
Definition response.hpp:157
Response(Forwards... forwards)
Definition response.hpp:46
auto cbegin() const
Definition response.hpp:180
bool hasField(boost::beast::http::field field) const
Definition response.hpp:216
auto begin()
Definition response.hpp:164
boost::beast::http::response< BodyT > response_
Definition response.hpp:404
Response & status(boost::beast::http::status status)
Sets the response status code.
Definition response.hpp:57
Response & setHeader(boost::beast::http::field field, char const *value)
Can be used to set a header field.
Definition response.hpp:287
Response & body(T &&toAssign)
This function can be used to assign something to the body.
Definition response.hpp:104
Response(boost::beast::http::response< BodyT > &&res)
Definition response.hpp:39
std::optional< std::string > getHeader(boost::beast::http::field field) const
Definition response.hpp:275
auto & body()
Retrieve the response body object.
Definition response.hpp:79
Response & enableCors(Request< RequestBodyT > const &req, std::optional< CorsSettings > cors=std::nullopt)
Sets cors headers.
Definition response.hpp:326
Response & setCookie(Cookie const &cookie)
Sets a set-cookie header entry.
Definition response.hpp:68
auto at(boost::beast::http::field field) const
Definition response.hpp:189
auto cend() const
Definition response.hpp:184
Response & preparePayload()
Sets header values that are implicit by the body (like Content-Lenght).
Definition response.hpp:298
auto equal_range(boost::beast::http::field field) const
Definition response.hpp:199
bool chunked() const
Definition response.hpp:131
auto find(boost::beast::http::field field) const
Definition response.hpp:208
Response & setHeader(boost::beast::http::field field, std::string const &value)
Can be used to set a header field.
Definition response.hpp:269
auto resultInt() const
Definition response.hpp:236
bool hasContentLength() const
Definition response.hpp:212
Response & contentLength(std::size_t length)
Definition response.hpp:153
auto erase(boost::beast::http::field field)
Definition response.hpp:204
boost::beast::http::response< BodyT > & response() &
Accessor for the underlying boost response.
Definition response.hpp:398
Response & chunked(bool activate=true)
(De)Activate chunked encoding.
Definition response.hpp:126
auto begin() const
Definition response.hpp:168
auto end() const
Definition response.hpp:176
auto payloadSize() const
Definition response.hpp:224
Response & enableCorsEverything()
Definition response.hpp:378
BodyT body_type
Definition response.hpp:31
boost::beast::http::response< BodyT > && response() &&
Ejects the underlying boost response.
Definition response.hpp:390
auto end()
Definition response.hpp:172
void clear()
Definition response.hpp:194
auto result() const
Definition response.hpp:232
auto reason() const
Definition response.hpp:228
auto joinList(std::vector< std::string > const &list)
Definition response.hpp:305
Response & keepAlive(bool keepAlive=true)
Set keep alive.
Definition response.hpp:90
Response & contentType(char const *type)
For setting of the content type.
Definition response.hpp:259
void set(boost::beast::http::field field, std::string const &value)
Definition response.hpp:240
Definition authorization.hpp:10
CorsSettings makePermissiveCorsSettings(std::string method)
Reflects all requested headers and origin or sets the Kleene star. Is as permissive as possible....
Definition cors.hpp:53