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 max_age_ = std::to_string(
max_age);
64 allow_credentials_ =
true;
89 void add_list_item(std::string& list,
const std::string& val)
91 if (list ==
"*") list =
"";
92 if (list.size() > 0) list +=
", ";
97 void set_header_no_override(
const std::string& key,
const std::string& value,
crow::response& res)
99 if (value.size() == 0)
return;
108 set_header_no_override(
"Access-Control-Allow-Origin", origin_, res);
109 set_header_no_override(
"Access-Control-Allow-Methods", methods_, res);
110 set_header_no_override(
"Access-Control-Allow-Headers", headers_, res);
111 set_header_no_override(
"Access-Control-Max-Age", max_age_, res);
112 if (allow_credentials_) set_header_no_override(
"Access-Control-Allow-Credentials",
"true", res);
115 bool ignore_ =
false;
117 std::string origin_ =
"*";
118 std::string methods_ =
"*";
119 std::string headers_ =
"*";
120 std::string max_age_;
121 bool allow_credentials_ =
false;
123 CORSHandler* handler_;
142 auto& rule = find_rule(req.
url);
150 return rules.back().second;
156 rules.emplace_back(bp.prefix(),
CORSRules(
this));
157 return rules.back().second;
167 CORSRules& find_rule(
const std::string& path)
170 for (
auto& rule : rules)
173 if (path.rfind(rule.first, 0) == 0)
181 std::vector<std::pair<std::string, CORSRules>> rules;
182 CORSRules default_ = CORSRules(
this);
197 return handler_->
global();
A blueprint can be considered a smaller section of a Crow app, specifically where the router is conce...
Definition: routing.h:1114
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:133
CORSRules & blueprint(const Blueprint &bp)
Handle CORS for a specific blueprint.
Definition: cors.h:154
CORSRules & prefix(const std::string &prefix)
Handle CORS on a specific prefix path.
Definition: cors.h:147
CORSRules & global()
Get the global CORS policy.
Definition: cors.h:161
Used for tuning CORS policies.
Definition: cors.h:12
CORSRules & global()
Global CORS policy.
Definition: cors.h:195
CORSRules & origin(const std::string &origin)
Set Access-Control-Allow-Origin. Default is "*".
Definition: cors.h:16
CORSRules & methods(crow::HTTPMethod method)
Set Access-Control-Allow-Methods. Default is "*".
Definition: cors.h:23
CORSRules & methods(crow::HTTPMethod method, Methods... method_list)
Set Access-Control-Allow-Methods. Default is "*".
Definition: cors.h:31
CORSRules & prefix(const std::string &prefix)
Handle CORS on specific prefix path.
Definition: cors.h:185
CORSRules & max_age(int max_age)
Set Access-Control-Max-Age. Default is none.
Definition: cors.h:55
CORSRules & allow_credentials()
Enable Access-Control-Allow-Credentials.
Definition: cors.h:62
CORSRules & blueprint(const Blueprint &bp)
Handle CORS for specific blueprint.
Definition: cors.h:190
CORSRules & headers(const std::string &header, Headers... header_list)
Set Access-Control-Allow-Headers. Default is "*".
Definition: cors.h:47
CORSRules & headers(const std::string &header)
Set Access-Control-Allow-Headers. Default is "*".
Definition: cors.h:39
void ignore()
Ignore CORS and don't send any headers.
Definition: cors.h:69
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