From c3ccc27c2fcd793f0fbc05e7d21ae2c8182a9945 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Sun, 17 Nov 2019 12:28:17 -0800 Subject: [PATCH 1/4] Add const char* content to ESP8266WebSerer::send() Supercedes #3492 Allow sending raw binary data or strings directly without conversion to a String to reduce memory overhead when possible. From original @timw1971 PR #3492 Added public functions to allow content to be uploaded using const char*. For some cases, this can remove the need for content to be copied into a String, and thus can be considerably more space-efficient. --- .../examples/HelloServer/HelloServer.ino | 11 +++++++++++ libraries/ESP8266WebServer/src/ESP8266WebServer.h | 9 +++++++++ 2 files changed, 20 insertions(+) diff --git a/libraries/ESP8266WebServer/examples/HelloServer/HelloServer.ino b/libraries/ESP8266WebServer/examples/HelloServer/HelloServer.ino index 40bb9ea294..6ff4535122 100644 --- a/libraries/ESP8266WebServer/examples/HelloServer/HelloServer.ino +++ b/libraries/ESP8266WebServer/examples/HelloServer/HelloServer.ino @@ -67,6 +67,17 @@ void setup(void) { server.send(200, "text/plain", "this works as well"); }); + server.on("/gif", []() { + uint8_t gif[] = { + 0x47, 0x49, 0x46, 0x38, 0x37, 0x61, 0x10, 0x00, 0x10, 0x00, 0x80, 0x01, + 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x2c, 0x00, 0x00, 0x00, 0x00, + 0x10, 0x00, 0x10, 0x00, 0x00, 0x02, 0x19, 0x8c, 0x8f, 0xa9, 0xcb, 0x9d, + 0x00, 0x5f, 0x74, 0xb4, 0x56, 0xb0, 0xb0, 0xd2, 0xf2, 0x35, 0x1e, 0x4c, + 0x0c, 0x24, 0x5a, 0xe6, 0x89, 0xa6, 0x4d, 0x01, 0x00, 0x3b }; + + server.send(200, "image/gif", gif, sizeof(gif)); + }); + server.onNotFound(handleNotFound); server.begin(); diff --git a/libraries/ESP8266WebServer/src/ESP8266WebServer.h b/libraries/ESP8266WebServer/src/ESP8266WebServer.h index 4281ce8636..60b10249d1 100644 --- a/libraries/ESP8266WebServer/src/ESP8266WebServer.h +++ b/libraries/ESP8266WebServer/src/ESP8266WebServer.h @@ -127,6 +127,15 @@ class ESP8266WebServerTemplate void send(int code, const char* content_type = NULL, const String& content = String("")); void send(int code, char* content_type, const String& content); void send(int code, const String& content_type, const String& content); + void send(int code, const char *content_type, const char *content, size_t content_length = 0) { + if (content_length == 0) { + content_length = strlen_P(content); + } + send_P(code, content_type, content, content_length); + } + void send(int code, const char *content_type, const uint8_t *content, size_t content_length) { + send_P(code, content_type, (const char *)content, content_length); + } void send_P(int code, PGM_P content_type, PGM_P content); void send_P(int code, PGM_P content_type, PGM_P content, size_t contentLength); From a0b94c659dc4ba7633d6f0a74c4876e2cb5bee05 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Sun, 17 Nov 2019 13:22:05 -0800 Subject: [PATCH 2/4] Fix example formatting --- .../ESP8266WebServer/examples/HelloServer/HelloServer.ino | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libraries/ESP8266WebServer/examples/HelloServer/HelloServer.ino b/libraries/ESP8266WebServer/examples/HelloServer/HelloServer.ino index 6ff4535122..609d86841d 100644 --- a/libraries/ESP8266WebServer/examples/HelloServer/HelloServer.ino +++ b/libraries/ESP8266WebServer/examples/HelloServer/HelloServer.ino @@ -73,7 +73,8 @@ void setup(void) { 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x2c, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x10, 0x00, 0x00, 0x02, 0x19, 0x8c, 0x8f, 0xa9, 0xcb, 0x9d, 0x00, 0x5f, 0x74, 0xb4, 0x56, 0xb0, 0xb0, 0xd2, 0xf2, 0x35, 0x1e, 0x4c, - 0x0c, 0x24, 0x5a, 0xe6, 0x89, 0xa6, 0x4d, 0x01, 0x00, 0x3b }; + 0x0c, 0x24, 0x5a, 0xe6, 0x89, 0xa6, 0x4d, 0x01, 0x00, 0x3b + }; server.send(200, "image/gif", gif, sizeof(gif)); }); From c320ab8bcf4f9722990658858bed6967ba6199f4 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Sun, 17 Nov 2019 14:03:12 -0800 Subject: [PATCH 3/4] Make GIF example use static const array --- libraries/ESP8266WebServer/examples/HelloServer/HelloServer.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/ESP8266WebServer/examples/HelloServer/HelloServer.ino b/libraries/ESP8266WebServer/examples/HelloServer/HelloServer.ino index 609d86841d..4ab74b92e3 100644 --- a/libraries/ESP8266WebServer/examples/HelloServer/HelloServer.ino +++ b/libraries/ESP8266WebServer/examples/HelloServer/HelloServer.ino @@ -68,7 +68,7 @@ void setup(void) { }); server.on("/gif", []() { - uint8_t gif[] = { + static const uint8_t gif[] = { 0x47, 0x49, 0x46, 0x38, 0x37, 0x61, 0x10, 0x00, 0x10, 0x00, 0x80, 0x01, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x2c, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x10, 0x00, 0x00, 0x02, 0x19, 0x8c, 0x8f, 0xa9, 0xcb, 0x9d, From 6c83b6396c5268c3c4a08cf179bd23fd025f0451 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Tue, 19 Nov 2019 08:07:25 -0700 Subject: [PATCH 4/4] Make the example really need to use const char* Make the generated GIF dynamic in the example and move the original to PROGMEM (since that's where const arrays like this belong). --- .../examples/HelloServer/HelloServer.ino | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/libraries/ESP8266WebServer/examples/HelloServer/HelloServer.ino b/libraries/ESP8266WebServer/examples/HelloServer/HelloServer.ino index 4ab74b92e3..1335c347af 100644 --- a/libraries/ESP8266WebServer/examples/HelloServer/HelloServer.ino +++ b/libraries/ESP8266WebServer/examples/HelloServer/HelloServer.ino @@ -68,15 +68,20 @@ void setup(void) { }); server.on("/gif", []() { - static const uint8_t gif[] = { + static const uint8_t gif[] PROGMEM = { 0x47, 0x49, 0x46, 0x38, 0x37, 0x61, 0x10, 0x00, 0x10, 0x00, 0x80, 0x01, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x2c, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x10, 0x00, 0x00, 0x02, 0x19, 0x8c, 0x8f, 0xa9, 0xcb, 0x9d, 0x00, 0x5f, 0x74, 0xb4, 0x56, 0xb0, 0xb0, 0xd2, 0xf2, 0x35, 0x1e, 0x4c, 0x0c, 0x24, 0x5a, 0xe6, 0x89, 0xa6, 0x4d, 0x01, 0x00, 0x3b }; - - server.send(200, "image/gif", gif, sizeof(gif)); + char gif_colored[sizeof(gif)]; + memcpy_P(gif_colored, gif, sizeof(gif)); + // Set the background to a random set of colors + gif_colored[16] = millis() % 256; + gif_colored[17] = millis() % 256; + gif_colored[18] = millis() % 256; + server.send(200, "image/gif", gif_colored, sizeof(gif_colored)); }); server.onNotFound(handleNotFound);