martonmiklos: Add GZIP compression for static files

This commit is contained in:
Jeroen Domburg
2015-04-10 18:00:28 +02:00
parent 48a017c029
commit 3cb7b32678
4 changed files with 88 additions and 2 deletions
+46
View File
@@ -75,6 +75,19 @@ static const MimeMap mimeTypes[]={
{NULL, "text/html"}, //default value
};
// The static files with the following extensions from the HTML folder will be compressed and served with GZIP compression
// Add any other file types you want compressed here (and don't forget to modify the Makefile too)
#ifdef GZIP_COMPRESSION
static const char * gzippedFileTypes[] = {
"js",
"html",
"css",
NULL
};
static const char gzipNonSupportedMessage[] = "<html><head></head><body>Your browser does not accept gzip-compressed data.</body></html>";
#endif
//Returns a static char* to a mime type for a given url to a file.
const char ICACHE_FLASH_ATTR *httpdGetMimetype(char *url) {
int i=0;
@@ -88,6 +101,39 @@ const char ICACHE_FLASH_ATTR *httpdGetMimetype(char *url) {
return mimeTypes[i].mimetype;
}
#ifdef GZIP_COMPRESSION
//Sends Content-encoding header if the requested file was GZIP compressed
//If the client does not sent the Accept-encoding, send out a static html message.
const char* sendGZIPEncodingIfNeeded(HttpdConnData *connData) {
int i=0;
char acceptEncodingBuffer[64];
//Go find the extension
char *ext=connData->url+(strlen(connData->url)-1);
while (ext!=connData->url && *ext!='.') ext--;
if (*ext=='.') ext++;
//ToDo: os_strcmp is case sensitive; we may want to do case-intensive matching here...
while (gzippedFileTypes[i]!=NULL) {
if (os_strcmp(ext, gzippedFileTypes[i])==0) {
//when serving gzipped files check the browser's "Accept-Encoding" header
//if the client does not advertises that he accepts GZIP send a warning message (telnet users for e.g.)
httpdGetHeader(connData, "Accept-Encoding", acceptEncodingBuffer, 64);
if (os_strstr(acceptEncodingBuffer, "gzip") == NULL) {
//No Accept-Encoding: gzip header present
return gzipNonSupportedMessage;
} else {
httpdHeader(connData, "Content-Encoding", "gzip");
return NULL;
}
}
i++;
}
return NULL;
}
#endif
//Looks up the connData info for a specific esp connection
static HttpdConnData ICACHE_FLASH_ATTR *httpdFindConnData(void *arg) {
int i;
+4
View File
@@ -14,6 +14,7 @@
#define HTTPD_METHOD_GET 1
#define HTTPD_METHOD_POST 2
typedef struct HttpdPriv HttpdPriv;
typedef struct HttpdConnData HttpdConnData;
typedef struct HttpdPostData HttpdPostData;
@@ -58,6 +59,9 @@ int httpdUrlDecode(char *val, int valLen, char *ret, int retLen);
int ICACHE_FLASH_ATTR httpdFindArg(char *line, char *arg, char *buff, int buffLen);
void ICACHE_FLASH_ATTR httpdInit(HttpdBuiltInUrl *fixedUrls, int port);
const char *httpdGetMimetype(char *url);
#ifdef GZIP_COMPRESSION
const char* sendGZIPEncodingIfNeeded(HttpdConnData *connData);
#endif
void ICACHE_FLASH_ATTR httpdStartResponse(HttpdConnData *conn, int code);
void ICACHE_FLASH_ATTR httpdHeader(HttpdConnData *conn, const char *field, const char *val);
void ICACHE_FLASH_ATTR httpdEndHeaders(HttpdConnData *conn);
+11
View File
@@ -31,6 +31,9 @@ int ICACHE_FLASH_ATTR cgiEspFsHook(HttpdConnData *connData) {
EspFsFile *file=connData->cgiData;
int len;
char buff[1024];
#ifdef GZIP_COMPRESSION
const char *gzipSendResult = NULL;
#endif
if (connData->conn==NULL) {
//Connection aborted. Clean up.
@@ -47,6 +50,14 @@ int ICACHE_FLASH_ATTR cgiEspFsHook(HttpdConnData *connData) {
connData->cgiData=file;
httpdStartResponse(connData, 200);
httpdHeader(connData, "Content-Type", httpdGetMimetype(connData->url));
#ifdef GZIP_COMPRESSION
gzipSendResult = sendGZIPEncodingIfNeeded(connData);
if (gzipSendResult != NULL) {
httpdEndHeaders(connData);
httpdSend(connData, gzipSendResult, os_strlen(gzipSendResult));
return HTTPD_CGI_DONE;
}
#endif
httpdHeader(connData, "Cache-Control", "max-age=3600, must-revalidate");
httpdEndHeaders(connData);
return HTTPD_CGI_MORE;