2#include "crow/http_request.h"
3#include "crow/http_response.h"
4#include "crow/routing.h"
25 add_list_item(methods_, crow::method_name(method));
30 template<
typename... Methods>
33 add_list_item(methods_, crow::method_name(method));
41 add_list_item(headers_, header);
46 template<
typename... Headers>
49 add_list_item(headers_, header);
57 add_list_item(exposed_headers_, header);
62 template<
typename... Headers>
65 add_list_item(exposed_headers_, header);
73 max_age_ = std::to_string(
max_age);
80 allow_credentials_ =
true;
105 void add_list_item(std::string& list,
const std::string& val)
107 if (list ==
"*") list =
"";
108 if (list.size() > 0) list +=
", ";
113 void set_header_no_override(
const std::string& key,
const std::string& value,
crow::response& res)
115 if (value.size() == 0)
return;
121 void apply(
const request& req, response& res)
125 set_header_no_override(
"Access-Control-Allow-Methods", methods_, res);
126 set_header_no_override(
"Access-Control-Allow-Headers", headers_, res);
127 set_header_no_override(
"Access-Control-Expose-Headers", exposed_headers_, res);
128 set_header_no_override(
"Access-Control-Max-Age", max_age_, res);
129 if (allow_credentials_) set_header_no_override(
"Access-Control-Allow-Credentials",
"true", res);
131 if (allow_credentials_ && origin_ ==
"*")
132 set_header_no_override(
"Access-Control-Allow-Origin", req.get_header_value(
"Origin"), res);
134 set_header_no_override(
"Access-Control-Allow-Origin", origin_, res);
137 bool ignore_ =
false;
139 std::string origin_ =
"*";
140 std::string methods_ =
"*";
141 std::string headers_ =
"*";
142 std::string exposed_headers_;
143 std::string max_age_;
144 bool allow_credentials_ =
false;
146 CORSHandler* handler_;
165 auto& rule = find_rule(req.
url);
166 rule.apply(req, res);
173 return rules.back().second;
179 rules.emplace_back(bp.prefix(),
CORSRules(
this));
180 return rules.back().second;
190 CORSRules& find_rule(
const std::string& path)
193 for (
auto& rule : rules)
196 if (path.rfind(rule.first, 0) == 0)
204 std::vector<std::pair<std::string, CORSRules>> rules;
205 CORSRules default_ = CORSRules(
this);
220 return handler_->
global();
A blueprint can be considered a smaller section of a Crow app, specifically where the router is conce...
Definition routing.h:1091
The main namespace of the library. In this namespace is defined the most important classes and functi...
const std::string & get_header_value(const T &headers, const std::string &key)
Find and return the value associated with the key. (returns an empty string if nothing is found)
Definition http_request.h:24
CORSHandler is a global middleware for setting CORS headers.
Definition cors.h:156
CORSRules & global()
Get the global CORS policy.
Definition cors.h:184
CORSRules & blueprint(const Blueprint &bp)
Handle CORS for a specific blueprint.
Definition cors.h:177
CORSRules & prefix(const std::string &prefix)
Handle CORS on a specific prefix path.
Definition cors.h:170
Used for tuning CORS policies.
Definition cors.h:12
CORSRules & global()
Global CORS policy.
Definition cors.h:218
CORSRules & origin(const std::string &origin)
Set Access-Control-Allow-Origin. Default is "*".
Definition cors.h:16
CORSRules & methods(crow::HTTPMethod method, Methods... method_list)
Set Access-Control-Allow-Methods. Default is "*".
Definition cors.h:31
CORSRules & headers(const std::string &header)
Set Access-Control-Allow-Headers. Default is "*".
Definition cors.h:39
CORSRules & expose(const std::string &header)
Set Access-Control-Expose-Headers. Default is none.
Definition cors.h:55
CORSRules & headers(const std::string &header, Headers... header_list)
Set Access-Control-Allow-Headers. Default is "*".
Definition cors.h:47
CORSRules & expose(const std::string &header, Headers... header_list)
Set Access-Control-Expose-Headers. Default is none.
Definition cors.h:63
CORSRules & prefix(const std::string &prefix)
Handle CORS on specific prefix path.
Definition cors.h:208
CORSRules & max_age(int max_age)
Set Access-Control-Max-Age. Default is none.
Definition cors.h:71
CORSRules & blueprint(const Blueprint &bp)
Handle CORS for specific blueprint.
Definition cors.h:213
CORSRules & allow_credentials()
Enable Access-Control-Allow-Credentials.
Definition cors.h:78
CORSRules & methods(crow::HTTPMethod method)
Set Access-Control-Allow-Methods. Default is "*".
Definition cors.h:23
void ignore()
Ignore CORS and don't send any headers.
Definition cors.h:85
An HTTP request.
Definition http_request.h:36
std::string url
The endpoint without any parameters.
Definition http_request.h:39
HTTP response.
Definition http_response.h:34
void add_header(std::string key, std::string value)
Add a new header to the response.
Definition http_response.h:58
ci_map headers
HTTP headers.
Definition http_response.h:42