roar
Loading...
Searching...
No Matches
cors.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <boost/beast/http/field.hpp>
4#include <boost/beast/http/verb.hpp>
5
6#include <string>
7#include <vector>
8#include <cctype>
9#include <optional>
10
11namespace Roar
12{
17 {
19 std::function<std::string(std::string const&)> allowedOrigin{[](std::string const& origin) {
20 // Requests with credentials CANNOT use a wildcard for any Allow-* header.
21 return origin.empty() ? std::string("*") : origin;
22 }};
23
25 std::function<std::vector<std::string>(std::vector<std::string> const&)> methodAllowSelection{
26 [](std::vector<std::string> const& requestedMethods) {
27 return requestedMethods;
28 }};
29
31 std::function<std::vector<std::string>(std::vector<std::string> const&)> headerAllowSelection{
32 [](std::vector<std::string> const& requestedHeaders) {
33 return requestedHeaders;
34 }};
35
37 std::vector<boost::beast::http::field> exposeHeaders{};
38
40 std::optional<bool> allowCredentials{std::nullopt};
41
44 };
45
53 inline CorsSettings makePermissiveCorsSettings(std::string method)
54 {
55 CorsSettings cors;
56 std::transform(begin(method), end(method), begin(method), [](auto character) {
57 return std::toupper(character);
58 });
60 [method = std::move(method)](std::vector<std::string> const&) -> std::vector<std::string> {
61 return std::vector<std::string>{method};
62 };
63 cors.allowCredentials = true;
64 return cors;
65 }
66
73 inline CorsSettings makePermissiveCorsSettings(boost::beast::http::verb method)
74 {
75 return makePermissiveCorsSettings(std::string{boost::beast::http::to_string(method)});
76 }
77}
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
Settings object to control CORS requests.
Definition cors.hpp:17
std::function< std::vector< std::string >(std::vector< std::string > const &)> methodAllowSelection
Access-Control-Allow-Methods.
Definition cors.hpp:25
std::optional< bool > allowCredentials
Access-Control-Allow-Credentials.
Definition cors.hpp:40
bool generatePreflightOptionsRoute
Generate an OPTIONS route for preflights?
Definition cors.hpp:43
std::vector< boost::beast::http::field > exposeHeaders
Access-Control-Expose-Headers, will not be set if empty.
Definition cors.hpp:37
std::function< std::string(std::string const &)> allowedOrigin
Access-Control-Allow-Origin.
Definition cors.hpp:19
std::function< std::vector< std::string >(std::vector< std::string > const &)> headerAllowSelection
Access-Control-Allow-Headers.
Definition cors.hpp:31