1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-19 23:22:16 +03:00

Make the web server not waste heap

added some helper methods as well
This commit is contained in:
ficeto 2015-05-19 14:06:34 +03:00
parent 8000f2778a
commit 555813545c
2 changed files with 29 additions and 2 deletions

View File

@ -101,7 +101,8 @@ void ESP8266WebServer::handleClient()
#endif #endif
// Wait for data from client to become available // Wait for data from client to become available
while(client.connected() && !client.available()){ uint16_t maxWait = HTTP_MAX_DATA_WAIT;
while(client.connected() && !client.available() && maxWait--){
delay(1); delay(1);
} }
@ -136,7 +137,12 @@ void ESP8266WebServer::send(int code, const char* content_type, String content)
if (!content_type) if (!content_type)
content_type = "text/html"; content_type = "text/html";
String len(content.length());
sendHeader("Content-Type", content_type, true); sendHeader("Content-Type", content_type, true);
sendHeader("Content-Length", len.c_str());
sendHeader("Connection", "close");
sendHeader("Access-Control-Allow-Origin", "*");
response += _responseHeaders; response += _responseHeaders;
response += "\r\n"; response += "\r\n";
@ -145,6 +151,14 @@ void ESP8266WebServer::send(int code, const char* content_type, String content)
sendContent(response); sendContent(response);
} }
void ESP8266WebServer::send(int code, char* content_type, String content) {
send(code, (const char*)content_type, content);
}
void ESP8266WebServer::send(int code, String content_type, String content) {
send(code, (const char*)content_type.c_str(), content);
}
void ESP8266WebServer::sendContent(String content) { void ESP8266WebServer::sendContent(String content) {
size_t size_to_send = content.length(); size_t size_to_send = content.length();
size_t size_sent = 0; size_t size_sent = 0;
@ -158,6 +172,10 @@ void ESP8266WebServer::sendContent(String content) {
break; break;
} }
} }
uint16_t maxWait = HTTP_MAX_CLOSE_WAIT;
while(_currentClient.connected() && maxWait--) {
delay(1);
}
} }
String ESP8266WebServer::arg(const char* name) { String ESP8266WebServer::arg(const char* name) {

View File

@ -31,6 +31,8 @@ enum HTTPUploadStatus { UPLOAD_FILE_START, UPLOAD_FILE_WRITE, UPLOAD_FILE_END };
#define HTTP_DOWNLOAD_UNIT_SIZE 1460 #define HTTP_DOWNLOAD_UNIT_SIZE 1460
#define HTTP_UPLOAD_BUFLEN 2048 #define HTTP_UPLOAD_BUFLEN 2048
#define HTTP_MAX_DATA_WAIT 1000 //ms to wait for the client to send the request
#define HTTP_MAX_CLOSE_WAIT 2000 //ms to wait for the client to close the connection
typedef struct { typedef struct {
HTTPUploadStatus status; HTTPUploadStatus status;
@ -73,6 +75,8 @@ public:
// content_type - HTTP content type, like "text/plain" or "image/png" // content_type - HTTP content type, like "text/plain" or "image/png"
// content - actual content body // content - actual content body
void send(int code, const char* content_type = NULL, String content = String("")); void send(int code, const char* content_type = NULL, String content = String(""));
void send(int code, char* content_type, String content);
void send(int code, String content_type, String content);
void sendHeader(String name, String value, bool first = false); void sendHeader(String name, String value, bool first = false);
void sendContent(String content); void sendContent(String content);
@ -87,7 +91,12 @@ template<typename T> size_t streamFile(T &file, String contentType){
head += "\r\n\r\n"; head += "\r\n\r\n";
_currentClient.print(head); _currentClient.print(head);
head = String(); head = String();
return _currentClient.write(file, HTTP_DOWNLOAD_UNIT_SIZE); size_t res = _currentClient.write(file, HTTP_DOWNLOAD_UNIT_SIZE);
uint16_t maxWait = HTTP_MAX_CLOSE_WAIT;
while(_currentClient.connected() && maxWait--) {
delay(1);
}
return res;
} }
protected: protected: