An HTTP web application handler that routes to other handlers. More...
#include <RoutingHttpWebApp.hpp>
Public Types | |
using | HttpWebAppPtr = std::shared_ptr< HttpWebApp > |
Shared pointer container for web app instances. More... | |
using | Node = Container::ObjectTrieNode< Value > |
The type of the routing nodes in the routing trie. More... | |
using | Routing = Container::ObjectTrie< Value > |
The type of the routing trie supplied to the routing handler constructor. More... | |
using | Value = std::tuple< std::string, HttpWebAppPtr, HttpWebAppPtr, HttpWebAppPtr > |
The type of the routing node values added to the routing trie. More... | |
Public Member Functions | |
RoutingHttpWebApp (Routing &&routing_) | |
Construct a routing HTTP handler, by supplying a preformed routing trie. More... | |
void | handleGetRequest (HttpSession &session, const StringRequest &request, std::map< std::string, std::string > &variables) override |
Handle a GET request. More... | |
void | handleHeadRequest (HttpSession &session, const StringRequest &request, std::map< std::string, std::string > &variables) override |
Handle a HEAD request. More... | |
void | handlePostRequest (HttpSession &session, const StringRequest &request, std::map< std::string, std::string > &variables) override |
Handle a POST request. More... | |
Public Member Functions inherited from HttpWebApp | |
virtual | ~HttpWebApp ()=default |
Destroy the HTTP web application instance. More... | |
Additional Inherited Members | |
Static Public Member Functions inherited from HttpWebApp | |
static EmptyResponse | createBadRequestHeadResponse (HttpSession &session, const StringRequest &request) |
Create a bad request response for a head request. More... | |
static StringResponse | createBadRequestResponse (HttpSession &session, const StringRequest &request, std::string_view errorMessage) |
Create a bad request response. More... | |
static EmptyResponse | createNotFoundHeadResponse (HttpSession &session, const StringRequest &request) |
Create a not found response for a head request. More... | |
static StringResponse | createNotFoundStringResponse (HttpSession &session, const StringRequest &request) |
Create a not found response. More... | |
static EmptyResponse | createOkHeadResponse (HttpSession &session, const StringRequest &request) |
Create an empty 200 response for a head request. More... | |
static StringResponse | createOkResponse (HttpSession &session, const StringRequest &request) |
Create an empty 200 response. More... | |
static EmptyResponse | createPermanentRedirectResponse (HttpSession &session, const StringRequest &request, std::string_view location) |
Create a permanent redirect response. More... | |
static EmptyResponse | createRedirectResponse (HttpSession &session, const StringRequest &request, std::string_view location) |
Create a redirect response. More... | |
static EmptyResponse | createServerErrorHeadResponse (HttpSession &session, const StringRequest &request) |
Create a server error response for a head request. More... | |
static StringResponse | createServerErrorResponse (HttpSession &session, const StringRequest &request, std::string_view errorMessage) |
Create a server error response. More... | |
template<typename WebAppT > | |
static void | registerHttpWebApp (const std::string &name) |
Register an HTTP web application. More... | |
An HTTP web application handler that routes to other handlers.
The routing handler works by selecting the most specific handler tuple for a particular path. If there is no match, not found is returned.
When the most specific handler tuple has been selected, the tuple's handler for the HTTP method in the request is called.
If a most specific match is not suitable for all paths, the resolved handlers may nevertheless return not found / bad request responses for invalid paths.
There are two ways to use the routing HTTP handler: automatically or manually.
The routing handler is automatically instantiated when the HTTP server is created via environment configuration. In this case, the HTTP server will create a populated routing handler from the HTTP web applications specified in the environment configuration. The HTTP server environment configuration thus specifies the routing hierarchy via the location properties in each web application.
Manual creation of a routing handler may also be performed if required, by constructing a routing trie with each node containing a routing node. Routing nodes are std::tuple objects, the first element containing a string component in the path and the second, third, and fourth elements containing shared pointers of the handlers to route to for GET, HEAD, and POST requests.
As the HTTP server will assign the same handler instance to all three GET, HEAD, and POST pointers for a particular location, manual creation can be useful when different handlers are required for each HTTP method in a single location.
To manually construct the routing trie, the routingNode() convenience functions may be used. If the routing trie is fixed at compile time, the fluent API of the ObjectTrie class can be used to construct the routing.
As the handlers are kept in shared pointers, handler instances may be shared between multiple nodes in the trie if required.
using HttpWebAppPtr = std::shared_ptr<HttpWebApp> |
Shared pointer container for web app instances.
using Node = Container::ObjectTrieNode<Value> |
The type of the routing nodes in the routing trie.
using Routing = Container::ObjectTrie<Value> |
The type of the routing trie supplied to the routing handler constructor.
using Value = std::tuple<std::string, HttpWebAppPtr, HttpWebAppPtr, HttpWebAppPtr> |
The type of the routing node values added to the routing trie.
|
inline |
Construct a routing HTTP handler, by supplying a preformed routing trie.
|
inlineoverridevirtual |
Handle a GET request.
The supplied session provides shared server state and the sendResponse method.
session | the HTTP session object, also containing the client session |
request | the HTTP request object |
variables | the request variables that are generated and consumed during the request |
Implements HttpWebApp.
|
inlineoverridevirtual |
Handle a HEAD request.
The supplied session provides shared server state and the sendResponse method.
session | the HTTP session object, also containing the client session |
request | the HTTP request object |
variables | the request variables that are generated and consumed during the request |
Implements HttpWebApp.
|
inlineoverridevirtual |
Handle a POST request.
The supplied session provides shared server state and the sendResponse method.
session | the HTTP session object, also containing the client session |
request | the HTTP request object |
variables | the request variables that are generated and consumed during the request |
Implements HttpWebApp.