Skip to content

Refactor AsyncFileResponse: remove String _path #235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/WebResponseImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ class AsyncFileResponse : public AsyncAbstractResponse {

private:
File _content;
String _path;
void _setContentTypeFromPath(const String &path);

public:
Expand Down
15 changes: 10 additions & 5 deletions src/WebResponses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -701,15 +701,21 @@ void AsyncFileResponse::_setContentTypeFromPath(const String &path) {
*/
AsyncFileResponse::AsyncFileResponse(FS &fs, const String &path, const char *contentType, bool download, AwsTemplateProcessor callback)
: AsyncAbstractResponse(callback) {
constexpr size_t MAX_PATH = 128;
const size_t GZ_SUFFIX_LEN = sizeof(T__gz);

// Try to open the uncompressed version first
_content = fs.open(path, fs::FileOpenMode::read);
if (_content.available()) {
_path = path;
_contentLength = _content.size();
} else {
// Try to open the compressed version (.gz)
_path = path + asyncsrv::T__gz;
_content = fs.open(_path, fs::FileOpenMode::read);
size_t pathLen = path.length();
char gzPath[MAX_PATH]; // Safe buffer size for ESP32 paths
memcpy(gzPath, path.c_str(), pathLen);
memcpy(gzPath + pathLen, asyncsrv::T__gz, GZ_SUFFIX_LEN);

_content = fs.open(gzPath, fs::FileOpenMode::read);
_contentLength = _content.size();

if (_content.seek(_contentLength - 8)) {
Expand Down Expand Up @@ -758,8 +764,7 @@ AsyncFileResponse::AsyncFileResponse(FS &fs, const String &path, const char *con
AsyncFileResponse::AsyncFileResponse(File content, const String &path, const char *contentType, bool download, AwsTemplateProcessor callback)
: AsyncAbstractResponse(callback) {
_code = 200;
_path = path;


if (!download && String(content.name()).endsWith(T__gz) && !path.endsWith(T__gz)) {
addHeader(T_Content_Encoding, T_gzip, false);
_callback = nullptr; // Unable to process gzipped templates
Expand Down