From e75dd5b02d29eaaeaa2127d41c05726cde737484 Mon Sep 17 00:00:00 2001 From: Mathieu Carbou Date: Mon, 30 Sep 2024 00:51:36 +0200 Subject: [PATCH 1/3] Fix compilation issue with ArduinoJson 5 and 6 (closes #108) --- platformio.ini | 2 ++ src/AsyncJson.cpp | 8 +++--- src/AsyncJson.h | 23 ++++++++--------- src/AsyncMessagePack.cpp | 31 +++++++++++++++++++--- src/AsyncMessagePack.h | 55 ++++++++++++++++++++++++++++++---------- 5 files changed, 84 insertions(+), 35 deletions(-) diff --git a/platformio.ini b/platformio.ini index d242032..c650376 100644 --- a/platformio.ini +++ b/platformio.ini @@ -24,6 +24,8 @@ upload_protocol = esptool monitor_speed = 115200 monitor_filters = esp32_exception_decoder, log2file lib_deps = + ; bblanchon/ArduinoJson @ 5.13.4 + ; bblanchon/ArduinoJson @ 6.21.5 bblanchon/ArduinoJson @ 7.2.0 mathieucarbou/AsyncTCP @ 3.2.5 board = esp32dev diff --git a/src/AsyncJson.cpp b/src/AsyncJson.cpp index ec464d1..7117a70 100644 --- a/src/AsyncJson.cpp +++ b/src/AsyncJson.cpp @@ -5,7 +5,7 @@ #if ARDUINOJSON_VERSION_MAJOR == 5 AsyncJsonResponse::AsyncJsonResponse(bool isArray) : _isValid{false} { _code = 200; - _contentType = JSON_MIMETYPE; + _contentType = asyncsrv::T_application_json; if (isArray) _root = _jsonBuffer.createArray(); else @@ -14,7 +14,7 @@ AsyncJsonResponse::AsyncJsonResponse(bool isArray) : _isValid{false} { #elif ARDUINOJSON_VERSION_MAJOR == 6 AsyncJsonResponse::AsyncJsonResponse(bool isArray, size_t maxJsonBufferSize) : _jsonBuffer(maxJsonBufferSize), _isValid{false} { _code = 200; - _contentType = JSON_MIMETYPE; + _contentType = asyncsrv::T_application_json; if (isArray) _root = _jsonBuffer.createNestedArray(); else @@ -23,7 +23,7 @@ AsyncJsonResponse::AsyncJsonResponse(bool isArray, size_t maxJsonBufferSize) : _ #else AsyncJsonResponse::AsyncJsonResponse(bool isArray) : _isValid{false} { _code = 200; - _contentType = JSON_MIMETYPE; + _contentType = asyncsrv::T_application_json; if (isArray) _root = _jsonBuffer.add(); else @@ -100,7 +100,7 @@ bool AsyncCallbackJsonWebHandler::canHandle(AsyncWebServerRequest* request) { if (_uri.length() && (_uri != request->url() && !request->url().startsWith(_uri + "/"))) return false; - if (request_method != HTTP_GET && !request->contentType().equalsIgnoreCase(JSON_MIMETYPE)) + if (request_method != HTTP_GET && !request->contentType().equalsIgnoreCase(asyncsrv::T_application_json)) return false; return true; diff --git a/src/AsyncJson.h b/src/AsyncJson.h index e0d88f0..6fc5eb7 100644 --- a/src/AsyncJson.h +++ b/src/AsyncJson.h @@ -36,10 +36,15 @@ #define ASYNC_JSON_H_ #if __has_include("ArduinoJson.h") - - #define ASYNC_JSON_SUPPORT 1 - #include + #if ARDUINOJSON_VERSION_MAJOR >= 5 + #define ASYNC_JSON_SUPPORT 1 + #else + #define ASYNC_JSON_SUPPORT 0 + #endif // ARDUINOJSON_VERSION_MAJOR >= 5 +#endif // __has_include("ArduinoJson.h") + +#if ASYNC_JSON_SUPPORT == 1 #include #include "ChunkPrint.h" @@ -50,12 +55,6 @@ #endif #endif -constexpr const char* JSON_MIMETYPE = "application/json"; - -/* - * Json Response - * */ - class AsyncJsonResponse : public AsyncAbstractResponse { protected: #if ARDUINOJSON_VERSION_MAJOR == 5 @@ -127,8 +126,6 @@ class AsyncCallbackJsonWebHandler : public AsyncWebHandler { virtual bool isRequestHandlerTrivial() override final { return !_onRequest; } }; -#else // __has_include("ArduinoJson.h") - #define ASYNC_JSON_SUPPORT 0 -#endif // __has_include("ArduinoJson.h") +#endif // ASYNC_JSON_SUPPORT == 1 -#endif +#endif // ASYNC_JSON_H_ diff --git a/src/AsyncMessagePack.cpp b/src/AsyncMessagePack.cpp index 27e0786..076f558 100644 --- a/src/AsyncMessagePack.cpp +++ b/src/AsyncMessagePack.cpp @@ -2,6 +2,16 @@ #if ASYNC_MSG_PACK_SUPPORT == 1 + #if ARDUINOJSON_VERSION_MAJOR == 6 +AsyncMessagePackResponse::AsyncMessagePackResponse(bool isArray, size_t maxJsonBufferSize) : _jsonBuffer(maxJsonBufferSize), _isValid{false} { + _code = 200; + _contentType = asyncsrv::T_application_msgpack; + if (isArray) + _root = _jsonBuffer.createNestedArray(); + else + _root = _jsonBuffer.createNestedObject(); +} + #else AsyncMessagePackResponse::AsyncMessagePackResponse(bool isArray) : _isValid{false} { _code = 200; _contentType = asyncsrv::T_application_msgpack; @@ -10,6 +20,7 @@ AsyncMessagePackResponse::AsyncMessagePackResponse(bool isArray) : _isValid{fals else _root = _jsonBuffer.add(); } + #endif size_t AsyncMessagePackResponse::setLength() { _contentLength = measureMsgPack(_root); @@ -25,8 +36,13 @@ size_t AsyncMessagePackResponse::_fillBuffer(uint8_t* data, size_t len) { return len; } -AsyncCallbackMessagePackWebHandler::AsyncCallbackMessagePackWebHandler(const String& uri, ArJsonRequestHandlerFunction onRequest) + #if ARDUINOJSON_VERSION_MAJOR == 6 +AsyncCallbackMessagePackWebHandler::AsyncCallbackMessagePackWebHandler(const String& uri, ArMessagePackRequestHandlerFunction onRequest, size_t maxJsonBufferSize) + : _uri(uri), _method(HTTP_GET | HTTP_POST | HTTP_PUT | HTTP_PATCH), _onRequest(onRequest), maxJsonBufferSize(maxJsonBufferSize), _maxContentLength(16384) {} + #else +AsyncCallbackMessagePackWebHandler::AsyncCallbackMessagePackWebHandler(const String& uri, ArMessagePackRequestHandlerFunction onRequest) : _uri(uri), _method(HTTP_GET | HTTP_POST | HTTP_PUT | HTTP_PATCH), _onRequest(onRequest), _maxContentLength(16384) {} + #endif bool AsyncCallbackMessagePackWebHandler::canHandle(AsyncWebServerRequest* request) { if (!_onRequest) @@ -51,13 +67,20 @@ void AsyncCallbackMessagePackWebHandler::handleRequest(AsyncWebServerRequest* re JsonVariant json; _onRequest(request, json); return; - } else if (request->_tempObject != NULL) { - JsonDocument jsonBuffer; - DeserializationError error = deserializeMsgPack(jsonBuffer, (uint8_t*)(request->_tempObject)); + #if ARDUINOJSON_VERSION_MAJOR == 6 + DynamicJsonDocument jsonBuffer(this->maxJsonBufferSize); + DeserializationError error = deserializeMsgPack(jsonBuffer, (uint8_t*)(request->_tempObject)); if (!error) { JsonVariant json = jsonBuffer.as(); + #else + JsonDocument jsonBuffer; + DeserializationError error = deserializeMsgPack(jsonBuffer, (uint8_t*)(request->_tempObject)); + if (!error) { + JsonVariant json = jsonBuffer.as(); + #endif + _onRequest(request, json); return; } diff --git a/src/AsyncMessagePack.h b/src/AsyncMessagePack.h index cfb9902..3194b5a 100644 --- a/src/AsyncMessagePack.h +++ b/src/AsyncMessagePack.h @@ -22,54 +22,81 @@ */ #if __has_include("ArduinoJson.h") - - #define ASYNC_MSG_PACK_SUPPORT 1 - #include + #if ARDUINOJSON_VERSION_MAJOR >= 6 + #define ASYNC_MSG_PACK_SUPPORT 1 + #else + #define ASYNC_MSG_PACK_SUPPORT 0 + #endif // ARDUINOJSON_VERSION_MAJOR >= 6 +#endif // __has_include("ArduinoJson.h") + +#if ASYNC_MSG_PACK_SUPPORT == 1 #include #include "ChunkPrint.h" + #if ARDUINOJSON_VERSION_MAJOR == 6 + #ifndef DYNAMIC_JSON_DOCUMENT_SIZE + #define DYNAMIC_JSON_DOCUMENT_SIZE 1024 + #endif + #endif + class AsyncMessagePackResponse : public AsyncAbstractResponse { protected: + #if ARDUINOJSON_VERSION_MAJOR == 6 + DynamicJsonDocument _jsonBuffer; + #else JsonDocument _jsonBuffer; + #endif + JsonVariant _root; bool _isValid; public: + #if ARDUINOJSON_VERSION_MAJOR == 6 + AsyncMessagePackResponse(bool isArray = false, size_t maxJsonBufferSize = DYNAMIC_JSON_DOCUMENT_SIZE); + #else AsyncMessagePackResponse(bool isArray = false); - + #endif JsonVariant& getRoot() { return _root; } bool _sourceValid() const { return _isValid; } size_t setLength(); size_t getSize() const { return _jsonBuffer.size(); } size_t _fillBuffer(uint8_t* data, size_t len); + #if ARDUINOJSON_VERSION_MAJOR >= 6 + bool overflowed() const { return _jsonBuffer.overflowed(); } + #endif }; -class AsyncCallbackMessagePackWebHandler : public AsyncWebHandler { - public: - typedef std::function ArJsonRequestHandlerFunction; +typedef std::function ArMessagePackRequestHandlerFunction; +class AsyncCallbackMessagePackWebHandler : public AsyncWebHandler { protected: const String _uri; WebRequestMethodComposite _method; - ArJsonRequestHandlerFunction _onRequest; + ArMessagePackRequestHandlerFunction _onRequest; size_t _contentLength; + #if ARDUINOJSON_VERSION_MAJOR == 6 + const size_t maxJsonBufferSize; + #endif size_t _maxContentLength; public: - AsyncCallbackMessagePackWebHandler(const String& uri, ArJsonRequestHandlerFunction onRequest = nullptr); + #if ARDUINOJSON_VERSION_MAJOR == 6 + AsyncCallbackMessagePackWebHandler(const String& uri, ArMessagePackRequestHandlerFunction onRequest = nullptr, size_t maxJsonBufferSize = DYNAMIC_JSON_DOCUMENT_SIZE); + #else + AsyncCallbackMessagePackWebHandler(const String& uri, ArMessagePackRequestHandlerFunction onRequest = nullptr); + #endif void setMethod(WebRequestMethodComposite method) { _method = method; } void setMaxContentLength(int maxContentLength) { _maxContentLength = maxContentLength; } - void onRequest(ArJsonRequestHandlerFunction fn) { _onRequest = fn; } + void onRequest(ArMessagePackRequestHandlerFunction fn) { _onRequest = fn; } + virtual bool canHandle(AsyncWebServerRequest* request) override final; virtual void handleRequest(AsyncWebServerRequest* request) override final; virtual void handleUpload(__unused AsyncWebServerRequest* request, __unused const String& filename, __unused size_t index, __unused uint8_t* data, __unused size_t len, __unused bool final) override final {} virtual void handleBody(AsyncWebServerRequest* request, uint8_t* data, size_t len, size_t index, size_t total) override final; - virtual bool isRequestHandlerTrivial() override final { return _onRequest ? false : true; } + virtual bool isRequestHandlerTrivial() override final { return !_onRequest; } }; -#else // __has_include("ArduinoJson.h") - #define ASYNC_MSG_PACK_SUPPORT 0 -#endif // __has_include("ArduinoJson.h") +#endif // ASYNC_MSG_PACK_SUPPORT == 1 From 786f7b924bb1065dd935f977d2d82433eb7847e8 Mon Sep 17 00:00:00 2001 From: Mathieu Carbou Date: Mon, 30 Sep 2024 00:53:38 +0200 Subject: [PATCH 2/3] v3.3.3 --- README.md | 2 +- docs/index.md | 2 +- library.json | 2 +- library.properties | 2 +- src/ESPAsyncWebServer.h | 4 ++-- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 4c9d32f..eb373f5 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ This fork is based on [yubox-node-org/ESPAsyncWebServer](https://github.com/yubo **WARNING** The library name was changed from `ESP Async WebServer` to `ESPAsyncWebServer` as per the Arduino Lint recommendations. ``` -mathieucarbou/ESPAsyncWebServer @ 3.3.2 +mathieucarbou/ESPAsyncWebServer @ 3.3.3 ``` Dependency: diff --git a/docs/index.md b/docs/index.md index 4c9d32f..eb373f5 100644 --- a/docs/index.md +++ b/docs/index.md @@ -30,7 +30,7 @@ This fork is based on [yubox-node-org/ESPAsyncWebServer](https://github.com/yubo **WARNING** The library name was changed from `ESP Async WebServer` to `ESPAsyncWebServer` as per the Arduino Lint recommendations. ``` -mathieucarbou/ESPAsyncWebServer @ 3.3.2 +mathieucarbou/ESPAsyncWebServer @ 3.3.3 ``` Dependency: diff --git a/library.json b/library.json index 2f67fc0..2d3c570 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "ESPAsyncWebServer", - "version": "3.3.2", + "version": "3.3.3", "description": "Asynchronous HTTP and WebSocket Server Library for ESP32, ESP8266 and RP2040. Supports: WebSocket, SSE, Authentication, Arduino Json 7, File Upload, Static File serving, URL Rewrite, URL Redirect, etc.", "keywords": "http,async,websocket,webserver", "homepage": "https://github.com/mathieucarbou/ESPAsyncWebServer", diff --git a/library.properties b/library.properties index 402beb2..6e2e949 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=ESPAsyncWebServer -version=3.3.2 +version=3.3.3 author=Me-No-Dev maintainer=Mathieu Carbou sentence=Asynchronous HTTP and WebSocket Server Library for ESP32, ESP8266 and RP2040 diff --git a/src/ESPAsyncWebServer.h b/src/ESPAsyncWebServer.h index 96a129e..2857884 100644 --- a/src/ESPAsyncWebServer.h +++ b/src/ESPAsyncWebServer.h @@ -48,10 +48,10 @@ #include "literals.h" -#define ASYNCWEBSERVER_VERSION "3.3.2" +#define ASYNCWEBSERVER_VERSION "3.3.3" #define ASYNCWEBSERVER_VERSION_MAJOR 3 #define ASYNCWEBSERVER_VERSION_MINOR 3 -#define ASYNCWEBSERVER_VERSION_REVISION 2 +#define ASYNCWEBSERVER_VERSION_REVISION 3 #define ASYNCWEBSERVER_FORK_mathieucarbou #ifdef ASYNCWEBSERVER_REGEX From 2706a62299e0825d585784b7f64e518e4449af4c Mon Sep 17 00:00:00 2001 From: Mathieu Carbou Date: Mon, 30 Sep 2024 01:02:38 +0200 Subject: [PATCH 3/3] Fix CI issue with RPI platform repo --- platformio.ini | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/platformio.ini b/platformio.ini index c650376..b0a28f1 100644 --- a/platformio.ini +++ b/platformio.ini @@ -65,7 +65,7 @@ lib_deps = [env:raspberrypi] upload_protocol = picotool ; platform = raspberrypi -platform = https://github.com/maxgerhardt/platform-raspberrypi.git +platform = https://github.com/maxgerhardt/platform-raspberrypi.git#f2687073f73d554c9db41f29b4769fd9703f4e55 board = rpipicow lib_deps = bblanchon/ArduinoJson @ 7.2.0 @@ -102,7 +102,7 @@ lib_deps = [env:ci-raspberrypi] ; platform = raspberrypi -platform = https://github.com/maxgerhardt/platform-raspberrypi.git +platform = https://github.com/maxgerhardt/platform-raspberrypi.git#f2687073f73d554c9db41f29b4769fd9703f4e55 board = ${sysenv.PIO_BOARD} lib_deps = bblanchon/ArduinoJson @ 7.2.0