mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-31 12:29:14 +03:00
34 lines
1.1 KiB
C++
34 lines
1.1 KiB
C++
#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);
|
|
};
|