Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 8 additions & 8 deletions src/WebResponses.cpp
Copy link
Member

@mathieucarbou mathieucarbou Jul 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Do not remove existing definitions
  • For the cases of CT change (like in 2022 for JS), add a new macro and do not change the existing definition to keep backward compatibility for users relying on the existing macros and its length.
  • IMO this is not the right way to speed up the comparison. Mime types are segmented by categories: application, text, image, etc. To speed up the comparison, first test if the content type starts with application/, text/, image/, etc. Then, for each one, you only test the second part of the content type. This will divide the number of comparisons to do way more and also avoid redoing each time the same comparisons for the categories.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extension removed:

  • .EOT. Only supported by Internet Explorer v9 to v11. Not supported by any current browser. Does it make sense to keep it?
  • .zip and .gz. MINE types are used to allow the browser to display files properly. All browsers download ZIP and GZ files, so the default mine type (application/octet-stream) is suitable for .zip and .gz.
    Having unnecessary extensions slows down your code, increases its size, and makes it harder to maintain. It's not a free breakfast.
    Do you still think it's worth keeping them?

I'll restore the old definition of for JS

I realize it's not the most efficient way to speed things up. But the cost is very low (I simply change the order) and the benefit is good. Do you think it's reasonable to leave it like this for now?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reordering the conditions is not something I would do because it just adds noise and more difficulties to review the diff.

I would do no reordering and instead to the correct code change to optimize the comparison by checking the mime category first and the the second part.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For content types: they should NEVER be linked to a file extension.

content types should be set by the user, depending on HOW the resource is used.

That’s totally ok to answer some js content with text/plain, like it is totally fine to serve an image with application/octet-stream. These are different usages on consumer side.

That being said, the server can assume some default content types for common static resources served on web pages like html, images, css, js, etc for example in the case of serveStatic because it assumes a standard use in a web browser.

And yes, defaulting to text/plain for some is bad, especially for binary content. Default should be application/octet-stream I think.

Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ void AsyncFileResponse::_setContentTypeFromPath(const String &path) {
const char *dot = strrchr(cpath, '.');

if (!dot) {
_contentType = T_text_plain;
_contentType = T_application_octet_stream;
return;
}

Expand Down Expand Up @@ -663,20 +663,20 @@ void AsyncFileResponse::_setContentTypeFromPath(const String &path) {
_contentType = T_font_woff;
} else if (strcmp(dot, T__ttf) == 0) {
_contentType = T_font_ttf;
} else if (strcmp(dot, T__eot) == 0) {
_contentType = T_font_eot;
} else if (strcmp(dot, T__xml) == 0) {
_contentType = T_text_xml;
} else if (strcmp(dot, T__pdf) == 0) {
_contentType = T_application_pdf;
} else if (strcmp(dot, T__mp4) == 0) {
_contentType = T_video_mp4;
} else if (strcmp(dot, T__zip) == 0) {
_contentType = T_application_zip;
} else if (strcmp(dot, T__gz) == 0) {
_contentType = T_application_x_gzip;
} else {
} else if (strcmp(dot, T__opus) == 0) {
_contentType = T_audio_opus;
} else if (strcmp(dot, T__webm) == 0) {
_contentType = T_video_webm;
} else if (strcmp(dot, T__txt) == 0) {
_contentType = T_text_plain;
} else {
_contentType = T_application_octet_stream;
}
#endif
}
Expand Down
56 changes: 30 additions & 26 deletions src/literals.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,35 +103,36 @@ static constexpr const char *T_RCT_EVENT = "RCT_EVENT";
static constexpr const char *T_ERROR = "ERROR";

