PsychicHTTP

This commit is contained in:
iranl
2024-08-26 23:41:54 +02:00
parent ca9c2feebc
commit 89393cee58
16 changed files with 1522 additions and 1447 deletions

View File

@@ -90,8 +90,8 @@ void MqttLogger::sendBuffer()
} }
if (doWebSerial) if (doWebSerial)
{ {
WebSerial.write(this->buffer, this->bufferCnt); //WebSerial.write(this->buffer, this->bufferCnt);
WebSerial.println(); //WebSerial.println();
} }
this->bufferCnt=0; this->bufferCnt=0;
} }

View File

@@ -12,7 +12,7 @@
#include <Arduino.h> #include <Arduino.h>
#include <Print.h> #include <Print.h>
#include <espMqttClient.h> #include <espMqttClient.h>
#include "MycilaWebSerial.h" //#include "MycilaWebSerial.h"
#define MQTT_MAX_PACKET_SIZE 1024 #define MQTT_MAX_PACKET_SIZE 1024

View File

@@ -325,11 +325,19 @@ const std::list<PsychicClient*>& PsychicHttpServer::getClientList() {
} }
bool ON_STA_FILTER(PsychicRequest *request) { bool ON_STA_FILTER(PsychicRequest *request) {
#ifndef CONFIG_IDF_TARGET_ESP32H2
return WiFi.localIP() == request->client()->localIP(); return WiFi.localIP() == request->client()->localIP();
#else
return false;
#endif
} }
bool ON_AP_FILTER(PsychicRequest *request) { bool ON_AP_FILTER(PsychicRequest *request) {
#ifndef CONFIG_IDF_TARGET_ESP32H2
return WiFi.softAPIP() == request->client()->localIP(); return WiFi.softAPIP() == request->client()->localIP();
#else
return false;
#endif
} }
String urlDecode(const char* encoded) String urlDecode(const char* encoded)

View File

