Skip to content

Commit 9d2bec2

Browse files
author
Niklas Lönn
committed
Reapplied changes from me-no-dev#1384
1 parent af1df03 commit 9d2bec2

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/AsyncJson.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,10 @@ class AsyncCallbackJsonWebHandler : public AsyncWebHandler {
197197
public:
198198
#if ARDUINOJSON_VERSION_MAJOR == 6
199199
AsyncCallbackJsonWebHandler(const String& uri, ArJsonRequestHandlerFunction onRequest, size_t maxJsonBufferSize = DYNAMIC_JSON_DOCUMENT_SIZE)
200-
: _uri(uri), _method(HTTP_POST | HTTP_PUT | HTTP_PATCH), _onRequest(onRequest), maxJsonBufferSize(maxJsonBufferSize), _maxContentLength(16384) {}
200+
: _uri(uri), _method(HTTP_GET | HTTP_POST | HTTP_PUT | HTTP_PATCH), _onRequest(onRequest), maxJsonBufferSize(maxJsonBufferSize), _maxContentLength(16384) {}
201201
#else
202202
AsyncCallbackJsonWebHandler(const String& uri, ArJsonRequestHandlerFunction onRequest)
203-
: _uri(uri), _method(HTTP_POST | HTTP_PUT | HTTP_PATCH), _onRequest(onRequest), _maxContentLength(16384) {}
203+
: _uri(uri), _method(HTTP_GET | HTTP_POST | HTTP_PUT | HTTP_PATCH), _onRequest(onRequest), _maxContentLength(16384) {}
204204
#endif
205205

206206
void setMethod(WebRequestMethodComposite method) { _method = method; }
@@ -211,22 +211,29 @@ class AsyncCallbackJsonWebHandler : public AsyncWebHandler {
211211
if (!_onRequest)
212212
return false;
213213

214-
if (!(_method & request->method()))
214+
WebRequestMethodComposite request_method = request->method();
215+
if (!(_method & request_method))
215216
return false;
216217

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

220-
if (!request->contentType().equalsIgnoreCase(JSON_MIMETYPE))
221+
if (request_method != HTTP_GET && !request->contentType().equalsIgnoreCase(JSON_MIMETYPE))
221222
return false;
222223

223224
request->addInterestingHeader("ANY");
224225
return true;
225226
}
226227

227228
virtual void handleRequest(AsyncWebServerRequest* request) override final {
229+
if((_username != "" && _password != "") && !request->authenticate(_username.c_str(), _password.c_str()))
230+
return request->requestAuthentication();
228231
if (_onRequest) {
229-
if (request->_tempObject != NULL) {
232+
if (request->method() == HTTP_GET) {
233+
JsonVariant json;
234+
_onRequest(request, json);
235+
return;
236+
} else if (request->_tempObject != NULL) {
230237

231238
#if ARDUINOJSON_VERSION_MAJOR == 5
232239
DynamicJsonBuffer jsonBuffer;

0 commit comments

Comments
 (0)