userver: server::handlers::HttpHandlerFlatbufBase< InputType, ReturnType > Class Template Reference
Loading...
Searching...
No Matches
server::handlers::HttpHandlerFlatbufBase< InputType, ReturnType > Class Template Referenceabstract

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

Detailed Description

template<typename InputType, typename ReturnType>
class server::handlers::HttpHandlerFlatbufBase< InputType, ReturnType >

Convenient base for handlers that accept requests with body in Flatbuffer format and respond with body in Flatbuffer format.

Example usage:

#include "flatbuffer_schema.fbs.h"
namespace samples::fbs_handle {
class FbsSumEcho final : public server::handlers::HttpHandlerFlatbufBase<fbs::SampleRequest, fbs::SampleResponse> {
public:
// `kName` is used as the component name in static config
static constexpr std::string_view kName = "handler-fbs-sample";
// Component is valid after construction and is able to accept requests
FbsSumEcho(const components::ComponentConfig& config, const components::ComponentContext& context)
: HttpHandlerFlatbufBase(config, context) {}
fbs::SampleResponse::NativeTableType
HandleRequestFlatbufThrow(const server::http::HttpRequest& request, const fbs::SampleRequest::NativeTableType& fbs_request, server::request::RequestContext&)
const override {
request.GetHttpResponse().SetContentType(http::content_type::kApplicationOctetStream);
fbs::SampleResponse::NativeTableType res;
res.sum = fbs_request.arg1 + fbs_request.arg2;
res.echo = fbs_request.data;
return res;
}
};
} // namespace samples::fbs_handle
Examples
samples/flatbuf_service/flatbuf_service.cpp.

Definition at line 40 of file http_handler_flatbuf_base.hpp.

+ Inheritance diagram for server::handlers::HttpHandlerFlatbufBase< InputType, ReturnType >:

Public Member Functions

 HttpHandlerFlatbufBase (const components::ComponentConfig &config, const components::ComponentContext &component_context)
 
std::string HandleRequestThrow (const http::HttpRequest &request, request::RequestContext &context) const final
 
virtual ReturnType::NativeTableType HandleRequestFlatbufThrow (const http::HttpRequest &request, const typename InputType::NativeTableType &input, request::RequestContext &context) const =0
 
const InputType::NativeTableType * GetInputData (const request::RequestContext &context) const
 
const ReturnType::NativeTableType * GetOutputData (const request::RequestContext &context) const
 
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
 
void HandleCustomHandlerException (const http::HttpRequest &request, const CustomHandlerException &ex) const
 
void HandleUnknownException (const http::HttpRequest &request, const std::exception &ex) const
 
void LogUnknownException (const std::exception &ex, std::optional< logging::Level > log_level_override={}) const
 Helper function to log an unknown exception.
 
const std::optional< logging::Level > & GetLogLevel () const
 Returns the default log level for the handler.
 
bool IsMonitor () const
 Returns whether this is a monitoring handler.
 
const HandlerConfigGetConfig () const
 Returns handler config.
 
ComponentHealth GetComponentHealth () const override
 
void OnLoadingCancelled () override
 
void OnAllComponentsLoaded () override
 
void OnAllComponentsAreStopping () override
 

Static Public Member Functions

static yaml_config::Schema GetStaticConfigSchema ()
 

Protected Types

using HandlerErrorCode = handlers::HandlerErrorCode
 
using InternalMessage = handlers::InternalMessage
 
using ExternalBody = handlers::ExternalBody
 
using ClientError = handlers::ClientError
 
using InternalServerError = handlers::InternalServerError
 
using LoggableComponentBase = ComponentBase
 Legacy alias, use ComponentBase instead.
 

Protected Member Functions

std::string GetRequestBodyForLogging (const http::HttpRequest &request, request::RequestContext &context, const std::string &request_body) const override
 Override it if you need a custom request body logging.
 
std::string GetResponseDataForLogging (const http::HttpRequest &request, request::RequestContext &context, const std::string &response_data) const override
 Override it if you need a custom response data logging.
 
void ParseRequestData (const http::HttpRequest &request, request::RequestContext &context) const final
 
