Skip to content

Commit c114ae7

Browse files
committed
Move global utility methods in asyncsrv namespace to avoid conflict with existing usr code
1 parent 78dc30f commit c114ae7

5 files changed

Lines changed: 15 additions & 4 deletions

File tree

examples/HTTPMethods/HTTPMethods.ino

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727

2828
static AsyncWebServer server(80);
2929

30+
// user defined functions that turns out to have similar signatures to the ones in global header
31+
int stringToMethod(const String &) { return 0; }
32+
const char *methodToString(WebRequestMethod) { return "METHOD"; }
33+
bool methodMatches(WebRequestMethodComposite c, WebRequestMethod m) { return false; };
34+
3035
void setup() {
3136
Serial.begin(115200);
3237

src/AsyncJson.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ AsyncCallbackJsonWebHandler::AsyncCallbackJsonWebHandler(AsyncURIMatcher uri, Ar
124124
#endif
125125

126126
bool AsyncCallbackJsonWebHandler::canHandle(AsyncWebServerRequest *request) const {
127-
if (!_onRequest || !request->isHTTP() || !methodMatches(_method, request->method())) {
127+
if (!_onRequest || !request->isHTTP() || !asyncsrv::methodMatches(_method, request->method())) {
128128
return false;
129129
}
130130

src/ESPAsyncWebServer.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,17 @@ constexpr inline WebRequestMethodComposite operator|(WebRequestMethodComposite l
172172
constexpr inline WebRequestMethodComposite operator|(WebRequestMethod l, WebRequestMethod r) {
173173
return static_cast<WebRequestMethodComposite>(l) | r;
174174
};
175+
176+
namespace asyncsrv {
177+
// utility method to check if a WebRequestMethod matches a WebRequestMethodComposite
175178
constexpr inline bool methodMatches(WebRequestMethodComposite c, WebRequestMethod m) {
176179
return c & static_cast<WebRequestMethodComposite>(m);
177180
};
178181

179182
// WebRequestMethod string conversion functions
180183
WebRequestMethod stringToMethod(const String &);
181184
const char *methodToString(WebRequestMethod);
185+
} // namespace asyncsrv
182186

183187
#if !defined(ASYNCWEBSERVER_NO_GLOBAL_HTTP_METHODS)
184188
// Import the method enum values to the global namespace
@@ -443,7 +447,7 @@ class AsyncWebServerRequest {
443447
}
444448

445449
inline const char *methodToString() const {
446-
return ::methodToString(_method);
450+
return asyncsrv::methodToString(_method);
447451
};
448452
const char *requestedConnTypeToString() const;
449453

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() || !methodMatches(_method, request->method())) {
303+
if (!_onRequest || !request->isHTTP() || !asyncsrv::methodMatches(_method, request->method())) {
304304
return false;
305305
}
306306
return _uri.matches(request);

src/WebRequest.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ bool AsyncWebServerRequest::_parseReqHead() {
314314
String u = _temp.substring(m.length() + 1, index);
315315
_temp = _temp.substring(index + 1);
316316

317-
_method = stringToMethod(m);
317+
_method = asyncsrv::stringToMethod(m);
318318
if (_method == HTTP_INVALID) {
319319
return false;
320320
}
@@ -1306,6 +1306,7 @@ AsyncClient *AsyncWebServerRequest::clientRelease() {
13061306
return c;
13071307
}
13081308

1309+
namespace asyncsrv {
13091310
// WebRequestMethod conversions
13101311
WebRequestMethod stringToMethod(const String &m) {
13111312
if (m == T_GET) {
@@ -1395,3 +1396,4 @@ const char *methodToString(WebRequestMethod method) {
13951396
default: return T_UNKNOWN;
13961397
}
13971398
}
1399+
} // namespace asyncsrv

0 commit comments

Comments
 (0)