2015-12-19 18:53:33 +02:00
|
|
|
|
/*
|
|
|
|
|
* AsyncWebServerHandlerImpl.h
|
|
|
|
|
*
|
|
|
|
|
* Created on: 19.12.2015 г.
|
|
|
|
|
* Author: ficeto
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef ASYNCWEBSERVERHANDLERIMPL_H_
|
|
|
|
|
#define ASYNCWEBSERVERHANDLERIMPL_H_
|
|
|
|
|
|
2016-01-27 22:16:21 +02:00
|
|
|
|
|
2015-12-19 18:53:33 +02:00
|
|
|
|
#include "stddef.h"
|
|
|
|
|
|
|
|
|
|
class AsyncStaticWebHandler: public AsyncWebHandler {
|
|
|
|
|
private:
|
2016-01-28 00:06:08 +02:00
|
|
|
|
String _getPath(AsyncWebServerRequest *request);
|
2015-12-19 18:53:33 +02:00
|
|
|
|
protected:
|
|
|
|
|
FS _fs;
|
|
|
|
|
String _uri;
|
|
|
|
|
String _path;
|
|
|
|
|
String _cache_header;
|
|
|
|
|
bool _isFile;
|
|
|
|
|
public:
|
|
|
|
|
AsyncStaticWebHandler(FS& fs, const char* path, const char* uri, const char* cache_header)
|
|
|
|
|
: _fs(fs), _uri(uri), _path(path), _cache_header(cache_header){
|
2016-01-27 22:01:05 +02:00
|
|
|
|
|
2016-01-23 19:08:28 +02:00
|
|
|
|
_isFile = _fs.exists(path) || _fs.exists((String(path)+".gz").c_str());
|
2016-01-27 21:59:27 +02:00
|
|
|
|
if (_uri != "/" && _uri.endsWith("/")) {
|
|
|
|
|
_uri = _uri.substring(0, _uri.length() - 1);
|
2016-01-27 23:42:16 +02:00
|
|
|
|
DEBUGF("[AsyncStaticWebHandler] _uri / removed\n");
|
2016-01-27 21:59:27 +02:00
|
|
|
|
}
|
|
|
|
|
if (_path != "/" && _path.endsWith("/")) {
|
|
|
|
|
_path = _path.substring(0, _path.length() - 1);
|
2016-01-27 23:42:16 +02:00
|
|
|
|
DEBUGF("[AsyncStaticWebHandler] _path / removed\n");
|
2016-01-27 21:59:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-12-19 18:53:33 +02:00
|
|
|
|
}
|
|
|
|
|
bool canHandle(AsyncWebServerRequest *request);
|
|
|
|
|
void handleRequest(AsyncWebServerRequest *request);
|
2016-01-28 00:06:08 +02:00
|
|
|
|
|
2015-12-19 18:53:33 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class AsyncCallbackWebHandler: public AsyncWebHandler {
|
|
|
|
|
private:
|
|
|
|
|
protected:
|
|
|
|
|
String _uri;
|
|
|
|
|
WebRequestMethod _method;
|
|
|
|
|
ArRequestHandlerFunction _onRequest;
|
|
|
|
|
ArUploadHandlerFunction _onUpload;
|
|
|
|
|
ArBodyHandlerFunction _onBody;
|
|
|
|
|
public:
|
|
|
|
|
AsyncCallbackWebHandler() : _uri(), _method(HTTP_ANY), _onRequest(NULL), _onUpload(NULL), _onBody(NULL){}
|
|
|
|
|
void setUri(String uri){ _uri = uri; }
|
|
|
|
|
void setMethod(WebRequestMethod method){ _method = method; }
|
|
|
|
|
void onRequest(ArRequestHandlerFunction fn){ _onRequest = fn; }
|
|
|
|
|
void onUpload(ArUploadHandlerFunction fn){ _onUpload = fn; }
|
|
|
|
|
void onBody(ArBodyHandlerFunction fn){ _onBody = fn; }
|
|
|
|
|
|
|
|
|
|
bool canHandle(AsyncWebServerRequest *request){
|
|
|
|
|
if(!_onRequest)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if(_method != HTTP_ANY && request->method() != _method)
|
|
|
|
|
return false;
|
|
|
|
|
|
2016-01-04 16:19:34 +02:00
|
|
|
|
if(_uri.length() && (_uri != request->url() && !request->url().startsWith(_uri+"/")))
|
2015-12-19 18:53:33 +02:00
|
|
|
|
return false;
|
|
|
|
|
|
2015-12-19 21:09:29 +02:00
|
|
|
|
request->addInterestingHeader("ANY");
|
2015-12-19 18:53:33 +02:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
void handleRequest(AsyncWebServerRequest *request){
|
|
|
|
|
if(_onRequest)
|
|
|
|
|
_onRequest(request);
|
|
|
|
|
else
|
|
|
|
|
request->send(500);
|
|
|
|
|
}
|
|
|
|
|
void handleUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final){
|
|
|
|
|
if(_onUpload)
|
|
|
|
|
_onUpload(request, filename, index, data, len, final);
|
|
|
|
|
}
|
|
|
|
|
void handleBody(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total){
|
|
|
|
|
if(_onBody)
|
|
|
|
|
_onBody(request, data, len, index, total);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif /* ASYNCWEBSERVERHANDLERIMPL_H_ */
|