Skip to content

Commit b658d62

Browse files
JosePineiromathieucarbouCopilotpre-commit-ci-lite[bot]
authored
Refactor AsyncFileResponse: remove String _path (#235)
* Refactor AsyncFileResponse: remove String _path This refactor eliminates the private String _path member from the AsyncFileResponse class, replacing its functionality with a fixed-size char gzPath[MAX_PATH] buffer insife of function. The use of Arduino's dynamic String class has known drawbacks in memory-constrained or real-time systems, particularly due to heap fragmentation and unpredictable allocation behavior. By switching to a statically allocated C-style buffer, the implementation now benefits from improved memory determinism, reduced fragmentation risk, and overall lower memory overhead. In addition to reducing memory fragmentation and improving stability in embedded environments, this change also yields better execution speed. This change maintains behavioral equivalence. * Change for use arduino String * Implemented @mathieucarbou's suggestion Thank you very much for indicating compatibility with other devices. * Add files via upload * Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update src/WebResponses.cpp * ci(pre-commit): Apply automatic fixes --------- Co-authored-by: Mathieu Carbou <mathieu.carbou@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent 18ee933 commit b658d62

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

src/WebResponseImpl.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ class AsyncFileResponse : public AsyncAbstractResponse {
7575

7676
private:
7777
File _content;
78-
String _path;
7978
void _setContentTypeFromPath(const String &path);
8079

8180
public:

src/WebResponses.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -696,15 +696,19 @@ void AsyncFileResponse::_setContentTypeFromPath(const String &path) {
696696
*/
697697
AsyncFileResponse::AsyncFileResponse(FS &fs, const String &path, const char *contentType, bool download, AwsTemplateProcessor callback)
698698
: AsyncAbstractResponse(callback) {
699+
699700
// Try to open the uncompressed version first
700701
_content = fs.open(path, fs::FileOpenMode::read);
701702
if (_content.available()) {
702-
_path = path;
703703
_contentLength = _content.size();
704704
} else {
705705
// Try to open the compressed version (.gz)
706-
_path = path + asyncsrv::T__gz;
707-
_content = fs.open(_path, fs::FileOpenMode::read);
706+
String gzPath;
707+
uint16_t pathLen = path.length();
708+
gzPath.reserve(pathLen + 3);
709+
gzPath.concat(path);
710+
gzPath.concat(asyncsrv::T__gz);
711+
_content = fs.open(gzPath, fs::FileOpenMode::read);
708712
_contentLength = _content.size();
709713

710714
if (_content.seek(_contentLength - 8)) {
@@ -753,7 +757,6 @@ AsyncFileResponse::AsyncFileResponse(FS &fs, const String &path, const char *con
753757
AsyncFileResponse::AsyncFileResponse(File content, const String &path, const char *contentType, bool download, AwsTemplateProcessor callback)
754758
: AsyncAbstractResponse(callback) {
755759
_code = 200;
756-
_path = path;
757760

758761
if (!download && String(content.name()).endsWith(T__gz) && !path.endsWith(T__gz)) {
759762
addHeader(T_Content_Encoding, T_gzip, false);

0 commit comments

Comments
 (0)