From b42b20850b07d8e0427be30ab1f4f3b0e1bb7aee Mon Sep 17 00:00:00 2001 From: lbernstone Date: Wed, 11 May 2022 00:01:11 -1000 Subject: [PATCH] Added overload on send to cleanly handle char arrays (#6721) --- libraries/WebServer/src/WebServer.cpp | 28 +++++++++++++++++++-------- libraries/WebServer/src/WebServer.h | 2 ++ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/libraries/WebServer/src/WebServer.cpp b/libraries/WebServer/src/WebServer.cpp index dbb3a4fe..bad6e73a 100644 --- a/libraries/WebServer/src/WebServer.cpp +++ b/libraries/WebServer/src/WebServer.cpp @@ -430,12 +430,32 @@ void WebServer::send(int code, const char* content_type, const String& content) // Can we asume the following? //if(code == 200 && content.length() == 0 && _contentLength == CONTENT_LENGTH_NOT_SET) // _contentLength = CONTENT_LENGTH_UNKNOWN; + if (content.length() == 0) { + log_w("content length is zero"); + } _prepareHeader(header, code, content_type, content.length()); _currentClientWrite(header.c_str(), header.length()); if(content.length()) sendContent(content); } +void WebServer::send(int code, char* content_type, const String& content) { + send(code, (const char*)content_type, content); +} + +void WebServer::send(int code, const String& content_type, const String& content) { + send(code, (const char*)content_type.c_str(), content); +} + +void WebServer::send(int code, const char* content_type, const char* content) +{ + const String passStr = (String)content; + if (strlen(content) != passStr.length()) { + log_e("String cast failed. Use send_P for long arrays"); + } + send(code, content_type, passStr); +} + void WebServer::send_P(int code, PGM_P content_type, PGM_P content) { size_t contentLength = 0; @@ -460,14 +480,6 @@ void WebServer::send_P(int code, PGM_P content_type, PGM_P content, size_t conte sendContent_P(content, contentLength); } -void WebServer::send(int code, char* content_type, const String& content) { - send(code, (const char*)content_type, content); -} - -void WebServer::send(int code, const String& content_type, const String& content) { - send(code, (const char*)content_type.c_str(), content); -} - void WebServer::sendContent(const String& content) { sendContent(content.c_str(), content.length()); } diff --git a/libraries/WebServer/src/WebServer.h b/libraries/WebServer/src/WebServer.h index 0293a09f..4a212678 100644 --- a/libraries/WebServer/src/WebServer.h +++ b/libraries/WebServer/src/WebServer.h @@ -120,6 +120,8 @@ public: 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); + 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);