Skip to content

Commit 5da58f3

Browse files
Merge pull request #400 from willmmiles/namespace-method-names
2 parents e939f7e + e0c85cb commit 5da58f3

6 files changed

Lines changed: 69 additions & 56 deletions

File tree

src/AsyncJson.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,15 @@ size_t AsyncMessagePackResponse::_fillBuffer(uint8_t *data, size_t len) {
112112
#endif
113113

114114
// Body handler supporting both content types: JSON and MessagePack
115+
constexpr static bool JsonHandlerMethods =
116+
AsyncWebRequestMethod::HTTP_GET | AsyncWebRequestMethod::HTTP_POST | AsyncWebRequestMethod::HTTP_PUT | AsyncWebRequestMethod::HTTP_PATCH;
115117

116118
#if ARDUINOJSON_VERSION_MAJOR == 6
117119
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) {}
120+
: _uri(std::move(uri)), _method(JsonHandlerTypes), _onRequest(onRequest), maxJsonBufferSize(maxJsonBufferSize), _maxContentLength(16384) {}
120121
#else
121122
AsyncCallbackJsonWebHandler::AsyncCallbackJsonWebHandler(AsyncURIMatcher uri, ArJsonRequestHandlerFunction onRequest)
122-
: _uri(std::move(uri)), _method(HTTP_GET | HTTP_POST | HTTP_PUT | HTTP_PATCH), _onRequest(onRequest), _maxContentLength(16384) {}
123+
: _uri(std::move(uri)), _method(JsonHandlerMethods), _onRequest(onRequest), _maxContentLength(16384) {}
123124
#endif
124125

125126
bool AsyncCallbackJsonWebHandler::canHandle(AsyncWebServerRequest *request) const {
@@ -132,17 +133,17 @@ bool AsyncCallbackJsonWebHandler::canHandle(AsyncWebServerRequest *request) cons
132133
}
133134

134135
#if ASYNC_MSG_PACK_SUPPORT == 1
135-
return request->method() == HTTP_GET || request->contentType().equalsIgnoreCase(asyncsrv::T_application_json)
136+
return request->method() == AsyncWebRequestMethod::HTTP_GET || request->contentType().equalsIgnoreCase(asyncsrv::T_application_json)
136137
|| request->contentType().equalsIgnoreCase(asyncsrv::T_application_msgpack);
137138
#else
138-
return request->method() == HTTP_GET || request->contentType().equalsIgnoreCase(asyncsrv::T_application_json);
139+
return request->method() == AsyncWebRequestMethod::HTTP_GET || request->contentType().equalsIgnoreCase(asyncsrv::T_application_json);
139140
#endif
140141
}
141142

