| 2 |
raymond |
1 |
// SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
2 |
// Copyright 2016-2026 Hristo Gochkov, Mathieu Carbou, Emil Muratov, Will Miles
|
|
|
3 |
|
|
|
4 |
#pragma once
|
|
|
5 |
|
|
|
6 |
#include <ESPAsyncWebServer.h>
|
|
|
7 |
#include "ChunkPrint.h"
|
|
|
8 |
|
|
|
9 |
#include <utility>
|
|
|
10 |
|
|
|
11 |
#if ASYNC_JSON_SUPPORT == 1
|
|
|
12 |
|
|
|
13 |
#if ARDUINOJSON_VERSION_MAJOR == 6
|
|
|
14 |
#ifndef DYNAMIC_JSON_DOCUMENT_SIZE
|
|
|
15 |
#define DYNAMIC_JSON_DOCUMENT_SIZE 1024
|
|
|
16 |
#endif
|
|
|
17 |
#endif
|
|
|
18 |
|
|
|
19 |
// Json content type response classes
|
|
|
20 |
|
|
|
21 |
class AsyncJsonResponse : public AsyncAbstractResponse {
|
|
|
22 |
protected:
|
|
|
23 |
#if ARDUINOJSON_VERSION_MAJOR == 5
|
|
|
24 |
DynamicJsonBuffer _jsonBuffer;
|
|
|
25 |
#elif ARDUINOJSON_VERSION_MAJOR == 6
|
|
|
26 |
DynamicJsonDocument _jsonBuffer;
|
|
|
27 |
#else
|
|
|
28 |
JsonDocument _jsonBuffer;
|
|
|
29 |
#endif
|
|
|
30 |
|
|
|
31 |
JsonVariant _root;
|
|
|
32 |
bool _isValid;
|
|
|
33 |
|
|
|
34 |
public:
|
|
|
35 |
#if ARDUINOJSON_VERSION_MAJOR == 6
|
|
|
36 |
AsyncJsonResponse(bool isArray = false, size_t maxJsonBufferSize = DYNAMIC_JSON_DOCUMENT_SIZE);
|
|
|
37 |
#else
|
|
|
38 |
AsyncJsonResponse(bool isArray = false);
|
|
|
39 |
#endif
|
|
|
40 |
JsonVariant &getRoot() {
|
|
|
41 |
return _root;
|
|
|
42 |
}
|
|
|
43 |
bool _sourceValid() const {
|
|
|
44 |
return _isValid;
|
|
|
45 |
}
|
|
|
46 |
virtual size_t setLength();
|
|
|
47 |
size_t getSize() const {
|
|
|
48 |
return _jsonBuffer.size();
|
|
|
49 |
}
|
|
|
50 |
virtual size_t _fillBuffer(uint8_t *data, size_t len);
|
|
|
51 |
#if ARDUINOJSON_VERSION_MAJOR >= 6
|
|
|
52 |
bool overflowed() const {
|
|
|
53 |
return _jsonBuffer.overflowed();
|
|
|
54 |
}
|
|
|
55 |
#endif
|
|
|
56 |
};
|
|
|
57 |
|
|
|
58 |
class PrettyAsyncJsonResponse : public AsyncJsonResponse {
|
|
|
59 |
public:
|
|
|
60 |
#if ARDUINOJSON_VERSION_MAJOR == 6
|
|
|
61 |
PrettyAsyncJsonResponse(bool isArray = false, size_t maxJsonBufferSize = DYNAMIC_JSON_DOCUMENT_SIZE);
|
|
|
62 |
#else
|
|
|
63 |
PrettyAsyncJsonResponse(bool isArray = false);
|
|
|
64 |
#endif
|
|
|
65 |
size_t setLength() override;
|
|
|
66 |
size_t _fillBuffer(uint8_t *data, size_t len) override;
|
|
|
67 |
};
|
|
|
68 |
|
|
|
69 |
// MessagePack content type response
|
|
|
70 |
#if ASYNC_MSG_PACK_SUPPORT == 1
|
|
|
71 |
|
|
|
72 |
class AsyncMessagePackResponse : public AsyncJsonResponse {
|
|
|
73 |
public:
|
|
|
74 |
#if ARDUINOJSON_VERSION_MAJOR == 6
|
|
|
75 |
AsyncMessagePackResponse(bool isArray = false, size_t maxJsonBufferSize = DYNAMIC_JSON_DOCUMENT_SIZE) : AsyncJsonResponse(isArray, maxJsonBufferSize) {
|
|
|
76 |
_contentType = asyncsrv::T_application_msgpack;
|
|
|
77 |
}
|
|
|
78 |
#else
|
|
|
79 |
AsyncMessagePackResponse(bool isArray = false) : AsyncJsonResponse(isArray) {
|
|
|
80 |
_contentType = asyncsrv::T_application_msgpack;
|
|
|
81 |
}
|
|
|
82 |
#endif
|
|
|
83 |
size_t setLength() override;
|
|
|
84 |
size_t _fillBuffer(uint8_t *data, size_t len) override;
|
|
|
85 |
};
|
|
|
86 |
|
|
|
87 |
#endif
|
|
|
88 |
|
|
|
89 |
// Body handler supporting both content types: JSON and MessagePack
|
|
|
90 |
|
|
|
91 |
class AsyncCallbackJsonWebHandler : public AsyncWebHandler {
|
|
|
92 |
protected:
|
|
|
93 |
AsyncURIMatcher _uri;
|
|
|
94 |
WebRequestMethodComposite _method;
|
|
|
95 |
ArJsonRequestHandlerFunction _onRequest;
|
|
|
96 |
#if ARDUINOJSON_VERSION_MAJOR == 6
|
|
|
97 |
size_t maxJsonBufferSize;
|
|
|
98 |
#endif
|
|
|
99 |
size_t _maxContentLength;
|
|
|
100 |
|
|
|
101 |
public:
|
|
|
102 |
#if ARDUINOJSON_VERSION_MAJOR == 6
|
|
|
103 |
AsyncCallbackJsonWebHandler(AsyncURIMatcher uri, ArJsonRequestHandlerFunction onRequest = nullptr, size_t maxJsonBufferSize = DYNAMIC_JSON_DOCUMENT_SIZE);
|
|
|
104 |
#else
|
|
|
105 |
AsyncCallbackJsonWebHandler(AsyncURIMatcher uri, ArJsonRequestHandlerFunction onRequest = nullptr);
|
|
|
106 |
#endif
|
|
|
107 |
|
|
|
108 |
void setMethod(WebRequestMethodComposite method) {
|
|
|
109 |
_method = std::move(method);
|
|
|
110 |
}
|
|
|
111 |
void setMaxContentLength(int maxContentLength) {
|
|
|
112 |
_maxContentLength = maxContentLength;
|
|
|
113 |
}
|
|
|
114 |
void onRequest(ArJsonRequestHandlerFunction fn) {
|
|
|
115 |
_onRequest = fn;
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
bool canHandle(AsyncWebServerRequest *request) const final;
|
|
|
119 |
void handleRequest(AsyncWebServerRequest *request) final;
|
|
|
120 |
void handleUpload(
|
|
|
121 |
__asyncws_unused AsyncWebServerRequest *request, __asyncws_unused const String &filename, __asyncws_unused size_t index, __asyncws_unused uint8_t *data,
|
|
|
122 |
__asyncws_unused size_t len, __asyncws_unused bool final
|
|
|
123 |
) final {}
|
|
|
124 |
void handleBody(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total) final;
|
|
|
125 |
bool isRequestHandlerTrivial() const final {
|
|
|
126 |
return !_onRequest;
|
|
|
127 |
}
|
|
|
128 |
};
|
|
|
129 |
|
|
|
130 |
#endif // ASYNC_JSON_SUPPORT == 1
|