FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ESPAsyncWebServer/src/WebHandlers.cpp at main · ESP32Async/ESPAsyncWebServer · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
ESP32Async
/
ESPAsyncWebServer
Public
forked from
me-no-dev/ESPAsyncWebServer
Notifications
You must be signed in to change notification settings
Fork
108
Star
648
Code
Issues
0
Pull requests
1
Discussions
Actions
Projects
Security and quality
3
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
ESPAsyncWebServer
/
src
/
WebHandlers.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
326 lines (279 loc) · 10.4 KB
Breadcrumbs
ESPAsyncWebServer
/
src
/
WebHandlers.cpp
Copy path
File metadata and controls
326 lines (279 loc) · 10.4 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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
//
SPDX-License-Identifier: LGPL-3.0-or-later
//
Copyright 2016-2026 Hristo Gochkov, Mathieu Carbou, Emil Muratov, Will Miles
#
include
"
ESPAsyncWebServer.h
"
#
include
"
WebHandlerImpl.h
"
#
include
"
AsyncWebServerLogging.h
"
#
include
<
cstdio
>
#
include
<
utility
>
using
namespace
asyncsrv
;
AsyncWebHandler &
AsyncWebHandler::setFilter
(ArRequestFilterFunction fn) {
_filter = fn;
return
*
this
;
}
AsyncWebHandler &
AsyncWebHandler::setAuthentication
(
const
char
*username,
const
char
*password, AsyncAuthType authMethod) {
if
(!_authMiddleware) {
_authMiddleware =
new
AsyncAuthenticationMiddleware
();
_authMiddleware->
_freeOnRemoval
=
true
;
addMiddleware
(_authMiddleware);
}
_authMiddleware->
setUsername
(username);
_authMiddleware->
setPassword
(password);
_authMiddleware->
setAuthType
(authMethod);
return
*
this
;
};
AsyncStaticWebHandler::AsyncStaticWebHandler
(
const
char
*uri,
FS
&fs,
const
char
*path,
const
char
*cache_control)
: _fs(fs), _uri(uri), _path(path), _default_file(F(
"
index.htm
"
)), _cache_control(cache_control), _last_modified(), _callback(
nullptr
) {
//
Ensure leading '/'
if
(_uri.
length
() ==
0
|| _uri[
0
] !=
'
/
'
) {
_uri =
String
(
'
/
'
) + _uri;
}
if
(_path.
length
() ==
0
|| _path[
0
] !=
'
/
'
) {
_path =
String
(
'
/
'
) + _path;
}
//
If path ends with '/' we assume a hint that this is a directory to improve performance.
//
However - if it does not end with '/' we, can't assume a file, path can still be a directory.
_isDir = _path[_path.
length
() -
1
] ==
'
/
'
;
//
Remove the trailing '/' so we can handle default file
//
Notice that root will be "" not "/"
if
(_uri[_uri.
length
() -
1
] ==
'
/
'
) {
_uri = _uri.
substring
(
0
, _uri.
length
() -
1
);
}
if
(_path[_path.
length
() -
1
] ==
'
/
'
) {
_path = _path.
substring
(
0
, _path.
length
() -
1
);
}
}
AsyncStaticWebHandler &
AsyncStaticWebHandler::setTryGzipFirst
(
bool
value) {
_tryGzipFirst = value;
return
*
this
;
}
AsyncStaticWebHandler &
AsyncStaticWebHandler::setIsDir
(
bool
isDir) {
_isDir = isDir;
return
*
this
;
}
AsyncStaticWebHandler &
AsyncStaticWebHandler::setDefaultFile
(
const
char
*filename) {
_default_file = filename;
return
*
this
;
}
AsyncStaticWebHandler &
AsyncStaticWebHandler::setCacheControl
(
const
char
*cache_control) {
_cache_control = cache_control;
return
*
this
;
}
AsyncStaticWebHandler &
AsyncStaticWebHandler::setLastModified
(
const
char
*last_modified) {
_last_modified = last_modified;
return
*
this
;
}
AsyncStaticWebHandler &
AsyncStaticWebHandler::setLastModified
(
struct
tm
*last_modified) {
char
result[
30
];
#
ifdef
ESP8266
auto
formatP =
PSTR
(
"
%a, %d %b %Y %H:%M:%S GMT
"
);
char
format[
strlen_P
(formatP) +
1
];
//
NOLINT(runtime/arrays)
strcpy_P
(format, formatP);
#
else
static
constexpr
const
char
*format =
"
%a, %d %b %Y %H:%M:%S GMT
"
;
#
endif
strftime
(result,
sizeof
(result), format, last_modified);
_last_modified = result;
return
*
this
;
}
AsyncStaticWebHandler &
AsyncStaticWebHandler::setLastModified
(
time_t
last_modified) {
return
setLastModified
((
struct
tm
*)
gmtime
(&last_modified));
}
AsyncStaticWebHandler &
AsyncStaticWebHandler::setLastModified
() {
time_t
last_modified;
if
(
time
(&last_modified) ==
0
) {
//
time is not yet set
return
*
this
;
}
return
setLastModified
(last_modified);
}
bool
AsyncStaticWebHandler::canHandle
(AsyncWebServerRequest *request)
const
{
return
request->
isHTTP
() && request->
method
() == AsyncWebRequestMethod::
HTTP_GET
&& request->
url
().
startsWith
(_uri) &&
_getFile
(request);
}
bool
AsyncStaticWebHandler::_getFile
(AsyncWebServerRequest *request)
const
{
//
Remove the found uri
String path = request->
url
().
substring
(_uri.
length
());
//
We can skip the file check and look for default if request is to the root of a directory or that request path ends with '/'
bool
canSkipFileCheck = (_isDir && path.
length
() ==
0
) || (path.
length
() && path[path.
length
() -
1
] ==
'
/
'
);
path = _path + path;
//
Do we have a file or .gz file
if
(!canSkipFileCheck &&
const_cast
<AsyncStaticWebHandler *>(
this
)->
_searchFile
(request, path)) {
return
true
;
}
//
Can't handle if not default file
if
(_default_file.
length
() ==
0
) {
return
false
;
}
//
Try to add default file, ensure there is a trailing '/' to the path.
if
(path.
length
() ==
0
|| path[path.
length
() -
1
] !=
'
/
'
) {
path +=
String
(
'
/
'
);
}
path += _default_file;
return
const_cast
<AsyncStaticWebHandler *>(
this
)->
_searchFile
(request, path);
}
#
ifdef
ESP32
#
define
FILE_IS_REAL
(
f
) (f ==
true
&& !f.isDirectory())
#
else
#
define
FILE_IS_REAL
(
f
) (f ==
true
)
#
endif
bool
AsyncStaticWebHandler::_searchFile
(AsyncWebServerRequest *request,
const
String &path) {
bool
fileFound =
false
;
bool
gzipFound =
false
;
String gzip = path + T__gz;
if
(_tryGzipFirst) {
if
(_fs.
exists
(gzip)) {
request->
_tempFile
= _fs.
open
(gzip, fs::FileOpenMode::read);
gzipFound =
FILE_IS_REAL
(request->
_tempFile
);
}
if
(!gzipFound) {
if
(_fs.
exists
(path)) {
request->
_tempFile
= _fs.
open
(path, fs::FileOpenMode::read);
fileFound =
FILE_IS_REAL
(request->
_tempFile
);
}
}
}
else
{
if
(_fs.
exists
(path)) {
request->
_tempFile
= _fs.
open
(path, fs::FileOpenMode::read);
fileFound =
FILE_IS_REAL
(request->
_tempFile
);
}
if
(!fileFound) {
if
(_fs.
exists
(gzip)) {
request->
_tempFile
= _fs.
open
(gzip, fs::FileOpenMode::read);
gzipFound =
FILE_IS_REAL
(request->
_tempFile
);
}
}
}
bool
found = fileFound || gzipFound;
if
(found) {
//
Extract the file name from the path and keep it in _tempObject
size_t
pathLen = path.
length
();
char
*_tempPath = (
char
*)
malloc
(pathLen +
1
);
if
(_tempPath ==
NULL
) {
async_ws_log_e
(
"
Failed to allocate
"
);
request->
_tempFile
.
close
();
request->
abort
();
return
false
;
}
snprintf_P
(_tempPath, pathLen +
1
,
PSTR
(
"
%s
"
), path.
c_str
());
request->
_tempObject
= (
void
*)_tempPath;
}
return
found;
}
/*
*
* @brief Handles an incoming HTTP request for a static file.
*
* This method processes a request for serving static files asynchronously.
* It determines the correct ETag (entity tag) for caching, checks if the file
* has been modified, and prepares the appropriate response (file response or 304 Not Modified).
*
* @param request Pointer to the incoming AsyncWebServerRequest object.
*/
void
AsyncStaticWebHandler::handleRequest
(AsyncWebServerRequest *request) {
//
Get the filename from request->_tempObject and free it
String
filename
((
char
*)request->
_tempObject
);
free
(request->
_tempObject
);
request->
_tempObject
=
nullptr
;
if
(request->
_tempFile
!=
true
) {
request->
send
(
404
);
return
;
}
//
Get server ETag. If file is not GZ and we have a Template Processor, ETag is set to an empty string
char
etag[
11
];
const
char
*tempFileName = request->
_tempFile
.
name
();
const
size_t
lenFilename =
strlen
(tempFileName);
if
(lenFilename >
T__GZ_LEN
&&
memcmp
(tempFileName + lenFilename -
T__GZ_LEN
, T__gz,
T__GZ_LEN
) ==
0
) {
//
File is a gz, get etag from CRC in trailer
if
(!
AsyncWebServerRequest::_getEtag
(request->
_tempFile
, etag)) {
//
File is corrupted or invalid
async_ws_log_w
(
"
File is corrupted or invalid: %s
"
, tempFileName);
request->
send
(
404
);
return
;
}
//
Reset file position to the beginning so the file can be served from the start.
request->
_tempFile
.
seek
(
0
);
}
else
if
(_callback ==
nullptr
) {
//
We don't have a Template processor
uint32_t
etagValue;
time_t
lastWrite = request->
_tempFile
.
getLastWrite
();
if
(lastWrite >
0
) {
//
Use timestamp-based ETag
etagValue =
static_cast
<
uint32_t
>(lastWrite);
}
else
{
//
No timestamp available, use filesize-based ETag
size_t
fileSize = request->
_tempFile
.
size
();
etagValue =
static_cast
<
uint32_t
>(fileSize);
}
//
RFC9110 Section-8.8.3: Value of the ETag response must be enclosed in double quotes
snprintf
(etag,
sizeof
(etag),
"
\"
%08
"
PRIx32
"
\"
"
, etagValue);
}
else
{
etag[
0
] =
'
\0
'
;
}
AsyncWebServerResponse *response;
bool
notModified =
false
;
//
1. If the client sent If-None-Match and we have an ETag → compare
if
(*etag !=
'
\0
'
&& request->
header
(
T_INM
) == etag) {
notModified =
true
;
}
//
2. Otherwise, if there is no ETag but we have Last-Modified and Last-Modified matches
else
if
(*etag ==
'
\0
'
&& _last_modified.
length
() >
0
&& request->
header
(
T_IMS
) == _last_modified) {
async_ws_log_d
(
"
_last_modified: %s
"
, _last_modified.
c_str
());
notModified =
true
;
}
if
(notModified) {
request->
_tempFile
.
close
();
response =
new
AsyncBasicResponse
(
304
);
//
Not modified
}
else
{
response =
new
AsyncFileResponse
(request->
_tempFile
, filename, asyncsrv::emptyString,
false
, _callback);
}
if
(!response) {
async_ws_log_e
(
"
Failed to allocate
"
);
request->
abort
();
return
;
}
if
(!notModified) {
//
Set ETag header
if
(*etag !=
'
\0
'
) {
response->
addHeader
(T_ETag, etag,
true
);
}
//
Set Last-Modified header
if
(_last_modified.
length
()) {
response->
addHeader
(T_Last_Modified, _last_modified.
c_str
(),
true
);
}
}
//
Set cache control
if
(_cache_control.
length
()) {
response->
addHeader
(T_Cache_Control, _cache_control.
c_str
(),
false
);
}
else
{
response->
addHeader
(T_Cache_Control, T_no_cache,
false
);
}
request->
send
(response);
}
AsyncStaticWebHandler &
AsyncStaticWebHandler::setTemplateProcessor
(AwsTemplateProcessor newCallback) {
_callback = newCallback;
return
*
this
;
}
void
AsyncCallbackWebHandler::setUri
(AsyncURIMatcher uri) {
_uri =
std::move
(uri);
}
bool
AsyncCallbackWebHandler::canHandle
(AsyncWebServerRequest *request)
const
{
if
(!_onRequest || !request->
isHTTP
() || !_method.
matches
(request->
method
())) {
return
false
;
}
return
_uri.
matches
(request);
}
void
AsyncCallbackWebHandler::handleRequest
(AsyncWebServerRequest *request) {
if
(_onRequest) {
_onRequest
(request);
}
else
{
request->
send
(
404
, T_text_plain,
"
Not found
"
);
}
}
void
AsyncCallbackWebHandler::handleUpload
(AsyncWebServerRequest *request,
const
String &filename,
size_t
index,
uint8_t
*data,
size_t
len,
bool
final
) {
if
(_onUpload) {
_onUpload
(request, filename, index, data, len,
final
);
}
}
void
AsyncCallbackWebHandler::handleBody
(AsyncWebServerRequest *request,
uint8_t
*data,
size_t
len,
size_t
index,
size_t
total) {
//
ESP_LOGD("AsyncWebServer", "AsyncCallbackWebHandler::handleBody");
if
(_onBody) {
_onBody
(request, data, len, index, total);
}
}
Back
|
FazBrowse Home
|
New Git URL