Compare commits

..

No commits in common. "63ead30ad01614c9b747e1039dc4803ec044cb69" and "6ffe25ec3bb2d250e7afa5999d8c6df288dc4260" have entirely different histories.

4 changed files with 33 additions and 63 deletions

View File

@ -27,11 +27,6 @@ class TCPServer
{ {
return _maxClient; return _maxClient;
} }
unsigned int getPort() const
{
return _port;
}
uint8_t getConnectedClientsCount() uint8_t getConnectedClientsCount()
{ {

View File

@ -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), _fileSentBytes(0) 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)
{ {
#ifdef DEBUG_WEBCL #ifdef DEBUG_WEBCL
Serial.println("WEBClient : Standard constructor called"); Serial.println("WEBClient : Standard constructor called");

View File

@ -15,9 +15,25 @@ class WEBClient : public TCPClient
protected: protected:
WEBServer<WEBClient>::WEBClientState _WEBClientState; WEBServer<WEBClient>::WEBClientState _WEBClientState;
private: private:
WEBServer<WEBClient>::HttpRequestData _httpRequestData; 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>::HttpParserStatus _httpParserState; WEBServer<WEBClient>::HttpParserStatus _httpParserState;
uint64_t _fileSentBytes;
void clearHttpRequestData(); void clearHttpRequestData();
}; };

View File

@ -5,9 +5,6 @@
#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>
{ {
@ -17,38 +14,9 @@ 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 {_100, _101, _200, _400, _401, _403, _404, _405, _500, _501}; enum HTTP_CODE {_400, _401, _403, _404, _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)
@ -75,7 +43,9 @@ class WEBServer : public TCPServer<T>
break; break;
case DONE: case DONE:
break; break;
} }
} }
void queryParser(T *client) void queryParser(T *client)
@ -283,25 +253,23 @@ 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:
code = 500; client->_client.print(F("HTTP/1.1 500 Internal Server Error\r\nContent-Type: text/html\r\nContent-Length: "));
strcpy_P(codeLiteral,(PGM_P)F("Internal Server Error")); 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>"));
break; break;
case _400: case _400:
code = 400; client->_client.print(F("HTTP/1.1 400 Bad Request\r\nContent-Type: text/html\r\nContent-Length: "));
strcpy_P(codeLiteral,(PGM_P)F("Bad Request")); 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>"));
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*/
@ -332,15 +300,6 @@ 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