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/loggable_component_base.hpp>
7#include <userver/server/handlers/exceptions.hpp>
8#include <userver/server/handlers/handler_config.hpp>
9#include <userver/server/request/request_base.hpp>
10#include <userver/server/request/request_context.hpp>
11
12USERVER_NAMESPACE_BEGIN
13
14namespace server::handlers {
15
16// clang-format off
17
18/// @brief Base class for the request handlers.
19///
20/// Each handler has an associated path and invoked only for the requests for
21/// that path.
22///
23/// ## Static options:
24/// Name | Description | Default value
25/// ---- | ----------- | -------------
26/// path | if a request matches this path wildcard then process it by handler | -
27/// as_fallback | set to "implicit-http-options" and do not specify a path if this handler processes the OPTIONS requests for paths that do not process OPTIONS method | -
28/// task_processor | a task processor to execute the requests | -
29/// method | comma-separated list of allowed HTTP methods. HEAD method is implicitly enabled if GET method is enabled | -
30/// max_request_size | max size of the whole request | 1024 * 1024
31/// max_headers_size | max request headers size | 65536
32/// parse_args_from_body | optional field to parse request according to x-www-form-urlencoded rules and make parameters accessible as query parameters | false
33/// auth | server::handlers::auth::HandlerAuthConfig authorization config | -
34/// url_trailing_slash | 'both' to treat URLs with and without a trailing slash as equal, 'strict-match' otherwise | 'both'
35/// max_requests_in_flight | integer to limit max pending requests to this handler | <no limit>
36/// request_body_size_log_limit | trim request to this size before logging | 512
37/// response_data_size_log_limit | trim responses to this size before logging | 512
38/// max_requests_per_second | integer to limit RPS to this handler | <no limit>
39/// decompress_request | allow decompression of the requests | true
40/// throttling_enabled | allow throttling of the requests by components::Server , for more info see its `max_response_size_in_flight` and `requests_queue_size_threshold` options | true
41/// set-response-server-hostname | set to true to add the `X-YaTaxi-Server-Hostname` header with instance name, set to false to not add the header | <takes the value from components::Server config>
42/// monitor-handler | Overrides the in-code `is_monitor` flag that makes the handler run either on `server.listener` or on `server.listener-monitor` | --
43/// set_tracing_headers | whether to set http tracing headers (X-YaTraceId, X-YaSpanId, X-RequestId) | true
44/// deadline_propagation_enabled | when `false`, disables HTTP handler @ref scripts/docs/en/userver/deadline_propagation.md "deadline propagation" | true
45/// deadline_expired_status_code | the HTTP status code to return if the request @ref scripts/docs/en/userver/deadline_propagation.md "deadline expires" | 498
46
47// clang-format on
49 public:
50 HandlerBase(const components::ComponentConfig& config,
51 const components::ComponentContext& component_context,
52 bool is_monitor = false);
53 ~HandlerBase() noexcept override = default;
54
55 /// Parses request, executes processing routines, and fills response
56 /// accordingly. Does not throw.
57 virtual void HandleRequest(request::RequestBase& request,
58 request::RequestContext& context) const = 0;
59
60 /// Produces response to a request unrecognized by the protocol based on
61 /// provided generic response. Does not throw.
63
64 /// Returns whether this is a monitoring handler.
65 bool IsMonitor() const { return is_monitor_; }
66
67 /// Returns handler config.
68 const HandlerConfig& GetConfig() const;
69
70 static yaml_config::Schema GetStaticConfigSchema();
71
72 protected:
73 // Pull the type names in the handler's scope to shorten throwing code
74 using HandlerErrorCode = handlers::HandlerErrorCode;
75 using InternalMessage = handlers::InternalMessage;
76 using ExternalBody = handlers::ExternalBody;
77
78 using ClientError = handlers::ClientError;
79 using InternalServerError = handlers::InternalServerError;
80
81 private:
82 bool is_monitor_;
83 HandlerConfig config_;
84};
85
86} // namespace server::handlers
87
88USERVER_NAMESPACE_END