Free flash space (WSSend)

Free flash space (WSSend)
This commit is contained in:
Theo Arends 2019-02-23 15:29:42 +01:00
parent a1b04d166b
commit 19bdf7e803
3 changed files with 42 additions and 40 deletions

View File

@ -432,11 +432,8 @@ const char HTTP_END[] PROGMEM =
const char HTTP_DEVICE_CONTROL[] PROGMEM = "<td style='width:%d%%'><button onclick='la(\"?o=%d\");'>%s%s</button></td>"; // ?o is related to WebGetArg("o", tmp, sizeof(tmp));
const char HTTP_DEVICE_STATE[] PROGMEM = "%s<td style='width:%d{c}%s;font-size:%dpx'>%s</div></td>"; // {c} = %'><div style='text-align:center;font-weight:
const char HDR_CTYPE_PLAIN[] PROGMEM = "text/plain";
const char HDR_CTYPE_HTML[] PROGMEM = "text/html";
const char HDR_CTYPE_XML[] PROGMEM = "text/xml";
const char HDR_CTYPE_JSON[] PROGMEM = "application/json";
const char HDR_CTYPE_STREAM[] PROGMEM = "application/octet-stream";
enum CTypes { CT_HTML, CT_PLAIN, CT_XML, CT_JSON, CT_STREAM };
const char kContentTypes[] PROGMEM = "text/html|text/plain|text/xml|application/json|application/octet-stream";
const char kUploadErrors[] PROGMEM =
D_UPLOAD_ERR_1 "|" D_UPLOAD_ERR_2 "|" D_UPLOAD_ERR_3 "|" D_UPLOAD_ERR_4 "|" D_UPLOAD_ERR_5 "|" D_UPLOAD_ERR_6 "|" D_UPLOAD_ERR_7 "|" D_UPLOAD_ERR_8 "|" D_UPLOAD_ERR_9
@ -582,6 +579,12 @@ void PollDnsWebserver(void)
/*********************************************************************************************/
void WSSend(int code, int ctype, const String& content)
{
char ct[25]; // strlen("application/octet-stream") +1 = Longest Content type string
WebServer->send(code, GetTextIndexed(ct, sizeof(ct), ctype, kContentTypes), content);
}
void SetHeader(void)
{
WebServer->sendHeader(F("Cache-Control"), F("no-cache, no-store, must-revalidate"));
@ -641,7 +644,7 @@ void ShowPage(String &page, bool auth)
ShowFreeMem(PSTR("ShowPage"));
WebServer->send(200, FPSTR(HDR_CTYPE_HTML), page);
WSSend(200, CT_HTML, page);
}
void ShowPage(String &page)
@ -857,7 +860,7 @@ void HandleAjaxStatusRefresh(void)
}
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s</tr></table>"), mqtt_data);
}
WebServer->send(200, FPSTR(HDR_CTYPE_HTML), mqtt_data);
WSSend(200, CT_HTML, mqtt_data);
}
bool HttpCheckPriviledgedAccess(bool autorequestauth = true)
@ -921,7 +924,7 @@ void HandleTemplateConfiguration(void)
snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SCRIPT_MODULE3, midx, AnyModuleName(midx).c_str(), midx +1);
page += mqtt_data;
}
WebServer->send(200, FPSTR(HDR_CTYPE_PLAIN), page);
WSSend(200, CT_PLAIN, page);
return;
}
@ -958,7 +961,7 @@ void HandleTemplateConfiguration(void)
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,%d,%d"), mqtt_data, flag, Settings.user_template_base); // FLAG: ,1 BASE: ,17
page += mqtt_data;
WebServer->send(200, FPSTR(HDR_CTYPE_PLAIN), page);
WSSend(200, CT_PLAIN, page);
return;
}
@ -1050,7 +1053,7 @@ void HandleModuleConfiguration(void)
snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SCRIPT_MODULE3, midx, AnyModuleName(midx).c_str(), vidx);
page += mqtt_data;
}
WebServer->send(200, FPSTR(HDR_CTYPE_PLAIN), page);
WSSend(200, CT_PLAIN, page);
return;
}
@ -1063,7 +1066,7 @@ void HandleModuleConfiguration(void)
page += mqtt_data;
}
}
WebServer->send(200, FPSTR(HDR_CTYPE_PLAIN), page);
WSSend(200, CT_PLAIN, page);
return;
}
@ -1461,7 +1464,7 @@ void HandleBackupConfiguration(void)
snprintf_P(attachment, sizeof(attachment), PSTR("attachment; filename=Config_%s_%s.dmp"), NoAlNumToUnderscore(friendlyname, Settings.friendlyname[0]), my_version);
WebServer->sendHeader(F("Content-Disposition"), attachment);
WebServer->send(200, FPSTR(HDR_CTYPE_STREAM), "");
WSSend(200, CT_STREAM, "");
uint16_t cfg_crc = Settings.cfg_crc;
Settings.cfg_crc = GetSettingsCrc(); // Calculate crc (again) as it might be wrong when savedata = 0 (#3918)
@ -1944,7 +1947,7 @@ void HandlePreflightRequest(void)
WebServer->sendHeader(F("Access-Control-Allow-Origin"), F("*"));
WebServer->sendHeader(F("Access-Control-Allow-Methods"), F("GET, POST"));
WebServer->sendHeader(F("Access-Control-Allow-Headers"), F("authorization"));
WebServer->send(200, FPSTR(HDR_CTYPE_HTML), "");
WSSend(200, CT_HTML, "");
}
/*-------------------------------------------------------------------------------------------*/
@ -2002,7 +2005,7 @@ void HandleHttpCommand(void)
message += F(D_NEED_USER_AND_PASSWORD "\"}");
}
SetHeader();
WebServer->send(200, FPSTR(HDR_CTYPE_JSON), message);
WSSend(200, CT_JSON, message);
}
/*-------------------------------------------------------------------------------------------*/
@ -2072,7 +2075,7 @@ void HandleAjaxConsoleRefresh(void)
} while (counter != web_log_index);
}
message += F("\1");
WebServer->send(200, FPSTR(HDR_CTYPE_XML), message);
WSSend(200, CT_XML, message);
}
/********************************************************************************************/
@ -2097,7 +2100,7 @@ void HandleNotFound(void)
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s %s: %s\n"), mqtt_data, WebServer->argName(i).c_str(), WebServer->arg(i).c_str());
}
SetHeader();
WebServer->send(404, FPSTR(HDR_CTYPE_PLAIN), mqtt_data);
WSSend(404, CT_PLAIN, mqtt_data);
}
}
@ -2108,7 +2111,7 @@ bool CaptivePortal(void)
AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_HTTP D_REDIRECTED));
WebServer->sendHeader(F("Location"), String("http://") + WebServer->client().localIP().toString(), true);
WebServer->send(302, FPSTR(HDR_CTYPE_PLAIN), ""); // Empty content inhibits Content-length header so we have to close the socket ourselves.
WSSend(302, CT_PLAIN, ""); // Empty content inhibits Content-length header so we have to close the socket ourselves.
WebServer->client().stop(); // Stop is needed because we sent no content length
return true;
}

