Moved the HttpRequestData Structure from the WEBClient class to the WEBServer class

This commit is contained in:
anschrammh 2019-10-10 18:16:33 +02:00
parent 3bad569f21
commit 63ead30ad0

View File

@ -5,6 +5,9 @@
#include "Dictionary.h"
#define DEBUG_WEBS
//forward declaration of HttpRequestData implemented in WEBClient.h
struct HttpRequestData;
template <typename T>
class WEBServer : public TCPServer<T>
{
@ -14,9 +17,38 @@ class WEBServer : public TCPServer<T>
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 HttpParserStatus {HTTP_VERB, HTTP_RESSOURCE, HTTP_VERSION, HTTP_PARAMS, POST_DATA};
enum WEBClientState {ACCEPTED, QUERY_PARSED, RESPONSE_SENT, DONE};
enum HTTP_CODE {_400, _401, _403, _404, _500, _501};
enum HTTP_CODE {_100, _101, _200, _400, _401, _403, _404, _405, _500, _501};
struct HttpRequestData
{
HttpRequestMethod HRM;
HttpVersion HV;
HttpMIMEType HMT;
Dictionary<DictionaryHelper::StringEntity> getParams;
char *getParamsDataPointer; //Used in the getParams algorithm
Dictionary<DictionaryHelper::StringEntity> postParams;
char *postParamsDataPointer; //Used in the postParams algorithm
char *httpResource;
uint16_t maxResourceBuffer;
char *httpBody;
uint16_t maxBodyBuffer;
};
WEBServer(unsigned int port = 80, uint8_t maxClient = MAX_CLIENT, uint16_t clientDataBufferSize = 512) : TCPServer<T>(port, maxClient, clientDataBufferSize) {}
boolean addApiRoutine(const char *uri, boolean (*apiRoutine)(HttpRequestData&, WiFiClient*, void*), void *pData, HttpRequestMethod HRM = UNDEFINED)
{
return _apiDictionary.add(uri, new ApiRoutine({apiRoutine, pData, HRM}));
}
void clearApiRoutine() { _apiDictionary.clear(); };
boolean removeApiRoutine(const char* uri)
{
return _apiDictionary.remove(uri);
}
protected:
private:
virtual T* createNewClient(WiFiClient wc)
@ -43,9 +75,7 @@ class WEBServer : public TCPServer<T>
break;
case DONE:
break;
}
}
}
void queryParser(T *client)
@ -253,23 +283,25 @@ class WEBServer : public TCPServer<T>
void sendInfoResponse(HTTP_CODE http_code, T *client, const char *message)
{
uint16_t code(0);
char codeLiteral[100];
switch(http_code)
{
case _500:
client->_client.print(F("HTTP/1.1 500 Internal Server Error\r\nContent-Type: text/html\r\nContent-Length: "));
client->_client.print(strlen(message) + 59);
client->_client.print(F("\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\n<h1>Error 500</h1><p>"));
client->_client.print(message);
client->_client.print(F("</p>\r\n</html>"));
code = 500;
strcpy_P(codeLiteral,(PGM_P)F("Internal Server Error"));
break;
case _400:
client->_client.print(F("HTTP/1.1 400 Bad Request\r\nContent-Type: text/html\r\nContent-Length: "));
client->_client.print(strlen(message) + 59);
client->_client.print(F("\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\n<h1>Error 400</h1><p>"));
client->_client.print(message);
client->_client.print(F("</p>\r\n</html>"));
code = 400;
strcpy_P(codeLiteral,(PGM_P)F("Bad Request"));
break;
}
client->_client.print(F("HTTP/1.1 "));client->_client.print(code);client->_client.print(F(" "));client->_client.print(codeLiteral);client->_client.print(F("\r\nContent-Type: text/html\r\nContent-Length: "));
client->_client.print(strlen(message) + 59);
client->_client.print(F("\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\n<h1>Error "));client->_client.print(code);client->_client.print(F("</h1><p>"));
client->_client.print(message);
client->_client.print(F("</p>\r\n</html>"));
}
/*Static helper methods*/
@ -300,6 +332,15 @@ class WEBServer : public TCPServer<T>
else
return HttpVersion::UNKNOWN;
}
struct ApiRoutine
{
boolean (*apiRoutine)(HttpRequestData&, WiFiClient*, void*);
void *pData;
HttpRequestMethod HRM;
};
Dictionary<ApiRoutine> _apiDictionary;
};
#endif //WEBSERVER_H