-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRestService.hpp
More file actions
203 lines (168 loc) · 4 KB
/
RestService.hpp
File metadata and controls
203 lines (168 loc) · 4 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#ifndef REST_SERVICE_HPP
#define REST_SERVICE_HPP
/*****************************************************************************
* Facilitates handling REST via libevhtp
* ****************************************************************************/
#include <stdexcept>
#include <string>
#include <evhtp.h>
/*****************************************************************************
* Base class to derive from to create
* REST service that handles requests to a part of hierarchy
* e.g. localhost:80/site/service1/for/bar or
* localhost:80/site/another_service/another_foo
* with all HTTP methods known for libevhtp library
* Child should provide a virtual function per HTTP method (GET, POST, etc)
* ****************************************************************************/
template<typename ARG>
class RestService
{
public:
RestService(const std::string& path_regex, ARG arg);
// register the service in libevent (via libevhtp)
void
reg(evhtp_t*);
protected:
// raises std::invalid_argument exception
// if the service does not support specific HTTP method
// should be overloaded if the behavior is not convenient
virtual
void
not_supported(htp_method);
public:
// a virtual function to specify in a child per HTTP method
virtual
void
GET(evhtp_request_t* request);
virtual
void
HEAD(evhtp_request_t* request);
virtual
void
POST(evhtp_request_t* request);
virtual
void
PUT(evhtp_request_t* request);
virtual
void
DELETE(evhtp_request_t* request);
virtual
void
MKCOL(evhtp_request_t* request);
virtual
void
COPY(evhtp_request_t* request);
virtual
void
MOVE(evhtp_request_t* request);
virtual
void
OPTIONS(evhtp_request_t* request);
virtual
void
PROPFIND(evhtp_request_t* request);
void
PROPPATCH(evhtp_request_t* request);
virtual
void
LOCK(evhtp_request_t* request);
virtual
void
UNLOCK(evhtp_request_t* request);
virtual
void
TRACE(evhtp_request_t* request);
virtual
void
CONNECT(evhtp_request_t* request);
virtual
void
PATCH(evhtp_request_t* request);
virtual
void
UNKNOWN(evhtp_request_t* request);
private:
// internal callback
void
call(evhtp_request_t* request);
protected:
const std::string path_regex_;
ARG arg_;
};
template<typename ARG>
RestService<ARG>::RestService(const std::string& path_regex, ARG arg)
: path_regex_(path_regex), arg_(arg)
{
}
template<typename ARG>
void
RestService<ARG>::reg(evhtp_t* evhtp)
{
evhtp_set_regex_cb(evhtp, path_regex_.c_str(),
[](evhtp_request_t* request, void* args)
{
RestService<ARG>* me = static_cast<RestService<ARG>*>(args);
me->call(request);
}
, this);
}
template<typename ARG>
void
RestService<ARG>::not_supported(htp_method method)
{
std::__throw_invalid_argument(("HTTP method " +
std::to_string(method) + " not supported by service" + path_regex_).c_str());
}
#define CASEMETHOD(method) { case htp_method_##method: \
method(request); break; }
template<typename ARG>
void
RestService<ARG>::call(evhtp_request_t* request)
{
htp_method method = evhtp_request_get_method(request);
switch(method)
{
CASEMETHOD(GET);
CASEMETHOD(HEAD);
CASEMETHOD(POST);
CASEMETHOD(PUT);
CASEMETHOD(DELETE);
CASEMETHOD(MKCOL);
CASEMETHOD(COPY);
CASEMETHOD(MOVE);
CASEMETHOD(OPTIONS);
CASEMETHOD(PROPFIND);
CASEMETHOD(PROPPATCH);
CASEMETHOD(LOCK);
CASEMETHOD(UNLOCK);
CASEMETHOD(TRACE);
CASEMETHOD(CONNECT);
CASEMETHOD(PATCH);
CASEMETHOD(UNKNOWN);
}
}
#define IMPLMETHOD(method) \
template<typename ARG> \
void \
RestService<ARG>::method(evhtp_request_t*) \
{ \
not_supported(htp_method_##method); \
}
IMPLMETHOD(GET)
IMPLMETHOD(HEAD)
IMPLMETHOD(POST)
IMPLMETHOD(PUT)
IMPLMETHOD(DELETE)
IMPLMETHOD(MKCOL)
IMPLMETHOD(COPY)
IMPLMETHOD(MOVE)
IMPLMETHOD(OPTIONS)
IMPLMETHOD(PROPFIND)
IMPLMETHOD(PROPPATCH)
IMPLMETHOD(LOCK)
IMPLMETHOD(UNLOCK)
IMPLMETHOD(TRACE)
IMPLMETHOD(CONNECT)
IMPLMETHOD(PATCH)
IMPLMETHOD(UNKNOWN)
#endif