mirror of
https://github.com/esp8266/Arduino.git
synced 2025-04-19 23:22:16 +03:00
Add setContentLength method to web server, update examples
related to #304
This commit is contained in:
parent
21d50e104c
commit
8fdb824e11
@ -106,36 +106,54 @@ void handleFileUpdate(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void handleFileDelete(){
|
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);
|
String path = server.arg(0);
|
||||||
if(path == "/")
|
if(path == "/") {
|
||||||
return server.send(500, "text/plain", "BAD PATH");
|
server.send(500, "text/plain", "BAD PATH");
|
||||||
if(!FS.exists((char *)(path.c_str())))
|
return;
|
||||||
return server.send(404, "text/plain", "FileNotFound");
|
}
|
||||||
|
if(!FS.exists((char *)(path.c_str()))) {
|
||||||
|
server.send(404, "text/plain", "FileNotFound");
|
||||||
|
return;
|
||||||
|
}
|
||||||
FS.remove((char *)path.c_str());
|
FS.remove((char *)path.c_str());
|
||||||
server.send(200, "text/plain", "");
|
server.send(200, "text/plain", "");
|
||||||
path = String();
|
path = String();
|
||||||
}
|
}
|
||||||
|
|
||||||
void handleFileCreate(){
|
void handleFileCreate(){
|
||||||
if(server.args() == 0)
|
if(server.args() == 0) {
|
||||||
return server.send(500, "text/plain", "BAD ARGS");
|
server.send(500, "text/plain", "BAD ARGS");
|
||||||
|
return;
|
||||||
|
}
|
||||||
String path = server.arg(0);
|
String path = server.arg(0);
|
||||||
if(path == "/")
|
if(path == "/") {
|
||||||
return server.send(500, "text/plain", "BAD PATH");
|
server.send(500, "text/plain", "BAD PATH");
|
||||||
if(FS.exists((char *)path.c_str()))
|
return;
|
||||||
return server.send(500, "text/plain", "FILE EXISTS");
|
}
|
||||||
|
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);
|
FSFile file = FS.open((char *)path.c_str(), FSFILE_OVERWRITE);
|
||||||
if(file)
|
if(file)
|
||||||
file.close();
|
file.close();
|
||||||
else
|
else {
|
||||||
return server.send(500, "text/plain", "CREATE FAILED");
|
server.send(500, "text/plain", "CREATE FAILED");
|
||||||
|
return;
|
||||||
|
}
|
||||||
server.send(200, "text/plain", "");
|
server.send(200, "text/plain", "");
|
||||||
path = String();
|
path = String();
|
||||||
}
|
}
|
||||||
|
|
||||||
void handleFileList() {
|
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");
|
String path = server.arg("dir");
|
||||||
|
|
||||||
FSFile entry;
|
FSFile entry;
|
||||||
@ -148,19 +166,21 @@ void handleFileList() {
|
|||||||
}
|
}
|
||||||
dir.rewindDirectory();
|
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 = "[";
|
String output = "[";
|
||||||
while(true){
|
while(true){
|
||||||
entry = dir.openNextFile();
|
entry = dir.openNextFile();
|
||||||
if (!entry) break;
|
if (!entry)
|
||||||
|
break;
|
||||||
|
|
||||||
if(!FS.exists(entry.name())){
|
if(!FS.exists(entry.name())){
|
||||||
os_printf("Entry[%s] Not Exists!\n", entry.name());
|
os_printf("Entry[%s] Not Exists!\n", entry.name());
|
||||||
entry.remove();
|
entry.remove();
|
||||||
entry.close();
|
entry.close();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if(output != "[") output += ',';
|
|
||||||
|
if(output != "[")
|
||||||
|
output += ',';
|
||||||
output += "{\"type\":\"";
|
output += "{\"type\":\"";
|
||||||
output += (entry.isDirectory())?"dir":"file";
|
output += (entry.isDirectory())?"dir":"file";
|
||||||
output += "\",\"name\":\"";
|
output += "\",\"name\":\"";
|
||||||
@ -171,12 +191,7 @@ void handleFileList() {
|
|||||||
dir.close();
|
dir.close();
|
||||||
|
|
||||||
output += "]";
|
output += "]";
|
||||||
client.write(output.c_str(), output.length());
|
server.send(200, "text/json", output);
|
||||||
output = String();
|
|
||||||
uint16_t maxWait = HTTP_MAX_CLOSE_WAIT;
|
|
||||||
while(client.connected() && maxWait--) {
|
|
||||||
delay(1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup(void){
|
void setup(void){
|
||||||
|
@ -87,14 +87,7 @@ bool loadFromSdCard(String path){
|
|||||||
|
|
||||||
if (server.hasArg("download")) dataType = "application/octet-stream";
|
if (server.hasArg("download")) dataType = "application/octet-stream";
|
||||||
|
|
||||||
server.sendHeader("Content-Length", String(dataFile.size()));
|
if (server.streamFile(dataFile, dataType) != 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) {
|
|
||||||
DBG_OUTPUT_PORT.println("Sent less data than expected!");
|
DBG_OUTPUT_PORT.println("Sent less data than expected!");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -187,7 +180,7 @@ void printDirectory() {
|
|||||||
return returnFail("NOT DIR");
|
return returnFail("NOT DIR");
|
||||||
}
|
}
|
||||||
dir.rewindDirectory();
|
dir.rewindDirectory();
|
||||||
|
server.setContentSize(CONTENT_SIZE_UNKNOWN);
|
||||||
server.send(200, "text/json", "");
|
server.send(200, "text/json", "");
|
||||||
WiFiClient client = server.client();
|
WiFiClient client = server.client();
|
||||||
|
|
||||||
|
@ -111,6 +111,7 @@ void ESP8266WebServer::handleClient()
|
|||||||
}
|
}
|
||||||
|
|
||||||
_currentClient = client;
|
_currentClient = client;
|
||||||
|
_contentLength = CONTENT_LENGTH_NOT_SET;
|
||||||
_handleRequest();
|
_handleRequest();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,9 +139,13 @@ 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());
|
if (_contentLength != CONTENT_LENGTH_UNKNOWN) {
|
||||||
|
size_t length = (_contentLength == CONTENT_LENGTH_NOT_SET) ?
|
||||||
|
content.length() : _contentLength;
|
||||||
|
String lengthStr(length);
|
||||||
|
sendHeader("Content-Length", lengthStr.c_str());
|
||||||
|
}
|
||||||
sendHeader("Connection", "close");
|
sendHeader("Connection", "close");
|
||||||
sendHeader("Access-Control-Allow-Origin", "*");
|
sendHeader("Access-Control-Allow-Origin", "*");
|
||||||
|
|
||||||
|
@ -34,6 +34,9 @@ enum HTTPUploadStatus { UPLOAD_FILE_START, UPLOAD_FILE_WRITE, UPLOAD_FILE_END };
|
|||||||
#define HTTP_MAX_DATA_WAIT 1000 //ms to wait for the client to send the request
|
#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
|
#define HTTP_MAX_CLOSE_WAIT 2000 //ms to wait for the client to close the connection
|
||||||
|
|
||||||
|
#define CONTENT_LENGTH_UNKNOWN ((size_t) -1)
|
||||||
|
#define CONTENT_LENGTH_NOT_SET ((size_t) -2)
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
HTTPUploadStatus status;
|
HTTPUploadStatus status;
|
||||||
String filename;
|
String filename;
|
||||||
@ -78,32 +81,19 @@ public:
|
|||||||
void send(int code, char* content_type, String content);
|
void send(int code, char* content_type, String content);
|
||||||
void send(int code, String content_type, String content);
|
void send(int code, String content_type, String content);
|
||||||
|
|
||||||
|
void setContentLength(size_t contentLength) { _contentLength = contentLength; }
|
||||||
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);
|
||||||
|
|
||||||
template<typename T> size_t streamFile(T &file, String contentType){
|
template<typename T> size_t streamFile(T &file, String contentType){
|
||||||
String head = "HTTP/1.1 200 OK\r\nContent-Type: ";
|
setContentLength(file.size());
|
||||||
head += contentType;
|
if (String(file.name()).endsWith(".gz") &&
|
||||||
head += "\r\nContent-Length: ";
|
|
||||||
head += file.size();
|
|
||||||
head += "\r\nConnection: close";
|
|
||||||
head += "\r\nAccess-Control-Allow-Origin: *";
|
|
||||||
if(
|
|
||||||
String(file.name()).endsWith(".gz") &&
|
|
||||||
contentType != "application/x-gzip" &&
|
contentType != "application/x-gzip" &&
|
||||||
contentType != "application/octet-stream"
|
contentType != "application/octet-stream"){
|
||||||
){
|
sendHeader("Content-Encoding", "gzip");
|
||||||
head += "\r\nContent-Encoding: gzip";
|
|
||||||
}
|
}
|
||||||
head += "\r\n\r\n";
|
send(200, contentType, "");
|
||||||
_currentClient.print(head);
|
return _currentClient.write(file, HTTP_DOWNLOAD_UNIT_SIZE);
|
||||||
head = String();
|
|
||||||
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:
|
||||||
@ -131,6 +121,7 @@ protected:
|
|||||||
RequestArgument* _currentArgs;
|
RequestArgument* _currentArgs;
|
||||||
HTTPUpload _currentUpload;
|
HTTPUpload _currentUpload;
|
||||||
|
|
||||||
|
size_t _contentLength;
|
||||||
String _responseHeaders;
|
String _responseHeaders;
|
||||||
|
|
||||||
RequestHandler* _firstHandler;
|
RequestHandler* _firstHandler;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user