Skip to content

Commit 615cff7

Browse files
authored
Merge pull request #225 from JosePineiro/optimize-content-type-detection
Optimize content type detection using C-strings
2 parents 7d1afa3 + b55f2df commit 615cff7

File tree

1 file changed

+40
-24
lines changed

1 file changed

+40
-24
lines changed

src/WebResponses.cpp

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,16 @@ size_t AsyncAbstractResponse::_fillBufferAndProcessTemplates(uint8_t *data, size
619619
* File Response
620620
* */
621621

622+
/**
623+
* @brief Sets the content type based on the file path extension
624+
*
625+
* This method determines the appropriate MIME content type for a file based on its
626+
* file extension. It supports both external content type functions (if available)
627+
* and an internal mapping of common file extensions to their corresponding MIME types.
628+
*
629+
* @param path The file path string from which to extract the extension
630+
* @note The method modifies the internal _contentType member variable
631+
*/
622632
void AsyncFileResponse::_setContentTypeFromPath(const String &path) {
623633
#if HAVE_EXTERN_GET_Content_Type_FUNCTION
624634
#ifndef ESP8266
@@ -628,41 +638,47 @@ void AsyncFileResponse::_setContentTypeFromPath(const String &path) {
628638
#endif
629639
_contentType = getContentType(path);
630640
#else
631-
if (path.endsWith(T__html)) {
632-
_contentType = T_text_html;
633-
} else if (path.endsWith(T__htm)) {
641+
const char *cpath = path.c_str();
642+
const char *dot = strrchr(cpath, '.');
643+
644+
if (!dot) {
645+
_contentType = T_text_plain;
646+
return;
647+
}
648+
649+
if (strcmp(dot, T__html) == 0 || strcmp(dot, T__htm) == 0) {
634650
_contentType = T_text_html;
635-
} else if (path.endsWith(T__css)) {
651+
} else if (strcmp(dot, T__css) == 0) {
636652
_contentType = T_text_css;
637-
} else if (path.endsWith(T__json)) {
638-
_contentType = T_application_json;
639-
} else if (path.endsWith(T__js)) {
653+
} else if (strcmp(dot, T__js) == 0) {
640654
_contentType = T_application_javascript;
641-
} else if (path.endsWith(T__png)) {
655+
} else if (strcmp(dot, T__json) == 0) {
656+
_contentType = T_application_json;
657+
} else if (strcmp(dot, T__png) == 0) {
642658
_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)) {
659+
} else if (strcmp(dot, T__ico) == 0) {
648660
_contentType = T_image_x_icon;
649-
} else if (path.endsWith(T__svg)) {
661+
} else if (strcmp(dot, T__svg) == 0) {
650662
_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)) {
663+
} else if (strcmp(dot, T__jpg) == 0) {
664+
_contentType = T_image_jpeg;
665+
} else if (strcmp(dot, T__gif) == 0) {
666+
_contentType = T_image_gif;
667+
} else if (strcmp(dot, T__woff2) == 0) {
656668
_contentType = T_font_woff2;
657-
} else if (path.endsWith(T__ttf)) {
669+
} else if (strcmp(dot, T__woff) == 0) {
670+
_contentType = T_font_woff;
671+
} else if (strcmp(dot, T__ttf) == 0) {
658672
_contentType = T_font_ttf;
659-
} else if (path.endsWith(T__xml)) {
673+
} else if (strcmp(dot, T__eot) == 0) {
674+
_contentType = T_font_eot;
675+
} else if (strcmp(dot, T__xml) == 0) {
660676
_contentType = T_text_xml;
661-
} else if (path.endsWith(T__pdf)) {
677+
} else if (strcmp(dot, T__pdf) == 0) {
662678
_contentType = T_application_pdf;
663-
} else if (path.endsWith(T__zip)) {
679+
} else if (strcmp(dot, T__zip) == 0) {
664680
_contentType = T_application_zip;
665-
} else if (path.endsWith(T__gz)) {
681+
} else if (strcmp(dot, T__gz) == 0) {
666682
_contentType = T_application_x_gzip;
667683
} else {
668684
_contentType = T_text_plain;

0 commit comments

Comments
 (0)