@@ -259,20 +259,19 @@ size_t HTTPConnection::readBytesToBuffer(byte* buffer, size_t length) {
259
259
return recv (_socket, buffer, length, MSG_WAITALL | MSG_DONTWAIT);
260
260
}
261
261
262
- void HTTPConnection::serverError ( ) {
262
+ void HTTPConnection::raiseError ( uint16_t code, std::string reason ) {
263
263
_connectionState = STATE_ERROR;
264
-
265
- 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
- writeBuffer ((byte*)staticResponse, strlen (staticResponse));
267
- closeConnection ();
268
- }
269
-
270
-
271
- void HTTPConnection::clientError () {
272
- _connectionState = STATE_ERROR;
273
-
274
- 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>" ;
275
- writeBuffer ((byte*)staticResponse, strlen (staticResponse));
264
+ std::string sCode = intToString (code);
265
+
266
+ char headers[] = " \r\n Connection: close\r\n Content-Type: text/plain;charset=utf8\r\n\r\n " ;
267
+ writeBuffer ((byte*)" HTTP/1.1 " , 9 );
268
+ writeBuffer ((byte*)sCode .c_str (), sCode .length ());
269
+ writeBuffer ((byte*)" " , 1 );
270
+ writeBuffer ((byte*)(reason.c_str ()), reason.length ());
271
+ writeBuffer ((byte*)headers, strlen (headers));
272
+ writeBuffer ((byte*)sCode .c_str (), sCode .length ());
273
+ writeBuffer ((byte*)" " , 1 );
274
+ writeBuffer ((byte*)(reason.c_str ()), reason.length ());
276
275
closeConnection ();
277
276
}
278
277
@@ -290,7 +289,7 @@ void HTTPConnection::readLine(int lengthLimit) {
290
289
} else {
291
290
// Line has not been terminated by \r\n
292
291
HTTPS_LOGW (" Line without \\ r\\ n (got only \\ r). FID=%d" , _socket);
293
- clientError ( );
292
+ raiseError ( 400 , " Bad Request " );
294
293
return ;
295
294
}
296
295
}
@@ -302,7 +301,7 @@ void HTTPConnection::readLine(int lengthLimit) {
302
301
// Check that the max request string size is not exceeded
303
302
if (_parserLine.text .length () > lengthLimit) {
304
303
HTTPS_LOGW (" Header length exceeded. FID=%d" , _socket);
305
- serverError ( );
304
+ raiseError ( 431 , " Request Header Fields Too Large " );
306
305
return ;
307
306
}
308
307
}
@@ -320,7 +319,7 @@ void HTTPConnection::signalClientClose() {
320
319
*/
321
320
void HTTPConnection::signalRequestError () {
322
321
// TODO: Check that no response has been transmitted yet
323
- serverError ( );
322
+ raiseError ( 400 , " Bad Request " );
324
323
}
325
324
326
325
/* *
@@ -360,7 +359,7 @@ void HTTPConnection::loop() {
360
359
size_t spaceAfterMethodIdx = _parserLine.text .find (' ' );
361
360
if (spaceAfterMethodIdx == std::string::npos) {
362
361
HTTPS_LOGW (" Missing space after method" );
363
- clientError ( );
362
+ raiseError ( 400 , " Bad Request " );
364
363
break ;
365
364
}
366
365
_httpMethod = _parserLine.text .substr (0 , spaceAfterMethodIdx);
@@ -369,7 +368,7 @@ void HTTPConnection::loop() {
369
368
size_t spaceAfterResourceIdx = _parserLine.text .find (' ' , spaceAfterMethodIdx + 1 );
370
369
if (spaceAfterResourceIdx == std::string::npos) {
371
370
HTTPS_LOGW (" Missing space after resource" );
372
- clientError ( );
371
+ raiseError ( 400 , " Bad Request " );
373
372
break ;
374
373
}
375
374
_httpResource = _parserLine.text .substr (spaceAfterMethodIdx + 1 , spaceAfterResourceIdx - _httpMethod.length () - 1 );
@@ -405,7 +404,7 @@ void HTTPConnection::loop() {
405
404
HTTPS_LOGD (" Header: %s = %s (FID=%d)" , _parserLine.text .substr (0 , idxColon).c_str (), _parserLine.text .substr (idxColon+2 ).c_str (), _socket);
406
405
} else {
407
406
HTTPS_LOGW (" Malformed request header: %s" , _parserLine.text .c_str ());
408
- clientError ( );
407
+ raiseError ( 400 , " Bad Request " );
409
408
break ;
410
409
}
411
410
}
@@ -547,7 +546,7 @@ void HTTPConnection::loop() {
547
546
} else {
548
547
// No match (no default route configured, nothing does match)
549
548
HTTPS_LOGW (" Could not find a matching resource" );
550
- serverError ( );
549
+ raiseError ( 404 , " Not Found " );
551
550
}
552
551
553
552
}
0 commit comments