handleHttpApi: Move web response to web context

No need to even consider this for non-web requests.  Move the request
special case to that context as well.
This commit is contained in:
Will Miles 2024-11-10 18:36:56 -05:00
parent e65f6c7bc7
commit cab2f91bc6
9 changed files with 18 additions and 20 deletions

View File

@ -235,7 +235,7 @@ void handleRemote(uint8_t *data, size_t len);
//set.cpp
bool isAsterisksOnly(const char* str, byte maxLen);
void handleSettingsSet(AsyncWebServerRequest *request, byte subPage);
bool handleHttpApi(AsyncWebServerRequest *request, const String& req, bool apply=true);
bool handleHttpApi(const String& req, bool apply=true);
//udp.cpp
void notify(byte callMode, bool followUp=false);

View File

@ -606,7 +606,7 @@ static void decodeIRJson(uint32_t code)
cmdStr += tmp;
}
fdo.clear(); // clear JSON buffer (it is no longer needed)
handleHttpApi(nullptr, cmdStr, false); // no stateUpdated() call here
handleHttpApi(cmdStr, false); // no stateUpdated() call here
}
} else {
// command is JSON object (TODO: currently will not handle irApplyToAllSelected correctly)

View File

@ -449,7 +449,7 @@ bool deserializeState(JsonObject root, byte callMode, byte presetId)
// HTTP API commands (must be handled before "ps")
const char* httpwin = root["win"];
if (httpwin) {
handleHttpApi(nullptr, httpwin, false); // may set stateChanged
handleHttpApi(httpwin, false); // may set stateChanged
}
// Applying preset from JSON API has 2 cases: a) "pd" AKA "preset direct" and b) "ps" AKA "preset select"

View File

@ -107,7 +107,7 @@ static void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProp
deserializeJson(*pDoc, payloadStr);
deserializeState(pDoc->as<JsonObject>());
} else { //HTTP API
handleHttpApi(nullptr, payloadStr);
handleHttpApi(payloadStr);
}
releaseJSONBufferLock();
}

View File

@ -180,7 +180,7 @@ void handlePresets()
//HTTP API commands
const char* httpwin = fdo["win"];
if (httpwin) {
handleHttpApi(nullptr, httpwin, false); // may call applyPreset() via PL=
handleHttpApi(httpwin, false); // may call applyPreset() via PL=
setValuesFromFirstSelectedSeg(); // fills legacy values
changePreset = true;
} else {

View File

@ -160,7 +160,7 @@ static bool remoteJson(int button)
cmdStr += tmp;
}
fdo.clear(); // clear JSON buffer (it is no longer needed)
handleHttpApi(nullptr, cmdStr, false); // no stateUpdated() call here
handleHttpApi(cmdStr, false); // no stateUpdated() call here
stateUpdated(CALL_MODE_BUTTON);
parsed = true;
}

View File

@ -803,7 +803,7 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage)
//HTTP API request parser
bool handleHttpApi(AsyncWebServerRequest *request, const String& req, bool apply)
bool handleHttpApi(const String& req, bool apply)
{
int pos = 0;
DEBUG_PRINTF_P(PSTR("API req: %s\n"), req.c_str());
@ -1030,10 +1030,7 @@ bool handleHttpApi(AsyncWebServerRequest *request, const String& req, bool apply
bool fxModeChanged = false, speedChanged = false, intensityChanged = false, paletteChanged = false;
bool custom1Changed = false, custom2Changed = false, custom3Changed = false, check1Changed = false, check2Changed = false, check3Changed = false;
// set effect parameters
if (updateVal(req.c_str(), "FX=", &effectIn, 0, strip.getModeCount()-1)) {
if (request != nullptr) unloadPlaylist(); // unload playlist if changing FX using web request
fxModeChanged = true;
}
fxModeChanged = updateVal(req.c_str(), "FX=", &effectIn, 0, strip.getModeCount()-1);
speedChanged = updateVal(req.c_str(), "SX=", &speedIn);
intensityChanged = updateVal(req.c_str(), "IX=", &intensityIn);
paletteChanged = updateVal(req.c_str(), "FP=", &paletteIn, 0, strip.getPaletteCount()-1);
@ -1187,12 +1184,5 @@ bool handleHttpApi(AsyncWebServerRequest *request, const String& req, bool apply
pos = req.indexOf(F("&NN")); //do not send UDP notifications this time
stateUpdated((pos >= 0) ? CALL_MODE_NO_NOTIFY : CALL_MODE_DIRECT_CHANGE);
// internal call, does not send XML response
if (request != nullptr) {
auto response = request->beginResponseStream("text/xml");
XML_response(*response);
request->send(response);
}
return true;
}

View File

@ -681,7 +681,7 @@ void handleNotifications()
if (requestJSONBufferLock(18)) {
if (udpIn[0] >= 'A' && udpIn[0] <= 'Z') { //HTTP API
handleHttpApi(nullptr, (char*)udpIn);
handleHttpApi((char*)udpIn);
} else if (udpIn[0] == '{') { //JSON API
DeserializationError error = deserializeJson(*pDoc, udpIn);
JsonObject root = pDoc->as<JsonObject>();

View File

@ -483,7 +483,15 @@ void initServer()
}
if (request->url().indexOf("win") == 0) {
if(handleHttpApi(request, request->url().substring(3))) return;
if (handleHttpApi(request->url().substring(3))) {
if (request->url().indexOf(F("&FX=")) > 0) {
unloadPlaylist(); // Setting an FX via HTTP disables any active playlist.
}
auto response = request->beginResponseStream(CONTENT_TYPE_XML);
XML_response(*response);
request->send(response);
return;
}
}
#ifndef WLED_DISABLE_ALEXA
if(espalexa.handleAlexaApiCall(request)) return;