userver: userver/server/middlewares/http_middleware_base.hpp Source File
Loading...
Searching...
No Matches
http_middleware_base.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/server/middlewares/http_middleware_base.hpp
4/// @brief Base classes for implementing custom middlewares
5
6#include <memory>
7
8#include <userver/components/component_base.hpp>
9#include <userver/yaml_config/schema.hpp>
10#include <userver/yaml_config/yaml_config.hpp>
11
12USERVER_NAMESPACE_BEGIN
13
14namespace server {
15
16namespace request {
17class RequestContext;
18}
19
20namespace http {
21class HttpRequest;
22}
23
24namespace handlers {
25class HttpHandlerBase;
26}
27
28/// HTTP server middlewares
29namespace middlewares {
30
31/// @ingroup userver_middlewares userver_base_classes
32///
33/// @brief Base class for a http middleware
35public:
36 HttpMiddlewareBase();
37 virtual ~HttpMiddlewareBase();
38
39protected:
40 /// @brief Override this method to implement a middleware logic
41 virtual void HandleRequest(http::HttpRequest& request, request::RequestContext& context) const = 0;
42
43 /// @brief The method to invoke the next middleware in a pipeline
44 void Next(http::HttpRequest& request, request::RequestContext& context) const;
45
46private:
47 friend class handlers::HttpHandlerBase;
48
49 std::unique_ptr<HttpMiddlewareBase> next_{nullptr};
50};
51
52/// @ingroup userver_middlewares userver_base_classes
53///
54/// @brief Base class for a http middleware-factory
56public:
57 HttpMiddlewareFactoryBase(const components::ComponentConfig&, const components::ComponentContext&);
58
59 std::unique_ptr<HttpMiddlewareBase>
60 CreateChecked(const handlers::HttpHandlerBase& handler, yaml_config::YamlConfig middleware_config) const;
61
62protected:
63 /// @brief This method should return the schema of a middleware configuration,
64 /// if any. You may override it
65 virtual yaml_config::Schema GetMiddlewareConfigSchema() const { return yaml_config::Schema::EmptyObject(); }
66
67 /// @brief Override this method to create an instance of a middleware
70};
71
72/// @ingroup userver_middlewares
73///
74/// @brief A short-cut for defining a middleware-factory
75template <typename Middleware>
76class SimpleHttpMiddlewareFactory final : public HttpMiddlewareFactoryBase {
77public:
78 static constexpr std::string_view kName = Middleware::kName;
79
80 using HttpMiddlewareFactoryBase::HttpMiddlewareFactoryBase;
81
82private:
83 std::unique_ptr<HttpMiddlewareBase> Create(const handlers::HttpHandlerBase& handler, yaml_config::YamlConfig)
84 const override {
85 return std::make_unique<Middleware>(handler);
86 }
87};
88
89} // namespace middlewares
90} // namespace server
91
92template <typename Middleware>
93inline constexpr bool components::kHasValidate<server::middlewares::SimpleHttpMiddlewareFactory<Middleware>> = true;
94
95template <typename Middleware>
96inline constexpr auto components::kConfigFileMode<server::middlewares::SimpleHttpMiddlewareFactory<Middleware>> =
98
99USERVER_NAMESPACE_END