Skip to content

Commit e8b753d

Browse files
committed
Add setContentLength method to web server, update examples
related to esp8266#304
1 parent 0480205 commit e8b753d

File tree

4 files changed

+62
-58
lines changed

4 files changed

+62
-58
lines changed

hardware/esp8266com/esp8266/libraries/ESP8266WebServer/examples/FSWebServer/FSWebServer.ino

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -106,36 +106,54 @@ void handleFileUpdate(){
106106
}
107107

108108
void handleFileDelete(){
109-
if(server.args() == 0) return server.send(500, "text/plain", "BAD ARGS");
109+
if(server.args() == 0) {
110+
server.send(500, "text/plain", "BAD ARGS");
111+
return;
112+
}
110113
String path = server.arg(0);
111-
if(path == "/")
112-
return server.send(500, "text/plain", "BAD PATH");
113-
if(!FS.exists((char *)(path.c_str())))
114-
return server.send(404, "text/plain", "FileNotFound");
114+
if(path == "/") {
115+
server.send(500, "text/plain", "BAD PATH");
116+
return;
117+
}
118+
if(!FS.exists((char *)(path.c_str()))) {
119+
server.send(404, "text/plain", "FileNotFound");
120+
return;
121+
}
115122
FS.remove((char *)path.c_str());
116123
server.send(200, "text/plain", "");
117124
path = String();
118125
}
119126

120127
void handleFileCreate(){
121-
if(server.args() == 0)
122-
return server.send(500, "text/plain", "BAD ARGS");
128+
if(server.args() == 0) {
129+
server.send(500, "text/plain", "BAD ARGS");
130+
return;
131+
}
123132
String path = server.arg(0);
124-
if(path == "/")
125-
return server.send(500, "text/plain", "BAD PATH");
126-
if(FS.exists((char *)path.c_str()))
127-
return server.send(500, "text/plain", "FILE EXISTS");
133+
if(path == "/") {
134+
server.send(500, "text/plain", "BAD PATH");
135+
return;
136+
}
137+
if(FS.exists((char *)path.c_str())) {
138+
server.send(500, "text/plain", "FILE EXISTS");
139+
return;
140+
}
128141
FSFile file = FS.open((char *)path.c_str(), FSFILE_OVERWRITE);
129142
if(file)
130143
file.close();
131-
else
132-
return server.send(500, "text/plain", "CREATE FAILED");
144+
else {
145+
server.send(500, "text/plain", "CREATE FAILED");
146+
return;
147+
}
133148
server.send(200, "text/plain", "");
134149
path = String();
135150
}
136151

