provide original name so proper content type is set for gzipped content

This commit is contained in:
Me No Dev
2016-06-17 02:23:18 +03:00
parent d8809137a3
commit dc5b7f708a
5 changed files with 17 additions and 11 deletions

View File

@@ -186,14 +186,14 @@ class AsyncWebServerRequest {
void send(AsyncWebServerResponse *response); void send(AsyncWebServerResponse *response);
void send(int code, String contentType=String(), String content=String()); void send(int code, String contentType=String(), String content=String());
void send(FS &fs, String path, String contentType=String(), bool download=false); void send(FS &fs, String path, String contentType=String(), bool download=false);
void send(File content, String contentType=String(), bool download=false); void send(File content, String path, String contentType=String(), bool download=false);
void send(Stream &stream, String contentType, size_t len); void send(Stream &stream, String contentType, size_t len);
void send(String contentType, size_t len, AwsResponseFiller callback); void send(String contentType, size_t len, AwsResponseFiller callback);
void sendChunked(String contentType, AwsResponseFiller callback); void sendChunked(String contentType, AwsResponseFiller callback);
AsyncWebServerResponse *beginResponse(int code, String contentType=String(), String content=String()); AsyncWebServerResponse *beginResponse(int code, String contentType=String(), String content=String());
AsyncWebServerResponse *beginResponse(FS &fs, String path, String contentType=String(), bool download=false); AsyncWebServerResponse *beginResponse(FS &fs, String path, String contentType=String(), bool download=false);
AsyncWebServerResponse *beginResponse(File content, String contentType=String(), bool download=false); AsyncWebServerResponse *beginResponse(File content, String path, String contentType=String(), bool download=false);
AsyncWebServerResponse *beginResponse(Stream &stream, String contentType, size_t len); AsyncWebServerResponse *beginResponse(Stream &stream, String contentType, size_t len);
AsyncWebServerResponse *beginResponse(String contentType, size_t len, AwsResponseFiller callback); AsyncWebServerResponse *beginResponse(String contentType, size_t len, AwsResponseFiller callback);
AsyncWebServerResponse *beginChunkedResponse(String contentType, AwsResponseFiller callback); AsyncWebServerResponse *beginChunkedResponse(String contentType, AwsResponseFiller callback);

View File

@@ -106,6 +106,10 @@ bool AsyncStaticWebHandler::_fileExists(AsyncWebServerRequest *request, const St
bool found = fileFound || gzipFound; bool found = fileFound || gzipFound;
if (found) { if (found) {
size_t plen = path.length();
char * _tempPath = (char*)malloc(plen+1);
snprintf(_tempPath, plen+1, "%s", path.c_str());
request->_tempObject = (void*)_tempPath;
_gzipStats = (_gzipStats << 1) + gzipFound ? 1 : 0; _gzipStats = (_gzipStats << 1) + gzipFound ? 1 : 0;
_fileStats = (_fileStats << 1) + fileFound ? 1 : 0; _fileStats = (_fileStats << 1) + fileFound ? 1 : 0;
_gzipFirst = _countBits(_gzipStats) > _countBits(_fileStats); _gzipFirst = _countBits(_gzipStats) > _countBits(_fileStats);
@@ -125,7 +129,9 @@ uint8_t AsyncStaticWebHandler::_countBits(const uint8_t value)
void AsyncStaticWebHandler::handleRequest(AsyncWebServerRequest *request) void AsyncStaticWebHandler::handleRequest(AsyncWebServerRequest *request)
{ {
if (request->_tempFile == true) { if (request->_tempFile == true) {
AsyncWebServerResponse * response = new AsyncFileResponse(request->_tempFile); AsyncWebServerResponse * response = new AsyncFileResponse(request->_tempFile, String((char*)request->_tempObject));
free(request->_tempObject);
request->_tempObject = NULL;
if (_cache_header.length() != 0) if (_cache_header.length() != 0)
response->addHeader("Cache-Control", _cache_header); response->addHeader("Cache-Control", _cache_header);
request->send(response); request->send(response);

View File

@@ -651,9 +651,9 @@ AsyncWebServerResponse * AsyncWebServerRequest::beginResponse(FS &fs, String pat
return NULL; return NULL;
} }
AsyncWebServerResponse * AsyncWebServerRequest::beginResponse(File content, String contentType, bool download){ AsyncWebServerResponse * AsyncWebServerRequest::beginResponse(File content, String path, String contentType, bool download){
if(content == true) if(content == true)
return new AsyncFileResponse(content, contentType, download); return new AsyncFileResponse(content, path, contentType, download);
return NULL; return NULL;
} }
@@ -685,9 +685,9 @@ void AsyncWebServerRequest::send(FS &fs, String path, String contentType, bool d
} else send(404); } else send(404);
} }
void AsyncWebServerRequest::send(File content, String contentType, bool download){ void AsyncWebServerRequest::send(File content, String path, String contentType, bool download){
if(content == true){ if(content == true){
send(beginResponse(content, contentType, download)); send(beginResponse(content, path, contentType, download));
} else send(404); } else send(404);
} }

View File

@@ -48,7 +48,7 @@ class AsyncFileResponse: public AsyncAbstractResponse {
void _setContentType(String path); void _setContentType(String path);
public: public:
AsyncFileResponse(FS &fs, String path, String contentType=String(), bool download=false); AsyncFileResponse(FS &fs, String path, String contentType=String(), bool download=false);
AsyncFileResponse(File content, String contentType=String(), bool download=false); AsyncFileResponse(File content, String path, String contentType=String(), bool download=false);
~AsyncFileResponse(); ~AsyncFileResponse();
bool _sourceValid(){ return !!(_content); } bool _sourceValid(){ return !!(_content); }
size_t _fillBuffer(uint8_t *buf, size_t maxLen); size_t _fillBuffer(uint8_t *buf, size_t maxLen);

View File

@@ -376,16 +376,16 @@ AsyncFileResponse::AsyncFileResponse(FS &fs, String path, String contentType, bo
_contentLength = _content.size(); _contentLength = _content.size();
} }
AsyncFileResponse::AsyncFileResponse(File content, String contentType, bool download){ AsyncFileResponse::AsyncFileResponse(File content, String path, String contentType, bool download){
_code = 200; _code = 200;
_content = content; _content = content;
_path = String(_content.name()); _path = path;
_contentLength = _content.size(); _contentLength = _content.size();
int filenameStart = _path.lastIndexOf('/') + 1; int filenameStart = _path.lastIndexOf('/') + 1;
char buf[26+_path.length()-filenameStart]; char buf[26+_path.length()-filenameStart];
char* filename = (char*)_path.c_str() + filenameStart; char* filename = (char*)_path.c_str() + filenameStart;
if(!download && _path.endsWith(".gz")) if(!download && String(_content.name()).endsWith(".gz"))
addHeader("Content-Encoding", "gzip"); addHeader("Content-Encoding", "gzip");
if(contentType == "") if(contentType == "")