@@ -619,6 +619,17 @@ size_t AsyncAbstractResponse::_fillBufferAndProcessTemplates(uint8_t *data, size
619
619
* File Response
620
620
* */
621
621
622
+
623
+ /* *
624
+ * @brief Sets the content type based on the file path extension
625
+ *
626
+ * This method determines the appropriate MIME content type for a file based on its
627
+ * file extension. It supports both external content type functions (if available)
628
+ * and an internal mapping of common file extensions to their corresponding MIME types.
629
+ *
630
+ * @param path The file path string from which to extract the extension
631
+ * @note The method modifies the internal _contentType member variable
632
+ */
622
633
void AsyncFileResponse::_setContentTypeFromPath (const String &path) {
623
634
#if HAVE_EXTERN_GET_Content_Type_FUNCTION
624
635
#ifndef ESP8266
@@ -628,48 +639,55 @@ void AsyncFileResponse::_setContentTypeFromPath(const String &path) {
628
639
#endif
629
640
_contentType = getContentType (path);
630
641
#else
631
- if (path.endsWith (T__html)) {
632
- _contentType = T_text_html;
633
- } else if (path.endsWith (T__htm)) {
642
+ const char *cpath = path.c_str ();
643
+ const char *dot = strrchr (cpath, ' .' );
644
+
645
+ if (!dot) {
646
+ _contentType = T_text_plain;
647
+ return ;
648
+ }
649
+
650
+ if (strcmp (dot, T__html) == 0 || strcmp (dot, T__htm) == 0 ) {
634
651
_contentType = T_text_html;
635
- } else if (path. endsWith ( T__css)) {
652
+ } else if (strcmp (dot, T__css) == 0 ) {
636
653
_contentType = T_text_css;
637
- } else if (path.endsWith (T__json)) {
638
- _contentType = T_application_json;
639
- } else if (path.endsWith (T__js)) {
654
+ } else if (strcmp (dot, T__js) == 0 ) {
640
655
_contentType = T_application_javascript;
641
- } else if (path.endsWith (T__png)) {
656
+ } else if (strcmp (dot, T__json) == 0 ) {
657
+ _contentType = T_application_json;
658
+ } else if (strcmp (dot, T__png) == 0 ) {
642
659
_contentType = T_image_png;
643
- } else if (path.endsWith (T__gif)) {
644
- _contentType = T_image_gif;
645
- } else if (path.endsWith (T__jpg)) {
646
- _contentType = T_image_jpeg;
647
- } else if (path.endsWith (T__ico)) {
660
+ } else if (strcmp (dot, T__ico) == 0 ) {
648
661
_contentType = T_image_x_icon;
649
- } else if (path. endsWith ( T__svg)) {
662
+ } else if (strcmp (dot, T__svg) == 0 ) {
650
663
_contentType = T_image_svg_xml;
651
- } else if (path. endsWith (T__eot) ) {
652
- _contentType = T_font_eot ;
653
- } else if (path. endsWith (T__woff) ) {
654
- _contentType = T_font_woff ;
655
- } else if (path. endsWith ( T__woff2)) {
664
+ } else if (strcmp (dot, T__jpg) == 0 ) {
665
+ _contentType = T_image_jpeg ;
666
+ } else if (strcmp (dot, T__gif) == 0 ) {
667
+ _contentType = T_image_gif ;
668
+ } else if (strcmp (dot, T__woff2) == 0 ) {
656
669
_contentType = T_font_woff2;
657
- } else if (path.endsWith (T__ttf)) {
670
+ } else if (strcmp (dot, T__woff) == 0 ) {
671
+ _contentType = T_font_woff;
672
+ } else if (strcmp (dot, T__ttf) == 0 ) {
658
673
_contentType = T_font_ttf;
659
- } else if (path.endsWith (T__xml)) {
674
+ } else if (strcmp (dot, T__eot) == 0 ) {
675
+ _contentType = T_font_eot;
676
+ } else if (strcmp (dot, T__xml) == 0 ) {
660
677
_contentType = T_text_xml;
661
- } else if (path. endsWith ( T__pdf)) {
678
+ } else if (strcmp (dot, T__pdf) == 0 ) {
662
679
_contentType = T_application_pdf;
663
- } else if (path. endsWith ( T__zip)) {
680
+ } else if (strcmp (dot, T__zip) == 0 ) {
664
681
_contentType = T_application_zip;
665
- } else if (path. endsWith ( T__gz)) {
682
+ } else if (strcmp (dot, T__gz) == 0 ) {
666
683
_contentType = T_application_x_gzip;
667
684
} else {
668
685
_contentType = T_text_plain;
669
686
}
670
687
#endif
671
688
}
672
689
690
+
673
691
/* *
674
692
* @brief Constructor for AsyncFileResponse that handles file serving with compression support
675
693
*
0 commit comments