未验证 提交 f57c3678 编写于 作者: N Niklas Mollenhauer 提交者: GitHub

Add sendContent overload that takes a const char* and a length (#4276)

The web server currently lacks the ability to send a buffer. Only strings are supported.

This PR adds an overload to sendContent.
上级 3054bdf5
...@@ -463,20 +463,23 @@ void WebServer::send(int code, const String& content_type, const String& content ...@@ -463,20 +463,23 @@ void WebServer::send(int code, const String& content_type, const String& content
} }
void WebServer::sendContent(const String& content) { void WebServer::sendContent(const String& content) {
sendContent(content.c_str(), content.length());
}
void WebServer::sendContent(const char* content, size_t contentLength) {
const char * footer = "\r\n"; const char * footer = "\r\n";
size_t len = content.length();
if(_chunked) { if(_chunked) {
char * chunkSize = (char *)malloc(11); char * chunkSize = (char *)malloc(11);
if(chunkSize){ if(chunkSize){
sprintf(chunkSize, "%x%s", len, footer); sprintf(chunkSize, "%x%s", contentLength, footer);
_currentClientWrite(chunkSize, strlen(chunkSize)); _currentClientWrite(chunkSize, strlen(chunkSize));
free(chunkSize); free(chunkSize);
} }
} }
_currentClientWrite(content.c_str(), len); _currentClientWrite(content, contentLength);
if(_chunked){ if(_chunked){
_currentClient.write(footer, 2); _currentClient.write(footer, 2);
if (len == 0) { if (contentLength == 0) {
_chunked = false; _chunked = false;
} }
} }
......
...@@ -130,6 +130,7 @@ public: ...@@ -130,6 +130,7 @@ public:
void setContentLength(const size_t contentLength); void setContentLength(const size_t contentLength);
void sendHeader(const String& name, const String& value, bool first = false); void sendHeader(const String& name, const String& value, bool first = false);
void sendContent(const String& content); 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);
void sendContent_P(PGM_P content, size_t size); void sendContent_P(PGM_P content, size_t size);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册