Skip to content

Commit 7c008e3

Browse files
authored
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
1 parent 3e4d7c7 commit 7c008e3

File tree

2 files changed

+90
-39
lines changed

2 files changed

+90
-39
lines changed

libraries/ESP8266WebServer/src/detail/mimetable.cpp

+84-35
Original file line numberDiff line numberDiff line change
@@ -5,48 +5,97 @@
55
namespace mime
66
{
77

8-
// Table of extension->MIME strings stored in PROGMEM, needs to be global due to GCC section typing rules
9-
const Entry mimeTable[maxType] PROGMEM =
8+
static const char kHtmlSuffix[] PROGMEM = ".html";
9+
static const char kHtmSuffix[] PROGMEM = ".htm";
10+
static const char kTxtSuffix[] PROGMEM = ".txt";
11+
#ifndef MIMETYPE_MINIMAL
12+
static const char kCssSuffix[] PROGMEM = ".css";
13+
static const char kJsSuffix[] PROGMEM = ".js";
14+
static const char kJsonSuffix[] PROGMEM = ".json";
15+
static const char kPngSuffix[] PROGMEM = ".png";
16+
static const char kGifSuffix[] PROGMEM = ".gif";
17+
static const char kJpgSuffix[] PROGMEM = ".jpg";
18+
static const char kJpegSuffix[] PROGMEM = ".jpeg";
19+
static const char kIcoSuffix[] PROGMEM = ".ico";
20+
static const char kSvgSuffix[] PROGMEM = ".svg";
21+
static const char kTtfSuffix[] PROGMEM = ".ttf";
22+
static const char kOtfSuffix[] PROGMEM = ".otf";
23+
static const char kWoffSuffix[] PROGMEM = ".woff";
24+
static const char kWoff2Suffix[] PROGMEM = ".woff2";
25+
static const char kEotSuffix[] PROGMEM = ".eot";
26+
static const char kSfntSuffix[] PROGMEM = ".sfnt";
27+
static const char kXmlSuffix[] PROGMEM = ".xml";
28+
static const char kPdfSuffix[] PROGMEM = ".pdf";
29+
static const char kZipSuffix[] PROGMEM = ".zip";
30+
static const char kAppcacheSuffix[] PROGMEM = ".appcache";
31+
#endif // MIMETYPE_MINIMAL
32+
static const char kGzSuffix[] PROGMEM = ".gz";
33+
static const char kDefaultSuffix[] PROGMEM = "";
34+
35+
static const char kHtml[] PROGMEM = "text/html";
36+
static const char kTxt[] PROGMEM = "text/plain";
37+
#ifndef MIMETYPE_MINIMAL
38+
static const char kCss[] PROGMEM = "text/css";
39+
static const char kJs[] PROGMEM = "application/javascript";
40+
static const char kJson[] PROGMEM = "application/json";
41+
static const char kPng[] PROGMEM = "image/png";
42+
static const char kGif[] PROGMEM = "image/gif";
43+
static const char kJpg[] PROGMEM = "image/jpeg";
44+
static const char kJpeg[] PROGMEM = "image/jpeg";
45+
static const char kIco[] PROGMEM = "image/x-icon";
46+
static const char kSvg[] PROGMEM = "image/svg+xml";
47+
static const char kTtf[] PROGMEM = "application/x-font-ttf";
48+
static const char kOtf[] PROGMEM = "application/x-font-opentype";
49+
static const char kWoff[] PROGMEM = "application/font-woff";
50+
static const char kWoff2[] PROGMEM = "application/font-woff2";
51+
static const char kEot[] PROGMEM = "application/vnd.ms-fontobject";
52+
static const char kSfnt[] PROGMEM = "application/font-sfnt";
53+
static const char kXml[] PROGMEM = "text/xml";
54+
static const char kPdf[] PROGMEM = "application/pdf";
55+
static const char kZip[] PROGMEM = "application/zip";
56+
static const char kAppcache[] PROGMEM = "text/cache-manifest";
57+
#endif // MIMETYPE_MINIMAL
58+
static const char kGz[] PROGMEM = "application/x-gzip";
59+
static const char kDefault[] PROGMEM = "application/octet-stream";
60+
61+
const Entry mimeTable[maxType] PROGMEM =
1062
{
11-
{ ".html", "text/html" },
12-
{ ".htm", "text/html" },
13-
{ ".css", "text/css" },
14-
{ ".txt", "text/plain" },
15-
{ ".js", "application/javascript" },
16-
{ ".json", "application/json" },
17-
{ ".png", "image/png" },
18-
{ ".gif", "image/gif" },
19-
{ ".jpg", "image/jpeg" },
20-
{ ".jpeg", "image/jpeg" },
21-
{ ".ico", "image/x-icon" },
22-
{ ".svg", "image/svg+xml" },
23-
{ ".ttf", "application/x-font-ttf" },
24-
{ ".otf", "application/x-font-opentype" },
25-
{ ".woff", "application/font-woff" },
26-
{ ".woff2", "application/font-woff2" },
27-
{ ".eot", "application/vnd.ms-fontobject" },
28-
{ ".sfnt", "application/font-sfnt" },
29-
{ ".xml", "text/xml" },
30-
{ ".pdf", "application/pdf" },
31-
{ ".zip", "application/zip" },
32-
{ ".gz", "application/x-gzip" },
33-
{ ".appcache", "text/cache-manifest" },
34-
{ "", "application/octet-stream" }
63+
{ kHtmlSuffix, kHtml },
64+
{ kHtmSuffix, kHtml },
65+
{ kTxtSuffix, kTxtSuffix },
66+
#ifndef MIMETYPE_MINIMAL
67+
{ kCssSuffix, kCss },
68+
{ kJsSuffix, kJs },
69+
{ kJsonSuffix, kJson },
70+
{ kPngSuffix, kPng },
71+
{ kGifSuffix, kGif },
72+
{ kJpgSuffix, kJpg },
73+
{ kJpegSuffix, kJpeg },
74+
{ kIcoSuffix, kIco },
75+
{ kSvgSuffix, kSvg },
76+
{ kTtfSuffix, kTtf },
77+
{ kOtfSuffix, kOtf },
78+
{ kWoffSuffix, kWoff },
79+
{ kWoff2Suffix, kWoff2 },
80+
{ kEotSuffix, kEot },
81+
{ kSfntSuffix, kSfnt },
82+
{ kXmlSuffix, kXml },
83+
{ kPdfSuffix, kPdf },
84+
{ kZipSuffix, kZip },
85+
{ kAppcacheSuffix, kAppcache },
86+
#endif // MIMETYPE_MINIMAL
87+
{ kGzSuffix, kGz },
88+
{ kDefaultSuffix, kDefault }
3589
};
3690

3791
String getContentType(const String& path) {
38-
char buff[sizeof(mimeTable[0].mimeType)];
39-
// Check all entries but last one for match, return if found
40-
for (size_t i=0; i < sizeof(mimeTable)/sizeof(mimeTable[0])-1; i++) {
41-
strcpy_P(buff, mimeTable[i].endsWith);
42-
if (path.endsWith(buff)) {
43-
strcpy_P(buff, mimeTable[i].mimeType);
44-
return String(buff);
92+
for (size_t i = 0; i < maxType; i++) {
93+
if (path.endsWith(FPSTR(mimeTable[i].endsWith))) {
94+
return String(FPSTR(mimeTable[i].mimeType));
4595
}
4696
}
4797
// Fall-through and just return default type
48-
strcpy_P(buff, mimeTable[sizeof(mimeTable)/sizeof(mimeTable[0])-1].mimeType);
49-
return String(buff);
98+
return String(FPSTR(kDefault));
5099
}
51100

52101
}

libraries/ESP8266WebServer/src/detail/mimetable.h

+6-4
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ enum type
1010
{
1111
html,
1212
htm,
13-
css,
1413
txt,
14+
#ifndef MIMETYPE_MINIMAL // allow to compile with only the strict minimum of mime-types
15+
css,
1516
js,
1617
json,
1718
png,
@@ -29,16 +30,17 @@ enum type
2930
xml,
3031
pdf,
3132
zip,
32-
gz,
3333
appcache,
34+
#endif // MIMETYPE_MINIMAL
35+
gz,
3436
none,
3537
maxType
3638
};
3739

3840
struct Entry
3941
{
40-
const char endsWith[16];
41-
const char mimeType[32];
42+
const char * endsWith;
43+
const char * mimeType;
4244
};
4345

4446

0 commit comments

Comments
 (0)