Stroika Library 3.0d25
 
Loading...
Searching...
No Matches
CORS.h
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2026. All rights reserved
3 */
4#ifndef _Stroika_Framework_WebServer_CORS_h_
5#define _Stroika_Framework_WebServer_CORS_h_ 1
6
7#include "Stroika/Frameworks/StroikaPreComp.h"
8
10#include "Stroika/Foundation/Containers/Set.h"
11
12/*
13 * \note Code-Status: <a href="Code-Status.md#Alpha">Alpha</a>
14 */
15
17
18 using namespace Stroika::Foundation;
20 using Containers::Set;
21
22 /**
23 * Options for how the HTTP Server handles CORS (mostly HTTP OPTIONS requests)
24 */
25 struct CORSOptions {
26
27 /**
28 * true or false if credentials allowed on CORS request
29 * \see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Credentials
30 *
31 * \note Allowed only for an origin fAllowedOrigins names - not with it * (any origin), beside which a browser refuses
32 * credentials (https://fetch.spec.whatwg.org/#cors-protocol-and-credentials).
33 */
34 optional<bool> fAllowCredentials;
35
36 /**
37 */
38 static constexpr bool kAllowCredentials_Default{true};
39
40 /**
41 * `Access-Control-Max-Age` header. How long the `Access-Control-Allow-Methods` and `Access-Control-Allow-Headers` headers can be cached.
42 * (rarely set - default good)
43 */
44 optional<unsigned int> fAccessControlMaxAge;
45
46 static constexpr unsigned int kAccessControlMaxAge_Default{24 * 60 * 60};
47
48 /**
49 * This can be {"*"} meaning any origin (default).
50 * Or it can be a list of values present in HTTP Origin Headers (typically just hostname but can be Host:port)
51 */
52 optional<Set<String>> fAllowedOrigins;
53
54 /**
55 */
56 static constexpr string_view kAccessControlWildcard = "*"sv;
57
58 /**
59 * This is the set of headers which will be allowed by Access-Control-Request-Headers OPTIONS requests.
60 * The default - * - kAccessControlWildcard - any - is typically just fine.
61 */
62 optional<Set<String>> fAllowedHeaders;
63
64 /**
65 * @see Characters::ToString ();
66 */
68 };
69 inline const CORSOptions kDefault_CORSOptions{CORSOptions::kAllowCredentials_Default, CORSOptions::kAccessControlMaxAge_Default,
70 Set<String>{CORSOptions::kAccessControlWildcard}, Set<String>{CORSOptions::kAccessControlWildcard}};
71
72}
73
74/*
75 ********************************************************************************
76 ***************************** Implementation Details ***************************
77 ********************************************************************************
78 */
79#include "CORS.inl"
80
81#endif /*_Stroika_Framework_WebServer_CORS_h_*/
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
Set<T> is a container of T, where once an item is added, additionally adds () do nothing.
Characters::String ToString() const
Definition CORS.cpp:22
optional< unsigned int > fAccessControlMaxAge
Definition CORS.h:44
optional< Set< String > > fAllowedOrigins
Definition CORS.h:52
optional< Set< String > > fAllowedHeaders
Definition CORS.h:62