Skip to content

Commit 1eb800a

Browse files
committed
Move content types in to PROGMEM
1 parent 4a55ee9 commit 1eb800a

File tree

5 files changed

+125
-27
lines changed

5 files changed

+125
-27
lines changed

src/ContentTypes.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include "ContentTypes.h"
2+
3+
static inline bool matches_p(const char* str, const char* progmem_str) {
4+
return strcmp_P(str, progmem_str) == 0;
5+
}
6+
7+
const __FlashStringHelper* contentTypeFor(const String& path) {
8+
// Find extension part of path
9+
auto idx = path.lastIndexOf('.');
10+
if (idx < path.length()) {
11+
auto ext_str = path.begin() + idx + 1;
12+
13+
if (matches_p(ext_str, HTML_EXTENSION)) return FPSTR(CONTENT_TYPE_HTML);
14+
else if (matches_p(ext_str, HTM_EXTENSION)) return FPSTR(CONTENT_TYPE_HTML);
15+
else if (matches_p(ext_str, CSS_EXTENSION)) return FPSTR(CONTENT_TYPE_CSS);
16+
else if (matches_p(ext_str, JSON_EXTENSION)) return FPSTR(CONTENT_TYPE_JSON);
17+
else if (matches_p(ext_str, JS_EXTENSION)) return FPSTR(CONTENT_TYPE_JAVASCRIPT);
18+
else if (matches_p(ext_str, PNG_EXTENSION)) return FPSTR(CONTENT_TYPE_PNG);
19+
else if (matches_p(ext_str, GIF_EXTENSION)) return FPSTR(CONTENT_TYPE_GIF);
20+
else if (matches_p(ext_str, JPG_EXTENSION)) return FPSTR(CONTENT_TYPE_JPEG);
21+
else if (matches_p(ext_str, ICO_EXTENSION)) return FPSTR(CONTENT_TYPE_XICON);
22+
else if (matches_p(ext_str, SVG_EXTENSION)) return FPSTR(CONTENT_TYPE_SVG);
23+
else if (matches_p(ext_str, EOT_EXTENSION)) return FPSTR(CONTENT_TYPE_EOT);
24+
else if (matches_p(ext_str, WOFF_EXTENSION)) return FPSTR(CONTENT_TYPE_WOFF);
25+
else if (matches_p(ext_str, WOFF2_EXTENSION)) return FPSTR(CONTENT_TYPE_WOFF2);
26+
else if (matches_p(ext_str, TTF_EXTENSION)) return FPSTR(CONTENT_TYPE_TTF);
27+
else if (matches_p(ext_str, XML_EXTENSION)) return FPSTR(CONTENT_TYPE_XML);
28+
else if (matches_p(ext_str, PDF_EXTENSION)) return FPSTR(CONTENT_TYPE_PDF);
29+
else if (matches_p(ext_str, ZIP_EXTENSION)) return FPSTR(CONTENT_TYPE_ZIP);
30+
else if (matches_p(ext_str, GZIP_EXTENSION)) return FPSTR(CONTENT_TYPE_GZIP);
31+
}
32+
return FPSTR(CONTENT_TYPE_PLAIN);
33+
}
34+
35+
const char CONTENT_TYPE_PLAIN[] PROGMEM = "text/plain";
36+
const char HTM_EXTENSION[] PROGMEM = "htm";
37+
const char HTML_EXTENSION[] PROGMEM = "html";
38+
const char CONTENT_TYPE_HTML[] PROGMEM = "text/html";
39+
const char CSS_EXTENSION[] PROGMEM = "css";
40+
const char CONTENT_TYPE_CSS[] PROGMEM = "text/css";
41+
const char JSON_EXTENSION[] PROGMEM = "json";
42+
const char CONTENT_TYPE_JSON[] PROGMEM = "application/json";
43+
const char JS_EXTENSION[] PROGMEM = "js";
44+
const char CONTENT_TYPE_JAVASCRIPT[] PROGMEM = "application/javascript";
45+
const char PNG_EXTENSION[] PROGMEM = "png";
46+
const char CONTENT_TYPE_PNG[] PROGMEM = "image/png";
47+
const char GIF_EXTENSION[] PROGMEM = "gif";
48+
const char CONTENT_TYPE_GIF[] PROGMEM = "image/gif";
49+
const char JPG_EXTENSION[] PROGMEM = "jpg";
50+
const char CONTENT_TYPE_JPEG[] PROGMEM = "image/jpeg";
51+
const char ICO_EXTENSION[] PROGMEM = "ico";
52+
const char CONTENT_TYPE_XICON[] PROGMEM = "image/x-icon";
53+
const char SVG_EXTENSION[] PROGMEM = "svg";
54+
const char CONTENT_TYPE_SVG[] PROGMEM = "image/svg+xml";
55+
const char EOT_EXTENSION[] PROGMEM = "eot";
56+
const char CONTENT_TYPE_EOT[] PROGMEM = "font/eot";
57+
const char WOFF_EXTENSION[] PROGMEM = "woff";
58+
const char CONTENT_TYPE_WOFF[] PROGMEM = "font/woff";
59+
const char WOFF2_EXTENSION[] PROGMEM = "woff2";
60+
const char CONTENT_TYPE_WOFF2[] PROGMEM = "font/woff2";
61+
const char TTF_EXTENSION[] PROGMEM = "ttf";
62+
const char CONTENT_TYPE_TTF[] PROGMEM = "font/ttf";
63+
const char XML_EXTENSION[] PROGMEM = "xml";
64+
const char CONTENT_TYPE_XML[] PROGMEM = "text/xml";
65+
const char PDF_EXTENSION[] PROGMEM = "pdf";
66+
const char CONTENT_TYPE_PDF[] PROGMEM = "application/pdf";
67+
const char ZIP_EXTENSION[] PROGMEM = "zip";
68+
const char CONTENT_TYPE_ZIP[] PROGMEM = "application/zip";
69+
const char GZIP_EXTENSION[] PROGMEM = "gz";
70+
const char CONTENT_TYPE_GZIP[] PROGMEM = "application/x-gzip";

