Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions configs/server.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ server {
listen 8080 80 9090;
host 0.0.0.0
host localhost;
port 80 83
port 81 82
error_page 404 /auto_error.html;
autoindex on;
root html_pages/;
Expand All @@ -23,3 +25,12 @@ server {
root icons/;
}
}

server {
location *.ico {
method SOPHIE;
root icons/;
}

}

17 changes: 17 additions & 0 deletions configs/server2.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
server {
port 11 12
port 13
}



server {
port 24 25
}

server {
port 36
port 37
something
port 38
}
80 changes: 54 additions & 26 deletions networking/Servers/parser_conf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,49 @@

#include "parser_conf.hpp"

using namespace std;

void set_port(string &value, vector<int> &port)
{
vector<int> elem = split_stoi(value, ' ');
for (vector<int>::iterator it = elem.begin(); it != elem.end(); ++it) {
port.push_back(*it);
}
}

void parse_conf::set_values_server(std::string s, t_server &server)
{
// this probably needs a better name than 'key'
std::string key = s.substr(0, s.find(' '));
if (key == "server_name")
server._server_name = s.substr(s.find(' ') + 1, s.size());
if (key == "listen")
server._port = ft_stoi((s.substr(s.find(' ') + 1, s.size())));
if (key == "host")
server._host = s.substr(s.find(' ') + 1, s.size());
if (key == "error_page")
server._error_page = split(s.substr(s.find(' ') + 1, s.size()), ' ');
if (key == "auto_index")
server._auto_index = ft_stoi(s.substr(s.find(' ') + 1, s.size()));
if (key == "root")
server._root = s.substr(s.find(' ') + 1, s.size());
if (key == "index")
server._index = s.substr(s.find(' ') + 1, s.size());
if (key == "key")
server._key = s.substr(s.find(' ') + 1, s.size());
if (key == "value")
server._value = s.substr(s.find(' ') + 1, s.size());
//server._port = split_stoi(value, ' ');
try
{
std::string key = s.substr(0, s.find(' '));
std::string value = s.substr(s.find(' ') + 1, s.find(';') - s.find(' ') - 1);
if (key == "server_name")
server._server_name = split(value, ' ');
else if (key == "port")
set_port(value, server._port);
else if (key == "host")
server._host = value;
else if (key == "error_page")
server._error_page = split(value, ' ');
else if (key == "autoindex")
server._autoindex = (value == "on") ? true : false;
else if (key == "root")
server._root = value;
else if (key == "index")
server._index = value;
else if (key == "key")
server._key = value;
else if (key == "value")
server._value = s.substr(s.find(' ') + 1, s.size());
else
throw (s);
}
catch (string &n)
{
std::cout << "Unknown setting: " << n << std::endl;
}
}

