Finds the plan file and returns its contents
This commit is contained in:
+44
-2
@@ -1,4 +1,9 @@
|
|||||||
#include "handler.hpp"
|
#include "handler.hpp"
|
||||||
|
#include <filesystem>
|
||||||
|
#include <fstream>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
|
const std::filesystem::path kPATH{"/var/finger/users/"};
|
||||||
|
|
||||||
std::string process(const std::string &username) {
|
std::string process(const std::string &username) {
|
||||||
try {
|
try {
|
||||||
@@ -19,9 +24,46 @@ std::string process(const std::string &username){
|
|||||||
if (username.find("/") != std::string::npos) {
|
if (username.find("/") != std::string::npos) {
|
||||||
throw InvalidInput("Path detected in username");
|
throw InvalidInput("Path detected in username");
|
||||||
}
|
}
|
||||||
}
|
} catch (InvalidInput &e) {
|
||||||
catch(InvalidInput&e){
|
|
||||||
return std::string("InvalidInput: ") + e.what() + std::string("\r\n");
|
return std::string("InvalidInput: ") + e.what() + std::string("\r\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Attempt to open the plan file (if any) and return the contents as a string
|
||||||
|
std::filesystem::path planPath = kPATH / username;
|
||||||
|
|
||||||
|
// Check if the plan file exists
|
||||||
|
if (!std::filesystem::exists(planPath)) {
|
||||||
|
// If no plan file exists, return just the username
|
||||||
return username;
|
return username;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Try to read the plan file
|
||||||
|
try {
|
||||||
|
std::ifstream planFile(planPath);
|
||||||
|
if (!planFile.is_open()) {
|
||||||
|
return username;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string content;
|
||||||
|
std::string line;
|
||||||
|
while (std::getline(planFile, line)) {
|
||||||
|
content += line + "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return the plan content with proper line endings
|
||||||
|
if (!content.empty() && content.back() == '\n') {
|
||||||
|
content.pop_back(); // Remove the last newline
|
||||||
|
content += "\r\n";
|
||||||
|
return content;
|
||||||
|
} else if (!content.empty()) {
|
||||||
|
content += "\r\n";
|
||||||
|
return content;
|
||||||
|
} else {
|
||||||
|
// If file exists but is empty, return just the username
|
||||||
|
return username;
|
||||||
|
}
|
||||||
|
} catch (...) {
|
||||||
|
// If there's any error reading the file, just return the username
|
||||||
|
return username;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -22,18 +22,33 @@ namespace this_coro = boost::asio::this_coro;
|
|||||||
|
|
||||||
awaitable<std::string> dofinger(const std::string &username) {
|
awaitable<std::string> dofinger(const std::string &username) {
|
||||||
co_return process(username);
|
co_return process(username);
|
||||||
//co_return std::string("this is some finger information for ") + username + std::string("\r\n");
|
// co_return std::string("this is some finger information for ") + username +
|
||||||
|
// std::string("\r\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
awaitable<void> echo(tcp::socket socket) {
|
awaitable<void> echo(tcp::socket socket) {
|
||||||
try {
|
try {
|
||||||
char data[1024];
|
char data[1024];
|
||||||
|
auto bytes_read =
|
||||||
co_await socket.async_read_some(boost::asio::buffer(data), deferred);
|
co_await socket.async_read_some(boost::asio::buffer(data), deferred);
|
||||||
auto response = co_await dofinger(std::string(data));
|
// strip \r\n from data
|
||||||
co_await async_write(socket, boost::asio::buffer(response), deferred);
|
std::string username(data, bytes_read);
|
||||||
|
// Remove trailing \r\n characters
|
||||||
|
while (!username.empty() &&
|
||||||
|
(username.back() == '\r' || username.back() == '\n')) {
|
||||||
|
username.pop_back();
|
||||||
|
}
|
||||||
|
auto response = co_await dofinger(username);
|
||||||
|
if (response.compare(std::string(username)) == 0) {
|
||||||
|
// No plan found
|
||||||
|
co_await async_write(
|
||||||
|
socket, boost::asio::buffer(std::string("No plan found\r\n")),
|
||||||
|
deferred);
|
||||||
co_return;
|
co_return;
|
||||||
}
|
}
|
||||||
catch (std::exception &e) {
|
co_await async_write(socket, boost::asio::buffer(response), deferred);
|
||||||
|
co_return;
|
||||||
|
} catch (std::exception &e) {
|
||||||
std::printf("echo Exception: %s\n", e.what());
|
std::printf("echo Exception: %s\n", e.what());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user