@@ -259,20 +259,19 @@ size_t HTTPConnection::readBytesToBuffer(byte* buffer, size_t length) {
259259 return recv (_socket, buffer, length, MSG_WAITALL | MSG_DONTWAIT);
260260}
261261
262- void HTTPConnection::serverError ( ) {
262+ void HTTPConnection::raiseError ( uint16_t code, std::string reason ) {
263263 _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 ());
276275 closeConnection ();
277276}
278277
@@ -290,7 +289,7 @@ void HTTPConnection::readLine(int lengthLimit) {
290289 } else {
291290 // Line has not been terminated by \r\n
292291 HTTPS_LOGW (" Line without \\ r\\ n (got only \\ r). FID=%d" , _socket);
293- clientError ( );
292+ raiseError ( 400 , " Bad Request " );
294293 return ;
295294 }
296295 }
@@ -302,7 +301,7 @@ void HTTPConnection::readLine(int lengthLimit) {
302301 // Check that the max request string size is not exceeded
303302 if (_parserLine.text .length () > lengthLimit) {
304303 HTTPS_LOGW (" Header length exceeded. FID=%d" , _socket);
305- serverError ( );
304+ raiseError ( 431 , " Request Header Fields Too Large " );
306305 return ;
307306 }
308307 }
@@ -320,7 +319,7 @@ void HTTPConnection::signalClientClose() {
320319 */
321320void HTTPConnection::signalRequestError () {
322321 // TODO: Check that no response has been transmitted yet
323- serverError ( );
322+ raiseError ( 400 , " Bad Request " );
324323}
325324
326325/* *
@@ -360,7 +359,7 @@ void HTTPConnection::loop() {
360359 size_t spaceAfterMethodIdx = _parserLine.text .find (' ' );
361360 if (spaceAfterMethodIdx == std::string::npos) {
362361 HTTPS_LOGW (" Missing space after method" );
363- clientError ( );
362+ raiseError ( 400 , " Bad Request " );
364363 break ;
365364 }
366365 _httpMethod = _parserLine.text .substr (0 , spaceAfterMethodIdx);
@@ -369,7 +368,7 @@ void HTTPConnection::loop() {
369368 size_t spaceAfterResourceIdx = _parserLine.text .find (' ' , spaceAfterMethodIdx + 1 );
370369 if (spaceAfterResourceIdx == std::string::npos) {
371370 HTTPS_LOGW (" Missing space after resource" );
372- clientError ( );
371+ raiseError ( 400 , " Bad Request " );
373372 break ;
374373 }
375374 _httpResource = _parserLine.text .substr (spaceAfterMethodIdx + 1 , spaceAfterResourceIdx - _httpMethod.length () - 1 );
@@ -405,7 +404,7 @@ void HTTPConnection::loop() {
405404 HTTPS_LOGD (" Header: %s = %s (FID=%d)" , _parserLine.text .substr (0 , idxColon).c_str (), _parserLine.text .substr (idxColon+2 ).c_str (), _socket);
406405 } else {
407406 HTTPS_LOGW (" Malformed request header: %s" , _parserLine.text .c_str ());
408- clientError ( );
407+ raiseError ( 400 , " Bad Request " );
409408 break ;
410409 }
411410 }
@@ -547,7 +546,7 @@ void HTTPConnection::loop() {
547546 } else {
548547 // No match (no default route configured, nothing does match)
549548 HTTPS_LOGW (" Could not find a matching resource" );
550- serverError ( );
549+ raiseError ( 404 , " Not Found " );
551550 }
552551
553552 }
0 commit comments