userver: userver/server/middlewares/cors.hpp Source File
Loading...
Searching...
No Matches
cors.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/server/middlewares/cors.hpp
4/// @brief CORS (Cross-Origin Resource Sharing) middleware
5
6#include <string>
7#include <vector>
8
9#include <userver/formats/parse/to.hpp>
10#include <userver/http/common_headers.hpp>
11#include <userver/server/http/http_method.hpp>
12#include <userver/server/middlewares/builtin.hpp>
13#include <userver/server/middlewares/http_middleware_base.hpp>
14#include <userver/yaml_config/schema.hpp>
15
16USERVER_NAMESPACE_BEGIN
17
18namespace server::middlewares {
19
20/// @brief CORS (Cross-Origin Resource Sharing) middleware
21///
22/// This middleware handles CORS preflight requests and adds appropriate CORS headers
23/// to responses to allow cross-origin requests from web browsers.
24///
25/// @note The middleware is usually created by @ref server::middlewares::CorsFactory
26class Cors final : public HttpMiddlewareBase {
27public:
28 struct Config {
29 /// Allowed origins. Use "*" to allow all origins (not recommended for production).
30 /// The "*" wildcard can not be combined with `allow_credentials`
31 std::vector<std::string> allowed_origins;
32
33 /// Allowed HTTP methods
34 std::vector<std::string> allowed_methods = {
39 ToString(http::HttpMethod::kDelete),
41 ToString(http::HttpMethod::kOptions),
42 };
43
44 /// Allowed headers
45 std::vector<std::string> allowed_headers = {
46 std::string(USERVER_NAMESPACE::http::headers::kAccept),
47 std::string(USERVER_NAMESPACE::http::headers::kAcceptLanguage),
48 std::string(USERVER_NAMESPACE::http::headers::kContentLanguage),
49 std::string(USERVER_NAMESPACE::http::headers::kContentType),
50 };
51
52 /// Headers that can be exposed to the browser
53 std::vector<std::string> exposed_headers;
54
55 /// Whether to allow credentials (cookies, authorization headers)
56 bool allow_credentials{false};
57
58 /// Maximum age for preflight cache
59 std::chrono::seconds max_age{std::chrono::hours(24)};
60 };
61
62 explicit Cors(const Config& config);
63
64private:
65 void HandleRequest(http::HttpRequest& request, request::RequestContext& context) const override;
66
67 bool IsPreflightRequest(const http::HttpRequest& request) const;
68 void HandlePreflightRequest(http::HttpRequest& request) const;
69 void AddCorsHeaders(http::HttpRequest& request, const std::string& origin) const;
70 bool IsOriginAllowed(const std::string& origin) const;
71 const std::string& GetOriginHeader(const http::HttpRequest& request) const;
72
73 const Config config_;
74};
75
76/// @ingroup userver_components
77///
78/// @brief Factory for @ref server::middlewares::Cors
79///
80/// The middleware handles CORS preflight requests and adds appropriate CORS headers
81/// to responses to allow cross-origin requests from web browsers.
82/// For additional information about CORS please see https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CORS.
83///
84/// ## Static options of `CorsFactory`:
85///
86/// @include{doc} scripts/docs/en/components_schema/core/src/server/middlewares/cors.md
87///
88/// Options inherited from @ref components::RawComponentBase :
89/// @include{doc} scripts/docs/en/components_schema/core/src/components/impl/component_base.md
90///
91/// @see @ref server::middlewares::Cors
92class CorsFactory final : public HttpMiddlewareFactoryBase {
93public:
94 /// @ingroup userver_component_names
95 /// @brief The default name of @ref server::middlewares::CorsFactory component
96 static constexpr std::string_view kName = "cors-middleware";
97
98 CorsFactory(const components::ComponentConfig&, const components::ComponentContext&);
99
100 static yaml_config::Schema GetStaticConfigSchema();
101
102 yaml_config::Schema GetMiddlewareConfigSchema() const override;
103
104private:
105 std::unique_ptr<HttpMiddlewareBase> Create(
106 const handlers::HttpHandlerBase& handler,
107 yaml_config::YamlConfig middleware_config
108 ) const override;
109
110 const yaml_config::YamlConfig global_config_;
111};
112
113/// Parse CORS configuration from YAML
114Cors::Config Parse(const yaml_config::YamlConfig& value, formats::parse::To<Cors::Config>);
115
116} // namespace server::middlewares
117
118template <>
119inline constexpr bool components::kHasValidate<server::middlewares::CorsFactory> = true;
120
121template <>
122inline constexpr auto components::kConfigFileMode<server::middlewares::CorsFactory> = ConfigFileMode::kRequired;
123
124USERVER_NAMESPACE_END