2828#include " FS.h"
2929#include " detail/RequestHandlersImpl.h"
3030
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-
3831static const char AUTHORIZATION_HEADER[] PROGMEM = " Authorization" ;
3932static const char qop_auth[] PROGMEM = " qop=auth" ;
4033static const char qop_auth_quoted[] PROGMEM = " qop=\" auth\" " ;
4134static const char WWW_Authenticate[] PROGMEM = " WWW-Authenticate" ;
4235static const char Content_Length[] PROGMEM = " Content-Length" ;
4336
44-
4537template <typename ServerType>
4638ESP8266WebServerTemplate<ServerType>::ESP8266WebServerTemplate(IPAddress addr, int port)
4739: _server(addr, port)
@@ -171,9 +163,7 @@ bool ESP8266WebServerTemplate<ServerType>::authenticateDigest(const String& user
171163 String authReq = header (FPSTR (AUTHORIZATION_HEADER));
172164 if (authReq.startsWith (F (" Digest" ))) {
173165 authReq = authReq.substring (7 );
174- #ifdef DEBUG_ESP_HTTP_SERVER
175- DEBUG_OUTPUT.println (authReq);
176- #endif
166+ DBGWS (" %s\n " , authReq.c_str ());
177167 String _username = _extractParam (authReq,F (" username=\" " ));
178168 if (!_username.length () || _username != String (username)) {
179169 authReq = " " ;
@@ -200,9 +190,7 @@ bool ESP8266WebServerTemplate<ServerType>::authenticateDigest(const String& user
200190 _nc = _extractParam (authReq, F (" nc=" ), ' ,' );
201191 _cnonce = _extractParam (authReq, F (" cnonce=\" " ));
202192 }
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 ());
206194 MD5Builder md5;
207195 md5.begin ();
208196 if (_currentMethod == HTTP_GET){
@@ -218,9 +206,7 @@ bool ESP8266WebServerTemplate<ServerType>::authenticateDigest(const String& user
218206 }
219207 md5.calculate ();
220208 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 ());
224210 md5.begin ();
225211 if (authReq.indexOf (FPSTR (qop_auth)) != -1 || authReq.indexOf (FPSTR (qop_auth_quoted)) != -1 ) {
226212 md5.add (H1 + ' :' + _nonce + ' :' + _nc + ' :' + _cnonce + F (" :auth:" ) + _H2);
@@ -229,9 +215,7 @@ bool ESP8266WebServerTemplate<ServerType>::authenticateDigest(const String& user
229215 }
230216 md5.calculate ();
231217 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 ());
235219 if (_response == _responsecheck){
236220 authReq = " " ;
237221 return true ;
@@ -315,9 +299,7 @@ void ESP8266WebServerTemplate<ServerType>::handleClient() {
315299 return ;
316300 }
317301
318- #ifdef DEBUG_ESP_HTTP_SERVER
319- DEBUG_OUTPUT.println (" New client" );
320- #endif
302+ DBGWS (" New client\n " );
321303
322304 _currentClient = client;
323305 _currentStatus = HC_WAIT_READ;
@@ -327,6 +309,13 @@ void ESP8266WebServerTemplate<ServerType>::handleClient() {
327309 bool keepCurrentClient = false ;
328310 bool callYield = false ;
329311
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+
330319 if (_currentClient.connected () || _currentClient.available ()) {
331320 if (_currentClient.available () && _keepAlive) {
332321 _currentStatus = HC_WAIT_READ;
@@ -339,34 +328,57 @@ void ESP8266WebServerTemplate<ServerType>::handleClient() {
339328 case HC_WAIT_READ:
340329 // Wait for data from client to become available
341330 if (_currentClient.available ()) {
342- if (_parseRequest (_currentClient)) {
331+ switch (_parseRequest (_currentClient))
332+ {
333+ case CLIENT_REQUEST_CAN_CONTINUE:
343334 _currentClient.setTimeout (HTTP_MAX_SEND_WAIT);
344335 _contentLength = CONTENT_LENGTH_NOT_SET;
345336 _handleRequest ();
346-
347- if (_currentClient.connected ()) {
337+ /* fallthrough */
338+ case CLIENT_REQUEST_IS_HANDLED:
339+ if (_currentClient.connected () || _currentClient.available ()) {
348340 _currentStatus = HC_WAIT_CLOSE;
349341 _statusChange = millis ();
350342 keepCurrentClient = true ;
351343 }
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
354359 if (millis () - _statusChange <= HTTP_MAX_DATA_WAIT) {
355360 keepCurrentClient = true ;
356361 }
362+ else
363+ DBGWS (" webserver: closing after read timeout\n " );
357364 callYield = true ;
358365 }
359366 break ;
360367 case HC_WAIT_CLOSE:
361368 // 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) ) {
363370 keepCurrentClient = true ;
364371 callYield = true ;
372+ if (_currentClient.available ())
373+ // continue serving current client
374+ _currentStatus = HC_WAIT_READ;
365375 }
366- }
376+ break ;
377+ } // switch _currentStatus
367378 }
368379
369380 if (!keepCurrentClient) {
381+ DBGWS (" Drop client\n " );
370382 _currentClient = ClientType ();
371383 _currentStatus = HC_NONE;
372384 _currentUpload.reset ();
@@ -687,17 +699,13 @@ template <typename ServerType>
687699void ESP8266WebServerTemplate<ServerType>::_handleRequest() {
688700 bool handled = false ;
689701 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 " );
693703 }
694704 else {
695705 handled = _currentHandler->handle (*this , _currentMethod, _currentUri);
696- #ifdef DEBUG_ESP_HTTP_SERVER
697706 if (!handled) {
698- DEBUG_OUTPUT. println (" request handler failed to handle request" );
707+ DBGWS (" request handler failed to handle request\n " );
699708 }
700- #endif
701709 }
702710 if (!handled && _notFoundHandler) {
703711 _notFoundHandler ();
0 commit comments