28
28
#include " FS.h"
29
29
#include " detail/RequestHandlersImpl.h"
30
30
31
- // #define DEBUG_ESP_HTTP_SERVER
32
- #ifdef DEBUG_ESP_PORT
33
- #define DEBUG_OUTPUT DEBUG_ESP_PORT
34
- #else
35
- #define DEBUG_OUTPUT Serial
36
- #endif
37
-
38
31
static const char AUTHORIZATION_HEADER[] PROGMEM = " Authorization" ;
39
32
static const char qop_auth[] PROGMEM = " qop=auth" ;
40
33
static const char qop_auth_quoted[] PROGMEM = " qop=\" auth\" " ;
41
34
static const char WWW_Authenticate[] PROGMEM = " WWW-Authenticate" ;
42
35
static const char Content_Length[] PROGMEM = " Content-Length" ;
43
36
44
-
45
37
template <typename ServerType>
46
38
ESP8266WebServerTemplate<ServerType>::ESP8266WebServerTemplate(IPAddress addr, int port)
47
39
: _server(addr, port)
@@ -171,9 +163,7 @@ bool ESP8266WebServerTemplate<ServerType>::authenticateDigest(const String& user
171
163
String authReq = header (FPSTR (AUTHORIZATION_HEADER));
172
164
if (authReq.startsWith (F (" Digest" ))) {
173
165
authReq = authReq.substring (7 );
174
- #ifdef DEBUG_ESP_HTTP_SERVER
175
- DEBUG_OUTPUT.println (authReq);
176
- #endif
166
+ DBGWS (" %s\n " , authReq.c_str ());
177
167
String _username = _extractParam (authReq,F (" username=\" " ));
178
168
if (!_username.length () || _username != String (username)) {
179
169
authReq = " " ;
@@ -200,9 +190,7 @@ bool ESP8266WebServerTemplate<ServerType>::authenticateDigest(const String& user
200
190
_nc = _extractParam (authReq, F (" nc=" ), ' ,' );
201
191
_cnonce = _extractParam (authReq, F (" cnonce=\" " ));
202
192
}
203
- #ifdef DEBUG_ESP_HTTP_SERVER
204
- DEBUG_OUTPUT.println (" Hash of user:realm:pass=" + H1);
205
- #endif
193
+ DBGWS (" Hash of user:realm:pass=%s\n " , H1.c_str ());
206
194
MD5Builder md5;
207
195
md5.begin ();
208
196
if (_currentMethod == HTTP_GET){
@@ -218,9 +206,7 @@ bool ESP8266WebServerTemplate<ServerType>::authenticateDigest(const String& user
218
206
}
219
207
md5.calculate ();
220
208
String _H2 = md5.toString ();
221
- #ifdef DEBUG_ESP_HTTP_SERVER
222
- DEBUG_OUTPUT.println (" Hash of GET:uri=" + _H2);
223
- #endif
209
+ DBGWS (" Hash of GET:uri=%s\n " , _H2.c_str ());
224
210
md5.begin ();
225
211
if (authReq.indexOf (FPSTR (qop_auth)) != -1 || authReq.indexOf (FPSTR (qop_auth_quoted)) != -1 ) {
226
212
md5.add (H1 + ' :' + _nonce + ' :' + _nc + ' :' + _cnonce + F (" :auth:" ) + _H2);
@@ -229,9 +215,7 @@ bool ESP8266WebServerTemplate<ServerType>::authenticateDigest(const String& user
229
215
}
230
216
md5.calculate ();
231
217
String _responsecheck = md5.toString ();
232
- #ifdef DEBUG_ESP_HTTP_SERVER
233
- DEBUG_OUTPUT.println (" The Proper response=" +_responsecheck);
234
- #endif
218
+ DBGWS (" The Proper response=%s\n " , _responsecheck.c_str ());
235
219
if (_response == _responsecheck){
236
220
authReq = " " ;
237
221
return true ;
@@ -315,9 +299,7 @@ void ESP8266WebServerTemplate<ServerType>::handleClient() {
315
299
return ;
316
300
}
317
301
318
- #ifdef DEBUG_ESP_HTTP_SERVER
319
- DEBUG_OUTPUT.println (" New client" );
320
- #endif
302
+ DBGWS (" New client\n " );
321
303
322
304
_currentClient = client;
323
305
_currentStatus = HC_WAIT_READ;
@@ -327,6 +309,13 @@ void ESP8266WebServerTemplate<ServerType>::handleClient() {
327
309
bool keepCurrentClient = false ;
328
310
bool callYield = false ;
329
311
312
+ DBGWS (" http-server loop: conn=%d avail=%d status=%s\n " ,
313
+ _currentClient.connected (), _currentClient.available (),
314
+ _currentStatus==HC_NONE?" none" :
315
+ _currentStatus==HC_WAIT_READ?" wait-read" :
316
+ _currentStatus==HC_WAIT_CLOSE?" wait-close" :
317
+ " ??" );
318
+
330
319
if (_currentClient.connected () || _currentClient.available ()) {
331
320
if (_currentClient.available () && _keepAlive) {
332
321
_currentStatus = HC_WAIT_READ;
@@ -339,34 +328,57 @@ void ESP8266WebServerTemplate<ServerType>::handleClient() {
339
328
case HC_WAIT_READ:
340
329
// Wait for data from client to become available
341
330
if (_currentClient.available ()) {
342
- if (_parseRequest (_currentClient)) {
331
+ switch (_parseRequest (_currentClient))
332
+ {
333
+ case CLIENT_REQUEST_CAN_CONTINUE:
343
334
_currentClient.setTimeout (HTTP_MAX_SEND_WAIT);
344
335
_contentLength = CONTENT_LENGTH_NOT_SET;
345
336
_handleRequest ();
346
-
347
- if (_currentClient.connected ()) {
337
+ /* fallthrough */
338
+ case CLIENT_REQUEST_IS_HANDLED:
339
+ if (_currentClient.connected () || _currentClient.available ()) {
348
340
_currentStatus = HC_WAIT_CLOSE;
349
341
_statusChange = millis ();
350
342
keepCurrentClient = true ;
351
343
}
352
- }
353
- } else { // !_currentClient.available()
344
+ else
345
+ DBGWS (" webserver: peer has closed after served\n " );
346
+ break ;
347
+ case CLIENT_MUST_STOP:
348
+ DBGWS (" Close client\n " );
349
+ _currentClient.stop ();
350
+ break ;
351
+ case CLIENT_IS_GIVEN:
352
+ // client must not be stopped but must not be handled here anymore
353
+ // (example: tcp connection given to websocket)
354
+ DBGWS (" Give client\n " );
355
+ break ;
356
+ } // switch _parseRequest()
357
+ } else {
358
+ // !_currentClient.available(): waiting for more data
354
359
if (millis () - _statusChange <= HTTP_MAX_DATA_WAIT) {
355
360
keepCurrentClient = true ;
356
361
}
362
+ else
363
+ DBGWS (" webserver: closing after read timeout\n " );
357
364
callYield = true ;
358
365
}
359
366
break ;
360
367
case HC_WAIT_CLOSE:
361
368
// Wait for client to close the connection
362
- if (millis () - _statusChange <= HTTP_MAX_CLOSE_WAIT) {
369
+ if (!_server. available () && ( millis () - _statusChange <= HTTP_MAX_CLOSE_WAIT) ) {
363
370
keepCurrentClient = true ;
364
371
callYield = true ;
372
+ if (_currentClient.available ())
373
+ // continue serving current client
374
+ _currentStatus = HC_WAIT_READ;
365
375
}
366
- }
376
+ break ;
377
+ } // switch _currentStatus
367
378
}
368
379
369
380
if (!keepCurrentClient) {
381
+ DBGWS (" Drop client\n " );
370
382
_currentClient = ClientType ();
371
383
_currentStatus = HC_NONE;
372
384
_currentUpload.reset ();
@@ -687,17 +699,13 @@ template <typename ServerType>
687
699
void ESP8266WebServerTemplate<ServerType>::_handleRequest() {
688
700
bool handled = false ;
689
701
if (!_currentHandler){
690
- #ifdef DEBUG_ESP_HTTP_SERVER
691
- DEBUG_OUTPUT.println (" request handler not found" );
692
- #endif
702
+ DBGWS (" request handler not found\n " );
693
703
}
694
704
else {
695
705
handled = _currentHandler->handle (*this , _currentMethod, _currentUri);
696
- #ifdef DEBUG_ESP_HTTP_SERVER
697
706
if (!handled) {
698
- DEBUG_OUTPUT. println (" request handler failed to handle request" );
707
+ DBGWS (" request handler failed to handle request\n " );
699
708
}
700
- #endif
701
709
}
702
710
if (!handled && _notFoundHandler) {
703
711
_notFoundHandler ();
0 commit comments