1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-22 21:23:07 +03:00

Merge pull request #1007 from sticilface/master

Changes to static request handler
This commit is contained in:
Ivan Grokhotkov 2015-11-13 16:03:24 +03:00
commit a6cb40f9d8
3 changed files with 32 additions and 12 deletions

View File

@ -83,8 +83,8 @@ void ESP8266WebServer::_addRequestHandler(RequestHandler* handler) {
} }
} }
void ESP8266WebServer::serveStatic(const char* uri, FS& fs, const char* path) { void ESP8266WebServer::serveStatic(const char* uri, FS& fs, const char* path, const char* cache_header) {
_addRequestHandler(new StaticRequestHandler(fs, path, uri)); _addRequestHandler(new StaticRequestHandler(fs, path, uri, cache_header));
} }
void ESP8266WebServer::handleClient() { void ESP8266WebServer::handleClient() {

View File

@ -69,7 +69,7 @@ public:
void on(const char* uri, THandlerFunction handler); void on(const char* uri, THandlerFunction handler);
void on(const char* uri, HTTPMethod method, THandlerFunction fn); void on(const char* uri, HTTPMethod method, THandlerFunction fn);
void addHandler(RequestHandler* handler); void addHandler(RequestHandler* handler);
void serveStatic(const char* uri, fs::FS& fs, const char* path); void serveStatic(const char* uri, fs::FS& fs, const char* path, const char* cache_header = NULL );
void onNotFound(THandlerFunction fn); //called when handler is not assigned void onNotFound(THandlerFunction fn); //called when handler is not assigned
void onFileUpload(THandlerFunction fn); //handle file uploads void onFileUpload(THandlerFunction fn); //handle file uploads

View File

@ -31,39 +31,57 @@ protected:
class StaticRequestHandler : public RequestHandler { class StaticRequestHandler : public RequestHandler {
public: public:
StaticRequestHandler(FS& fs, const char* path, const char* uri) StaticRequestHandler(FS& fs, const char* path, const char* uri, const char* cache_header)
: _fs(fs) : _fs(fs)
, _uri(uri) , _uri(uri)
, _path(path) , _path(path)
, _cache_header(cache_header)
{ {
_isFile = fs.exists(path); _isFile = fs.exists(path);
DEBUGV("StaticRequestHandler: path=%s uri=%s isFile=%d\r\n", path, uri, _isFile); DEBUGV("StaticRequestHandler: path=%s uri=%s isFile=%d, cache_header=%s\r\n", path, uri, _isFile, cache_header);
_baseUriLength = _uri.length(); _baseUriLength = _uri.length();
} }
bool handle(ESP8266WebServer& server, HTTPMethod requestMethod, String requestUri) override { bool handle(ESP8266WebServer& server, HTTPMethod requestMethod, String requestUri) override {
if (requestMethod != HTTP_GET) if (requestMethod != HTTP_GET)
return false; return false;
DEBUGV("StaticRequestHandler::handle: request=%s _uri=%s\r\n", requestUri.c_str(), _uri.c_str()); DEBUGV("StaticRequestHandler::handle: request=%s _uri=%s\r\n", requestUri.c_str(), _uri.c_str());
if (!requestUri.startsWith(_uri)) if (!requestUri.startsWith(_uri))
return false; return false;
String path(_path); String path(_path);
if (!_isFile) {
if(path.endsWith("/")) path += "index.htm";
if (!_isFile) {
// Base URI doesn't point to a file. Append whatever follows this // Base URI doesn't point to a file. Append whatever follows this
// URI in request to get the file path. // URI in request to get the file path.
path += requestUri.substring(_baseUriLength); path += requestUri.substring(_baseUriLength);
} }
else if (requestUri != _uri) { else if (requestUri != _uri) {
// Base URI points to a file but request doesn't match this URI exactly // Base URI points to a file but request doesn't match this URI exactly
return false; return false;
} }
DEBUGV("StaticRequestHandler::handle: path=%s, isFile=%d\r\n", path.c_str(), _isFile); DEBUGV("StaticRequestHandler::handle: path=%s, isFile=%d\r\n", path.c_str(), _isFile);
String contentType = getContentType(path);
// look for gz file, only if the original specified path is not a gz. So part only works to send gzip via content encoding when a non compressed is asked for
// if you point the the path to gzip you will serve the gzip as content type "application/x-gzip", not text or javascript etc...
if (!path.endsWith(".gz") && !SPIFFS.exists(path)) {
String pathWithGz = path + ".gz";
if(SPIFFS.exists(pathWithGz))
path += ".gz";
}
File f = _fs.open(path, "r"); File f = _fs.open(path, "r");
if (!f) if (!f)
return false; return false;
server.streamFile(f, getContentType(path)); if (_cache_header.length() != 0)
server.sendHeader("Cache-Control", _cache_header);
server.streamFile(f, contentType);
return true; return true;
} }
@ -81,6 +99,7 @@ public:
else if (path.endsWith(".xml")) return "text/xml"; else if (path.endsWith(".xml")) return "text/xml";
else if (path.endsWith(".pdf")) return "application/pdf"; else if (path.endsWith(".pdf")) return "application/pdf";
else if (path.endsWith(".zip")) return "application/zip"; else if (path.endsWith(".zip")) return "application/zip";
else if(path.endsWith(".gz")) return "application/x-gzip";
return "application/octet-stream"; return "application/octet-stream";
} }
@ -88,6 +107,7 @@ protected:
FS _fs; FS _fs;
String _uri; String _uri;
String _path; String _path;
String _cache_header;
bool _isFile; bool _isFile;
size_t _baseUriLength; size_t _baseUriLength;
}; };