@@ -37,15 +37,15 @@ int HTTPConnection::initialize(int serverSocketID, HTTPHeaders *defaultHeaders)
37
37
38
38
// Build up SSL Connection context if the socket has been created successfully
39
39
if (_socket >= 0 ) {
40
- HTTPS_DLOGHEX ( " [-->] New connection. Socket fid is: " , _socket);
40
+ HTTPS_LOGI ( " New connection. Socket FID=%d " , _socket);
41
41
_connectionState = STATE_INITIAL;
42
42
_httpHeaders = new HTTPHeaders ();
43
43
refreshTimeout ();
44
44
return _socket;
45
45
46
46
}
47
47
48
- HTTPS_DLOG ( " [ERR] Could not accept() new connection" );
48
+ HTTPS_LOGE ( " Could not accept() new connection" );
49
49
50
50
_connectionState = STATE_ERROR;
51
51
_clientState = CSTATE_ACTIVE;
@@ -110,7 +110,7 @@ void HTTPConnection::closeConnection() {
110
110
111
111
// Tear down the socket
112
112
if (_socket >= 0 ) {
113
- HTTPS_DLOGHEX ( " [<--] Connection has been closed. fid = " , _socket);
113
+ HTTPS_LOGI ( " Connection closed. Socket FID=%d " , _socket);
114
114
close (_socket);
115
115
_socket = -1 ;
116
116
_addrLen = 0 ;
@@ -121,13 +121,13 @@ void HTTPConnection::closeConnection() {
121
121
}
122
122
123
123
if (_httpHeaders != NULL ) {
124
- HTTPS_DLOG ( " [ ] Free headers" );
124
+ HTTPS_LOGD ( " Free headers" );
125
125
delete _httpHeaders;
126
126
_httpHeaders = NULL ;
127
127
}
128
128
129
129
if (_wsHandler != nullptr ) {
130
- HTTPS_DLOG ( " [ ] Freeing WS Handler" );
130
+ HTTPS_LOGD ( " Free WS Handler" );
131
131
delete _wsHandler;
132
132
}
133
133
}
@@ -165,7 +165,7 @@ int HTTPConnection::updateBuffer() {
165
165
if (_bufferUnusedIdx < HTTPS_CONNECTION_DATA_CHUNK_SIZE) {
166
166
if (canReadData ()) {
167
167
168
- HTTPS_DLOGHEX ( " [ ] There is data on the connection socket. fid= " , _socket)
168
+ HTTPS_LOGD ( " Data on Socket FID=%d " , _socket);
169
169
170
170
int readReturnCode;
171
171
@@ -188,13 +188,13 @@ int HTTPConnection::updateBuffer() {
188
188
} else if (readReturnCode == 0 ) {
189
189
// The connection has been closed by the client
190
190
_clientState = CSTATE_CLOSED;
191
- HTTPS_DLOGHEX ( " [ x ] Client closed connection, fid= " , _socket);
191
+ HTTPS_LOGI ( " Client closed connection, FID=%d " , _socket);
192
192
// TODO: If we are in state websocket, we might need to do something here
193
193
return 0 ;
194
194
} else {
195
195
// An error occured
196
196
_connectionState = STATE_ERROR;
197
- HTTPS_DLOGHEX ( " [ERR] An receive error occured, fid= " , _socket);
197
+ HTTPS_LOGE ( " An receive error occured, FID=%d " , _socket);
198
198
closeConnection ();
199
199
return -1 ;
200
200
}
@@ -261,7 +261,6 @@ size_t HTTPConnection::readBytesToBuffer(byte* buffer, size_t length) {
261
261
void HTTPConnection::serverError () {
262
262
_connectionState = STATE_ERROR;
263
263
264
- Serial.println (" Server error" );
265
264
char staticResponse[] = " HTTP/1.1 500 Internal Server Error\r\n Server: esp32https\r\n Connection:close\r\n Content-Type: text/html\r\n Content-Length:34\r\n\r\n <h1>500 Internal Server Error</h1>" ;
266
265
writeBuffer ((byte*)staticResponse, strlen (staticResponse));
267
266
closeConnection ();
@@ -271,7 +270,6 @@ void HTTPConnection::serverError() {
271
270
void HTTPConnection::clientError () {
272
271
_connectionState = STATE_ERROR;
273
272
274
- Serial.println (" Client error" );
275
273
char staticResponse[] = " HTTP/1.1 400 Bad Request\r\n Server: esp32https\r\n Connection:close\r\n Content-Type: text/html\r\n Content-Length:26\r\n\r\n <h1>400 Bad Request</h1>" ;
276
274
writeBuffer ((byte*)staticResponse, strlen (staticResponse));
277
275
closeConnection ();
@@ -290,7 +288,7 @@ void HTTPConnection::readLine(int lengthLimit) {
290
288
return ;
291
289
} else {
292
290
// Line has not been terminated by \r\n
293
- HTTPS_DLOG ( " [ERR] Line that has not been terminated by \\ r\\ n (got only \\ r). Client error. " );
291
+ HTTPS_LOGW ( " Line without \\ r\\ n (got only \\ r). FID=%d " , _socket );
294
292
clientError ();
295
293
return ;
296
294
}
@@ -302,7 +300,7 @@ void HTTPConnection::readLine(int lengthLimit) {
302
300
303
301
// Check that the max request string size is not exceeded
304
302
if (_parserLine.text .length () > lengthLimit) {
305
- HTTPS_DLOG ( " [ERR] Line length exceeded. Server error. " );
303
+ HTTPS_LOGW ( " Header length exceeded. FID=%d " , _socket );
306
304
serverError ();
307
305
return ;
308
306
}
@@ -339,15 +337,15 @@ void HTTPConnection::loop() {
339
337
updateBuffer ();
340
338
341
339
if (_clientState == CSTATE_CLOSED) {
342
- HTTPS_DLOGHEX ( " [ ] Client closed in state " , _clientState)
340
+ HTTPS_LOGI ( " Client closed (FID=%d, cstate=%d) " , _socket, _clientState);
343
341
}
344
342
345
343
if (_clientState == CSTATE_CLOSED && _bufferProcessed == _bufferUnusedIdx && _connectionState < STATE_HEADERS_FINISHED) {
346
344
closeConnection ();
347
345
}
348
346
349
347
if (!isClosed () && isTimeoutExceeded ()) {
350
- HTTPS_DLOGHEX ( " [zZz] Connection timeout exceeded, closing connection. fid= " , _socket)
348
+ HTTPS_LOGI ( " Connection timeout. FID=%d " , _socket);
351
349
closeConnection ();
352
350
}
353
351
@@ -360,7 +358,7 @@ void HTTPConnection::loop() {
360
358
// Find the method
361
359
size_t spaceAfterMethodIdx = _parserLine.text .find (' ' );
362
360
if (spaceAfterMethodIdx == std::string::npos) {
363
- HTTPS_DLOG ( " [ERR] Missing space after HTTP method. Client Error. " )
361
+ HTTPS_LOGW ( " Missing space after method" );
364
362
clientError ();
365
363
break ;
366
364
}
@@ -369,15 +367,15 @@ void HTTPConnection::loop() {
369
367
// Find the resource string:
370
368
size_t spaceAfterResourceIdx = _parserLine.text .find (' ' , spaceAfterMethodIdx + 1 );
371
369
if (spaceAfterResourceIdx == std::string::npos) {
372
- HTTPS_DLOG ( " [ERR] Missing space after HTTP resource. Client Error. " )
370
+ HTTPS_LOGW ( " Missing space after resource" );
373
371
clientError ();
374
372
break ;
375
373
}
376
374
_httpResource = _parserLine.text .substr (spaceAfterMethodIdx + 1 , spaceAfterResourceIdx - _httpMethod.length () - 1 );
377
375
378
376
_parserLine.parsingFinished = false ;
379
377
_parserLine.text = " " ;
380
- HTTPS_DLOG (( " [ ] Request line finished: method= " + _httpMethod+ " , resource= " + _httpResource) .c_str ());
378
+ HTTPS_LOGI ( " Request: %s %s (FID=%d) " , _httpMethod. c_str (), _httpResource.c_str (), _socket );
381
379
_connectionState = STATE_REQUEST_FINISHED;
382
380
}
383
381
@@ -389,7 +387,7 @@ void HTTPConnection::loop() {
389
387
if (_parserLine.parsingFinished && _connectionState != STATE_ERROR) {
390
388
391
389
if (_parserLine.text .empty ()) {
392
- HTTPS_DLOG ( " [ ] Headers finished" );
390
+ HTTPS_LOGD ( " Headers finished, FID=%d " , _socket );
393
391
_connectionState = STATE_HEADERS_FINISHED;
394
392
395
393
// Break, so that the rest of the body does not get flushed through
@@ -403,10 +401,9 @@ void HTTPConnection::loop() {
403
401
_parserLine.text .substr (0 , idxColon),
404
402
_parserLine.text .substr (idxColon+2 )
405
403
));
406
- HTTPS_DLOG (( " [ ] Header: " + _parserLine.text .substr (0 , idxColon) + " : " + _parserLine.text .substr (idxColon+2 )) .c_str ());
404
+ HTTPS_LOGD ( " Header: %s = %s (FID=%d) " , _parserLine.text .substr (0 , idxColon). c_str (), _parserLine.text .substr (idxColon+2 ).c_str (), _socket );
407
405
} else {
408
- HTTPS_DLOG (" Malformed header line detected. Client error." );
409
- HTTPS_DLOG (_parserLine.text .c_str ());
406
+ HTTPS_LOGW (" Malformed request header: %s" , _parserLine.text .c_str ());
410
407
clientError ();
411
408
break ;
412
409
}
@@ -420,7 +417,7 @@ void HTTPConnection::loop() {
420
417
break ;
421
418
case STATE_HEADERS_FINISHED: // Handle body
422
419
{
423
- HTTPS_DLOG ( " [ ] Resolving resource..." );
420
+ HTTPS_LOGD ( " Resolving resource..." );
424
421
ResolvedResource resolvedResource;
425
422
426
423
// Check which kind of node we need (Websocket or regular)
@@ -445,10 +442,10 @@ void HTTPConnection::loop() {
445
442
);
446
443
}
447
444
if (std::string (" keep-alive" ).compare (connectionHeaderValue)==0 ) {
448
- HTTPS_DLOGHEX ( " [ ] Keep-Alive activated. fid= " , _socket);
445
+ HTTPS_LOGD ( " Keep-Alive activated. FID=%d " , _socket);
449
446
_isKeepAlive = true ;
450
447
} else {
451
- HTTPS_DLOGHEX ( " [ ] Keep-Alive disabled. fid= " , _socket);
448
+ HTTPS_LOGD ( " Keep-Alive disabled. FID=%d " , _socket);
452
449
_isKeepAlive = false ;
453
450
}
454
451
} else {
@@ -505,7 +502,7 @@ void HTTPConnection::loop() {
505
502
// However, if it does not, we need to clear the request body now,
506
503
// because otherwise it would be parsed in the next request.
507
504
if (!req.requestComplete ()) {
508
- HTTPS_DLOG ( " [ERR] Callback function did not parse full request body" );
505
+ HTTPS_LOGW ( " Callback function did not parse full request body" );
509
506
req.discardRequestBody ();
510
507
}
511
508
@@ -516,7 +513,7 @@ void HTTPConnection::loop() {
516
513
_connectionState = STATE_WEBSOCKET;
517
514
} else {
518
515
// Handling the request is done
519
- HTTPS_DLOG ( " [ ] Handler function done, request complete" );
516
+ HTTPS_LOGD ( " Handler function done, request complete" );
520
517
521
518
// Now we need to check if we can use keep-alive to reuse the SSL connection
522
519
// However, if the client did not set content-size or defined connection: close,
@@ -548,7 +545,7 @@ void HTTPConnection::loop() {
548
545
}
549
546
} else {
550
547
// No match (no default route configured, nothing does match)
551
- HTTPS_DLOG ( " [ERR] Could not find a matching resource. Server error. " );
548
+ HTTPS_LOGW ( " Could not find a matching resource" );
552
549
serverError ();
553
550
}
554
551
@@ -563,12 +560,12 @@ void HTTPConnection::loop() {
563
560
case STATE_WEBSOCKET: // Do handling of the websocket
564
561
refreshTimeout (); // don't timeout websocket connection
565
562
if (pendingBufferSize () > 0 ) {
566
- HTTPS_DLOG ( " [ ] websocket handler" );
563
+ HTTPS_LOGD ( " Calling WS handler, FID=%d " , _socket );
567
564
_wsHandler->loop ();
568
565
}
569
566
// If the handler has terminated the connection, clean up and close the socket too
570
567
if (_wsHandler->closed ()) {
571
- HTTPS_DLOG ( " [ ] WS Connection closed. Freeing WS Handler" );
568
+ HTTPS_LOGI ( " WS closed, freeing Handler, FID=%d " , _socket );
572
569
delete _wsHandler;
573
570
_wsHandler = nullptr ;
574
571
_connectionState = STATE_CLOSING;
@@ -589,7 +586,7 @@ bool HTTPConnection::checkWebsocket() {
589
586
!_httpHeaders->getValue (" Sec-WebSocket-Key" ).empty () &&
590
587
_httpHeaders->getValue (" Sec-WebSocket-Version" ) == " 13" ) {
591
588
592
- HTTPS_DLOG ( " [-->] Websocket detected " );
589
+ HTTPS_LOGI ( " Upgrading to WS, FID=%d " , _socket );
593
590
return true ;
594
591
} else
595
592
return false ;
0 commit comments