ESPAsyncWebServer/src/WebServer.cpp

184 lines
5.4 KiB
C++
Raw Normal View History

/*
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 08:53:33 -08:00
#include "ESPAsyncWebServer.h"
#include "WebHandlerImpl.h"
2015-12-19 08:53:33 -08:00
AsyncWebServer::AsyncWebServer(uint16_t port):_server(port), _rewrites(0), _handlers(0){
2016-04-08 07:43:16 -07:00
_catchAllHandler = new AsyncCallbackWebHandler();
if(_catchAllHandler == NULL)
return;
2015-12-19 08:53:33 -08:00
_server.onClient([](void *s, AsyncClient* c){
2016-04-08 07:43:16 -07:00
if(c == NULL)
return;
c->setRxTimeout(3);
2016-04-08 07:43:16 -07:00
AsyncWebServerRequest *r = new AsyncWebServerRequest((AsyncWebServer*)s, c);
if(r == NULL){
c->close(true);
c->free();
delete c;
}
2015-12-19 08:53:33 -08:00
}, this);
}
AsyncWebServer::~AsyncWebServer(){
while(_rewrites != NULL){
AsyncWebRewrite *r = _rewrites;
_rewrites = r->next;
delete r;
}
while(_handlers != NULL){
AsyncWebHandler *h = _handlers;
_handlers = h->next;
delete h;
}
if (_catchAllHandler != NULL){
delete _catchAllHandler;
}
}
AsyncWebRewrite& AsyncWebServer::addRewrite(AsyncWebRewrite* rewrite){
if (_rewrites == NULL){
_rewrites = rewrite;
} else {
AsyncWebRewrite *r = _rewrites;
while(r->next != NULL) r = r->next;
r->next = rewrite;
}
return *rewrite;
}
AsyncWebRewrite& AsyncWebServer::rewrite(const char* from, const char* to){
return addRewrite(new AsyncWebRewrite(from, to));
}
AsyncWebHandler& AsyncWebServer::addHandler(AsyncWebHandler* handler){
2015-12-19 08:53:33 -08:00
if(_handlers == NULL){
_handlers = handler;
} else {
AsyncWebHandler* h = _handlers;
while(h->next != NULL) h = h->next;
h->next = handler;
}
return *handler;
2015-12-19 08:53:33 -08:00
}
void AsyncWebServer::begin(){
_server.begin();
}
2016-08-25 16:44:20 -07:00
#if ASYNC_TCP_SSL_ENABLED
2016-08-24 12:32:27 -07:00
void AsyncWebServer::onSslFileRequest(AcSSlFileHandler cb, void* arg){
_server.onSslFileRequest(cb, arg);
}
void AsyncWebServer::beginSecure(const char *cert, const char *key, const char *password){
_server.beginSecure(cert, key, password);
}
2016-08-25 16:44:20 -07:00
#endif
2016-08-24 12:32:27 -07:00
2015-12-19 08:53:33 -08:00
void AsyncWebServer::_handleDisconnect(AsyncWebServerRequest *request){
delete request;
}
void AsyncWebServer::_rewriteRequest(AsyncWebServerRequest *request){
AsyncWebRewrite *r = _rewrites;
while(r){
if (r->from() == request->_url && r->filter(request)){
request->_url = r->toUrl();
request->_addGetParams(r->params());
}
r = r->next;
}
}
void AsyncWebServer::_attachHandler(AsyncWebServerRequest *request){
2015-12-19 08:53:33 -08:00
if(_handlers){
AsyncWebHandler* h = _handlers;
while(h){
if (h->filter(request) && h->canHandle(request)){
request->setHandler(h);
return;
}
h = h->next;
2015-12-19 08:53:33 -08:00
}
}
request->addInterestingHeader("ANY");
2015-12-19 08:53:33 -08:00
request->setHandler(_catchAllHandler);
}
AsyncCallbackWebHandler& AsyncWebServer::on(const char* uri, WebRequestMethodComposite method, ArRequestHandlerFunction onRequest, ArUploadHandlerFunction onUpload, ArBodyHandlerFunction onBody){
2015-12-19 08:53:33 -08:00
AsyncCallbackWebHandler* handler = new AsyncCallbackWebHandler();
handler->setUri(uri);
handler->setMethod(method);
handler->onRequest(onRequest);
handler->onUpload(onUpload);
handler->onBody(onBody);
addHandler(handler);
return *handler;
2015-12-19 08:53:33 -08:00
}
AsyncCallbackWebHandler& AsyncWebServer::on(const char* uri, WebRequestMethodComposite method, ArRequestHandlerFunction onRequest, ArUploadHandlerFunction onUpload){
2015-12-19 08:53:33 -08:00
AsyncCallbackWebHandler* handler = new AsyncCallbackWebHandler();
handler->setUri(uri);
handler->setMethod(method);
handler->onRequest(onRequest);
handler->onUpload(onUpload);
addHandler(handler);
return *handler;
2015-12-19 08:53:33 -08:00
}
AsyncCallbackWebHandler& AsyncWebServer::on(const char* uri, WebRequestMethodComposite method, ArRequestHandlerFunction onRequest){
2015-12-19 08:53:33 -08:00
AsyncCallbackWebHandler* handler = new AsyncCallbackWebHandler();
handler->setUri(uri);
handler->setMethod(method);
handler->onRequest(onRequest);
addHandler(handler);
return *handler;
2015-12-19 08:53:33 -08:00
}
AsyncCallbackWebHandler& AsyncWebServer::on(const char* uri, ArRequestHandlerFunction onRequest){
2015-12-19 09:50:42 -08:00
AsyncCallbackWebHandler* handler = new AsyncCallbackWebHandler();
handler->setUri(uri);
handler->onRequest(onRequest);
addHandler(handler);
return *handler;
2015-12-19 08:53:33 -08:00
}
AsyncStaticWebHandler& AsyncWebServer::serveStatic(const char* uri, fs::FS& fs, const char* path, const char* cache_control){
AsyncStaticWebHandler* handler = new AsyncStaticWebHandler(uri, fs, path, cache_control);
addHandler(handler);
return *handler;
2015-12-19 08:53:33 -08:00
}
void AsyncWebServer::onNotFound(ArRequestHandlerFunction fn){
((AsyncCallbackWebHandler*)_catchAllHandler)->onRequest(fn);
}
void AsyncWebServer::onFileUpload(ArUploadHandlerFunction fn){
((AsyncCallbackWebHandler*)_catchAllHandler)->onUpload(fn);
}
void AsyncWebServer::onRequestBody(ArBodyHandlerFunction fn){
((AsyncCallbackWebHandler*)_catchAllHandler)->onBody(fn);
}