diff --git a/src/HTTPConnection.cpp b/src/HTTPConnection.cpp index 0ab739c..d0b8742 100644 --- a/src/HTTPConnection.cpp +++ b/src/HTTPConnection.cpp @@ -481,10 +481,10 @@ void HTTPConnection::loop() { } // Find the request handler callback - HTTPSCallbackFunction * resourceCallback; + HTTPSCallbackFunction resourceCallback; if (websocketRequested) { // For the websocket, we use the handshake callback defined below - resourceCallback = &handleWebsocketHandshake; + resourceCallback = HTTPSCallbackFunction(&handleWebsocketHandshake); } else { // For resource nodes, we use the callback defined by the node itself resourceCallback = ((ResourceNode*)resolvedResource.getMatchingNode())->_callback; diff --git a/src/HTTPSCallbackFunction.hpp b/src/HTTPSCallbackFunction.hpp index 1329e38..3a2e5e0 100644 --- a/src/HTTPSCallbackFunction.hpp +++ b/src/HTTPSCallbackFunction.hpp @@ -1,6 +1,8 @@ #ifndef SRC_HTTPSCALLBACKFUNCTION_HPP_ #define SRC_HTTPSCALLBACKFUNCTION_HPP_ +#include + #include "HTTPRequest.hpp" #include "HTTPResponse.hpp" @@ -8,7 +10,7 @@ namespace httpsserver { /** * \brief A callback function that will be called by the server to handle a request */ - typedef void (HTTPSCallbackFunction)(HTTPRequest * req, HTTPResponse * res); + typedef std::function HTTPSCallbackFunction; } #endif /* SRC_HTTPSCALLBACKFUNCTION_HPP_ */ diff --git a/src/ResourceNode.cpp b/src/ResourceNode.cpp index 9b61e48..3881875 100644 --- a/src/ResourceNode.cpp +++ b/src/ResourceNode.cpp @@ -2,13 +2,20 @@ namespace httpsserver { -ResourceNode::ResourceNode(const std::string &path, const std::string &method, const HTTPSCallbackFunction * callback, const std::string &tag): +ResourceNode::ResourceNode(const std::string &path, const std::string &method, const HTTPSCallbackFunction callback, const std::string &tag): HTTPNode(path, HANDLER_CALLBACK, tag), _method(method), _callback(callback) { } +ResourceNode::ResourceNode(const std::string &path, const std::string &method, void (*callback)(HTTPRequest * req, HTTPResponse * res), const std::string &tag): + HTTPNode(path, HANDLER_CALLBACK, tag), + _method(method), + _callback(HTTPSCallbackFunction(callback)) { + +} + ResourceNode::~ResourceNode() { } diff --git a/src/ResourceNode.hpp b/src/ResourceNode.hpp index c7c4519..fe72ccd 100644 --- a/src/ResourceNode.hpp +++ b/src/ResourceNode.hpp @@ -10,16 +10,35 @@ namespace httpsserver { /** * \brief This HTTPNode represents a route that maps to a regular HTTP request for a resource (static or dynamic) - * + * * It therefore contrasts to the WebsocketNode, which handles requests for Websockets. */ class ResourceNode : public HTTPNode { public: - ResourceNode(const std::string &path, const std::string &method, const HTTPSCallbackFunction * callback, const std::string &tag = ""); + /** + * \brief Create the Resource Node with C++-Style functional attribute. + * + * This variant is more flexible and allows to use std::bind for example to call class member functions. + * + * \param path The path/route to register the handler to, e.g. "/config" + * \param method The method required to match this node, e.g. "GET" + * \param callback The function to call when the route is accessed + * \param tag Optional tag that can be accessed in the handler function. Use it for example to define the roles required to access this route + */ + ResourceNode(const std::string &path, const std::string &method, const HTTPSCallbackFunction callback, const std::string &tag = ""); + /** + * \brief Create the Resource Node with a C-Style function pointer. + * + * \param path The path/route to register the handler to, e.g. "/config" + * \param method The method required to match this node, e.g. "GET" + * \param callback The function callback. Must return void, first parameter is a pointer to a HTTPRequest, second a pointer to a HTTPResponse + * \param tag Optional tag that can be accessed in the handler function. Use it for example to define the roles required to access this route + */ + ResourceNode(const std::string &path, const std::string &method, void (*const callback)(HTTPRequest * req, HTTPResponse * res), const std::string &tag = ""); virtual ~ResourceNode(); const std::string _method; - const HTTPSCallbackFunction * _callback; + const HTTPSCallbackFunction _callback; std::string getMethod() { return _method; } };