Check if file exists before returning canHandle true for serveStatic
This commit is contained in:
parent
c6b2fa0ad4
commit
09ed9271a2
|
@ -13,6 +13,7 @@
|
||||||
|
|
||||||
class AsyncStaticWebHandler: public AsyncWebHandler {
|
class AsyncStaticWebHandler: public AsyncWebHandler {
|
||||||
private:
|
private:
|
||||||
|
String _getPath(AsyncWebServerRequest *request);
|
||||||
protected:
|
protected:
|
||||||
FS _fs;
|
FS _fs;
|
||||||
String _uri;
|
String _uri;
|
||||||
|
@ -37,6 +38,7 @@ class AsyncStaticWebHandler: public AsyncWebHandler {
|
||||||
}
|
}
|
||||||
bool canHandle(AsyncWebServerRequest *request);
|
bool canHandle(AsyncWebServerRequest *request);
|
||||||
void handleRequest(AsyncWebServerRequest *request);
|
void handleRequest(AsyncWebServerRequest *request);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class AsyncCallbackWebHandler: public AsyncWebHandler {
|
class AsyncCallbackWebHandler: public AsyncWebHandler {
|
||||||
|
|
|
@ -12,16 +12,26 @@ bool AsyncStaticWebHandler::canHandle(AsyncWebServerRequest *request)
|
||||||
if (request->method() != HTTP_GET) {
|
if (request->method() != HTTP_GET) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if ((_isFile && request->url() != _uri) || !request->url().startsWith(_uri)) {
|
if ((_isFile && request->url() != _uri) ) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
// if the root of the request matches the _uri then it checks to see if there is a file it can handle.
|
||||||
|
if (request->url().startsWith(_uri)) {
|
||||||
|
String path = _getPath(request);
|
||||||
|
if (_fs.exists(path) || _fs.exists(path + ".gz")) {
|
||||||
|
DEBUGF("[AsyncStaticWebHandler::canHandle] TRUE\n");
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void AsyncStaticWebHandler::handleRequest(AsyncWebServerRequest *request)
|
String AsyncStaticWebHandler::_getPath(AsyncWebServerRequest *request)
|
||||||
{
|
{
|
||||||
|
|
||||||
String path = request->url();
|
String path = request->url();
|
||||||
DEBUGF("[AsyncStaticWebHandler::handleRequest]\n");
|
DEBUGF("[AsyncStaticWebHandler::_getPath]\n");
|
||||||
DEBUGF(" [stored] _uri = %s, _path = %s\n" , _uri.c_str(), _path.c_str() ) ;
|
DEBUGF(" [stored] _uri = %s, _path = %s\n" , _uri.c_str(), _path.c_str() ) ;
|
||||||
DEBUGF(" [request] url = %s\n", request->url().c_str() );
|
DEBUGF(" [request] url = %s\n", request->url().c_str() );
|
||||||
|
|
||||||
|
@ -45,7 +55,17 @@ void AsyncStaticWebHandler::handleRequest(AsyncWebServerRequest *request)
|
||||||
path = _path;
|
path = _path;
|
||||||
}
|
}
|
||||||
|
|
||||||
DEBUGF("[AsyncStaticWebHandler::handleRequest] final path = %s\n", path.c_str());
|
DEBUGF(" final path = %s\n", path.c_str());
|
||||||
|
DEBUGF("[AsyncStaticWebHandler::_getPath] END\n\n");
|
||||||
|
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void AsyncStaticWebHandler::handleRequest(AsyncWebServerRequest *request)
|
||||||
|
{
|
||||||
|
|
||||||
|
String path = _getPath(request);
|
||||||
|
|
||||||
if (_fs.exists(path) || _fs.exists(path + ".gz")) {
|
if (_fs.exists(path) || _fs.exists(path + ".gz")) {
|
||||||
AsyncWebServerResponse * response = request->beginResponse(_fs, path);
|
AsyncWebServerResponse * response = request->beginResponse(_fs, path);
|
||||||
|
@ -57,6 +77,5 @@ void AsyncStaticWebHandler::handleRequest(AsyncWebServerRequest *request)
|
||||||
}
|
}
|
||||||
path = String();
|
path = String();
|
||||||
|
|
||||||
DEBUGF("[AsyncStaticWebHandler::handleRequest] END\n\n");
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue