Github   Telegram
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Protected Member Functions | List of all members
server::handlers::HttpHandlerBase Class Reference

Base class for all the Userver HTTP Handlers. More...

#include <userver/server/handlers/http_handler_base.hpp>

+ Inheritance diagram for server::handlers::HttpHandlerBase:
+ Collaboration diagram for server::handlers::HttpHandlerBase:

Public Member Functions

 HttpHandlerBase (const components::ComponentConfig &config, const components::ComponentContext &component_context, bool is_monitor=false)
 
void HandleRequest (request::RequestBase &request, request::RequestContext &context) const override
 
void ReportMalformedRequest (request::RequestBase &request) const final
 
virtual const std::string & HandlerName () const
 
const std::vector< http::HttpMethod > & GetAllowedMethods () const
 
virtual logging::Level GetLogLevelForResponseStatus (http::HttpStatus status) const
 
virtual FormattedErrorData GetFormattedExternalErrorBody (const CustomHandlerException &exc) const
 
std::string GetResponseDataForLoggingChecked (const http::HttpRequest &request, request::RequestContext &context, const std::string &response_data) const
 
- Public Member Functions inherited from server::handlers::HandlerBase
 HandlerBase (const components::ComponentConfig &config, const components::ComponentContext &component_context, bool is_monitor=false)
 
virtual void HandleRequest (request::RequestBase &request, request::RequestContext &context) const =0
 
virtual void ReportMalformedRequest (request::RequestBase &) const
 
bool IsMonitor () const
 Returns whether this is a monitoring handler. More...
 
const HandlerConfigGetConfig () const
 Returns handler config.
 
- Public Member Functions inherited from components::LoggableComponentBase
 LoggableComponentBase (const ComponentConfig &, const ComponentContext &)
 
 LoggableComponentBase (LoggableComponentBase &&)=delete
 
 LoggableComponentBase (const LoggableComponentBase &)=delete
 
 ~LoggableComponentBase () override=default
 
ComponentHealth GetComponentHealth () const override
 
void OnLoadingCancelled () override
 
void OnAllComponentsLoaded () override
 
void OnAllComponentsAreStopping () override
 

Static Public Member Functions

static yaml_config::Schema GetStaticConfigSchema ()
 
- Static Public Member Functions inherited from server::handlers::HandlerBase
static yaml_config::Schema GetStaticConfigSchema ()
 
- Static Public Member Functions inherited from components::LoggableComponentBase
static yaml_config::Schema GetStaticConfigSchema ()
 

Protected Member Functions

void ThrowUnsupportedHttpMethod (const http::HttpRequest &request) const
 
virtual std::string HandleRequestThrow (const http::HttpRequest &request, request::RequestContext &context) const
 
virtual void OnRequestCompleteThrow (const http::HttpRequest &, request::RequestContext &) const
 
virtual void HandleStreamRequest (const server::http::HttpRequest &, server::request::RequestContext &, server::http::ResponseBodyStream &) const
 
virtual bool IsStreamed () const
 
virtual bool IsMethodStatisticIncluded () const
 
virtual bool NeedCheckAuth () const
 
virtual std::string GetRequestBodyForLogging (const http::HttpRequest &request, request::RequestContext &context, const std::string &request_body) const
 Override it if you need a custom request body logging. More...
 
virtual std::string GetResponseDataForLogging (const http::HttpRequest &request, request::RequestContext &context, const std::string &response_data) const
 Override it if you need a custom response data logging. More...
 
virtual void ParseRequestData (const http::HttpRequest &, request::RequestContext &) const
 
virtual std::string GetMetaType (const http::HttpRequest &) const
 

Additional Inherited Members

- Protected Types inherited from server::handlers::HandlerBase
using HandlerErrorCode = handlers::HandlerErrorCode
 
using InternalMessage = handlers::InternalMessage
 
using ExternalBody = handlers::ExternalBody
 
using ClientError = handlers::ClientError
 
using InternalServerError = handlers::InternalServerError
 

Detailed Description

Base class for all the Userver HTTP Handlers.

Static options:

