From e52a1887942c1dc57dce0a39b47874ca0819697b Mon Sep 17 00:00:00 2001 From: Relintai Date: Thu, 21 Jul 2022 15:51:44 +0200 Subject: [PATCH] Implement parsing cookies in the requests for the SimpleWebServer. --- modules/web/http_server_simple/http_parser.cpp | 15 +++++++++++++++ .../simple_web_server_request.cpp | 15 +++++++++++++++ .../simple_web_server_request.h | 9 +++++++++ 3 files changed, 39 insertions(+) diff --git a/modules/web/http_server_simple/http_parser.cpp b/modules/web/http_server_simple/http_parser.cpp index 355813b2a..e3a476a8b 100644 --- a/modules/web/http_server_simple/http_parser.cpp +++ b/modules/web/http_server_simple/http_parser.cpp @@ -389,6 +389,21 @@ int HTTPParser::on_header_value(const char *at, size_t length) { _content_type = REQUEST_CONTENT_TEXT_PLAIN; //maybe just close the connection? } + } else if (_queued_header_field == "Cookie") { + Vector cookies = s.split(";"); + + for (int i = 0; i < cookies.size(); ++i) { + String c = cookies[i].strip_edges(); + + if (c.get_slice_count("=") != 2) { + continue; + } + + String key = c.get_slice("=", 0); + String val = c.get_slice("=", 1); + + _request->add_cookie_data(key, val); + } } //TODO close connection on chunked connection (for now) diff --git a/modules/web/http_server_simple/simple_web_server_request.cpp b/modules/web/http_server_simple/simple_web_server_request.cpp index 5a72f9bf3..26d154792 100644 --- a/modules/web/http_server_simple/simple_web_server_request.cpp +++ b/modules/web/http_server_simple/simple_web_server_request.cpp @@ -13,6 +13,13 @@ #include "http_server_simple.h" String SimpleWebServerRequest::get_cookie(const String &key) { + for (int i = 0; i < _cookies.size(); ++i) { + const CookieData &d = _cookies[i]; + if (d.key == key) { + return d.value; + } + } + return ""; } @@ -121,6 +128,14 @@ void SimpleWebServerRequest::add_file(const String &key, const String &file_name _files.push_back(e); } +void SimpleWebServerRequest::add_cookie_data(const String &key, const String &value) { + CookieData d; + d.key = key; + d.value = value; + + _cookies.push_back(d); +} + SimpleWebServerRequest::SimpleWebServerRequest() { _server = nullptr; } diff --git a/modules/web/http_server_simple/simple_web_server_request.h b/modules/web/http_server_simple/simple_web_server_request.h index 174e33a38..cf6ef5686 100644 --- a/modules/web/http_server_simple/simple_web_server_request.h +++ b/modules/web/http_server_simple/simple_web_server_request.h @@ -47,6 +47,8 @@ public: void add_file(const String &key, const String &file_name, const PoolByteArray &data); + void add_cookie_data(const String &key, const String &value); + //virtual String get_path_full() const; SimpleWebServerRequest(); @@ -68,6 +70,13 @@ protected: }; Vector _files; + + struct CookieData { + String key; + String value; + }; + + Vector _cookies; }; #endif