// extensions & MIME-Types
static constexpr const char *T__avif = ".avif";
static constexpr const char *T__css = ".css";
static constexpr const char *T__eot = ".eot";
static constexpr const char *T__gif = ".gif";
static constexpr const char *T__gz = ".gz";
static constexpr const char *T__htm = ".htm";
static constexpr const char *T__html = ".html";
static constexpr const char *T__ico = ".ico";
static constexpr const char *T__jpg = ".jpg";
static constexpr const char *T__js = ".js";
static constexpr const char *T__json = ".json";
static constexpr const char *T__mp4 = ".mp4";
static constexpr const char *T__pdf = ".pdf";
static constexpr const char *T__png = ".png";
static constexpr const char *T__svg = ".svg";
static constexpr const char *T__ttf = ".ttf";
static constexpr const char *T__webp = ".webp";
static constexpr const char *T__woff = ".woff";
static constexpr const char *T__woff2 = ".woff2";
static constexpr const char *T__xml = ".xml";
static constexpr const char *T__zip = ".zip";
static constexpr const char *T_application_javascript = "application/javascript";
static constexpr const char *T__avif = ".avif"; // AVIF: Highly compressed images. Compatible with all modern browsers.
static constexpr const char *T__csv = ".csv"; // CSV: Data logging and configuration
static constexpr const char *T__css = ".css"; // CSS: Styling for web interfaces
static constexpr const char *T__gif = ".gif"; // GIF: Simple animations. Legacy support
static constexpr const char *T__gz = ".gz"; // GZ: compressed files
static constexpr const char *T__htm = ".htm"; // HTM: Web interface files
static constexpr const char *T__html = ".html"; // HTML: Web interface files
static constexpr const char *T__ico = ".ico"; // ICO: Favicons, system icons. Legacy support
static constexpr const char *T__jpg = ".jpg"; // JPEG/JPG: Photos. Legacy support
static constexpr const char *T__js = ".js"; // JavaScript: Interactive functionality
static constexpr const char *T__json = ".json"; // JSON: Data exchange format
static constexpr const char *T__mp4 = ".mp4"; // MP4: Proprietary format. Worse compression than WEBM.
static constexpr const char *T__opus = ".opus"; // OPUS: High compression audio format
static constexpr const char *T__pdf = ".pdf"; // PDF: Universal document format
static constexpr const char *T__png = ".png"; // PNG: Icons, logos, transparency. Legacy support
static constexpr const char *T__svg = ".svg"; // SVG: Vector graphics, icons (scalable, tiny file sizes)
static constexpr const char *T__ttf = ".ttf"; // TTF: Font file. Legacy support
static constexpr const char *T__txt = ".txt"; // TXT: Plain text files
static constexpr const char *T__webm = ".webm"; // WebM: Video. Open source, optimized for web. Compatible with all modern browsers.
static constexpr const char *T__webp = ".webp"; // WebP: Highly compressed images. Compatible with all modern browsers.
static constexpr const char *T__woff = ".woff"; // WOFF: Font file. Legacy support
static constexpr const char *T__woff2 = ".woff2"; // WOFF2: Better compression. Compatible with all modern browsers.
static constexpr const char *T__xml = ".xml"; // XML: Configuration and data files
static constexpr const char *T_application_javascript = "application/javascript"; // Obsolete type for JavaScript
static constexpr const char *T_application_json = "application/json";
static constexpr const char *T_application_msgpack = "application/msgpack";
static constexpr const char *T_application_octet_stream = "application/octet-stream";
static constexpr const char *T_application_pdf = "application/pdf";
static constexpr const char *T_application_x_gzip = "application/x-gzip";
static constexpr const char *T_app_xform_urlencoded = "application/x-www-form-urlencoded";
static constexpr const char *T_application_zip = "application/zip";
static constexpr const char *T_font_eot = "font/eot";
static constexpr const char *T_audio_opus = "audio/opus";
static constexpr const char *T_font_ttf = "font/ttf";
static constexpr const char *T_font_woff = "font/woff";
static constexpr const char *T_font_woff2 = "font/woff2";
Expand All @@ -143,11 +144,14 @@ static constexpr const char *T_image_svg_xml = "image/svg+xml";
static constexpr const char *T_image_webp = "image/webp";
static constexpr const char *T_image_x_icon = "image/x-icon";
static constexpr const char *T_text_css = "text/css";
static constexpr const char *T_text_csv = "text/csv";
static constexpr const char *T_text_event_stream = "text/event-stream";
static constexpr const char *T_text_html = "text/html";
static constexpr const char *T_text_javascript = "text/javascript";
static constexpr const char *T_text_plain = "text/plain";
static constexpr const char *T_video_mp4 = "video/mp4";
static constexpr const char *T_text_xml = "text/xml";
static constexpr const char *T_video_mp4 = "video/mp4";
static constexpr const char *T_video_webm = "video/webm";

// Response codes
static constexpr const char *T_HTTP_CODE_100 = "Continue";
Expand Down