From bbebac621295c6cf16f9d95b59bab3114e57e191 Mon Sep 17 00:00:00 2001 From: anschrammh Date: Thu, 20 Oct 2022 08:22:11 +0200 Subject: [PATCH] Started to add support for Cookies, not fully implemented yet --- src/app/WEBServer.h | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/app/WEBServer.h b/src/app/WEBServer.h index 35cf555..fab9aa7 100644 --- a/src/app/WEBServer.h +++ b/src/app/WEBServer.h @@ -23,6 +23,15 @@ class WEBServer : public TCPServer, 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, 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, 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, public HttpConstants Dictionary _apiDictionary; Dictionary _httpHeadersDictionary; + Dictionary _setCookieDictionary; SDClass *_sdClass; char *_WWWDir = nullptr; //Website root folder };