From a9cf12fcf0d0b7aac903e48699a766fe0722a3db Mon Sep 17 00:00:00 2001 From: vdeconinck Date: Thu, 30 Apr 2020 05:36:42 +0200 Subject: [PATCH] Make getContentType available for 3rd party usage (#7254) * Refactored to make getContentType public for 3rd party use. * Added missing "jpeg" extension * Use getContentType() from mime namespace. * Also add .jpeg extension --- .../examples/FSBrowser/FSBrowser.ino | 45 +------------------ .../src/detail/RequestHandlersImpl.h | 18 ++------ .../ESP8266WebServer/src/detail/mimetable.cpp | 17 +++++++ .../ESP8266WebServer/src/detail/mimetable.h | 5 ++- 4 files changed, 26 insertions(+), 59 deletions(-) diff --git a/libraries/ESP8266WebServer/examples/FSBrowser/FSBrowser.ino b/libraries/ESP8266WebServer/examples/FSBrowser/FSBrowser.ino index caf863492..beebeb014 100644 --- a/libraries/ESP8266WebServer/examples/FSBrowser/FSBrowser.ino +++ b/libraries/ESP8266WebServer/examples/FSBrowser/FSBrowser.ino @@ -115,49 +115,6 @@ void replyServerError(String msg) { server.send(500, FPSTR(TEXT_PLAIN), msg + "\r\n"); } -String getContentType(String filename) { - if (filename.endsWith(".htm")) { - return "text/html"; - } - if (filename.endsWith(".html")) { - return "text/html"; - } - if (filename.endsWith(".css")) { - return "text/css"; - } - if (filename.endsWith(".js")) { - return "application/javascript"; - } - if (filename.endsWith(".png")) { - return "image/png"; - } - if (filename.endsWith(".gif")) { - return "image/gif"; - } - if (filename.endsWith(".jpg")) { - return "image/jpeg"; - } - if (filename.endsWith(".jpeg")) { - return "image/jpeg"; - } - if (filename.endsWith(".ico")) { - return "image/x-icon"; - } - if (filename.endsWith(".xml")) { - return "text/xml"; - } - if (filename.endsWith(".pdf")) { - return "application/x-pdf"; - } - if (filename.endsWith(".zip")) { - return "application/x-zip"; - } - if (filename.endsWith(".gz")) { - return "application/x-gzip"; - } - return FPSTR(TEXT_PLAIN); -} - #ifdef USE_SPIFFS /* Checks filename for character combinations that are not supported by FSBrowser (alhtough valid on SPIFFS). @@ -304,7 +261,7 @@ bool handleFileRead(String path) { if (server.hasArg("download")) { contentType = F("application/octet-stream"); } else { - contentType = getContentType(path); + contentType = mime::getContentType(path); } if (!fileSystem->exists(path)) { diff --git a/libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h b/libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h index 9388d4ac4..fb452af48 100644 --- a/libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h +++ b/libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h @@ -120,7 +120,7 @@ public: } DEBUGV("StaticRequestHandler::handle: path=%s, isFile=%d\r\n", path.c_str(), _isFile); - String contentType = getContentType(path); + String contentType = mime::getContentType(path); // look for gz file, only if the original specified path is not a gz. So part only works to send gzip via content encoding when a non compressed is asked for // if you point the the path to gzip you will serve the gzip as content type "application/x-gzip", not text or javascript etc... @@ -146,19 +146,9 @@ public: return true; } - static String getContentType(const String& path) { - char buff[sizeof(mimeTable[0].mimeType)]; - // Check all entries but last one for match, return if found - for (size_t i=0; i < sizeof(mimeTable)/sizeof(mimeTable[0])-1; i++) { - strcpy_P(buff, mimeTable[i].endsWith); - if (path.endsWith(buff)) { - strcpy_P(buff, mimeTable[i].mimeType); - return String(buff); - } - } - // Fall-through and just return default type - strcpy_P(buff, mimeTable[sizeof(mimeTable)/sizeof(mimeTable[0])-1].mimeType); - return String(buff); + /* Deprecated version. Please use mime::getContentType instead */ + static String getContentType(const String& path) __attribute__((deprecated)) { + return mime::getContentType(path); } protected: diff --git a/libraries/ESP8266WebServer/src/detail/mimetable.cpp b/libraries/ESP8266WebServer/src/detail/mimetable.cpp index d646857a8..c4a20cc0e 100644 --- a/libraries/ESP8266WebServer/src/detail/mimetable.cpp +++ b/libraries/ESP8266WebServer/src/detail/mimetable.cpp @@ -1,5 +1,6 @@ #include "mimetable.h" #include "pgmspace.h" +#include "WString.h" namespace mime { @@ -16,6 +17,7 @@ const Entry mimeTable[maxType] PROGMEM = { ".png", "image/png" }, { ".gif", "image/gif" }, { ".jpg", "image/jpeg" }, + { ".jpeg", "image/jpeg" }, { ".ico", "image/x-icon" }, { ".svg", "image/svg+xml" }, { ".ttf", "application/x-font-ttf" }, @@ -32,4 +34,19 @@ const Entry mimeTable[maxType] PROGMEM = { "", "application/octet-stream" } }; + String getContentType(const String& path) { + char buff[sizeof(mimeTable[0].mimeType)]; + // Check all entries but last one for match, return if found + for (size_t i=0; i < sizeof(mimeTable)/sizeof(mimeTable[0])-1; i++) { + strcpy_P(buff, mimeTable[i].endsWith); + if (path.endsWith(buff)) { + strcpy_P(buff, mimeTable[i].mimeType); + return String(buff); + } + } + // Fall-through and just return default type + strcpy_P(buff, mimeTable[sizeof(mimeTable)/sizeof(mimeTable[0])-1].mimeType); + return String(buff); + } + } diff --git a/libraries/ESP8266WebServer/src/detail/mimetable.h b/libraries/ESP8266WebServer/src/detail/mimetable.h index 191356c48..6e6a4e963 100644 --- a/libraries/ESP8266WebServer/src/detail/mimetable.h +++ b/libraries/ESP8266WebServer/src/detail/mimetable.h @@ -1,6 +1,7 @@ #ifndef __MIMETABLE_H__ #define __MIMETABLE_H__ +#include "WString.h" namespace mime { @@ -16,6 +17,7 @@ enum type png, gif, jpg, + jpeg, ico, svg, ttf, @@ -41,7 +43,8 @@ struct Entry extern const Entry mimeTable[maxType]; + +String getContentType(const String& path); } - #endif