Skip to content

Commit 8f4de1f

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

4 files changed

Lines changed: 30 additions & 19 deletions

File tree

src/AsyncJson.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,19 @@ 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()) {
126+
return false;
127+
}
128+
129+
// HTTP_GET | HTTP_POST | HTTP_PUT | HTTP_PATCH
130+
if (!request->isMethod(HTTP_GET) && !request->isMethod(HTTP_POST) && !request->isMethod(HTTP_PUT) && !request->isMethod(HTTP_PATCH)) {
127131
return false;
128132
}
129133

src/ESPAsyncWebServer.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,13 @@ class AsyncWebServerRequest {
347347
WebRequestMethodComposite method() const {
348348
return _method;
349349
}
350+
bool isMethod(WebRequestMethodComposite method) const {
351+
#if defined(TARGET_RP2040) || defined(TARGET_RP2350) || defined(PICO_RP2040) || defined(PICO_RP2350)
352+
return (_method == method) || (method == HTTP_ANY);
353+
#else
354+
return (_method & method) != 0;
355+
#endif
356+
}
350357
const String &url() const {
351358
return _url;
352359
}

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)