17 #ifndef COM_BORA_SOFTWARE__BALAU_NETWORK_HTTP_SERVER_HTTP_WEB_APPS__ROUTING_HTTP_WEB_APP 18 #define COM_BORA_SOFTWARE__BALAU_NETWORK_HTTP_SERVER_HTTP_WEB_APPS__ROUTING_HTTP_WEB_APP 73 public:
using Value = std::tuple<std::string, HttpWebAppPtr, HttpWebAppPtr, HttpWebAppPtr>;
85 public:
static constexpr
size_t KeyIndex = 0;
86 public:
static constexpr
size_t GetHandlerIndex = 1;
87 public:
static constexpr
size_t HeadHandlerIndex = 2;
88 public:
static constexpr
size_t PostHandlerIndex = 3;
97 std::map<std::string, std::string> & variables)
override {
98 HttpWebApp * handler = resolve(session, request);
100 if (handler !=
nullptr) {
107 std::map<std::string, std::string> & variables)
override {
108 HttpWebApp * handler = resolve(session, request);
110 if (handler !=
nullptr) {
117 std::map<std::string, std::string> & variables)
override {
118 HttpWebApp * handler = resolve(session, request);
120 if (handler !=
nullptr) {
129 const std::string_view & path = std::string_view(request.target().data(), request.target().length());
131 std::vector<std::string_view> components;
132 components.reserve(pathComponents.size() + 1);
133 components.emplace_back(
"");
136 Node * node = routing.
findNearest(components, [] (
auto & lhs,
auto & rhs) {
return std::get<KeyIndex>(lhs) == rhs; },
false);
138 if (node !=
nullptr) {
141 switch (request.method()) {
143 handler = std::get<GetHandlerIndex>(node->
value).
get();
148 handler = std::get<HeadHandlerIndex>(node->
value).
get();
153 handler = std::get<PostHandlerIndex>(node->
value).
get();
163 if (handler !=
nullptr) {
169 sendNotFoundResponse(session, request);
184 template <
typename HandlerT,
typename ... ParamT>
186 auto handler = std::shared_ptr<HttpWebApp>(
new HandlerT(std::move(param) ...));
196 template <
typename HandlerT>
198 auto handler = std::shared_ptr<HttpWebApp>(
new HandlerT);
206 auto handler = std::shared_ptr<HttpWebApp>(
nullptr);
212 #endif // COM_BORA_SOFTWARE__BALAU_NETWORK_HTTP_SERVER_HTTP_WEB_APPS__ROUTING_HTTP_WEB_APP virtual void handleGetRequest(HttpSession &session, const StringRequest &request, std::map< std::string, std::string > &variables)=0
Handle a GET request.
virtual void handlePostRequest(HttpSession &session, const StringRequest &request, std::map< std::string, std::string > &variables)=0
Handle a POST request.
Manages the handling of HTTP messages and WebSocket upgrade requests in an HTTP connection.
Definition: HttpSession.hpp:46
T value
The publicly accessible data contained in the node.
Definition: ObjectTrie.hpp:98
ObjectTrieNode< T > * findNearest(const ContainerT< U, UC ... > &values, bool skipRoot=true)
Descend into the trie, locating matches of the supplied values.
Definition: ObjectTrie.hpp:2028
RoutingHttpWebApp(Routing &&routing_)
Construct a routing HTTP handler, by supplying a preformed routing trie.
Definition: RoutingHttpWebApp.hpp:93
An HTTP web application handler that routes to other handlers.
Definition: RoutingHttpWebApp.hpp:64
static std::vector< std::string_view > split(const std::string_view &input, const std::regex &delimiter, bool returnDelimiters=false, bool compress=true)
Split the input string on each of the occurrences of the specified delimiter regular expression...
Definition: Strings.hpp:1240
void handlePostRequest(HttpSession &session, const StringRequest &request, std::map< std::string, std::string > &variables) override
Handle a POST request.
Definition: RoutingHttpWebApp.hpp:115
void handleGetRequest(HttpSession &session, const StringRequest &request, std::map< std::string, std::string > &variables) override
Handle a GET request.
Definition: RoutingHttpWebApp.hpp:95
void handleHeadRequest(HttpSession &session, const StringRequest &request, std::map< std::string, std::string > &variables) override
Handle a HEAD request.
Definition: RoutingHttpWebApp.hpp:105
A set of HTTP web apps provided by the Balau library.
Definition: CannedHttpWebApp.hpp:27
Abstract base class of HTTP web application handlers.
Definition: HttpWebApp.hpp:39
std::shared_ptr< HttpWebApp > HttpWebAppPtr
Shared pointer container for web app instances.
Definition: RoutingHttpWebApp.hpp:68
Abstract base class of HTTP web application handlers.
Request< StringBody > StringRequest
A request with a string body.
Definition: NetworkTypes.hpp:267
static void append(std::vector< T > &dst, const std::vector< T > &src)
Appends the source vector to the destination vector.
Definition: Vectors.hpp:105
RoutingHttpWebApp::Value routingNode(const std::string &key, ParamT &&... param)
Convenience function to make a routing node from a handler type, a string key, and the handler constr...
Definition: RoutingHttpWebApp.hpp:185
std::tuple< std::string, HttpWebAppPtr, HttpWebAppPtr, HttpWebAppPtr > Value
The type of the routing node values added to the routing trie.
Definition: RoutingHttpWebApp.hpp:73
An object based trie used for parent-child hierarchies.
virtual void handleHeadRequest(HttpSession &session, const StringRequest &request, std::map< std::string, std::string > &variables)=0
Handle a HEAD request.