Skip to content

ESP8266WebServer minor public interface addition - content from const char* #3492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions libraries/ESP8266WebServer/src/ESP8266WebServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,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;

Expand Down Expand Up @@ -458,6 +466,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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation does not match the one for sendContent(String&), which uses chunked encoding and leaves fragmentation to the lower level code (WiFiClient). It would be nice if this sendContent(data, size) and sendContent(String) would behave the same way.

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));
}
Expand Down
2 changes: 2 additions & 0 deletions libraries/ESP8266WebServer/src/ESP8266WebServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,15 @@ 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);

void setContentLength(const 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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For many people programming in C, const char* implies a zero-terminated string. Of course, it can also be used to pass arbitrary binary data, but most APIs i have seen use either const void* or const uint8_t* for this purpose. I would suggest changing the type here to hint that a null-terminated string is not expected.

void sendContent_P(PGM_P content);
void sendContent_P(PGM_P content, size_t size);

Expand Down