Implement parsing cookies in the requests for the SimpleWebServer.

This commit is contained in:
Relintai 2022-07-21 15:51:44 +02:00
parent e7e95a677a
commit e52a188794
3 changed files with 39 additions and 0 deletions

View File

@ -389,6 +389,21 @@ int HTTPParser::on_header_value(const char *at, size_t length) {
_content_type = REQUEST_CONTENT_TEXT_PLAIN; _content_type = REQUEST_CONTENT_TEXT_PLAIN;
//maybe just close the connection? //maybe just close the connection?
} }
} else if (_queued_header_field == "Cookie") {
Vector<String> 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) //TODO close connection on chunked connection (for now)

View File

@ -13,6 +13,13 @@
#include "http_server_simple.h" #include "http_server_simple.h"
String SimpleWebServerRequest::get_cookie(const String &key) { 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 ""; return "";
} }
@ -121,6 +128,14 @@ void SimpleWebServerRequest::add_file(const String &key, const String &file_name
_files.push_back(e); _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() { SimpleWebServerRequest::SimpleWebServerRequest() {
_server = nullptr; _server = nullptr;
} }

View File

@ -47,6 +47,8 @@ public:
void add_file(const String &key, const String &file_name, const PoolByteArray &data); 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; //virtual String get_path_full() const;
SimpleWebServerRequest(); SimpleWebServerRequest();
@ -68,6 +70,13 @@ protected:
}; };
Vector<FileEntry> _files; Vector<FileEntry> _files;
struct CookieData {
String key;
String value;
};
Vector<CookieData> _cookies;
}; };
#endif #endif