137152
void handleFileList() {
138-
if(!server.hasArg("dir")) return server.send(500, "text/plain", "BAD ARGS");
153+
if(!server.hasArg("dir")) {
154+
server.send(500, "text/plain", "BAD ARGS");
155+
return;
156+
}
139157
String path = server.arg("dir");
140158

141159
FSFile entry;
@@ -148,19 +166,21 @@ void handleFileList() {
148166
}
149167
dir.rewindDirectory();
150168

151-
WiFiClient client = server.client();
152-
client.print("HTTP/1.1 200 OK\r\nContent-Type: text/json\r\nConnection: close\r\n\r\n");
153169
String output = "[";
154170
while(true){
155171
entry = dir.openNextFile();
156-
if (!entry) break;
172+
if (!entry)
173+
break;
174+
157175
if(!FS.exists(entry.name())){
158176
os_printf("Entry[%s] Not Exists!\n", entry.name());
159177
entry.remove();
160178
entry.close();
161179
continue;
162180
}
163-
if(output != "[") output += ',';
181+
182+
if(output != "[")
183+
output += ',';
164184
output += "{\"type\":\"";
165185
output += (entry.isDirectory())?"dir":"file";
166186
output += "\",\"name\":\"";
@@ -169,14 +189,9 @@ void handleFileList() {
169189
entry.close();
170190
}
171191
dir.close();
172-
192+
173193
output += "]";
174-
client.write(output.c_str(), output.length());
175-
output = String();
176-
uint16_t maxWait = HTTP_MAX_CLOSE_WAIT;
177-
while(client.connected() && maxWait--) {
178-
delay(1);
179-
}
194+
server.send(200, "text/json", output);
180195
}
181196

182197
void setup(void){

hardware/esp8266com/esp8266/libraries/ESP8266WebServer/examples/SDWebServer/SDWebServer.ino

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,9 @@ bool loadFromSdCard(String path){
8585
if (!dataFile)
8686
return false;
8787

88-
if(server.hasArg("download")) dataType = "application/octet-stream";
88+
if (server.hasArg("download")) dataType = "application/octet-stream";
8989

90-
server.sendHeader("Content-Length", String(dataFile.size()));
91-
server.sendHeader("Connection", "close");
92-
server.sendHeader("Access-Control-Allow-Origin", "*");
93-
server.send(200, dataType.c_str(), "");
94-
95-
WiFiClient client = server.client();
96-
size_t totalSize = dataFile.size();
97-
if (client.write(dataFile, HTTP_DOWNLOAD_UNIT_SIZE) != totalSize) {
90+
if (server.streamFile(dataFile, dataType) != dataFile.size()) {
9891
DBG_OUTPUT_PORT.println("Sent less data than expected!");
9992
}
10093

@@ -187,7 +180,7 @@ void printDirectory() {
187180
return returnFail("NOT DIR");
188181
}
189182
dir.rewindDirectory();
190-
183+
server.setContentSize(CONTENT_SIZE_UNKNOWN);
191184
server.send(200, "text/json", "");
192185
WiFiClient client = server.client();
193186

hardware/esp8266com/esp8266/libraries/ESP8266WebServer/src/ESP8266WebServer.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ void ESP8266WebServer::handleClient()
111111
}
112112

113113
_currentClient = client;
114+
_contentLength = CONTENT_LENGTH_NOT_SET;
114115
_handleRequest();
115116
}
116117

@@ -138,9 +139,13 @@ void ESP8266WebServer::send(int code, const char* content_type, String content)
138139
if (!content_type)
139140
content_type = "text/html";
140141

141-
String len(content.length());
142142
sendHeader("Content-Type", content_type, true);
143-
sendHeader("Content-Length", len.c_str());
143+
if (_contentLength != CONTENT_LENGTH_UNKNOWN) {
144+
size_t length = (_contentLength == CONTENT_LENGTH_NOT_SET) ?
145+
content.length() : _contentLength;
146+
String lengthStr(length);
147+
sendHeader("Content-Length", lengthStr.c_str());
148+
}
144149
sendHeader("Connection", "close");
145150
sendHeader("Access-Control-Allow-Origin", "*");
146151

hardware/esp8266com/esp8266/libraries/ESP8266WebServer/src/ESP8266WebServer.h

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ enum HTTPUploadStatus { UPLOAD_FILE_START, UPLOAD_FILE_WRITE, UPLOAD_FILE_END };
3434
#define HTTP_MAX_DATA_WAIT 1000 //ms to wait for the client to send the request
3535
#define HTTP_MAX_CLOSE_WAIT 2000 //ms to wait for the client to close the connection
3636

37+
#define CONTENT_LENGTH_UNKNOWN ((size_t) -1)
38+
#define CONTENT_LENGTH_NOT_SET ((size_t) -2)
39+
3740
typedef struct {
3841
HTTPUploadStatus status;
3942
String filename;
@@ -78,32 +81,19 @@ class ESP8266WebServer
7881
void send(int code, char* content_type, String content);
7982
void send(int code, String content_type, String content);
8083

84+
void setContentLength(size_t contentLength) { _contentLength = contentLength; }
8185
void sendHeader(String name, String value, bool first = false);
8286
void sendContent(String content);
8387

8488
template<typename T> size_t streamFile(T &file, String contentType){
85-
String head = "HTTP/1.1 200 OK\r\nContent-Type: ";
86-
head += contentType;
87-
head += "\r\nContent-Length: ";
88-
head += file.size();
89-
head += "\r\nConnection: close";
90-
head += "\r\nAccess-Control-Allow-Origin: *";
91-
if(
92-
String(file.name()).endsWith(".gz") &&
93-
contentType != "application/x-gzip" &&
94-
contentType != "application/octet-stream"
95-
){
96-
head += "\r\nContent-Encoding: gzip";
97-
}
98-
head += "\r\n\r\n";
99-
_currentClient.print(head);
100-
head = String();
101-
size_t res = _currentClient.write(file, HTTP_DOWNLOAD_UNIT_SIZE);
102-
uint16_t maxWait = HTTP_MAX_CLOSE_WAIT;
103-
while(_currentClient.connected() && maxWait--) {
104-
delay(1);
89+
setContentLength(file.size());
90+
if (String(file.name()).endsWith(".gz") &&
91+
contentType != "application/x-gzip" &&
92+
contentType != "application/octet-stream"){
93+
sendHeader("Content-Encoding", "gzip");
10594
}
106-
return res;
95+
send(200, contentType, "");
96+
return _currentClient.write(file, HTTP_DOWNLOAD_UNIT_SIZE);
10797
}
10898

10999
protected:
@@ -131,6 +121,7 @@ template<typename T> size_t streamFile(T &file, String contentType){
131121
RequestArgument* _currentArgs;
132122
HTTPUpload _currentUpload;
133123

124+
size_t _contentLength;
134125
String _responseHeaders;
135126

136127
RequestHandler* _firstHandler;

0 commit comments

Comments
 (0)