From d5a09d2ee92a83cfa426bd7ab808dea1645f8ae2 Mon Sep 17 00:00:00 2001 From: Mathieu Carbou Date: Mon, 9 Sep 2024 00:31:51 +0200 Subject: [PATCH] (feat) Add support for request attributes - request->setAttribute(name, val) - request->hasAttribute(name) - request->getAttribute(name, defaultValue) --- src/ESPAsyncWebServer.h | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/ESPAsyncWebServer.h b/src/ESPAsyncWebServer.h index 39bf285..fe8e4c9 100644 --- a/src/ESPAsyncWebServer.h +++ b/src/ESPAsyncWebServer.h @@ -26,6 +26,7 @@ #include "FS.h" #include #include +#include #include #ifdef ESP32 @@ -217,6 +218,8 @@ class AsyncWebServerRequest { std::list _params; std::vector _pathParams; + std::unordered_map, std::equal_to> _attributes; + uint8_t _multiParseState; uint8_t _boundaryPosition; size_t _itemStartIndex; @@ -469,6 +472,37 @@ class AsyncWebServerRequest { const String& header(size_t i) const; // get request header value by number const String& headerName(size_t i) const; // get request header name by number + // REQUEST ATTRIBUTES + + void setAttribute(const char* name, const char* value) { _attributes[name] = value; } + void setAttribute(const char* name, bool value) { _attributes[name] = value ? "1" : emptyString; } + void setAttribute(const char* name, long value) { _attributes[name] = String(value); } + void setAttribute(const char* name, float value, unsigned int decimalPlaces = 2) { _attributes[name] = String(value, decimalPlaces); } + void setAttribute(const char* name, double value, unsigned int decimalPlaces = 2) { _attributes[name] = String(value, decimalPlaces); } + + bool hasAttribute(const char* name) const { return _attributes.find(name) != _attributes.end(); } + + const String& getAttribute(const char* name, const String& defaultValue = emptyString) const { + auto it = _attributes.find(name); + return it != _attributes.end() ? it->second : defaultValue; + } + bool getAttribute(const char* name, bool defaultValue) const { + auto it = _attributes.find(name); + return it != _attributes.end() ? it->second == "1" : defaultValue; + } + long getAttribute(const char* name, long defaultValue) const { + auto it = _attributes.find(name); + return it != _attributes.end() ? it->second.toInt() : defaultValue; + } + float getAttribute(const char* name, float defaultValue) const { + auto it = _attributes.find(name); + return it != _attributes.end() ? it->second.toFloat() : defaultValue; + } + double getAttribute(const char* name, double defaultValue) const { + auto it = _attributes.find(name); + return it != _attributes.end() ? it->second.toDouble() : defaultValue; + } + String urlDecode(const String& text) const; };