Skip to content

Commit c7e98a6

Browse files
committed
New feature: functional callbacks
1 parent daf6ca1 commit c7e98a6

File tree

4 files changed

+35
-7
lines changed

4 files changed

+35
-7
lines changed

Diff for: src/HTTPConnection.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -481,10 +481,10 @@ void HTTPConnection::loop() {
481481
}
482482

483483
// Find the request handler callback
484-
HTTPSCallbackFunction * resourceCallback;
484+
HTTPSCallbackFunction resourceCallback;
485485
if (websocketRequested) {
486486
// For the websocket, we use the handshake callback defined below
487-
resourceCallback = &handleWebsocketHandshake;
487+
resourceCallback = HTTPSCallbackFunction(&handleWebsocketHandshake);
488488
} else {
489489
// For resource nodes, we use the callback defined by the node itself
490490
resourceCallback = ((ResourceNode*)resolvedResource.getMatchingNode())->_callback;

Diff for: src/HTTPSCallbackFunction.hpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
#ifndef SRC_HTTPSCALLBACKFUNCTION_HPP_
22
#define SRC_HTTPSCALLBACKFUNCTION_HPP_
33

4+
#include <functional>
5+
46
#include "HTTPRequest.hpp"
57
#include "HTTPResponse.hpp"
68

79
namespace httpsserver {
810
/**
911
* \brief A callback function that will be called by the server to handle a request
1012
*/
11-
typedef void (HTTPSCallbackFunction)(HTTPRequest * req, HTTPResponse * res);
13+
typedef std::function<void(HTTPRequest *, HTTPResponse *)> HTTPSCallbackFunction;
1214
}
1315

1416
#endif /* SRC_HTTPSCALLBACKFUNCTION_HPP_ */

Diff for: src/ResourceNode.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,20 @@
22

33
namespace httpsserver {
44

5-
ResourceNode::ResourceNode(const std::string &path, const std::string &method, const HTTPSCallbackFunction * callback, const std::string &tag):
5+
ResourceNode::ResourceNode(const std::string &path, const std::string &method, const HTTPSCallbackFunction callback, const std::string &tag):
66
HTTPNode(path, HANDLER_CALLBACK, tag),
77
_method(method),
88
_callback(callback) {
99

1010
}
1111

12+
ResourceNode::ResourceNode(const std::string &path, const std::string &method, void (*callback)(HTTPRequest * req, HTTPResponse * res), const std::string &tag):
13+
HTTPNode(path, HANDLER_CALLBACK, tag),
14+
_method(method),
15+
_callback(HTTPSCallbackFunction(callback)) {
16+
17+
}
18+
1219
ResourceNode::~ResourceNode() {
1320

1421
}

Diff for: src/ResourceNode.hpp

+22-3
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,35 @@ namespace httpsserver {
1010

1111
/**
1212
* \brief This HTTPNode represents a route that maps to a regular HTTP request for a resource (static or dynamic)
13-
*
13+
*
1414
* It therefore contrasts to the WebsocketNode, which handles requests for Websockets.
1515
*/
1616
class ResourceNode : public HTTPNode {
1717
public:
18-
ResourceNode(const std::string &path, const std::string &method, const HTTPSCallbackFunction * callback, const std::string &tag = "");
18+
/**
19+
* \brief Create the Resource Node with C++-Style functional attribute.
20+
*
21+
* This variant is more flexible and allows to use std::bind for example to call class member functions.
22+
*
23+
* \param path The path/route to register the handler to, e.g. "/config"
24+
* \param method The method required to match this node, e.g. "GET"
25+
* \param callback The function to call when the route is accessed
26+
* \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
27+
*/
28+
ResourceNode(const std::string &path, const std::string &method, const HTTPSCallbackFunction callback, const std::string &tag = "");
29+
/**
30+
* \brief Create the Resource Node with a C-Style function pointer.
31+
*
32+
* \param path The path/route to register the handler to, e.g. "/config"
33+
* \param method The method required to match this node, e.g. "GET"
34+
* \param callback The function callback. Must return void, first parameter is a pointer to a HTTPRequest, second a pointer to a HTTPResponse
35+
* \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
36+
*/
37+
ResourceNode(const std::string &path, const std::string &method, void (*const callback)(HTTPRequest * req, HTTPResponse * res), const std::string &tag = "");
1938
virtual ~ResourceNode();
2039

2140
const std::string _method;
22-
const HTTPSCallbackFunction * _callback;
41+
const HTTPSCallbackFunction _callback;
2342
std::string getMethod() { return _method; }
2443
};
2544

0 commit comments

Comments
 (0)