forked from etr/libhttpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_resource.hpp
More file actions
224 lines (205 loc) · 6.68 KB
/
http_resource.hpp
File metadata and controls
224 lines (205 loc) · 6.68 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
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_hpp_
#define _http_resource_hpp_
#include <map>
#include <string>
#ifdef DEBUG
#include <iostream>
#endif
namespace httpserver
{
class webserver;
class http_request;
class http_response;
namespace details
{
void empty_render(const http_request& r, http_response** res);
};
/**
* Class representing a callable http resource.
**/
void resource_init(std::map<std::string, bool>& res);
class http_resource
{
public:
/**
* Class destructor
**/
virtual ~http_resource()
{
}
/**
* Method used to answer to a generic request
* @param req Request passed through http
* @return A http_response object
**/
virtual void render(const http_request& r, http_response** res)
{
details::empty_render(r, res);
}
/**
* Method used to answer to a GET request
* @param req Request passed through http
* @return A http_response object
**/
virtual void render_GET(const http_request& req, http_response** res)
{
render(req, res);
}
/**
* Method used to answer to a POST request
* @param req Request passed through http
* @return A http_response object
**/
virtual void render_POST(const http_request& req, http_response** res)
{
render(req, res);
}
/**
* Method used to answer to a PUT request
* @param req Request passed through http
* @return A http_response object
**/
virtual void render_PUT(const http_request& req, http_response** res)
{
render(req, res);
}
/**
* Method used to answer to a HEAD request
* @param req Request passed through http
* @return A http_response object
**/
virtual void render_HEAD(const http_request& req, http_response** res)
{
render(req, res);
}
/**
* Method used to answer to a DELETE request
* @param req Request passed through http
* @return A http_response object
**/
virtual void render_DELETE(const http_request& req, http_response** res)
{
render(req, res);
}
/**
* Method used to answer to a TRACE request
* @param req Request passed through http
* @return A http_response object
**/
virtual void render_TRACE(const http_request& req, http_response** res)
{
render(req, res);
}
/**
* Method used to answer to a OPTIONS request
* @param req Request passed through http
* @return A http_response object
**/
virtual void render_OPTIONS(const http_request& req, http_response** res)
{
render(req, res);
}
/**
* Method used to answer to a CONNECT request
* @param req Request passed through http
* @return A http_response object
**/
virtual void render_CONNECT(const http_request& req, http_response** res)
{
render(req, res);
}
/**
* 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(this->allowed_methods.count(method))
{
this->allowed_methods[method] = allowed;
}
}
/**
* Method used to implicitly allow all methods
**/
void allow_all()
{
std::map<std::string,bool>::iterator it;
for ( it=this->allowed_methods.begin() ; it != this->allowed_methods.end(); ++it )
this->allowed_methods[(*it).first] = true;
}
/**
* Method used to implicitly disallow all methods
**/
void disallow_all()
{
std::map<std::string,bool>::iterator it;
for ( it=this->allowed_methods.begin() ; it != this->allowed_methods.end(); ++it )
this->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(this->allowed_methods.count(method))
{
return this->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) : allowed_methods(b.allowed_methods) { }
http_resource& operator = (const http_resource& b)
{
allowed_methods = b.allowed_methods;
return (*this);
}
private:
friend class webserver;
friend void resource_init(std::map<std::string, bool>& res);
std::map<std::string, bool> allowed_methods;
};
};
#endif