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// clang-format off
27/// @ingroup userver_components userver_http_handlers
28///
29/// @brief A "magical" handler that will respond to OPTIONS HTTP method for any
30/// other handler that cannot handle OPTIONS itself.
31///
32/// It returns `Allow` header, listing all the methods that are supported
33/// for the given HTTP path, including OPTIONS itself.
34///
35/// ## Auth checker testing
36///
37/// In addition to the above RFC behavior, an optional extension is supported.
38/// If `auth_checkers` static config option is specified,
39/// http::headers::kXYaTaxiAllowAuthRequest header can be passed containing
40/// the checker key in the static config.
41///
42/// The request will then be passed as-is to the auth checker.
43/// The result of the check will be returned in
44/// http::headers::kXYaTaxiAllowAuthResponse header. The following responses are
45/// possible:
46///
47/// * `unknown checker` if the checker is not found
48/// * `OK` if the check succeeds
49/// * an unspecified error message if the check fails
50///
51/// ## Static options
52///
53/// The component has no service configuration except the
54/// @ref userver_http_handlers "common handler options".
55/// and adds the following ones:
56///
57/// Name | Description | Default value
58/// ---- | ----------- | -------------
59/// auth | server::handlers::auth::HandlerAuthConfig authorization config | auth checker testing is disabled
60///
61/// ## Static configuration example:
62///
63/// @snippet components/common_server_component_list_test.cpp Sample handler implicit http options component config
64///
65/// ## Scheme
66/// Provide an optional query parameter `body` to get the bodies of all the
67/// in-flight requests.
68// clang-format on
69class ImplicitOptions /*non-final*/ : public HttpHandlerBase {
70 public:
71 /// @ingroup userver_component_names
72 /// @brief The default name of server::handlers::ImplicitOptions component
73 static constexpr std::string_view kName = "handler-implicit-http-options";
74
75 ImplicitOptions(const components::ComponentConfig& config,
76 const components::ComponentContext& component_context,
77 bool is_monitor = false);
78
79 ~ImplicitOptions() override;
80
81 std::string HandleRequestThrow(
82 const server::http::HttpRequest& request,
83 server::request::RequestContext& context) const override;
84
85 static yaml_config::Schema GetStaticConfigSchema();
86
87 private:
88 using AuthCheckers =
90
91 std::string ExtractAllowedMethods(const std::string& path) const;
92
93 const http::HandlerInfoIndex& GetHandlerInfoIndex() const;
94
95 const Server& server_;
96 const AuthCheckers auth_checkers_;
97
98 mutable engine::Mutex handler_info_index_mutex_;
99 mutable const http::HandlerInfoIndex* handler_info_index_ = nullptr;
100};
101
102} // namespace server::handlers
103
104template <>
105inline constexpr bool
106 components::kHasValidate<server::handlers::ImplicitOptions> = true;
107
108USERVER_NAMESPACE_END