@@ -318,6 +318,11 @@ PsychicWebParameter * PsychicRequest::addParam(PsychicWebParameter *param) {
return param; return param;
} }
int PsychicRequest::params()
{
return _params.size();
}
bool PsychicRequest::hasParam(const char *key) bool PsychicRequest::hasParam(const char *key)
{ {
return getParam(key) != NULL; return getParam(key) != NULL;
@@ -332,6 +337,18 @@ PsychicWebParameter * PsychicRequest::getParam(const char *key)
return NULL; return NULL;
} }
PsychicWebParameter * PsychicRequest::getParam(int index)
{
if (_params.size() > index){
std::list<PsychicWebParameter*>::iterator it = _params.begin();
for(int i=0; i<index; i++){
++it;
}
return *it;
}
return NULL;
}
bool PsychicRequest::hasSessionKey(const String& key) bool PsychicRequest::hasSessionKey(const String& key)
{ {
return this->_session->find(key) != this->_session->end(); return this->_session->find(key) != this->_session->end();

View File

@@ -81,9 +81,11 @@ class PsychicRequest {
void loadParams(); void loadParams();
PsychicWebParameter * addParam(PsychicWebParameter *param); PsychicWebParameter * addParam(PsychicWebParameter *param);
PsychicWebParameter * addParam(const String &name, const String &value, bool decode = true, bool post = false); PsychicWebParameter * addParam(const String &name, const String &value, bool decode = true, bool post = false);
int params();
bool hasParam(const char *key); bool hasParam(const char *key);
PsychicWebParameter * getParam(const char *name); PsychicWebParameter * getParam(const char *name);
PsychicWebParameter * getParam(int index);
const String getFilename(); const String getFilename();
bool authenticate(const char * username, const char * password); bool authenticate(const char * username, const char * password);

View File

@@ -627,30 +627,28 @@ boolean WiFiManager::configPortalHasTimeout(){
} }
void WiFiManager::setupHTTPServer(){ void WiFiManager::setupHTTPServer(){
server->listen(_httpPort);
server.reset(new WM_WebServer(_httpPort));
/* Setup httpd callbacks, web pages: root, wifi config pages, SO captive portal detectors and not found. */ /* Setup httpd callbacks, web pages: root, wifi config pages, SO captive portal detectors and not found. */
server->on(String(FPSTR(R_wifi)).c_str(), HTTP_ANY, std::bind(&WiFiManager::handleWifi, this,std::placeholders::_1,true)); server->on(String(FPSTR(R_wifi)).c_str(), (PsychicHttpRequestCallback)std::bind(&WiFiManager::handleWifi, this,std::placeholders::_1,true));
server->on(String(FPSTR(R_wifinoscan)).c_str(), HTTP_ANY, std::bind(&WiFiManager::handleWifi, this,std::placeholders::_1,false)); server->on(String(FPSTR(R_wifinoscan)).c_str(), (PsychicHttpRequestCallback)std::bind(&WiFiManager::handleWifi, this,std::placeholders::_1,false));
server->on(String(FPSTR(R_erase)).c_str(), HTTP_ANY, std::bind(&WiFiManager::handleErase, this,std::placeholders::_1,false)); server->on(String(FPSTR(R_erase)).c_str(), (PsychicHttpRequestCallback)std::bind(&WiFiManager::handleErase, this,std::placeholders::_1,false));
{ {
using namespace std::placeholders; using namespace std::placeholders;
server->on(String(FPSTR(R_root)).c_str(), HTTP_ANY, std::bind(&WiFiManager::handleRoot, this,_1)); server->on(String(FPSTR(R_root)).c_str(), (PsychicHttpRequestCallback)std::bind(&WiFiManager::handleRoot, this,_1));
server->on(String(FPSTR(R_wifisave)).c_str(), HTTP_ANY, std::bind(&WiFiManager::handleWifiSave, this,_1)); server->on(String(FPSTR(R_wifisave)).c_str(), (PsychicHttpRequestCallback)std::bind(&WiFiManager::handleWifiSave, this,_1));
server->on(String(FPSTR(R_info)).c_str(), HTTP_ANY, std::bind(&WiFiManager::handleInfo, this,_1)); server->on(String(FPSTR(R_info)).c_str(), (PsychicHttpRequestCallback)std::bind(&WiFiManager::handleInfo, this,_1));
server->on(String(FPSTR(R_param)).c_str(), HTTP_ANY, std::bind(&WiFiManager::handleParam, this,_1)); server->on(String(FPSTR(R_param)).c_str(), (PsychicHttpRequestCallback)std::bind(&WiFiManager::handleParam, this,_1));
server->on(String(FPSTR(R_paramsave)).c_str(), HTTP_ANY, std::bind(&WiFiManager::handleParamSave, this,_1)); server->on(String(FPSTR(R_paramsave)).c_str(), (PsychicHttpRequestCallback)std::bind(&WiFiManager::handleParamSave, this,_1));
server->on(String(FPSTR(R_restart)).c_str(), HTTP_ANY, std::bind(&WiFiManager::handleReset, this,_1)); server->on(String(FPSTR(R_restart)).c_str(), (PsychicHttpRequestCallback)std::bind(&WiFiManager::handleReset, this,_1));
server->on(String(FPSTR(R_exit)).c_str(), HTTP_ANY, std::bind(&WiFiManager::handleExit, this,_1)); server->on(String(FPSTR(R_exit)).c_str(), (PsychicHttpRequestCallback)std::bind(&WiFiManager::handleExit, this,_1));
server->on(String(FPSTR(R_close)).c_str(), HTTP_ANY, std::bind(&WiFiManager::handleClose, this,_1)); server->on(String(FPSTR(R_close)).c_str(), (PsychicHttpRequestCallback)std::bind(&WiFiManager::handleClose, this,_1));
server->on(String(FPSTR(R_status)).c_str(), HTTP_ANY, std::bind(&WiFiManager::handleWiFiStatus, this,_1)); server->on(String(FPSTR(R_status)).c_str(), (PsychicHttpRequestCallback)std::bind(&WiFiManager::handleWiFiStatus, this,_1));
server->onNotFound (std::bind(&WiFiManager::handleNotFound, this, _1)); server->onNotFound ((PsychicHttpRequestCallback)std::bind(&WiFiManager::handleNotFound, this, _1));
server->on(String(FPSTR(R_update)).c_str(), HTTP_ANY, std::bind(&WiFiManager::handleUpdate, this, _1)); server->on(String(FPSTR(R_update)).c_str(), (PsychicHttpRequestCallback)std::bind(&WiFiManager::handleUpdate, this, _1));
server->on(String(FPSTR(R_updatedone)).c_str(), HTTP_POST,std::bind(&WiFiManager::handleUpdateDone, this, _1), std::bind(&WiFiManager::handleUpdating, this, _1,_2,_3,_4,_5,_6)); //server->on(String(FPSTR(R_updatedone)).c_str(), HTTP_POST, std::bind(&WiFiManager::handleUpdateDone, this, _1), std::bind(&WiFiManager::handleUpdating, this, _1,_2,_3,_4,_5,_6));
} }
server->begin(); // Web server start
} }
void WiFiManager::teardownHTTPServer(){ void WiFiManager::teardownHTTPServer(){
@@ -979,7 +977,7 @@ bool WiFiManager::shutdownConfigPortal(){
// @todo what is the proper way to shutdown and free the server up // @todo what is the proper way to shutdown and free the server up
// debug - many open issues aobut port not clearing for use with other servers // debug - many open issues aobut port not clearing for use with other servers
#ifdef WM_ASYNCWEBSERVER #ifdef WM_ASYNCWEBSERVER
server->end(); server->stop();
#else #else
server->stop(); server->stop();
#endif #endif
@@ -1423,10 +1421,13 @@ String WiFiManager::getHTTPHead(String title){
return page; return page;
} }
void WiFiManager::HTTPSend(AsyncWebServerRequest *request, String page){ esp_err_t WiFiManager::HTTPSend(PsychicRequest *request, String page){
AsyncWebServerResponse *response = request->beginResponse(200,FPSTR(HTTP_HEAD_CT), page); PsychicResponse response(request);
response->addHeader(FPSTR(HTTP_HEAD_CL), String(page.length())); response.addHeader(HTTP_HEAD_CL, ((String)page.length()).c_str());
request->send(response); response.setCode(200);
response.setContentType(HTTP_HEAD_CT);
response.setContent(page.c_str());
return response.send();
} }
/** /**
@@ -1446,12 +1447,12 @@ void WiFiManager::handleRequest() {
/** /**
* HTTPD CALLBACK root or redirect to captive portal * HTTPD CALLBACK root or redirect to captive portal
*/ */
void WiFiManager::handleRoot(AsyncWebServerRequest *request) { esp_err_t WiFiManager::handleRoot(PsychicRequest *request) {
#ifdef WM_DEBUG_LEVEL #ifdef WM_DEBUG_LEVEL
DEBUG_WM(WM_DEBUG_VERBOSE,F("<- HTTP Root")); DEBUG_WM(WM_DEBUG_VERBOSE,F("<- HTTP Root"));
#endif #endif
if(strlen(_credUser) > 0 && strlen(_credPassword) > 0) if(!request->authenticate(_credUser, _credPassword)) return request->requestAuthentication(); if(strlen(_credUser) > 0 && strlen(_credPassword) > 0) if(!request->authenticate(_credUser, _credPassword)) return request->requestAuthentication(BASIC_AUTH, "Nuki Hub", "You must log in.");
if (captivePortal(request)) return; // If captive portal redirect instead of displaying the page if (captivePortal(request)) return 0; // If captive portal redirect instead of displaying the page
handleRequest(); handleRequest();
String page = getHTTPHead(_title); // @token options @todo replace options with title String page = getHTTPHead(_title); // @token options @todo replace options with title
String str = FPSTR(HTTP_ROOT_MAIN); // @todo custom title String str = FPSTR(HTTP_ROOT_MAIN); // @todo custom title
@@ -1463,28 +1464,29 @@ void WiFiManager::handleRoot(AsyncWebServerRequest *request) {
reportStatus(page); reportStatus(page);
page += FPSTR(HTTP_END); page += FPSTR(HTTP_END);
HTTPSend(request,page);
if(_preloadwifiscan) WiFi_scanNetworks(_scancachetime,true); // preload wifiscan throttled, async if(_preloadwifiscan) WiFi_scanNetworks(_scancachetime,true); // preload wifiscan throttled, async
// @todo buggy, captive portals make a query on every page load, causing this to run every time in addition to the real page load // @todo buggy, captive portals make a query on every page load, causing this to run every time in addition to the real page load
// I dont understand why, when you are already in the captive portal, I guess they want to know that its still up and not done or gone // I dont understand why, when you are already in the captive portal, I guess they want to know that its still up and not done or gone
// if we can detect these and ignore them that would be great, since they come from the captive portal redirect maybe there is a refferer // if we can detect these and ignore them that would be great, since they come from the captive portal redirect maybe there is a refferer
return HTTPSend(request,page);
} }
/** /**
* HTTPD CALLBACK Wifi config page handler * HTTPD CALLBACK Wifi config page handler
*/ */
void WiFiManager::handleWifi(AsyncWebServerRequest *request,bool scan = true) { esp_err_t WiFiManager::handleWifi(PsychicRequest *request,bool scan = true) {
#ifdef WM_DEBUG_LEVEL #ifdef WM_DEBUG_LEVEL
DEBUG_WM(WM_DEBUG_VERBOSE,F("<- HTTP Wifi")); DEBUG_WM(WM_DEBUG_VERBOSE,F("<- HTTP Wifi"));
#endif #endif
if(strlen(_credUser) > 0 && strlen(_credPassword) > 0) if(!request->authenticate(_credUser, _credPassword)) return request->requestAuthentication(); if(strlen(_credUser) > 0 && strlen(_credPassword) > 0) if(!request->authenticate(_credUser, _credPassword)) return request->requestAuthentication(BASIC_AUTH, "Nuki Hub", "You must log in.");
handleRequest(); handleRequest();
String page = getHTTPHead(FPSTR(S_titlewifi)); // @token titlewifi String page = getHTTPHead(FPSTR(S_titlewifi)); // @token titlewifi
if (scan) { if (scan) {
#ifdef WM_DEBUG_LEVEL #ifdef WM_DEBUG_LEVEL
// DEBUG_WM(WM_DEBUG_DEV,"refresh flag:",request->hasArg(F("refresh"))); // DEBUG_WM(WM_DEBUG_DEV,"refresh flag:",request->hasArg(F("refresh")));
#endif #endif
WiFi_scanNetworks(request->hasArg(F("refresh")),true); //wifiscan, force if arg refresh WiFi_scanNetworks(request->hasParam("refresh")); //wifiscan, force if arg refresh
page += getScanItemOut(); page += getScanItemOut();
} }
String pitem = ""; String pitem = "";
@@ -1520,21 +1522,21 @@ void WiFiManager::handleWifi(AsyncWebServerRequest *request,bool scan = true) {
reportStatus(page); reportStatus(page);
page += FPSTR(HTTP_END); page += FPSTR(HTTP_END);
HTTPSend(request,page);
#ifdef WM_DEBUG_LEVEL #ifdef WM_DEBUG_LEVEL
DEBUG_WM(WM_DEBUG_DEV,F("Sent config page")); DEBUG_WM(WM_DEBUG_DEV,F("Sent config page"));
#endif #endif
return HTTPSend(request,page);
} }
/** /**
* HTTPD CALLBACK Wifi param page handler * HTTPD CALLBACK Wifi param page handler
*/ */
void WiFiManager::handleParam(AsyncWebServerRequest *request){ esp_err_t WiFiManager::handleParam(PsychicRequest *request){
#ifdef WM_DEBUG_LEVEL #ifdef WM_DEBUG_LEVEL
DEBUG_WM(WM_DEBUG_VERBOSE,F("<- HTTP Param")); DEBUG_WM(WM_DEBUG_VERBOSE,F("<- HTTP Param"));
#endif #endif
if(strlen(_credUser) > 0 && strlen(_credPassword) > 0) if(!request->authenticate(_credUser, _credPassword)) return request->requestAuthentication(); if(strlen(_credUser) > 0 && strlen(_credPassword) > 0) if(!request->authenticate(_credUser, _credPassword)) return request->requestAuthentication(BASIC_AUTH, "Nuki Hub", "You must log in.");
handleRequest(); handleRequest();
String page = getHTTPHead(FPSTR(S_titleparam)); // @token titlewifi String page = getHTTPHead(FPSTR(S_titleparam)); // @token titlewifi
@@ -1550,11 +1552,11 @@ void WiFiManager::handleParam(AsyncWebServerRequest *request){
reportStatus(page); reportStatus(page);
page += FPSTR(HTTP_END); page += FPSTR(HTTP_END);
HTTPSend(request,page);
#ifdef WM_DEBUG_LEVEL #ifdef WM_DEBUG_LEVEL
DEBUG_WM(WM_DEBUG_DEV,F("Sent param page")); DEBUG_WM(WM_DEBUG_DEV,F("Sent param page"));
#endif #endif
return HTTPSend(request,page);
} }
@@ -1914,34 +1916,35 @@ String WiFiManager::getParamOut(){
return page; return page;
} }
void WiFiManager::handleWiFiStatus(AsyncWebServerRequest *request){ esp_err_t WiFiManager::handleWiFiStatus(PsychicRequest *request){
#ifdef WM_DEBUG_LEVEL #ifdef WM_DEBUG_LEVEL
DEBUG_WM(WM_DEBUG_VERBOSE,F("<- HTTP WiFi status ")); DEBUG_WM(WM_DEBUG_VERBOSE,F("<- HTTP WiFi status "));
#endif #endif
if(strlen(_credUser) > 0 && strlen(_credPassword) > 0) if(!request->authenticate(_credUser, _credPassword)) return request->requestAuthentication(); if(strlen(_credUser) > 0 && strlen(_credPassword) > 0) if(!request->authenticate(_credUser, _credPassword)) return request->requestAuthentication(BASIC_AUTH, "Nuki Hub", "You must log in.");
handleRequest(); handleRequest();
String page; String page;
// String page = "{\"result\":true,\"count\":1}"; // String page = "{\"result\":true,\"count\":1}";
#ifdef WM_JSTEST #ifdef WM_JSTEST
page = FPSTR(HTTP_JS); page = FPSTR(HTTP_JS);
#endif #endif
HTTPSend(request,page);
return HTTPSend(request,page);
} }
/** /**
* HTTPD CALLBACK save form and redirect to WLAN config page again * HTTPD CALLBACK save form and redirect to WLAN config page again
*/ */
void WiFiManager::handleWifiSave(AsyncWebServerRequest *request) { esp_err_t WiFiManager::handleWifiSave(PsychicRequest *request) {
#ifdef WM_DEBUG_LEVEL #ifdef WM_DEBUG_LEVEL
DEBUG_WM(WM_DEBUG_VERBOSE,F("<- HTTP WiFi save ")); DEBUG_WM(WM_DEBUG_VERBOSE,F("<- HTTP WiFi save "));
DEBUG_WM(WM_DEBUG_DEV,F("Method:"),request->method() == HTTP_GET ? (String)FPSTR(S_GET) : (String)FPSTR(S_POST)); DEBUG_WM(WM_DEBUG_DEV,F("Method:"),request->method() == HTTP_GET ? (String)FPSTR(S_GET) : (String)FPSTR(S_POST));
#endif #endif
if(strlen(_credUser) > 0 && strlen(_credPassword) > 0) if(!request->authenticate(_credUser, _credPassword)) return request->requestAuthentication(); if(strlen(_credUser) > 0 && strlen(_credPassword) > 0) if(!request->authenticate(_credUser, _credPassword)) return request->requestAuthentication(BASIC_AUTH, "Nuki Hub", "You must log in.");
handleRequest(); handleRequest();
//SAVE/connect here //SAVE/connect here
_ssid = request->arg(F("s")).c_str(); _ssid = request->getParam("s")->value();
_pass = request->arg(F("p")).c_str(); _pass = request->getParam("p")->value();
if(_ssid == "" && _pass != ""){ if(_ssid == "" && _pass != ""){
_ssid = WiFi_SSID(true); // password change, placeholder ssid, @todo compare pass to old?, confirm ssid is clean _ssid = WiFi_SSID(true); // password change, placeholder ssid, @todo compare pass to old?, confirm ssid is clean
@@ -1957,40 +1960,40 @@ void WiFiManager::handleWifiSave(AsyncWebServerRequest *request) {
requestinfo += "\nMethod: "; requestinfo += "\nMethod: ";
requestinfo += (request->method() == HTTP_GET) ? "GET" : "POST"; requestinfo += (request->method() == HTTP_GET) ? "GET" : "POST";
requestinfo += "\nArguments: "; requestinfo += "\nArguments: ";
requestinfo += request->args(); requestinfo += request->params();
requestinfo += "\n"; requestinfo += "\n";
for (uint8_t i = 0; i < request->args(); i++) { for (uint8_t i = 0; i < request->params(); i++) {
requestinfo += " " + request->argName(i) + ": " + request->arg(i) + "\n"; requestinfo += " " + request->getParam(i)->name() + ": " + request->getParam(i)->value() + "\n";
} }
DEBUG_WM(WM_DEBUG_MAX,requestinfo); DEBUG_WM(WM_DEBUG_MAX,requestinfo);
#endif #endif
// set static ips from server args // set static ips from server args
if (request->arg(FPSTR(S_ip)) != "") { if (request->getParam(S_ip)->value() != "") {
//_sta_static_ip.fromString(request->arg(FPSTR(S_ip)); //_sta_static_ip.fromString(request->arg(FPSTR(S_ip));
String ip = request->arg(FPSTR(S_ip)); String ip = request->getParam(S_ip)->value();
optionalIPFromString(&_sta_static_ip, ip.c_str()); optionalIPFromString(&_sta_static_ip, ip.c_str());
#ifdef WM_DEBUG_LEVEL #ifdef WM_DEBUG_LEVEL
DEBUG_WM(WM_DEBUG_DEV,F("static ip:"),ip); DEBUG_WM(WM_DEBUG_DEV,F("static ip:"),ip);
#endif #endif
} }
if (request->arg(FPSTR(S_gw)) != "") { if (request->getParam(S_gw)->value() != "") {
String gw = request->arg(FPSTR(S_gw)); String gw = request->getParam(S_gw)->value();
optionalIPFromString(&_sta_static_gw, gw.c_str()); optionalIPFromString(&_sta_static_gw, gw.c_str());
#ifdef WM_DEBUG_LEVEL #ifdef WM_DEBUG_LEVEL
DEBUG_WM(WM_DEBUG_DEV,F("static gateway:"),gw); DEBUG_WM(WM_DEBUG_DEV,F("static gateway:"),gw);
#endif #endif
} }
if (request->arg(FPSTR(S_sn)) != "") { if (request->getParam(S_sn)->value() != "") {
String sn = request->arg(FPSTR(S_sn)); String sn = request->getParam(S_sn)->value();
optionalIPFromString(&_sta_static_sn, sn.c_str()); optionalIPFromString(&_sta_static_sn, sn.c_str());
#ifdef WM_DEBUG_LEVEL #ifdef WM_DEBUG_LEVEL
DEBUG_WM(WM_DEBUG_DEV,F("static netmask:"),sn); DEBUG_WM(WM_DEBUG_DEV,F("static netmask:"),sn);
#endif #endif
} }
if (request->arg(FPSTR(S_dns)) != "") { if (request->getParam(S_dns)->value() != "") {
String dns = request->arg(FPSTR(S_dns)); String dns = request->getParam(S_dns)->value();
optionalIPFromString(&_sta_static_dns, dns.c_str()); optionalIPFromString(&_sta_static_dns, dns.c_str());
#ifdef WM_DEBUG_LEVEL #ifdef WM_DEBUG_LEVEL
DEBUG_WM(WM_DEBUG_DEV,F("static DNS:"),dns); DEBUG_WM(WM_DEBUG_DEV,F("static DNS:"),dns);
@@ -2018,16 +2021,17 @@ void WiFiManager::handleWifiSave(AsyncWebServerRequest *request) {
page += FPSTR(HTTP_END); page += FPSTR(HTTP_END);
//server->sendHeader(FPSTR(HTTP_HEAD_CORS), FPSTR(HTTP_HEAD_CORS_ALLOW_ALL)); // @HTTPHEAD send cors //server->sendHeader(FPSTR(HTTP_HEAD_CORS), FPSTR(HTTP_HEAD_CORS_ALLOW_ALL)); // @HTTPHEAD send cors
HTTPSend(request,page);
#ifdef WM_DEBUG_LEVEL #ifdef WM_DEBUG_LEVEL
DEBUG_WM(WM_DEBUG_DEV,F("Sent wifi save page")); DEBUG_WM(WM_DEBUG_DEV,F("Sent wifi save page"));
#endif #endif
connect = true; //signal ready to connect/reset process in processConfigPortal connect = true; //signal ready to connect/reset process in processConfigPortal
return HTTPSend(request,page);
} }
void WiFiManager::handleParamSave(AsyncWebServerRequest *request) { esp_err_t WiFiManager::handleParamSave(PsychicRequest *request) {
#ifdef WM_DEBUG_LEVEL #ifdef WM_DEBUG_LEVEL
DEBUG_WM(WM_DEBUG_VERBOSE,F("<- HTTP Param save ")); DEBUG_WM(WM_DEBUG_VERBOSE,F("<- HTTP Param save "));
@@ -2035,7 +2039,7 @@ void WiFiManager::handleParamSave(AsyncWebServerRequest *request) {
#ifdef WM_DEBUG_LEVEL #ifdef WM_DEBUG_LEVEL
DEBUG_WM(WM_DEBUG_DEV,F("Method:"),request->method() == HTTP_GET ? (String)FPSTR(S_GET) : (String)FPSTR(S_POST)); DEBUG_WM(WM_DEBUG_DEV,F("Method:"),request->method() == HTTP_GET ? (String)FPSTR(S_GET) : (String)FPSTR(S_POST));
#endif #endif
if(strlen(_credUser) > 0 && strlen(_credPassword) > 0) if(!request->authenticate(_credUser, _credPassword)) return request->requestAuthentication(); if(strlen(_credUser) > 0 && strlen(_credPassword) > 0) if(!request->authenticate(_credUser, _credPassword)) return request->requestAuthentication(BASIC_AUTH, "Nuki Hub", "You must log in.");
handleRequest(); handleRequest();
doParamSave(request); doParamSave(request);
@@ -2045,14 +2049,14 @@ void WiFiManager::handleParamSave(AsyncWebServerRequest *request) {
if(_showBack) page += FPSTR(HTTP_BACKBTN); if(_showBack) page += FPSTR(HTTP_BACKBTN);
page += FPSTR(HTTP_END); page += FPSTR(HTTP_END);
HTTPSend(request,page);
#ifdef WM_DEBUG_LEVEL #ifdef WM_DEBUG_LEVEL
DEBUG_WM(WM_DEBUG_DEV,F("Sent param save page")); DEBUG_WM(WM_DEBUG_DEV,F("Sent param save page"));
#endif #endif
return HTTPSend(request,page);
} }
void WiFiManager::doParamSave(AsyncWebServerRequest *request){ void WiFiManager::doParamSave(PsychicRequest *request){
// @todo use new callback for before paramsaves, is this really needed? // @todo use new callback for before paramsaves, is this really needed?
if ( _presaveparamscallback != NULL) { if ( _presaveparamscallback != NULL) {
_presaveparamscallback(); // @CALLBACK _presaveparamscallback(); // @CALLBACK
@@ -2075,10 +2079,10 @@ void WiFiManager::doParamSave(AsyncWebServerRequest *request){
//read parameter from server //read parameter from server
String name = (String)FPSTR(S_parampre)+(String)i; String name = (String)FPSTR(S_parampre)+(String)i;
String value; String value;
if(request->hasArg(name.c_str())) { if(request->hasParam(name.c_str())) {
value = request->arg(name); value = request->getParam(name.c_str())->value();
} else { } else {
value = request->arg(_params[i]->getID()); value = request->getParam(_params[i]->getID())->value();
} }
//store it in params array //store it in params array
@@ -2101,11 +2105,11 @@ void WiFiManager::doParamSave(AsyncWebServerRequest *request){
/** /**
* HTTPD CALLBACK info page * HTTPD CALLBACK info page
*/ */
void WiFiManager::handleInfo(AsyncWebServerRequest *request) { esp_err_t WiFiManager::handleInfo(PsychicRequest *request) {
#ifdef WM_DEBUG_LEVEL #ifdef WM_DEBUG_LEVEL
DEBUG_WM(WM_DEBUG_VERBOSE,F("<- HTTP Info")); DEBUG_WM(WM_DEBUG_VERBOSE,F("<- HTTP Info"));
#endif #endif
if(strlen(_credUser) > 0 && strlen(_credPassword) > 0) if(!request->authenticate(_credUser, _credPassword)) return request->requestAuthentication(); if(strlen(_credUser) > 0 && strlen(_credPassword) > 0) if(!request->authenticate(_credUser, _credPassword)) return request->requestAuthentication(BASIC_AUTH, "Nuki Hub", "You must log in.");
//handleRequest(); //handleRequest();
String page = getHTTPHead(FPSTR(S_titleinfo)); // @token titleinfo String page = getHTTPHead(FPSTR(S_titleinfo)); // @token titleinfo
reportStatus(page); reportStatus(page);
@@ -2203,11 +2207,11 @@ void WiFiManager::handleInfo(AsyncWebServerRequest *request) {
page += FPSTR(HTTP_HELP); page += FPSTR(HTTP_HELP);
page += FPSTR(HTTP_END); page += FPSTR(HTTP_END);
HTTPSend(request,page);
#ifdef WM_DEBUG_LEVEL #ifdef WM_DEBUG_LEVEL
DEBUG_WM(WM_DEBUG_DEV,F("Sent info page")); DEBUG_WM(WM_DEBUG_DEV,F("Sent info page"));
#endif #endif
return HTTPSend(request,page);
} }
String WiFiManager::getInfoData(String id){ String WiFiManager::getInfoData(String id){
@@ -2447,41 +2451,45 @@ String WiFiManager::getInfoData(String id){
/** /**
* HTTPD CALLBACK exit, closes configportal if blocking, if non blocking undefined * HTTPD CALLBACK exit, closes configportal if blocking, if non blocking undefined
*/ */
void WiFiManager::handleExit(AsyncWebServerRequest *request) { esp_err_t WiFiManager::handleExit(PsychicRequest *request) {
#ifdef WM_DEBUG_LEVEL #ifdef WM_DEBUG_LEVEL
DEBUG_WM(WM_DEBUG_VERBOSE,F("<- HTTP Exit")); DEBUG_WM(WM_DEBUG_VERBOSE,F("<- HTTP Exit"));
#endif #endif
if(strlen(_credUser) > 0 && strlen(_credPassword) > 0) if(!request->authenticate(_credUser, _credPassword)) return request->requestAuthentication(); if(strlen(_credUser) > 0 && strlen(_credPassword) > 0) if(!request->authenticate(_credUser, _credPassword)) return request->requestAuthentication(BASIC_AUTH, "Nuki Hub", "You must log in.");
handleRequest(); handleRequest();
String page = getHTTPHead(FPSTR(S_titleexit)); // @token titleexit String page = getHTTPHead(FPSTR(S_titleexit)); // @token titleexit
page += FPSTR(S_exiting); // @token exiting page += FPSTR(S_exiting); // @token exiting
// ('Logout', 401, {'WWW-Authenticate': 'Basic realm="Login required"'}) // ('Logout', 401, {'WWW-Authenticate': 'Basic realm="Login required"'})
AsyncWebServerResponse *response = request->beginResponse(200,FPSTR(HTTP_HEAD_CT), page);
response->addHeader(F("Cache-Control"), F("no-cache, no-store, must-revalidate"));
request->send(response);
delay(2000); delay(2000);
abort = true; abort = true;
PsychicResponse response(request);
response.addHeader("Cache-Control", "no-cache, no-store, must-revalidate");
response.setCode(200);
response.setContentType(HTTP_HEAD_CT);
response.setContent(page.c_str());
return response.send();
} }
/** /**
* HTTPD CALLBACK reset page * HTTPD CALLBACK reset page
*/ */
void WiFiManager::handleReset(AsyncWebServerRequest *request) { esp_err_t WiFiManager::handleReset(PsychicRequest *request) {
#ifdef WM_DEBUG_LEVEL #ifdef WM_DEBUG_LEVEL
DEBUG_WM(WM_DEBUG_VERBOSE,F("<- HTTP Reset")); DEBUG_WM(WM_DEBUG_VERBOSE,F("<- HTTP Reset"));
#endif #endif
if(strlen(_credUser) > 0 && strlen(_credPassword) > 0) if(!request->authenticate(_credUser, _credPassword)) return request->requestAuthentication(); if(strlen(_credUser) > 0 && strlen(_credPassword) > 0) if(!request->authenticate(_credUser, _credPassword)) return request->requestAuthentication(BASIC_AUTH, "Nuki Hub", "You must log in.");
handleRequest(); handleRequest();
String page = getHTTPHead(FPSTR(S_titlereset)); //@token titlereset String page = getHTTPHead(FPSTR(S_titlereset)); //@token titlereset
page += FPSTR(S_resetting); //@token resetting page += FPSTR(S_resetting); //@token resetting
page += FPSTR(HTTP_END); page += FPSTR(HTTP_END);
HTTPSend(request,page);
#ifdef WM_DEBUG_LEVEL #ifdef WM_DEBUG_LEVEL
DEBUG_WM(F("RESETTING ESP")); DEBUG_WM(F("RESETTING ESP"));
#endif #endif
_rebootNeeded = true; _rebootNeeded = true;
return HTTPSend(request,page);
} }
/** /**
@@ -2491,11 +2499,11 @@ void WiFiManager::handleReset(AsyncWebServerRequest *request) {
// void WiFiManager::handleErase() { // void WiFiManager::handleErase() {
// handleErase(false); // handleErase(false);
// } // }
void WiFiManager::handleErase(AsyncWebServerRequest *request,bool opt = false) { esp_err_t WiFiManager::handleErase(PsychicRequest *request,bool opt = false) {
#ifdef WM_DEBUG_LEVEL #ifdef WM_DEBUG_LEVEL
DEBUG_WM(WM_DEBUG_NOTIFY,F("<- HTTP Erase")); DEBUG_WM(WM_DEBUG_NOTIFY,F("<- HTTP Erase"));
#endif #endif
if(strlen(_credUser) > 0 && strlen(_credPassword) > 0) if(!request->authenticate(_credUser, _credPassword)) return request->requestAuthentication(); if(strlen(_credUser) > 0 && strlen(_credPassword) > 0) if(!request->authenticate(_credUser, _credPassword)) return request->requestAuthentication(BASIC_AUTH, "Nuki Hub", "You must log in.");
handleRequest(); handleRequest();
String page = getHTTPHead(FPSTR(S_titleerase)); // @token titleerase String page = getHTTPHead(FPSTR(S_titleerase)); // @token titleerase
@@ -2510,18 +2518,19 @@ void WiFiManager::handleErase(AsyncWebServerRequest *request,bool opt = false) {
} }
page += FPSTR(HTTP_END); page += FPSTR(HTTP_END);
HTTPSend(request,page);
if(ret){ if(ret){
_rebootNeeded = true; _rebootNeeded = true;
} }
return HTTPSend(request,page);
} }
/** /**
* HTTPD CALLBACK 404 * HTTPD CALLBACK 404
*/ */
void WiFiManager::handleNotFound(AsyncWebServerRequest *request) { esp_err_t WiFiManager::handleNotFound(PsychicRequest *request) {
if (captivePortal(request)) return; // If captive portal redirect instead of displaying the page if (captivePortal(request)) return 0; // If captive portal redirect instead of displaying the page
handleRequest(); handleRequest();
String message = FPSTR(S_notfound); // @token notfound String message = FPSTR(S_notfound); // @token notfound
@@ -2532,18 +2541,22 @@ void WiFiManager::handleNotFound(AsyncWebServerRequest *request) {
message += FPSTR(S_method); // @token method message += FPSTR(S_method); // @token method
message += ( request->method() == HTTP_GET ) ? FPSTR(S_GET) : FPSTR(S_POST); message += ( request->method() == HTTP_GET ) ? FPSTR(S_GET) : FPSTR(S_POST);
message += FPSTR(S_args); // @token args message += FPSTR(S_args); // @token args
message += request->args(); message += request->params();
message += F("\n"); message += F("\n");
for ( uint8_t i = 0; i < request->args(); i++ ) { for ( uint8_t i = 0; i < request->params(); i++ ) {
message += " " + request->argName ( i ) + ": " + request->arg ( i ) + "\n"; message += " " + request->getParam(i)->name() + ": " + request->getParam(i)->value() + "\n";
} }
} }
AsyncWebServerResponse *response = request->beginResponse(404,FPSTR(HTTP_HEAD_CT2), message);
response->addHeader(F("Cache-Control"), F("no-cache, no-store, must-revalidate")); PsychicResponse response(request);
response->addHeader(F("Pragma"), F("no-cache")); response.addHeader("Cache-Control", "no-cache, no-store, must-revalidate");
response->addHeader(F("Expires"), F("-1")); response.addHeader("Pragma", "no-cache");
request->send(response); response.addHeader("Expires", "-1");
response.setCode(404);
response.setContentType(HTTP_HEAD_CT2);
response.setContent(message.c_str());
return response.send();
} }
/** /**
@@ -2551,7 +2564,7 @@ void WiFiManager::handleNotFound(AsyncWebServerRequest *request) {
* Redirect to captive portal if we got a request for another domain. * Redirect to captive portal if we got a request for another domain.
* Return true in that case so the page handler do not try to handle the request again. * Return true in that case so the page handler do not try to handle the request again.
*/ */
boolean WiFiManager::captivePortal(AsyncWebServerRequest *request) { boolean WiFiManager::captivePortal(PsychicRequest *request) {
if(!_enableCaptivePortal || !configPortalActive) return false; // skip redirections if cp not enabled or not in ap mode if(!_enableCaptivePortal || !configPortalActive) return false; // skip redirections if cp not enabled or not in ap mode
@@ -2578,9 +2591,11 @@ boolean WiFiManager::captivePortal(AsyncWebServerRequest *request) {
DEBUG_WM(WM_DEBUG_VERBOSE,F("<- Request redirected to captive portal")); DEBUG_WM(WM_DEBUG_VERBOSE,F("<- Request redirected to captive portal"));
DEBUG_WM(WM_DEBUG_DEV,"serverLoc " + serverLoc); DEBUG_WM(WM_DEBUG_DEV,"serverLoc " + serverLoc);
#endif #endif
AsyncWebServerResponse *response = request->beginResponse(302,FPSTR(HTTP_HEAD_CT2), ""); PsychicResponse response(request);
response->addHeader(F("Location"), (String)F("http://") + serverLoc); response.addHeader("Location", ((String)("http://") + serverLoc).c_str());
request->send(response); response.setCode(302);
response.setContentType(HTTP_HEAD_CT2);
response.send();
return true; return true;
} }
return false; return false;
@@ -2592,9 +2607,9 @@ void WiFiManager::stopCaptivePortal(){
} }
// HTTPD CALLBACK, handle close, stop captive portal, if not enabled undefined // HTTPD CALLBACK, handle close, stop captive portal, if not enabled undefined
void WiFiManager::handleClose(AsyncWebServerRequest *request){ esp_err_t WiFiManager::handleClose(PsychicRequest *request){
DEBUG_WM(WM_DEBUG_VERBOSE,F("Disabling Captive Portal")); DEBUG_WM(WM_DEBUG_VERBOSE,F("Disabling Captive Portal"));
if(strlen(_credUser) > 0 && strlen(_credPassword) > 0) if(!request->authenticate(_credUser, _credPassword)) return request->requestAuthentication(); if(strlen(_credUser) > 0 && strlen(_credPassword) > 0) if(!request->authenticate(_credUser, _credPassword)) return request->requestAuthentication(BASIC_AUTH, "Nuki Hub", "You must log in.");
stopCaptivePortal(); stopCaptivePortal();
#ifdef WM_DEBUG_LEVEL #ifdef WM_DEBUG_LEVEL
DEBUG_WM(WM_DEBUG_VERBOSE,F("<- HTTP close")); DEBUG_WM(WM_DEBUG_VERBOSE,F("<- HTTP close"));
@@ -2602,7 +2617,7 @@ void WiFiManager::handleClose(AsyncWebServerRequest *request){
handleRequest(); handleRequest();
String page = getHTTPHead(FPSTR(S_titleclose)); // @token titleclose String page = getHTTPHead(FPSTR(S_titleclose)); // @token titleclose
page += FPSTR(S_closing); // @token closing page += FPSTR(S_closing); // @token closing
HTTPSend(request,page); return HTTPSend(request,page);
} }
void WiFiManager::reportStatus(String &page){ void WiFiManager::reportStatus(String &page){
@@ -4011,12 +4026,12 @@ void WiFiManager::WiFi_autoReconnect(){
} }
// Called when /update is requested // Called when /update is requested
void WiFiManager::handleUpdate(AsyncWebServerRequest *request) { esp_err_t WiFiManager::handleUpdate(PsychicRequest *request) {
#ifdef WM_DEBUG_LEVEL #ifdef WM_DEBUG_LEVEL
DEBUG_WM(WM_DEBUG_VERBOSE,F("<- Handle update")); DEBUG_WM(WM_DEBUG_VERBOSE,F("<- Handle update"));
#endif #endif
if(strlen(_credUser) > 0 && strlen(_credPassword) > 0) if(!request->authenticate(_credUser, _credPassword)) return request->requestAuthentication(); if(strlen(_credUser) > 0 && strlen(_credPassword) > 0) if(!request->authenticate(_credUser, _credPassword)) return request->requestAuthentication(BASIC_AUTH, "Nuki Hub", "You must log in.");
if (captivePortal(request)) return; // If captive portal redirect instead of displaying the page if (captivePortal(request)) return 0; // If captive portal redirect instead of displaying the page
String page = getHTTPHead(_title); // @token options String page = getHTTPHead(_title); // @token options
String str = FPSTR(HTTP_ROOT_MAIN); String str = FPSTR(HTTP_ROOT_MAIN);
str.replace(FPSTR(T_t), _title); str.replace(FPSTR(T_t), _title);
@@ -4026,12 +4041,12 @@ void WiFiManager::handleUpdate(AsyncWebServerRequest *request) {
page += FPSTR(HTTP_UPDATE); page += FPSTR(HTTP_UPDATE);
page += FPSTR(HTTP_END); page += FPSTR(HTTP_END);
HTTPSend(request,page); return HTTPSend(request,page);
} }
// upload via /u POST // upload via /u POST
void WiFiManager::handleUpdating(AsyncWebServerRequest *request,String filename, size_t index, uint8_t *data, size_t len, bool final){ void WiFiManager::handleUpdating(String filename, size_t index, uint8_t *data, size_t len, bool final){
// @todo // @todo
// cannot upload files in captive portal, file select is not allowed, show message with link or hide // cannot upload files in captive portal, file select is not allowed, show message with link or hide
// cannot upload if softreset after upload, maybe check for hard reset at least for dev, ERROR[11]: Invalid bootstrapping state, reset ESP8266 before updating // cannot upload if softreset after upload, maybe check for hard reset at least for dev, ERROR[11]: Invalid bootstrapping state, reset ESP8266 before updating
@@ -4123,10 +4138,10 @@ void WiFiManager::handleUpdating(AsyncWebServerRequest *request,String filename,
} }
// upload and ota done, show status // upload and ota done, show status
void WiFiManager::handleUpdateDone(AsyncWebServerRequest *request) { esp_err_t WiFiManager::handleUpdateDone(PsychicRequest *request) {
DEBUG_WM(WM_DEBUG_VERBOSE, F("<- Handle update done")); DEBUG_WM(WM_DEBUG_VERBOSE, F("<- Handle update done"));
// if (captivePortal(request)) return; // If captive portal redirect instead of displaying the page // if (captivePortal(request)) return; // If captive portal redirect instead of displaying the page
if(strlen(_credUser) > 0 && strlen(_credPassword) > 0) if(!request->authenticate(_credUser, _credPassword)) return request->requestAuthentication(); if(strlen(_credUser) > 0 && strlen(_credPassword) > 0) if(!request->authenticate(_credUser, _credPassword)) return request->requestAuthentication(BASIC_AUTH, "Nuki Hub", "You must log in.");
String page = getHTTPHead(FPSTR(S_options)); // @token options String page = getHTTPHead(FPSTR(S_options)); // @token options
String str = FPSTR(HTTP_ROOT_MAIN); String str = FPSTR(HTTP_ROOT_MAIN);
str.replace(FPSTR(T_t),_title); str.replace(FPSTR(T_t),_title);
@@ -4148,13 +4163,13 @@ void WiFiManager::handleUpdateDone(AsyncWebServerRequest *request) {
} }
page += FPSTR(HTTP_END); page += FPSTR(HTTP_END);
HTTPSend(request,page);
// delay(1000); // send page // delay(1000); // send page
if (!Update.hasError()) { if (!Update.hasError()) {
//ESP.restart(); //ESP.restart();
_rebootNeeded = true; _rebootNeeded = true;
} }
return HTTPSend(request,page);
} }
#endif #endif

View File

@@ -97,8 +97,8 @@
#define WM_WIFIOPEN WIFI_AUTH_OPEN #define WM_WIFIOPEN WIFI_AUTH_OPEN
#ifdef WM_ASYNCWEBSERVER #ifdef WM_ASYNCWEBSERVER
#include <AsyncTCP.h> #include <PsychicHttp.h>
#include <ESPAsyncWebServer.h> #include <PsychicHttpsServer.h>
#else #else
#ifndef WEBSERVER_H #ifndef WEBSERVER_H
#ifdef WM_WEBSERVERSHIM #ifdef WM_WEBSERVERSHIM
@@ -521,7 +521,7 @@ class WiFiManager
#if defined(ESP32) && defined(WM_WEBSERVERSHIM) #if defined(ESP32) && defined(WM_WEBSERVERSHIM)
#ifdef WM_ASYNCWEBSERVER #ifdef WM_ASYNCWEBSERVER
using WM_WebServer = AsyncWebServer; using WM_WebServer = PsychicHttpServer;
#else #else
using WM_WebServer = WebServer; using WM_WebServer = WebServer;
#endif #endif
@@ -694,32 +694,32 @@ public:
bool WiFi_scanNetworks(bool force,bool async); bool WiFi_scanNetworks(bool force,bool async);
protected: protected:
// webserver handlers // webserver handlers
void handleRoot(AsyncWebServerRequest *request); esp_err_t handleRoot(PsychicRequest *request);
void handleWifi(AsyncWebServerRequest *request,bool scan); esp_err_t handleWifi(PsychicRequest *request,bool scan);
void handleWifiSave(AsyncWebServerRequest *request); esp_err_t handleWifiSave(PsychicRequest *request);
void handleInfo(AsyncWebServerRequest *request); esp_err_t handleInfo(PsychicRequest *request);
void handleReset(AsyncWebServerRequest *request); esp_err_t handleReset(PsychicRequest *request);
void handleNotFound(AsyncWebServerRequest *request); esp_err_t handleNotFound(PsychicRequest *request);
void handleExit(AsyncWebServerRequest *request); esp_err_t handleExit(PsychicRequest *request);
void handleClose(AsyncWebServerRequest *request); esp_err_t handleClose(PsychicRequest *request);
// void handleErase(AsyncWebServerRequest *request); // esp_err_t handleErase(PsychicRequest *request);
void handleErase(AsyncWebServerRequest *request, bool opt); esp_err_t handleErase(PsychicRequest *request, bool opt);
void handleParam(AsyncWebServerRequest *request); esp_err_t handleParam(PsychicRequest *request);
void handleWiFiStatus(AsyncWebServerRequest *request); esp_err_t handleWiFiStatus(PsychicRequest *request);
void handleParamSave(AsyncWebServerRequest *request); esp_err_t handleParamSave(PsychicRequest *request);
void doParamSave(AsyncWebServerRequest *request); void doParamSave(PsychicRequest *request);
void handleRequest(); void handleRequest();
void HTTPSend(AsyncWebServerRequest *request, String page); esp_err_t HTTPSend(PsychicRequest *request, String page);
boolean captivePortal(AsyncWebServerRequest *request); boolean captivePortal(PsychicRequest *request);
boolean configPortalHasTimeout(); boolean configPortalHasTimeout();
uint8_t processConfigPortal(); uint8_t processConfigPortal();
void stopCaptivePortal(); void stopCaptivePortal();
// OTA Update handler // OTA Update handler
void handleUpdate(AsyncWebServerRequest *request); esp_err_t handleUpdate(PsychicRequest *request);
void handleUpdating(AsyncWebServerRequest *request,String filename, size_t index, uint8_t *data, size_t len, bool final); void handleUpdating(String filename, size_t index, uint8_t *data, size_t len, bool final);
void handleUpdateDone(AsyncWebServerRequest *request); esp_err_t handleUpdateDone(PsychicRequest *request);
// wifi platform abstractions // wifi platform abstractions
bool WiFi_Mode(WiFiMode_t m); bool WiFi_Mode(WiFiMode_t m);

View File

@@ -82,4 +82,13 @@ CONFIG_ETH_SPI_ETHERNET_KSZ8851SNL=y
CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=y CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=y
CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_NONE=y CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_NONE=y
CONFIG_MBEDTLS_CUSTOM_CERTIFICATE_BUNDLE=y CONFIG_MBEDTLS_CUSTOM_CERTIFICATE_BUNDLE=y
CONFIG_MBEDTLS_CUSTOM_CERTIFICATE_BUNDLE_PATH="resources/github_root_ca.pem" CONFIG_MBEDTLS_CUSTOM_CERTIFICATE_BUNDLE_PATH="resources/github_root_ca.pem"
CONFIG_ESP_HTTP_CLIENT_ENABLE_HTTPS=y
CONFIG_ESP_HTTP_CLIENT_ENABLE_BASIC_AUTH=y
CONFIG_ESP_HTTP_CLIENT_ENABLE_DIGEST_AUTH=y
CONFIG_HTTPD_MAX_REQ_HDR_LEN=1024
CONFIG_HTTPD_MAX_URI_LEN=512
CONFIG_HTTPD_ERR_RESP_NO_DELAY=y
CONFIG_HTTPD_PURGE_BUF_LEN=32
CONFIG_HTTPD_WS_SUPPORT=y
CONFIG_ESP_HTTPS_SERVER_ENABLE=y

View File

@@ -4,7 +4,7 @@
#define NUKI_HUB_VERSION "9.01" #define NUKI_HUB_VERSION "9.01"
#define NUKI_HUB_BUILD "unknownbuildnr" #define NUKI_HUB_BUILD "unknownbuildnr"
#define NUKI_HUB_DATE "2024-08-18" #define NUKI_HUB_DATE "2024-08-26"
#define GITHUB_LATEST_RELEASE_URL (char*)"https://github.com/technyon/nuki_hub/releases/latest" #define GITHUB_LATEST_RELEASE_URL (char*)"https://github.com/technyon/nuki_hub/releases/latest"
#define GITHUB_OTA_MANIFEST_URL (char*)"https://raw.githubusercontent.com/technyon/nuki_hub/binary/ota/manifest.json" #define GITHUB_OTA_MANIFEST_URL (char*)"https://raw.githubusercontent.com/technyon/nuki_hub/binary/ota/manifest.json"

View File

@@ -16,8 +16,6 @@
#include "NukiConstants.h" #include "NukiConstants.h"
#endif #endif
#define JSON_BUFFER_SIZE 1024
class NukiNetwork class NukiNetwork
{ {
public: public:

View File

@@ -13,8 +13,6 @@
#include "QueryCommand.h" #include "QueryCommand.h"
#include "LockActionResult.h" #include "LockActionResult.h"
#define LOCK_LOG_JSON_BUFFER_SIZE 2048
class NukiNetworkLock : public MqttReceiver class NukiNetworkLock : public MqttReceiver
{ {
public: public:

File diff suppressed because it is too large Load Diff

View File

@@ -1,9 +1,8 @@
#pragma once #pragma once
#include <Preferences.h> #include <Preferences.h>
#include <AsyncTCP.h> #include <PsychicHttp.h>
#include <DNSServer.h> #include <PsychicHttpsServer.h>
#include <ESPAsyncWebServer.h>
#include "esp_ota_ops.h" #include "esp_ota_ops.h"
#include "Config.h" #include "Config.h"
@@ -37,9 +36,9 @@ class WebCfgServer
{ {
public: public:
#ifndef NUKI_HUB_UPDATER #ifndef NUKI_HUB_UPDATER
WebCfgServer(NukiWrapper* nuki, NukiOpenerWrapper* nukiOpener, NukiNetwork* network, Gpio* gpio, Preferences* preferences, bool allowRestartToPortal, uint8_t partitionType, AsyncWebServer* asyncServer); WebCfgServer(NukiWrapper* nuki, NukiOpenerWrapper* nukiOpener, NukiNetwork* network, Gpio* gpio, Preferences* preferences, bool allowRestartToPortal, uint8_t partitionType, PsychicHttpServer* psychicServer);
#else #else
WebCfgServer(NukiNetwork* network, Preferences* preferences, bool allowRestartToPortal, uint8_t partitionType, AsyncWebServer* asyncServer); WebCfgServer(NukiNetwork* network, Preferences* preferences, bool allowRestartToPortal, uint8_t partitionType, PsychicHttpServer* psychicServer);
#endif #endif
~WebCfgServer() = default; ~WebCfgServer() = default;
@@ -47,34 +46,34 @@ public:
private: private:
#ifndef NUKI_HUB_UPDATER #ifndef NUKI_HUB_UPDATER
void sendSettings(AsyncWebServerRequest *request); esp_err_t sendSettings(PsychicRequest *request);
bool processArgs(AsyncWebServerRequest *request, String& message); bool processArgs(PsychicRequest *request, String& message);
bool processImport(AsyncWebServerRequest *request, String& message); bool processImport(PsychicRequest *request, String& message);
void processGpioArgs(AsyncWebServerRequest *request); void processGpioArgs(PsychicRequest *request);
void buildHtml(AsyncWebServerRequest *request); esp_err_t buildHtml(PsychicRequest *request);
void buildAccLvlHtml(AsyncWebServerRequest *request); esp_err_t buildAccLvlHtml(PsychicRequest *request);
void buildCredHtml(AsyncWebServerRequest *request); esp_err_t buildCredHtml(PsychicRequest *request);
void buildImportExportHtml(AsyncWebServerRequest *request); esp_err_t buildImportExportHtml(PsychicRequest *request);
void buildMqttConfigHtml(AsyncWebServerRequest *request); esp_err_t buildMqttConfigHtml(PsychicRequest *request);
void buildStatusHtml(AsyncWebServerRequest *request); esp_err_t buildStatusHtml(PsychicRequest *request);
void buildAdvancedConfigHtml(AsyncWebServerRequest *request); esp_err_t buildAdvancedConfigHtml(PsychicRequest *request);
void buildNukiConfigHtml(AsyncWebServerRequest *request); esp_err_t buildNukiConfigHtml(PsychicRequest *request);
void buildGpioConfigHtml(AsyncWebServerRequest *request); esp_err_t buildGpioConfigHtml(PsychicRequest *request);
#ifndef CONFIG_IDF_TARGET_ESP32H2 #ifndef CONFIG_IDF_TARGET_ESP32H2
void buildConfigureWifiHtml(AsyncWebServerRequest *request); esp_err_t buildConfigureWifiHtml(PsychicRequest *request);
#endif #endif
void buildInfoHtml(AsyncWebServerRequest *request); esp_err_t buildInfoHtml(PsychicRequest *request);
void buildCustomNetworkConfigHtml(AsyncWebServerRequest *request); esp_err_t buildCustomNetworkConfigHtml(PsychicRequest *request);
void processUnpair(AsyncWebServerRequest *request, bool opener); esp_err_t processUnpair(PsychicRequest *request, bool opener);
void processUpdate(AsyncWebServerRequest *request); esp_err_t processUpdate(PsychicRequest *request);
void processFactoryReset(AsyncWebServerRequest *request); esp_err_t processFactoryReset(PsychicRequest *request);
void printInputField(const char* token, const char* description, const char* value, const size_t& maxLength, const char* args, const bool& isPassword = false, const bool& showLengthRestriction = false); void printInputField(PsychicStreamResponse *response, const char* token, const char* description, const char* value, const size_t& maxLength, const char* args, const bool& isPassword = false, const bool& showLengthRestriction = false);
void printInputField(const char* token, const char* description, const int value, size_t maxLength, const char* args); void printInputField(PsychicStreamResponse *response, const char* token, const char* description, const int value, size_t maxLength, const char* args);
void printCheckBox(const char* token, const char* description, const bool value, const char* htmlClass); void printCheckBox(PsychicStreamResponse *response, const char* token, const char* description, const bool value, const char* htmlClass);
void printTextarea(const char *token, const char *description, const char *value, const size_t& maxLength, const bool& enabled = true, const bool& showLengthRestriction = false); void printTextarea(PsychicStreamResponse *response, const char *token, const char *description, const char *value, const size_t& maxLength, const bool& enabled = true, const bool& showLengthRestriction = false);
void printDropDown(const char *token, const char *description, const String preselectedValue, std::vector<std::pair<String, String>> options, const String className); void printDropDown(PsychicStreamResponse *response, const char *token, const char *description, const String preselectedValue, std::vector<std::pair<String, String>> options, const String className);
void buildNavigationButton(const char* caption, const char* targetPath, const char* labelText = ""); void buildNavigationButton(PsychicStreamResponse *response, const char* caption, const char* targetPath, const char* labelText = "");
void buildNavigationMenuEntry(const char *title, const char *targetPath, const char* warningMessage = ""); void buildNavigationMenuEntry(PsychicStreamResponse *response, const char *title, const char *targetPath, const char* warningMessage = "");
const std::vector<std::pair<String, String>> getNetworkDetectionOptions() const; const std::vector<std::pair<String, String>> getNetworkDetectionOptions() const;
const std::vector<std::pair<String, String>> getGpioOptions() const; const std::vector<std::pair<String, String>> getGpioOptions() const;
@@ -86,8 +85,8 @@ private:
String getPreselectionForGpio(const uint8_t& pin); String getPreselectionForGpio(const uint8_t& pin);
String pinStateToString(uint8_t value); String pinStateToString(uint8_t value);
void printParameter(const char* description, const char* value, const char *link = "", const char *id = ""); void printParameter(PsychicStreamResponse *response, const char* description, const char* value, const char *link = "", const char *id = "");
NukiWrapper* _nuki = nullptr; NukiWrapper* _nuki = nullptr;
NukiOpenerWrapper* _nukiOpener = nullptr; NukiOpenerWrapper* _nukiOpener = nullptr;
Gpio* _gpio = nullptr; Gpio* _gpio = nullptr;
@@ -96,21 +95,19 @@ private:
bool _rebootRequired = false; bool _rebootRequired = false;
#endif #endif
String _response;
String generateConfirmCode(); String generateConfirmCode();
String _confirmCode = "----"; String _confirmCode = "----";
void buildConfirmHtml(AsyncWebServerRequest *request, const String &message, uint32_t redirectDelay = 5, bool redirect = false); esp_err_t buildConfirmHtml(PsychicRequest *request, const String &message, uint32_t redirectDelay = 5, bool redirect = false);
void buildOtaHtml(AsyncWebServerRequest *request, bool debug = false); esp_err_t buildOtaHtml(PsychicRequest *request, bool debug = false);
void buildOtaCompletedHtml(AsyncWebServerRequest *request); esp_err_t buildOtaCompletedHtml(PsychicRequest *request);
void sendCss(AsyncWebServerRequest *request); esp_err_t sendCss(PsychicRequest *request);
void sendFavicon(AsyncWebServerRequest *request); esp_err_t sendFavicon(PsychicRequest *request);
void buildHtmlHeader(String additionalHeader = ""); void buildHtmlHeader(PsychicStreamResponse *response, String additionalHeader = "");
void waitAndProcess(const bool blocking, const uint32_t duration); void waitAndProcess(const bool blocking, const uint32_t duration);
void handleOtaUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final); void handleOtaUpload(PsychicRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final);
void printProgress(size_t prg, size_t sz); void printProgress(size_t prg, size_t sz);
void sendResponse(AsyncWebServerRequest *request);
PsychicHttpServer* _psychicServer = nullptr;
AsyncWebServer* _asyncServer = nullptr;
NukiNetwork* _network = nullptr; NukiNetwork* _network = nullptr;
Preferences* _preferences = nullptr; Preferences* _preferences = nullptr;

View File

@@ -19,11 +19,10 @@
#include "Logger.h" #include "Logger.h"
#include "PreferencesKeys.h" #include "PreferencesKeys.h"
#include "RestartReason.h" #include "RestartReason.h"
#include <AsyncTCP.h> #ifdef DEBUG_NUKIHUB
#include <DNSServer.h>
#include <ESPAsyncWebServer.h>
#include <WString.h> #include <WString.h>
#include <MycilaWebSerial.h> #include <MycilaWebSerial.h>
#endif
char log_print_buffer[1024]; char log_print_buffer[1024];
@@ -54,7 +53,7 @@ int64_t restartTs = 10 * 1000 * 60000;
#endif #endif
AsyncWebServer* asyncServer = nullptr; PsychicHttpServer* psychicServer = nullptr;
NukiNetwork* network = nullptr; NukiNetwork* network = nullptr;
WebCfgServer* webCfgServer = nullptr; WebCfgServer* webCfgServer = nullptr;
Preferences* preferences = nullptr; Preferences* preferences = nullptr;
@@ -418,11 +417,11 @@ void setup()
if(!doOta) if(!doOta)
{ {
asyncServer = new AsyncWebServer(80); psychicServer = new PsychicHttpServer;
webCfgServer = new WebCfgServer(network, preferences, network->networkDeviceType() == NetworkDeviceType::WiFi, partitionType, asyncServer); webCfgServer = new WebCfgServer(network, preferences, network->networkDeviceType() == NetworkDeviceType::WiFi, partitionType, psychicServer);
webCfgServer->initialize(); webCfgServer->initialize();
asyncServer->onNotFound([](AsyncWebServerRequest* request) { request->redirect("/"); }); psychicServer->onNotFound([](PsychicRequest* request) { return request->redirect("/"); });
asyncServer->begin(); psychicServer->listen(80);
} }
#else #else
Log->print(F("Nuki Hub version ")); Log->print(F("Nuki Hub version "));
@@ -490,24 +489,26 @@ void setup()
{ {
if(!doOta) if(!doOta)
{ {
asyncServer = new AsyncWebServer(80); psychicServer = new PsychicHttpServer;
if(forceEnableWebServer || preferences->getBool(preference_webserver_enabled, true)) if(forceEnableWebServer || preferences->getBool(preference_webserver_enabled, true))
{ {
webCfgServer = new WebCfgServer(nuki, nukiOpener, network, gpio, preferences, network->networkDeviceType() == NetworkDeviceType::WiFi, partitionType, asyncServer); webCfgServer = new WebCfgServer(nuki, nukiOpener, network, gpio, preferences, network->networkDeviceType() == NetworkDeviceType::WiFi, partitionType, psychicServer);
webCfgServer->initialize(); webCfgServer->initialize();
asyncServer->onNotFound([](AsyncWebServerRequest* request) { request->redirect("/"); }); psychicServer->onNotFound([](PsychicRequest* request) { return request->redirect("/"); });
} }
else asyncServer->onNotFound([](AsyncWebServerRequest* request) { request->redirect("/webserial"); }); #ifdef DEBUG_NUKIHUB
else psychicServer->onNotFound([](PsychicRequest* request) { return request->redirect("/webserial"); });
if(preferences->getBool(preference_webserial_enabled, false)) if(preferences->getBool(preference_webserial_enabled, false))
{ {
WebSerial.setAuthentication(preferences->getString(preference_cred_user), preferences->getString(preference_cred_password)); //WebSerial.setAuthentication(preferences->getString(preference_cred_user), preferences->getString(preference_cred_password));
WebSerial.begin(asyncServer); //WebSerial.begin(asyncServer);
WebSerial.setBuffer(1024); //WebSerial.setBuffer(1024);
} }
#endif
asyncServer->begin(); psychicServer->listen(80);
} }
} }
#endif #endif

View File

@@ -53,8 +53,7 @@ lib_ignore =
SimpleBLE SimpleBLE
WiFiProv WiFiProv
lib_deps = lib_deps =
AsyncTCP=symlink://../lib/AsyncTCP PsychicHttp=symlink://../lib/PsychicHttp
ESPAsyncWebServer=symlink://../lib/ESPAsyncWebServer
WiFiManager=symlink://../lib/WiFiManager WiFiManager=symlink://../lib/WiFiManager
monitor_speed = 115200 monitor_speed = 115200
@@ -86,8 +85,7 @@ board = esp32-h2-devkitm-1
board_build.cmake_extra_args = board_build.cmake_extra_args =
-DNUKI_TARGET_H2=y -DNUKI_TARGET_H2=y
lib_deps = lib_deps =
AsyncTCP=symlink://../lib/AsyncTCP PsychicHttp=symlink://../lib/PsychicHttp
ESPAsyncWebServer=symlink://../lib/ESPAsyncWebServer
[env:updater_esp32-solo1] [env:updater_esp32-solo1]
extends = env:updater_esp32 extends = env:updater_esp32

View File

@@ -22,4 +22,13 @@ CONFIG_ETH_ENABLED=y
CONFIG_ETH_USE_SPI_ETHERNET=y CONFIG_ETH_USE_SPI_ETHERNET=y
CONFIG_ETH_SPI_ETHERNET_W5500=y CONFIG_ETH_SPI_ETHERNET_W5500=y
CONFIG_ETH_SPI_ETHERNET_DM9051=y CONFIG_ETH_SPI_ETHERNET_DM9051=y
CONFIG_ETH_SPI_ETHERNET_KSZ8851SNL=y CONFIG_ETH_SPI_ETHERNET_KSZ8851SNL=y
CONFIG_ESP_HTTP_CLIENT_ENABLE_HTTPS=y
CONFIG_ESP_HTTP_CLIENT_ENABLE_BASIC_AUTH=y
CONFIG_ESP_HTTP_CLIENT_ENABLE_DIGEST_AUTH=y
CONFIG_HTTPD_MAX_REQ_HDR_LEN=1024
CONFIG_HTTPD_MAX_URI_LEN=512
CONFIG_HTTPD_ERR_RESP_NO_DELAY=y
CONFIG_HTTPD_PURGE_BUF_LEN=32
CONFIG_HTTPD_WS_SUPPORT=y
CONFIG_ESP_HTTPS_SERVER_ENABLE=y