From bb841f90c8d631e24323a9e55ae69e1e3df55e4c Mon Sep 17 00:00:00 2001 From: adjoly Date: Fri, 11 Apr 2025 12:13:27 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D=20wip:?= =?UTF-8?q?=20started=20mainloop=20added=20help=20and=20file=20parsing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/help.hpp | 4 +- includes/log.hpp | 6 +- sample.conf | 9 +++ src/config/Server.cpp | 32 ++++++++-- src/help.cpp | 22 +++++-- src/main.cpp | 145 +++++++----------------------------------- test.toml | 0 7 files changed, 79 insertions(+), 139 deletions(-) create mode 100644 sample.conf create mode 100644 test.toml diff --git a/includes/help.hpp b/includes/help.hpp index 442cfc1..900ba33 100644 --- a/includes/help.hpp +++ b/includes/help.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/10 13:43:54 by adjoly #+# #+# */ -/* Updated: 2025/04/10 13:58:52 by adjoly ### ########.fr */ +/* Updated: 2025/04/11 11:36:22 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,3 +14,5 @@ #define SAMPLE_CONF_PATH "./sample.conf" #define WEBSRV_VERSION "v0.1" + +bool help(int, char **); diff --git a/includes/log.hpp b/includes/log.hpp index 9d38ea3..b39558e 100644 --- a/includes/log.hpp +++ b/includes/log.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/20 09:28:27 by adjoly #+# #+# */ -/* Updated: 2025/04/10 14:21:46 by adjoly ### ########.fr */ +/* Updated: 2025/04/11 11:54:37 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -103,9 +103,9 @@ class Logger { os << type << "(" << what << "):" << msg; #else if (what.empty()) - os << "「" << emoji << "」" << type << ":" << msg; + os << "「" << emoji << "」" << type << ": " << msg; else - os << "「" << emoji << "」" << type << "(" << what << "):" << msg; + os << "「" << emoji << "」" << type << "(" << what << "): " << msg; #endif return os.str(); } diff --git a/sample.conf b/sample.conf new file mode 100644 index 0000000..531a1af --- /dev/null +++ b/sample.conf @@ -0,0 +1,9 @@ +[server] +host = "localhost" +port = 8080 + +[server.location./] +methods = { "GET" } +root = "/var/www/html" +dirlist = true +client_max_body_size = "10M" diff --git a/src/config/Server.cpp b/src/config/Server.cpp index 06b4e77..1624708 100644 --- a/src/config/Server.cpp +++ b/src/config/Server.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/24 15:10:07 by adjoly #+# #+# */ -/* Updated: 2025/03/26 08:47:50 by adjoly ### ########.fr */ +/* Updated: 2025/04/11 12:12:56 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,6 +19,7 @@ #include "node/default.hpp" #include "tomlpp.hpp" #include +#include #include #include #include @@ -32,7 +33,7 @@ toml::ANode *Server::_getServerTable(void) { _table->getTable()->find("server"); if (table == _table->getTable()->end()) throw std::runtime_error( - "could not find any [server] table in config file :("); + "could not find any [server] table in config file :("); else serverT = table->second; return serverT; @@ -57,13 +58,23 @@ Server::Server(std::string file_name) { std::string log_file = *static_cast(val); } _log = new Logger(log_file); - _table = _getServerTable(); + try { + _table = _getServerTable(); + } catch(std::runtime_error &e) { + delete _table; + delete tomlFile; + delete _log; + throw e; + } // host and port parsing void *host = accessValue("host", toml::STRING, _table, _log); if (host != not_nullptr) { _host = *static_cast(host); } else { + delete _table; + delete tomlFile; + delete _log; throw std::runtime_error( "no host specified - please specify one in server.host"); } @@ -71,6 +82,9 @@ Server::Server(std::string file_name) { if (host != not_nullptr) { _port = *static_cast(port); } else { + delete _table; + delete tomlFile; + delete _log; throw std::runtime_error( "no port specified - please specify one in server.port"); } @@ -86,9 +100,11 @@ Server::Server(std::string file_name) { std::string str = *static_cast((*vecIt)->getValue()); _server_names->push_back(str); } - } else + } else { _log->warn( - "no server_names all request will be accepted from any hostname"); + "no server_names all request will be accepted from any hostname"); + _server_names = not_nullptr; + } // error_pages parsing map = static_cast *>( @@ -102,7 +118,8 @@ Server::Server(std::string file_name) { it = _table->accessIt("location", toml::TABLE, found); if (found == true && it != _table->getTable()->end()) { _routes = new std::map; - std::map *location_table = it->second->getTable(); + std::map *location_table = + it->second->getTable(); for (it = location_table->begin(); it != location_table->end(); it++) { if (_routes->find(it->first) != _routes->end()) continue; @@ -120,7 +137,8 @@ Server::~Server(void) { } delete _routes; delete _err_pages; - delete _server_names; + if (_server_names != not_nullptr) + delete _server_names; delete _log; // to see if nessecary } diff --git a/src/help.cpp b/src/help.cpp index 7bc3781..56b42d1 100644 --- a/src/help.cpp +++ b/src/help.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/10 13:08:36 by adjoly #+# #+# */ -/* Updated: 2025/04/10 14:20:36 by adjoly ### ########.fr */ +/* Updated: 2025/04/11 11:39:00 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,7 +37,7 @@ void _generateConf(void) { file << "[server]\nhost = \"localhost\"\nport = " "8080\n\n[server.location./]\nmethods = { \"GET\" }\nroot " "= \"/var/www/html\"\ndirlist = true\nclient_max_body_size " - "= \"10M\""; + "= \"10M\"\n"; file.close(); _log.info("config file successfully generated"); } else { @@ -50,16 +50,24 @@ void _printVersion(void) { std::cout << "You are running : Webserv " << WEBSRV_VERSION << std::endl; } -void help(int ac, char **av) { +bool help(int ac, char **av) { if (ac < 2) { _printHelp(); - return; + return true; } std::string option = av[1]; - if (option == "--help" || option == "-v") + if (option == "--help" || option == "-v") { _printHelp(); - else if (option == "--generate" || option == "-g") + return true; + } + else if (option == "--generate" || option == "-g") { _generateConf(); - else if (option == "--version" || option == "-v") + return true; + } + else if (option == "--version" || option == "-v") { _printVersion(); + return true; + } + else + return false; } diff --git a/src/main.cpp b/src/main.cpp index 5c7e217..13b1232 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,128 +12,31 @@ /* ************************************************************************** */ #include -#include -#include +#include +#include +#include #include +#include +#include +#include -#define PORT 8080 -#define BUFFER_SIZE 4096 +int main(int ac, char **av) { + if (help(ac, av)) { + return EXIT_SUCCESS; + } + std::cout << "Starting server..." << std::endl; + if (access(av[1], F_OK) < 0) { + std::cout << "File : " << av[1] << " could not be opened" << std::endl; + return EXIT_FAILURE; + } -int server_socket; -int client_socket; - -void close_socket(int signal) -{ - std::cerr << std::endl << "closing..." << std::endl; - close(client_socket); - close(server_socket); - exit(signal); -} - -std::string getMethod(std::string &data) -{ - return (data.substr(0, data.substr(0, 4).find_last_not_of(" ") + 1)); -} - -int main() -{ - // handle ctrl-C to close server socket - if (signal(SIGPIPE, SIG_IGN) == SIG_ERR || signal(SIGINT, close_socket) == SIG_ERR || signal(SIGQUIT, close_socket) == SIG_ERR) - { - std::cerr << "Error registering signal handlers!" << std::endl; - return 1; - } - - // create a socket - server_socket = socket(AF_INET, SOCK_STREAM, 0); - if (server_socket == -1) - { - std::cerr << "Failed to create socket" << std::endl; - return 1; - } - - // prepare the server address - sockaddr_in server_address; - std::memset(&server_address, 0, sizeof(server_address)); - server_address.sin_family = AF_INET; - server_address.sin_addr.s_addr = INADDR_ANY; - server_address.sin_port = htons(PORT); - - if (bind(server_socket, (sockaddr*)&server_address, sizeof(server_address)) == -1) - { - std::cerr << "Failed to bind socket" << std::endl; - return 1; - } - if (listen(server_socket, 5) == -1) - { - std::cerr << "Failed to listen on socket" << std::endl; - return 1; - } - - std::cout << "Server is listening on port " << PORT << std::endl; - - while (true) - { - // accept an incoming connection - sockaddr_in client_address; - socklen_t client_address_len = sizeof(client_address); - //int client_socket = accept(server_socket, (sockaddr*)&client_address, &client_address_len); - client_socket = accept(server_socket, (sockaddr*)&client_address, &client_address_len); - if (client_socket == -1) { - std::cerr << "Failed to accept connection" << std::endl; - continue; - } - - // receive the HTTP request - std::string received_data; - char buffer[BUFFER_SIZE]; - ssize_t bytes_received; - do - { - std::memset(buffer, 0, BUFFER_SIZE); - bytes_received = recv(client_socket, buffer, BUFFER_SIZE - 1, 0); - if (bytes_received == -1) - { - std::cerr << "Failed to receive request" << std::endl; - close(client_socket); - continue; - } - received_data += std::string(buffer, bytes_received); - } - while (buffer[bytes_received]); - - // parse the request - - // handle the request - std::string response; - - //std::cout << received_data << std::endl; - std::cout << getMethod(received_data) << std::endl; - - if (getMethod(received_data) == "GET") - { - std::cout << "------------ GET REQUEST ------------" << std::endl; - http::Get request(received_data); - - response = request.execute().str(); - } - else if (getMethod(received_data) == "POST") - { - std::cout << "------------ POST REQUEST ------------" << std::endl; - http::Post request(received_data); - - response = request.execute().str(); - //std::cout << "worked" << std::endl; - } - else - { - response = "HTTP/1.1 501 Not Implemented\r\nContent-Type: text/html\r\n\r\n

501 Not Implemented

"; - } - - send(client_socket, response.c_str(), response.length(), 0); - //close(client_socket); - } - - close(server_socket); - return 0; + config::Server *conf; + try { + conf = new config::Server(av[1]); + } catch (std::exception &e) { + std::cout << e.what() << std::endl; + return 1; + } + (void)conf; + delete conf; } diff --git a/test.toml b/test.toml new file mode 100644 index 0000000..e69de29