Merge pull request #10 from sticilface/fix-serveStatic

Fix serve static
This commit is contained in:
Me No Dev 2016-01-28 01:38:08 +02:00
commit 61b3d5e635
3 changed files with 42 additions and 18 deletions

View File

@ -13,6 +13,7 @@
class AsyncStaticWebHandler: public AsyncWebHandler {
private:
String _getPath(AsyncWebServerRequest *request);
protected:
FS _fs;
String _uri;
@ -26,17 +27,18 @@ class AsyncStaticWebHandler: public AsyncWebHandler {
_isFile = _fs.exists(path) || _fs.exists((String(path)+".gz").c_str());
if (_uri != "/" && _uri.endsWith("/")) {
_uri = _uri.substring(0, _uri.length() - 1);
DEBUGF("[AsyncStaticWebHandler] _uri / removed");
DEBUGF("[AsyncStaticWebHandler] _uri / removed\n");
}
if (_path != "/" && _path.endsWith("/")) {
_path = _path.substring(0, _path.length() - 1);
DEBUGF("[AsyncStaticWebHandler] _path / removed");
DEBUGF("[AsyncStaticWebHandler] _path / removed\n");
}
}
bool canHandle(AsyncWebServerRequest *request);
void handleRequest(AsyncWebServerRequest *request);
};
class AsyncCallbackWebHandler: public AsyncWebHandler {

View File

@ -9,7 +9,7 @@
#include "StringArray.h"
#define DEBUGF(...) //os_printf(__VA_ARGS__)
#define DEBUGF(...) //Serial.printf(__VA_ARGS__)
class AsyncWebServer;

View File

@ -12,37 +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 += "/";
}
path = _path + baserequestUrl;
DEBUGF(" path = path + baserequestUrl, path = %s\n", path.c_str());
if (baserequestUrl.length()) {
path = _path + baserequestUrl;
DEBUGF(" baserequestUrl length > 0: 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);
@ -54,6 +77,5 @@ void AsyncStaticWebHandler::handleRequest(AsyncWebServerRequest *request)
}
path = String();
DEBUGF("[AsyncStaticWebHandler::handleRequest] END\n\n");
}