FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
libhttpserver/src/httpserver/http_resource.hpp at master · gre-42/libhttpserver · GitHub
gre-42
/
libhttpserver
Public
forked from
etr/libhttpserver
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
libhttpserver
/
src
/
httpserver
/
http_resource.hpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
219 lines (188 loc) · 6.61 KB
Breadcrumbs
libhttpserver
/
src
/
httpserver
/
http_resource.hpp
Copy path
File metadata and controls
219 lines (188 loc) · 6.61 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
This file is part of libhttpserver
Copyright (C) 2011-2019 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
SRC_HTTPSERVER_HTTP_RESOURCE_HPP_
#
define
SRC_HTTPSERVER_HTTP_RESOURCE_HPP_
#
ifdef
DEBUG
#
include
<
iostream
>
#
endif
#
include
<
map
>
#
include
<
memory
>
#
include
<
string
>
#
include
<
utility
>
namespace
httpserver
{
class
http_request
; }
namespace
httpserver
{
class
http_response
; }
namespace
httpserver
{
namespace
details
{ std::shared_ptr<http_response>
empty_render
(
const
http_request& r); }
void
resource_init
(std::map<std::string,
bool
>* res);
/*
*
* Class representing a callable http resource.
*
*/
class
http_resource
{
public:
/*
*
* Class destructor
*
*/
virtual
~http_resource
() =
default
;
/*
*
* Method used to answer to a generic request
* @param req Request passed through http
* @return A http_response object
*
*/
virtual
const
std::shared_ptr<http_response>
render
(
const
http_request& req) {
return
details::empty_render
(req);
}
/*
*
* Method used to answer to a GET request
* @param req Request passed through http
* @return A http_response object
*
*/
virtual
const
std::shared_ptr<http_response>
render_GET
(
const
http_request& req) {
return
render
(req);
}
/*
*
* Method used to answer to a POST request
* @param req Request passed through http
* @return A http_response object
*
*/
virtual
const
std::shared_ptr<http_response>
render_POST
(
const
http_request& req) {
return
render
(req);
}
/*
*
* Method used to answer to a PUT request
* @param req Request passed through http
* @return A http_response object
*
*/
virtual
const
std::shared_ptr<http_response>
render_PUT
(
const
http_request& req) {
return
render
(req);
}
/*
*
* Method used to answer to a HEAD request
* @param req Request passed through http
* @return A http_response object
*
*/
virtual
const
std::shared_ptr<http_response>
render_HEAD
(
const
http_request& req) {
return
render
(req);
}
/*
*
* Method used to answer to a DELETE request
* @param req Request passed through http
* @return A http_response object
*
*/
virtual
const
std::shared_ptr<http_response>
render_DELETE
(
const
http_request& req) {
return
render
(req);
}
/*
*
* Method used to answer to a TRACE request
* @param req Request passed through http
* @return A http_response object
*
*/
virtual
const
std::shared_ptr<http_response>
render_TRACE
(
const
http_request& req) {
return
render
(req);
}
/*
*
* Method used to answer to a OPTIONS request
* @param req Request passed through http
* @return A http_response object
*
*/
virtual
const
std::shared_ptr<http_response>
render_OPTIONS
(
const
http_request& req) {
return
render
(req);
}
/*
*
* Method used to answer to a PATCH request
* @param req Request passed through http
* @return A http_response object
*
*/
virtual
const
std::shared_ptr<http_response>
render_PATCH
(
const
http_request& req) {
return
render
(req);
}
/*
*
* Method used to answer to a CONNECT request
* @param req Request passed through http
* @return A http_response object
*
*/
virtual
const
std::shared_ptr<http_response>
render_CONNECT
(
const
http_request& req) {
return
render
(req);
}
/*
*
* Method used to set if a specific method is allowed or not on this request
* @param method method to set permission on
* @param allowed boolean indicating if the method is allowed or not
*
*/
void
set_allowing
(
const
std::string& method,
bool
allowed) {
if
(allowed_methods.
count
(method)) {
allowed_methods[method] = allowed;
}
}
/*
*
* Method used to implicitly allow all methods
*
*/
void
allow_all
() {
std::map<std::string,
bool
>::iterator it;
for
(it=allowed_methods.
begin
(); it != allowed_methods.
end
(); ++it) {
allowed_methods[(*it).
first
] =
true
;
}
}
/*
*
* Method used to implicitly disallow all methods
*
*/
void
disallow_all
() {
std::map<std::string,
bool
>::iterator it;
for
(it=allowed_methods.
begin
(); it != allowed_methods.
end
(); ++it) {
allowed_methods[(*it).
first
] =
false
;
}
}
/*
*
* Method used to discover if an http method is allowed or not for this resource
* @param method Method to discover allowings
* @return true if the method is allowed
*
*/
bool
is_allowed
(
const
std::string& method) {
if
(allowed_methods.
count
(method)) {
return
allowed_methods[method];
}
else
{
#
ifdef
DEBUG
std::map<std::string,
bool
>::iterator it;
for
(it = allowed_methods.
begin
(); it != allowed_methods.
end
(); ++it) {
std::cout << (*it).
first
<<
"
->
"
<< (*it).
second
<< std::endl;
}
#
endif
//
DEBUG
return
false
;
}
}
protected:
/*
*
* Constructor of the class
*
*/
http_resource
() {
resource_init
(&allowed_methods);
}
/*
*
* Copy constructor
*
*/
http_resource
(
const
http_resource& b) =
default
;
http_resource
(http_resource&& b)
noexcept
=
default
;
http_resource&
operator
=(
const
http_resource& b) =
default
;
http_resource&
operator
=(http_resource&& b) =
default
;
private:
friend
class
webserver
;
friend
void
resource_init
(std::map<std::string,
bool
>* res);
std::map<std::string,
bool
> allowed_methods;
};
}
//
namespace httpserver
#
endif
//
SRC_HTTPSERVER_HTTP_RESOURCE_HPP_
Back
|
FazBrowse Home
|
New Git URL