View File

@ -980,18 +980,18 @@ void HandleKNXConfiguration(void)
page.replace( F("</script>"),
F("function GAwarning()"
"{"
"var GA_FNUM = document.getElementById('GA_FNUM');"
"var GA_AREA = document.getElementById('GA_AREA');"
"var GA_FDEF = document.getElementById('GA_FDEF');"
"var GA_FNUM = eb('GA_FNUM');"
"var GA_AREA = eb('GA_AREA');"
"var GA_FDEF = eb('GA_FDEF');"
"if ( GA_FNUM != null && GA_FNUM.value == '0' && GA_AREA.value == '0' && GA_FDEF.value == '0' ) {"
"alert('" D_KNX_WARNING "');"
"}"
"}"
"function CBwarning()"
"{"
"var CB_FNUM = document.getElementById('CB_FNUM');"
"var CB_AREA = document.getElementById('CB_AREA');"
"var CB_FDEF = document.getElementById('CB_FDEF');"
"var CB_FNUM = eb('CB_FNUM');"
"var CB_AREA = eb('CB_AREA');"
"var CB_FDEF = eb('CB_FDEF');"
"if ( CB_FNUM != null && CB_FNUM.value == '0' && CB_AREA.value == '0' && CB_FDEF.value == '0' ) {"
"alert('" D_KNX_WARNING "');"
"}"

View File

@ -405,21 +405,21 @@ void HandleUpnpEvent(void)
state_xml.replace(F("Set"), F("Get"));
}
state_xml.replace("{x1", String(bitRead(power, devices_present -1)));
WebServer->send(200, FPSTR(HDR_CTYPE_XML), state_xml);
WSSend(200, CT_XML, state_xml);
}
void HandleUpnpService(void)
{
AddLog_P(LOG_LEVEL_DEBUG, S_LOG_HTTP, PSTR(D_WEMO_EVENT_SERVICE));
WebServer->send(200, FPSTR(HDR_CTYPE_PLAIN), FPSTR(WEMO_EVENTSERVICE_XML));
WSSend(200, CT_PLAIN, FPSTR(WEMO_EVENTSERVICE_XML));
}
void HandleUpnpMetaService(void)
{
AddLog_P(LOG_LEVEL_DEBUG, S_LOG_HTTP, PSTR(D_WEMO_META_SERVICE));
WebServer->send(200, FPSTR(HDR_CTYPE_PLAIN), FPSTR(WEMO_METASERVICE_XML));
WSSend(200, CT_PLAIN, FPSTR(WEMO_METASERVICE_XML));
}
void HandleUpnpSetupWemo(void)
@ -430,7 +430,7 @@ void HandleUpnpSetupWemo(void)
setup_xml.replace("{x1", Settings.friendlyname[0]);
setup_xml.replace("{x2", WemoUuid());
setup_xml.replace("{x3", WemoSerialnumber());
WebServer->send(200, FPSTR(HDR_CTYPE_XML), setup_xml);
WSSend(200, CT_XML, setup_xml);
}
/*********************************************************************************************\
@ -532,7 +532,7 @@ void HandleUpnpSetupHue(void)
description_xml.replace("{x1", WiFi.localIP().toString());
description_xml.replace("{x2", HueUuid());
description_xml.replace("{x3", HueSerialnumber());
WebServer->send(200, FPSTR(HDR_CTYPE_XML), description_xml);
WSSend(200, CT_XML, description_xml);
}
void HueNotImplemented(String *path)
@ -540,7 +540,7 @@ void HueNotImplemented(String *path)
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_HTTP D_HUE_API_NOT_IMPLEMENTED " (%s)"), path->c_str());
AddLog(LOG_LEVEL_DEBUG_MORE);
WebServer->send(200, FPSTR(HDR_CTYPE_JSON), "{}");
WSSend(200, CT_JSON, "{}");
}
void HueConfigResponse(String *response)
@ -559,7 +559,7 @@ void HueConfig(String *path)
{
String response = "";
HueConfigResponse(&response);
WebServer->send(200, FPSTR(HDR_CTYPE_JSON), response);
WSSend(200, CT_JSON, response);
}
bool g_gotct = false;
@ -610,7 +610,7 @@ void HueGlobalConfig(String *path)
response += F("},\"groups\":{},\"schedules\":{},\"config\":");
HueConfigResponse(&response);
response += "}";
WebServer->send(200, FPSTR(HDR_CTYPE_JSON), response);
WSSend(200, CT_JSON, response);
}
void HueAuthentication(String *path)
@ -618,7 +618,7 @@ void HueAuthentication(String *path)
char response[38];
snprintf_P(response, sizeof(response), PSTR("[{\"success\":{\"username\":\"%s\"}}]"), GetHueUserId().c_str());
WebServer->send(200, FPSTR(HDR_CTYPE_JSON), response);
WSSend(200, CT_JSON, response);
}
void HueLights(String *path)
@ -627,15 +627,16 @@ void HueLights(String *path)
* http://sonoff/api/username/lights/1/state?1={"on":true,"hue":56100,"sat":254,"bri":254,"alert":"none","transitiontime":40}
*/
String response;
uint8_t device = 1;
uint16_t tmp = 0;
int code = 200;
float bri = 0;
float hue = 0;
float sat = 0;
uint16_t tmp = 0;
uint16_t ct = 0;
bool resp = false;
bool on = false;
bool change = false;
uint8_t device = 1;
uint8_t maxhue = (devices_present > MAX_FRIENDLYNAMES) ? MAX_FRIENDLYNAMES : devices_present;
path->remove(0,path->indexOf("/lights")); // Remove until /lights
@ -651,7 +652,6 @@ void HueLights(String *path)
}
}
response += "}";
WebServer->send(200, FPSTR(HDR_CTYPE_JSON), response);
}
else if (path->endsWith("/state")) { // Got ID/state
path->remove(0,8); // Remove /lights/
@ -761,8 +761,6 @@ void HueLights(String *path)
else {
response = FPSTR(HUE_ERROR_JSON);
}
WebServer->send(200, FPSTR(HDR_CTYPE_JSON), response);
}
else if(path->indexOf("/lights/") >= 0) { // Got /lights/ID
path->remove(0,8); // Remove /lights/
@ -773,11 +771,12 @@ void HueLights(String *path)
response += F("{\"state\":");
HueLightStatus1(device, &response);
HueLightStatus2(device, &response);
WebServer->send(200, FPSTR(HDR_CTYPE_JSON), response);
}
else {
WebServer->send(406, FPSTR(HDR_CTYPE_JSON), "{}");
response = "{}";
code = 406;
}
WSSend(code, CT_JSON, response);
}
void HueGroups(String *path)
@ -799,7 +798,7 @@ void HueGroups(String *path)
response += F("}");
}
WebServer->send(200, FPSTR(HDR_CTYPE_JSON), response);
WSSend(200, CT_JSON, response);
}
void HandleHueApi(String *path)