Request Context

A context wraps a raw HTTP request and is passed to all callables once its corresponding url pattern is matched. The context also associates the request with the underlying server. So using the server the server common functions that are not specific any particular callable like logging, template rendering etc. can be performed. The context can be stateful or stateless. With a stateless context session informations can not be accessed even if the server is stateful. A stateful context is instantiated with a set of states that can be accessed from the callable. The set of states with which the stateful context is created should be the subset of the states of a stateful server. As the stateful context contains a subset of server states only that subset of state information is accessible through the stateful context.

Note

The context templates require the bridge and HTTP request type to be passed as the template parameters while instantiation. Its adviced to use the conveniance types instead of directly instiating these templates.

str::string profile(udho::contexts::stateful<user> ctx){
    if(ctx.session().exists<user>()){
      user data;
      ctx.session() >> data;
      // user logged in
    }else{
      // user not logged in
    }
}

udho::contexts::stateful is actually a typedef of udho::context with default bridge and request type

std::string profile(udho::contexts::stateless ctx, std::size_t uid){
    // view public profile of user uid
}

udho::contexts::stateless is actually a typedef of udho::context with default bridge and request type

Request

The request parameters can be accessed either through the conveniance methods or through the raw boost-beast request object. Use ctx.request() to get the underlying request object from the context. However for common tasks there are conveniance methods that extracts information from the request object and returns.

Warning

doxygenfunction: Cannot find function “udho::context::request” in doxygen xml output for project “udho” from directory: dox/xml

Warning

doxygenfunction: Cannot find function “udho::context::target” in doxygen xml output for project “udho” from directory: dox/xml

Warning

doxygenfunction: Cannot find function “udho::context::path” in doxygen xml output for project “udho” from directory: dox/xml

Warning

doxygenfunction: Cannot find function “udho::context::query” in doxygen xml output for project “udho” from directory: dox/xml

Logging

The context passes a logging message to the logger attached with the server.

Warning

doxygenfunction: Cannot find function “udho::context::log” in doxygen xml output for project “udho” from directory: dox/xml

API

Stateful Context Template

template<typename AuxT, typename RequestT, typename ShadowT>
struct udho::context : public udho::detail::context_common<AuxT, RequestT>

A Stateful context passed to all callables along with the arguments. The context is always the first argument to the callable. Even if the callable takes no arguments, it must take the context as the first argument. A stateful context should be used in callables that need to use session states.

Note

instead of instantiating this template directly use udho::contexts::stateful

Template Parameters
  • AuxT: bridge between the server and the callable

  • RequestT: HTTP request type

  • ShadowT: the session data structure

Public Functions

session_type &session()

access the HTTP Session

See

udho::session_

Stateless Context Template

template<typename AuxT, typename RequestT>
struct context<AuxT, RequestT, void> : public udho::detail::context_common<AuxT, RequestT>

A Stateless context passed to all callables along with the arguments. he context is always the first argument to the callable. Even if the callable takes no arguments, it must take the context as the first argument. A stateless context should be used in callables that need not to use session states.

Note

instead of instantiating this template directly use udho::contexts::stateless

Template Parameters
  • AuxT: bridge between the server and the callable

  • RequestT: HTTP request type