From 82748a872cecc2115ca821ded1717b8e2fce7210 Mon Sep 17 00:00:00 2001 From: Martin Ayotte Date: Sun, 6 Sep 2015 13:27:12 -0400 Subject: [PATCH] add ESP8266WebServer::sendContent_P with 'size_t size' argument for binary content --- .../ESP8266WebServer/src/ESP8266WebServer.cpp | 29 +++++++++++++++++++ .../ESP8266WebServer/src/ESP8266WebServer.h | 1 + 2 files changed, 30 insertions(+) diff --git a/libraries/ESP8266WebServer/src/ESP8266WebServer.cpp b/libraries/ESP8266WebServer/src/ESP8266WebServer.cpp index 3af856184..f2f21c781 100644 --- a/libraries/ESP8266WebServer/src/ESP8266WebServer.cpp +++ b/libraries/ESP8266WebServer/src/ESP8266WebServer.cpp @@ -221,6 +221,35 @@ void ESP8266WebServer::sendContent_P(PGM_P content) { } } +void ESP8266WebServer::sendContent_P(PGM_P content, size_t size) { + char contentUnit[HTTP_DOWNLOAD_UNIT_SIZE + 1]; + + contentUnit[HTTP_DOWNLOAD_UNIT_SIZE] = '\0'; + + while (content != NULL) { + size_t contentUnitLen; + PGM_P contentNext; + + // due to the memcpy signature, lots of casts are needed + contentNext = (PGM_P)memcpy_P((void*)contentUnit, (PGM_VOID_P)content, HTTP_DOWNLOAD_UNIT_SIZE); + + if (contentNext == NULL) { + // no terminator, more data available + content += HTTP_DOWNLOAD_UNIT_SIZE; + contentUnitLen = HTTP_DOWNLOAD_UNIT_SIZE; + } + else { + // reached terminator + contentUnitLen = contentNext - content; + content = NULL; + } + + if (size < WIFICLIENT_MAX_PACKET_SIZE) contentUnitLen = size; + // write is so overloaded, had to use the cast to get it pick the right one + _currentClient.write((const char*)contentUnit, contentUnitLen); + } +} + String ESP8266WebServer::arg(const char* name) { for (int i = 0; i < _currentArgCount; ++i) { if (_currentArgs[i].key == name) diff --git a/libraries/ESP8266WebServer/src/ESP8266WebServer.h b/libraries/ESP8266WebServer/src/ESP8266WebServer.h index 33b7b8401..4f455c377 100644 --- a/libraries/ESP8266WebServer/src/ESP8266WebServer.h +++ b/libraries/ESP8266WebServer/src/ESP8266WebServer.h @@ -95,6 +95,7 @@ public: void sendHeader(const String& name, const String& value, bool first = false); void sendContent(const String& content); void sendContent_P(PGM_P content); + void sendContent_P(PGM_P content, size_t size); template size_t streamFile(T &file, const String& contentType){ setContentLength(file.size());