Skip to content

Commit b42b208

Browse files
authored
Added overload on send to cleanly handle char arrays (espressif#6721)
1 parent edd2bd2 commit b42b208

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

Diff for: libraries/WebServer/src/WebServer.cpp

+20-8
Original file line numberDiff line numberDiff line change
@@ -430,12 +430,32 @@ void WebServer::send(int code, const char* content_type, const String& content)
430430
// Can we asume the following?
431431
//if(code == 200 && content.length() == 0 && _contentLength == CONTENT_LENGTH_NOT_SET)
432432
// _contentLength = CONTENT_LENGTH_UNKNOWN;
433+
if (content.length() == 0) {
434+
log_w("content length is zero");
435+
}
433436
_prepareHeader(header, code, content_type, content.length());
434437
_currentClientWrite(header.c_str(), header.length());
435438
if(content.length())
436439
sendContent(content);
437440
}
438441

442+
void WebServer::send(int code, char* content_type, const String& content) {
443+
send(code, (const char*)content_type, content);
444+
}
445+
446+
void WebServer::send(int code, const String& content_type, const String& content) {
447+
send(code, (const char*)content_type.c_str(), content);
448+
}
449+
450+
void WebServer::send(int code, const char* content_type, const char* content)
451+
{
452+
const String passStr = (String)content;
453+
if (strlen(content) != passStr.length()) {
454+
log_e("String cast failed. Use send_P for long arrays");
455+
}
456+
send(code, content_type, passStr);
457+
}
458+
439459
void WebServer::send_P(int code, PGM_P content_type, PGM_P content) {
440460
size_t contentLength = 0;
441461

@@ -460,14 +480,6 @@ void WebServer::send_P(int code, PGM_P content_type, PGM_P content, size_t conte
460480
sendContent_P(content, contentLength);
461481
}
462482

463-
void WebServer::send(int code, char* content_type, const String& content) {
464-
send(code, (const char*)content_type, content);
465-
}
466-
467-
void WebServer::send(int code, const String& content_type, const String& content) {
468-
send(code, (const char*)content_type.c_str(), content);
469-
}
470-
471483
void WebServer::sendContent(const String& content) {
472484
sendContent(content.c_str(), content.length());
473485
}

Diff for: libraries/WebServer/src/WebServer.h

+2
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ class WebServer
120120
void send(int code, const char* content_type = NULL, const String& content = String(""));
121121
void send(int code, char* content_type, const String& content);
122122
void send(int code, const String& content_type, const String& content);
123+
void send(int code, const char* content_type, const char* content);
124+
123125
void send_P(int code, PGM_P content_type, PGM_P content);
124126
void send_P(int code, PGM_P content_type, PGM_P content, size_t contentLength);
125127

0 commit comments

Comments
 (0)