1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-06 05:21:22 +03:00

Flash size reduction for mime-type (#7312)

* Flash size reduction for mime-type
* moving from fixed size strings to standard PROGMEM strings
* adding `#define MIMETYPE_MINIMAL` to reduce the footprint to
  mime-types that are strictly necessary

* Added MIMETYPE_MINIMAL conditionals
This commit is contained in:
s-hadinger 2020-05-19 05:16:11 +02:00 committed by GitHub
parent 3e4d7c76c4
commit 7c008e31bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 90 additions and 39 deletions

View File

@ -5,48 +5,97 @@
namespace mime namespace mime
{ {
// Table of extension->MIME strings stored in PROGMEM, needs to be global due to GCC section typing rules static const char kHtmlSuffix[] PROGMEM = ".html";
static const char kHtmSuffix[] PROGMEM = ".htm";
static const char kTxtSuffix[] PROGMEM = ".txt";
#ifndef MIMETYPE_MINIMAL
static const char kCssSuffix[] PROGMEM = ".css";
static const char kJsSuffix[] PROGMEM = ".js";
static const char kJsonSuffix[] PROGMEM = ".json";
static const char kPngSuffix[] PROGMEM = ".png";
static const char kGifSuffix[] PROGMEM = ".gif";
static const char kJpgSuffix[] PROGMEM = ".jpg";
static const char kJpegSuffix[] PROGMEM = ".jpeg";
static const char kIcoSuffix[] PROGMEM = ".ico";
static const char kSvgSuffix[] PROGMEM = ".svg";
static const char kTtfSuffix[] PROGMEM = ".ttf";
static const char kOtfSuffix[] PROGMEM = ".otf";
static const char kWoffSuffix[] PROGMEM = ".woff";
static const char kWoff2Suffix[] PROGMEM = ".woff2";
static const char kEotSuffix[] PROGMEM = ".eot";
static const char kSfntSuffix[] PROGMEM = ".sfnt";
static const char kXmlSuffix[] PROGMEM = ".xml";
static const char kPdfSuffix[] PROGMEM = ".pdf";
static const char kZipSuffix[] PROGMEM = ".zip";
static const char kAppcacheSuffix[] PROGMEM = ".appcache";
#endif // MIMETYPE_MINIMAL
static const char kGzSuffix[] PROGMEM = ".gz";
static const char kDefaultSuffix[] PROGMEM = "";
static const char kHtml[] PROGMEM = "text/html";
static const char kTxt[] PROGMEM = "text/plain";
#ifndef MIMETYPE_MINIMAL
static const char kCss[] PROGMEM = "text/css";
static const char kJs[] PROGMEM = "application/javascript";
static const char kJson[] PROGMEM = "application/json";
static const char kPng[] PROGMEM = "image/png";
static const char kGif[] PROGMEM = "image/gif";
static const char kJpg[] PROGMEM = "image/jpeg";
static const char kJpeg[] PROGMEM = "image/jpeg";
static const char kIco[] PROGMEM = "image/x-icon";
static const char kSvg[] PROGMEM = "image/svg+xml";
static const char kTtf[] PROGMEM = "application/x-font-ttf";
static const char kOtf[] PROGMEM = "application/x-font-opentype";
static const char kWoff[] PROGMEM = "application/font-woff";
static const char kWoff2[] PROGMEM = "application/font-woff2";
static const char kEot[] PROGMEM = "application/vnd.ms-fontobject";
static const char kSfnt[] PROGMEM = "application/font-sfnt";
static const char kXml[] PROGMEM = "text/xml";
static const char kPdf[] PROGMEM = "application/pdf";
static const char kZip[] PROGMEM = "application/zip";
static const char kAppcache[] PROGMEM = "text/cache-manifest";
#endif // MIMETYPE_MINIMAL
static const char kGz[] PROGMEM = "application/x-gzip";
static const char kDefault[] PROGMEM = "application/octet-stream";
const Entry mimeTable[maxType] PROGMEM = const Entry mimeTable[maxType] PROGMEM =
{ {
{ ".html", "text/html" }, { kHtmlSuffix, kHtml },
{ ".htm", "text/html" }, { kHtmSuffix, kHtml },
{ ".css", "text/css" }, { kTxtSuffix, kTxtSuffix },
{ ".txt", "text/plain" }, #ifndef MIMETYPE_MINIMAL
{ ".js", "application/javascript" }, { kCssSuffix, kCss },
{ ".json", "application/json" }, { kJsSuffix, kJs },
{ ".png", "image/png" }, { kJsonSuffix, kJson },
{ ".gif", "image/gif" }, { kPngSuffix, kPng },
{ ".jpg", "image/jpeg" }, { kGifSuffix, kGif },
{ ".jpeg", "image/jpeg" }, { kJpgSuffix, kJpg },
{ ".ico", "image/x-icon" }, { kJpegSuffix, kJpeg },
{ ".svg", "image/svg+xml" }, { kIcoSuffix, kIco },
{ ".ttf", "application/x-font-ttf" }, { kSvgSuffix, kSvg },
{ ".otf", "application/x-font-opentype" }, { kTtfSuffix, kTtf },
{ ".woff", "application/font-woff" }, { kOtfSuffix, kOtf },
{ ".woff2", "application/font-woff2" }, { kWoffSuffix, kWoff },
{ ".eot", "application/vnd.ms-fontobject" }, { kWoff2Suffix, kWoff2 },
{ ".sfnt", "application/font-sfnt" }, { kEotSuffix, kEot },
{ ".xml", "text/xml" }, { kSfntSuffix, kSfnt },
{ ".pdf", "application/pdf" }, { kXmlSuffix, kXml },
{ ".zip", "application/zip" }, { kPdfSuffix, kPdf },
{ ".gz", "application/x-gzip" }, { kZipSuffix, kZip },
{ ".appcache", "text/cache-manifest" }, { kAppcacheSuffix, kAppcache },
{ "", "application/octet-stream" } #endif // MIMETYPE_MINIMAL
{ kGzSuffix, kGz },
{ kDefaultSuffix, kDefault }
}; };
String getContentType(const String& path) { String getContentType(const String& path) {
char buff[sizeof(mimeTable[0].mimeType)]; for (size_t i = 0; i < maxType; i++) {
// Check all entries but last one for match, return if found if (path.endsWith(FPSTR(mimeTable[i].endsWith))) {
for (size_t i=0; i < sizeof(mimeTable)/sizeof(mimeTable[0])-1; i++) { return String(FPSTR(mimeTable[i].mimeType));
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 // Fall-through and just return default type
strcpy_P(buff, mimeTable[sizeof(mimeTable)/sizeof(mimeTable[0])-1].mimeType); return String(FPSTR(kDefault));
return String(buff);
} }
} }

View File

@ -10,8 +10,9 @@ enum type
{ {
html, html,
htm, htm,
css,
txt, txt,
#ifndef MIMETYPE_MINIMAL // allow to compile with only the strict minimum of mime-types
css,
js, js,
json, json,
png, png,
@ -29,16 +30,17 @@ enum type
xml, xml,
pdf, pdf,
zip, zip,
gz,
appcache, appcache,
#endif // MIMETYPE_MINIMAL
gz,
none, none,
maxType maxType
}; };
struct Entry struct Entry
{ {
const char endsWith[16]; const char * endsWith;
const char mimeType[32]; const char * mimeType;
}; };