src/ContentTypes.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* ContentTypes.h
2+
3+
A global collection of PROGMEM content type strings, usable by applications
4+
5+
*/
6+
7+
#include "Arduino.h"
8+
9+
// Convenient lookup function
10+
const __FlashStringHelper* contentTypeFor(const String& filename);
11+
12+
// These are all PROGMEM-type variables; use accordingly
13+
extern const char CONTENT_TYPE_PLAIN[];
14+
extern const char HTM_EXTENSION[];
15+
extern const char HTML_EXTENSION[];
16+
extern const char CONTENT_TYPE_HTML[];
17+
extern const char CSS_EXTENSION[];
18+
extern const char CONTENT_TYPE_CSS[];
19+
extern const char JSON_EXTENSION[];
20+
extern const char CONTENT_TYPE_JSON[];
21+
extern const char JS_EXTENSION[];
22+
extern const char CONTENT_TYPE_JAVASCRIPT[];
23+
extern const char PNG_EXTENSION[];
24+
extern const char CONTENT_TYPE_PNG[];
25+
extern const char GIF_EXTENSION[];
26+
extern const char CONTENT_TYPE_GIF[];
27+
extern const char JPG_EXTENSION[];
28+
extern const char CONTENT_TYPE_JPEG[];
29+
extern const char ICO_EXTENSION[];
30+
extern const char CONTENT_TYPE_XICON[];
31+
extern const char SVG_EXTENSION[];
32+
extern const char CONTENT_TYPE_SVG[];
33+
extern const char EOT_EXTENSION[];
34+
extern const char CONTENT_TYPE_EOT[];
35+
extern const char WOFF_EXTENSION[];
36+
extern const char CONTENT_TYPE_WOFF[];
37+
extern const char WOFF2_EXTENSION[];
38+
extern const char CONTENT_TYPE_WOFF2[];
39+
extern const char TTF_EXTENSION[];
40+
extern const char CONTENT_TYPE_TTF[];
41+
extern const char XML_EXTENSION[];
42+
extern const char CONTENT_TYPE_XML[];
43+
extern const char PDF_EXTENSION[];
44+
extern const char CONTENT_TYPE_PDF[];
45+
extern const char ZIP_EXTENSION[];
46+
extern const char CONTENT_TYPE_ZIP[];
47+
extern const char GZIP_EXTENSION[];
48+
extern const char CONTENT_TYPE_GZIP[];

