Boost C++ Libraries

PrevUpHomeNext

Struct template guard_handler

boost::taar::handler::guard_handler — Handler decorator that runs a check before the inner handler.

Synopsis

// In header: <boost/taar/handler/guard.hpp>

template<typename Check, typename Inner> 
struct guard_handler {
  // types
  typedef unspecified request_type;
  typedef Inner       inner_type;    // The wrapped handler, for documentation generators that introspect it. 

  // public member functions
  template<typename Contexts> 
    auto operator()(Contexts const &, request_type const &, 
                    matcher::context const &);
  auto path_arg_names() const;
  auto params() const;

  // public data members
  static constexpr bool context_aware;
  Check check_;
  Inner inner_;
};

Description

Produced by guard. The check has the interceptor signature (leading context parameters injected by type, then boost::beast::http::request_header<> const& and matcher::context&) and returns taar::interceptor_result, directly or through an awaitable. On std::nullopt the inner handler runs with the same contexts, request and matcher context, so attributes the check stored are visible to it; on a response that response is the route's answer. The check receives a copy of the request's matcher context; the attributes it stores are carried over to the request's context, other changes are discarded.

The inner handler may produce a typed boost::beast::http::response, a message_generator, or a stream (chunked_response<T> or async_generator<T>, directly or through an awaitable). A rejection is converted to the inner's response type: for a typed response with a body that does not accept a std::string, only the status and headers are kept; for a stream it becomes a one-shot chunked_response<T> with the rejection's status and headers (and its text when T is std::string).

The check runs when the route's handler is invoked, after the request body has been read. Use a session interceptor (basic_http::add_interceptor(matcher, fn)) when the decision must be taken before the body is read.

params(), path_arg_names() and inner_type forward to the inner handler, so OpenAPI and the registration checks see through the guard. It composes with with_session in either order; inside with_session the check can request the keyed context.

A check that throws propagates to the session's soft error handler, like a handler exception.

guard_handler public member functions

  1. template<typename Contexts> 
      auto operator()(Contexts const & ctxs, request_type const & request, 
                      matcher::context const & mctx);
  2. auto path_arg_names() const;
  3. auto params() const;

PrevUpHomeNext