Skip to content

Commit 2773653

Browse files
JosePineiroCopilotpre-commit-ci-lite[bot]mathieucarbou
authored
feature: refactor content type and MIME list (#241)
* feature: refactor content type and MIME list This pull request refactors the _setContentTypeFromPath method in AsyncFileResponse to improve maintainability and performance in MIME type detection. Key changes: - More accurate fallback behavior, switching the default MIME type from text/plain to application/octet-stream for unknown file types. - Formats Added: .txt → text/plain .opus → audio/opus (great compression, open and support in all modern browsers) .webm → video/webm (great compression, open and support in all modern browsers) - Formats Removed: .zip → application/zip (send application/octet-stream. Browsers will always download it and not display it on the screen.) .gz → application/x-gzip (send application/octet-stream. Browsers will always download it and not display it on the screen.) .eot → font/eot (Font file. Obsolete, no longer supported in modern browsers.) .ttf → font/ttf (Size it is too large for embedded systems. WOLF2 should be used.) The update brings the function more in line with modern web and IoT usage while maintaining backward compatibility with legacy formats where still relevant. Preguntar a ChatGPT * Update src/WebResponses.cpp Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Add files via upload Remove autoMINE extensions: zip, gz y eot Add extensions: csv, opus, txt, webm * Add files via upload * ci(pre-commit): Apply automatic fixes --------- 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> Co-authored-by: Mathieu Carbou <mathieu.carbou@gmail.com>
1 parent ec133bb commit 2773653

2 files changed

Lines changed: 38 additions & 34 deletions

File tree

src/WebResponses.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ void AsyncFileResponse::_setContentTypeFromPath(const String &path) {
631631
const char *dot = strrchr(cpath, '.');
632632

633633
if (!dot) {
634-
_contentType = T_text_plain;
634+
_contentType = T_application_octet_stream;
635635
return;
636636
}
637637

@@ -663,20 +663,20 @@ void AsyncFileResponse::_setContentTypeFromPath(const String &path) {
663663
_contentType = T_font_woff;
664664
} else if (strcmp(dot, T__ttf) == 0) {
665665
_contentType = T_font_ttf;
666-
} else if (strcmp(dot, T__eot) == 0) {
667-
_contentType = T_font_eot;
668666
} else if (strcmp(dot, T__xml) == 0) {
669667
_contentType = T_text_xml;
670668
} else if (strcmp(dot, T__pdf) == 0) {
671669
_contentType = T_application_pdf;
672670
} else if (strcmp(dot, T__mp4) == 0) {
673671
_contentType = T_video_mp4;
674-
} else if (strcmp(dot, T__zip) == 0) {
675-
_contentType = T_application_zip;
676-
} else if (strcmp(dot, T__gz) == 0) {
677-
_contentType = T_application_x_gzip;
678-
} else {
672+
} else if (strcmp(dot, T__opus) == 0) {
673+
_contentType = T_audio_opus;
674+
} else if (strcmp(dot, T__webm) == 0) {
675+
_contentType = T_video_webm;
676+
} else if (strcmp(dot, T__txt) == 0) {
679677
_contentType = T_text_plain;
678+
} else {
679+
_contentType = T_application_octet_stream;
680680
}
681681
#endif
682682
}

src/literals.h

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -103,35 +103,36 @@ static constexpr const char *T_RCT_EVENT = "RCT_EVENT";
103103
static constexpr const char *T_ERROR = "ERROR";
104104

105105
// extensions & MIME-Types
106-
static constexpr const char *T__avif = ".avif";
107-
static constexpr const char *T__css = ".css";
108-
static constexpr const char *T__eot = ".eot";
109-
static constexpr const char *T__gif = ".gif";
110-
static constexpr const char *T__gz = ".gz";
111-
static constexpr const char *T__htm = ".htm";
112-
static constexpr const char *T__html = ".html";
113-
static constexpr const char *T__ico = ".ico";
114-
static constexpr const char *T__jpg = ".jpg";
115-
static constexpr const char *T__js = ".js";
116-
static constexpr const char *T__json = ".json";
117-
static constexpr const char *T__mp4 = ".mp4";
118-
static constexpr const char *T__pdf = ".pdf";
119-
static constexpr const char *T__png = ".png";
120-
static constexpr const char *T__svg = ".svg";
121-
static constexpr const char *T__ttf = ".ttf";
122-
static constexpr const char *T__webp = ".webp";
123-
static constexpr const char *T__woff = ".woff";
124-
static constexpr const char *T__woff2 = ".woff2";
125-
static constexpr const char *T__xml = ".xml";
126-
static constexpr const char *T__zip = ".zip";
127-
static constexpr const char *T_application_javascript = "application/javascript";
106+
static constexpr const char *T__avif = ".avif"; // AVIF: Highly compressed images. Compatible with all modern browsers.
107+
static constexpr const char *T__csv = ".csv"; // CSV: Data logging and configuration
108+
static constexpr const char *T__css = ".css"; // CSS: Styling for web interfaces
109+
static constexpr const char *T__gif = ".gif"; // GIF: Simple animations. Legacy support
110+
static constexpr const char *T__gz = ".gz"; // GZ: compressed files
111+
static constexpr const char *T__htm = ".htm"; // HTM: Web interface files
112+
static constexpr const char *T__html = ".html"; // HTML: Web interface files
113+
static constexpr const char *T__ico = ".ico"; // ICO: Favicons, system icons. Legacy support
114+
static constexpr const char *T__jpg = ".jpg"; // JPEG/JPG: Photos. Legacy support
115+
static constexpr const char *T__js = ".js"; // JavaScript: Interactive functionality
116+
static constexpr const char *T__json = ".json"; // JSON: Data exchange format
117+
static constexpr const char *T__mp4 = ".mp4"; // MP4: Proprietary format. Worse compression than WEBM.
118+
static constexpr const char *T__opus = ".opus"; // OPUS: High compression audio format
119+
static constexpr const char *T__pdf = ".pdf"; // PDF: Universal document format
120+
static constexpr const char *T__png = ".png"; // PNG: Icons, logos, transparency. Legacy support
121+
static constexpr const char *T__svg = ".svg"; // SVG: Vector graphics, icons (scalable, tiny file sizes)
122+
static constexpr const char *T__ttf = ".ttf"; // TTF: Font file. Legacy support
123+
static constexpr const char *T__txt = ".txt"; // TXT: Plain text files
124+
static constexpr const char *T__webm = ".webm"; // WebM: Video. Open source, optimized for web. Compatible with all modern browsers.
125+
static constexpr const char *T__webp = ".webp"; // WebP: Highly compressed images. Compatible with all modern browsers.
126+
static constexpr const char *T__woff = ".woff"; // WOFF: Font file. Legacy support
127+
static constexpr const char *T__woff2 = ".woff2"; // WOFF2: Better compression. Compatible with all modern browsers.
128+
static constexpr const char *T__xml = ".xml"; // XML: Configuration and data files
129+
static constexpr const char *T_application_javascript = "application/javascript"; // Obsolete type for JavaScript
128130
static constexpr const char *T_application_json = "application/json";
129131
static constexpr const char *T_application_msgpack = "application/msgpack";
132+
static constexpr const char *T_application_octet_stream = "application/octet-stream";
130133
static constexpr const char *T_application_pdf = "application/pdf";
131-
static constexpr const char *T_application_x_gzip = "application/x-gzip";
132134
static constexpr const char *T_app_xform_urlencoded = "application/x-www-form-urlencoded";
133-
static constexpr const char *T_application_zip = "application/zip";
134-
static constexpr const char *T_font_eot = "font/eot";
135+
static constexpr const char *T_audio_opus = "audio/opus";
135136
static constexpr const char *T_font_ttf = "font/ttf";
136137
static constexpr const char *T_font_woff = "font/woff";
137138
static constexpr const char *T_font_woff2 = "font/woff2";
@@ -143,11 +144,14 @@ static constexpr const char *T_image_svg_xml = "image/svg+xml";
143144
static constexpr const char *T_image_webp = "image/webp";
144145
static constexpr const char *T_image_x_icon = "image/x-icon";
145146
static constexpr const char *T_text_css = "text/css";
147+
static constexpr const char *T_text_csv = "text/csv";
146148
static constexpr const char *T_text_event_stream = "text/event-stream";
147149
static constexpr const char *T_text_html = "text/html";
150+
static constexpr const char *T_text_javascript = "text/javascript";
148151
static constexpr const char *T_text_plain = "text/plain";
149-
static constexpr const char *T_video_mp4 = "video/mp4";
150152
static constexpr const char *T_text_xml = "text/xml";
153+
static constexpr const char *T_video_mp4 = "video/mp4";
154+
static constexpr const char *T_video_webm = "video/webm";
151155

152156
// Response codes
153157
static constexpr const char *T_HTTP_CODE_100 = "Continue";

0 commit comments

Comments
 (0)