9#include <unordered_set>
12#include <userver/dynamic_config/source.hpp>
13#include <userver/logging/level.hpp>
14#include <userver/utils/statistics/entry.hpp>
15#include <userver/utils/token_bucket.hpp>
17#include <userver/server/handlers/exceptions.hpp>
18#include <userver/server/handlers/formatted_error_data.hpp>
19#include <userver/server/handlers/handler_base.hpp>
20#include <userver/server/http/http_request.hpp>
21#include <userver/server/http/http_response.hpp>
22#include <userver/server/http/http_response_body_stream_fwd.hpp>
23#include <userver/server/request/request_base.hpp>
25#include <userver/tracing/span.hpp>
27USERVER_NAMESPACE_BEGIN
36namespace server::handlers {
38class HttpHandlerStatistics;
39class HttpRequestStatistics;
40class HttpHandlerMethodStatistics;
41class HttpHandlerStatisticsScope;
69 HttpHandlerBase(
const components::ComponentConfig& config,
70 const components::ComponentContext& component_context,
71 bool is_monitor =
false);
73 ~HttpHandlerBase()
override;
76 request::RequestContext& context)
const override;
80 virtual const std::string& HandlerName()
const;
82 const std::vector<http::HttpMethod>& GetAllowedMethods()
const;
86 HttpHandlerStatistics& GetHandlerStatistics()
const;
89 HttpRequestStatistics& GetRequestStatistics()
const;
95 http::HttpStatus status)
const;
100 std::string GetResponseDataForLoggingChecked(
101 const http::HttpRequest& request,
request::RequestContext& context,
102 const std::string& response_data)
const;
112 const std::exception& ex)
const;
116 const std::exception& ex,
117 std::optional<
logging::
Level> log_level_override = {})
const;
122 static yaml_config::Schema GetStaticConfigSchema();
125 [[
noreturn]]
void ThrowUnsupportedHttpMethod(
126 const http::HttpRequest& request)
const;
133 const http::HttpRequest& request,
request::RequestContext& context)
const;
147 server::
request::RequestContext&,
148 server::
http::ResponseBodyStream&)
const;
156 virtual bool IsStreamed()
const {
return is_body_streamed_; }
168 const http::HttpRequest& request,
request::RequestContext& context,
169 const std::string& request_body)
const;
173 const http::HttpRequest& request,
request::RequestContext& context,
174 const std::string& response_data)
const;
179 request::RequestContext&)
const {}
181 virtual std::string GetMetaType(
const http::HttpRequest&)
const;
187 void HandleHttpRequest(
http::HttpRequest& request,
188 request::RequestContext& context)
const;
190 void HandleRequestStream(
const http::HttpRequest& http_request,
191 request::RequestContext& context)
const;
193 std::string GetRequestBodyForLoggingChecked(
194 const http::HttpRequest& request,
request::RequestContext& context,
195 const std::string& request_body)
const;
197 template <
typename HttpStatistics>
198 void FormatStatistics(utils::statistics::Writer result,
199 const HttpStatistics& stats);
201 void SetResponseServerHostname(
http::HttpResponse& response)
const;
203 void BuildMiddlewarePipeline(
const components::ComponentConfig&,
206 const dynamic_config::Source config_source_;
207 const std::vector<http::HttpMethod> allowed_methods_;
208 const std::string handler_name_;
209 utils::statistics::Entry statistics_holder_;
211 std::unordered_map<
int, logging::Level> log_level_for_status_codes_;
213 std::unique_ptr<HttpHandlerStatistics> handler_statistics_;
214 std::unique_ptr<HttpRequestStatistics> request_statistics_;
216 bool set_response_server_hostname_;
217 bool is_body_streamed_;
219 std::unique_ptr<middlewares::HttpMiddlewareBase> first_middleware_;