|
5 | 5 | namespace mime
|
6 | 6 | {
|
7 | 7 |
|
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 = |
10 | 62 | {
|
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 } |
35 | 89 | };
|
36 | 90 |
|
37 | 91 | 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)); |
45 | 95 | }
|
46 | 96 | }
|
47 | 97 | // 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)); |
50 | 99 | }
|
51 | 100 |
|
52 | 101 | }
|
0 commit comments