Skip to content

Commit

Permalink
cache header by mime, buggy tpl with % outside tags, cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
MightyPork committed Jul 23, 2017
1 parent daa039f commit f3dd1a2
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 21 deletions.
40 changes: 28 additions & 12 deletions core/httpd.c
Expand Up @@ -73,6 +73,8 @@ static const ICACHE_RODATA_ATTR MimeMap mimeTypes[]={
{"jpg", "image/jpeg"},
{"jpeg", "image/jpeg"},
{"png", "image/png"},
{"gif", "image/gif"},
{"bmp", "image/bmp"},
{"svg", "image/svg+xml"},
{"xml", "text/xml"},
{"json", "application/json"},
Expand All @@ -89,13 +91,6 @@ const char ICACHE_FLASH_ATTR *httpdGetMimetype(const char *url) {

while (mimeTypes[i].ext!=NULL && strcasecmp(ext, mimeTypes[i].ext)!=0) i++;

if (mimeTypes[i].ext==NULL) {
// we didn't find any proper match, check for common API paths ("pretty URLs")
if (strstarts(url, "/json/")) {
return "application/json";
}
}

return mimeTypes[i].mimetype;
}

Expand Down Expand Up @@ -129,6 +124,22 @@ const char ICACHE_FLASH_ATTR *code2str(int code)
}
}

/**
* Add sensible cache control headers to avoid needless asset reloading
*
* @param connData
* @param mime - mime type string
*/
void httpdAddCacheHeaders(HttpdConnData *connData, const char *mime)
{
if (streq(mime, "text/html")) return;
if (streq(mime, "text/plain")) return;
if (streq(mime, "text/csv")) return;
if (streq(mime, "application/json")) return;

httpdHeader(connData, "Cache-Control", "max-age=3600, public, must-revalidate");
}

//Looks up the connData info for a specific connection
static HttpdConnData ICACHE_FLASH_ATTR *httpdFindConnData(ConnTypePtr conn, char *remIp, int remPort) {
for (int i=0; i<HTTPD_MAX_CONNECTIONS; i++) {
Expand Down Expand Up @@ -587,13 +598,18 @@ static void ICACHE_FLASH_ATTR httpdProcessRequest(HttpdConnData *conn) {
//Look up URL in the built-in URL table.
while (builtInUrls[i].url!=NULL) {
int match=0;
const char const * route = builtInUrls[i].url;
//See if there's a literal match
if (strcmp(builtInUrls[i].url, conn->url)==0) match=1;
//See if there's a wildcard match
if (builtInUrls[i].url[strlen(builtInUrls[i].url)-1]=='*' &&
strncmp(builtInUrls[i].url, conn->url, strlen(builtInUrls[i].url)-1)==0) match=1;
if (streq(route, conn->url)) match=1;
//See if there's a wildcard match (*)
if (last_char(route) == '*' &&
strneq(route, conn->url, strlen(route)-1)) match=1;
// Optional slash (/?)
if (last_char(route) == '?' && last_char_n(route, 2) == '/' &&
strneq(route, conn->url, strlen(route)-2) &&
strlen(conn->url) <= strlen(route)-1) match=1;
if (match) {
dbg("Matched route #%d, url=%s", i, builtInUrls[i].url);
dbg("Matched route #%d, url=%s", i, route);
conn->cgiData=NULL;
conn->cgi=builtInUrls[i].cgiCb;
conn->cgiArg=builtInUrls[i].cgiArg;
Expand Down
38 changes: 33 additions & 5 deletions core/httpdespfs.c
Expand Up @@ -129,11 +129,12 @@ serveStaticFile(HttpdConnData *connData, const char* filepath) {

connData->cgiData=file;
httpdStartResponse(connData, 200);
httpdHeader(connData, "Content-Type", httpdGetMimetype(filepath));
const char *mime = httpdGetMimetype(filepath);
httpdHeader(connData, "Content-Type", mime);
if (isGzip) {
httpdHeader(connData, "Content-Encoding", "gzip");
}
httpdHeader(connData, "Cache-Control", "max-age=3600, must-revalidate");
httpdAddCacheHeaders(connData, mime);
httpdEndHeaders(connData);
return HTTPD_CGI_MORE;
}
Expand Down Expand Up @@ -229,7 +230,9 @@ httpd_cgi_state ICACHE_FLASH_ATTR cgiEspFsTemplate(HttpdConnData *connData) {
}
connData->cgiData=tpd;
httpdStartResponse(connData, 200);
httpdHeader(connData, "Content-Type", httpdGetMimetype(connData->url));
const char *mime = httpdGetMimetype(connData->url);
httpdHeader(connData, "Content-Type", mime);
httpdAddCacheHeaders(connData, mime);
httpdEndHeaders(connData);
return HTTPD_CGI_MORE;
}
Expand Down Expand Up @@ -295,8 +298,33 @@ httpd_cgi_state ICACHE_FLASH_ATTR cgiEspFsTemplate(HttpdConnData *connData) {
//Go collect normal chars again.
e=&buff[x+1];
tpd->tokenPos=-1;
} else {
if (tpd->tokenPos<(sizeof(tpd->token)-1)) tpd->token[tpd->tokenPos++]=buff[x];
}
else {
// Add char to the token buf
char c = buff[x];
bool outOfSpace = tpd->tokenPos >= (sizeof(tpd->token) - 1);
if (outOfSpace ||
( !(c >= 'a' && c <= 'z') &&
!(c >= 'A' && c <= 'Z') &&
!(c >= '0' && c <= '9') &&
c != '.' && c != '_' && c != '-'
)) {
// looks like we collected some garbage
httpdSend(connData, "%", 1);
if (tpd->tokenPos > 0) {
httpdSend(connData, tpd->token, tpd->tokenPos);
}
// the bad char
httpdSend(connData, &c, 1);

//Go collect normal chars again.
e=&buff[x+1];
tpd->tokenPos=-1;
}
else {
// collect it
tpd->token[tpd->tokenPos++] = c;
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions include/httpd.h
Expand Up @@ -163,6 +163,7 @@ void httpdFlushSendBuffer(HttpdConnData *conn);
void httpdContinue(HttpdConnData *conn);
void httpdConnSendStart(HttpdConnData *conn);
void httpdConnSendFinish(HttpdConnData *conn);
void httpdAddCacheHeaders(HttpdConnData *connData, const char *mime);

//Platform dependent code should call these.
void httpdSentCb(ConnTypePtr conn, char *remIp, int remPort);
Expand Down
10 changes: 6 additions & 4 deletions include/platform.h
Expand Up @@ -33,11 +33,13 @@ typedef struct espconn* ConnTypePtr;
#define httpd_printf(format, ...) os_printf(format, ##__VA_ARGS__)

// Custom helpers
#define streq(a, b) (strcmp(a, b) == 0)
#define strneq(a, b, n) (strncmp(a, b, n) == 0)
#define strstarts(a, b) strneq(a, b, (int)strlen(b))
#define streq(a, b) (strcmp((const char*)(a), (const char*)(b)) == 0)
#define strneq(a, b, n) (strncmp((const char*)(a), (const char*)(b), n) == 0)
#define strstarts(a, b) strneq((a), (b), (int)strlen(b))
#define last_char_n(str, n) (str)[strlen(str) - (n)]
#define last_char(str) last_char_n(str, 1)
#endif



#endif
#endif

0 comments on commit f3dd1a2

Please sign in to comment.