Files
ESPAsyncWebServer/src/WebHandlers.cpp

60 lines
1.8 KiB
C++
Raw Normal View History

2015-12-19 18:53:33 +02:00
/*
* WebHandlers.cpp
*
* Created on: 18.12.2015 г.
* Author: ficeto
*/
#include "ESPAsyncWebServer.h"
#include "AsyncWebServerHandlerImpl.h"
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) || !request->url().startsWith(_uri)) {
2015-12-19 18:53:33 +02:00
return false;
2016-01-27 21:59:27 +02:00
}
2015-12-19 18:53:33 +02:00
return true;
}
2016-01-27 21:59:27 +02:00
void AsyncStaticWebHandler::handleRequest(AsyncWebServerRequest *request)
{
2015-12-19 18:53:33 +02:00
String path = request->url();
2016-01-27 21:59:27 +02:00
// 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");
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
}
2016-01-27 21:59:27 +02:00
// os_printf("[AsyncStaticWebHandler::handleRequest] final path = %s\n", path.c_str());
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
// os_printf("[AsyncStaticWebHandler::handleRequest] END\n\n");
2015-12-19 18:53:33 +02:00
}