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