mirror of
https://github.com/esp8266/Arduino.git
synced 2025-04-21 10:26:06 +03:00
Merge pull request #1007 from sticilface/master
Changes to static request handler
This commit is contained in:
commit
a6cb40f9d8
@ -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() {
|
||||||
|
@ -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
|
||||||
|
|
||||||
|
@ -31,16 +31,16 @@ 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;
|
||||||
@ -49,21 +49,39 @@ public:
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
String path(_path);
|
String path(_path);
|
||||||
|
|
||||||
|
if(path.endsWith("/")) path += "index.htm";
|
||||||
|
|
||||||
if (!_isFile) {
|
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;
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user