Skip to content
This repository was archived by the owner on Jan 21, 2025. It is now read-only.

Commit b473625

Browse files
committed
Moved implementations in cpp files
1 parent 546f9ed commit b473625

16 files changed

+553
-483
lines changed

platformio.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ board = rpipicow
6161
lib_deps =
6262
bblanchon/ArduinoJson @ 7.2.0
6363
khoih-prog/AsyncTCP_RP2040W @ 1.2.0
64+
build_flags = ${env.build_flags}
65+
-Wno-missing-field-initializers
6466

6567
; CI
6668

@@ -90,3 +92,5 @@ board = ${sysenv.PIO_BOARD}
9092
lib_deps =
9193
bblanchon/ArduinoJson @ 7.2.0
9294
khoih-prog/AsyncTCP_RP2040W @ 1.2.0
95+
build_flags = ${env.build_flags}
96+
-Wno-missing-field-initializers

src/AsyncEventSource.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include <rom/ets_sys.h>
2323
#endif
2424
#include "AsyncEventSource.h"
25-
#include "literals.h"
2625

2726
using namespace asyncsrv;
2827

@@ -288,6 +287,12 @@ void AsyncEventSource::onConnect(ArEventHandlerFunction cb) {
288287
_connectcb = cb;
289288
}
290289

