| 1 |
|
|
1 |
|
|
| 2 |
|
// Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
|
2 |
|
// Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
|
| 3 |
|
|
3 |
|
|
| 4 |
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
4 |
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
| 5 |
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
5 |
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
| 6 |
|
|
6 |
|
|
| 7 |
|
// Official repository: https://github.com/cppalliance/http
|
7 |
|
// Official repository: https://github.com/cppalliance/http
|
| 8 |
|
|
8 |
|
|
| 9 |
|
|
9 |
|
|
| 10 |
|
#ifndef BOOST_HTTP_SERVER_CORS_HPP
|
10 |
|
#ifndef BOOST_HTTP_SERVER_CORS_HPP
|
| 11 |
|
#define BOOST_HTTP_SERVER_CORS_HPP
|
11 |
|
#define BOOST_HTTP_SERVER_CORS_HPP
|
| 12 |
|
|
12 |
|
|
| 13 |
|
#include <boost/http/detail/config.hpp>
|
13 |
|
#include <boost/http/detail/config.hpp>
|
| 14 |
|
#include <boost/http/server/route_handler.hpp>
|
14 |
|
#include <boost/http/server/route_handler.hpp>
|
| 15 |
|
#include <boost/http/status.hpp>
|
15 |
|
#include <boost/http/status.hpp>
|
| 16 |
|
|
16 |
|
|
| 17 |
|
|
17 |
|
|
| 18 |
|
|
18 |
|
|
| 19 |
|
|
19 |
|
|
| 20 |
|
|
20 |
|
|
| 21 |
|
/** Options for CORS middleware configuration.
|
21 |
|
/** Options for CORS middleware configuration.
|
| 22 |
|
|
22 |
|
|
| 23 |
|
|
23 |
|
|
| 24 |
|
|
24 |
|
|
| 25 |
|
/// Allowed origin, or "*" for any. Empty defaults to "*".
|
25 |
|
/// Allowed origin, or "*" for any. Empty defaults to "*".
|
| 26 |
|
|
26 |
|
|
| 27 |
|
|
27 |
|
|
| 28 |
|
/// Allowed HTTP methods. Empty defaults to common methods.
|
28 |
|
/// Allowed HTTP methods. Empty defaults to common methods.
|
| 29 |
|
|
29 |
|
|
| 30 |
|
|
30 |
|
|
| 31 |
|
/// Allowed request headers.
|
31 |
|
/// Allowed request headers.
|
| 32 |
|
std::string allowedHeaders;
|
32 |
|
std::string allowedHeaders;
|
| 33 |
|
|
33 |
|
|
| 34 |
|
/// Response headers exposed to client.
|
34 |
|
/// Response headers exposed to client.
|
| 35 |
|
std::string exposedHeaders;
|
35 |
|
std::string exposedHeaders;
|
| 36 |
|
|
36 |
|
|
| 37 |
|
/// Max age for preflight cache.
|
37 |
|
/// Max age for preflight cache.
|
| 38 |
|
std::chrono::seconds max_age{ 0 };
|
38 |
|
std::chrono::seconds max_age{ 0 };
|
| 39 |
|
|
39 |
|
|
| 40 |
|
/// Status code for preflight response.
|
40 |
|
/// Status code for preflight response.
|
| 41 |
|
status result = status::no_content;
|
41 |
|
status result = status::no_content;
|
| 42 |
|
|
42 |
|
|
| 43 |
|
/// If true, pass preflight to next handler.
|
43 |
|
/// If true, pass preflight to next handler.
|
| 44 |
|
bool preFlightContinue = false;
|
44 |
|
bool preFlightContinue = false;
|
| 45 |
|
|
45 |
|
|
| 46 |
|
/// If true, allow credentials.
|
46 |
|
/// If true, allow credentials.
|
| 47 |
|
bool credentials = false;
|
47 |
|
bool credentials = false;
|
| 48 |
|
|
48 |
|
|
| 49 |
|
|
49 |
|
|
| 50 |
|
/** CORS middleware for handling cross-origin requests.
|
50 |
|
/** CORS middleware for handling cross-origin requests.
|
| 51 |
|
|
51 |
|
|
| 52 |
|
This middleware handles Cross-Origin Resource Sharing
|
52 |
|
This middleware handles Cross-Origin Resource Sharing
|
| 53 |
|
(CORS) by setting appropriate response headers and
|
53 |
|
(CORS) by setting appropriate response headers and
|
| 54 |
|
handling preflight OPTIONS requests.
|
54 |
|
handling preflight OPTIONS requests.
|
| 55 |
|
|
55 |
|
|
| 56 |
|
|
56 |
|
|
| 57 |
|
|
57 |
|
|
| 58 |
|
|
58 |
|
|
| 59 |
|
|
59 |
|
|
| 60 |
|
opts.methods = "GET,POST,PUT,DELETE";
|
60 |
|
opts.methods = "GET,POST,PUT,DELETE";
|
| 61 |
|
|
61 |
|
|
| 62 |
|
|
62 |
|
|
| 63 |
|
router.use( cors( opts ) );
|
63 |
|
router.use( cors( opts ) );
|
| 64 |
|
|
64 |
|
|
| 65 |
|
|
65 |
|
|
| 66 |
|
|
66 |
|
|
| 67 |
|
|
67 |
|
|
| 68 |
|
class BOOST_HTTP_DECL cors
|
68 |
|
class BOOST_HTTP_DECL cors
|
| 69 |
|
|
69 |
|
|
| 70 |
|
|
70 |
|
|
| 71 |
|
|
71 |
|
|
| 72 |
|
|
72 |
|
|
| 73 |
|
/** Construct a CORS middleware.
|
73 |
|
/** Construct a CORS middleware.
|
| 74 |
|
|
74 |
|
|
| 75 |
|
@param options Configuration options.
|
75 |
|
@param options Configuration options.
|
| 76 |
|
|
76 |
|
|
| 77 |
|
explicit cors(cors_options options = {}) noexcept;
|
77 |
|
explicit cors(cors_options options = {}) noexcept;
|
| 78 |
|
|
78 |
|
|
| 79 |
|
|
79 |
|
|
| 80 |
|
|
80 |
|
|
| 81 |
|
Sets CORS headers and handles preflight requests.
|
81 |
|
Sets CORS headers and handles preflight requests.
|
| 82 |
|
|
82 |
|
|
| 83 |
|
@param rp The route parameters.
|
83 |
|
@param rp The route parameters.
|
| 84 |
|
|
84 |
|
|
| 85 |
|
@return A task that completes with the routing result.
|
85 |
|
@return A task that completes with the routing result.
|
| 86 |
|
|
86 |
|
|
| 87 |
|
route_task operator()(route_params& rp) const;
|
87 |
|
route_task operator()(route_params& rp) const;
|
| 88 |
|
|
88 |
|
|
| 89 |
|
|
89 |
|
|
| 90 |
|
|
90 |
|
|
| 91 |
|
|
91 |
|
|
| 92 |
|
|
92 |
|
|
| 93 |
|
|
93 |
|
|