mirror of
https://github.com/KeyZox71/webserv.git
synced 2025-05-10 20:28:46 +02:00
「🏗️」 wip: started mainloop added help and file parsing
This commit is contained in:
@ -6,7 +6,7 @@
|
|||||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/04/10 13:43:54 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 SAMPLE_CONF_PATH "./sample.conf"
|
||||||
#define WEBSRV_VERSION "v0.1"
|
#define WEBSRV_VERSION "v0.1"
|
||||||
|
|
||||||
|
bool help(int, char **);
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/03/20 09:28:27 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 */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
9
sample.conf
Normal file
9
sample.conf
Normal 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"
|
@ -6,7 +6,7 @@
|
|||||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/03/24 15:10:07 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 "node/default.hpp"
|
||||||
#include "tomlpp.hpp"
|
#include "tomlpp.hpp"
|
||||||
#include <config/default.hpp>
|
#include <config/default.hpp>
|
||||||
|
#include <exception>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
@ -57,13 +58,23 @@ Server::Server(std::string file_name) {
|
|||||||
std::string log_file = *static_cast<std::string *>(val);
|
std::string log_file = *static_cast<std::string *>(val);
|
||||||
}
|
}
|
||||||
_log = new Logger(log_file);
|
_log = new Logger(log_file);
|
||||||
|
try {
|
||||||
_table = _getServerTable();
|
_table = _getServerTable();
|
||||||
|
} catch(std::runtime_error &e) {
|
||||||
|
delete _table;
|
||||||
|
delete tomlFile;
|
||||||
|
delete _log;
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
|
||||||
// host and port parsing
|
// host and port parsing
|
||||||
void *host = accessValue("host", toml::STRING, _table, _log);
|
void *host = accessValue("host", toml::STRING, _table, _log);
|
||||||
if (host != not_nullptr) {
|
if (host != not_nullptr) {
|
||||||
_host = *static_cast<std::string *>(host);
|
_host = *static_cast<std::string *>(host);
|
||||||
} else {
|
} else {
|
||||||
|
delete _table;
|
||||||
|
delete tomlFile;
|
||||||
|
delete _log;
|
||||||
throw std::runtime_error(
|
throw std::runtime_error(
|
||||||
"no host specified - please specify one in server.host");
|
"no host specified - please specify one in server.host");
|
||||||
}
|
}
|
||||||
@ -71,6 +82,9 @@ Server::Server(std::string file_name) {
|
|||||||
if (host != not_nullptr) {
|
if (host != not_nullptr) {
|
||||||
_port = *static_cast<unsigned short *>(port);
|
_port = *static_cast<unsigned short *>(port);
|
||||||
} else {
|
} else {
|
||||||
|
delete _table;
|
||||||
|
delete tomlFile;
|
||||||
|
delete _log;
|
||||||
throw std::runtime_error(
|
throw std::runtime_error(
|
||||||
"no port specified - please specify one in server.port");
|
"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());
|
std::string str = *static_cast<std::string *>((*vecIt)->getValue());
|
||||||
_server_names->push_back(str);
|
_server_names->push_back(str);
|
||||||
}
|
}
|
||||||
} else
|
} else {
|
||||||
_log->warn(
|
_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
|
// error_pages parsing
|
||||||
map = static_cast<std::map<std::string, toml::ANode *> *>(
|
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);
|
it = _table->accessIt("location", toml::TABLE, found);
|
||||||
if (found == true && it != _table->getTable()->end()) {
|
if (found == true && it != _table->getTable()->end()) {
|
||||||
_routes = new std::map<std::string, Route *>;
|
_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++) {
|
for (it = location_table->begin(); it != location_table->end(); it++) {
|
||||||
if (_routes->find(it->first) != _routes->end())
|
if (_routes->find(it->first) != _routes->end())
|
||||||
continue;
|
continue;
|
||||||
@ -120,6 +137,7 @@ Server::~Server(void) {
|
|||||||
}
|
}
|
||||||
delete _routes;
|
delete _routes;
|
||||||
delete _err_pages;
|
delete _err_pages;
|
||||||
|
if (_server_names != not_nullptr)
|
||||||
delete _server_names;
|
delete _server_names;
|
||||||
delete _log; // to see if nessecary
|
delete _log; // to see if nessecary
|
||||||
}
|
}
|
||||||
|
26
src/help.cpp
26
src/help.cpp
@ -6,7 +6,7 @@
|
|||||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/04/10 13:08:36 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 = "
|
file << "[server]\nhost = \"localhost\"\nport = "
|
||||||
"8080\n\n[server.location./]\nmethods = { \"GET\" }\nroot "
|
"8080\n\n[server.location./]\nmethods = { \"GET\" }\nroot "
|
||||||
"= \"/var/www/html\"\ndirlist = true\nclient_max_body_size "
|
"= \"/var/www/html\"\ndirlist = true\nclient_max_body_size "
|
||||||
"= \"10M\"";
|
"= \"10M\"\n";
|
||||||
file.close();
|
file.close();
|
||||||
_log.info("config file successfully generated");
|
_log.info("config file successfully generated");
|
||||||
} else {
|
} else {
|
||||||
@ -50,16 +50,24 @@ void _printVersion(void) {
|
|||||||
std::cout << "You are running : Webserv " << WEBSRV_VERSION << std::endl;
|
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) {
|
if (ac < 2) {
|
||||||
_printHelp();
|
_printHelp();
|
||||||
return;
|
return true;
|
||||||
}
|
}
|
||||||
std::string option = av[1];
|
std::string option = av[1];
|
||||||
if (option == "--help" || option == "-v")
|
if (option == "--help" || option == "-v") {
|
||||||
_printHelp();
|
_printHelp();
|
||||||
else if (option == "--generate" || option == "-g")
|
return true;
|
||||||
_generateConf();
|
}
|
||||||
else if (option == "--version" || option == "-v")
|
else if (option == "--generate" || option == "-g") {
|
||||||
_printVersion();
|
_generateConf();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else if (option == "--version" || option == "-v") {
|
||||||
|
_printVersion();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
139
src/main.cpp
139
src/main.cpp
@ -12,128 +12,31 @@
|
|||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include <config/Server.hpp>
|
#include <config/Server.hpp>
|
||||||
#include <tomlpp.hpp>
|
#include <cstdlib>
|
||||||
#include <webserv.hpp>
|
#include <exception>
|
||||||
|
#include <help.hpp>
|
||||||
#include <requests/default.hpp>
|
#include <requests/default.hpp>
|
||||||
|
#include <tomlpp.hpp>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <webserv.hpp>
|
||||||
|
|
||||||
#define PORT 8080
|
int main(int ac, char **av) {
|
||||||
#define BUFFER_SIZE 4096
|
if (help(ac, av)) {
|
||||||
|
return EXIT_SUCCESS;
|
||||||
int server_socket;
|
}
|
||||||
int client_socket;
|
std::cout << "Starting server..." << std::endl;
|
||||||
|
if (access(av[1], F_OK) < 0) {
|
||||||
void close_socket(int signal)
|
std::cout << "File : " << av[1] << " could not be opened" << std::endl;
|
||||||
{
|
return EXIT_FAILURE;
|
||||||
std::cerr << std::endl << "closing..." << std::endl;
|
|
||||||
close(client_socket);
|
|
||||||
close(server_socket);
|
|
||||||
exit(signal);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string getMethod(std::string &data)
|
config::Server *conf;
|
||||||
{
|
try {
|
||||||
return (data.substr(0, data.substr(0, 4).find_last_not_of(" ") + 1));
|
conf = new config::Server(av[1]);
|
||||||
}
|
} catch (std::exception &e) {
|
||||||
|
std::cout << e.what() << std::endl;
|
||||||
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;
|
return 1;
|
||||||
}
|
}
|
||||||
|
(void)conf;
|
||||||
// create a socket
|
delete conf;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user