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
5
6#include <memory>
7#include <optional>
8#include <string>
9#include <vector>
10
14
21// Not needed here, but a lot of code depends on it being included transitively
23
24USERVER_NAMESPACE_BEGIN
25
28class HandlerAdapter;
29class HandlerMetrics;
30class Auth;
31} // namespace server::middlewares
32
34namespace server::handlers {
35
36class HttpHandlerStatistics;
37class HttpHandlerStatisticsAggregate;
38class HttpHandlerMethodStatistics;
39class HttpHandlerStatisticsScope;
40
58class HttpHandlerBase : public HandlerBase {
59public:
60 HttpHandlerBase(
61 const components::ComponentConfig& config,
62 const components::ComponentContext& component_context,
63 bool is_monitor = false
64 );
65
66 ~HttpHandlerBase() override;
67
69
71
73 virtual const std::string& HandlerName() const;
74
75 const std::vector<http::HttpMethod>& GetAllowedMethods() const;
76
78 // For internal use only.
79 HttpHandlerStatisticsAggregate& GetHandlerStatistics() const;
81
85
86 virtual FormattedErrorData GetFormattedExternalErrorBody(const CustomHandlerException& exc) const;
87
88 std::string GetResponseDataForLoggingChecked(
91 const std::string& response_data
92 ) const;
93
94 std::string GetUrlForLoggingChecked(const http::HttpRequest& request, request::RequestContext& context) const;
95
101 const CustomHandlerException& ex
102 ) const;
103
109 const std::exception& ex
110 ) const;
111
113 void LogUnknownException(const std::exception& ex, std::optional<logging::Level> log_level_override = {}) const;
114
116 const std::optional<logging::Level>& GetLogLevel() const;
117
119
126
127protected:
132
133 [[noreturn]] void ThrowUnsupportedHttpMethod(const http::HttpRequest& request) const;
134
136 virtual std::string HandleRequestThrow(const http::HttpRequest& request, request::RequestContext& context) const;
137
141 virtual std::string HandleRequest(http::HttpRequest& request, request::RequestContext& context) const;
142
156 virtual void
158 const;
159
161 bool IsBodyStreamingEnabledInConfig() const noexcept { return is_body_streamed_; }
162
165 virtual bool IsMethodStatisticIncluded() const { return false; }
166
169 virtual bool NeedCheckAuth() const { return true; }
170
172 virtual std::string GetRequestBodyForLogging(
175 const std::string& request_body
176 ) const;
177
179 virtual std::string GetResponseDataForLogging(
182 const std::string& response_data
183 ) const;
184
186 virtual std::string GetUrlForLogging(const http::HttpRequest& request, request::RequestContext& context) const;
187
191
192 virtual std::string GetMetaType(const http::HttpRequest&) const;
193
194private:
195 friend class middlewares::HandlerAdapter;
196 friend class middlewares::HandlerMetrics;
197 friend class middlewares::Auth;
198
199 void HandleHttpRequest(http::HttpRequest& request, request::RequestContext& context) const;
200
201 std::string GetRequestBodyForLoggingChecked(
204 const std::string& request_body
205 ) const;
206
207 template <typename HttpStatistics>
208 void FormatStatistics(utils::statistics::Writer result, const HttpStatistics& stats);
209
210 void FormatPerLabelStatistics(utils::statistics::Writer result) const;
211
212 void SetResponseServerHostname(http::HttpResponse& response) const;
213
214 void BuildMiddlewarePipeline(const components::ComponentConfig&, const components::ComponentContext&);
215
216 const dynamic_config::Source config_source_;
217 const std::vector<http::HttpMethod> allowed_methods_;
218 const std::string handler_name_;
219 std::optional<logging::Level> log_level_;
220 std::unordered_map<int, logging::Level> log_level_for_status_codes_;
221
222 std::unique_ptr<HttpHandlerStatisticsAggregate> handler_statistics_;
223
224 bool set_response_server_hostname_;
225 bool is_body_streamed_;
226
227 std::unique_ptr<middlewares::HttpMiddlewareBase> first_middleware_;
228};
229
230} // namespace server::handlers
231
232USERVER_NAMESPACE_END