From 3d8a14197292e89086251b63d5bc3efbae62eb5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=20M=2E=20Pi=C3=B1eiro?= Date: Tue, 15 Jul 2025 21:00:34 +0200 Subject: [PATCH 1/2] Fix bug. Sometimes the content type isn't sent. Fixes an regresive bug where Content-Type was not properly set when serving files, which specifically affected Firefox when using "view page source" functionality. The issue primarily manifested in Firefox's view-source feature, but the fix ensures consistent behavior across all browsers and use cases. Changes: Fixed missing contentType headers Optimized string handling by replacing PSTR() with static constant - Optimized string handling by replacing snprintf_P with snprintf - Changed from dynamic buffer formatting to direct header addition using static template - Improved memory efficiency and code readability --- src/AsyncWebServerRequest.cpp | 2 +- src/WebResponses.cpp | 6 +++--- src/literals.h | 3 +++ 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/AsyncWebServerRequest.cpp b/src/AsyncWebServerRequest.cpp index fbe662f72..18b4da059 100644 --- a/src/AsyncWebServerRequest.cpp +++ b/src/AsyncWebServerRequest.cpp @@ -27,7 +27,7 @@ void AsyncWebServerRequest::send(FS &fs, const String &path, const char *content // Handle compressed version const String gzPath = path + asyncsrv::T__gz; - File gzFile = fs.open(gzPath, "r"); + File gzFile = fs.open(gzPath, fs::FileOpenMode::read); // Compressed file not found or invalid if (!gzFile.seek(gzFile.size() - 8)) { diff --git a/src/WebResponses.cpp b/src/WebResponses.cpp index 3b3fb6ceb..2f54e7426 100644 --- a/src/WebResponses.cpp +++ b/src/WebResponses.cpp @@ -734,7 +734,7 @@ AsyncFileResponse::AsyncFileResponse(FS &fs, const String &path, const char *con } } - if (*contentType != '\0') { + if (*contentType == '\0') { _setContentTypeFromPath(path); } else { _contentType = contentType; @@ -745,11 +745,11 @@ AsyncFileResponse::AsyncFileResponse(FS &fs, const String &path, const char *con int filenameStart = path.lastIndexOf('/') + 1; char buf[26 + path.length() - filenameStart]; char *filename = (char *)path.c_str() + filenameStart; - snprintf_P(buf, sizeof(buf), PSTR("attachment; filename=\"%s\""), filename); + snprintf(buf, sizeof(buf), T_attachment, filename); addHeader(T_Content_Disposition, buf, false); } else { // Serve file inline (display in browser) - addHeader(T_Content_Disposition, PSTR("inline"), false); + addHeader(T_Content_Disposition, T_inline, false); } _code = 200; diff --git a/src/literals.h b/src/literals.h index a69f78b47..ff73e61dd 100644 --- a/src/literals.h +++ b/src/literals.h @@ -7,6 +7,9 @@ namespace asyncsrv { static constexpr const char *empty = ""; +static const char T_inline[] = "inline"; +static const char T_attachment[] = "attachment; filename=\"%s\""; + static constexpr const char *T__opaque = "\", opaque=\""; static constexpr const char *T_100_CONTINUE = "100-continue"; static constexpr const char *T_13 = "13"; From c19a64c9520621d215dea83d90057a35e587e97a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=20M=2E=20Pi=C3=B1eiro?= Date: Tue, 15 Jul 2025 23:48:38 +0200 Subject: [PATCH 2/2] Update src/literals.h Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/literals.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/literals.h b/src/literals.h index ff73e61dd..b3ea800d5 100644 --- a/src/literals.h +++ b/src/literals.h @@ -7,8 +7,8 @@ namespace asyncsrv { static constexpr const char *empty = ""; -static const char T_inline[] = "inline"; -static const char T_attachment[] = "attachment; filename=\"%s\""; +static constexpr const char *T_inline = "inline"; +static constexpr const char *T_attachment = "attachment; filename=\"%s\""; static constexpr const char *T__opaque = "\", opaque=\""; static constexpr const char *T_100_CONTINUE = "100-continue";