Updated the HttpClient class and the WEBServer class

This commit is contained in:
anschrammh 2020-07-16 22:20:23 +02:00
parent abed6ebd9b
commit 944c11477b
4 changed files with 160 additions and 102 deletions

View File

@ -1,20 +1,21 @@
#include "HttpClient.h" #include "HttpClient.h"
#include <IPAddress.h> #include <IPAddress.h>
#define DEBUG_HTTP_CLIENT //#define DEBUG_HTTP_CLIENT
/* /*
* End of HttpClientHelper * End of HttpClientHelper
*/ */
HttpClient::HttpClient(const char *address, uint16_t port) : WiFiClient(), _connectionStatus(NO_ATTEMPT), _resource(NULL), _pAddress(address), _port(0), _keepAlive(false), _maxRetries(-1), _retries(0), _isIp(false), _httpCode(HTTP_CODE::UNDEFINED_CODE), _httpCodeParsed(false) HttpClient::HttpClient(const char *address, uint16_t port, uint32_t timeout) : WiFiClient(), _connectionStatus(NO_ATTEMPT), _resource(NULL), _pAddress(address), _port(0), _keepAlive(false), _maxRetries(-1), _retries(0), _isIp(false), _httpCode(HTTP_CODE::UNDEFINED_CODE), _httpCodeParsed(false)
{ {
_port = port; _port = port;
setTimeout(timeout);
connectByHostOrIp(); connectByHostOrIp();
} }
HttpClient::HttpClient(const char *address, const char *resource, uint16_t port) : HttpClient(address, port) HttpClient::HttpClient(const char *address, const char *resource, uint16_t port, uint32_t timeout) : HttpClient(address, port, timeout)
{ {
if(resource != NULL) if(resource != NULL)
{ {
@ -51,6 +52,9 @@ HttpClient::~HttpClient()
boolean HttpClient::connectByHostOrIp() boolean HttpClient::connectByHostOrIp()
{ {
IPAddress ipAddress; IPAddress ipAddress;
#ifdef DEBUG_HTTP_CLIENT
Serial.printf("About to connect\n");
#endif
if(ipAddress.fromString(_pAddress)) if(ipAddress.fromString(_pAddress))
{ {
_isIp = true; _isIp = true;
@ -71,7 +75,7 @@ boolean HttpClient::connectByHostOrIp()
return _connectionStatus == SUCCESSFUL; return _connectionStatus == SUCCESSFUL;
} }
boolean HttpClient::sendHttpQuery(const char *resource, HttpRequestMethod method, Dictionary<DictionaryHelper::StringEntity> *getData, Dictionary<DictionaryHelper::StringEntity> *postData) boolean HttpClient::sendHttpQuery(const char *resource, HttpRequestMethod method, Dictionary<DictionaryHelper::StringEntity> *getData, Dictionary<DictionaryHelper::StringEntity> *postData, Dictionary<DictionaryHelper::StringEntity> *headerData)
{ {
if(resource != NULL) //We overwrite the resource if it has been already defined if(resource != NULL) //We overwrite the resource if it has been already defined
{ {
@ -83,10 +87,10 @@ boolean HttpClient::sendHttpQuery(const char *resource, HttpRequestMethod method
} }
//We check the result //We check the result
return sendHttpQuery(method, getData, postData); return sendHttpQuery(method, getData, postData, headerData);
} }
boolean HttpClient::sendHttpQuery(HttpRequestMethod method, Dictionary<DictionaryHelper::StringEntity> *getData, Dictionary<DictionaryHelper::StringEntity> *postData) boolean HttpClient::sendHttpQuery(HttpRequestMethod method, Dictionary<DictionaryHelper::StringEntity> *getData, Dictionary<DictionaryHelper::StringEntity> *postData, Dictionary<DictionaryHelper::StringEntity> *headerData)
{ {
//We reset this two flags //We reset this two flags
_httpCode = HTTP_CODE::UNDEFINED_CODE; _httpCode = HTTP_CODE::UNDEFINED_CODE;
@ -139,14 +143,14 @@ boolean HttpClient::sendHttpQuery(HttpRequestMethod method, Dictionary<Dictionar
printf("GET %s", _resource == NULL ? "/" : _resource); printf("GET %s", _resource == NULL ? "/" : _resource);
//Here we send the parameters //Here we send the parameters
sendUriWithGetParams(getData); sendUriWithGetParams(getData);
sendHeader(); sendHeader(HttpMIMEType::UNKNOWN_MIME, 0, headerData);
break; break;
case HttpRequestMethod::POST: case HttpRequestMethod::POST:
//It is necessary to compute the content length //It is necessary to compute the content length
printf("POST %s", _resource == NULL ? "/" : _resource); printf("POST %s", _resource == NULL ? "/" : _resource);
//Here we send the parameters //Here we send the parameters
sendUriWithGetParams(getData); sendUriWithGetParams(getData);
sendHeader(HttpMIMEType::APPLICATION_X_WWW_FORM_URLENCODED, computeBodyLength(postData)); sendHeader(HttpMIMEType::APPLICATION_X_WWW_FORM_URLENCODED, computeBodyLength(postData), headerData);
sendPostData(postData); sendPostData(postData);
break; break;
default: default:
@ -166,6 +170,8 @@ HttpClient::HTTP_CODE HttpClient::isReplyAvailable(uint16_t timeout)
{ {
uint32_t ts(millis()); uint32_t ts(millis());
char buffer[100]; char buffer[100];
char *bodyDelimiter(NULL);
char *newLineDelimiter(NULL);
if(!_httpCodeParsed)_httpCodeParsed = true; if(!_httpCodeParsed)_httpCodeParsed = true;
else else
@ -177,14 +183,16 @@ HttpClient::HTTP_CODE HttpClient::isReplyAvailable(uint16_t timeout)
#endif #endif
//This is the loop where we parse the data //This is the loop where we parse the data
while(available() || millis() - ts < timeout) while((available() || !bodyDelimiter) && millis() - ts < timeout)
{ {
if(available()) int bytesCount(available());
if(bytesCount)
{ {
//If we do not have the HTTP response code yet, we parse it //If we do not have the HTTP response code yet, we parse it
if(_httpCode == HTTP_CODE::UNDEFINED_CODE) if(_httpCode == HTTP_CODE::UNDEFINED_CODE)
{ {
uint8_t bytesRed = buffer[peekBytes((uint8_t*)buffer,99)] = '\0'; uint16_t safeSize = bytesCount > 99 ? 99 : bytesCount;
uint8_t bytesRed = buffer[peekBytes((uint8_t*)buffer,safeSize)] = '\0';
//We look for the end of the first line ie : HTTP/1.1 200 OK\r\n //We look for the end of the first line ie : HTTP/1.1 200 OK\r\n
char *pNewLine = strstr(buffer, "\r\n"); char *pNewLine = strstr(buffer, "\r\n");
//If we found the new line, we can retrieve the code //If we found the new line, we can retrieve the code
@ -207,15 +215,17 @@ HttpClient::HTTP_CODE HttpClient::isReplyAvailable(uint16_t timeout)
#endif #endif
_httpCode = numberToHTTP_CODE(strtoul(code+1, NULL, 10)); _httpCode = (HTTP_CODE)strtoul(code+1, NULL, 10);
//We can now discard the first line //We can now discard the first line
#ifdef DEBUG_HTTP_CLIENT #ifdef DEBUG_HTTP_CLIENT
Serial.printf("First line length : %u\n",(pNewLine - buffer) + 2); Serial.printf("First line length : %u\n",(pNewLine - buffer) + 2);
#endif #endif
read((uint8_t *)buffer, (pNewLine - buffer) + 2); read((uint8_t *)buffer, (pNewLine - buffer) + 2);
#ifdef DEBUG_HTTP_CLIENT #ifdef DEBUG_HTTP_CLIENT
buffer[peekBytes((uint8_t*)buffer,99)] = '\0'; bytesCount = available();
Serial.printf("Next chunk is : %s\n",buffer); safeSize = bytesCount > 99 ? 99 : bytesCount;
buffer[peekBytes((uint8_t*)buffer,safeSize)] = '\0';
Serial.printf("Next chunk is (size %u) : %s\n",safeSize,buffer);
#endif #endif
} }
} }
@ -238,39 +248,42 @@ HttpClient::HTTP_CODE HttpClient::isReplyAvailable(uint16_t timeout)
} }
else//We found the HTTP code, now we discard all the header data else//We found the HTTP code, now we discard all the header data
{ {
char *bodyDelimiter(NULL); int safeSize = bytesCount > 99 ? 99 : bytesCount;
char *newLineDelimiter; buffer[peekBytes((uint8_t*)buffer,safeSize)] = '\0';
do #ifdef DEBUG_HTTP_CLIENT
Serial.printf("Peeked for (size %u) : %s\n",safeSize,buffer);
#endif
bodyDelimiter = strstr(buffer, "\r\n\r\n");
newLineDelimiter = strstr(buffer, "\r\n");
if(!bodyDelimiter)
{ {
buffer[peekBytes((uint8_t*)buffer,99)] = '\0'; if(newLineDelimiter)
bodyDelimiter = strstr(buffer, "\r\n\r\n");
newLineDelimiter = strstr(buffer, "\r\n");
if(!bodyDelimiter)
{ {
if(newLineDelimiter) read((uint8_t *)buffer, (newLineDelimiter - buffer) + 2);
{
read((uint8_t *)buffer, (newLineDelimiter - buffer) + 2);
}
else
{
read((uint8_t *)buffer, strlen(buffer));
}
} }
else else
{ {
#ifdef DEBUG_HTTP_CLIENT read((uint8_t *)buffer, strlen(buffer));
Serial.println("We found the body delimiter");
#endif
read((uint8_t *)buffer, (bodyDelimiter - buffer) + 4);
#ifdef DEBUG_HTTP_CLIENT
buffer[peekBytes((uint8_t*)buffer,99)] = '\0';
Serial.printf("Body chunk is : %s\n",buffer);
#endif
} }
}while(!bodyDelimiter); }
break; else
{
#ifdef DEBUG_HTTP_CLIENT
Serial.println("We found the body delimiter");
#endif
read((uint8_t *)buffer, (bodyDelimiter - buffer) + 4);
#ifdef DEBUG_HTTP_CLIENT
safeSize = available() > 99 ? 99 : available();
buffer[peekBytes((uint8_t*)buffer,safeSize)] = '\0';
Serial.printf("Body chunk is : %s\n",buffer);
#endif
break;
}
} }
ts = millis();
} }
yield();
} }
#ifdef DEBUG_HTTP_CLIENT #ifdef DEBUG_HTTP_CLIENT
@ -343,7 +356,7 @@ void HttpClient::keepAlive(boolean enabled)
_keepAlive = enabled; _keepAlive = enabled;
} }
void HttpClient::sendHeader(HttpMIMEType contentType, uint64_t contentLength, HttpVersion httpVersion) void HttpClient::sendHeader(HttpMIMEType contentType, uint64_t contentLength, Dictionary<DictionaryHelper::StringEntity> *headerData, HttpVersion httpVersion)
{ {
char mime[255] = "", httpVer[15] = ""; char mime[255] = "", httpVer[15] = "";
//Host could be an IP address or a host name //Host could be an IP address or a host name
@ -357,6 +370,19 @@ void HttpClient::sendHeader(HttpMIMEType contentType, uint64_t contentLength, Ht
printf("Content-Length: %u\r\n", contentLength); printf("Content-Length: %u\r\n", contentLength);
printf("Content-Type: %s\r\n", httpMIMETypeToString(contentType, mime)); printf("Content-Type: %s\r\n", httpMIMETypeToString(contentType, mime));
} }
//We send the headerData if not NULL
if(headerData != NULL)
{
uint8_t count(headerData->count());
if(count > 0)
{
for(uint8_t i(0); i < count; i++)
{
printf("%s: %s\r\n", headerData->getParameter(i), headerData->getAt(i) != NULL ? headerData->getAt(i)->getString() : "");
}
}
}
printf("\r\n"); printf("\r\n");
} }

View File

@ -19,18 +19,26 @@ class HttpClient : public WiFiClient, public HttpConstants
public: public:
enum ConnectionStatus { NO_ATTEMPT = 0, SUCCESSFUL, FAILED }; enum ConnectionStatus { NO_ATTEMPT = 0, SUCCESSFUL, FAILED };
HttpClient(const char *address, uint16_t port = 80); HttpClient(const char *address, uint16_t port = 80, uint32_t timeout = 5000);
HttpClient(const char *address, const char *resource, uint16_t port = 80); HttpClient(const char *address, const char *resource, uint16_t port = 80, uint32_t timeout = 5000);
HttpClient(const HttpClient &object); HttpClient(const HttpClient &object);
virtual ~HttpClient(); virtual ~HttpClient();
boolean sendHttpQuery(const char *ressource, HttpRequestMethod method = HttpRequestMethod::GET, Dictionary<DictionaryHelper::StringEntity> *getData = NULL, Dictionary<DictionaryHelper::StringEntity> *postData = NULL); boolean sendHttpQuery(const char *ressource,
boolean sendHttpQuery(HttpRequestMethod method = HttpRequestMethod::GET, Dictionary<DictionaryHelper::StringEntity> *getData = NULL, Dictionary<DictionaryHelper::StringEntity> *postData = NULL); HttpRequestMethod method = HttpRequestMethod::GET,
Dictionary<DictionaryHelper::StringEntity> *getData = NULL,
Dictionary<DictionaryHelper::StringEntity> *postData = NULL,
Dictionary<DictionaryHelper::StringEntity> *headerData = NULL);
boolean sendHttpQuery(HttpRequestMethod method = HttpRequestMethod::GET,
Dictionary<DictionaryHelper::StringEntity> *getData = NULL,
Dictionary<DictionaryHelper::StringEntity> *postData = NULL,
Dictionary<DictionaryHelper::StringEntity> *headerData = NULL);
void keepAlive(boolean enabled); void keepAlive(boolean enabled);
void setMaxRetries(int16_t retries); void setMaxRetries(int16_t retries);
//100 ms is the default timeout //100 ms is the default timeout
HTTP_CODE isReplyAvailable(uint16_t timeout = 100); HTTP_CODE isReplyAvailable(uint16_t timeout = 10000);
uint16_t readHttpBody(uint8_t *buffer, uint32_t size); uint16_t readHttpBody(uint8_t *buffer, uint32_t size);
protected: protected:
@ -38,7 +46,7 @@ class HttpClient : public WiFiClient, public HttpConstants
boolean connectByHostOrIp(); boolean connectByHostOrIp();
void sendUriWithGetParams(Dictionary<DictionaryHelper::StringEntity> *data); void sendUriWithGetParams(Dictionary<DictionaryHelper::StringEntity> *data);
void sendPostData(Dictionary<DictionaryHelper::StringEntity> *data); void sendPostData(Dictionary<DictionaryHelper::StringEntity> *data);
void sendHeader(HttpMIMEType contentType = HttpMIMEType::UNKNOWN_MIME, uint64_t contentLength = 0, HttpVersion httpVersion = HttpVersion::HTTP_1_1); void sendHeader(HttpMIMEType contentType = HttpMIMEType::UNKNOWN_MIME, uint64_t contentLength = 0, Dictionary<DictionaryHelper::StringEntity> *headerData = NULL, HttpVersion httpVersion = HttpVersion::HTTP_1_1);
uint64_t computeBodyLength(Dictionary<DictionaryHelper::StringEntity> *data); uint64_t computeBodyLength(Dictionary<DictionaryHelper::StringEntity> *data);
ConnectionStatus _connectionStatus; ConnectionStatus _connectionStatus;

View File

@ -7,7 +7,67 @@ class HttpConstants
enum HttpRequestMethod {UNDEFINED, GET, HEAD, POST, PUT, DELETE, CONNECT, OPTIONS, TRACE, PATCH}; enum HttpRequestMethod {UNDEFINED, GET, HEAD, POST, PUT, DELETE, CONNECT, OPTIONS, TRACE, PATCH};
enum HttpVersion {UNKNOWN, HTTP_0_9, HTTP_1_1, HTTP_1_0, HTTP_2_0}; enum HttpVersion {UNKNOWN, HTTP_0_9, HTTP_1_1, HTTP_1_0, HTTP_2_0};
enum HttpMIMEType {UNKNOWN_MIME, TEXT_PLAIN, TEXT_CSS, TEXT_HTML, TEXT_JAVASCRIPT, APPLICATION_JSON, APPLICATION_X_WWW_FORM_URLENCODED, IMAGE_PNG, IMAGE_JPEG, AUDIO_MPEG, APPLICATION_OCTET_STREAM}; enum HttpMIMEType {UNKNOWN_MIME, TEXT_PLAIN, TEXT_CSS, TEXT_HTML, TEXT_JAVASCRIPT, APPLICATION_JSON, APPLICATION_X_WWW_FORM_URLENCODED, IMAGE_PNG, IMAGE_JPEG, AUDIO_MPEG, APPLICATION_OCTET_STREAM};
enum HTTP_CODE {UNDEFINED_CODE, _100, _101, _200, _400, _401, _403, _404, _405, _500, _501}; enum HTTP_CODE {
UNDEFINED_CODE = 0,
HTTP_CODE_CONTINUE = 100,
HTTP_CODE_SWITCHING_PROTOCOLS = 101,
HTTP_CODE_PROCESSING = 102,
HTTP_CODE_OK = 200,
HTTP_CODE_CREATED = 201,
HTTP_CODE_ACCEPTED = 202,
HTTP_CODE_NON_AUTHORITATIVE_INFORMATION = 203,
HTTP_CODE_NO_CONTENT = 204,
HTTP_CODE_RESET_CONTENT = 205,
HTTP_CODE_PARTIAL_CONTENT = 206,
HTTP_CODE_MULTI_STATUS = 207,
HTTP_CODE_ALREADY_REPORTED = 208,
HTTP_CODE_IM_USED = 226,
HTTP_CODE_MULTIPLE_CHOICES = 300,
HTTP_CODE_MOVED_PERMANENTLY = 301,
HTTP_CODE_FOUND = 302,
HTTP_CODE_SEE_OTHER = 303,
HTTP_CODE_NOT_MODIFIED = 304,
HTTP_CODE_USE_PROXY = 305,
HTTP_CODE_TEMPORARY_REDIRECT = 307,
HTTP_CODE_PERMANENT_REDIRECT = 308,
HTTP_CODE_BAD_REQUEST = 400,
HTTP_CODE_UNAUTHORIZED = 401,
HTTP_CODE_PAYMENT_REQUIRED = 402,
HTTP_CODE_FORBIDDEN = 403,
HTTP_CODE_NOT_FOUND = 404,
HTTP_CODE_METHOD_NOT_ALLOWED = 405,
HTTP_CODE_NOT_ACCEPTABLE = 406,
HTTP_CODE_PROXY_AUTHENTICATION_REQUIRED = 407,
HTTP_CODE_REQUEST_TIMEOUT = 408,
HTTP_CODE_CONFLICT = 409,
HTTP_CODE_GONE = 410,
HTTP_CODE_LENGTH_REQUIRED = 411,
HTTP_CODE_PRECONDITION_FAILED = 412,
HTTP_CODE_PAYLOAD_TOO_LARGE = 413,
HTTP_CODE_URI_TOO_LONG = 414,
HTTP_CODE_UNSUPPORTED_MEDIA_TYPE = 415,
HTTP_CODE_RANGE_NOT_SATISFIABLE = 416,
HTTP_CODE_EXPECTATION_FAILED = 417,
HTTP_CODE_MISDIRECTED_REQUEST = 421,
HTTP_CODE_UNPROCESSABLE_ENTITY = 422,
HTTP_CODE_LOCKED = 423,
HTTP_CODE_FAILED_DEPENDENCY = 424,
HTTP_CODE_UPGRADE_REQUIRED = 426,
HTTP_CODE_PRECONDITION_REQUIRED = 428,
HTTP_CODE_TOO_MANY_REQUESTS = 429,
HTTP_CODE_REQUEST_HEADER_FIELDS_TOO_LARGE = 431,
HTTP_CODE_INTERNAL_SERVER_ERROR = 500,
HTTP_CODE_NOT_IMPLEMENTED = 501,
HTTP_CODE_BAD_GATEWAY = 502,
HTTP_CODE_SERVICE_UNAVAILABLE = 503,
HTTP_CODE_GATEWAY_TIMEOUT = 504,
HTTP_CODE_HTTP_VERSION_NOT_SUPPORTED = 505,
HTTP_CODE_VARIANT_ALSO_NEGOTIATES = 506,
HTTP_CODE_INSUFFICIENT_STORAGE = 507,
HTTP_CODE_LOOP_DETECTED = 508,
HTTP_CODE_NOT_EXTENDED = 510,
HTTP_CODE_NETWORK_AUTHENTICATION_REQUIRED = 511
};
static char *httpVersionToString(HttpVersion v, char *buffer) static char *httpVersionToString(HttpVersion v, char *buffer)
{ {
@ -57,36 +117,7 @@ class HttpConstants
return buffer; return buffer;
} }
static HTTP_CODE numberToHTTP_CODE(uint32_t code)
{
switch(code)
{
case 100:
return HTTP_CODE::_100;
case 101:
return HTTP_CODE::_101;
case 200:
return HTTP_CODE::_200;
case 400:
return HTTP_CODE::_400;
case 401:
return HTTP_CODE::_401;
case 403:
return HTTP_CODE::_403;
case 404:
return HTTP_CODE::_404;
case 405:
return HTTP_CODE::_405;
case 500:
return HTTP_CODE::_500;
case 501:
return HTTP_CODE::_501;
default :
return HTTP_CODE::UNDEFINED_CODE;
}
}
protected: protected:
private: private:
}; };

View File

@ -126,7 +126,7 @@ class WEBServer : public TCPServer<T>, public HttpConstants
if(client->_httpRequestData.HRM == HttpRequestMethod::UNDEFINED) //Error 400 if(client->_httpRequestData.HRM == HttpRequestMethod::UNDEFINED) //Error 400
{ {
sendInfoResponse(HTTP_CODE::_400, client, "The server could not understand the request due to invalid syntax"); sendInfoResponse(HTTP_CODE::HTTP_CODE_BAD_REQUEST, client, "The server could not understand the request due to invalid syntax");
client->_clientState = TCPClient::ClientState::DISCARDED; client->_clientState = TCPClient::ClientState::DISCARDED;
break; break;
} }
@ -140,7 +140,7 @@ class WEBServer : public TCPServer<T>, public HttpConstants
} }
else else
{ {
sendInfoResponse(HTTP_CODE::_400, client, "The server could not understand the request due to invalid syntax"); sendInfoResponse(HTTP_CODE::HTTP_CODE_BAD_REQUEST, client, "The server could not understand the request due to invalid syntax");
client->_clientState = TCPClient::ClientState::DISCARDED; client->_clientState = TCPClient::ClientState::DISCARDED;
} }
} }
@ -166,7 +166,7 @@ class WEBServer : public TCPServer<T>, public HttpConstants
} }
else //Error 500 else //Error 500
{ {
sendInfoResponse(HTTP_CODE::_500, client, "Failed to allocate memory for resources"); sendInfoResponse(HTTP_CODE::HTTP_CODE_INTERNAL_SERVER_ERROR, client, "Failed to allocate memory for resources");
client->_clientState = TCPClient::ClientState::DISCARDED; client->_clientState = TCPClient::ClientState::DISCARDED;
break; break;
} }
@ -188,7 +188,7 @@ class WEBServer : public TCPServer<T>, public HttpConstants
} }
else //Error 500 else //Error 500
{ {
sendInfoResponse(HTTP_CODE::_500, client, "Failed to allocate memory for resources"); sendInfoResponse(HTTP_CODE::HTTP_CODE_INTERNAL_SERVER_ERROR, client, "Failed to allocate memory for resources");
client->_clientState = TCPClient::ClientState::DISCARDED; client->_clientState = TCPClient::ClientState::DISCARDED;
break; break;
} }
@ -298,7 +298,7 @@ class WEBServer : public TCPServer<T>, public HttpConstants
break; break;
default : default :
sendInfoResponse(HTTP_CODE::_500, client, "WEB server error"); sendInfoResponse(HTTP_CODE::HTTP_CODE_INTERNAL_SERVER_ERROR, client, "WEB server error");
client->_clientState = TCPClient::ClientState::DISCARDED; client->_clientState = TCPClient::ClientState::DISCARDED;
break; break;
} }
@ -483,7 +483,7 @@ class WEBServer : public TCPServer<T>, public HttpConstants
filePath = getFilePathByHttpResource(client->_httpRequestData.httpResource); filePath = getFilePathByHttpResource(client->_httpRequestData.httpResource);
if(filePath == NULL) if(filePath == NULL)
{ {
sendInfoResponse(HTTP_CODE::_500, client, "Failed to allocate memory for the filePath"); sendInfoResponse(HTTP_CODE::HTTP_CODE_INTERNAL_SERVER_ERROR, client, "Failed to allocate memory for the filePath");
return false; return false;
} }
@ -503,12 +503,12 @@ class WEBServer : public TCPServer<T>, public HttpConstants
response = (char *) malloc(sizeof(char) * (strlen_P((PGM_P)F("Resource : not found on this server")) + strlen(client->_httpRequestData.httpResource) + 1)); response = (char *) malloc(sizeof(char) * (strlen_P((PGM_P)F("Resource : not found on this server")) + strlen(client->_httpRequestData.httpResource) + 1));
if(response == NULL) if(response == NULL)
{ {
sendInfoResponse(HTTP_CODE::_500, client, "Failed to allocate memory for the response"); sendInfoResponse(HTTP_CODE::HTTP_CODE_INTERNAL_SERVER_ERROR, client, "Failed to allocate memory for the response");
return false; return false;
} }
sprintf(response, "Resource : %s not found on this server", client->_httpRequestData.httpResource); sprintf(response, "Resource : %s not found on this server", client->_httpRequestData.httpResource);
sendInfoResponse(HTTP_CODE::_404, client, response); sendInfoResponse(HTTP_CODE::HTTP_CODE_NOT_FOUND, client, response);
free(response);response = NULL; free(response);response = NULL;
return false; return false;
@ -525,7 +525,7 @@ class WEBServer : public TCPServer<T>, public HttpConstants
{ {
pageToSend.close(); pageToSend.close();
sendInfoResponse(HTTP_CODE::_403, client, "The file you want to access is a folder"); sendInfoResponse(HTTP_CODE::HTTP_CODE_FORBIDDEN, client, "The file you want to access is a folder");
return false; return false;
} }
@ -545,7 +545,7 @@ class WEBServer : public TCPServer<T>, public HttpConstants
if(header == NULL) if(header == NULL)
{ {
sendInfoResponse(HTTP_CODE::_500, client, "Failed to allocate memory for the header"); sendInfoResponse(HTTP_CODE::HTTP_CODE_INTERNAL_SERVER_ERROR, client, "Failed to allocate memory for the header");
pageToSend.close(); pageToSend.close();
return false; return false;
} }
@ -584,13 +584,13 @@ class WEBServer : public TCPServer<T>, public HttpConstants
pageToSend.close(); pageToSend.close();
break; break;
default: //If not supported default: //If not supported
sendInfoResponse(HTTP_CODE::_500, client, "The method used is not allowed"); sendInfoResponse(HTTP_CODE::HTTP_CODE_METHOD_NOT_ALLOWED, client, "The method used is not allowed");
return false; return false;
break; break;
} }
}else }else
{ {
sendInfoResponse(HTTP_CODE::_500, client, "Unable to access the SDCard"); sendInfoResponse(HTTP_CODE::HTTP_CODE_INTERNAL_SERVER_ERROR, client, "Unable to access the SDCard");
return false; return false;
} }
@ -601,36 +601,29 @@ class WEBServer : public TCPServer<T>, public HttpConstants
static void sendInfoResponse(HTTP_CODE http_code, T *client, const char *message) static void sendInfoResponse(HTTP_CODE http_code, T *client, const char *message)
{ {
uint16_t code(0);
char codeLiteral[100]; char codeLiteral[100];
switch(http_code) switch(http_code)
{ {
case _400: case HTTP_CODE_BAD_REQUEST:
code = 400;
strcpy_P(codeLiteral,PSTR("Bad Request")); strcpy_P(codeLiteral,PSTR("Bad Request"));
break; break;
case _404: case HTTP_CODE_NOT_FOUND:
code = 404;
strcpy_P(codeLiteral,PSTR("Not Found")); strcpy_P(codeLiteral,PSTR("Not Found"));
break; break;
case _403: case HTTP_CODE_FORBIDDEN:
code = 403;
strcpy_P(codeLiteral,PSTR("Forbidden")); strcpy_P(codeLiteral,PSTR("Forbidden"));
break; break;
case _405: case HTTP_CODE_METHOD_NOT_ALLOWED:
code = 405;
strcpy_P(codeLiteral,PSTR("Method Not Allowed")); strcpy_P(codeLiteral,PSTR("Method Not Allowed"));
break; break;
case _500: case HTTP_CODE_INTERNAL_SERVER_ERROR:
code = 500;
strcpy_P(codeLiteral,PSTR("Internal Server Error")); strcpy_P(codeLiteral,PSTR("Internal Server Error"));
break; break;
default: default:
code = 000;
strcpy_P(codeLiteral,PSTR("Error Not Defined")); strcpy_P(codeLiteral,PSTR("Error Not Defined"));
break; break;
} }
client->_client.printf_P(PSTR("HTTP/1.1 %d %s\r\nContent-Type: text/html\r\nContent-Length: %d\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\n<h1>Error %d</h1><p>%s</p>\r\n</html>"), code, codeLiteral, strlen(message) + 56 + (code != 0 ? 3:1), code , message); client->_client.printf_P(PSTR("HTTP/1.1 %d %s\r\nContent-Type: text/html\r\nContent-Length: %d\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\n<h1>Error %d</h1><p>%s</p>\r\n</html>"), http_code, codeLiteral, strlen(message) + 56 + (http_code != 0 ? 3:1), http_code , message);
} }
static HttpRequestMethod getHttpVerbEnumValue(const char *parseBuffer) static HttpRequestMethod getHttpVerbEnumValue(const char *parseBuffer)