290+
void AsyncEventSource::authorizeConnect(ArAuthorizeConnectHandler cb) {
291+
AuthorizationMiddleware* m = new AuthorizationMiddleware(401, cb);
292+
m->_freeOnRemoval = true;
293+
addMiddleware(m);
294+
}
295+
291296
void AsyncEventSource::_addClient(AsyncEventSourceClient* client) {
292297
if (!client)
293298
return;

src/AsyncEventSource.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,7 @@ class AsyncEventSource : public AsyncWebHandler {
124124
const char* url() const { return _url.c_str(); }
125125
void close();
126126
void onConnect(ArEventHandlerFunction cb);
127-
void authorizeConnect(ArAuthorizeConnectHandler cb) {
128-
AuthorizationMiddleware* m = new AuthorizationMiddleware(401, cb);
129-
m->_freeOnRemoval = true;
130-
addMiddleware(m);
131-
}
127+
void authorizeConnect(ArAuthorizeConnectHandler cb);
132128
void send(const String& message, const String& event, uint32_t id = 0, uint32_t reconnect = 0) { send(message.c_str(), event.c_str(), id, reconnect); }
133129
void send(const String& message, const char* event, uint32_t id = 0, uint32_t reconnect = 0) { send(message.c_str(), event, id, reconnect); }
134130
void send(const char* message, const char* event = NULL, uint32_t id = 0, uint32_t reconnect = 0);

src/AsyncJson.cpp

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
#include "AsyncJson.h"
2+
3+
#if ARDUINOJSON_VERSION_MAJOR == 5
4+
AsyncJsonResponse::AsyncJsonResponse(bool isArray) : _isValid{false} {
5+
_code = 200;
6+
_contentType = JSON_MIMETYPE;
7+
if (isArray)
8+
_root = _jsonBuffer.createArray();
9+
else
10+
_root = _jsonBuffer.createObject();
11+
}
12+
#elif ARDUINOJSON_VERSION_MAJOR == 6
13+
AsyncJsonResponse::AsyncJsonResponse(bool isArray, size_t maxJsonBufferSize) : _jsonBuffer(maxJsonBufferSize), _isValid{false} {
14+
_code = 200;
15+
_contentType = JSON_MIMETYPE;
16+
if (isArray)
17+
_root = _jsonBuffer.createNestedArray();
18+
else
19+
_root = _jsonBuffer.createNestedObject();
20+
}
21+
#else
22+
AsyncJsonResponse::AsyncJsonResponse(bool isArray) : _isValid{false} {
23+
_code = 200;
24+
_contentType = JSON_MIMETYPE;
25+
if (isArray)
26+
_root = _jsonBuffer.add<JsonArray>();
27+
else
28+
_root = _jsonBuffer.add<JsonObject>();
29+
}
30+
#endif
31+
32+
size_t AsyncJsonResponse::setLength() {
33+
#if ARDUINOJSON_VERSION_MAJOR == 5
34+
_contentLength = _root.measureLength();
35+
#else
36+
_contentLength = measureJson(_root);
37+
#endif
38+
if (_contentLength) {
39+
_isValid = true;
40+
}
41+
return _contentLength;
42+
}
43+
44+
size_t AsyncJsonResponse::_fillBuffer(uint8_t* data, size_t len) {
45+
ChunkPrint dest(data, _sentLength, len);
46+
#if ARDUINOJSON_VERSION_MAJOR == 5
47+
_root.printTo(dest);
48+
#else
49+
serializeJson(_root, dest);
50+
#endif
51+
return len;
52+
}
53+
54+
#if ARDUINOJSON_VERSION_MAJOR == 6
55+
PrettyAsyncJsonResponse::PrettyAsyncJsonResponse(bool isArray, size_t maxJsonBufferSize) : AsyncJsonResponse{isArray, maxJsonBufferSize} {}
56+
#else
57+
PrettyAsyncJsonResponse::PrettyAsyncJsonResponse(bool isArray) : AsyncJsonResponse{isArray} {}
58+
#endif
59+
60+
size_t PrettyAsyncJsonResponse::setLength() {
61+
#if ARDUINOJSON_VERSION_MAJOR == 5
62+
_contentLength = _root.measurePrettyLength();
63+
#else
64+
_contentLength = measureJsonPretty(_root);
65+
#endif
66+
if (_contentLength) {
67+
_isValid = true;
68+
}
69+
return _contentLength;
70+
}
71+
72+
size_t PrettyAsyncJsonResponse::_fillBuffer(uint8_t* data, size_t len) {
73+
ChunkPrint dest(data, _sentLength, len);
74+
#if ARDUINOJSON_VERSION_MAJOR == 5
75+
_root.prettyPrintTo(dest);
76+
#else
77+
serializeJsonPretty(_root, dest);
78+
#endif
79+
return len;
80+
}
81+
82+
#if ARDUINOJSON_VERSION_MAJOR == 6
83+
AsyncCallbackJsonWebHandler::AsyncCallbackJsonWebHandler(const String& uri, ArJsonRequestHandlerFunction onRequest, size_t maxJsonBufferSize)
84+
: _uri(uri), _method(HTTP_GET | HTTP_POST | HTTP_PUT | HTTP_PATCH), _onRequest(onRequest), maxJsonBufferSize(maxJsonBufferSize), _maxContentLength(16384) {}
85+
#else
86+
AsyncCallbackJsonWebHandler::AsyncCallbackJsonWebHandler(const String& uri, ArJsonRequestHandlerFunction onRequest)
87+
: _uri(uri), _method(HTTP_GET | HTTP_POST | HTTP_PUT | HTTP_PATCH), _onRequest(onRequest), _maxContentLength(16384) {}
88+
#endif
89+
90+
bool AsyncCallbackJsonWebHandler::canHandle(AsyncWebServerRequest* request) {
91+
if (!_onRequest)
92+
return false;
93+
94+
WebRequestMethodComposite request_method = request->method();
95+
if (!(_method & request_method))
96+
return false;
97+
98+
if (_uri.length() && (_uri != request->url() && !request->url().startsWith(_uri + "/")))
99+
return false;
100+
101+
if (request_method != HTTP_GET && !request->contentType().equalsIgnoreCase(JSON_MIMETYPE))
102+
return false;
103+
104+
return true;
105+
}
106+
107+
void AsyncCallbackJsonWebHandler::handleRequest(AsyncWebServerRequest* request) {
108+
if (_onRequest) {
109+
if (request->method() == HTTP_GET) {
110+
JsonVariant json;
111+
_onRequest(request, json);
112+
return;
113+
} else if (request->_tempObject != NULL) {
114+
115+
#if ARDUINOJSON_VERSION_MAJOR == 5
116+
DynamicJsonBuffer jsonBuffer;
117+
JsonVariant json = jsonBuffer.parse((uint8_t*)(request->_tempObject));
118+
if (json.success()) {
119+
#elif ARDUINOJSON_VERSION_MAJOR == 6
120+
DynamicJsonDocument jsonBuffer(this->maxJsonBufferSize);
121+
DeserializationError error = deserializeJson(jsonBuffer, (uint8_t*)(request->_tempObject));
122+
if (!error) {
123+
JsonVariant json = jsonBuffer.as<JsonVariant>();
124+
#else
125+
JsonDocument jsonBuffer;
126+
DeserializationError error = deserializeJson(jsonBuffer, (uint8_t*)(request->_tempObject));
127+
if (!error) {
128+
JsonVariant json = jsonBuffer.as<JsonVariant>();
129+
#endif
130+
131+
_onRequest(request, json);
132+
return;
133+
}
134+
}
135+
request->send(_contentLength > _maxContentLength ? 413 : 400);
136+
} else {
137+
request->send(500);
138+
}
139+
}
140+
141+
void AsyncCallbackJsonWebHandler::handleBody(AsyncWebServerRequest* request, uint8_t* data, size_t len, size_t index, size_t total) {
142+
if (_onRequest) {
143+
_contentLength = total;
144+
if (total > 0 && request->_tempObject == NULL && total < _maxContentLength) {
145+
request->_tempObject = malloc(total);
146+
}
147+
if (request->_tempObject != NULL) {
148+
memcpy((uint8_t*)(request->_tempObject) + index, data, len);
149+
}
150+
}
151+
}

0 commit comments

Comments
 (0)