Skip to content

Commit a2403f1

Browse files
authored
WifiClient::write refactoring (second attempt) (#2177)
* WiFiClient: use DataSource for writes * ESP8266WebServer: delegate writing to WiFiClient * ESP8266WebServer: set write timeout before sending content
1 parent 353a353 commit a2403f1

File tree

2 files changed

+10
-59
lines changed

2 files changed

+10
-59
lines changed

src/ESP8266WebServer.cpp

+7-57
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ void ESP8266WebServer::handleClient() {
193193
_currentStatus = HC_NONE;
194194
return;
195195
}
196-
196+
_currentClient.setTimeout(HTTP_MAX_SEND_WAIT);
197197
_contentLength = CONTENT_LENGTH_NOT_SET;
198198
_handleRequest();
199199

@@ -241,6 +241,9 @@ void ESP8266WebServer::sendHeader(const String& name, const String& value, bool
241241
}
242242
}
243243

244+
void ESP8266WebServer::setContentLength(size_t contentLength) {
245+
_contentLength = contentLength;
246+
}
244247

245248
void ESP8266WebServer::_prepareHeader(String& response, int code, const char* content_type, size_t contentLength) {
246249
response = "HTTP/1.1 ";
@@ -270,7 +273,6 @@ void ESP8266WebServer::send(int code, const char* content_type, const String& co
270273
String header;
271274
_prepareHeader(header, code, content_type, content.length());
272275
sendContent(header);
273-
274276
sendContent(content);
275277
}
276278

@@ -307,67 +309,15 @@ void ESP8266WebServer::send(int code, const String& content_type, const String&
307309
}
308310

309311
void ESP8266WebServer::sendContent(const String& content) {
310-
const size_t unit_size = HTTP_DOWNLOAD_UNIT_SIZE;
311-
size_t size_to_send = content.length();
312-
const char* send_start = content.c_str();
313-
314-
while (size_to_send) {
315-
size_t will_send = (size_to_send < unit_size) ? size_to_send : unit_size;
316-
size_t sent = _currentClient.write(send_start, will_send);
317-
if (sent == 0) {
318-
break;
319-
}
320-
size_to_send -= sent;
321-
send_start += sent;
322-
}
312+
_currentClient.write(content.c_str(), content.length());
323313
}
324314

325315
void ESP8266WebServer::sendContent_P(PGM_P content) {
326-
char contentUnit[HTTP_DOWNLOAD_UNIT_SIZE + 1];
327-
328-
contentUnit[HTTP_DOWNLOAD_UNIT_SIZE] = '\0';
329-
330-
while (content != NULL) {
331-
size_t contentUnitLen;
332-
PGM_P contentNext;
333-
334-
// due to the memccpy signature, lots of casts are needed
335-
contentNext = (PGM_P)memccpy_P((void*)contentUnit, (PGM_VOID_P)content, 0, HTTP_DOWNLOAD_UNIT_SIZE);
336-
337-
if (contentNext == NULL) {
338-
// no terminator, more data available
339-
content += HTTP_DOWNLOAD_UNIT_SIZE;
340-
contentUnitLen = HTTP_DOWNLOAD_UNIT_SIZE;
341-
}
342-
else {
343-
// reached terminator. Do not send the terminator
344-
contentUnitLen = contentNext - contentUnit - 1;
345-
content = NULL;
346-
}
347-
348-
// write is so overloaded, had to use the cast to get it pick the right one
349-
_currentClient.write((const char*)contentUnit, contentUnitLen);
350-
}
316+
_currentClient.write_P(content, strlen_P(content));
351317
}
352318

353319
void ESP8266WebServer::sendContent_P(PGM_P content, size_t size) {
354-
char contentUnit[HTTP_DOWNLOAD_UNIT_SIZE + 1];
355-
contentUnit[HTTP_DOWNLOAD_UNIT_SIZE] = '\0';
356-
size_t remaining_size = size;
357-
358-
while (content != NULL && remaining_size > 0) {
359-
size_t contentUnitLen = HTTP_DOWNLOAD_UNIT_SIZE;
360-
361-
if (remaining_size < HTTP_DOWNLOAD_UNIT_SIZE) contentUnitLen = remaining_size;
362-
// due to the memcpy signature, lots of casts are needed
363-
memcpy_P((void*)contentUnit, (PGM_VOID_P)content, contentUnitLen);
364-
365-
content += contentUnitLen;
366-
remaining_size -= contentUnitLen;
367-
368-
// write is so overloaded, had to use the cast to get it pick the right one
369-
_currentClient.write((const char*)contentUnit, contentUnitLen);
370-
}
320+
_currentClient.write_P(content, size);
371321
}
372322

373323

src/ESP8266WebServer.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ enum HTTPClientStatus { HC_NONE, HC_WAIT_READ, HC_WAIT_CLOSE };
3636
#define HTTP_UPLOAD_BUFLEN 2048
3737
#define HTTP_MAX_DATA_WAIT 1000 //ms to wait for the client to send the request
3838
#define HTTP_MAX_POST_WAIT 1000 //ms to wait for POST data to arrive
39+
#define HTTP_MAX_SEND_WAIT 5000 //ms to wait for data chunk to be ACKed
3940
#define HTTP_MAX_CLOSE_WAIT 2000 //ms to wait for the client to close the connection
4041

4142
#define CONTENT_LENGTH_UNKNOWN ((size_t) -1)
@@ -113,7 +114,7 @@ class ESP8266WebServer
113114
void send_P(int code, PGM_P content_type, PGM_P content);
114115
void send_P(int code, PGM_P content_type, PGM_P content, size_t contentLength);
115116

116-
void setContentLength(size_t contentLength) { _contentLength = contentLength; }
117+
void setContentLength(size_t contentLength);
117118
void sendHeader(const String& name, const String& value, bool first = false);
118119
void sendContent(const String& content);
119120
void sendContent_P(PGM_P content);
@@ -129,7 +130,7 @@ template<typename T> size_t streamFile(T &file, const String& contentType){
129130
sendHeader("Content-Encoding", "gzip");
130131
}
131132
send(200, contentType, "");
132-
return _currentClient.write(file, HTTP_DOWNLOAD_UNIT_SIZE);
133+
return _currentClient.write(file);
133134
}
134135

135136
protected:

0 commit comments

Comments
 (0)