Skip to content

Commit e8fbf42

Browse files
committed
Introduce request#isMethod(...) to check the http method of the request (fixes #398)
1 parent e939f7e commit e8fbf42

4 files changed

Lines changed: 21 additions & 25 deletions

File tree

src/AsyncJson.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,14 @@ size_t AsyncMessagePackResponse::_fillBuffer(uint8_t *data, size_t len) {
115115

116116
#if ARDUINOJSON_VERSION_MAJOR == 6
117117
AsyncCallbackJsonWebHandler::AsyncCallbackJsonWebHandler(AsyncURIMatcher uri, ArJsonRequestHandlerFunction onRequest, size_t maxJsonBufferSize)
118-
: _uri(std::move(uri)), _method(HTTP_GET | HTTP_POST | HTTP_PUT | HTTP_PATCH), _onRequest(onRequest), maxJsonBufferSize(maxJsonBufferSize),
119-
_maxContentLength(16384) {}
118+
: _uri(std::move(uri)), _method(HTTP_ANY), _onRequest(onRequest), maxJsonBufferSize(maxJsonBufferSize), _maxContentLength(16384) {}
120119
#else
121120
AsyncCallbackJsonWebHandler::AsyncCallbackJsonWebHandler(AsyncURIMatcher uri, ArJsonRequestHandlerFunction onRequest)
122-
: _uri(std::move(uri)), _method(HTTP_GET | HTTP_POST | HTTP_PUT | HTTP_PATCH), _onRequest(onRequest), _maxContentLength(16384) {}
121+
: _uri(std::move(uri)), _method(HTTP_ANY), _onRequest(onRequest), _maxContentLength(16384) {}
123122
#endif
124123

125124
bool AsyncCallbackJsonWebHandler::canHandle(AsyncWebServerRequest *request) const {
126-
if (!_onRequest || !request->isHTTP() || !(_method & request->method())) {
125+
if (!_onRequest || !request->isHTTP() || !request->isMethod(_method)) {
127126
return false;
128127
}
129128

src/ESPAsyncWebServer.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@
4141
#include <ESPAsyncTCP.h>
4242
#elif defined(TARGET_RP2040) || defined(TARGET_RP2350) || defined(PICO_RP2040) || defined(PICO_RP2350)
4343
#include <RPAsyncTCP.h>
44-
#include <HTTP_Method.h>
45-
#include <http_parser.h>
4644
#else
4745
#error Platform not supported
4846
#endif
@@ -80,9 +78,6 @@ class AsyncCallbackWebHandler;
8078
class AsyncResponseStream;
8179
class AsyncMiddlewareChain;
8280

83-
#if defined(TARGET_RP2040) || defined(TARGET_RP2350) || defined(PICO_RP2040) || defined(PICO_RP2350)
84-
typedef enum http_method WebRequestMethod;
85-
#else
8681
#ifndef WEBSERVER_H
8782
typedef enum {
8883
HTTP_GET = 0b0000000000000001,
@@ -103,7 +98,6 @@ typedef enum {
10398
HTTP_ANY = 0b0111111111111111,
10499
} WebRequestMethod;
105100
#endif
106-
#endif
107101

108102
#ifndef HAVE_FS_FILE_OPEN_MODE
109103
namespace fs {
@@ -347,6 +341,9 @@ class AsyncWebServerRequest {
347341
WebRequestMethodComposite method() const {
348342
return _method;
349343
}
344+
bool isMethod(WebRequestMethodComposite method) const {
345+
return (_method & method) != 0;
346+
}
350347
const String &url() const {
351348
return _url;
352349
}

src/WebHandlers.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ void AsyncCallbackWebHandler::setUri(AsyncURIMatcher uri) {
300300
}
301301

302302
bool AsyncCallbackWebHandler::canHandle(AsyncWebServerRequest *request) const {
303-
if (!_onRequest || !request->isHTTP() || !(_method & request->method())) {
303+
if (!_onRequest || !request->isHTTP() || !request->isMethod(_method)) {
304304
return false;
305305
}
306306
return _uri.matches(request);

src/WebRequest.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,46 +1314,46 @@ const char *AsyncWebServerRequest::methodToString() const {
13141314
if (_method == HTTP_ANY) {
13151315
return T_ANY;
13161316
}
1317-
if (_method & HTTP_GET) {
1317+
if (isMethod(HTTP_GET)) {
13181318
return T_GET;
13191319
}
1320-
if (_method & HTTP_POST) {
1320+
if (isMethod(HTTP_POST)) {
13211321
return T_POST;
13221322
}
1323-
if (_method & HTTP_DELETE) {
1323+
if (isMethod(HTTP_DELETE)) {
13241324
return T_DELETE;
13251325
}
1326-
if (_method & HTTP_PUT) {
1326+
if (isMethod(HTTP_PUT)) {
13271327
return T_PUT;
13281328
}
1329-
if (_method & HTTP_PATCH) {
1329+
if (isMethod(HTTP_PATCH)) {
13301330
return T_PATCH;
13311331
}
1332-
if (_method & HTTP_HEAD) {
1332+
if (isMethod(HTTP_HEAD)) {
13331333
return T_HEAD;
13341334
}
1335-
if (_method & HTTP_OPTIONS) {
1335+
if (isMethod(HTTP_OPTIONS)) {
13361336
return T_OPTIONS;
13371337
}
1338-
if (_method & HTTP_PROPFIND) {
1338+
if (isMethod(HTTP_PROPFIND)) {
13391339
return T_PROPFIND;
13401340
}
1341-
if (_method & HTTP_LOCK) {
1341+
if (isMethod(HTTP_LOCK)) {
13421342
return T_LOCK;
13431343
}
1344-
if (_method & HTTP_UNLOCK) {
1344+
if (isMethod(HTTP_UNLOCK)) {
13451345
return T_UNLOCK;
13461346
}
1347-
if (_method & HTTP_PROPPATCH) {
1347+
if (isMethod(HTTP_PROPPATCH)) {
13481348
return T_PROPPATCH;
13491349
}
1350-
if (_method & HTTP_MKCOL) {
1350+
if (isMethod(HTTP_MKCOL)) {
13511351
return T_MKCOL;
13521352
}
1353-
if (_method & HTTP_MOVE) {
1353+
if (isMethod(HTTP_MOVE)) {
13541354
return T_MOVE;
13551355
}
1356-
if (_method & HTTP_COPY) {
1356+
if (isMethod(HTTP_COPY)) {
13571357
return T_COPY;
13581358
}
13591359
return T_UNKNOWN;

0 commit comments

Comments
 (0)