From dc5b7f708a667ee6cf83a054ef37cc4a31645054 Mon Sep 17 00:00:00 2001 From: Me No Dev Date: Fri, 17 Jun 2016 02:23:18 +0300 Subject: [PATCH] provide original name so proper content type is set for gzipped content --- src/ESPAsyncWebServer.h | 4 ++-- src/WebHandlers.cpp | 8 +++++++- src/WebRequest.cpp | 8 ++++---- src/WebResponseImpl.h | 2 +- src/WebResponses.cpp | 6 +++--- 5 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/ESPAsyncWebServer.h b/src/ESPAsyncWebServer.h index db4807b..9fe4873 100644 --- a/src/ESPAsyncWebServer.h +++ b/src/ESPAsyncWebServer.h @@ -186,14 +186,14 @@ class AsyncWebServerRequest { void send(AsyncWebServerResponse *response); void send(int code, String contentType=String(), String content=String()); 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(String contentType, size_t len, AwsResponseFiller callback); void sendChunked(String contentType, AwsResponseFiller callback); AsyncWebServerResponse *beginResponse(int code, String contentType=String(), String content=String()); 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(String contentType, size_t len, AwsResponseFiller callback); AsyncWebServerResponse *beginChunkedResponse(String contentType, AwsResponseFiller callback); diff --git a/src/WebHandlers.cpp b/src/WebHandlers.cpp index 5c64c61..94483ae 100644 --- a/src/WebHandlers.cpp +++ b/src/WebHandlers.cpp @@ -106,6 +106,10 @@ bool AsyncStaticWebHandler::_fileExists(AsyncWebServerRequest *request, const St bool found = fileFound || gzipFound; 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; _fileStats = (_fileStats << 1) + fileFound ? 1 : 0; _gzipFirst = _countBits(_gzipStats) > _countBits(_fileStats); @@ -125,7 +129,9 @@ uint8_t AsyncStaticWebHandler::_countBits(const uint8_t value) void AsyncStaticWebHandler::handleRequest(AsyncWebServerRequest *request) { 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) response->addHeader("Cache-Control", _cache_header); request->send(response); diff --git a/src/WebRequest.cpp b/src/WebRequest.cpp index 32d065e..ee8efcd 100644 --- a/src/WebRequest.cpp +++ b/src/WebRequest.cpp @@ -651,9 +651,9 @@ AsyncWebServerResponse * AsyncWebServerRequest::beginResponse(FS &fs, String pat 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) - return new AsyncFileResponse(content, contentType, download); + return new AsyncFileResponse(content, path, contentType, download); return NULL; } @@ -685,9 +685,9 @@ void AsyncWebServerRequest::send(FS &fs, String path, String contentType, bool d } 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){ - send(beginResponse(content, contentType, download)); + send(beginResponse(content, path, contentType, download)); } else send(404); } diff --git a/src/WebResponseImpl.h b/src/WebResponseImpl.h index e9e64ff..c7f539b 100644 --- a/src/WebResponseImpl.h +++ b/src/WebResponseImpl.h @@ -48,7 +48,7 @@ class AsyncFileResponse: public AsyncAbstractResponse { void _setContentType(String path); public: 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(); bool _sourceValid(){ return !!(_content); } size_t _fillBuffer(uint8_t *buf, size_t maxLen); diff --git a/src/WebResponses.cpp b/src/WebResponses.cpp index 60a3d3b..5650bbb 100644 --- a/src/WebResponses.cpp +++ b/src/WebResponses.cpp @@ -376,16 +376,16 @@ AsyncFileResponse::AsyncFileResponse(FS &fs, String path, String contentType, bo _contentLength = _content.size(); } -AsyncFileResponse::AsyncFileResponse(File content, String contentType, bool download){ +AsyncFileResponse::AsyncFileResponse(File content, String path, String contentType, bool download){ _code = 200; _content = content; - _path = String(_content.name()); + _path = path; _contentLength = _content.size(); int filenameStart = _path.lastIndexOf('/') + 1; char buf[26+_path.length()-filenameStart]; char* filename = (char*)_path.c_str() + filenameStart; - if(!download && _path.endsWith(".gz")) + if(!download && String(_content.name()).endsWith(".gz")) addHeader("Content-Encoding", "gzip"); if(contentType == "")