From 116380849577d005364d10642fe0a41349bffba3 Mon Sep 17 00:00:00 2001 From: Me No Dev Date: Thu, 25 Aug 2016 22:15:17 +0300 Subject: [PATCH] add some mime types and a commented implementation of etag for cache --- src/WebHandlers.cpp | 15 ++++++++++++++- src/WebResponses.cpp | 5 +++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/WebHandlers.cpp b/src/WebHandlers.cpp index d125992..fc5814e 100644 --- a/src/WebHandlers.cpp +++ b/src/WebHandlers.cpp @@ -89,6 +89,9 @@ bool AsyncStaticWebHandler::canHandle(AsyncWebServerRequest *request) if (_last_modified.length()) request->addInterestingHeader("If-Modified-Since"); + //if(_cache_control.length()) + // request->addInterestingHeader("If-None-Match"); + DEBUGF("[AsyncStaticWebHandler::canHandle] TRUE\n"); return true; } @@ -180,14 +183,24 @@ void AsyncStaticWebHandler::handleRequest(AsyncWebServerRequest *request) request->_tempObject = NULL; if (request->_tempFile == true) { + //String etag = String(request->_tempFile.size()); if (_last_modified.length() && _last_modified == request->header("If-Modified-Since")) { + request->_tempFile.close(); request->send(304); // Not modified + /*} else if (_cache_control.length() && request->hasHeader("If-None-Match") && request->header("If-None-Match").equals(etag)) { + request->_tempFile.close(); + AsyncWebServerResponse * response = new AsyncBasicResponse(304); // Not modified + response->addHeader("Cache-Control", _cache_control); + response->addHeader("ETag", etag); + request->send(response);*/ } else { AsyncWebServerResponse * response = new AsyncFileResponse(request->_tempFile, filename); if (_last_modified.length()) response->addHeader("Last-Modified", _last_modified); - if (_cache_control.length()) + if (_cache_control.length()){ response->addHeader("Cache-Control", _cache_control); + //response->addHeader("ETag", etag); + } request->send(response); } } else { diff --git a/src/WebResponses.cpp b/src/WebResponses.cpp index 2936c82..eacef6e 100644 --- a/src/WebResponses.cpp +++ b/src/WebResponses.cpp @@ -347,12 +347,17 @@ void AsyncFileResponse::_setContentType(String path){ if (path.endsWith(".html")) _contentType = "text/html"; else if (path.endsWith(".htm")) _contentType = "text/html"; else if (path.endsWith(".css")) _contentType = "text/css"; + else if (path.endsWith(".json")) _contentType = "text/json"; else if (path.endsWith(".js")) _contentType = "application/javascript"; else if (path.endsWith(".png")) _contentType = "image/png"; else if (path.endsWith(".gif")) _contentType = "image/gif"; else if (path.endsWith(".jpg")) _contentType = "image/jpeg"; else if (path.endsWith(".ico")) _contentType = "image/x-icon"; else if (path.endsWith(".svg")) _contentType = "image/svg+xml"; + else if (path.endsWith(".eot")) _contentType = "font/eot"; + else if (path.endsWith(".woff")) _contentType = "font/woff"; + else if (path.endsWith(".woff2")) _contentType = "font/woff2"; + else if (path.endsWith(".ttf")) _contentType = "font/ttf"; else if (path.endsWith(".xml")) _contentType = "text/xml"; else if (path.endsWith(".pdf")) _contentType = "application/pdf"; else if (path.endsWith(".zip")) _contentType = "application/zip";