Inherits all the options from server::handlers::HandlerBase and adds the following ones:

Name Description Default value
log-level overrides log level for this handle <no override>

Example usage:

namespace samples::hello {
class Hello final : public server::handlers::HttpHandlerBase {
public:
// `kName` is used as the component name in static config
static constexpr std::string_view kName = "handler-hello-sample";
// Component is valid after construction and is able to accept requests
using HttpHandlerBase::HttpHandlerBase;
std::string HandleRequestThrow(
return "Hello world!\n";
}
};
} // namespace samples::hello
Examples
samples/grpc_service/grpc_service.cpp, samples/hello_service/hello_service.cpp, samples/http_caching/http_caching.cpp, samples/mongo_service/mongo_service.cpp, samples/postgres_service/postgres_service.cpp, and samples/redis_service/redis_service.cpp.

Definition at line 55 of file http_handler_base.hpp.

Member Function Documentation

◆ GetLogLevelForResponseStatus()

virtual logging::Level server::handlers::HttpHandlerBase::GetLogLevelForResponseStatus ( http::HttpStatus  status) const
virtual

Override it if you need a custom logging level for messages about finish of request handling for some http statuses.

◆ GetRequestBodyForLogging()

virtual std::string server::handlers::HttpHandlerBase::GetRequestBodyForLogging ( const http::HttpRequest request,
request::RequestContext context,
const std::string &  request_body 
) const
protectedvirtual

Override it if you need a custom request body logging.

Reimplemented in server::handlers::HttpHandlerFlatbufBase< InputType, ReturnType >.

◆ GetResponseDataForLogging()

virtual std::string server::handlers::HttpHandlerBase::GetResponseDataForLogging ( const http::HttpRequest request,
request::RequestContext context,
const std::string &  response_data 
) const
protectedvirtual

Override it if you need a custom response data logging.

Reimplemented in server::handlers::HttpHandlerFlatbufBase< InputType, ReturnType >.

◆ HandleRequest()

void server::handlers::HttpHandlerBase::HandleRequest ( request::RequestBase request,
request::RequestContext context 
) const
overridevirtual

Parses request, executes processing routines, and fills response accordingly. Does not throw.

Implements server::handlers::HandlerBase.

◆ IsMethodStatisticIncluded()

virtual bool server::handlers::HttpHandlerBase::IsMethodStatisticIncluded ( ) const
inlineprotectedvirtual

Override it to show per HTTP-method statistics besides statistics for all methods

Definition at line 112 of file http_handler_base.hpp.

◆ IsStreamed()

virtual bool server::handlers::HttpHandlerBase::IsStreamed ( ) const
inlineprotectedvirtual

Definition at line 108 of file http_handler_base.hpp.

◆ NeedCheckAuth()

virtual bool server::handlers::HttpHandlerBase::NeedCheckAuth ( ) const
inlineprotectedvirtual

Override it if you want to disable auth checks in handler by some condition

Definition at line 116 of file http_handler_base.hpp.

◆ OnRequestCompleteThrow()

virtual void server::handlers::HttpHandlerBase::OnRequestCompleteThrow ( const http::HttpRequest ,
request::RequestContext  
) const
inlineprotectedvirtual

Definition at line 100 of file http_handler_base.hpp.

◆ ParseRequestData()

virtual void server::handlers::HttpHandlerBase::ParseRequestData ( const http::HttpRequest ,
request::RequestContext  
) const
inlineprotectedvirtual

For internal use. You don't need to override it. This method is overriden in format-specific base handlers.

Reimplemented in server::handlers::HttpHandlerFlatbufBase< InputType, ReturnType >, and server::handlers::HttpHandlerJsonBase.

Definition at line 130 of file http_handler_base.hpp.

◆ ReportMalformedRequest()

void server::handlers::HttpHandlerBase::ReportMalformedRequest ( request::RequestBase ) const
finalvirtual

Produces response to a request unrecognized by the protocol based on provided generic response. Does not throw.

Reimplemented from server::handlers::HandlerBase.


The documentation for this class was generated from the following file: