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

Add support for Auth & HTTP_GET for AsyncCallbackJsonWebHandler #1384

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/AsyncJson.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,10 @@ class AsyncCallbackJsonWebHandler: public AsyncWebHandler {
public:
#ifdef ARDUINOJSON_5_COMPATIBILITY
AsyncCallbackJsonWebHandler(const String& uri, ArJsonRequestHandlerFunction onRequest)
: _uri(uri), _method(HTTP_POST|HTTP_PUT|HTTP_PATCH), _onRequest(onRequest), _maxContentLength(16384) {}
: _uri(uri), _method(HTTP_GET|HTTP_POST|HTTP_PUT|HTTP_PATCH), _onRequest(onRequest), _maxContentLength(16384) {}
#else
AsyncCallbackJsonWebHandler(const String& uri, ArJsonRequestHandlerFunction onRequest, size_t maxJsonBufferSize=DYNAMIC_JSON_DOCUMENT_SIZE)
: _uri(uri), _method(HTTP_POST|HTTP_PUT|HTTP_PATCH), _onRequest(onRequest), maxJsonBufferSize(maxJsonBufferSize), _maxContentLength(16384) {}
: _uri(uri), _method(HTTP_GET|HTTP_POST|HTTP_PUT|HTTP_PATCH), _onRequest(onRequest), maxJsonBufferSize(maxJsonBufferSize), _maxContentLength(16384) {}
#endif

void setMethod(WebRequestMethodComposite method){ _method = method; }
Expand All @@ -199,22 +199,32 @@ class AsyncCallbackJsonWebHandler: public AsyncWebHandler {
if(!_onRequest)
return false;

if(!(_method & request->method()))
WebRequestMethodComposite request_method = request->method();

if(!(_method & request_method))
return false;

if(_uri.length() && (_uri != request->url() && !request->url().startsWith(_uri+"/")))
return false;

if ( !request->contentType().equalsIgnoreCase(JSON_MIMETYPE) )
if (request_method != HTTP_GET && !request->contentType().equalsIgnoreCase(JSON_MIMETYPE) )
return false;

request->addInterestingHeader("ANY");
return true;
}

virtual void handleRequest(AsyncWebServerRequest *request) override final {
if((_username != "" && _password != "") && !request->authenticate(_username.c_str(), _password.c_str()))
return request->requestAuthentication();

if(_onRequest) {
if (request->_tempObject != NULL) {

if (request->method() == HTTP_GET) {
JsonVariant json;
_onRequest(request, json);
return;
} else if (request->_tempObject != NULL) {

#ifdef ARDUINOJSON_5_COMPATIBILITY
DynamicJsonBuffer jsonBuffer;
Expand Down