From d67ca5cbda2d35eba8a99ffd0269dade2505c393 Mon Sep 17 00:00:00 2001 From: Andrew Melvin Date: Wed, 27 Jan 2016 21:59:27 +0200 Subject: [PATCH 1/2] fix serveStatic to map paths correctly --- src/AsyncWebServerHandlerImpl.h | 10 ++++++++ src/WebHandlers.cpp | 45 +++++++++++++++++++++++---------- 2 files changed, 42 insertions(+), 13 deletions(-) diff --git a/src/AsyncWebServerHandlerImpl.h b/src/AsyncWebServerHandlerImpl.h index 6f9133a..0cb2f4b 100644 --- a/src/AsyncWebServerHandlerImpl.h +++ b/src/AsyncWebServerHandlerImpl.h @@ -22,6 +22,16 @@ class AsyncStaticWebHandler: public AsyncWebHandler { AsyncStaticWebHandler(FS& fs, const char* path, const char* uri, const char* cache_header) : _fs(fs), _uri(uri), _path(path), _cache_header(cache_header){ _isFile = _fs.exists(path); + if (_uri != "/" && _uri.endsWith("/")) { + _uri = _uri.substring(0, _uri.length() - 1); + //os_printf("[AsyncStaticWebHandler] _uri / removed"); + } + if (_path != "/" && _path.endsWith("/")) { + _path = _path.substring(0, _path.length() - 1); + //os_printf("[AsyncStaticWebHandler] _path / removed"); + } + + } bool canHandle(AsyncWebServerRequest *request); void handleRequest(AsyncWebServerRequest *request); diff --git a/src/WebHandlers.cpp b/src/WebHandlers.cpp index de74b4a..7e24d75 100644 --- a/src/WebHandlers.cpp +++ b/src/WebHandlers.cpp @@ -7,34 +7,53 @@ #include "ESPAsyncWebServer.h" #include "AsyncWebServerHandlerImpl.h" -bool AsyncStaticWebHandler::canHandle(AsyncWebServerRequest *request){ - if(request->method() != HTTP_GET) +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) || !request->url().startsWith(_uri)) { return false; + } return true; } -void AsyncStaticWebHandler::handleRequest(AsyncWebServerRequest *request){ +void AsyncStaticWebHandler::handleRequest(AsyncWebServerRequest *request) +{ String path = request->url(); - if(!_isFile){ - if(_uri != "/"){ - path = path.substring(_uri.length()); - if(!path.length()) - path = "/"; - } - if(path.endsWith("/")) +// os_printf("[AsyncStaticWebHandler::handleRequest]\n"); +// os_printf(" [stored] _uri = %s, _path = %s\n" , _uri.c_str(), _path.c_str() ) ; +// os_printf(" [request] url = %s\n", request->url().c_str() ); + + if (!_isFile) { + //os_printf(" _isFile = false\n"); + String baserequestUrl = request->url().substring(_uri.length()); // this is the request - stored _uri... /espman/ + //os_printf(" baserequestUrl = %s\n", baserequestUrl.c_str()); + + if (baserequestUrl.length()) { + path = _path + baserequestUrl; + //os_printf(" baserequestUrl length > 0: path = path + baserequestUrl, path = %s\n", path.c_str()); + } + if (path.endsWith("/")) { + //os_printf(" 3 path ends with / : path = index.htm \n"); path += "index.htm"; + } } else { path = _path; } + +// os_printf("[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); if (_cache_header.length() != 0) response->addHeader("Cache-Control", _cache_header); request->send(response); - } else + } else { request->send(404); + } path = String(); + +// os_printf("[AsyncStaticWebHandler::handleRequest] END\n\n"); + } From 9c5d0edf7642b067c23685d63938530c7d4aa0b9 Mon Sep 17 00:00:00 2001 From: Andrew Melvin Date: Wed, 27 Jan 2016 22:16:21 +0200 Subject: [PATCH 2/2] Add DEBUGF --- src/AsyncWebServerHandlerImpl.h | 5 +++-- src/ESPAsyncWebServer.h | 3 +++ src/WebHandlers.cpp | 18 +++++++++--------- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/AsyncWebServerHandlerImpl.h b/src/AsyncWebServerHandlerImpl.h index 8f4794f..9bb2ee7 100644 --- a/src/AsyncWebServerHandlerImpl.h +++ b/src/AsyncWebServerHandlerImpl.h @@ -8,6 +8,7 @@ #ifndef ASYNCWEBSERVERHANDLERIMPL_H_ #define ASYNCWEBSERVERHANDLERIMPL_H_ + #include "stddef.h" class AsyncStaticWebHandler: public AsyncWebHandler { @@ -25,11 +26,11 @@ 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); - //os_printf("[AsyncStaticWebHandler] _uri / removed"); + DEBUGF("[AsyncStaticWebHandler] _uri / removed"); } if (_path != "/" && _path.endsWith("/")) { _path = _path.substring(0, _path.length() - 1); - //os_printf("[AsyncStaticWebHandler] _path / removed"); + DEBUGF("[AsyncStaticWebHandler] _path / removed"); } diff --git a/src/ESPAsyncWebServer.h b/src/ESPAsyncWebServer.h index eb28239..67a34ad 100644 --- a/src/ESPAsyncWebServer.h +++ b/src/ESPAsyncWebServer.h @@ -9,6 +9,9 @@ #include "StringArray.h" +#define DEBUGF(...) //os_printf(__VA_ARGS__) + + class AsyncWebServer; class AsyncWebServerRequest; class AsyncWebServerResponse; diff --git a/src/WebHandlers.cpp b/src/WebHandlers.cpp index 7e24d75..5f5b71d 100644 --- a/src/WebHandlers.cpp +++ b/src/WebHandlers.cpp @@ -21,28 +21,28 @@ bool AsyncStaticWebHandler::canHandle(AsyncWebServerRequest *request) void AsyncStaticWebHandler::handleRequest(AsyncWebServerRequest *request) { String path = request->url(); -// os_printf("[AsyncStaticWebHandler::handleRequest]\n"); -// os_printf(" [stored] _uri = %s, _path = %s\n" , _uri.c_str(), _path.c_str() ) ; -// os_printf(" [request] url = %s\n", request->url().c_str() ); + 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) { - //os_printf(" _isFile = false\n"); + DEBUGF(" _isFile = false\n"); String baserequestUrl = request->url().substring(_uri.length()); // this is the request - stored _uri... /espman/ - //os_printf(" baserequestUrl = %s\n", baserequestUrl.c_str()); + DEBUGF(" baserequestUrl = %s\n", baserequestUrl.c_str()); if (baserequestUrl.length()) { path = _path + baserequestUrl; - //os_printf(" baserequestUrl length > 0: path = path + baserequestUrl, path = %s\n", path.c_str()); + DEBUGF(" baserequestUrl length > 0: path = path + baserequestUrl, path = %s\n", path.c_str()); } if (path.endsWith("/")) { - //os_printf(" 3 path ends with / : path = index.htm \n"); + DEBUGF(" 3 path ends with / : path = index.htm \n"); path += "index.htm"; } } else { path = _path; } -// os_printf("[AsyncStaticWebHandler::handleRequest] final path = %s\n", path.c_str()); + DEBUGF("[AsyncStaticWebHandler::handleRequest] final path = %s\n", path.c_str()); if (_fs.exists(path) || _fs.exists(path + ".gz")) { AsyncWebServerResponse * response = request->beginResponse(_fs, path); @@ -54,6 +54,6 @@ void AsyncStaticWebHandler::handleRequest(AsyncWebServerRequest *request) } path = String(); -// os_printf("[AsyncStaticWebHandler::handleRequest] END\n\n"); + DEBUGF("[AsyncStaticWebHandler::handleRequest] END\n\n"); }