/*
PsychoPlug
ESP8266 based remote outlet with standalone timer and MQTT integration
Copyright (C) 2017 Earle F. Philhower, III
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
*/
#include
#include
#include "web.h"
#define LogPrintf(fmt, ...) { Serial.printf_P(PSTR(fmt), ## __VA_ARGS__); }
// Web request line (URL, PARAMs parsed in-line)
static char reqBuff[384];
void WebPrintError(WiFiClient *client, int code)
{
switch(code) {
case 301: WebPrintf(client, "301 Moved Permanently"); break;
case 400: WebPrintf(client, "400 Bad Request"); break;
case 401: WebPrintf(client, "401 Unauthorized"); break;
case 404: WebPrintf(client, "404 Not Found"); break;
case 405: WebPrintf(client, "405 Method Not Allowed"); break;
default: WebPrintf(client, "500 Server Error"); break;
}
}
void WebError(WiFiClient *client, int code, const char *headers, bool usePMEM)
{
LogPrintf("+WebError: Begin, free=%d\n", ESP.getFreeHeap());
LogPrintf(" Sending headers...\n");
WebPrintf(client, "HTTP/1.1 %d\r\n", code);
WebPrintf(client, "Server: PsychoPlug\r\n");
WebPrintf(client, "Content-type: text/html\r\n");
WebPrintf(client, "Cache-Control: no-cache, no-store, must-revalidate\r\n");
WebPrintf(client, "Pragma: no-cache\r\n");
WebPrintf(client, "Expires: 0\r\n");
WebPrintf(client, "Connection: close\r\n");
LogPrintf("+WebError: Writing error headers: %08x\n", headers);
if (headers) {
if (!usePMEM) {
WebPrintf(client, "%s", headers);
} else {
WebPrintfPSTR(client, headers);
}
}
WebPrintf(client, "\r\n\r\n");
WebPrintf(client, DOCTYPE);
WebPrintf(client, "");
WebPrintError(client, code);
WebPrintf(client, "" ENCODING "\n");
WebPrintf(client, "