Compare commits
3 Commits
6ffe25ec3b
...
63ead30ad0
Author | SHA1 | Date | |
---|---|---|---|
|
63ead30ad0 | ||
|
3bad569f21 | ||
|
a95a387d30 |
@ -28,6 +28,11 @@ class TCPServer
|
||||
return _maxClient;
|
||||
}
|
||||
|
||||
unsigned int getPort() const
|
||||
{
|
||||
return _port;
|
||||
}
|
||||
|
||||
uint8_t getConnectedClientsCount()
|
||||
{
|
||||
return _clientList.count();
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
#define DEBUG_WEBCL
|
||||
|
||||
WEBClient::WEBClient(WiFiClient client, uint8_t id, uint16_t maxResourceBuffer, uint16_t maxBodyBuffer, uint16_t dataBufferSize) : TCPClient(client, id, dataBufferSize), _WEBClientState(WEBServer<WEBClient>::WEBClientState::ACCEPTED), _httpParserState(WEBServer<WEBClient>::HttpParserStatus::HTTP_VERB)
|
||||
WEBClient::WEBClient(WiFiClient client, uint8_t id, uint16_t maxResourceBuffer, uint16_t maxBodyBuffer, uint16_t dataBufferSize) : TCPClient(client, id, dataBufferSize), _WEBClientState(WEBServer<WEBClient>::WEBClientState::ACCEPTED), _httpParserState(WEBServer<WEBClient>::HttpParserStatus::HTTP_VERB), _fileSentBytes(0)
|
||||
{
|
||||
#ifdef DEBUG_WEBCL
|
||||
Serial.println("WEBClient : Standard constructor called");
|
||||
|
@ -15,25 +15,9 @@ class WEBClient : public TCPClient
|
||||
protected:
|
||||
WEBServer<WEBClient>::WEBClientState _WEBClientState;
|
||||
private:
|
||||
struct HttpRequestData
|
||||
{
|
||||
WEBServer<WEBClient>::HttpRequestMethod HRM;
|
||||
WEBServer<WEBClient>::HttpVersion HV;
|
||||
WEBServer<WEBClient>::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;
|
||||
} _httpRequestData;
|
||||
|
||||
WEBServer<WEBClient>::HttpRequestData _httpRequestData;
|
||||
WEBServer<WEBClient>::HttpParserStatus _httpParserState;
|
||||
|
||||
uint64_t _fileSentBytes;
|
||||
void clearHttpRequestData();
|
||||
};
|
||||
|
||||
|
@ -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)
|
||||
@ -44,8 +76,6 @@ class WEBServer : public TCPServer<T>
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user