Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New feature: functional callbacks #91

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/HTTPConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/HTTPSCallbackFunction.hpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#ifndef SRC_HTTPSCALLBACKFUNCTION_HPP_
#define SRC_HTTPSCALLBACKFUNCTION_HPP_

#include <functional>

#include "HTTPRequest.hpp"
#include "HTTPResponse.hpp"

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<void(HTTPRequest *, HTTPResponse *)> HTTPSCallbackFunction;
}

#endif /* SRC_HTTPSCALLBACKFUNCTION_HPP_ */
9 changes: 8 additions & 1 deletion src/ResourceNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

}
Expand Down
25 changes: 22 additions & 3 deletions src/ResourceNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
};

Expand Down