still working on integrating the config file stuff
This commit is contained in:
12
Makefile
12
Makefile
@@ -13,11 +13,19 @@ VPATH = $(DIR_SRCS)
|
|||||||
DIR_SRCS = srcs
|
DIR_SRCS = srcs
|
||||||
|
|
||||||
HEADERS_D = ./srcs
|
HEADERS_D = ./srcs
|
||||||
HEADERS = Webserv.hpp
|
HEADERS = Webserv.hpp \
|
||||||
|
ConfigParser.hpp \
|
||||||
|
ServerConfig.hpp \
|
||||||
|
LocationConfig.hpp \
|
||||||
|
|
||||||
DEPENDENCIES = $(HEADERS:%=$(HEADERS_D)/%)
|
DEPENDENCIES = $(HEADERS:%=$(HEADERS_D)/%)
|
||||||
|
|
||||||
SRCS = main.cpp Webserv.cpp
|
SRCS = main.cpp \
|
||||||
|
Webserv.cpp \
|
||||||
|
ConfigParser.cpp \
|
||||||
|
ServerConfig.cpp \
|
||||||
|
LocationConfig.cpp \
|
||||||
|
|
||||||
|
|
||||||
DIR_OBJS = builds
|
DIR_OBJS = builds
|
||||||
OBJS = $(SRCS:%.cpp=$(DIR_OBJS)/%.o)
|
OBJS = $(SRCS:%.cpp=$(DIR_OBJS)/%.o)
|
||||||
|
|||||||
16
default.config
Normal file
16
default.config
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
server {
|
||||||
|
|
||||||
|
server_name our_server;
|
||||||
|
|
||||||
|
listen 0.0.0.0:80;
|
||||||
|
|
||||||
|
|
||||||
|
index index.html;
|
||||||
|
root ./www/;
|
||||||
|
|
||||||
|
allow_methods GET;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: me <erlazo@student.42.fr> +#+ +:+ +#+ */
|
/* By: me <erlazo@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/07/13 22:11:17 by me #+# #+# */
|
/* 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...
|
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())
|
while (!file.eof())
|
||||||
{
|
{
|
||||||
|
// if we remove the Emtpy lines as well it simplifies things later...
|
||||||
getline(file, buf);
|
getline(file, buf);
|
||||||
// remove # comments here.
|
// remove # comments here.
|
||||||
if ((comment = buf.find_first_of("#")) == std::string::npos)
|
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)
|
// 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
|
// 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()
|
std::vector<Server> * ConfigParser::parse()
|
||||||
{
|
{
|
||||||
// is this the best way to do this? new?
|
// 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 prev = 0;
|
||||||
size_t curr = _content.find_first_not_of(" \t\n", prev);
|
size_t curr = _content.find_first_not_of(" \t\n", prev);
|
||||||
|
|
||||||
|
|
||||||
if (curr == std::string::npos)
|
if (curr == std::string::npos)
|
||||||
throw std::invalid_argument("empty config file");
|
throw std::invalid_argument("empty config file");
|
||||||
while (cur != std::string::npos)
|
while (cur != std::string::npos)
|
||||||
@@ -115,16 +127,18 @@ std::vector<Server> * ConfigParser::parse()
|
|||||||
ret->push_back(parse_server(&curr);
|
ret->push_back(parse_server(&curr);
|
||||||
}
|
}
|
||||||
return (ret);
|
return (ret);
|
||||||
|
// or
|
||||||
|
// return (&ret); // wait no, that doesn't work...
|
||||||
}
|
}
|
||||||
|
|
||||||
// could we return a ref instead?
|
// could we return a ref instead?
|
||||||
// i think no
|
// i think no
|
||||||
Server ConfigParser::parse_server(size_t *start)
|
ServerConfig ConfigParser::parse_server(size_t *start)
|
||||||
{
|
{
|
||||||
Server ret;
|
ServerConfig ret;
|
||||||
size_t key_start;
|
size_t key_start;
|
||||||
size_t value_end;
|
size_t value_end;
|
||||||
size_t prev = _content.find_first_not_of(" \t\n", *start);
|
size_t prev = _content.find_first_not_of(" \t\n", *start);
|
||||||
|
|
||||||
if (prev == std::string::npos || _content[prev] != '{')
|
if (prev == std::string::npos || _content[prev] != '{')
|
||||||
throw std::invalid_argument("bad config file syntax");
|
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...
|
// 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)
|
if ((value_end = check_line_syntax(_content.substr(key_start, curr - key_start))) == FAILED)
|
||||||
throw std::invalid_argument("bad config file arguments");
|
throw std::invalid_argument("bad config file arguments");
|
||||||
if ((int)value_end == EMPTY)
|
// if ((int)value_end == EMPTY)
|
||||||
continue ;
|
// continue ;
|
||||||
|
// could i shove this in the _set_server_value() func call?
|
||||||
std::string value = _content.substr(prev, value_end - prev + key_start + 1)
|
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!
|
// since there's not return anymore
|
||||||
if (_set_server_value(&ret, key, value) == FAILED)
|
// if (_set_server_value(&ret, key, value) == FAILED)
|
||||||
throw std::invalid_argument("bad config file arguments");
|
// throw std::invalid_argument("bad config file arguments");
|
||||||
|
_set_server_value(&ret, key, value); // it handles the throws
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return (ret);
|
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;
|
LocationConfig ret;
|
||||||
size_t key_start;
|
size_t key_start;
|
||||||
size_t value_end;
|
size_t value_end;
|
||||||
size_t prev = _content.find_first_not_of(" \t\n", *start);
|
size_t prev = _content.find_first_not_of(" \t\n", *start);
|
||||||
|
|
||||||
if (prev == std::string::npos || _content[prev] != '{')
|
if (prev == std::string::npos || _content[prev] != '{')
|
||||||
throw std::invalid_argument("bad config file syntax");
|
throw std::invalid_argument("bad config file syntax");
|
||||||
@@ -196,14 +212,18 @@ Location ConfigParser::parse_location(size_t *start)
|
|||||||
break ;
|
break ;
|
||||||
default:
|
default:
|
||||||
_check_proper_line_end(&prev, &curr);
|
_check_proper_line_end(&prev, &curr);
|
||||||
// why bother with the if.. why not throw exception in check_line...
|
// 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)
|
// 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");
|
throw std::invalid_argument("bad config file arguments");
|
||||||
if ((int)value_end == EMPTY)
|
// if we remove all empty lines at the start no need for this
|
||||||
continue ;
|
// 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)
|
std::string value = _content.substr(prev, value_end - prev + key_start + 1)
|
||||||
if (_set_location_value(&ret, key, value) == FAILED)
|
// if (_set_location_value(&ret, key, value) == FAILED)
|
||||||
throw std::invalid_argument("bad config file arguments");
|
// throw std::invalid_argument("bad config file arguments");
|
||||||
|
_set_location_value(&ret, key, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return (ret);
|
return (ret);
|
||||||
@@ -212,8 +232,12 @@ Location ConfigParser::parse_location(size_t *start)
|
|||||||
// not convinced i want to return an int...
|
// not convinced i want to return an int...
|
||||||
// but could be helpful to get more feed back
|
// but could be helpful to get more feed back
|
||||||
// maybe use a throw... but catch in parse_server...
|
// 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)
|
switch (key)
|
||||||
{
|
{
|
||||||
case "server_name":
|
case "server_name":
|
||||||
@@ -274,7 +298,7 @@ int ConfigParser::_set_server_value(Server *server, const std::string key, const
|
|||||||
default :
|
default :
|
||||||
throw std::invalid_argument("bad config file arguments");
|
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
|
// 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...
|
// i'm not sure i like this...
|
||||||
// and it needs to be _check_...
|
// and it needs to be _check_...
|
||||||
// rework this whole thing...
|
// 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
|
// line must be end with semicolon
|
||||||
size_t semicol;
|
size_t semicol;
|
||||||
size_t find;
|
size_t find;
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: me <erlazo@student.42.fr> +#+ +:+ +#+ */
|
/* By: me <erlazo@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/07/11 23:01:41 by me #+# #+# */
|
/* 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?
|
void _check_proper_line_end(size_t prev, size_t curr); // const?
|
||||||
|
|
||||||
|
int _check_for_semicolon(std::string line); // const?
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: me <erlazo@student.42.fr> +#+ +:+ +#+ */
|
/* By: me <erlazo@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/07/23 16:08:00 by me #+# #+# */
|
/* 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 */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|||||||
@@ -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()
|
Webserv::~Webserv()
|
||||||
{
|
{
|
||||||
close(_socket_fd);
|
close(_socket_fd);
|
||||||
|
|||||||
@@ -28,11 +28,20 @@
|
|||||||
#define MSG_TEST "Le Webserv / 20 =D\n"
|
#define MSG_TEST "Le Webserv / 20 =D\n"
|
||||||
#define MSG_BOUNCE "bounced properly ;)\n" // placeholder
|
#define MSG_BOUNCE "bounced properly ;)\n" // placeholder
|
||||||
|
|
||||||
|
// these might only be TMP
|
||||||
|
# define FAILURE -1
|
||||||
|
# define SUCCESS 1
|
||||||
|
|
||||||
class Webserv
|
class Webserv
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
Webserv();
|
Webserv();
|
||||||
// Webserv(Webserv const &src);
|
// Webserv(Webserv const &src);
|
||||||
|
|
||||||
|
// what should it take as arg, *, &, ?
|
||||||
|
Webserv(std::vector<ServerConfig>& servers);
|
||||||
|
|
||||||
~Webserv();
|
~Webserv();
|
||||||
// Webserv &operator=(Webserv const &rhs);
|
// Webserv &operator=(Webserv const &rhs);
|
||||||
|
|
||||||
@@ -41,7 +50,9 @@ class Webserv
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
int _socket_fd; // TODO: replace with vector of "Server" struct
|
int _socket_fd; // TODO: replace with vector of "Server" struct
|
||||||
std::vector<ServerConfig> _servers;
|
|
||||||
|
std::vector<ServerConfig> _servers; // should these be const?
|
||||||
|
|
||||||
int _epfd;
|
int _epfd;
|
||||||
|
|
||||||
// WIP global buffer. Need one variable set per "Client"
|
// WIP global buffer. Need one variable set per "Client"
|
||||||
|
|||||||
@@ -4,11 +4,21 @@
|
|||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <Webserv.hpp>
|
#include <Webserv.hpp>
|
||||||
|
|
||||||
int main(void)
|
int main(int ac, char **av)
|
||||||
{
|
{
|
||||||
try
|
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.init_virtual_servers();
|
||||||
serv.start();
|
serv.start();
|
||||||
|
|||||||
16
www/index.html
Normal file
16
www/index.html
Normal 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>
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user