forked from me-no-dev/ESPAsyncWebServer
Merge pull request #10 from sticilface/fix-serveStatic
Fix serve static
This commit is contained in:
@@ -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;
|
||||||
@@ -26,17 +27,18 @@ class AsyncStaticWebHandler: public AsyncWebHandler {
|
|||||||
_isFile = _fs.exists(path) || _fs.exists((String(path)+".gz").c_str());
|
_isFile = _fs.exists(path) || _fs.exists((String(path)+".gz").c_str());
|
||||||
if (_uri != "/" && _uri.endsWith("/")) {
|
if (_uri != "/" && _uri.endsWith("/")) {
|
||||||
_uri = _uri.substring(0, _uri.length() - 1);
|
_uri = _uri.substring(0, _uri.length() - 1);
|
||||||
DEBUGF("[AsyncStaticWebHandler] _uri / removed");
|
DEBUGF("[AsyncStaticWebHandler] _uri / removed\n");
|
||||||
}
|
}
|
||||||
if (_path != "/" && _path.endsWith("/")) {
|
if (_path != "/" && _path.endsWith("/")) {
|
||||||
_path = _path.substring(0, _path.length() - 1);
|
_path = _path.substring(0, _path.length() - 1);
|
||||||
DEBUGF("[AsyncStaticWebHandler] _path / removed");
|
DEBUGF("[AsyncStaticWebHandler] _path / removed\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
bool canHandle(AsyncWebServerRequest *request);
|
bool canHandle(AsyncWebServerRequest *request);
|
||||||
void handleRequest(AsyncWebServerRequest *request);
|
void handleRequest(AsyncWebServerRequest *request);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class AsyncCallbackWebHandler: public AsyncWebHandler {
|
class AsyncCallbackWebHandler: public AsyncWebHandler {
|
||||||
|
@@ -9,7 +9,7 @@
|
|||||||
|
|
||||||
#include "StringArray.h"
|
#include "StringArray.h"
|
||||||
|
|
||||||
#define DEBUGF(...) //os_printf(__VA_ARGS__)
|
#define DEBUGF(...) //Serial.printf(__VA_ARGS__)
|
||||||
|
|
||||||
|
|
||||||
class AsyncWebServer;
|
class AsyncWebServer;
|
||||||
|
@@ -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() );
|
||||||
|
|
||||||
@@ -30,10 +40,13 @@ void AsyncStaticWebHandler::handleRequest(AsyncWebServerRequest *request)
|
|||||||
String baserequestUrl = request->url().substring(_uri.length()); // this is the request - stored _uri... /espman/
|
String baserequestUrl = request->url().substring(_uri.length()); // this is the request - stored _uri... /espman/
|
||||||
DEBUGF(" baserequestUrl = %s\n", baserequestUrl.c_str());
|
DEBUGF(" baserequestUrl = %s\n", baserequestUrl.c_str());
|
||||||
|
|
||||||
if (baserequestUrl.length()) {
|
if (!baserequestUrl.length()) {
|
||||||
path = _path + baserequestUrl;
|
baserequestUrl += "/";
|
||||||
DEBUGF(" baserequestUrl length > 0: 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("/")) {
|
if (path.endsWith("/")) {
|
||||||
DEBUGF(" 3 path ends with / : path = index.htm \n");
|
DEBUGF(" 3 path ends with / : path = index.htm \n");
|
||||||
path += "index.htm";
|
path += "index.htm";
|
||||||
@@ -42,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);
|
||||||
@@ -54,6 +77,5 @@ void AsyncStaticWebHandler::handleRequest(AsyncWebServerRequest *request)
|
|||||||
}
|
}
|
||||||
path = String();
|
path = String();
|
||||||
|
|
||||||
DEBUGF("[AsyncStaticWebHandler::handleRequest] END\n\n");
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user