// This function gets the whole line and a struct (t_location)
Expand All @@ -41,7 +62,7 @@ void parse_conf::set_values_location(std::string s, t_location &location)
if (key == "cgi")
location._cgi = value;
if (key == "autoindex")
location._autoindex= value;
location._autoindex= (value == "on");
if (key == "client_body_size")
location._client_body_size = ft_stoi(value);
}
Expand All @@ -57,12 +78,17 @@ void parse_conf::set_values_location(std::string s, t_location &location)
bool is_acc = false;
int server_count = 0;
std::string map_key;
while(std::getline(file, line, '\t'))
while(std::getline(file, line, '\n'))
{
std::string key = line.substr(0, line.find(" "));
line = line.substr(0, line.find('\n'));
if (line.empty())
line = trim_whitespace_front(line);
if (line.empty()) {
std::cout << "empty line" << std::endl;
continue;
}
std::string key = line.substr(0, line.find(' '));
//line = line.substr(0, line.find('\n'));
//std::cout << "l|" << line << "|"<< std::endl;
//std::cout << "k|" << key << "|"<< std::endl;
if (key == "server") {
server_count++;
_server.resize(server_count);
Expand All @@ -78,8 +104,10 @@ void parse_conf::set_values_location(std::string s, t_location &location)
}

std::cout << line << std::endl;
if (!is_acc)
if (!is_acc) {
cout << "setting server, sc: " << server_count << endl;
set_values_server(line, _server[server_count - 1]);
}
if (is_acc) {
// send the map with the appropriate key
set_values_location(line, _server[server_count - 1]._location_map[map_key]); // for some reason it is not working yet
Expand All @@ -94,7 +122,7 @@ const std::vector<t_server> &parse_conf::get_server() const
}

// gaat dit werken?
int parse_conf::get_server_port(const t_server &server) {
const std::vector<int> &parse_conf::get_server_port(const t_server &server) {
return server._port;
}

Expand Down
56 changes: 30 additions & 26 deletions networking/Servers/parser_conf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,37 @@
#include "../../all_libs.hpp"
#include "../../http.hpp"

typedef struct s_location {
std::string _address; //this one is replaced by the map-key
std::string _method;
std::string _root;
std::string _cgi;
std::string _autoindex;
int _client_body_size;
} t_location;

typedef struct s_server {
std::string _server_name;
int _port;
std::string _host;
std::vector<std::string> _error_page;
int _auto_index;
std::string _root;
std::string _index;
std::string _key;
std::string _value;
using namespace std;
typedef struct s_location {
string _address; //this one is replaced by the map-key
string _method;
string _root;
string _cgi;
bool _autoindex;
int _client_body_size;
} t_location;

std::map<std::string, t_location> _location_map;
} t_server;
typedef struct s_server {
vector<string> _server_name;
vector<int> _port;
string _host;
vector<string> _error_page;
bool _autoindex;
string _root;
string _index;
string _key;
string _value;
map<string, t_location> _location_map;
} t_server;



class parse_conf
{
private:
<<<<<<< HEAD
vector<t_server> _server;
=======
/*
std::string _server_name;
int _port;
Expand All @@ -52,15 +55,16 @@ class parse_conf

//private
std::vector<t_server> _server;
>>>>>>> sophie_request

void set_values_server(std::string s, t_server &server);
void set_values_location(std::string s, t_location &location);
void set_values_server(string s, t_server &server);
void set_values_location(string s, t_location &location);
public:
parse_conf(std::ifstream &file);
parse_conf(ifstream &file);

/* GETTERS */
int get_server_port(const t_server &server);
const std::vector<t_server> &get_server() const;
const vector<int> &get_server_port(const t_server &server);
const vector<t_server> &get_server() const;
/*
const std::string &get_server_name() const;
int get_port() const;
Expand Down
41 changes: 13 additions & 28 deletions networking/Servers/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,28 @@ int main()
{
std::cout<< RED<< "MAIN" << RESET << std::endl;
std::ifstream file;
const char *path = "configs/server.conf";
const char *path = "configs/server2.conf";
file.open(path);
if(file.is_open()){
std::cout<< RED<< "###########CONFIG OPEN##########" << RESET << std::endl;
parse_conf ex(file);
parse_conf conf(file);



std::cout << "..." << ex.get_server_port(ex.get_server()[0])<< std::endl;
std::cout << "..." << ex.get_server().size() << std::endl;

/*
std::cout << "EX.GETservname(): |" << ex.get_server_name() << std::endl;
std::cout << BLUE << "----------" << RESET << std::endl;
std::cout << "sn " << ex.get_server_name() <<std::endl;
std::cout << "p " << ex.get_port() <<std::endl;
std::cout << "h " << ex.get_host() <<std::endl;
std::cout << "ep.front " << ex.get_error_page().front() <<std::endl;
std::cout << "ep.back " << ex.get_error_page().back() <<std::endl;
std::cout << "ai " << ex.get_auto_index() <<std::endl;
std::cout << "r " << ex.get_root() <<std::endl;
std::cout << "i " << ex.get_index() <<std::endl;
std::cout << "k " << ex.get_key() <<std::endl;
std::cout << "v " << ex.get_value() <<std::endl;
std::cout << BLUE << "----------" << RESET << std::endl;
std::map<std::string, t_location> map_locations = ex.get_location();

std::cout << "loc_map[\"/\"]._method: " << map_locations["/"]._method << std::endl;
std::cout << "loc_map[*.error_image.png]._method: " << map_locations["*.error_image.png"]._method << std::endl;
std::cout << "loc_map[*.ico]._method: " << map_locations["*.ico"]._method << std::endl;
*/
for (int i = 0; i < conf.get_server().size(); ++i) {
vector<int> p = conf.get_server_port(conf.get_server()[i]);
std::cout << "Server: " << i << std::endl;
for (vector<int>::iterator it = p.begin(); it != p.end(); ++it)
std::cout << "port: " << *it << std::endl;
}
vector<t_server> a = conf.get_server();
std::cout<< a[0]._server_name[0] << std::endl;

}
else
std::cout << RED << " ERROR no configfile" << RESET << std::endl;
file.close();



//HDE::parser_config_open r();
HTTP::select_server t;
HTTP::select_server t;
}
20 changes: 20 additions & 0 deletions networking/utils/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,30 @@ std::vector<std::string> split(const std::string &s, char delim)
return elems;
}

std::vector<int> split_stoi(std::string s, char delim)
{
std::vector<int> elems;
std::stringstream ss(s);
std::string item;
while (getline(ss, item, delim))
{
elems.push_back(ft_stoi(item));
}
return elems;
}

int ft_stoi(std::string s)
{
int i;
std::istringstream(s) >> i;
return i;
}

std::string trim_whitespace_front(const std::string &s)
{
int i = 0;
int len = s.length();
while(s[i] == ' ' || s[i] == '\t')
i++;
return (s.substr(i, len - i));
}
2 changes: 2 additions & 0 deletions networking/utils/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ int ft_stoi(std::string s);
void db(std::string s);
void dbe(std::string s);
std::vector<std::string> split(const std::string &s, char delim);
std::vector<int> split_stoi(std::string s, char delim);
std::string trim_whitespace_front(const std::string &s);
#endif //INC_13JUL_UTILS_HPP