20
20
*/
21
21
22
22
#include < Arduino.h>
23
+ #include < esp32-hal-log.h>
23
24
#include " WiFiServer.h"
24
25
#include " WiFiClient.h"
25
26
#include " WebServer.h"
26
27
#include " detail/mimetable.h"
27
28
28
- // #define DEBUG_ESP_HTTP_SERVER
29
- #ifdef DEBUG_ESP_PORT
30
- #define DEBUG_OUTPUT DEBUG_ESP_PORT
31
- #else
32
- #define DEBUG_OUTPUT Serial
33
- #endif
34
-
35
29
#ifndef WEBSERVER_MAX_POST_ARGS
36
30
#define WEBSERVER_MAX_POST_ARGS 32
37
31
#endif
@@ -85,10 +79,7 @@ bool WebServer::_parseRequest(WiFiClient& client) {
85
79
int addr_start = req.indexOf (' ' );
86
80
int addr_end = req.indexOf (' ' , addr_start + 1 );
87
81
if (addr_start == -1 || addr_end == -1 ) {
88
- #ifdef DEBUG_ESP_HTTP_SERVER
89
- DEBUG_OUTPUT.print (" Invalid request: " );
90
- DEBUG_OUTPUT.println (req);
91
- #endif
82
+ log_e (" Invalid request: %s" , req.c_str ());
92
83
return false ;
93
84
}
94
85
@@ -119,14 +110,7 @@ bool WebServer::_parseRequest(WiFiClient& client) {
119
110
}
120
111
_currentMethod = method;
121
112
122
- #ifdef DEBUG_ESP_HTTP_SERVER
123
- DEBUG_OUTPUT.print (" method: " );
124
- DEBUG_OUTPUT.print (methodStr);
125
- DEBUG_OUTPUT.print (" url: " );
126
- DEBUG_OUTPUT.print (url);
127
- DEBUG_OUTPUT.print (" search: " );
128
- DEBUG_OUTPUT.println (searchStr);
129
- #endif
113
+ log_v (" method: %s url: %s search: %s" , methodStr.c_str (), url.c_str (), searchStr.c_str ());
130
114
131
115
// attach handler
132
116
RequestHandler* handler;
@@ -159,12 +143,8 @@ bool WebServer::_parseRequest(WiFiClient& client) {
159
143
headerValue.trim ();
160
144
_collectHeader (headerName.c_str (),headerValue.c_str ());
161
145
162
- #ifdef DEBUG_ESP_HTTP_SERVER
163
- DEBUG_OUTPUT.print (" headerName: " );
164
- DEBUG_OUTPUT.println (headerName);
165
- DEBUG_OUTPUT.print (" headerValue: " );
166
- DEBUG_OUTPUT.println (headerValue);
167
- #endif
146
+ log_v (" headerName: %s" , headerName.c_str ());
147
+ log_v (" headerValue: %s" , headerValue.c_str ());
168
148
169
149
if (headerName.equalsIgnoreCase (FPSTR (Content_Type))){
170
150
using namespace mime ;
@@ -206,10 +186,7 @@ bool WebServer::_parseRequest(WiFiClient& client) {
206
186
arg.value = String (plainBuf);
207
187
}
208
188
209
- #ifdef DEBUG_ESP_HTTP_SERVER
210
- DEBUG_OUTPUT.print (" Plain: " );
211
- DEBUG_OUTPUT.println (plainBuf);
212
- #endif
189
+ log_v (" Plain: %s" , plainBuf);
213
190
free (plainBuf);
214
191
} else {
215
192
// No content - but we can still have arguments in the URL.
@@ -239,12 +216,8 @@ bool WebServer::_parseRequest(WiFiClient& client) {
239
216
headerValue = req.substring (headerDiv + 2 );
240
217
_collectHeader (headerName.c_str (),headerValue.c_str ());
241
218
242
- #ifdef DEBUG_ESP_HTTP_SERVER
243
- DEBUG_OUTPUT.print (" headerName: " );
244
- DEBUG_OUTPUT.println (headerName);
245
- DEBUG_OUTPUT.print (" headerValue: " );
246
- DEBUG_OUTPUT.println (headerValue);
247
- #endif
219
+ log_v (" headerName: %s" , headerName.c_str ());
220
+ log_v (" headerValue: %s" , headerValue.c_str ());
248
221
249
222
if (headerName.equalsIgnoreCase (" Host" )){
250
223
_hostHeader = headerValue;
@@ -254,12 +227,8 @@ bool WebServer::_parseRequest(WiFiClient& client) {
254
227
}
255
228
client.flush ();
256
229
257
- #ifdef DEBUG_ESP_HTTP_SERVER
258
- DEBUG_OUTPUT.print (" Request: " );
259
- DEBUG_OUTPUT.println (url);
260
- DEBUG_OUTPUT.print (" Arguments: " );
261
- DEBUG_OUTPUT.println (searchStr);
262
- #endif
230
+ log_v (" Request: %s" , url.c_str ());
231
+ log_v (" Arguments: %s" , searchStr.c_str ());
263
232
264
233
return true ;
265
234
}
@@ -275,10 +244,7 @@ bool WebServer::_collectHeader(const char* headerName, const char* headerValue)
275
244
}
276
245
277
246
void WebServer::_parseArguments (String data) {
278
- #ifdef DEBUG_ESP_HTTP_SERVER
279
- DEBUG_OUTPUT.print (" args: " );
280
- DEBUG_OUTPUT.println (data);
281
- #endif
247
+ log_v (" args: %s" , data.c_str ());
282
248
if (_currentArgs)
283
249
delete[] _currentArgs;
284
250
_currentArgs = 0 ;
@@ -296,30 +262,17 @@ void WebServer::_parseArguments(String data) {
296
262
++i;
297
263
++_currentArgCount;
298
264
}
299
- #ifdef DEBUG_ESP_HTTP_SERVER
300
- DEBUG_OUTPUT.print (" args count: " );
301
- DEBUG_OUTPUT.println (_currentArgCount);
302
- #endif
265
+ log_v (" args count: %d" , _currentArgCount);
303
266
304
267
_currentArgs = new RequestArgument[_currentArgCount+1 ];
305
268
int pos = 0 ;
306
269
int iarg;
307
270
for (iarg = 0 ; iarg < _currentArgCount;) {
308
271
int equal_sign_index = data.indexOf (' =' , pos);
309
272
int next_arg_index = data.indexOf (' &' , pos);
310
- #ifdef DEBUG_ESP_HTTP_SERVER
311
- DEBUG_OUTPUT.print (" pos " );
312
- DEBUG_OUTPUT.print (pos);
313
- DEBUG_OUTPUT.print (" =@ " );
314
- DEBUG_OUTPUT.print (equal_sign_index);
315
- DEBUG_OUTPUT.print (" &@ " );
316
- DEBUG_OUTPUT.println (next_arg_index);
317
- #endif
273
+ log_v (" pos %d =@%d &@%d" , pos, equal_sign_index, next_arg_index);
318
274
if ((equal_sign_index == -1 ) || ((equal_sign_index > next_arg_index) && (next_arg_index != -1 ))) {
319
- #ifdef DEBUG_ESP_HTTP_SERVER
320
- DEBUG_OUTPUT.print (" arg missing value: " );
321
- DEBUG_OUTPUT.println (iarg);
322
- #endif
275
+ log_e (" arg missing value: %d" , iarg);
323
276
if (next_arg_index == -1 )
324
277
break ;
325
278
pos = next_arg_index + 1 ;
@@ -328,24 +281,14 @@ void WebServer::_parseArguments(String data) {
328
281
RequestArgument& arg = _currentArgs[iarg];
329
282
arg.key = urlDecode (data.substring (pos, equal_sign_index));
330
283
arg.value = urlDecode (data.substring (equal_sign_index + 1 , next_arg_index));
331
- #ifdef DEBUG_ESP_HTTP_SERVER
332
- DEBUG_OUTPUT.print (" arg " );
333
- DEBUG_OUTPUT.print (iarg);
334
- DEBUG_OUTPUT.print (" key: " );
335
- DEBUG_OUTPUT.print (arg.key );
336
- DEBUG_OUTPUT.print (" value: " );
337
- DEBUG_OUTPUT.println (arg.value );
338
- #endif
284
+ log_v (" arg %d key: %s value: %s" , iarg, arg.key .c_str (), arg.value .c_str ());
339
285
++iarg;
340
286
if (next_arg_index == -1 )
341
287
break ;
342
288
pos = next_arg_index + 1 ;
343
289
}
344
290
_currentArgCount = iarg;
345
- #ifdef DEBUG_ESP_HTTP_SERVER
346
- DEBUG_OUTPUT.print (" args count: " );
347
- DEBUG_OUTPUT.println (_currentArgCount);
348
- #endif
291
+ log_v (" args count: %d" , _currentArgCount);
349
292
350
293
}
351
294
@@ -371,12 +314,7 @@ int WebServer::_uploadReadByte(WiFiClient& client){
371
314
372
315
bool WebServer::_parseForm (WiFiClient& client, String boundary, uint32_t len){
373
316
(void ) len;
374
- #ifdef DEBUG_ESP_HTTP_SERVER
375
- DEBUG_OUTPUT.print (" Parse Form: Boundary: " );
376
- DEBUG_OUTPUT.print (boundary);
377
- DEBUG_OUTPUT.print (" Length: " );
378
- DEBUG_OUTPUT.println (len);
379
- #endif
317
+ log_v (" Parse Form: Boundary: %s Length: %d" , boundary.c_str (), len);
380
318
String line;
381
319
int retry = 0 ;
382
320
do {
@@ -410,18 +348,12 @@ bool WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t len){
410
348
argFilename = argName.substring (nameStart+2 , argName.length () - 1 );
411
349
argName = argName.substring (0 , argName.indexOf (' "' ));
412
350
argIsFile = true ;
413
- #ifdef DEBUG_ESP_HTTP_SERVER
414
- DEBUG_OUTPUT.print (" PostArg FileName: " );
415
- DEBUG_OUTPUT.println (argFilename);
416
- #endif
351
+ log_v (" PostArg FileName: %s" ,argFilename.c_str ());
417
352
// use GET to set the filename if uploading using blob
418
- if (argFilename == F (" blob" ) && hasArg (FPSTR (filename)))
353
+ if (argFilename == F (" blob" ) && hasArg (FPSTR (filename)))
419
354
argFilename = arg (FPSTR (filename));
420
355
}
421
- #ifdef DEBUG_ESP_HTTP_SERVER
422
- DEBUG_OUTPUT.print (" PostArg Name: " );
423
- DEBUG_OUTPUT.println (argName);
424
- #endif
356
+ log_v (" PostArg Name: %s" , argName.c_str ());
425
357
using namespace mime ;
426
358
argType = FPSTR (mimeTable[txt].mimeType );
427
359
line = client.readStringUntil (' \r ' );
@@ -432,10 +364,7 @@ bool WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t len){
432
364
client.readStringUntil (' \r ' );
433
365
client.readStringUntil (' \n ' );
434
366
}
435
- #ifdef DEBUG_ESP_HTTP_SERVER
436
- DEBUG_OUTPUT.print (" PostArg Type: " );
437
- DEBUG_OUTPUT.println (argType);
438
- #endif
367
+ log_v (" PostArg Type: %s" , argType.c_str ());
439
368
if (!argIsFile){
440
369
while (1 ){
441
370
line = client.readStringUntil (' \r ' );
@@ -444,20 +373,14 @@ bool WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t len){
444
373
if (argValue.length () > 0 ) argValue += " \n " ;
445
374
argValue += line;
446
375
}
447
- #ifdef DEBUG_ESP_HTTP_SERVER
448
- DEBUG_OUTPUT.print (" PostArg Value: " );
449
- DEBUG_OUTPUT.println (argValue);
450
- DEBUG_OUTPUT.println ();
451
- #endif
376
+ log_v (" PostArg Value: %s" , argValue.c_str ());
452
377
453
378
RequestArgument& arg = _postArgs[_postArgsLen++];
454
379
arg.key = argName;
455
380
arg.value = argValue;
456
381
457
382
if (line == (" --" +boundary+" --" )){
458
- #ifdef DEBUG_ESP_HTTP_SERVER
459
- DEBUG_OUTPUT.println (" Done Parsing POST" );
460
- #endif
383
+ log_v (" Done Parsing POST" );
461
384
break ;
462
385
}
463
386
} else {
@@ -468,12 +391,7 @@ bool WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t len){
468
391
_currentUpload->type = argType;
469
392
_currentUpload->totalSize = 0 ;
470
393
_currentUpload->currentSize = 0 ;
471
- #ifdef DEBUG_ESP_HTTP_SERVER
472
- DEBUG_OUTPUT.print (" Start File: " );
473
- DEBUG_OUTPUT.print (_currentUpload->filename );
474
- DEBUG_OUTPUT.print (" Type: " );
475
- DEBUG_OUTPUT.println (_currentUpload->type );
476
- #endif
394
+ log_v (" Start File: %s Type: %s" , _currentUpload->filename .c_str (), _currentUpload->type .c_str ());
477
395
if (_currentHandler && _currentHandler->canUpload (_currentUri))
478
396
_currentHandler->upload (*this , _currentUri, *_currentUpload);
479
397
_currentUpload->status = UPLOAD_FILE_WRITE;
@@ -518,20 +436,11 @@ bool WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t len){
518
436
_currentUpload->status = UPLOAD_FILE_END;
519
437
if (_currentHandler && _currentHandler->canUpload (_currentUri))
520
438
_currentHandler->upload (*this , _currentUri, *_currentUpload);
521
- #ifdef DEBUG_ESP_HTTP_SERVER
522
- DEBUG_OUTPUT.print (" End File: " );
523
- DEBUG_OUTPUT.print (_currentUpload->filename );
524
- DEBUG_OUTPUT.print (" Type: " );
525
- DEBUG_OUTPUT.print (_currentUpload->type );
526
- DEBUG_OUTPUT.print (" Size: " );
527
- DEBUG_OUTPUT.println (_currentUpload->totalSize );
528
- #endif
439
+ log_v (" End File: %s Type: %s Size: %d" , _currentUpload->filename .c_str (), _currentUpload->type .c_str (), _currentUpload->totalSize );
529
440
line = client.readStringUntil (0x0D );
530
441
client.readStringUntil (0x0A );
531
442
if (line == " --" ){
532
- #ifdef DEBUG_ESP_HTTP_SERVER
533
- DEBUG_OUTPUT.println (" Done Parsing POST" );
534
- #endif
443
+ log_v (" Done Parsing POST" );
535
444
break ;
536
445
}
537
446
continue ;
@@ -579,10 +488,7 @@ bool WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t len){
579
488
}
580
489
return true ;
581
490
}
582
- #ifdef DEBUG_ESP_HTTP_SERVER
583
- DEBUG_OUTPUT.print (" Error: line: " );
584
- DEBUG_OUTPUT.println (line);
585
- #endif
491
+ log_e (" Error: line: %s" , line.c_str ());
586
492
return false ;
587
493
}
588
494
0 commit comments