userver: userver/server/handlers/handler_base.hpp Source File
Loading...
Searching...
No Matches
handler_base.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/server/handlers/handler_base.hpp
4/// @brief @copybrief server::handlers::HandlerBase
5
6#include <userver/components/component_base.hpp>
7#include <userver/server/handlers/exceptions.hpp>
8#include <userver/server/handlers/handler_config.hpp>
9#include <userver/server/http/http_request.hpp>
10#include <userver/server/request/request_context.hpp>
11
12USERVER_NAMESPACE_BEGIN
13
14namespace server::handlers {
15
16/// @brief Base class for the request handlers.
17///
18/// Each handler has an associated path and invoked only for the requests for that path.
19///
20/// ## Static options of server::handlers::HandlerBase :
21/// @include{doc} scripts/docs/en/components_schema/core/src/server/handlers/handler_base.md
22///
23/// Options inherited from @ref components::ComponentBase :
24/// @include{doc} scripts/docs/en/components_schema/core/src/components/impl/component_base.md
26public:
27 HandlerBase(
28 const components::ComponentConfig& config,
29 const components::ComponentContext& component_context,
30 bool is_monitor = false
31 );
32 ~HandlerBase() noexcept override = default;
33
34 /// Parses request, executes processing routines, and fills response
35 /// accordingly. Does not throw.
36 virtual void PrepareAndHandleRequest(http::HttpRequest& request, request::RequestContext& context) const = 0;
37
38 /// Produces response to a request unrecognized by the protocol based on
39 /// provided generic response. Does not throw.
40 /// The error should be stored into response status and body prior to this call.
41 virtual void ReportMalformedRequest(http::HttpRequest&) const {}
42
43 /// Returns whether this is a monitoring handler.
44 bool IsMonitor() const { return is_monitor_; }
45
46 /// Returns handler config.
47 const HandlerConfig& GetConfig() const;
48
49 static yaml_config::Schema GetStaticConfigSchema();
50
51protected:
52 // Pull the type names in the handler's scope to shorten throwing code
53 using HandlerErrorCode = handlers::HandlerErrorCode;
54 using InternalMessage = handlers::InternalMessage;
55 using ExternalBody = handlers::ExternalBody;
56
57 using ClientError = handlers::ClientError;
58 using InternalServerError = handlers::InternalServerError;
59
60private:
61 bool is_monitor_;
62 HandlerConfig config_;
63};
64
65} // namespace server::handlers
66
67USERVER_NAMESPACE_END