diff --git a/docs/requests.md b/docs/requests.md index b58016ce5..9bc250218 100644 --- a/docs/requests.md +++ b/docs/requests.md @@ -6,7 +6,7 @@ ```cpp request->version(); // uint8_t: 0 = HTTP/1.0, 1 = HTTP/1.1 -request->method(); // enum: HTTP_GET, HTTP_POST, HTTP_DELETE, HTTP_PUT, HTTP_PATCH, HTTP_HEAD, HTTP_OPTIONS +request->method(); // enum: HTTP_GET, HTTP_POST, HTTP_DELETE, HTTP_PUT, HTTP_PATCH, HTTP_HEAD, HTTP_OPTIONS, HTTP_QUERY request->url(); // String: URL of the request (not including host, port or GET parameters) request->host(); // String: The requested host (can be used for virtual hosting) request->contentType(); // String: ContentType of the request (not available in Handler::canHandle) diff --git a/examples/arduino/HTTPMethodsWithArduino/HTTPMethodsWithArduino.ino b/examples/arduino/HTTPMethodsWithArduino/HTTPMethodsWithArduino.ino index 209417087..053304303 100644 --- a/examples/arduino/HTTPMethodsWithArduino/HTTPMethodsWithArduino.ino +++ b/examples/arduino/HTTPMethodsWithArduino/HTTPMethodsWithArduino.ino @@ -87,6 +87,12 @@ void setup() { request->send(200, "text/plain", "Hello"); }); + // HTTP_Method.h / http_parser.h is included above: use the namespaced bit, not HTTP_QUERY. + // curl -v -X QUERY -H 'Content-Type: application/json' -d '{"q":1}' http://192.168.4.1/query + server.on("/query", AsyncWebRequestMethod::HTTP_QUERY, [](AsyncWebServerRequest *request) { + request->send(200, "text/plain", request->methodToString()); + }); + #if ASYNC_JSON_SUPPORT == 1 // curl -v http://192.168.4.1/test => Not Implemented // curl -v -X POST -H 'Content-Type: application/json' -d '{"name":"You"}' http://192.168.4.1/test => OK @@ -116,6 +122,10 @@ void setup() { assert(composite3 == composite4); assert(composite1 != composite3); assert(composite5 == AsyncWebRequestMethod::HTTP_GET); + + WebRequestMethodComposite queryMethod = AsyncWebRequestMethod::HTTP_QUERY; + assert(queryMethod.matches(AsyncWebRequestMethod::HTTP_QUERY)); + assert(!queryMethod.matches(AsyncWebRequestMethod::HTTP_GET)); } // not needed diff --git a/examples/arduino/HTTPMethodsWithESPIDF/HTTPMethodsWithESPIDF.ino b/examples/arduino/HTTPMethodsWithESPIDF/HTTPMethodsWithESPIDF.ino index 7112a7a9a..0fdfc2cc6 100644 --- a/examples/arduino/HTTPMethodsWithESPIDF/HTTPMethodsWithESPIDF.ino +++ b/examples/arduino/HTTPMethodsWithESPIDF/HTTPMethodsWithESPIDF.ino @@ -90,6 +90,12 @@ void setup() { request->send(200, "text/plain", "Hello"); }); + // http_parser.h is included above: use the namespaced bit, not HTTP_QUERY (platform enum 33). + // curl -v -X QUERY -H 'Content-Type: application/json' -d '{"q":1}' http://192.168.4.1/query + server.on("/query", AsyncWebRequestMethod::HTTP_QUERY, [](AsyncWebServerRequest *request) { + request->send(200, "text/plain", request->methodToString()); + }); + #if ASYNC_JSON_SUPPORT == 1 // curl -v http://192.168.4.1/test => Not Implemented // curl -v -X POST -H 'Content-Type: application/json' -d '{"name":"You"}' http://192.168.4.1/test => OK @@ -119,6 +125,10 @@ void setup() { assert(composite3 == composite4); assert(composite1 != composite3); assert(composite5 == AsyncWebRequestMethod::HTTP_GET); + + WebRequestMethodComposite queryMethod = AsyncWebRequestMethod::HTTP_QUERY; + assert(queryMethod.matches(AsyncWebRequestMethod::HTTP_QUERY)); + assert(!queryMethod.matches(AsyncWebRequestMethod::HTTP_GET)); } // not needed diff --git a/examples/arduino/Json/Json.ino b/examples/arduino/Json/Json.ino index eeaab75aa..87808c9de 100644 --- a/examples/arduino/Json/Json.ino +++ b/examples/arduino/Json/Json.ino @@ -87,6 +87,20 @@ void setup() { server.addHandler(handler); + // curl -v -X QUERY -H 'Content-Type: application/json' -d '{"q":1}' http://192.168.4.1/json-query + AsyncCallbackJsonWebHandler *queryHandler = new AsyncCallbackJsonWebHandler("/json-query"); + queryHandler->setMethod(HTTP_QUERY); + queryHandler->onRequest([](AsyncWebServerRequest *request, JsonVariant &json) { + serializeJson(json, Serial); + Serial.println(); + AsyncJsonResponse *response = new AsyncJsonResponse(); + JsonObject root = response->getRoot().to(); + root["hello"] = json.as()["q"]; + response->setLength(); + request->send(response); + }); + server.addHandler(queryHandler); + // New Json API since 3.8.2, which works for both Json and MessagePack bodies // curl -v -X POST -H 'Content-Type: application/json' -d '{"name":"You"}' http://192.168.4.1/json3 diff --git a/src/ESPAsyncWebServer.h b/src/ESPAsyncWebServer.h index 2358e89db..6a17a049a 100644 --- a/src/ESPAsyncWebServer.h +++ b/src/ESPAsyncWebServer.h @@ -151,6 +151,9 @@ enum AsyncWebRequestMethodType : uint32_t { HTTP_LINK = 1u << 22, HTTP_UNLINK = 1u << 23, + /* RFC 10008 */ + HTTP_QUERY = 1u << 24, + /* icecast */ // HTTP_SOURCE diff --git a/src/WebRequest.cpp b/src/WebRequest.cpp index 6822f8750..d32cd8e8b 100644 --- a/src/WebRequest.cpp +++ b/src/WebRequest.cpp @@ -1544,6 +1544,8 @@ WebRequestMethod stringToMethod(const String &m) { return AsyncWebRequestMethod::HTTP_UNBIND; } else if (m == T_ACL) { return AsyncWebRequestMethod::HTTP_ACL; + } else if (m == T_QUERY) { + return AsyncWebRequestMethod::HTTP_QUERY; } else { return AsyncWebRequestMethod::HTTP_INVALID; } @@ -1579,6 +1581,8 @@ const char *methodToString(WebRequestMethod method) { /* RFC-2068, section 19.6.1.2 */ case AsyncWebRequestMethod::HTTP_LINK: return T_LINK; case AsyncWebRequestMethod::HTTP_UNLINK: return T_UNLINK; + /* RFC 10008 */ + case AsyncWebRequestMethod::HTTP_QUERY: return T_QUERY; // Unsupported default: return T_UNKNOWN; } diff --git a/src/literals.h b/src/literals.h index 5d12fcf26..85d04dfd6 100644 --- a/src/literals.h +++ b/src/literals.h @@ -36,6 +36,7 @@ static constexpr const char T__opaque[] = "\", opaque=\""; static constexpr const char T_100_CONTINUE[] = "100-continue"; static constexpr const char T_13[] = "13"; static constexpr const char T_ACCEPT[] = "Accept"; +static constexpr const char T_Accept_Query[] = "Accept-Query"; static constexpr const char T_Accept_Ranges[] = "Accept-Ranges"; static constexpr const char T_attachment[] = "attachment; filename=\""; static constexpr const char T_AUTH[] = "Authorization"; @@ -137,6 +138,7 @@ static constexpr const char T_ACL[] = "ACL"; static constexpr const char T_PURGE[] = "PURGE"; static constexpr const char T_LINK[] = "LINK"; static constexpr const char T_UNLINK[] = "UNLINK"; +static constexpr const char T_QUERY[] = "QUERY"; // Req content types static constexpr const char T_RCT_HTTP[] = "RCT_HTTP";