still working on integrating the config file stuff

This commit is contained in:
Me
2022-07-25 21:00:04 +02:00
parent b9ccf09089
commit 35ac55b5e8
9 changed files with 142 additions and 36 deletions

View File

@@ -13,11 +13,19 @@ VPATH = $(DIR_SRCS)
DIR_SRCS = srcs
HEADERS_D = ./srcs
HEADERS = Webserv.hpp
HEADERS = Webserv.hpp \
ConfigParser.hpp \
ServerConfig.hpp \
LocationConfig.hpp \
DEPENDENCIES = $(HEADERS:%=$(HEADERS_D)/%)
SRCS = main.cpp Webserv.cpp
SRCS = main.cpp \
Webserv.cpp \
ConfigParser.cpp \
ServerConfig.cpp \
LocationConfig.cpp \
DIR_OBJS = builds
OBJS = $(SRCS:%.cpp=$(DIR_OBJS)/%.o)

16
default.config Normal file
View File

@@ -0,0 +1,16 @@
server {
server_name our_server;
listen 0.0.0.0:80;
index index.html;
root ./www/;
allow_methods GET;
}

View File

@@ -6,7 +6,7 @@
/* By: me <erlazo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/13 22:11:17 by me #+# #+# */
/* Updated: 2022/07/25 02:56:26 by me ### ########.fr */
/* Updated: 2022/07/25 20:56:51 by me ### ########.fr */
/* */
/* ************************************************************************** */
@@ -20,6 +20,13 @@ I don't love the use of EMPTY and FAILURE and those
I would rather throw exceptions everywhere...
where do i check if there is only a ";"
we could make a print _content just to make sure it works properly...
need to figure out why return std::vector<ServerConfig> * rather than just simple
not a pointer...
is there a good reason?
*/
@@ -51,11 +58,14 @@ ConfigParser::ConfigParser(const char* path)
{
while (!file.eof())
{
// if we remove the Emtpy lines as well it simplifies things later...
getline(file, buf);
// remove # comments here.
if ((comment = buf.find_first_of("#")) == std::string::npos)
{
_content.append(buf + '\n');
// remove empty lines, i think...
if ((buf.find_first_not_of(" \t")) != std::string::npos)
_content.append(buf + '\n');
}
// else if (comment > 0 && (buf.find_first_not_of(" \t")) != std::string::npos)
// i think this works cuz it will find # no matter what, so it will find
@@ -90,15 +100,17 @@ ConfigParser & ConfigParser::operator=(const ConfigParser& rhs)
*/
// ok what exactly is the benefit of returning a pointer here...
std::vector<Server> * ConfigParser::parse()
{
// is this the best way to do this? new?
std::vector<Server> *ret = new std::vector<Server>();
std::vector<ServerConfig> *ret = new std::vector<Server>();
// yea i could do
std::vector<ServerConfig> ret;
size_t prev = 0;
size_t curr = _content.find_first_not_of(" \t\n", prev);
if (curr == std::string::npos)
throw std::invalid_argument("empty config file");
while (cur != std::string::npos)
@@ -115,16 +127,18 @@ std::vector<Server> * ConfigParser::parse()
ret->push_back(parse_server(&curr);
}
return (ret);
// or
// return (&ret); // wait no, that doesn't work...
}
// could we return a ref instead?
// i think no
Server ConfigParser::parse_server(size_t *start)
ServerConfig ConfigParser::parse_server(size_t *start)
{
Server ret;
size_t key_start;
size_t value_end;
size_t prev = _content.find_first_not_of(" \t\n", *start);
ServerConfig ret;
size_t key_start;
size_t value_end;
size_t prev = _content.find_first_not_of(" \t\n", *start);
if (prev == std::string::npos || _content[prev] != '{')
throw std::invalid_argument("bad config file syntax");
@@ -159,12 +173,14 @@ Server ConfigParser::parse_server(size_t *start)
// why bother with the if.. why not throw exception in check_line...
if ((value_end = check_line_syntax(_content.substr(key_start, curr - key_start))) == FAILED)
throw std::invalid_argument("bad config file arguments");
if ((int)value_end == EMPTY)
continue ;
// if ((int)value_end == EMPTY)
// continue ;
// could i shove this in the _set_server_value() func call?
std::string value = _content.substr(prev, value_end - prev + key_start + 1)
// I might need to catch here, or i could do nothing and trow all the way to the top!
if (_set_server_value(&ret, key, value) == FAILED)
throw std::invalid_argument("bad config file arguments");
// since there's not return anymore
// if (_set_server_value(&ret, key, value) == FAILED)
// throw std::invalid_argument("bad config file arguments");
_set_server_value(&ret, key, value); // it handles the throws
}
}
return (ret);
@@ -172,12 +188,12 @@ Server ConfigParser::parse_server(size_t *start)
Location ConfigParser::parse_location(size_t *start)
LocationConfig ConfigParser::parse_location(size_t *start)
{
Location ret;
size_t key_start;
size_t value_end;
size_t prev = _content.find_first_not_of(" \t\n", *start);
LocationConfig ret;
size_t key_start;
size_t value_end;
size_t prev = _content.find_first_not_of(" \t\n", *start);
if (prev == std::string::npos || _content[prev] != '{')
throw std::invalid_argument("bad config file syntax");
@@ -196,14 +212,18 @@ Location ConfigParser::parse_location(size_t *start)
break ;
default:
_check_proper_line_end(&prev, &curr);
// why bother with the if.. why not throw exception in check_line...
if ((value_end = check_line_syntax(_content.substr(key_start, curr - key_start))) == FAILED)
// why bother with the if.. why not throw exception in check_line...
// ok no we need the if cuz have to set value_end
if ((value_end = _check_for_semicolon(_content.substr(key_start, curr - key_start))) == FAILED)
throw std::invalid_argument("bad config file arguments");
if ((int)value_end == EMPTY)
continue ;
// if we remove all empty lines at the start no need for this
// if ((int)value_end == EMPTY)
// continue ;
// shove this in _set_location_value() func call?
std::string value = _content.substr(prev, value_end - prev + key_start + 1)
if (_set_location_value(&ret, key, value) == FAILED)
throw std::invalid_argument("bad config file arguments");
// if (_set_location_value(&ret, key, value) == FAILED)
// throw std::invalid_argument("bad config file arguments");
_set_location_value(&ret, key, value);
}
}
return (ret);
@@ -212,8 +232,12 @@ Location ConfigParser::parse_location(size_t *start)
// not convinced i want to return an int...
// but could be helpful to get more feed back
// maybe use a throw... but catch in parse_server...
int ConfigParser::_set_server_value(Server *server, const std::string key, const std::string value)
// yea i don't see any reason to return an int
//int ConfigParser::_set_server_value(Server *server, const std::string key, const std::string value)
void ConfigParser::_set_server_value(Server *server, const std::string key, const std::string value)
{
// i could do some checks at the start for value being bad or empty? or is
// that unnecessary since in theory i've already checked that it's good...
switch (key)
{
case "server_name":
@@ -274,7 +298,7 @@ int ConfigParser::_set_server_value(Server *server, const std::string key, const
default :
throw std::invalid_argument("bad config file arguments");
}
return (1); // for now but prolly will change...
// return (1); // for now but prolly will change...
}
// again not sure i want an int ret
@@ -322,9 +346,14 @@ void ConfigParser::_check_proper_line_end(size_t *prev, size_t *curr)
// i'm not sure i like this...
// and it needs to be _check_...
// rework this whole thing...
int ConfigParser::check_line_syntax(std::string line)
{
// no longer returning an int
// fuck no we have to return an int...
// ok keep FAILED as being -1
// i mean we could send a pointer to value_end and then ret void, but why
// bother chaning it now...
int ConfigParser::_check_for_semicolon(std::string line)
{
// line must be end with semicolon
size_t semicol;
size_t find;

View File

@@ -6,7 +6,7 @@
/* By: me <erlazo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/11 23:01:41 by me #+# #+# */
/* Updated: 2022/07/23 15:53:19 by me ### ########.fr */
/* Updated: 2022/07/25 20:56:53 by me ### ########.fr */
/* */
/* ************************************************************************** */
@@ -45,6 +45,7 @@ private:
void _check_proper_line_end(size_t prev, size_t curr); // const?
int _check_for_semicolon(std::string line); // const?
};

View File

@@ -6,7 +6,7 @@
/* By: me <erlazo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/23 16:08:00 by me #+# #+# */
/* Updated: 2022/07/23 16:14:01 by me ### ########.fr */
/* Updated: 2022/07/25 20:09:48 by me ### ########.fr */
/* */
/* ************************************************************************** */

View File

@@ -18,6 +18,21 @@ Webserv::Webserv()
} */
Webserv::Webserv(std::vector<ServerConfig>* servers)
: _servers(servers)
{
// talk to luke about what all this does
// the Param Constructor might need to do dif stuff
std::cout << "Server init\n";
_epfd = ::epoll_create1(0); // (EPOLL_CLOEXEC) for CGI fork ?
if (_epfd == -1)
{
std::perror("err epoll_create1(): ");
throw std::runtime_error("Epoll init");
}
}
Webserv::~Webserv()
{
close(_socket_fd);

View File

@@ -28,11 +28,20 @@
#define MSG_TEST "Le Webserv / 20 =D\n"
#define MSG_BOUNCE "bounced properly ;)\n" // placeholder
// these might only be TMP
# define FAILURE -1
# define SUCCESS 1
class Webserv
{
public:
Webserv();
// Webserv(Webserv const &src);
// what should it take as arg, *, &, ?
Webserv(std::vector<ServerConfig>& servers);
~Webserv();
// Webserv &operator=(Webserv const &rhs);
@@ -41,7 +50,9 @@ class Webserv
private:
int _socket_fd; // TODO: replace with vector of "Server" struct
std::vector<ServerConfig> _servers;
std::vector<ServerConfig> _servers; // should these be const?
int _epfd;
// WIP global buffer. Need one variable set per "Client"

View File

@@ -4,11 +4,21 @@
#include <stdexcept>
#include <Webserv.hpp>
int main(void)
int main(int ac, char **av)
{
try
{
Webserv serv;
std::string config = (ac == 2 ? av[1] : "./default.config");
// like this just looks kinda gross, why bother creating an instance
// and not immediately parsing? like it servers no other purpose...
// what if i call parse directly in the constructor?
// oh because the constructor has no return, but still
// is there a better way?
ConfigParser configParser(config.c_str());
Webserv serv(configParser.parse());
// is this better or worse than using
serv.init_virtual_servers();
serv.start();

16
www/index.html Normal file
View File

@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<!-- <link href="styles/style.css" rel="stylesheet"> -->
</head>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
</html>