src/ESPAsyncWebServer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,5 +468,6 @@ class DefaultHeaders {
468468
#include "WebHandlerImpl.h"
469469
#include "AsyncWebSocket.h"
470470
#include "AsyncEventSource.h"
471+
#include "ContentTypes.h"
471472

472473
#endif /* _AsyncWebServer_H_ */

src/WebRequest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ void AsyncWebServerRequest::_onData(void *buf, size_t len){
156156
if(_parsedLength == 0){
157157
if(_contentType.startsWith("application/x-www-form-urlencoded")){
158158
_isPlainPost = true;
159-
} else if(_contentType == "text/plain" && __is_param_char(((char*)buf)[0])){
159+
} else if(_contentType == FPSTR(CONTENT_TYPE_PLAIN) && __is_param_char(((char*)buf)[0])){
160160
size_t i = 0;
161161
while (i<len && __is_param_char(((char*)buf)[i++]));
162162
if(i < len && ((char*)buf)[i-1] == '='){

src/WebResponses.cpp

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,9 @@ AsyncBasicResponse::AsyncBasicResponse(int code, String contentType, String cont
209209
if(_content.length()){
210210
_contentLength = _content.length();
211211
if(!_contentType.length())
212-
_contentType = "text/plain";
212+
_contentType = FPSTR(CONTENT_TYPE_PLAIN);
213213
}
214-
addHeader("Connection","close");
214+
addHeader(F("Connection"),F("close"));
215215
}
216216

217217
void AsyncBasicResponse::_respond(AsyncWebServerRequest *request){
@@ -571,34 +571,13 @@ AsyncFileResponse::~AsyncFileResponse(){
571571
_content.close();
572572
}
573573

574-
const static char GZIP_EXTENSION_PMEM[] PROGMEM = ".gz";
575-
const static auto GZIP_EXTENSION = FPSTR(GZIP_EXTENSION_PMEM);
576-
577574
void AsyncFileResponse::_setContentType(const String& path){
578-
if (path.endsWith(F(".html"))) _contentType = F("text/html");
579-
else if (path.endsWith(F(".htm"))) _contentType = F("text/html");
580-
else if (path.endsWith(F(".css"))) _contentType = F("text/css");
581-
else if (path.endsWith(F(".json"))) _contentType = F("application/json");
582-
else if (path.endsWith(F(".js"))) _contentType = F("application/javascript");
583-
else if (path.endsWith(F(".png"))) _contentType = F("image/png");
584-
else if (path.endsWith(F(".gif"))) _contentType = F("image/gif");
585-
else if (path.endsWith(F(".jpg"))) _contentType = F("image/jpeg");
586-
else if (path.endsWith(F(".ico"))) _contentType = F("image/x-icon");
587-
else if (path.endsWith(F(".svg"))) _contentType = F("image/svg+xml");
588-
else if (path.endsWith(F(".eot"))) _contentType = F("font/eot");
589-
else if (path.endsWith(F(".woff"))) _contentType = F("font/woff");
590-
else if (path.endsWith(F(".woff2"))) _contentType = F("font/woff2");
591-
else if (path.endsWith(F(".ttf"))) _contentType = F("font/ttf");
592-
else if (path.endsWith(F(".xml"))) _contentType = F("text/xml");
593-
else if (path.endsWith(F(".pdf"))) _contentType = F("application/pdf");
594-
else if (path.endsWith(F(".zip"))) _contentType = F("application/zip");
595-
else if(path.endsWith(GZIP_EXTENSION)) _contentType = F("application/x-gzip");
596-
else _contentType = F("text/plain");
575+
_contentType = contentTypeFor(path);
597576
}
598577

599578
static File fs_open_zipped(FS& fs, const String& path, bool force_absolute) {
600579
if (!force_absolute && !fs.exists(path)) {
601-
auto gz_path = path + GZIP_EXTENSION;
580+
auto gz_path = path + "." + FPSTR(GZIP_EXTENSION);
602581
if (fs.exists(gz_path)) return fs.open(gz_path, "r");
603582
}
604583
return fs.open(path, "r");
@@ -611,7 +590,7 @@ AsyncFileResponse::AsyncFileResponse(File content, const String& path, const Str
611590
_code = 200;
612591
_path = path;
613592

614-
if(!download && String(content.name()).endsWith(GZIP_EXTENSION) && !path.endsWith(GZIP_EXTENSION)){
593+
if(!download && String(content.name()).endsWith(FPSTR(GZIP_EXTENSION)) && !path.endsWith(FPSTR(GZIP_EXTENSION))){
615594
addHeader(F("Content-Encoding"), F("gzip"));
616595
_callback = nullptr; // Unable to process gzipped templates
617596
_sendContentLength = true;

0 commit comments

Comments
 (0)