Files
ESPAsyncWebServer/src/WebHandlers.cpp

96 lines
2.9 KiB
C++
Raw Normal View History

2015-12-19 18:53:33 +02:00
/*
Asynchronous WebServer library for Espressif MCUs
Copyright (c) 2016 Hristo Gochkov. All rights reserved.
This file is part of the esp8266 core for Arduino environment.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
2015-12-19 18:53:33 +02:00
#include "ESPAsyncWebServer.h"
#include "WebHandlerImpl.h"
2015-12-19 18:53:33 +02:00
2016-01-27 21:59:27 +02:00
bool AsyncStaticWebHandler::canHandle(AsyncWebServerRequest *request)
{
if (request->method() != HTTP_GET) {
2015-12-19 18:53:33 +02:00
return false;
2016-01-27 21:59:27 +02:00
}
if ((_isFile && request->url() != _uri) ) {
2015-12-19 18:53:33 +02:00
return false;
2016-01-27 21:59:27 +02:00
}
// 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;
2015-12-19 18:53:33 +02:00
}
String AsyncStaticWebHandler::_getPath(AsyncWebServerRequest *request)
2016-01-27 21:59:27 +02:00
{
2015-12-19 18:53:33 +02:00
String path = request->url();
DEBUGF("[AsyncStaticWebHandler::_getPath]\n");
2016-01-27 22:16:21 +02:00
DEBUGF(" [stored] _uri = %s, _path = %s\n" , _uri.c_str(), _path.c_str() ) ;
DEBUGF(" [request] url = %s\n", request->url().c_str() );
2016-01-27 21:59:27 +02:00
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());
if (!baserequestUrl.length()) {
baserequestUrl += "/";
}
2016-01-27 21:59:27 +02:00
path = _path + baserequestUrl;
DEBUGF(" path = path + baserequestUrl, path = %s\n", path.c_str());
2016-01-27 23:42:16 +02:00
2016-01-27 21:59:27 +02:00
if (path.endsWith("/")) {
DEBUGF(" 3 path ends with / : path = index.htm \n");
2015-12-19 18:53:33 +02:00
path += "index.htm";
2016-01-27 21:59:27 +02:00
}
} else {
path = _path;
2015-12-19 18:53:33 +02:00
}
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);
2015-12-19 18:53:33 +02:00
2016-01-27 21:59:27 +02:00
if (_fs.exists(path) || _fs.exists(path + ".gz")) {
2015-12-19 18:53:33 +02:00
AsyncWebServerResponse * response = request->beginResponse(_fs, path);
if (_cache_header.length() != 0)
response->addHeader("Cache-Control", _cache_header);
request->send(response);
2016-01-27 21:59:27 +02:00
} else {
2015-12-19 18:53:33 +02:00
request->send(404);
2016-01-27 21:59:27 +02:00
}
2015-12-19 18:53:33 +02:00
path = String();
2016-01-27 21:59:27 +02:00
2015-12-19 18:53:33 +02:00
}