update CSpot

This commit is contained in:
philippe44
2022-03-04 20:06:19 -08:00
parent 7b1d1ad45e
commit 57b77766ff
28 changed files with 226 additions and 110 deletions

View File

@@ -170,16 +170,23 @@ void bell::HTTPServer::readFromClient(int clientFd) {
if (line.find("Content-Length: ") != std::string::npos) {
conn.contentLength =
std::stoi(line.substr(16, line.size() - 1));
//BELL_LOG(info, "http", "Content-Length: %d",
// conn.contentLength);
}
// detect hostname for captive portal
if (line.find("Host: connectivitycheck.gstatic.com") != std::string::npos) {
conn.isCaptivePortal = true;
BELL_LOG(info, "http", "Captive portal request detected");
}
if (line.size() == 0) {
if (conn.contentLength != 0) {
conn.isReadingBody = true;
goto READBODY;
} else {
findAndHandleRoute(conn.httpMethod, conn.currentLine,
clientFd);
if (!conn.isCaptivePortal) {
findAndHandleRoute(conn.httpMethod, conn.currentLine, clientFd);
} else {
this->redirectCaptivePortal(clientFd);
}
}
}
}
@@ -277,6 +284,21 @@ void bell::HTTPServer::respond(const HTTPResponse &response) {
writeResponse(response);
}
void bell::HTTPServer::redirectCaptivePortal(int connectionFd) {
std::lock_guard lock(this->responseMutex);
std::stringstream stream;
stream << "HTTP/1.1 302 Found\r\n";
stream << "Server: bell-http\r\n";
stream << "Connection: close\r\n";
stream << "Location: http://euphonium.audio\r\n\r\n";
stream << "Content-Length: 9\r\n";
stream << "302 Found";
auto responseStr = stream.str();
write(connectionFd, responseStr.c_str(), responseStr.size());
this->closeConnection(connectionFd);
}
void bell::HTTPServer::redirectTo(const std::string & url, int connectionFd) {
std::lock_guard lock(this->responseMutex);
std::stringstream stream;
@@ -407,6 +429,11 @@ void bell::HTTPServer::findAndHandleRoute(std::string &url, std::string &body,
if (routeSplit.back().find('*') != std::string::npos &&
urlSplit[1] == routeSplit[1]) {
matches = true;
for (int x = 1; x <= routeSplit.size() - 2; x++) {
if (urlSplit[x] != routeSplit[x]) {
matches = false;
}
}
}
if (matches) {