Move request buff to stack

Save 384 heap bytes by handling the web request buffering in the loop()
stack context.
This commit is contained in:
Earle F. Philhower, III committed 2017-12-14 09:47:00 -08:00
1 parent ec4979e6f5
commit 143c4e65ae
3 files changed
+7 -8

No files matched your search

+3 -2
View File
@@ -319,7 +319,7 @@ void loop()
if (decoder && decoder->isRunning()) { if (decoder && decoder->isRunning()) {
strcpy_P(status, PSTR("Playing")); // By default we're OK unless the decoder says otherwise strcpy_P(status, PSTR("Playing")); // By default we're OK unless the decoder says otherwise
if (!decoder->loop()) { if (!decoder->loop()) {
Serial.printf_P(PSTR("Stopping decoder")); Serial.printf_P(PSTR("Stopping decoder\n"));
decoder->stop(); decoder->stop();
strcpy_P(title, PSTR("Stopped")); strcpy_P(title, PSTR("Stopped"));
} }
@@ -330,7 +330,8 @@ void loop()
char *reqUrl; char *reqUrl;
char *params; char *params;
WiFiClient client = server.available(); WiFiClient client = server.available();
if (client && WebReadRequest(&client, &reqUrl, &params)) { char *reqBuff = (char *)alloca(384);
if (client && WebReadRequest(&client, reqBuff, 384, &reqUrl, &params)) {
Serial.printf_P(PSTR("URL: '%s'\n"), reqUrl); Serial.printf_P(PSTR("URL: '%s'\n"), reqUrl);
if (IsIndexHTML(reqUrl)) { if (IsIndexHTML(reqUrl)) {
HandleIndex(&client); HandleIndex(&client);
+3 -5
View File
@@ -24,8 +24,6 @@
#define LogPrintf(fmt, ...) { Serial.printf_P(PSTR(fmt), ## __VA_ARGS__); } #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) void WebPrintError(WiFiClient *client, int code)
{ {
@@ -155,7 +153,7 @@ void URLDecode(char *ptr)
// Parse HTTP request // Parse HTTP request
bool WebReadRequest(WiFiClient *client, char **urlStr, char **paramStr) bool WebReadRequest(WiFiClient *client, char *reqBuff, int reqBuffLen, char **urlStr, char **paramStr)
{ {
static char NUL = 0; // Get around writable strings... static char NUL = 0; // Get around writable strings...
@@ -169,7 +167,7 @@ bool WebReadRequest(WiFiClient *client, char **urlStr, char **paramStr)
LogPrintf("-WebReadRequest: Timeout @ %d\n", millis()); LogPrintf("-WebReadRequest: Timeout @ %d\n", millis());
return false; return false;
} }
int wlen = client->readBytesUntil('\r', reqBuff, sizeof(reqBuff)-1); int wlen = client->readBytesUntil('\r', reqBuff, reqBuffLen-1);
reqBuff[wlen] = 0; reqBuff[wlen] = 0;
@@ -206,7 +204,7 @@ bool WebReadRequest(WiFiClient *client, char **urlStr, char **paramStr)
qp = strchr(url, '?'); qp = strchr(url, '?');
if (qp) *qp = 0; // End URL @ ? if (qp) *qp = 0; // End URL @ ?
// In a POST the params are in the body // In a POST the params are in the body
int sizeleft = sizeof(reqBuff) - strlen(reqBuff) - 1; int sizeleft = reqBuffLen - strlen(reqBuff) - 1;
qp = reqBuff + strlen(reqBuff) + 1; qp = reqBuff + strlen(reqBuff) + 1;
int wlen = client->readBytesUntil('\r', qp, sizeleft-1); int wlen = client->readBytesUntil('\r', qp, sizeleft-1);
qp[wlen] = 0; qp[wlen] = 0;
+1 -1
View File
@@ -41,7 +41,7 @@ void Base64Decode(char *str); // In-place B64 decode
void URLDecode(char *ptr); // In-place URL decode void URLDecode(char *ptr); // In-place URL decode
// GET/POST parsing // GET/POST parsing
bool WebReadRequest(WiFiClient *client, char **urlStr, char **paramStr); bool WebReadRequest(WiFiClient *client, char *reqBuff, int reqBuffLen, char **urlStr, char **paramStr);
bool ParseParam(char **paramStr, char **name, char **value); // Get next name/parameter from a param string bool ParseParam(char **paramStr, char **name, char **value); // Get next name/parameter from a param string
bool IsIndexHTML(const char *url); // Is this meant to be index.html (/, index.htm, etc.) bool IsIndexHTML(const char *url); // Is this meant to be index.html (/, index.htm, etc.)