Moved the HttpRequestData Structure from the WEBClient class to the WEBServer class
This commit is contained in:
parent
3bad569f21
commit
63ead30ad0
@ -5,6 +5,9 @@
|
|||||||
#include "Dictionary.h"
|
#include "Dictionary.h"
|
||||||
#define DEBUG_WEBS
|
#define DEBUG_WEBS
|
||||||
|
|
||||||
|
//forward declaration of HttpRequestData implemented in WEBClient.h
|
||||||
|
struct HttpRequestData;
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class WEBServer : public TCPServer<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 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 HttpParserStatus {HTTP_VERB, HTTP_RESSOURCE, HTTP_VERSION, HTTP_PARAMS, POST_DATA};
|
||||||
enum WEBClientState {ACCEPTED, QUERY_PARSED, RESPONSE_SENT, DONE};
|
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) {}
|
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:
|
protected:
|
||||||
private:
|
private:
|
||||||
virtual T* createNewClient(WiFiClient wc)
|
virtual T* createNewClient(WiFiClient wc)
|
||||||
@ -44,8 +76,6 @@ class WEBServer : public TCPServer<T>
|
|||||||
case DONE:
|
case DONE:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void queryParser(T *client)
|
void queryParser(T *client)
|
||||||
@ -253,23 +283,25 @@ class WEBServer : public TCPServer<T>
|
|||||||
|
|
||||||
void sendInfoResponse(HTTP_CODE http_code, T *client, const char *message)
|
void sendInfoResponse(HTTP_CODE http_code, T *client, const char *message)
|
||||||
{
|
{
|
||||||
|
uint16_t code(0);
|
||||||
|
char codeLiteral[100];
|
||||||
switch(http_code)
|
switch(http_code)
|
||||||
{
|
{
|
||||||
case _500:
|
case _500:
|
||||||
client->_client.print(F("HTTP/1.1 500 Internal Server Error\r\nContent-Type: text/html\r\nContent-Length: "));
|
code = 500;
|
||||||
client->_client.print(strlen(message) + 59);
|
strcpy_P(codeLiteral,(PGM_P)F("Internal Server Error"));
|
||||||
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>"));
|
|
||||||
break;
|
break;
|
||||||
case _400:
|
case _400:
|
||||||
client->_client.print(F("HTTP/1.1 400 Bad Request\r\nContent-Type: text/html\r\nContent-Length: "));
|
code = 400;
|
||||||
client->_client.print(strlen(message) + 59);
|
strcpy_P(codeLiteral,(PGM_P)F("Bad Request"));
|
||||||
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>"));
|
|
||||||
break;
|
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*/
|
/*Static helper methods*/
|
||||||
@ -300,6 +332,15 @@ class WEBServer : public TCPServer<T>
|
|||||||
else
|
else
|
||||||
return HttpVersion::UNKNOWN;
|
return HttpVersion::UNKNOWN;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct ApiRoutine
|
||||||
|
{
|
||||||
|
boolean (*apiRoutine)(HttpRequestData&, WiFiClient*, void*);
|
||||||
|
void *pData;
|
||||||
|
HttpRequestMethod HRM;
|
||||||
|
};
|
||||||
|
|
||||||
|
Dictionary<ApiRoutine> _apiDictionary;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //WEBSERVER_H
|
#endif //WEBSERVER_H
|
||||||
|
Loading…
Reference in New Issue
Block a user