forked from etr/libhttpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_resource_mirror.hpp
More file actions
206 lines (184 loc) · 7.6 KB
/
http_resource_mirror.hpp
File metadata and controls
206 lines (184 loc) · 7.6 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
204
205
206
/*
This file is part of libhttpserver
Copyright (C) 2011, 2012, 2013, 2014, 2015 Sebastiano Merlino
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
*/
#if !defined (_HTTPSERVER_HPP_INSIDE_) && !defined (HTTPSERVER_COMPILATION)
#error "Only <httpserver.hpp> or <httpserverpp> can be included directly."
#endif
#ifndef _HTTP_RESOURCE_MIRROR_HPP_
#define _HTTP_RESOURCE_MIRROR_HPP_
#include "httpserver/binders.hpp"
#define CREATE_METHOD_DETECTOR(X) \
template<typename T, typename RESULT, typename ARG1, typename ARG2> \
class has_##X \
{ \
template <typename U, RESULT (U::*)(ARG1, ARG2)> struct Check; \
template <typename U> static char func(Check<U, &U::X> *); \
template <typename U> static int func(...); \
public: \
enum { value = sizeof(func<T>(0)) == sizeof(char) }; \
};
#define HAS_METHOD(X, T, RESULT, ARG1, ARG2) \
has_##X<T, RESULT, ARG1, ARG2>::value
namespace httpserver {
template<typename CHILD> class http_resource;
class http_request;
class http_response;
class webserver;
namespace details
{
class http_endpoint;
struct modded_request;
struct daemon_item;
typedef bool(*is_allowed_ptr)(const std::string&);
CREATE_METHOD_DETECTOR(render);
CREATE_METHOD_DETECTOR(render_GET);
CREATE_METHOD_DETECTOR(render_POST);
CREATE_METHOD_DETECTOR(render_PUT);
CREATE_METHOD_DETECTOR(render_HEAD);
CREATE_METHOD_DETECTOR(render_DELETE);
CREATE_METHOD_DETECTOR(render_TRACE);
CREATE_METHOD_DETECTOR(render_OPTIONS);
CREATE_METHOD_DETECTOR(render_CONNECT);
CREATE_METHOD_DETECTOR(render_not_acceptable);
void empty_render(const http_request& r, http_response** res);
void empty_not_acceptable_render(
const http_request& r, http_response** res
);
bool empty_is_allowed(const std::string& method);
class http_resource_mirror
{
public:
http_resource_mirror()
{
}
~http_resource_mirror()
{
}
private:
typedef binders::functor_two<const http_request&,
http_response**, void> functor;
typedef binders::functor_one<const std::string&,
bool> functor_allowed;
const functor render;
const functor render_GET;
const functor render_POST;
const functor render_PUT;
const functor render_HEAD;
const functor render_DELETE;
const functor render_TRACE;
const functor render_OPTIONS;
const functor render_CONNECT;
const functor_allowed is_allowed;
functor method_not_acceptable_resource;
http_resource_mirror& operator= (const http_resource_mirror& o)
{
return *this;
}
template<typename T>
http_resource_mirror(http_resource<T>* res):
render(
HAS_METHOD(render, T, void,
const http_request&, http_response**
) ? functor(res, &T::render) : functor(&empty_render)
),
render_GET(
HAS_METHOD(render_GET, T, void,
const http_request&, http_response**
) ? functor(res, &T::render_GET) :
(
HAS_METHOD(render, T, void,
const http_request&, http_response**
) ? functor(res, &T::render) : functor(&empty_render)
)
),
render_POST(
HAS_METHOD(render_POST, T, void,
const http_request&, http_response**
) ? functor(res, &T::render_POST) :
(
HAS_METHOD(render, T, void,
const http_request&, http_response**
) ? functor(res, &T::render) : functor(&empty_render)
)
),
render_PUT(
HAS_METHOD(render_PUT, T, void,
const http_request&, http_response**
) ? functor(res, &T::render_PUT) :
(
HAS_METHOD(render, T, void,
const http_request&, http_response**
) ? functor(res, &T::render) : functor(&empty_render)
)
),
render_HEAD(
HAS_METHOD(render_HEAD, T, void,
const http_request&, http_response**
) ? functor(res, &T::render_HEAD) :
(
HAS_METHOD(render, T, void,
const http_request&, http_response**
) ? functor(res, &T::render) : functor(&empty_render)
)
),
render_DELETE(
HAS_METHOD(render_DELETE, T, void,
const http_request&, http_response**
) ? functor(res, &T::render_DELETE) :
(
HAS_METHOD(render, T, void,
const http_request&, http_response**
) ? functor(res, &T::render) : functor(&empty_render)
)
),
render_TRACE(
HAS_METHOD(render_TRACE, T, void,
const http_request&, http_response**
) ? functor(res, &T::render_TRACE) :
(
HAS_METHOD(render, T, void,
const http_request&, http_response**
) ? functor(res, &T::render) : functor(&empty_render)
)
),
render_OPTIONS(
HAS_METHOD(render_OPTIONS, T, void,
const http_request&, http_response**
) ? functor(res, &T::render_OPTIONS) :
(
HAS_METHOD(render, T, void,
const http_request&, http_response**
) ? functor(res, &T::render) : functor(&empty_render)
)
),
render_CONNECT(
HAS_METHOD(render_CONNECT, T, void,
const http_request&, http_response**
) ? functor(res, &T::render_CONNECT) :
(
HAS_METHOD(render, T, void,
const http_request&, http_response**
) ? functor(res, &T::render) : functor(&empty_render)
)
),
is_allowed(res, &T::is_allowed)
{
}
friend class ::httpserver::webserver;
};
} //details
} //httpserver
#endif //_HTTP_RESOURCE_MIRROR_HPP_