void ThrowUnsupportedHttpMethod (const http::HttpRequest &request) 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 GetMetaType (const http::HttpRequest &) const
 

Member Typedef Documentation

◆ ClientError

Definition at line 80 of file handler_base.hpp.

◆ ExternalBody

Definition at line 78 of file handler_base.hpp.

◆ HandlerErrorCode

◆ InternalMessage

Definition at line 77 of file handler_base.hpp.

◆ InternalServerError

◆ LoggableComponentBase

using components::ComponentBase::LoggableComponentBase = ComponentBase
protectedinherited

Legacy alias, use ComponentBase instead.

Definition at line 67 of file component_base.hpp.

Constructor & Destructor Documentation

◆ HttpHandlerFlatbufBase()

template<typename InputType , typename ReturnType >
server::handlers::HttpHandlerFlatbufBase< InputType, ReturnType >::HttpHandlerFlatbufBase ( const components::ComponentConfig & config,
const components::ComponentContext & component_context )

Definition at line 93 of file http_handler_flatbuf_base.hpp.

Member Function Documentation

◆ GetComponentHealth()

ComponentHealth components::ComponentBase::GetComponentHealth ( ) const
inlineoverridevirtualinherited

Override this function to inform the world of the state of your component.

Warning
The function is called concurrently from multiple threads.

Reimplemented from components::RawComponentBase.

Reimplemented in server::handlers::Restart.

Definition at line 35 of file component_base.hpp.

◆ GetInputData()

template<typename InputType , typename ReturnType >
const InputType::NativeTableType * server::handlers::HttpHandlerFlatbufBase< InputType, ReturnType >::GetInputData ( const request::RequestContext & context) const
Returns
A pointer to input data if it was parsed successfully or nullptr otherwise.

Definition at line 117 of file http_handler_flatbuf_base.hpp.

◆ GetLogLevelForResponseStatus()

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

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

◆ GetOutputData()

template<typename InputType , typename ReturnType >
const ReturnType::NativeTableType * server::handlers::HttpHandlerFlatbufBase< InputType, ReturnType >::GetOutputData ( const request::RequestContext & context) const
Returns
a pointer to output data if it was returned successfully by HandleRequestFlatbufThrow() or nullptr otherwise.

Definition at line 124 of file http_handler_flatbuf_base.hpp.

◆ GetRequestBodyForLogging()

template<typename InputType , typename ReturnType >
std::string server::handlers::HttpHandlerFlatbufBase< InputType, ReturnType >::GetRequestBodyForLogging ( const http::HttpRequest & request,
request::RequestContext & context,
const std::string & request_body ) const
overrideprotectedvirtual

Override it if you need a custom request body logging.

Reimplemented from server::handlers::HttpHandlerBase.

Definition at line 131 of file http_handler_flatbuf_base.hpp.

◆ GetResponseDataForLogging()

template<typename InputType , typename ReturnType >
std::string server::handlers::HttpHandlerFlatbufBase< InputType, ReturnType >::GetResponseDataForLogging ( const http::HttpRequest & request,
request::RequestContext & context,
const std::string & response_data ) const
overrideprotectedvirtual

Override it if you need a custom response data logging.

Reimplemented from server::handlers::HttpHandlerBase.

Definition at line 141 of file http_handler_flatbuf_base.hpp.

◆ GetStaticConfigSchema()

template<typename InputType , typename ReturnType >
yaml_config::Schema server::handlers::HttpHandlerFlatbufBase< InputType, ReturnType >::GetStaticConfigSchema ( )
static

Definition at line 169 of file http_handler_flatbuf_base.hpp.

◆ HandleCustomHandlerException()

void server::handlers::HttpHandlerBase::HandleCustomHandlerException ( const http::HttpRequest & request,
const CustomHandlerException & ex ) const
inherited

Takes the exception and formats it into response, as specified by exception.

◆ HandleRequest()

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

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

Implements server::handlers::HandlerBase.

◆ HandleRequestThrow()

template<typename InputType , typename ReturnType >
std::string server::handlers::HttpHandlerFlatbufBase< InputType, ReturnType >::HandleRequestThrow ( const http::HttpRequest & request,
request::RequestContext & context ) const
finalvirtual

