Skip to content

Commit 36f9034

Browse files
earlephilhowerdevyte
authored andcommitted
Add const char* content to ESP8266WebServer::send() (#6797)
* 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. * Fix example formatting * Make GIF example use static const array * 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).
1 parent 919c753 commit 36f9034

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

libraries/ESP8266WebServer/examples/HelloServer/HelloServer.ino

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,23 @@ void setup(void) {
6767
server.send(200, "text/plain", "this works as well");
6868
});
6969

70+
server.on("/gif", []() {
71+
static const uint8_t gif[] PROGMEM = {
72+
0x47, 0x49, 0x46, 0x38, 0x37, 0x61, 0x10, 0x00, 0x10, 0x00, 0x80, 0x01,
73+
0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x2c, 0x00, 0x00, 0x00, 0x00,
74+
0x10, 0x00, 0x10, 0x00, 0x00, 0x02, 0x19, 0x8c, 0x8f, 0xa9, 0xcb, 0x9d,
75+
0x00, 0x5f, 0x74, 0xb4, 0x56, 0xb0, 0xb0, 0xd2, 0xf2, 0x35, 0x1e, 0x4c,
76+
0x0c, 0x24, 0x5a, 0xe6, 0x89, 0xa6, 0x4d, 0x01, 0x00, 0x3b
77+
};
78+
char gif_colored[sizeof(gif)];
79+
memcpy_P(gif_colored, gif, sizeof(gif));
80+
// Set the background to a random set of colors
81+
gif_colored[16] = millis() % 256;
82+
gif_colored[17] = millis() % 256;
83+
gif_colored[18] = millis() % 256;
84+
server.send(200, "image/gif", gif_colored, sizeof(gif_colored));
85+
});
86+
7087
server.onNotFound(handleNotFound);
7188

7289
server.begin();

libraries/ESP8266WebServer/src/ESP8266WebServer.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,15 @@ class ESP8266WebServerTemplate
127127
void send(int code, const char* content_type = NULL, const String& content = String(""));
128128
void send(int code, char* content_type, const String& content);
129129
void send(int code, const String& content_type, const String& content);
130+
void send(int code, const char *content_type, const char *content, size_t content_length = 0) {
131+
if (content_length == 0) {
132+
content_length = strlen_P(content);
133+
}
134+
send_P(code, content_type, content, content_length);
135+
}
136+
void send(int code, const char *content_type, const uint8_t *content, size_t content_length) {
137+
send_P(code, content_type, (const char *)content, content_length);
138+
}
130139
void send_P(int code, PGM_P content_type, PGM_P content);
131140
void send_P(int code, PGM_P content_type, PGM_P content, size_t contentLength);
132141

0 commit comments

Comments
 (0)