Merge pull request #9 from sticilface/fix-serveStatic

Fix serve static
This commit is contained in:
Me No Dev
2016-01-27 22:55:25 +02:00
3 changed files with 47 additions and 13 deletions

View File

@@ -8,6 +8,7 @@
#ifndef ASYNCWEBSERVERHANDLERIMPL_H_ #ifndef ASYNCWEBSERVERHANDLERIMPL_H_
#define ASYNCWEBSERVERHANDLERIMPL_H_ #define ASYNCWEBSERVERHANDLERIMPL_H_
#include "stddef.h" #include "stddef.h"
class AsyncStaticWebHandler: public AsyncWebHandler { class AsyncStaticWebHandler: public AsyncWebHandler {
@@ -21,7 +22,18 @@ class AsyncStaticWebHandler: public AsyncWebHandler {
public: public:
AsyncStaticWebHandler(FS& fs, const char* path, const char* uri, const char* cache_header) AsyncStaticWebHandler(FS& fs, const char* path, const char* uri, const char* cache_header)
: _fs(fs), _uri(uri), _path(path), _cache_header(cache_header){ : _fs(fs), _uri(uri), _path(path), _cache_header(cache_header){
_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("/")) {
_uri = _uri.substring(0, _uri.length() - 1);
DEBUGF("[AsyncStaticWebHandler] _uri / removed");
}
if (_path != "/" && _path.endsWith("/")) {
_path = _path.substring(0, _path.length() - 1);
DEBUGF("[AsyncStaticWebHandler] _path / removed");
}
} }
bool canHandle(AsyncWebServerRequest *request); bool canHandle(AsyncWebServerRequest *request);
void handleRequest(AsyncWebServerRequest *request); void handleRequest(AsyncWebServerRequest *request);

View File

@@ -9,6 +9,9 @@
#include "StringArray.h" #include "StringArray.h"
#define DEBUGF(...) //os_printf(__VA_ARGS__)
class AsyncWebServer; class AsyncWebServer;
class AsyncWebServerRequest; class AsyncWebServerRequest;
class AsyncWebServerResponse; class AsyncWebServerResponse;

View File

@@ -7,34 +7,53 @@
#include "ESPAsyncWebServer.h" #include "ESPAsyncWebServer.h"
#include "AsyncWebServerHandlerImpl.h" #include "AsyncWebServerHandlerImpl.h"
bool AsyncStaticWebHandler::canHandle(AsyncWebServerRequest *request){ 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) || !request->url().startsWith(_uri)) {
return false; return false;
}
return true; return true;
} }
void AsyncStaticWebHandler::handleRequest(AsyncWebServerRequest *request){ void AsyncStaticWebHandler::handleRequest(AsyncWebServerRequest *request)
{
String path = request->url(); String path = request->url();
DEBUGF("[AsyncStaticWebHandler::handleRequest]\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) { if (!_isFile) {
if(_uri != "/"){ DEBUGF(" _isFile = false\n");
path = path.substring(_uri.length()); String baserequestUrl = request->url().substring(_uri.length()); // this is the request - stored _uri... /espman/
if(!path.length()) DEBUGF(" baserequestUrl = %s\n", baserequestUrl.c_str());
path = "/";
if (baserequestUrl.length()) {
path = _path + baserequestUrl;
DEBUGF(" baserequestUrl length > 0: path = path + baserequestUrl, path = %s\n", path.c_str());
} }
if(path.endsWith("/")) if (path.endsWith("/")) {
DEBUGF(" 3 path ends with / : path = index.htm \n");
path += "index.htm"; path += "index.htm";
}
} else { } else {
path = _path; path = _path;
} }
DEBUGF("[AsyncStaticWebHandler::handleRequest] final path = %s\n", path.c_str());
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);
if (_cache_header.length() != 0) if (_cache_header.length() != 0)
response->addHeader("Cache-Control", _cache_header); response->addHeader("Cache-Control", _cache_header);
request->send(response); request->send(response);
} else } else {
request->send(404); request->send(404);
path = String(); }
path = String();
DEBUGF("[AsyncStaticWebHandler::handleRequest] END\n\n");
} }