Skip to content

Commit 2ff30c8

Browse files
committed
Improving example to make sure the old way to match with a bit operator (&) works.
1 parent c114ae7 commit 2ff30c8

1 file changed

Lines changed: 28 additions & 3 deletions

File tree

examples/HTTPMethods/HTTPMethods.ino

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,29 @@
2828
static AsyncWebServer server(80);
2929

3030
// 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; };
31+
int stringToMethod(const String &) {
32+
return 0;
33+
}
34+
const char *methodToString(WebRequestMethod) {
35+
return "METHOD";
36+
}
37+
bool methodMatches(WebRequestMethodComposite c, WebRequestMethod m) {
38+
return false;
39+
};
40+
41+
static WebRequestMethodComposite allowed = AsyncWebRequestMethod::HTTP_HEAD | AsyncWebRequestMethod::HTTP_OPTIONS;
42+
43+
class MyRequestHandler : public AsyncWebHandler {
44+
public:
45+
bool canHandle(__unused AsyncWebServerRequest *request) const override {
46+
// Test backward compatibility with previous way of checking if a method is allowed in a composite using a bit operator
47+
return allowed & request->method();
48+
}
49+
50+
void handleRequest(AsyncWebServerRequest *request) override {
51+
request->send(200, "text/plain", "Hello from custom handler");
52+
}
53+
};
3454

3555
void setup() {
3656
Serial.begin(115200);
@@ -51,6 +71,11 @@ void setup() {
5171
request->send(200, "text/plain", "Hello");
5272
});
5373

74+
// curl -v -X HEAD http://192.168.4.1/custom => answers
75+
// curl -v -X OPTIONS http://192.168.4.1/custom => answers
76+
// curl -v -X POST http://192.168.4.1/custom => no answer
77+
server.addHandler(new MyRequestHandler());
78+
5479
server.begin();
5580
}
5681

0 commit comments

Comments
 (0)