Skip to content

Commit

Permalink
[issue letscontrolit#1066] Fix Webserver handle_control
Browse files Browse the repository at this point in the history
There wasn't a proper start/end for the streaming webserver.
See discussion on issue letscontrolit#1066
  • Loading branch information
TD-er committed Mar 14, 2018
1 parent 5a15288 commit 14d87d9
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions src/WebServer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ public:
}

void startStream() {
startStream(false);
}

void startJsonStream() {
startStream(true);
}

private:
void startStream(bool json) {
maxCoreUsage = maxServerUsage = 0;
initialRam = ESP.getFreeHeap();
beforeTXRam = initialRam;
Expand All @@ -89,7 +98,7 @@ public:
#endif
return;
} else
sendHeaderBlocking();
sendHeaderBlocking(json);
}

void trackTotalMem() {
Expand All @@ -98,6 +107,8 @@ public:
maxServerUsage = initialRam - beforeTXRam;
}

public:

void trackCoreMem() {
duringTXRam = ESP.getFreeHeap();
if ((initialRam - duringTXRam) > maxCoreUsage)
Expand Down Expand Up @@ -158,14 +169,14 @@ void sendContentBlocking(String& data) {
data = "";
}

void sendHeaderBlocking() {
void sendHeaderBlocking(bool json) {
checkRAM(F("sendHeaderBlocking"));
#if defined(ESP8266) && defined(ARDUINO_ESP8266_RELEASE_2_3_0)
WebServer.setContentLength(CONTENT_LENGTH_UNKNOWN);
WebServer.sendHeader("Content-Type", "text/html", true);
WebServer.sendHeader("Accept-Ranges", "none");
WebServer.sendHeader("Cache-Control", "no-cache");
WebServer.sendHeader("Transfer-Encoding", "chunked");
WebServer.sendHeader(F("Content-Type"), json ? F("application/json") : F("text/html"), true);
WebServer.sendHeader(F("Accept-Ranges"), F("none"));
WebServer.sendHeader(F("Cache-Control"), F("no-cache"));
WebServer.sendHeader(F("Transfer-Encoding"), F("chunked"));
WebServer.send(200);
#else
unsigned int timeout = 0;
Expand All @@ -174,8 +185,8 @@ void sendHeaderBlocking() {
if (freeBeforeSend < 4000) timeout = 1000;
const uint32_t beginWait = millis();
WebServer.setContentLength(CONTENT_LENGTH_UNKNOWN);
WebServer.sendHeader("Content-Type", "text/html", true);
WebServer.sendHeader("Cache-Control", "no-cache");
WebServer.sendHeader(F("Content-Type"), json ? F("application/json") : F("text/html"), true);
WebServer.sendHeader(F("Cache-Control"), F("no-cache"));
WebServer.send(200);
// dont wait on 2.3.0. Memory returns just too slow.
while ((ESP.getFreeHeap() < freeBeforeSend) &&
Expand Down Expand Up @@ -1879,7 +1890,7 @@ void handle_devices() {

//allow the plugin to save plugin-specific form settings.
PluginCall(PLUGIN_WEBFORM_SAVE, &TempEvent, dummyString);

// notify controllers: CPLUGIN_TASK_CHANGE_NOTIFICATION
for (byte x=0; x < CONTROLLER_MAX; x++)
{
Expand Down Expand Up @@ -3444,8 +3455,7 @@ void handle_control() {
checkRAM(F("handle_control"));
if (!clientIPallowed()) return;
//TXBuffer.startStream(true); // true= json
// sendHeadandTail(F("TmplStd"),_HEAD);

// sendHeadandTail(F("TmplStd"),_HEAD);
String webrequest = WebServer.arg(F("cmd"));

// in case of event, store to buffer and return...
Expand All @@ -3464,18 +3474,18 @@ void handle_control() {
printToWeb = true;
printWebString = "";

if (printToWebJSON)
TXBuffer.startJsonStream();
else
TXBuffer.startStream();

if (PluginCall(PLUGIN_WRITE, &TempEvent, webrequest));
else if (remoteConfig(&TempEvent, webrequest));
else
TXBuffer += F("Unknown or restricted command!");

TXBuffer += printWebString;

if (printToWebJSON)
WebServer.send(200, "application/json");
else
WebServer.send(200, "text/html");
TXBuffer += printWebString;
TXBuffer.endStream();

printWebString = "";
printToWeb = false;
Expand Down

0 comments on commit 14d87d9

Please sign in to comment.