The core method for HTTP request handling. request arg contains HTTP headers, full body, etc. The method should return response body.

Note
It is used only if IsStreamed() returned false.

Reimplemented from server::handlers::HttpHandlerBase.

Definition at line 100 of file http_handler_flatbuf_base.hpp.

◆ HandleStreamRequest()

virtual void server::handlers::HttpHandlerBase::HandleStreamRequest ( const server::http::HttpRequest & ,
server::request::RequestContext & ,
server::http::ResponseBodyStream &  ) const
protectedvirtualinherited

The core method for HTTP request handling. request arg contains HTTP headers, full body, etc. The response body is passed in parts to ResponseBodyStream. Stream transmission is useful when: 1) The body size is unknown beforehand. 2) The client may take advantage of early body transmission (e.g. a Web Browser may start rendering the HTML page or downloading dependant resources). 3) The body size is huge and we want to have only a part of it in memory.

Note
It is used only if IsStreamed() returned true.

◆ HandleUnknownException()

void server::handlers::HttpHandlerBase::HandleUnknownException ( const http::HttpRequest & request,
const std::exception & ex ) const
inherited

Takes the exception and formats it into response as an internal server error.

◆ IsMethodStatisticIncluded()

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

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

Definition at line 154 of file http_handler_base.hpp.

◆ IsMonitor()

bool server::handlers::HandlerBase::IsMonitor ( ) const
inlineinherited

Returns whether this is a monitoring handler.

Definition at line 67 of file handler_base.hpp.

◆ IsStreamed()

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

If IsStreamed() returns true, call HandleStreamRequest() for request handling, HandleRequestThrow() is not called. If it returns false, HandleRequestThrow() is called instead, and HandleStreamRequest() is not called.

Note
The default implementation returns the cached value of "response-body-streamed" value from static config.

Definition at line 150 of file http_handler_base.hpp.

◆ NeedCheckAuth()

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

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

Definition at line 158 of file http_handler_base.hpp.

◆ OnAllComponentsAreStopping()

void components::ComponentBase::OnAllComponentsAreStopping ( )
inlineoverridevirtualinherited

Component may use this function to stop doing work before the stop of the components that depend on it.

Base components may override it and make final to do some work before the derived object constructor is called. Don't use it otherwise.

Reimplemented from components::RawComponentBase.

Reimplemented in urabbitmq::ConsumerComponentBase, and components::Server.

Definition at line 58 of file component_base.hpp.

◆ OnAllComponentsLoaded()

void components::ComponentBase::OnAllComponentsLoaded ( )
inlineoverridevirtualinherited

Component may use this function to finalize registration of other components that depend on it (for example, handler components register in server component, and the latter uses OnAllComponentsLoaded() to start processing requests).

Base components may override it and make final to do some work after the derived object constructor is called. Don't use it otherwise.

Reimplemented from components::RawComponentBase.

Reimplemented in urabbitmq::ConsumerComponentBase, components::Server, and server::handlers::Ping.

Definition at line 51 of file component_base.hpp.

◆ OnLoadingCancelled()

void components::ComponentBase::OnLoadingCancelled ( )
inlineoverridevirtualinherited

Called once if the creation of any other component failed. If the current component expects some other component to take any action with the current component, this call is a signal that such action may never happen due to components loading was cancelled. Application components might not want to override it.

Reimplemented from components::RawComponentBase.

Definition at line 42 of file component_base.hpp.

◆ ParseRequestData()

template<typename InputType , typename ReturnType >
void server::handlers::HttpHandlerFlatbufBase< InputType, ReturnType >::ParseRequestData ( const http::HttpRequest & ,
request::RequestContext &  ) const
finalprotectedvirtual

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

Reimplemented from server::handlers::HttpHandlerBase.

Definition at line 151 of file http_handler_flatbuf_base.hpp.

◆ ReportMalformedRequest()

void server::handlers::HttpHandlerBase::ReportMalformedRequest ( request::RequestBase & ) const
finalvirtualinherited

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: