userver: userver/server/handlers/implicit_options.hpp Source File
Loading...
Searching...
No Matches
implicit_options.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/server/handlers/implicit_options.hpp
4/// @brief @copybrief server::handlers::ImplicitOptions
5
6#include <userver/engine/mutex.hpp>
7#include <userver/server/handlers/http_handler_base.hpp>
8
9USERVER_NAMESPACE_BEGIN
10
11namespace server {
12class Server;
13} // namespace server
14
15namespace server::http {
16class HandlerInfoIndex;
17} // namespace server::http
18
19namespace server::handlers::auth {
20class AuthCheckerBase;
21using AuthCheckerBasePtr = std::shared_ptr<AuthCheckerBase>;
22} // namespace server::handlers::auth
23
24namespace server::handlers {
25
26/// @ingroup userver_components userver_http_handlers
27///
28/// @brief A "magical" handler that will respond to OPTIONS HTTP method for any
29/// other handler that cannot handle OPTIONS itself.
30///
31/// It returns `Allow` header, listing all the methods that are supported
32/// for the given HTTP path, including OPTIONS itself.
33///
34/// ## Auth checker testing
35///
36/// In addition to the above RFC behavior, an optional extension is supported.
37/// If `auth_checkers` static config option is specified,
38/// http::headers::kXYaTaxiAllowAuthRequest header can be passed containing
39/// the checker key in the static config.
40///
41/// The request will then be passed as-is to the auth checker.
42/// The result of the check will be returned in
43/// http::headers::kXYaTaxiAllowAuthResponse header. The following responses are
44/// possible:
45///
46/// * `unknown checker` if the checker is not found
47/// * `OK` if the check succeeds
48/// * an unspecified error message if the check fails
49///
50/// ## Static options of server::handlers::ImplicitOptions :
51/// @include{doc} scripts/docs/en/components_schema/core/src/server/handlers/implicit_options.md
52///
53/// Options inherited from @ref server::handlers::HttpHandlerBase :
54/// @include{doc} scripts/docs/en/components_schema/core/src/server/handlers/http_handler_base.md
55///
56/// Options inherited from @ref server::handlers::HandlerBase :
57/// @include{doc} scripts/docs/en/components_schema/core/src/server/handlers/handler_base.md
58///
59/// Options inherited from @ref components::ComponentBase :
60/// @include{doc} scripts/docs/en/components_schema/core/src/components/impl/component_base.md
61///
62/// ## Static configuration example:
63///
64/// @snippet components/common_server_component_list_test.cpp Sample handler implicit http options component config
65///
66/// ## Scheme
67/// Provide an optional query parameter `body` to get the bodies of all the in-flight requests.
68class ImplicitOptions /*non-final*/ : public HttpHandlerBase {
69public:
70 /// @ingroup userver_component_names
71 /// @brief The default name of server::handlers::ImplicitOptions component
72 static constexpr std::string_view kName = "handler-implicit-http-options";
73
74 ImplicitOptions(
75 const components::ComponentConfig& config,
76 const components::ComponentContext& component_context,
77 bool is_monitor = false
78 );
79
80 ~ImplicitOptions() override;
81
82 std::string HandleRequestThrow(const server::http::HttpRequest& request, server::request::RequestContext& context)
83 const override;
84
85 static yaml_config::Schema GetStaticConfigSchema();
86
87private:
88 using AuthCheckers = std::unordered_map<std::string, auth::AuthCheckerBasePtr>;
89
90 std::string ExtractAllowedMethods(const std::string& path) const;
91
92 const http::HandlerInfoIndex& GetHandlerInfoIndex() const;
93
94 const Server& server_;
95 const AuthCheckers auth_checkers_;
96
97 mutable engine::Mutex handler_info_index_mutex_;
98 mutable const http::HandlerInfoIndex* handler_info_index_ = nullptr;
99};
100
101} // namespace server::handlers
102
103template <>
104inline constexpr bool components::kHasValidate<server::handlers::ImplicitOptions> = true;
105
106USERVER_NAMESPACE_END