mirror of
https://github.com/wled/WLED.git
synced 2025-07-19 08:46:34 +00:00
Prevent JSON buffer clear after failed lock attempt
(alternative to #3743)
This commit is contained in:
parent
e165838e54
commit
6dcd9596a2
@ -1030,17 +1030,18 @@ void serializeModeNames(JsonArray arr)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Global buffer locking response helper class (to make sure lock is released when AsyncJsonResponse is destroyed)
|
||||||
// Global buffer locking response helper class
|
class LockedJsonResponse: public AsyncJsonResponse {
|
||||||
class GlobalBufferAsyncJsonResponse: public JSONBufferGuard, public AsyncJsonResponse {
|
|
||||||
public:
|
public:
|
||||||
inline GlobalBufferAsyncJsonResponse(bool isArray) : JSONBufferGuard(17), AsyncJsonResponse(pDoc, isArray) {};
|
// WARNING: constructor assumes requestJSONBufferLock() was successfully acquired externally/prior to constructing the instance
|
||||||
virtual ~GlobalBufferAsyncJsonResponse() {};
|
// Not a good practice with C++. Unfortunately AsyncJsonResponse only has 2 constructors - for dynamic buffer or existing buffer,
|
||||||
|
// with existing buffer it clears its content during construction
|
||||||
// Other members are inherited
|
// if the lock was not acquired (using JSONBufferGuard class) previous implementation still cleared existing buffer
|
||||||
|
inline LockedJsonResponse(JsonDocument *doc, bool isArray) : AsyncJsonResponse(doc, isArray) {};
|
||||||
|
// destructor will remove JSON buffer lock when response is destroyed in AsyncWebServer
|
||||||
|
virtual ~LockedJsonResponse() { releaseJSONBufferLock(); };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
void serveJson(AsyncWebServerRequest* request)
|
void serveJson(AsyncWebServerRequest* request)
|
||||||
{
|
{
|
||||||
byte subJson = 0;
|
byte subJson = 0;
|
||||||
@ -1071,12 +1072,13 @@ void serveJson(AsyncWebServerRequest* request)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
GlobalBufferAsyncJsonResponse *response = new GlobalBufferAsyncJsonResponse(subJson==JSON_PATH_FXDATA || subJson==JSON_PATH_EFFECTS); // will clear and convert JsonDocument into JsonArray if necessary
|
if (!requestJSONBufferLock(17)) {
|
||||||
if (!response->owns_lock()) {
|
|
||||||
serveJsonError(request, 503, ERR_NOBUF);
|
serveJsonError(request, 503, ERR_NOBUF);
|
||||||
delete response;
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// releaseJSONBufferLock() will be called when "response" is destroyed (from AsyncWebServer)
|
||||||
|
// make sure you delete "response" if no "request->send(response);" is made
|
||||||
|
LockedJsonResponse *response = new LockedJsonResponse(pDoc, subJson==JSON_PATH_FXDATA || subJson==JSON_PATH_EFFECTS); // will clear and convert JsonDocument into JsonArray if necessary
|
||||||
|
|
||||||
JsonVariant lDoc = response->getRoot();
|
JsonVariant lDoc = response->getRoot();
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user