142143
void AsyncCallbackJsonWebHandler::handleRequest(AsyncWebServerRequest *request) {
143144
if (_onRequest) {
144145
// GET request:
145-
if (request->method() == HTTP_GET) {
146+
if (request->method() == AsyncWebRequestMethod::HTTP_GET) {
146147
JsonVariant json;
147148
_onRequest(request, json);
148149
return;

src/ESPAsyncWebServer.h

Lines changed: 24 additions & 13 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,11 +78,10 @@ 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
86-
#ifndef WEBSERVER_H
87-
typedef enum {
81+
// Namespace for web request method defines
82+
namespace AsyncWebRequestMethod {
83+
// The long name here is because we sometimes include this in the global namespace
84+
enum AsyncWebRequestMethodType {
8885
HTTP_GET = 0b0000000000000001,
8986
HTTP_POST = 0b0000000000000010,
9087
HTTP_DELETE = 0b0000000000000100,
@@ -101,8 +98,23 @@ typedef enum {
10198
HTTP_COPY = 0b0010000000000000,
10299
HTTP_RESERVED = 0b0100000000000000,
103100
HTTP_ANY = 0b0111111111111111,
104-
} WebRequestMethod;
105-
#endif
101+
};
102+
}; // namespace AsyncWebRequestMethod
103+
104+
typedef AsyncWebRequestMethod::AsyncWebRequestMethodType WebRequestMethod;
105+
typedef uint16_t WebRequestMethodComposite;
106+
107+
// Type-safe helper functions for composite methods
108+
extern constexpr inline WebRequestMethodComposite operator|(WebRequestMethodComposite l, WebRequestMethod r) {
109+
return l | static_cast<WebRequestMethodComposite>(r);
110+
};
111+
extern constexpr inline WebRequestMethodComposite operator|(WebRequestMethod l, WebRequestMethod r) {
112+
return static_cast<WebRequestMethodComposite>(l) | r;
113+
};
114+
115+
#if !defined(ASYNCWEBSERVER_NO_GLOBAL_HTTP_METHODS)
116+
// Import the method enum values to the global namespace
117+
using namespace AsyncWebRequestMethod;
106118
#endif
107119

108120
#ifndef HAVE_FS_FILE_OPEN_MODE
@@ -122,7 +134,6 @@ class FileOpenMode {
122134
#define RESPONSE_TRY_AGAIN 0xFFFFFFFF
123135
#define RESPONSE_STREAM_BUFFER_SIZE 1460
124136

125-
typedef uint16_t WebRequestMethodComposite;
126137
typedef std::function<void(void)> ArDisconnectHandler;
127138

128139
/*
@@ -372,10 +383,10 @@ class AsyncWebServerRequest {
372383
bool isExpectedRequestedConnType(RequestedConnectionType erct1, RequestedConnectionType erct2 = RCT_NOT_USED, RequestedConnectionType erct3 = RCT_NOT_USED)
373384
const;
374385
bool isWebSocketUpgrade() const {
375-
return _method == HTTP_GET && isExpectedRequestedConnType(RCT_WS);
386+
return _method == AsyncWebRequestMethod::HTTP_GET && isExpectedRequestedConnType(RCT_WS);
376387
}
377388
bool isSSE() const {
378-
return _method == HTTP_GET && isExpectedRequestedConnType(RCT_EVENT);
389+
return _method == AsyncWebRequestMethod::HTTP_GET && isExpectedRequestedConnType(RCT_EVENT);
379390
}
380391
bool isHTTP() const {
381392
return isExpectedRequestedConnType(RCT_DEFAULT, RCT_HTTP);
@@ -1545,7 +1556,7 @@ class AsyncWebServer : public AsyncMiddlewareChain {
15451556
bool removeHandler(AsyncWebHandler *handler);
15461557

15471558
AsyncCallbackWebHandler &on(AsyncURIMatcher uri, ArRequestHandlerFunction onRequest) {
1548-
return on(std::move(uri), HTTP_ANY, onRequest);
1559+
return on(std::move(uri), AsyncWebRequestMethod::HTTP_ANY, onRequest);
15491560
}
15501561
AsyncCallbackWebHandler &on(
15511562
AsyncURIMatcher uri, WebRequestMethodComposite method, ArRequestHandlerFunction onRequest, ArUploadHandlerFunction onUpload = nullptr,

src/Middleware.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ void AsyncCorsMiddleware::run(AsyncWebServerRequest *request, ArMiddlewareNext n
249249
// Origin header ? => CORS handling
250250
if (request->hasHeader(asyncsrv::T_CORS_O)) {
251251
// check if this is a preflight request => handle it and return
252-
if (request->method() == HTTP_OPTIONS) {
252+
if (request->method() == AsyncWebRequestMethod::HTTP_OPTIONS) {
253253
AsyncWebServerResponse *response = request->beginResponse(200);
254254
addCORSHeaders(request, response);
255255
request->send(response);

src/WebHandlerImpl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class AsyncCallbackWebHandler : public AsyncWebHandler {
6262
bool _isRegex;
6363

6464
public:
65-
AsyncCallbackWebHandler() : _uri(), _method(HTTP_ANY), _onRequest(NULL), _onUpload(NULL), _onBody(NULL), _isRegex(false) {}
65+
AsyncCallbackWebHandler() : _uri(), _method(AsyncWebRequestMethod::HTTP_ANY), _onRequest(NULL), _onUpload(NULL), _onBody(NULL), _isRegex(false) {}
6666
void setUri(AsyncURIMatcher uri);
6767
void setMethod(WebRequestMethodComposite method) {
6868
_method = method;

src/WebHandlers.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ AsyncStaticWebHandler &AsyncStaticWebHandler::setLastModified() {
103103
}
104104

105105
bool AsyncStaticWebHandler::canHandle(AsyncWebServerRequest *request) const {
106-
return request->isHTTP() && request->method() == HTTP_GET && request->url().startsWith(_uri) && _getFile(request);
106+
return request->isHTTP() && request->method() == AsyncWebRequestMethod::HTTP_GET && request->url().startsWith(_uri) && _getFile(request);
107107
}
108108

109109
bool AsyncStaticWebHandler::_getFile(AsyncWebServerRequest *request) const {

src/WebRequest.cpp

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@ enum {
3939
};
4040

4141
AsyncWebServerRequest::AsyncWebServerRequest(AsyncWebServer *s, AsyncClient *c)
42-
: _client(c), _server(s), _handler(NULL), _response(NULL), _onDisconnectfn(NULL), _temp(), _parseState(PARSE_REQ_START), _version(0), _method(HTTP_ANY),
43-
_url(), _host(), _contentType(), _boundary(), _authorization(), _reqconntype(RCT_HTTP), _authMethod(AsyncAuthType::AUTH_NONE), _isMultipart(false),
44-
_isPlainPost(false), _expectingContinue(false), _contentLength(0), _parsedLength(0), _multiParseState(0), _boundaryPosition(0), _itemStartIndex(0),
45-
_itemSize(0), _itemName(), _itemFilename(), _itemType(), _itemValue(), _itemBuffer(0), _itemBufferIndex(0), _itemIsFile(false), _chunkStartIndex(0),
46-
_chunkOffset(0), _chunkSize(0), _chunkedParseState(CHUNK_NONE), _chunkedLastChar(0), _tempObject(NULL) {
42+
: _client(c), _server(s), _handler(NULL), _response(NULL), _onDisconnectfn(NULL), _temp(), _parseState(PARSE_REQ_START), _version(0),
43+
_method(AsyncWebRequestMethod::HTTP_ANY), _url(), _host(), _contentType(), _boundary(), _authorization(), _reqconntype(RCT_HTTP),
44+
_authMethod(AsyncAuthType::AUTH_NONE), _isMultipart(false), _isPlainPost(false), _expectingContinue(false), _contentLength(0), _parsedLength(0),
45+
_multiParseState(0), _boundaryPosition(0), _itemStartIndex(0), _itemSize(0), _itemName(), _itemFilename(), _itemType(), _itemValue(), _itemBuffer(0),
46+
_itemBufferIndex(0), _itemIsFile(false), _chunkStartIndex(0), _chunkOffset(0), _chunkSize(0), _chunkedParseState(CHUNK_NONE), _chunkedLastChar(0),
47+
_tempObject(NULL) {
4748
c->onError(
4849
[](void *r, AsyncClient *c, int8_t error) {
4950
(void)c;
@@ -314,33 +315,33 @@ bool AsyncWebServerRequest::_parseReqHead() {
314315
_temp = _temp.substring(index + 1);
315316

316317
if (m == T_GET) {
317-
_method = HTTP_GET;
318+
_method = AsyncWebRequestMethod::HTTP_GET;
318319
} else if (m == T_POST) {
319-
_method = HTTP_POST;
320+
_method = AsyncWebRequestMethod::HTTP_POST;
320321
} else if (m == T_DELETE) {
321-
_method = HTTP_DELETE;
322+
_method = AsyncWebRequestMethod::HTTP_DELETE;
322323
} else if (m == T_PUT) {
323-
_method = HTTP_PUT;
324+
_method = AsyncWebRequestMethod::HTTP_PUT;
324325
} else if (m == T_PATCH) {
325-
_method = HTTP_PATCH;
326+
_method = AsyncWebRequestMethod::HTTP_PATCH;
326327
} else if (m == T_HEAD) {
327-
_method = HTTP_HEAD;
328+
_method = AsyncWebRequestMethod::HTTP_HEAD;
328329
} else if (m == T_OPTIONS) {
329-
_method = HTTP_OPTIONS;
330+
_method = AsyncWebRequestMethod::HTTP_OPTIONS;
330331
} else if (m == T_PROPFIND) {
331-
_method = HTTP_PROPFIND;
332+
_method = AsyncWebRequestMethod::HTTP_PROPFIND;
332333
} else if (m == T_LOCK) {
333-
_method = HTTP_LOCK;
334+
_method = AsyncWebRequestMethod::HTTP_LOCK;
334335
} else if (m == T_UNLOCK) {
335-
_method = HTTP_UNLOCK;
336+
_method = AsyncWebRequestMethod::HTTP_UNLOCK;
336337
} else if (m == T_PROPPATCH) {
337-
_method = HTTP_PROPPATCH;
338+
_method = AsyncWebRequestMethod::HTTP_PROPPATCH;
338339
} else if (m == T_MKCOL) {
339-
_method = HTTP_MKCOL;
340+
_method = AsyncWebRequestMethod::HTTP_MKCOL;
340341
} else if (m == T_MOVE) {
341-
_method = HTTP_MOVE;
342+
_method = AsyncWebRequestMethod::HTTP_MOVE;
342343
} else if (m == T_COPY) {
343-
_method = HTTP_COPY;
344+
_method = AsyncWebRequestMethod::HTTP_COPY;
344345
} else {
345346
return false;
346347
}
@@ -1311,49 +1312,49 @@ String AsyncWebServerRequest::urlDecode(const String &text) const {
13111312
}
13121313

13131314
const char *AsyncWebServerRequest::methodToString() const {
1314-
if (_method == HTTP_ANY) {
1315+
if (_method == AsyncWebRequestMethod::HTTP_ANY) {
13151316
return T_ANY;
13161317
}
1317-
if (_method & HTTP_GET) {
1318+
if (_method & AsyncWebRequestMethod::HTTP_GET) {
13181319
return T_GET;
13191320
}
1320-
if (_method & HTTP_POST) {
1321+
if (_method & AsyncWebRequestMethod::HTTP_POST) {
13211322
return T_POST;
13221323
}
1323-
if (_method & HTTP_DELETE) {
1324+
if (_method & AsyncWebRequestMethod::HTTP_DELETE) {
13241325
return T_DELETE;
13251326
}
1326-
if (_method & HTTP_PUT) {
1327+
if (_method & AsyncWebRequestMethod::HTTP_PUT) {
13271328
return T_PUT;
13281329
}
1329-
if (_method & HTTP_PATCH) {
1330+
if (_method & AsyncWebRequestMethod::HTTP_PATCH) {
13301331
return T_PATCH;
13311332
}
1332-
if (_method & HTTP_HEAD) {
1333+
if (_method & AsyncWebRequestMethod::HTTP_HEAD) {
13331334
return T_HEAD;
13341335
}
1335-
if (_method & HTTP_OPTIONS) {
1336+
if (_method & AsyncWebRequestMethod::HTTP_OPTIONS) {
13361337
return T_OPTIONS;
13371338
}
1338-
if (_method & HTTP_PROPFIND) {
1339+
if (_method & AsyncWebRequestMethod::HTTP_PROPFIND) {
13391340
return T_PROPFIND;
13401341
}
1341-
if (_method & HTTP_LOCK) {
1342+
if (_method & AsyncWebRequestMethod::HTTP_LOCK) {
13421343
return T_LOCK;
13431344
}
1344-
if (_method & HTTP_UNLOCK) {
1345+
if (_method & AsyncWebRequestMethod::HTTP_UNLOCK) {
13451346
return T_UNLOCK;
13461347
}
1347-
if (_method & HTTP_PROPPATCH) {
1348+
if (_method & AsyncWebRequestMethod::HTTP_PROPPATCH) {
13481349
return T_PROPPATCH;
13491350
}
1350-
if (_method & HTTP_MKCOL) {
1351+
if (_method & AsyncWebRequestMethod::HTTP_MKCOL) {
13511352
return T_MKCOL;
13521353
}
1353-
if (_method & HTTP_MOVE) {
1354+
if (_method & AsyncWebRequestMethod::HTTP_MOVE) {
13541355
return T_MOVE;
13551356
}
1356-
if (_method & HTTP_COPY) {
1357+
if (_method & AsyncWebRequestMethod::HTTP_COPY) {
13571358
return T_COPY;
13581359
}
13591360
return T_UNKNOWN;

0 commit comments

Comments
 (0)