Compare commits
3 Commits
6ffe25ec3b
...
63ead30ad0
Author | SHA1 | Date | |
---|---|---|---|
|
63ead30ad0 | ||
|
3bad569f21 | ||
|
a95a387d30 |
@ -27,6 +27,11 @@ class TCPServer
|
|||||||
{
|
{
|
||||||
return _maxClient;
|
return _maxClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsigned int getPort() const
|
||||||
|
{
|
||||||
|
return _port;
|
||||||
|
}
|
||||||
|
|
||||||
uint8_t getConnectedClientsCount()
|
uint8_t getConnectedClientsCount()
|
||||||
{
|
{
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
#define DEBUG_WEBCL
|
#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
|
#ifdef DEBUG_WEBCL
|
||||||
Serial.println("WEBClient : Standard constructor called");
|
Serial.println("WEBClient : Standard constructor called");
|
||||||
|
@ -15,25 +15,9 @@ class WEBClient : public TCPClient
|
|||||||
protected:
|
protected:
|
||||||
WEBServer<WEBClient>::WEBClientState _WEBClientState;
|
WEBServer<WEBClient>::WEBClientState _WEBClientState;
|
||||||
private:
|
private:
|
||||||
struct HttpRequestData
|
WEBServer<WEBClient>::HttpRequestData _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>::HttpParserStatus _httpParserState;
|
WEBServer<WEBClient>::HttpParserStatus _httpParserState;
|
||||||
|
uint64_t _fileSentBytes;
|
||||||
void clearHttpRequestData();
|
void clearHttpRequestData();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -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)
|
||||||
@ -43,9 +75,7 @@ class WEBServer : public TCPServer<T>
|
|||||||
break;
|
break;
|
||||||
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