api: rework plain and JSON implementations (#2405)
- match paths through a custom AsyncWebHandler instead of using generic not-found fallback handler
- allow MQTT-like patterns when registering paths (`simple/path`, `path/+/something`, `path/#`)
Replaces `relay/0`, `relay/1` etc. with `relay/+`. Magnitudes are plain paths, but using `/+` in case there's more than 1 magnitude of the same type.
- restore `std::function` as callback container (no more single-byte arg nonsense). Still, limit to 1 type per handler type
- adds JSON handlers which will receive JsonObject root as both input and output. Same logic as plain - GET returns resource data, PUT updates it.
- breaking change to `apiAuthenticate(request)`, it no longer will do `request->send(403)` and expect this to be handled externally.
- allow `Api-Key` header containing the key, works for both GET & PUT plain requests. The only way to set apikey for JSON.
- add `ApiRequest::param` to retrieve both GET and PUT params (aka args), remove ApiBuffer
- remove `API_BUFFER_SIZE`. Allow custom form-data key=value pairs for requests, allow to send basic `String`.
- add `API_JSON_BUFFER_SIZE` for the JSON buffer (both input and output)
- `/apis` replaced with `/api/list`, no longer uses custom handler and is an `apiRegister` callback
- `/api/rpc` custom handler replaced with an `apiRegister` callback
WIP further down:
- no more `webLog` for API requests, unless `webAccessLog` / `WEB_ACCESS_LOG` is set to `1`. This also needs to happen to the other handlers.
- migrate to ArduinoJson v6, since it become apparent it is actually a good upgrade :)
- actually make use of JSON endpoints more, right now it's just existing GET for sensors and relays
- fork ESPAsyncWebServer to cleanup path parsing and temporary objects attached to the request (also, fix things a lot of things based on PRs there...) 4 years ago |
|
- /*
-
- Part of the WEBSERVER module
-
- Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
- Copyright (C) 2020 by Maxim Prokhorov <prokhorov dot max at outlook dot com>
-
- */
-
- #include "ota.h"
-
- #if WEB_SUPPORT && OTA_WEB_SUPPORT
-
- #include "web.h"
- #include "ws.h"
-
- void _onUpgradeResponse(AsyncWebServerRequest *request, int code, const String& payload = "") {
-
- auto *response = request->beginResponseStream("text/plain", 256);
- response->addHeader("Connection", "close");
- response->addHeader("X-XSS-Protection", "1; mode=block");
- response->addHeader("X-Content-Type-Options", "nosniff");
- response->addHeader("X-Frame-Options", "deny");
-
- response->setCode(code);
-
- if (payload.length()) {
- response->printf("%s", payload.c_str());
- } else {
- if (!Update.hasError()) {
- response->print("OK");
- } else {
- #if defined(ARDUINO_ESP8266_RELEASE_2_3_0)
- Update.printError(reinterpret_cast<Stream&>(response));
- #else
- Update.printError(*response);
- #endif
- }
- }
-
- request->send(response);
-
- }
-
- void _onUpgradeStatusSet(AsyncWebServerRequest *request, int code, const String& payload = "") {
- _onUpgradeResponse(request, code, payload);
- request->_tempObject = malloc(sizeof(bool));
- }
-
- void _onUpgrade(AsyncWebServerRequest *request) {
-
- webLog(request);
- if (!webAuthenticate(request)) {
- return request->requestAuthentication(getSetting("hostname").c_str());
- }
-
- if (request->_tempObject) {
- return;
- }
-
- _onUpgradeResponse(request, 200);
-
- }
-
- void _onUpgradeFile(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) {
-
- if (!webAuthenticate(request)) {
- return request->requestAuthentication(getSetting("hostname").c_str());
- }
-
- // We set this after we are done with the request
- // It is still possible to re-enter this callback even after connection is already closed
- // 1.15.0: TODO: see https://github.com/me-no-dev/ESPAsyncWebServer/pull/660
- // remote close or request sending some data before finishing parsing of the body will leak 1460 bytes
- // waiting a bit for upstream. looks more and more we need to fork the server
- if (request->_tempObject) {
- return;
- }
-
- if (!index) {
-
- // TODO: stop network activity completely when handling Update through ArduinoOTA or `ota` command?
- if (Update.isRunning()) {
- _onUpgradeStatusSet(request, 400, F("ERROR: Upgrade in progress"));
- return;
- }
-
- // Check that header is correct and there is more data before anything is written to the flash
- if (final || !len) {
- _onUpgradeStatusSet(request, 400, F("ERROR: Invalid request"));
- return;
- }
-
- if (!otaVerifyHeader(data, len)) {
- _onUpgradeStatusSet(request, 400, F("ERROR: No magic byte / invalid flash config"));
- return;
- }
-
- // Disabling EEPROM rotation to prevent writing to EEPROM after the upgrade
- eepromRotate(false);
-
- DEBUG_MSG_P(PSTR("[UPGRADE] Start: %s\n"), filename.c_str());
- Update.runAsync(true);
-
- // Note: cannot use request->contentLength() for multipart/form-data
- if (!Update.begin((ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000)) {
- _onUpgradeStatusSet(request, 500);
- eepromRotate(true);
- return;
- }
-
- }
-
- if (request->_tempObject) {
- return;
- }
-
- // Any error will cancel the update, but request may still be alive
- if (!Update.isRunning()) {
- return;
- }
-
- if (Update.write(data, len) != len) {
- _onUpgradeStatusSet(request, 500);
- Update.end();
- eepromRotate(true);
- return;
- }
-
- if (final) {
- otaFinalize(index + len, CUSTOM_RESET_UPGRADE, true);
- } else {
- otaProgress(index + len);
- }
-
- }
-
- void otaWebSetup() {
- webServer().on("/upgrade", HTTP_POST, _onUpgrade, _onUpgradeFile);
- wsRegister().
- onVisible([](JsonObject& root) {
- root["otaVisible"] = 1;
- });
- }
-
- #endif // OTA_WEB_SUPPORT
-
|