🏗️」 wip: started mainloop added help and file parsing

This commit is contained in:
2025-04-11 12:13:27 +02:00
parent 504ba7c66c
commit bb841f90c8
7 changed files with 79 additions and 139 deletions

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 **);

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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();
}

9
sample.conf Normal file
View File

@ -0,0 +1,9 @@
[server]
host = "localhost"
port = 8080
[server.location./]
methods = { "GET" }
root = "/var/www/html"
dirlist = true
client_max_body_size = "10M"

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <config/default.hpp>
#include <exception>
#include <stdexcept>
#include <string>
#include <sys/types.h>
@ -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<std::string *>(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<std::string *>(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<unsigned short *>(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<std::string *>((*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<std::map<std::string, toml::ANode *> *>(
@ -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::string, Route *>;
std::map<std::string, toml::ANode *> *location_table = it->second->getTable();
std::map<std::string, toml::ANode *> *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
}

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
}

View File

@ -12,128 +12,31 @@
/* ************************************************************************** */
#include <config/Server.hpp>
#include <tomlpp.hpp>
#include <webserv.hpp>
#include <cstdlib>
#include <exception>
#include <help.hpp>
#include <requests/default.hpp>
#include <tomlpp.hpp>
#include <unistd.h>
#include <webserv.hpp>
#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<html><body><h1>501 Not Implemented</h1></body></html>";
}
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;
}

0
test.toml Normal file
View File