Check if file exists before returning canHandle true for serveStatic

This commit is contained in:
Andrew Melvin 2016-01-28 00:06:08 +02:00
parent c6b2fa0ad4
commit 09ed9271a2
2 changed files with 38 additions and 17 deletions

View File

@ -13,6 +13,7 @@
class AsyncStaticWebHandler: public AsyncWebHandler {
private:
String _getPath(AsyncWebServerRequest *request);
protected:
FS _fs;
String _uri;
@ -37,6 +38,7 @@ class AsyncStaticWebHandler: public AsyncWebHandler {
}
bool canHandle(AsyncWebServerRequest *request);
void handleRequest(AsyncWebServerRequest *request);
};
class AsyncCallbackWebHandler: public AsyncWebHandler {

View File

@ -12,40 +12,60 @@ bool AsyncStaticWebHandler::canHandle(AsyncWebServerRequest *request)
if (request->method() != HTTP_GET) {
return false;
}
if ((_isFile && request->url() != _uri) || !request->url().startsWith(_uri)) {
if ((_isFile && request->url() != _uri) ) {
return false;
}
return true;
// 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 false;
}
void AsyncStaticWebHandler::handleRequest(AsyncWebServerRequest *request)
String AsyncStaticWebHandler::_getPath(AsyncWebServerRequest *request)
{
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(" [request] url = %s\n", request->url().c_str() );
if (!_isFile) {
DEBUGF(" _isFile = false\n");
String baserequestUrl = request->url().substring(_uri.length()); // this is the request - stored _uri... /espman/
DEBUGF(" baserequestUrl = %s\n", baserequestUrl.c_str());
DEBUGF(" _isFile = false\n");
String baserequestUrl = request->url().substring(_uri.length()); // this is the request - stored _uri... /espman/
DEBUGF(" baserequestUrl = %s\n", baserequestUrl.c_str());
if (!baserequestUrl.length()) {
baserequestUrl += "/";
}
if (!baserequestUrl.length()) {
baserequestUrl += "/";
}
path = _path + baserequestUrl;
DEBUGF(" path = path + baserequestUrl, path = %s\n", path.c_str());
path = _path + baserequestUrl;
DEBUGF(" path = path + baserequestUrl, path = %s\n", path.c_str());
if (path.endsWith("/")) {
DEBUGF(" 3 path ends with / : path = index.htm \n");
DEBUGF(" 3 path ends with / : path = index.htm \n");
path += "index.htm";
}
} else {
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")) {
AsyncWebServerResponse * response = request->beginResponse(_fs, path);
@ -57,6 +77,5 @@ void AsyncStaticWebHandler::handleRequest(AsyncWebServerRequest *request)
}
path = String();
DEBUGF("[AsyncStaticWebHandler::handleRequest] END\n\n");
}