-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
Copy pathRequestHandlersImpl.h
297 lines (243 loc) · 8.2 KB
/
RequestHandlersImpl.h
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#ifndef REQUESTHANDLERSIMPL_H
#define REQUESTHANDLERSIMPL_H
#include "RequestHandler.h"
#include "mimetable.h"
#include "WString.h"
#include "Uri.h"
#include <MD5Builder.h>
#include <base64.h>
using namespace mime;
RequestHandler &RequestHandler::addMiddleware(Middleware *middleware) {
if (!_chain) {
_chain = new MiddlewareChain();
}
_chain->addMiddleware(middleware);
return *this;
}
RequestHandler &RequestHandler::addMiddleware(Middleware::Function fn) {
if (!_chain) {
_chain = new MiddlewareChain();
}
_chain->addMiddleware(fn);
return *this;
}
RequestHandler &RequestHandler::removeMiddleware(Middleware *middleware) {
if (_chain) {
_chain->removeMiddleware(middleware);
}
return *this;
}
bool RequestHandler::process(WebServer &server, HTTPMethod requestMethod, String requestUri) {
if (_chain) {
return _chain->runChain(server, [this, &server, &requestMethod, &requestUri]() {
return handle(server, requestMethod, requestUri);
});
} else {
return handle(server, requestMethod, requestUri);
}
}
class FunctionRequestHandler : public RequestHandler {
public:
FunctionRequestHandler(WebServer::THandlerFunction fn, WebServer::THandlerFunction ufn, const Uri &uri, HTTPMethod method)
: _fn(fn), _ufn(ufn), _uri(uri.clone()), _method(method) {
_uri->initPathArgs(pathArgs);
}
~FunctionRequestHandler() {
delete _uri;
}
bool canHandle(HTTPMethod requestMethod, const String &requestUri) override {
if (_method != HTTP_ANY && _method != requestMethod) {
return false;
}
return _uri->canHandle(requestUri, pathArgs);
}
bool canUpload(const String &requestUri) override {
if (!_ufn || !canHandle(HTTP_POST, requestUri)) {
return false;
}
return true;
}
bool canRaw(const String &requestUri) override {
if (!_ufn || _method == HTTP_GET) {
return false;
}
return true;
}
bool canHandle(WebServer &server, HTTPMethod requestMethod, const String &requestUri) override {
if (_method != HTTP_ANY && _method != requestMethod) {
return false;
}
return _uri->canHandle(requestUri, pathArgs) && (_filter != NULL ? _filter(server) : true);
}
bool canUpload(WebServer &server, const String &requestUri) override {
if (!_ufn || !canHandle(server, HTTP_POST, requestUri)) {
return false;
}
return true;
}
bool canRaw(WebServer &server, const String &requestUri) override {
if (!_ufn || _method == HTTP_GET || (_filter != NULL ? _filter(server) == false : false)) {
return false;
}
return true;
}
bool handle(WebServer &server, HTTPMethod requestMethod, const String &requestUri) override {
if (!canHandle(server, requestMethod, requestUri)) {
return false;
}
_fn();
return true;
}
void upload(WebServer &server, const String &requestUri, HTTPUpload &upload) override {
(void)upload;
if (canUpload(server, requestUri)) {
_ufn();
}
}
void raw(WebServer &server, const String &requestUri, HTTPRaw &raw) override {
(void)raw;
if (canRaw(server, requestUri)) {
_ufn();
}
}
FunctionRequestHandler &setFilter(WebServer::FilterFunction filter) {
_filter = filter;
return *this;
}
protected:
WebServer::THandlerFunction _fn;
WebServer::THandlerFunction _ufn;
// _filter should return 'true' when the request should be handled
// and 'false' when the request should be ignored
WebServer::FilterFunction _filter;
Uri *_uri;
HTTPMethod _method;
};
class StaticRequestHandler : public RequestHandler {
public:
StaticRequestHandler(FS &fs, const char *path, const char *uri, const char *cache_header) : _fs(fs), _uri(uri), _path(path), _cache_header(cache_header) {
File f = fs.open(path);
_isFile = (f && (!f.isDirectory()));
log_v(
"StaticRequestHandler: path=%s uri=%s isFile=%d, cache_header=%s\r\n", path, uri, _isFile, cache_header ? cache_header : ""
); // issue 5506 - cache_header can be nullptr
_baseUriLength = _uri.length();
}
bool canHandle(HTTPMethod requestMethod, const String &requestUri) override {
if (requestMethod != HTTP_GET) {
return false;
}
if ((_isFile && requestUri != _uri) || !requestUri.startsWith(_uri)) {
return false;
}
return true;
}
bool canHandle(WebServer &server, HTTPMethod requestMethod, const String &requestUri) override {
if (requestMethod != HTTP_GET) {
return false;
}
if ((_isFile && requestUri != _uri) || !requestUri.startsWith(_uri)) {
return false;
}
if (_filter != NULL ? _filter(server) == false : false) {
return false;
}
return true;
}
bool handle(WebServer &server, HTTPMethod requestMethod, const String &requestUri) override {
if (!canHandle(server, requestMethod, requestUri)) {
return false;
}
log_v("StaticRequestHandler::handle: request=%s _uri=%s\r\n", requestUri.c_str(), _uri.c_str());
String path(_path);
if (!_isFile) {
// Base URI doesn't point to a file.
// If a directory is requested, look for index file.
if (requestUri.endsWith("/")) {
return handle(server, requestMethod, String(requestUri + "index.htm"));
}
// Append whatever follows this URI in request to get the file path.
path += requestUri.substring(_baseUriLength);
}
log_v("StaticRequestHandler::handle: path=%s, isFile=%d\r\n", path.c_str(), _isFile);
String contentType = getContentType(path);
// look for gz file, only if the original specified path is not a gz. So part only works to send gzip via content encoding when a non compressed is asked for
// if you point the the path to gzip you will serve the gzip as content type "application/x-gzip", not text or javascript etc...
if (!path.endsWith(FPSTR(mimeTable[gz].endsWith)) && !_fs.exists(path)) {
String pathWithGz = path + FPSTR(mimeTable[gz].endsWith);
if (_fs.exists(pathWithGz)) {
path += FPSTR(mimeTable[gz].endsWith);
}
}
File f = _fs.open(path, "r");
if (!f || !f.available()) {
return false;
}
String eTagCode;
if (server._eTagEnabled) {
if (server._eTagFunction) {
eTagCode = (server._eTagFunction)(_fs, path);
} else {
eTagCode = calcETag(_fs, path);
}
if (server.header("If-None-Match") == eTagCode) {
server.send(304);
return true;
}
}
if (_cache_header.length() != 0) {
server.sendHeader("Cache-Control", _cache_header);
}
if ((server._eTagEnabled) && (eTagCode.length() > 0)) {
server.sendHeader("ETag", eTagCode);
}
server.streamFile(f, contentType);
return true;
}
static String getContentType(const String &path) {
char buff[sizeof(mimeTable[0].mimeType)];
// Check all entries but last one for match, return if found
for (size_t i = 0; i < sizeof(mimeTable) / sizeof(mimeTable[0]) - 1; i++) {
strcpy_P(buff, mimeTable[i].endsWith);
if (path.endsWith(buff)) {
strcpy_P(buff, mimeTable[i].mimeType);
return String(buff);
}
}
// Fall-through and just return default type
strcpy_P(buff, mimeTable[sizeof(mimeTable) / sizeof(mimeTable[0]) - 1].mimeType);
return String(buff);
}
// calculate an ETag for a file in filesystem based on md5 checksum
// that can be used in the http headers - include quotes.
static String calcETag(FS &fs, const String &path) {
String result;
// calculate eTag using md5 checksum
uint8_t md5_buf[16];
File f = fs.open(path, "r");
MD5Builder calcMD5;
calcMD5.begin();
calcMD5.addStream(f, f.size());
calcMD5.calculate();
calcMD5.getBytes(md5_buf);
f.close();
// create a minimal-length eTag using base64 byte[]->text encoding.
result = "\"" + base64::encode(md5_buf, 16) + "\"";
return (result);
} // calcETag
StaticRequestHandler &setFilter(WebServer::FilterFunction filter) {
_filter = filter;
return *this;
}
protected:
// _filter should return 'true' when the request should be handled
// and 'false' when the request should be ignored
WebServer::FilterFunction _filter;
FS _fs;
String _uri;
String _path;
String _cache_header;
bool _isFile;
size_t _baseUriLength;
};
#endif //REQUESTHANDLERSIMPL_H