mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-13 13:01:55 +03:00
Add setContentLength method to web server, update examples
related to #304
This commit is contained in:
@ -111,6 +111,7 @@ void ESP8266WebServer::handleClient()
|
||||
}
|
||||
|
||||
_currentClient = client;
|
||||
_contentLength = CONTENT_LENGTH_NOT_SET;
|
||||
_handleRequest();
|
||||
}
|
||||
|
||||
@ -138,9 +139,13 @@ void ESP8266WebServer::send(int code, const char* content_type, String content)
|
||||
if (!content_type)
|
||||
content_type = "text/html";
|
||||
|
||||
String len(content.length());
|
||||
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("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_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 {
|
||||
HTTPUploadStatus status;
|
||||
String filename;
|
||||
@ -78,32 +81,19 @@ public:
|
||||
void send(int code, char* 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 sendContent(String content);
|
||||
|
||||
template<typename T> size_t streamFile(T &file, String contentType){
|
||||
String head = "HTTP/1.1 200 OK\r\nContent-Type: ";
|
||||
head += contentType;
|
||||
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/octet-stream"
|
||||
){
|
||||
head += "\r\nContent-Encoding: gzip";
|
||||
setContentLength(file.size());
|
||||
if (String(file.name()).endsWith(".gz") &&
|
||||
contentType != "application/x-gzip" &&
|
||||
contentType != "application/octet-stream"){
|
||||
sendHeader("Content-Encoding", "gzip");
|
||||
}
|
||||
head += "\r\n\r\n";
|
||||
_currentClient.print(head);
|
||||
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;
|
||||
send(200, contentType, "");
|
||||
return _currentClient.write(file, HTTP_DOWNLOAD_UNIT_SIZE);
|
||||
}
|
||||
|
||||
protected:
|
||||
@ -131,6 +121,7 @@ protected:
|
||||
RequestArgument* _currentArgs;
|
||||
HTTPUpload _currentUpload;
|
||||
|
||||
size_t _contentLength;
|
||||
String _responseHeaders;
|
||||
|
||||
RequestHandler* _firstHandler;
|
||||
|
Reference in New Issue
Block a user