forked from RipcordSoftware/libhttpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest_router.h
More file actions
50 lines (38 loc) · 1.32 KB
/
Copy pathrequest_router.h
File metadata and controls
50 lines (38 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#ifndef RS_LIBHTTPSERVER_REQUEST_ROUTER_H
#define RS_LIBHTTPSERVER_REQUEST_ROUTER_H
#include <boost/regex.hpp>
#include <boost/function.hpp>
#include <boost/optional.hpp>
#include <vector>
#include <map>
#include <cstring>
#include "types.h"
namespace rs {
namespace httpserver {
/**
* A class that routes requests to a handler based on the method/uri
*/
class RequestRouter final {
public:
struct CallbackArgsKeyComparer {
bool operator()(const char* a, const char* b) const {
return std::strcmp(a, b) < 0;
}
};
typedef std::map<const char*, const char*, CallbackArgsKeyComparer> CallbackArgs;
typedef boost::function<bool(request_ptr request, const CallbackArgs&, response_ptr response)> Callback;
RequestRouter();
/**
* Adds a new method/regex pair into the router
*/
void Add(const char* method, const char* re, Callback func);
/**
* Matches the request against the routes and invokes the associated callback if one is found
* @return true if the request was handled
*/
bool Match(request_ptr request, response_ptr response) const;
private:
std::map<std::string, std::vector<std::tuple<boost::basic_regex<char>, Callback, std::vector<std::string>>>> routes_;
};
}}
#endif /* RS_LIBHTTPSERVER_REQUEST_ROUTER_H */