-
Couldn't load subscription status.
- Fork 70
feature: refactor content type and MIME list #241
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
feature: refactor content type and MIME list #241
Conversation
|
adding is OK, removing, I am not a fan of. Things like zip, gz and ttf should stay |
|
Also the default type needs to be tested. Surely there was a reason to be text/plain by default |
There was a problem hiding this comment.
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 withapplication/, 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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Browsers use Content-Type to know how to handle received files. |
|
Make changes: No reordering, as it doesn't provide much benefit. I hadn't realized the difficulty of reviewing changes. I hope these changes are enough. |
|
The search system you propose isn't adequate, as it's the extensions you're searching for, not the MINE types. |
Oh sorry you are completely right! Sorry... 😓 |
I think it is OK (providing we add some comments to explain why it is like that). I would extract that in a specific static method: |
|
Version 1 is the version in this PR. The results are: === SYSTEM INFORMATION === You can test in https://github.com/JosePineiro/esp32_benchmark_1 Code of version 3: |
Indeed, in that case do not optimize and go with the more readable version… thanks for the comparison efforts! This helps taking the right décision. |
|
You now have the PR with the most readable version. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR refactors MIME type handling for file responses to modernize supported formats and improve browser behavior. The changes align with current web standards and IoT requirements while optimizing for embedded systems.
Key changes:
- Updates JavaScript MIME type from obsolete "application/javascript" to standard "text/javascript" per RFC 9239
- Changes default fallback from "text/plain" to "application/octet-stream" to ensure unknown files are downloaded rather than displayed
- Adds modern formats (.opus, .webm, .txt, .csv) and removes obsolete/problematic formats (.eot, .zip, .gz)
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/literals.h | Adds new file extension constants and MIME types, removes obsolete formats, adds documentation comments |
| src/WebResponses.cpp | Updates MIME type detection logic with new formats and improved fallback behavior |
Comments suppressed due to low confidence (1)
src/WebResponses.cpp:676
- The JavaScript file extension is still mapped to the obsolete 'application/javascript' MIME type instead of the new standard 'text/javascript' mentioned in the PR description. This should be updated to use T_text_javascript.
_contentType = T_font_ttf;
src/literals.h
Outdated
| 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 |
Copilot
AI
Jul 27, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The obsolete 'application/javascript' constant is still defined but appears unused after the refactor. Consider removing it to reduce code clutter, or if keeping for backward compatibility, add a comment explaining why it's retained.
| static constexpr const char *T_application_javascript = "application/javascript"; // Obsolete type for JavaScript | |
| static constexpr const char *T_application_javascript = "application/javascript"; // Obsolete type for JavaScript | |
| // Retained for backward compatibility with older systems or clients that may still rely on this MIME type. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with the proposed behavior changes - they are more aligned with what a http server should do.
|
@me-no-dev : can you please have a look ? thanks! |
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
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Remove autoMINE extensions: zip, gz y eot Add extensions: csv, opus, txt, webm
94969f2 to
b0db1e1
Compare
This pull request refactors the _setContentTypeFromPath method in AsyncFileResponse to improve maintainability and performance in MIME type detection.
Key changes:
The update brings the function more in line with modern web and IoT usage while maintaining backward compatibility with legacy formats where still relevant.