Request handler

In general request handler is a function-object receiving exactly one parameter: request handle. The function signature of the handler must be:

restinio::request_handling_status_t
handler( restinio::request_handle_t req );

Each request handler receives a handle on a conrete request. restinio::request_handle_t is a alias for std::shared_ptr<restinio::request_t>. req parameters is passed by value and thus can be passed to another processing flow (that is where an async handling becomes possible).

Handler must return handling status via request_handling_status_t enum. If the handler does handle request it must return accepted value. If handler refuses to handle request it must return rejected. There are two helper functions: restinio::request_accepted() and restinio::request_rejected() for refering an itemes of the enum.

Default request handler used in traits is the following.

using default_request_handler_t =
    std::function<request_handling_status_t (request_handle_t)>;

Request structure

Let’s have a look at a very important object – restinio::request_t. It brings all the data of a given HTTP request and some internal stuff to make response mechanism work.

There are two major data pieces of request: request header and request body.

Request header can be accesed by request_t::header() function which returns a reference to restinio::http_request_header_t object. Refer to Working with http headers section for more detail on a header.

Body can be accesed by request_t::body() function which returns a reference to std::string.

Support for incoming requests with chunked encoding

Since v.0.6.9 RESTinio accepts incoming requests with chunked encoding (previous versions of RESTinio returned HTTP 501 error in such cases). RESTino v.0.6.9 glues all chunks together and holds them in a single concatenated body (that is accessible via request_t::body() method).

Information about individual chunks is preserved and can be accessed via request_t::chunked_input_info() method. But note that this method returns nullptr if the request uses different Transfer-Encoding scheme. It means that the result of request_t::chunked_input_info() should be checked before the use:

auto my_handler(restinio::request_handle_t req)
{
   const auto chunked_info = req->chunked_input_info();
   if(chunked_info)
   {
      for(const auto chunk : chunked_info->chunks())
         ... // Do something with chunk.
   }
}

If an incoming request with chunked encoding has trailing HTTP fields those fields can be accessed via chunked_input_info_t object too:

auto my_handler(restinio::request_handle_t req)
{
   const auto chunked_info = req->chunked_input_info();
   if(chunked_info)
   {
      const restinio::http_header_fields_t & trailing_fields =
         chunked_info->trailing_fields();
      trailing_fields.for_each_value_of("My-Trailing-Field",
         [](auto value) {
            ... // Handling of field value.
         });
   }
}