Skip to content

Commit 7eefa6b

Browse files
refactor: drop HTTP_ALL, use HTTP_ANY (255) from HTTP_Method.h as the match-any sentinel
- Include <HTTP_Method.h> first (defines HTTPMethod and HTTP_ANY=(HTTPMethod)(255)); fall back to <http_parser.h> then inline fallback, both of which also define HTTP_ANY ((http_method)(255)) matching Arduino's value - Remove #define HTTP_ALL entirely - users use HTTP_ANY from the platform - WebRequestMethodComposite(WebRequestMethod m): treat HTTP_ANY (value 255) as "match any" sentinel by creating an empty composite (same as default ctor) - Update on() default and all docs from HTTP_ALL -> HTTP_ANY Co-authored-by: mathieucarbou <61346+mathieucarbou@users.noreply.github.com>
1 parent e0a7b64 commit 7eefa6b

3 files changed

Lines changed: 45 additions & 39 deletions

File tree

docs/backup/wiki.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1887,7 +1887,7 @@ void setup(){
18871887
}, onUpload);
18881888

18891889
// send a file when /index is requested
1890-
server.on("/index", HTTP_ALL, [](AsyncWebServerRequest *request){
1890+
server.on("/index", HTTP_ANY, [](AsyncWebServerRequest *request){
18911891
request->send(SPIFFS, "/index.htm");
18921892
});
18931893

@@ -1974,10 +1974,10 @@ public :
19741974
19751975
void begin(){
19761976
// attach global request handler
1977-
classWebServer.on("/example", HTTP_ALL, handleRequest);
1977+
classWebServer.on("/example", HTTP_ANY, handleRequest);
19781978
19791979
// attach class request handler
1980-
classWebServer.on("/example", HTTP_ALL, std::bind(&WebClass::classRequest, this, std::placeholders::_1));
1980+
classWebServer.on("/example", HTTP_ANY, std::bind(&WebClass::classRequest, this, std::placeholders::_1));
19811981
}
19821982
};
19831983
@@ -1986,10 +1986,10 @@ WebClass webClassInstance;
19861986
19871987
void setup() {
19881988
// attach global request handler
1989-
globalWebServer.on("/example", HTTP_ALL, handleRequest);
1989+
globalWebServer.on("/example", HTTP_ANY, handleRequest);
19901990
19911991
// attach class request handler
1992-
globalWebServer.on("/example", HTTP_ALL, std::bind(&WebClass::classRequest, webClassInstance, std::placeholders::_1));
1992+
globalWebServer.on("/example", HTTP_ANY, std::bind(&WebClass::classRequest, webClassInstance, std::placeholders::_1));
19931993
}
19941994
19951995
void loop() {

docs/setup.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,12 @@ void setup(){
7171
}, onUpload);
7272

7373
// send a file when /index is requested (SPIFFS example)
74-
server.on("/index", HTTP_ALL, [](AsyncWebServerRequest *request){
74+
server.on("/index", HTTP_ANY, [](AsyncWebServerRequest *request){
7575
request->send(SPIFFS, "/index.htm");
7676
});
7777

7878
// send a file when /index is requested (LittleFS example)
79-
server.on("/index", HTTP_ALL, [](AsyncWebServerRequest *request){
79+
server.on("/index", HTTP_ANY, [](AsyncWebServerRequest *request){
8080
request->send(LittleFS, "/index.htm");
8181
});
8282

@@ -161,10 +161,10 @@ public :
161161
162162
void begin(){
163163
// attach global request handler
164-
classWebServer.on("/example", HTTP_ALL, handleRequest);
164+
classWebServer.on("/example", HTTP_ANY, handleRequest);
165165
166166
// attach class request handler
167-
classWebServer.on("/example", HTTP_ALL, std::bind(&WebClass::classRequest, this, std::placeholders::_1));
167+
classWebServer.on("/example", HTTP_ANY, std::bind(&WebClass::classRequest, this, std::placeholders::_1));
168168
}
169169
};
170170
@@ -173,10 +173,10 @@ WebClass webClassInstance;
173173
174174
void setup() {
175175
// attach global request handler
176-
globalWebServer.on("/example", HTTP_ALL, handleRequest);
176+
globalWebServer.on("/example", HTTP_ANY, handleRequest);
177177
178178
// attach class request handler
179-
globalWebServer.on("/example", HTTP_ALL, std::bind(&WebClass::classRequest, webClassInstance, std::placeholders::_1));
179+
globalWebServer.on("/example", HTTP_ANY, std::bind(&WebClass::classRequest, webClassInstance, std::placeholders::_1));
180180
}
181181
182182
void loop() {

src/ESPAsyncWebServer.h

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,25 @@
4545
#error Platform not supported
4646
#endif
4747

48-
// HTTP method enum from the underlying http_parser library.
49-
// All supported TCP libraries (AsyncTCP, ESPAsyncTCP, RPAsyncTCP) are built on
50-
// http_parser, so http_method is available on all supported platforms.
51-
// Using the platform's enum avoids redefining HTTP_GET, HTTP_POST, etc. ourselves,
52-
// which eliminates name conflicts with other libraries (e.g., Arduino WebServer.h).
53-
#if __has_include(<http_parser.h>)
48+
// HTTP method types from the platform's HTTP parser library.
49+
// Arduino ESP32 core provides HTTP_Method.h which typedef's http_method as
50+
// HTTPMethod and defines HTTP_ANY = (HTTPMethod)(255) as the "match any" sentinel.
51+
// Fall back to the raw http_parser.h (always available via the TCP library) or,
52+
// as a last resort, to a fully inline fallback definition.
53+
#if __has_include(<HTTP_Method.h>)
54+
# include <HTTP_Method.h>
55+
// HTTP_Method.h provides: typedef enum http_method HTTPMethod;
56+
// and: #define HTTP_ANY (HTTPMethod)(255)
57+
#elif __has_include(<http_parser.h>)
5458
# include <http_parser.h>
59+
// http_parser.h provides enum http_method but not the HTTP_ANY sentinel.
60+
// Define the sentinel here, matching the value used by Arduino's HTTP_Method.h.
61+
# ifndef HTTP_ANY
62+
# define HTTP_ANY ((http_method)(255))
63+
# endif
5564
#else
56-
// Fallback definition matching the llhttp/http_parser spec (used by all platforms).
57-
// This is provided only for toolchains that do not expose http_parser.h directly.
65+
// Full fallback for toolchains that expose neither header.
66+
// Enum values match the llhttp/http_parser spec used by all supported platforms.
5867
typedef enum {
5968
HTTP_DELETE = 0, HTTP_GET = 1, HTTP_HEAD = 2, HTTP_POST = 3, HTTP_PUT = 4,
6069
HTTP_CONNECT = 5, HTTP_OPTIONS = 6, HTTP_TRACE = 7, HTTP_COPY = 8, HTTP_LOCK = 9,
@@ -65,6 +74,7 @@ typedef enum {
6574
HTTP_SUBSCRIBE = 26, HTTP_UNSUBSCRIBE = 27, HTTP_PATCH = 28, HTTP_PURGE = 29,
6675
HTTP_MKCALENDAR = 30, HTTP_LINK = 31, HTTP_UNLINK = 32,
6776
} http_method;
77+
# define HTTP_ANY ((http_method)(255))
6878
#endif
6979

7080
#include "AsyncWebServerVersion.h"
@@ -104,28 +114,35 @@ class AsyncMiddlewareChain;
104114
// platform's http_parser enum. HTTP_GET, HTTP_POST, HTTP_DELETE, HTTP_PUT,
105115
// HTTP_PATCH, HTTP_HEAD, HTTP_OPTIONS, HTTP_PROPFIND, HTTP_LOCK, HTTP_UNLOCK,
106116
// HTTP_PROPPATCH, HTTP_MKCOL, HTTP_MOVE, HTTP_COPY and many others are already
107-
// defined globally by <http_parser.h> above — no redefinition needed here.
117+
// defined globally by the platform headers above — no redefinition needed here.
118+
// HTTP_ANY (= 255) is the "match any method" sentinel, also from the platform.
108119
typedef http_method WebRequestMethod;
109120

110121
// WebRequestMethodComposite: an ordered list of HTTP methods that a handler
111-
// accepts. An empty composite matches *any* method (HTTP_ANY / HTTP_ALL
112-
// semantics). Using a list avoids bitmask arithmetic on the platform enum's
122+
// accepts. An empty composite (or one that contains HTTP_ANY) matches *any*
123+
// method. Using a list avoids bitmask arithmetic on the platform enum's
113124
// non-power-of-two sequential values.
114125
class WebRequestMethodComposite {
115126
public:
116127
// Maximum number of methods that can be listed. In practice, handlers
117-
// accept at most a handful of methods; use the empty composite (HTTP_ALL)
118-
// when you want to match any method.
128+
// accept at most a handful of methods; use HTTP_ANY when you want to
129+
// match any method.
119130
static const uint8_t MAX_METHODS = 8;
120131

121132
// Empty composite = match any method.
122133
WebRequestMethodComposite() : _count(0) {}
123134

124135
// Single-method composite. Intentionally implicit so that a bare
125136
// WebRequestMethod (e.g. HTTP_GET) is accepted wherever a composite is
126-
// expected.
127-
WebRequestMethodComposite(WebRequestMethod m) : _count(1) {
128-
_methods[0] = m;
137+
// expected. HTTP_ANY (value 255) is treated as the "match any" sentinel:
138+
// it creates an empty composite rather than a single-method one.
139+
WebRequestMethodComposite(WebRequestMethod m) {
140+
if (m == static_cast<WebRequestMethod>(HTTP_ANY)) {
141+
_count = 0; // HTTP_ANY: match any method
142+
} else {
143+
_count = 1;
144+
_methods[0] = m;
145+
}
129146
}
130147

131148
// Append a method; silently ignores the call once MAX_METHODS is reached.
@@ -179,17 +196,6 @@ inline bool operator^(const WebRequestMethodComposite &c, WebRequestMethod m) {
179196
return c.contains(m);
180197
}
181198

182-
// HTTP_ALL: empty composite — matches any HTTP method.
183-
// HTTP_ANY: backward-compatible alias for HTTP_ALL.
184-
// HTTP_ANY is only defined here when external headers have not already defined
185-
// it as a macro (e.g., Arduino core may define HTTP_ANY as an integer). When
186-
// an external macro is present the name remains an integer; user code should
187-
// migrate to HTTP_ALL which is always safe.
188-
#define HTTP_ALL WebRequestMethodComposite()
189-
#ifndef HTTP_ANY
190-
#define HTTP_ANY WebRequestMethodComposite()
191-
#endif
192-
193199
#ifndef HAVE_FS_FILE_OPEN_MODE
194200
namespace fs {
195201
class FileOpenMode {
@@ -1629,7 +1635,7 @@ class AsyncWebServer : public AsyncMiddlewareChain {
16291635
bool removeHandler(AsyncWebHandler *handler);
16301636

16311637
AsyncCallbackWebHandler &on(AsyncURIMatcher uri, ArRequestHandlerFunction onRequest) {
1632-
return on(std::move(uri), HTTP_ALL, onRequest);
1638+
return on(std::move(uri), HTTP_ANY, onRequest);
16331639
}
16341640
AsyncCallbackWebHandler &on(
16351641
AsyncURIMatcher uri, WebRequestMethodComposite method, ArRequestHandlerFunction onRequest, ArUploadHandlerFunction onUpload = nullptr,

0 commit comments

Comments
 (0)