From 5d08c8ee5d4946f469b6dc053d1df19957bd36a9 Mon Sep 17 00:00:00 2001 From: timw1971 Date: Thu, 3 Aug 2017 13:38:42 +0100 Subject: [PATCH] Add files via upload 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. --- .../ESP8266WebServer/src/ESP8266WebServer.cpp | 22 +++++++++++++++++++ .../ESP8266WebServer/src/ESP8266WebServer.h | 2 ++ 2 files changed, 24 insertions(+) diff --git a/libraries/ESP8266WebServer/src/ESP8266WebServer.cpp b/libraries/ESP8266WebServer/src/ESP8266WebServer.cpp index bc41a5603b..c0d43f8b0c 100644 --- a/libraries/ESP8266WebServer/src/ESP8266WebServer.cpp +++ b/libraries/ESP8266WebServer/src/ESP8266WebServer.cpp @@ -292,6 +292,14 @@ void ESP8266WebServer::send(int code, const char* content_type, const String& co sendContent(content); } +void ESP8266WebServer::send(int code, const char* content_type, const char* content, size_t contentLength) { + String header; + _prepareHeader(header, code, content_type, contentLength); + sendContent(header); + + sendContent(content, contentLength); +} + void ESP8266WebServer::send_P(int code, PGM_P content_type, PGM_P content) { size_t contentLength = 0; @@ -341,6 +349,20 @@ void ESP8266WebServer::sendContent(const String& content) { } } +void ESP8266WebServer::sendContent(const char* content, size_t contentLength) { + const size_t unit_size = HTTP_DOWNLOAD_UNIT_SIZE; + + while (contentLength) { + size_t will_send = (contentLength < unit_size) ? contentLength : unit_size; + size_t sent = _currentClient.write(content, will_send); + if (sent == 0) { + break; + } + contentLength -= sent; + content += sent; + } +} + void ESP8266WebServer::sendContent_P(PGM_P content) { sendContent_P(content, strlen_P(content)); } diff --git a/libraries/ESP8266WebServer/src/ESP8266WebServer.h b/libraries/ESP8266WebServer/src/ESP8266WebServer.h index ab22fe659e..60330d9d1e 100644 --- a/libraries/ESP8266WebServer/src/ESP8266WebServer.h +++ b/libraries/ESP8266WebServer/src/ESP8266WebServer.h @@ -114,6 +114,7 @@ class ESP8266WebServer // content - actual content body 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 char* content_type, const char* content, size_t contentLength); void send(int code, const String& content_type, const String& content); 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); @@ -121,6 +122,7 @@ class ESP8266WebServer void setContentLength(size_t contentLength); void sendHeader(const String& name, const String& value, bool first = false); void sendContent(const String& content); + void sendContent(const char* content, size_t contentLength); void sendContent_P(PGM_P content); void sendContent_P(PGM_P content, size_t size);