1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-12 01:53:07 +03:00

Add setContentLength method to web server, update examples

related to #304
This commit is contained in:
Ivan Grokhotkov
2015-05-22 17:57:30 +03:00
parent 21d50e104c
commit 8fdb824e11
4 changed files with 62 additions and 58 deletions

View File

@ -106,36 +106,54 @@ void handleFileUpdate(){
}
void handleFileDelete(){
if(server.args() == 0) return server.send(500, "text/plain", "BAD ARGS");
if(server.args() == 0) {
server.send(500, "text/plain", "BAD ARGS");
return;
}
String path = server.arg(0);
if(path == "/")
return server.send(500, "text/plain", "BAD PATH");
if(!FS.exists((char *)(path.c_str())))
return server.send(404, "text/plain", "FileNotFound");
if(path == "/") {
server.send(500, "text/plain", "BAD PATH");
return;
}
if(!FS.exists((char *)(path.c_str()))) {
server.send(404, "text/plain", "FileNotFound");
return;
}
FS.remove((char *)path.c_str());
server.send(200, "text/plain", "");
path = String();
}
void handleFileCreate(){
if(server.args() == 0)
return server.send(500, "text/plain", "BAD ARGS");
if(server.args() == 0) {
server.send(500, "text/plain", "BAD ARGS");
return;
}
String path = server.arg(0);
if(path == "/")
return server.send(500, "text/plain", "BAD PATH");
if(FS.exists((char *)path.c_str()))
return server.send(500, "text/plain", "FILE EXISTS");
if(path == "/") {
server.send(500, "text/plain", "BAD PATH");
return;
}
if(FS.exists((char *)path.c_str())) {
server.send(500, "text/plain", "FILE EXISTS");
return;
}
FSFile file = FS.open((char *)path.c_str(), FSFILE_OVERWRITE);
if(file)
file.close();
else
return server.send(500, "text/plain", "CREATE FAILED");
else {
server.send(500, "text/plain", "CREATE FAILED");
return;
}
server.send(200, "text/plain", "");
path = String();
}
void handleFileList() {
if(!server.hasArg("dir")) return server.send(500, "text/plain", "BAD ARGS");
if(!server.hasArg("dir")) {
server.send(500, "text/plain", "BAD ARGS");
return;
}
String path = server.arg("dir");
FSFile entry;
@ -148,19 +166,21 @@ void handleFileList() {
}
dir.rewindDirectory();
WiFiClient client = server.client();
client.print("HTTP/1.1 200 OK\r\nContent-Type: text/json\r\nConnection: close\r\n\r\n");
String output = "[";
while(true){
entry = dir.openNextFile();
if (!entry) break;
if (!entry)
break;
if(!FS.exists(entry.name())){
os_printf("Entry[%s] Not Exists!\n", entry.name());
entry.remove();
entry.close();
continue;
}
if(output != "[") output += ',';
if(output != "[")
output += ',';
output += "{\"type\":\"";
output += (entry.isDirectory())?"dir":"file";
output += "\",\"name\":\"";
@ -169,14 +189,9 @@ void handleFileList() {
entry.close();
}
dir.close();
output += "]";
client.write(output.c_str(), output.length());
output = String();
uint16_t maxWait = HTTP_MAX_CLOSE_WAIT;
while(client.connected() && maxWait--) {
delay(1);
}
server.send(200, "text/json", output);
}
void setup(void){

View File

@ -85,16 +85,9 @@ bool loadFromSdCard(String path){
if (!dataFile)
return false;
if(server.hasArg("download")) dataType = "application/octet-stream";
if (server.hasArg("download")) dataType = "application/octet-stream";
server.sendHeader("Content-Length", String(dataFile.size()));
server.sendHeader("Connection", "close");
server.sendHeader("Access-Control-Allow-Origin", "*");
server.send(200, dataType.c_str(), "");
WiFiClient client = server.client();
size_t totalSize = dataFile.size();
if (client.write(dataFile, HTTP_DOWNLOAD_UNIT_SIZE) != totalSize) {
if (server.streamFile(dataFile, dataType) != dataFile.size()) {
DBG_OUTPUT_PORT.println("Sent less data than expected!");
}
@ -187,7 +180,7 @@ void printDirectory() {
return returnFail("NOT DIR");
}
dir.rewindDirectory();
server.setContentSize(CONTENT_SIZE_UNKNOWN);
server.send(200, "text/json", "");
WiFiClient client = server.client();