mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-31 20:39:14 +03:00
Terminal
This commit is contained in:
33
src/CaptiveRequestHandler.cpp
Normal file
33
src/CaptiveRequestHandler.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#include "CaptiveRequestHandler.h"
|
||||
|
||||
CaptiveRequestHandler::CaptiveRequestHandler(const char *host) {
|
||||
strlcpy(_local_name, host, sizeof(_local_name));
|
||||
strcat(_local_name, ".local");
|
||||
}
|
||||
|
||||
CaptiveRequestHandler::~CaptiveRequestHandler() {
|
||||
}
|
||||
|
||||
bool CaptiveRequestHandler::isLocalIp(String address) {
|
||||
IPAddress ip;
|
||||
return !ip.fromString(address) || (ip != _local_ip);
|
||||
}
|
||||
|
||||
bool CaptiveRequestHandler::isLocalName(String host_name) {
|
||||
return host_name.equalsIgnoreCase(_local_name);
|
||||
}
|
||||
|
||||
bool CaptiveRequestHandler::canHandle(AsyncWebServerRequest *request) {
|
||||
_local_ip = request->client()->localIP();
|
||||
|
||||
return !isLocalIp(request->getHeader("HOST")->value()) && !isLocalName(request->getHeader("HOST")->value());
|
||||
}
|
||||
|
||||
void CaptiveRequestHandler::CaptiveRequestHandler::handleRequest(AsyncWebServerRequest *request) {
|
||||
char buf[64];
|
||||
sprintf(buf, "http://%s%s", _local_name, request->url().c_str());
|
||||
auto response = request->beginResponse(302, "text/html");
|
||||
response->addHeader("Location", buf);
|
||||
response->addHeader("Connection", "close");
|
||||
request->send(response);
|
||||
};
|
||||
Reference in New Issue
Block a user