userver: userver/server/handlers/http_handler_base.hpp Source File
Loading...
Searching...
No Matches
http_handler_base.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/server/handlers/http_handler_base.hpp
4/// @brief @copybrief server::handlers::HttpHandlerBase
5
6#include <memory>
7#include <optional>
8#include <string>
9#include <vector>
10
11#include <userver/dynamic_config/source.hpp>
12#include <userver/logging/level.hpp>
13#include <userver/utils/statistics/entry.hpp>
14#include <userver/utils/token_bucket.hpp>
15
16#include <userver/server/handlers/exceptions.hpp>
17#include <userver/server/handlers/formatted_error_data.hpp>
18#include <userver/server/handlers/handler_base.hpp>
19#include <userver/server/http/http_request.hpp>
20#include <userver/server/http/http_response.hpp>
21#include <userver/server/http/http_response_body_stream_fwd.hpp>
22#include <userver/server/request/request_base.hpp>
23// Not needed here, but a lot of code depends on it being included transitively
24#include <userver/tracing/span.hpp>
25
26USERVER_NAMESPACE_BEGIN
27
28namespace server::middlewares {
30class HandlerAdapter;
31class Auth;
32} // namespace server::middlewares
33
34/// @brief Most common \ref userver_http_handlers "userver HTTP handlers"
35namespace server::handlers {
36
37class HttpHandlerStatistics;
38class HttpRequestStatistics;
39class HttpHandlerMethodStatistics;
40class HttpHandlerStatisticsScope;
41
42// clang-format off
43
44/// @ingroup userver_components userver_http_handlers userver_base_classes
45///
46/// @brief Base class for all the
47/// \ref userver_http_handlers "Userver HTTP Handlers".
48///
49/// ## Static options:
50/// Inherits all the options from server::handlers::HandlerBase and adds the
51/// following ones:
52///
53/// Name | Description | Default value
54/// ---- | ----------- | -------------
55/// log-level | overrides log level for this handle | <no override>
56/// status-codes-log-level | map of "status": log_level items to override span log level for specific status codes | {}
57/// middlewares.pipeline-builder | name of a component to build a middleware pipeline for this particular handler | default-handler-middleware-pipeline-builder
58///
59/// ## Example usage:
60///
61/// @include samples/hello_service/src/hello_handler.hpp
62/// @include samples/hello_service/src/hello_handler.cpp
63
64// clang-format on
65
67public:
68 HttpHandlerBase(
69 const components::ComponentConfig& config,
70 const components::ComponentContext& component_context,
71 bool is_monitor = false
72 );
73
74 ~HttpHandlerBase() override;
75
76 void HandleRequest(request::RequestBase& request, request::RequestContext& context) const override;
77
78 void ReportMalformedRequest(request::RequestBase& request) const final;
79
80 virtual const std::string& HandlerName() const;
81
82 const std::vector<http::HttpMethod>& GetAllowedMethods() const;
83
84 /// @cond
85 // For internal use only.
86 HttpHandlerStatistics& GetHandlerStatistics() const;
87
88 // For internal use only.
89 HttpRequestStatistics& GetRequestStatistics() const;
90 /// @endcond
91
92 /// Override it if you need a custom logging level for messages about finish
93 /// of request handling for some http statuses.
94 virtual logging::Level GetLogLevelForResponseStatus(http::HttpStatus status) const;
95
96 virtual FormattedErrorData GetFormattedExternalErrorBody(const CustomHandlerException& exc) const;
97
98 std::string GetResponseDataForLoggingChecked(
99 const http::HttpRequest& request,
100 request::RequestContext& context,
101 const std::string& response_data
102 ) const;
103
104 /// Takes the exception and formats it into response, as specified by
105 /// exception.
106 void HandleCustomHandlerException(const http::HttpRequest& request, const CustomHandlerException& ex) const;
107
108 /// Takes the exception and formats it into response as an internal server
109 /// error.
110 void HandleUnknownException(const http::HttpRequest& request, const std::exception& ex) const;
111
112 /// Helper function to log an unknown exception
113 void LogUnknownException(const std::exception& ex, std::optional<logging::Level> log_level_override = {}) const;
114
115 /// Returns the default log level for the handler
116 const std::optional<logging::Level>& GetLogLevel() const;
117
118 static yaml_config::Schema GetStaticConfigSchema();
119
120protected:
121 [[noreturn]] void ThrowUnsupportedHttpMethod(const http::HttpRequest& request) const;
122
123 /// The core method for HTTP request handling.
124 /// `request` arg contains HTTP headers, full body, etc.
125 /// The method should return response body.
126 /// @note It is used only if IsStreamed() returned `false`.
127 virtual std::string HandleRequestThrow(const http::HttpRequest& request, request::RequestContext& context) const;
128
129 /// The core method for HTTP request handling.
130 /// `request` arg contains HTTP headers, full body, etc.
131 /// The response body is passed in parts to `ResponseBodyStream`.
132 /// Stream transmission is useful when:
133 /// 1) The body size is unknown beforehand.
134 /// 2) The client may take advantage of early body transmission
135 /// (e.g. a Web Browser may start rendering the HTML page
136 /// or downloading dependant resources).
137 /// 3) The body size is huge and we want to have only a part of it
138 /// in memory.
139 /// @note It is used only if IsStreamed() returned `true`.
140 virtual void
141 HandleStreamRequest(const server::http::HttpRequest&, server::request::RequestContext&, server::http::ResponseBodyStream&)
142 const;
143
144 /// If IsStreamed() returns `true`, call HandleStreamRequest()
145 /// for request handling, HandleRequestThrow() is not called.
146 /// If it returns `false`, HandleRequestThrow() is called instead,
147 /// and HandleStreamRequest() is not called.
148 /// @note The default implementation returns the cached value of
149 /// "response-body-streamed" value from static config.
150 virtual bool IsStreamed() const { return is_body_streamed_; }
151
152 /// Override it to show per HTTP-method statistics besides statistics for all
153 /// methods
154 virtual bool IsMethodStatisticIncluded() const { return false; }
155
156 /// Override it if you want to disable auth checks in handler by some
157 /// condition
158 virtual bool NeedCheckAuth() const { return true; }
159
160 /// Override it if you need a custom request body logging.
161 virtual std::string GetRequestBodyForLogging(
162 const http::HttpRequest& request,
163 request::RequestContext& context,
164 const std::string& request_body
165 ) const;
166
167 /// Override it if you need a custom response data logging.
168 virtual std::string GetResponseDataForLogging(
169 const http::HttpRequest& request,
170 request::RequestContext& context,
171 const std::string& response_data
172 ) const;
173
174 /// For internal use. You don't need to override it. This method is overridden
175 /// in format-specific base handlers.
176 virtual void ParseRequestData(const http::HttpRequest&, request::RequestContext&) const {}
177
178 virtual std::string GetMetaType(const http::HttpRequest&) const;
179
180private:
181 friend class middlewares::HandlerAdapter;
182 friend class middlewares::Auth;
183
184 void HandleHttpRequest(http::HttpRequest& request, request::RequestContext& context) const;
185
186 void HandleRequestStream(const http::HttpRequest& http_request, request::RequestContext& context) const;
187
188 std::string GetRequestBodyForLoggingChecked(
189 const http::HttpRequest& request,
190 request::RequestContext& context,
191 const std::string& request_body
192 ) const;
193
194 template <typename HttpStatistics>
195 void FormatStatistics(utils::statistics::Writer result, const HttpStatistics& stats);
196
197 void SetResponseServerHostname(http::HttpResponse& response) const;
198
199 void BuildMiddlewarePipeline(const components::ComponentConfig&, const components::ComponentContext&);
200
201 const dynamic_config::Source config_source_;
202 const std::vector<http::HttpMethod> allowed_methods_;
203 const std::string handler_name_;
204 utils::statistics::Entry statistics_holder_;
205 std::optional<logging::Level> log_level_;
206 std::unordered_map<int, logging::Level> log_level_for_status_codes_;
207
208 std::unique_ptr<HttpHandlerStatistics> handler_statistics_;
209 std::unique_ptr<HttpRequestStatistics> request_statistics_;
210
211 bool set_response_server_hostname_;
212 bool is_body_streamed_;
213
214 std::unique_ptr<middlewares::HttpMiddlewareBase> first_middleware_;
215};
216
217} // namespace server::handlers
218
219USERVER_NAMESPACE_END