Started to add support for Cookies, not fully implemented yet

This commit is contained in:
anschrammh 2022-10-20 08:22:11 +02:00
parent 444d1ff60c
commit bbebac6212

View File

@ -23,6 +23,15 @@ class WEBServer : public TCPServer<T>, public HttpConstants
PARSE_HTTP_HEADER_PARAMS
};
enum WEBClientState {ACCEPTED, PARSING, QUERY_PARSED, RESPONSE_SENT, DONE};
struct HttpCookie
{
DictionaryHelper::StringEntity value;
DictionaryHelper::StringEntity domain;
DictionaryHelper::StringEntity path;
int32_t sameSite : 1, httpOnly : 1, maxAge : 30;
//Need to add the expires field as well. Thinking about the best way of doing it.
};
struct HttpRequestData
{
@ -87,6 +96,25 @@ class WEBServer : public TCPServer<T>, public HttpConstants
return _httpHeadersDictionary.count();
}
/**
* The addCookie method adds cookies to the cookie dictionary which will be used when calling the sendHTTPResponse method.
* Once the cookies are sent, the dictionary will be emptied.
**/
boolean addCookies(const char *cookieName, const char *cookieValue, int32_t maxAge = -1, const char *cookiePath = nullptr, const char *cookieDomain = nullptr, boolean httpOnly = false, boolean sameSite = false)
{
return _setCookieDictionary.add(cookieName, new HttpCookie({cookieValue, cookieDomain, cookiePath, sameSite, httpOnly, maxAge}));
}
void clearCookies(void)
{
_setCookieDictionary.clear();
}
boolean removeCookie(const char *cookieName)
{
return _setCookieDictionary.remove(cookieName);
}
void sendHTTPResponse(WiFiClient *client, const char *contentType, const size_t contentLength = 0, HttpVersion version = HttpVersion::HTTP_1_1, HTTP_CODE HTTPCode = HTTP_CODE::HTTP_CODE_OK)
{
if(!client) return;
@ -104,6 +132,15 @@ class WEBServer : public TCPServer<T>, public HttpConstants
{
client->printf("\r\n%s: %s", _httpHeadersDictionary.getParameter(i), _httpHeadersDictionary.getAt(i) ? _httpHeadersDictionary.getAt(i)->getString() : "");
}
//We here send the user defined cookies :)
for(unsigned int i(0); i < _setCookieDictionary.count(); i++)
{
//client
}
//We do not forget to clear them after
clearCookies();
client->print("\r\n\r\n");
}
@ -1075,6 +1112,7 @@ class WEBServer : public TCPServer<T>, public HttpConstants
Dictionary<ApiRoutine> _apiDictionary;
Dictionary<DictionaryHelper::StringEntity> _httpHeadersDictionary;
Dictionary<HttpCookie> _setCookieDictionary;
SDClass *_sdClass;
char *_WWWDir = nullptr; //Website root folder
};