This commit is contained in:
Talyx
2022-01-23 15:56:32 +03:00
26 changed files with 1020 additions and 734 deletions

View File

@@ -11,19 +11,22 @@ SRC = $(wildcard ./src/*.cpp)
SRC += $(wildcard ./src/*/*.cpp) SRC += $(wildcard ./src/*/*.cpp)
HEADERS = $(wildcard ./includes/*.hpp) HEADERS = $(wildcard ./includes/*.hpp)
HEADERS += $(wildcard src/Autoindex/*.hpp)
HEADERS += $(wildcard src/Header/*.hpp)
HEADERS += $(wildcard src/Server/*.hpp)
HEADERS += $(wildcard src/config/*.hpp) HEADERS += $(wildcard src/config/*.hpp)
INCLUDES = ./includes/ INCLUDES = ./includes/ -I src/Autoindex -I src/config -I src/Header -I src/Server
OBJ = $(SRC:.cpp=.o) OBJ = $(SRC:.cpp=.o)
all: $(NAME) all: $(NAME)
$(OBJ): %.o: %.cpp $(SRC) $(HEADERS) Makefile $(OBJ): %.o: %.cpp $(SRC) $(HEADERS) Makefile
$(CXX) $(CXXFLAGS) -c $< -I $(INCLUDES) -o $@ $(CXX) $(CXXFLAGS) -c $< -I $(INCLUDES) -o $@
$(NAME): $(OBJ) $(HEADERS) $(NAME): $(OBJ) $(HEADERS)
$(CXX) $(CXXFLAGS) $(OBJ) -o $(NAME) $(CXX) $(CXXFLAGS) $(OBJ) -o $(NAME)
clean: clean:
$(RM) $(OBJ) $(RM) $(OBJ)

View File

@@ -2,12 +2,16 @@
name = ["jopa.com", "popa.org"] name = ["jopa.com", "popa.org"]
host = "127.0.0.1" host = "127.0.0.1"
port = 8080 port = 8080
body_size_limit = 10
[server.error_page] [server.error_page]
404 = "/var/www/html/error_404.html" 404 = "/var/www/html/error_404.html"
405 = "/var/www/html/error_405.html" 405 = "/var/www/html/error_405.html"
body_size_limit = 10 # error_404 = "error_404.html"
[server.error_page]
400 = "error_400.html"
401 = "error_401.html"
[[server.location]] [[server.location]]
redirect = [301, "http://localhost/secret"] redirect = ["301", "http://localhost/secret"]
location = "/" location = "/"
root = "/var/www/html/" root = "/var/www/html/"
methods = ["GET", "POST"] methods = ["GET", "POST"]
@@ -25,7 +29,7 @@
[[server]] [[server]]
name = "2222" name = "2222"
host = "10.0.0.1" ip = "10.0.0.1"
port = 8081 port = 8081
body_size_limit = 10 body_size_limit = 10
[[server.location]] [[server.location]]

View File

@@ -2,8 +2,9 @@
name = "serv1" name = "serv1"
host = "127.0.0.1" host = "127.0.0.1"
port = 8080 port = 8080
error_page = "error.html"
body_size_limit = 10 body_size_limit = 10
[server.error_page]
404 = "error_404.html"
[[server.location]] [[server.location]]
location = "/" location = "/"
root = "/var/www/html/jopa.html" root = "/var/www/html/jopa.html"
@@ -22,8 +23,12 @@
name = "2222" name = "2222"
host = "10.0.0.1" host = "10.0.0.1"
port = 8081 port = 8081
error_page = "error2.html"
body_size_limit = 10 body_size_limit = 10
[server.error_page]
404 = "error_404.html"
405 = "error_405.html"
406 = "error_406.html"
407 = "error_407.html"
[[server.location]] [[server.location]]
location = "/root2/" location = "/root2/"
root = "/var/www/html/jopa.html" root = "/var/www/html/jopa.html"

View File

@@ -8,53 +8,39 @@
#define PINK "\033[35m" #define PINK "\033[35m"
#define TURGUOISE "\033[36m" #define TURGUOISE "\033[36m"
#define ZERO_C "\033[0m" #define ZERO_C "\033[0m"
#define HOME "www" #define HOME "www"
#define BUFFSIZE 1024
#define MAX_CLIENT 1000
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <arpa/inet.h>
#include <sys/select.h>
#include <poll.h>
#include <sys/epoll.h> #include <sys/epoll.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <arpa/inet.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <poll.h>
#include <dirent.h>
#include <fstream> #include <fstream>
#include <vector> #include <vector>
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <string.h>
#include <sys/stat.h>
#include <map> #include <map>
#include <fstream> #include <fstream>
#include <dirent.h>
#include <algorithm> #include <algorithm>
#include <cctype> #include <cctype>
#include "Socket.hpp"
#include "HeaderHandl.hpp"
#include "ServerConfig.hpp"
#include "Autoindex.hpp"
#include "Server.hpp"
#include "Header.hpp"
#include <map>
#include <vector>
void parse(void);
class toml_node;
/* typedef std::vector<TOMLMap *> TOMLArray; */
/* typedef std::vector<TOMLArray *> TOMLArrayOfMap; */
typedef std::map<std::string, toml_node *> TOMLMap; // = JSONObject
typedef std::vector<TOMLMap *> TOMLMapArray;
typedef std::vector<toml_node *> TOMLArray;
void display(TOMLMap *config);
#endif #endif

View File

@@ -2,6 +2,8 @@
# define HEADER_HPP # define HEADER_HPP
#include "webserv.hpp" #include "webserv.hpp"
#include "HeaderHandl.hpp"
#include "Autoindex.hpp"
class Header class Header
{ {

View File

@@ -1,4 +1,5 @@
#include "webserv.hpp" #include "Server.hpp"
//----------------------------------------------Constructors----------------------------------------------------------------------------------- //----------------------------------------------Constructors-----------------------------------------------------------------------------------
Server::Server() Server::Server()
{ {
@@ -17,6 +18,36 @@ Server::Server(std::string path)
//----------------------------------------------Configuration----------------------------------------------------------------------------------- //----------------------------------------------Configuration-----------------------------------------------------------------------------------
void Server::readConfig(void) void Server::readConfig(void)
{ {
TOMLMap *root = parse();
TOMLMap *map;
TOMLMap::iterator it1;
TOMLMapArray *arr;
TOMLMapArray::iterator it;
arr = root->find("server")->second->getMapArray();
it = arr->begin();
while (it != arr->end())
{
std::cout << BLUE << *it << std::endl;
map = *it;
it1 = map->begin();
while (it1 != map->end())
{
std::cout << TURGUOISE << it1->first << it1->second << ZERO_C << std::endl;
++it1;
}
++it;
}
} }

View File

@@ -2,9 +2,10 @@
#define SERVER_HPP #define SERVER_HPP
#include "webserv.hpp" #include "webserv.hpp"
#include "Header.hpp"
#define BUFFSIZE 1024 #include "ServerConfig.hpp"
#define MAX_CLIENT 1000 #include "Socket.hpp"
#include "parse.hpp"
class Header; class Header;
@@ -17,6 +18,7 @@ private:
struct epoll_event _events[MAX_CLIENT]; struct epoll_event _events[MAX_CLIENT];
struct sockaddr_in _addres; struct sockaddr_in _addres;
std::string _ip; std::string _ip;
std::vector<ServerConfig> _configs;
private: private:
void checkError(int fd, std::string str); void checkError(int fd, std::string str);

165
src/config/TOMLNode.cpp Normal file
View File

@@ -0,0 +1,165 @@
#include "TOMLNode.hpp"
/* toml_node::toml_node(void) */
/* { */
/* return; */
/* } */
/* toml_node::toml_node(const toml_node &src) */
/* { */
/* *this = src; */
/* return; */
/* } */
/* toml_node::~toml_node(void) */
/* { */
/* return; */
/* } */
/* toml_node &toml_node::operator=(const toml_node &rhs) */
/* { */
/* //code */
/* return (*this); */
/* } */
toml_node::e_type toml_node::get_type(void)
{
return (type);
}
TOMLMap *toml_node::getMap(void)
{
return (value.map);
}
TOMLMapArray *toml_node::getMapArray(void)
{
return (value.map_array);
}
void toml_node::setString(std::string *str)
{
value.str = str;
type = STRING;
}
void toml_node::setNumber(int num)
{
value.integer = num;
type = NUM;
}
void toml_node::setArr(TOMLArray *toml_array)
{
value.array = toml_array;
type = ARRAY;
}
void toml_node::setBool(bool b)
{
value.boolean = b;
type = BOOL;
}
void toml_node::setNil(void)
{
type = NIL;
}
void toml_node::setObject(TOMLMap *obj)
{
value.map = obj;
type = MAP;
}
void toml_node::setMapArray(TOMLMapArray *map_array)
{
value.map_array = map_array;
type = MAPARRAY;
}
std::string *toml_node::toString(void) const
{
switch (type)
{
case STRING:
{
return (value.str);
}
case NUM:
{
std::stringstream ss;
ss << value.integer;
std::string *result = new std::string();
ss >> *result;
return (result);
}
case ARRAY:
{
TOMLArray::iterator it;
std::string *result = new std::string("[ ");
for (it = value.array->begin(); it != value.array->end(); ++it)
{
*result += *((*it)->toString());
*result += ", ";
}
*result += " ]";
return (result);
}
case MAP:
{
return (TOMLMap_to_string(value.map));
}
case MAPARRAY:
{
std::stringstream ss;
std::string *result = new std::string();
TOMLMapArray::iterator it;
TOMLMapArray *map_array = value.map_array;
ss << "[\n";
for (it = map_array->begin(); it != map_array->end(); ++it)
{
ss << (*TOMLMap_to_string(*it));
ss << ", " << std::endl;
}
ss << "]\n";
/* ss >> *result; */
*result = ss.str();
return (result);
}
case BOOL:
{
std::string *result;
if (value.boolean)
result = new std::string("true");
else
result = new std::string("false");
return (result);
}
default:
return ( new std::string("Not implemented :)"));
}
}
std::string *toml_node::TOMLMap_to_string(TOMLMap *map)
{
std::stringstream ss;
std::string *result = new std::string();
TOMLMap::iterator it;
ss << "{\n";
for (it = map->begin(); it != map->end(); ++it)
{
ss << it->first
<< ": "
<< *(it->second->toString())
<< std::endl;
}
ss << "}" << std::endl;
/* ss >> *result; */
*result = ss.str();
return (result);
}

View File

@@ -2,6 +2,8 @@
#define TOMLNODE_HPP #define TOMLNODE_HPP
#include "webserv.hpp" #include "webserv.hpp"
#include "tomlstuff.hpp"
#include <map> #include <map>
#include <vector> #include <vector>
#include <fstream> #include <fstream>
@@ -11,9 +13,6 @@
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
class toml_node class toml_node
{ {
@@ -37,145 +36,23 @@ class toml_node
STRING, NUM, BOOL, ARRAY, MAP, MAPARRAY, NIL STRING, NUM, BOOL, ARRAY, MAP, MAPARRAY, NIL
} type; } type;
enum e_type get_type(void) enum e_type get_type(void);
{
return (type);
}
TOMLMap *getMap(void) TOMLMap *getMap(void);
{ TOMLMapArray *getMapArray(void);
return (value.map);
}
TOMLMapArray *getMapArray(void) void setString(std::string *str);
{ void setNumber(int num);
return (value.map_array);
}
void setString(std::string *str) void setArr(TOMLArray *toml_array);
{ void setBool(bool b);
value.str = str; void setNil(void);
type = STRING; void setObject(TOMLMap *obj);
} void setMapArray(TOMLMapArray *map_array);
void setNumber(int num) std::string *toString(void) const;
{ static std::string *TOMLMap_to_string(TOMLMap *map);
value.integer = num;
type = NUM;
}
void setArr(TOMLArray *toml_array)
{
value.array = toml_array;
type = ARRAY;
}
void setBool(bool b)
{
value.boolean = b;
type = BOOL;
}
void setNil(void)
{
type = NIL;
}
void setObject(TOMLMap *obj)
{
value.map = obj;
type = MAP;
}
void setMapArray(TOMLMapArray *map_array)
{
value.map_array = map_array;
type = MAPARRAY;
}
static std::string *TOMLMap_to_string(TOMLMap *map)
{
std::stringstream ss;
std::string *result = new std::string();
TOMLMap::iterator it;
ss << "{\n";
for (it = map->begin(); it != map->end(); ++it)
{
ss << it->first
<< ": "
<< *(it->second->toString())
<< std::endl;
}
ss << "}" << std::endl;
/* ss >> *result; */
*result = ss.str();
return (result);
}
std::string *toString(void) const
{
switch (type)
{
case STRING:
{
return (value.str);
}
case NUM:
{
std::stringstream ss;
ss << value.integer;
std::string *result = new std::string();
ss >> *result;
return (result);
}
case ARRAY:
{
TOMLArray::iterator it;
std::string *result = new std::string("[ ");
for (it = value.array->begin(); it != value.array->end(); ++it)
{
*result += *((*it)->toString());
*result += ", ";
}
*result += " ]";
return (result);
}
case MAP:
{
return (TOMLMap_to_string(value.map));
}
case MAPARRAY:
{
std::stringstream ss;
std::string *result = new std::string();
TOMLMapArray::iterator it;
TOMLMapArray *map_array = value.map_array;
ss << "[\n";
for (it = map_array->begin(); it != map_array->end(); ++it)
{
ss << (*TOMLMap_to_string(*it));
ss << ", " << std::endl;
}
ss << "]\n";
/* ss >> *result; */
*result = ss.str();
return (result);
}
case BOOL:
{
std::string *result;
if (value.boolean)
result = new std::string("true");
else
result = new std::string("false");
return (result);
}
default:
return ( new std::string("Not implemented :)"));
}
}
}; };
#endif #endif

482
src/config/TOMLParser.cpp Normal file
View File

@@ -0,0 +1,482 @@
#include "TOMLParser.hpp"
/* -Template::-Template(void) */
/* { */
/* return; */
/* } */
/* -Template::-Template(const -Template &src) */
/* { */
/* *this = src; */
/* return; */
/* } */
/* -Template::~-Template(void) */
/* { */
/* return; */
/* } */
/* -Template &-Template::operator=(const -Template &rhs) */
/* { */
/* //code */
/* return (*this); */
/* } */
namespace config
{
TOMLParser::TOMLParser(const std::string filename) : tokenizer(filename)
{};
void TOMLParser::processMap(void)
{
std::cerr << "Processing map" << std::endl;
toml_node *map_node;
s_token current;
try { current = tokenizer.getToken(); }
catch (std::logic_error e)
{
std::cerr << e.what() << std::endl;
return;
}
if (current.type == MAP_DECL)
{
if (tokenizer.getToken().type != NEWLINE)
throw std::logic_error("no newline after MAP_DECL");
map_node = parseMap();
}
else
throw std::logic_error("unexpected token in processMap");
std::cout << current.value << std::endl;
std::vector<std::string> full_name;
full_name = split_name(current.value);
put_to_subtable(root, full_name, map_node, toml_node::MAP);
}
toml_node *TOMLParser::parseMap(void)
{
std::cerr << "Parsing map" << std::endl;
toml_node *node = new toml_node;
TOMLMap *mapObject = new TOMLMap;
bool completed = false;
while (!completed)
{
if (tokenizer.hasMoreTokens())
{
s_token nextToken;
try { nextToken = tokenizer.getToken(); }
catch (std::logic_error e)
{
std::cerr << e.what() << std::endl;
break;
}
if (nextToken.type == MAPARRAY_DECL)
{
tokenizer.rollBackToken();
tokenizer.set_last(NEWLINE);
break;
}
else if (nextToken.type == MAP_DECL)
{
tokenizer.rollBackToken();
tokenizer.set_last(NEWLINE);
break;
}
std::string key = nextToken.value;
std::cerr << key << std::endl;
if (tokenizer.getToken().type != ASSIGN)
throw std::logic_error("EXPECTED assign!");
nextToken = tokenizer.getToken();
switch (nextToken.type)
{
case STRING:
{
tokenizer.rollBackToken();
(*mapObject)[key] = parseString();
break;
}
case OPEN_BRACKET:
{
(*mapObject)[key] = parseArray();
break;
}
case NUMBER:
{
tokenizer.rollBackToken();
(*mapObject)[key] = parseNumber();
break;
}
case BOOL:
{
tokenizer.rollBackToken();
(*mapObject)[key] = parseBool();
break;
}
case MAPARRAY_DECL:
{
std::cerr << "reached MAPARRAY_DECL in parseMap" << std::endl;
completed = true;
break;
}
default:
{
/* throw std::logic_error("jopa in parseMap"); */
std::cerr << "Unknown token, marking as complete" << std::endl;
completed = true;
break;
}
}
if (tokenizer.hasMoreTokens())
nextToken = tokenizer.getToken();
else
break;
if (nextToken.type != NEWLINE)
{
throw std::logic_error("EXPECTED newline");
}
}
else
{
throw std::logic_error("parseMap: no more tokens");
}
}
node->setObject(mapObject);
return (node);
}
void TOMLParser::processMapArray(void)
{
std::cerr << "Parsing MapArray" << std::endl;
toml_node *map_node;
s_token current;
try { current = tokenizer.getToken(); }
catch (std::logic_error e)
{
std::cerr << e.what() << std::endl;
return;
}
if (current.type == MAPARRAY_DECL)
{
if (tokenizer.getToken().type != NEWLINE)
throw std::logic_error("no newline after map_array declaration");
map_node = parseMap();
}
else
throw std::logic_error("unexpected token in processMapArray");
std::cout << current.value << std::endl;
std::vector<std::string> full_name;
full_name = split_name(current.value);
put_to_subtable(root, full_name, map_node, toml_node::MAPARRAY);
}
toml_node *TOMLParser::parseString(void)
{
/* toml_node *node; */
toml_node *node = new toml_node;
std::string *sValue;
std::cerr << "Parsing string" << std::endl;
s_token token = tokenizer.getToken();
sValue = new std::string(token.value);
node->setString(sValue);
return (node);
}
toml_node *TOMLParser::parseNumber(void)
{
toml_node *node = new toml_node;
int value;
std::cerr << "Parsing number" << std::endl;
s_token token = tokenizer.getToken();
value = std::atoi(token.value.c_str());
node->setNumber(value);
return (node);
}
toml_node *TOMLParser::parseArray(void)
{
std::cerr << "Parsing array" << std::endl;
toml_node *node;
toml_node *result = new toml_node;
TOMLArray *array = new TOMLArray;
bool completed = false;
s_token current;
while (!completed)
{
if (!tokenizer.hasMoreTokens())
throw std::logic_error("No more tokens");
else
{
current = tokenizer.getToken();
switch (current.type)
{
case OPEN_BRACKET:
{
node = parseArray();
break;
}
case STRING:
{
tokenizer.rollBackToken();
node = parseString();
break;
}
case NUMBER:
{
tokenizer.rollBackToken();
node = parseNumber();
break;
}
case BOOL:
{
tokenizer.rollBackToken();
node = parseBool();
break;
}
case NIL:
{
node = parseNil();
break;
}
default:
{
throw std::logic_error("unkown token in parseArray");
}
}
array->push_back(node);
current = tokenizer.getToken();
if (current.type == COMMA)
continue;
else if (current.type == CLOSE_BRACKET)
completed = true;
else
throw std::invalid_argument("Unexpected token in array!");
}
}
result->setArr(array);
return (result);
}
toml_node *TOMLParser::parseBool(void)
{
toml_node *node = new toml_node;
bool value;
std::cerr << "Parsing bool" << std::endl;
s_token token = tokenizer.getToken();
if (token.value == "true")
value = true;
else if (token.value == "false")
value = false;
else
throw std::invalid_argument("Unexpected bool value");
node->setBool(value);
return (node);
}
toml_node *TOMLParser::parseNil(void)
{
toml_node *node = new toml_node;
std::cerr << "Parsing NIL" << std::endl;
node->setNil();
return (node);
}
/* parse tha root ! */
TOMLMap *TOMLParser::parse(void)
{
std::cerr << "Parsing ROOT" << std::endl;
root = new TOMLMap;
bool completed = false;
while (!completed)
{
if (tokenizer.hasMoreTokens())
{
s_token current;
try { current = tokenizer.getToken(); }
catch (std::logic_error e)
{
std::cerr << e.what() << std::endl;
break;
}
if (current.type == MAPARRAY_DECL)
{
/* processMapArray(); */
tokenizer.set_last(NEWLINE);
tokenizer.rollBackToken();
processMapArray();
}
else if (current.type == MAP_DECL)
{
std::cerr << "MAP_DECL value: " << current.value << std::endl;
tokenizer.set_last(NEWLINE);
tokenizer.rollBackToken();
/* if (tokenizer.getToken().type != NEWLINE) */
/* throw std::logic_error("no newline after MAP_DECL"); */
/* (*mapObject)[nextToken.value] = parseMap(); */
processMap();
continue;
}
else
{
std::string key = current.value;
std::cerr << key << std::endl;
if (tokenizer.getToken().type != ASSIGN)
throw std::logic_error("EXPECTED assign!");
current = tokenizer.getToken();
switch (current.type)
{
case STRING:
{
tokenizer.rollBackToken();
(*root)[key] = parseString();
break;
}
case OPEN_BRACKET:
{
(*root)[key] = parseArray();
break;
}
case NUMBER:
{
tokenizer.rollBackToken();
(*root)[key] = parseNumber();
break;
}
case BOOL:
{
tokenizer.rollBackToken();
(*root)[key] = parseBool();
break;
}
default:
{
/* throw std::logic_error("jopa in parseMap"); */
std::cerr << "Unknown token, marking as complete" << std::endl;
completed = true;
break;
}
}
if (tokenizer.hasMoreTokens())
current = tokenizer.getToken();
else
break;
if (current.type != NEWLINE)
{
throw std::logic_error("EXPECTED newline");
}
}
}
else
{
completed = true;
break;
}
}
return (root);
}
std::vector<std::string> TOMLParser::split_name(std::string name)
{
std::vector<std::string> full_name;
size_t dot;
while (1)
{
dot = name.find('.');
if (dot == std::string::npos)
break;
/* std::cout << dot << std::endl; */
full_name.push_back(name.substr(0, dot));
name.erase(0, dot + 1);
}
full_name.push_back(name);
for (size_t i = 0; i < full_name.size(); i++)
std::cout << full_name[i] << ", ";
std::cout << std::endl;
return (full_name);
}
void TOMLParser::put_to_subtable(TOMLMap *root,
std::vector<std::string> full_name,
toml_node *map_node, toml_node::e_type type)
{
std::vector<std::string>::iterator subname = full_name.begin();
toml_node *maparr_node;
TOMLMap::iterator it;
TOMLMap *local_root = root;
while (1)
{
if (subname + 1 == full_name.end())
{
it = local_root->find(*subname);
if (type == toml_node::MAPARRAY)
{
if (it == local_root->end())
{
maparr_node = new toml_node;
TOMLMapArray *map_array = new TOMLMapArray;
map_array->push_back(map_node->getMap());
maparr_node->setMapArray(map_array);
(*local_root)[*subname] = maparr_node;
}
else
(it->second)->getMapArray()->push_back(map_node->getMap());
}
else if (type == toml_node::MAP)
{
if (it == local_root->end())
{
(*local_root)[*subname] = map_node;
}
else
throw std::logic_error("map already in subtable!");
}
break;
}
else
{
it = local_root->find(*subname);
toml_node *map_node2;
map_node2 = new toml_node;
TOMLMap *map = new TOMLMap;
map_node2->setObject(map);
/* subname not found in local_root */
if (it == local_root->end())
{
(*local_root)[*subname] = map_node2;
local_root = map;
}
/* subname found in local_root */
else
local_root = *((it->second)->getMapArray()->end() - 1);
}
++subname;
}
}
}

View File

@@ -1,8 +1,10 @@
#ifndef TOMLPARSER_HPP #ifndef TOMLPARSER_HPP
#define TOMLPARSER_HPP #define TOMLPARSER_HPP
#include "TOMLNode.hpp" #include "webserv.hpp"
#include "tomlstuff.hpp"
#include "Tokenizer.hpp" #include "Tokenizer.hpp"
#include "TOMLNode.hpp"
#include <string> #include <string>
namespace config namespace config
@@ -15,380 +17,33 @@ namespace config
/* toml_node *current; //node currently being parsed */ /* toml_node *current; //node currently being parsed */
Tokenizer tokenizer; Tokenizer tokenizer;
static std::vector<std::string> split_name(std::string name);
static void put_to_subtable(TOMLMap *root,
std::vector<std::string> full_name,
toml_node *map_node, toml_node::e_type type);
public: public:
TOMLParser(const std::string filename) : tokenizer(filename) {} TOMLParser(const std::string filename);
TOMLMap *parse(void); TOMLMap *parse(void);
toml_node *parseMap(void) toml_node *parseMap(void);
{ void processMap(void);
std::cerr << "Parsing object" << std::endl;
toml_node *node = new toml_node;
TOMLMap *mapObject = new TOMLMap;
bool completed = false;
while (!completed)
{
if (tokenizer.hasMoreTokens())
{
s_token nextToken;
try { nextToken = tokenizer.getToken(); }
catch (std::logic_error e)
{
std::cerr << e.what() << std::endl;
break;
}
if (nextToken.type == MAPARRAY_DECL)
{
tokenizer.rollBackToken();
break;
}
std::string key = nextToken.value;
std::cerr << key << std::endl;
if (tokenizer.getToken().type != ASSIGN)
throw std::logic_error("EXPECTED assign!");
nextToken = tokenizer.getToken();
switch (nextToken.type)
{
case STRING:
{
tokenizer.rollBackToken();
(*mapObject)[key] = parseString();
break;
}
case ARR_OPEN:
{
(*mapObject)[key] = parseArray();
break;
}
case NUMBER:
{
tokenizer.rollBackToken();
(*mapObject)[key] = parseNumber();
break;
}
case BOOL:
{
tokenizer.rollBackToken();
(*mapObject)[key] = parseBool();
break;
}
case MAPARRAY_DECL:
{
std::cerr << "reached MAPARRAY_DECL in parseMap" << std::endl;
completed = true;
break;
}
default:
{
/* throw std::logic_error("jopa in parseMap"); */
std::cerr << "Unknown token, marking as complete" << std::endl;
completed = true;
break;
}
}
if (tokenizer.hasMoreTokens())
nextToken = tokenizer.getToken();
else
break;
if (nextToken.type != NEWLINE)
{
throw std::logic_error("EXPECTED newline");
}
}
else
{
throw std::logic_error("parseMap: no more tokens");
}
}
node->setObject(mapObject);
return (node);
}
toml_node *parseString(void) void processMapArray(void);
{
/* toml_node *node; */
toml_node *node = new toml_node;
std::string *sValue;
std::cerr << "Parsing string" << std::endl; toml_node *parseString(void);
s_token token = tokenizer.getToken();
sValue = new std::string(token.value);
node->setString(sValue);
return (node); toml_node *parseNumber(void);
}
toml_node *parseNumber(void) toml_node *parseArray(void);
{
toml_node *node = new toml_node;
int value;
std::cerr << "Parsing number" << std::endl; toml_node *parseBool(void);
s_token token = tokenizer.getToken();
value = std::atoi(token.value.c_str());
node->setNumber(value);
return (node); toml_node *parseNil(void);
}
toml_node *parseArray(void)
{
std::cerr << "Parsing array" << std::endl;
toml_node *node;
toml_node *result = new toml_node;
TOMLArray *array = new TOMLArray;
bool completed = false;
s_token current;
while (!completed)
{
if (!tokenizer.hasMoreTokens())
throw std::logic_error("No more tokens");
else
{
current = tokenizer.getToken();
switch (current.type)
{
case ARR_OPEN:
{
node = parseArray();
break;
}
case STRING:
{
tokenizer.rollBackToken();
node = parseString();
break;
}
case NUMBER:
{
tokenizer.rollBackToken();
node = parseNumber();
break;
}
case BOOL:
{
tokenizer.rollBackToken();
node = parseBool();
break;
}
case NIL:
{
node = parseNil();
break;
}
default:
{
throw std::logic_error("unkown token in parseArray");
}
}
array->push_back(node);
current = tokenizer.getToken();
if (current.type == COMMA)
continue;
else if (current.type == ARR_CLOSE)
completed = true;
else
throw std::invalid_argument("Unexpected token in array!");
}
}
result->setArr(array);
return (result);
}
toml_node *parseBool(void)
{
toml_node *node = new toml_node;
bool value;
std::cerr << "Parsing bool" << std::endl;
s_token token = tokenizer.getToken();
if (token.value == "true")
value = true;
else if (token.value == "false")
value = false;
else
throw std::invalid_argument("Unexpected bool value");
node->setBool(value);
return (node);
}
toml_node *parseNil(void)
{
toml_node *node = new toml_node;
std::cerr << "Parsing NIL" << std::endl;
node->setNil();
return (node);
}
}; };
/* parse tha root ! */ /* parse tha root ! */
TOMLMap *TOMLParser::parse(void) /* TOMLMap *TOMLParser::parse(void); */
{
std::cerr << "Parsing ROOT" << std::endl;
root = new TOMLMap;
bool completed = false;
while (!completed)
{
if (tokenizer.hasMoreTokens())
{
s_token current;
try { current = tokenizer.getToken(); }
catch (std::logic_error e)
{
std::cerr << e.what() << std::endl;
break;
}
if (current.type == MAPARRAY_DECL)
{
/* parseMapArray(); */
tokenizer.rollBackToken();
std::cerr << "Parsing MapArray" << std::endl;
toml_node *map_node;
toml_node *maparr_node;
s_token current;
try { current = tokenizer.getToken(); }
catch (std::logic_error e)
{
std::cerr << e.what() << std::endl;
break;
}
if (current.type == MAPARRAY_DECL)
{
if (tokenizer.getToken().type != NEWLINE)
throw std::logic_error("no newline after map_array declaration");
map_node = parseMap();
}
else
throw std::logic_error("unexpected token in parseMapArray");
TOMLMap::iterator it;
std::cout << current.value << std::endl;
std::string name = current.value;
std::vector<std::string> full_name;
size_t dot;
while (1)
{
dot = name.find('.');
if (dot == std::string::npos)
break;
/* std::cout << dot << std::endl; */
full_name.push_back(name.substr(0, dot));
name.erase(0, dot + 1);
}
full_name.push_back(name);
for (size_t i = 0; i < full_name.size(); i++)
std::cout << full_name[i] << ", ";
std::cout << std::endl;
/* throw std::logic_error("tha end"); */
TOMLMap *local_root;
std::vector<std::string>::iterator subname = full_name.begin();
local_root = root;
while (1)
{
if (subname + 1 == full_name.end())
{
it = local_root->find(*subname);
if (it == local_root->end())
{
maparr_node = new toml_node;
TOMLMapArray *map_array = new TOMLMapArray;
map_array->push_back(map_node->getMap());
maparr_node->setMapArray(map_array);
(*local_root)[*subname] = maparr_node;
}
else
(it->second)->getMapArray()->push_back(map_node->getMap());
break;
}
else
{
it = local_root->find(*subname);
toml_node *map_node2;
map_node2 = new toml_node;
TOMLMap *map = new TOMLMap;
map_node2->setObject(map);
/* subname not found in local_root */
if (it == local_root->end())
{
(*local_root)[*subname] = map_node2;
local_root = map;
}
/* subname found in local_root */
else
{
local_root = *((it->second)->getMapArray()->end() - 1);
}
}
++subname;
}
}
else
{
std::string key = current.value;
std::cerr << key << std::endl;
if (tokenizer.getToken().type != ASSIGN)
throw std::logic_error("EXPECTED assign!");
current = tokenizer.getToken();
switch (current.type)
{
case STRING:
{
tokenizer.rollBackToken();
(*root)[key] = parseString();
break;
}
case ARR_OPEN:
{
(*root)[key] = parseArray();
break;
}
case NUMBER:
{
tokenizer.rollBackToken();
(*root)[key] = parseNumber();
break;
}
case BOOL:
{
tokenizer.rollBackToken();
(*root)[key] = parseBool();
break;
}
default:
{
/* throw std::logic_error("jopa in parseMap"); */
std::cerr << "Unknown token, marking as complete" << std::endl;
completed = true;
break;
}
}
if (tokenizer.hasMoreTokens())
current = tokenizer.getToken();
else
break;
if (current.type != NEWLINE)
{
throw std::logic_error("EXPECTED newline");
}
}
}
else
{
completed = true;
break;
}
}
return (root);
}
} }
#endif #endif

223
src/config/Tokenizer.cpp Normal file
View File

@@ -0,0 +1,223 @@
#include "Tokenizer.hpp"
/* -Template::-Template(void) */
/* { */
/* return; */
/* } */
/* -Template::-Template(const -Template &src) */
/* { */
/* *this = src; */
/* return; */
/* } */
/* -Template::~-Template(void) */
/* { */
/* return; */
/* } */
/* -Template &-Template::operator=(const -Template &rhs) */
/* { */
/* //code */
/* return (*this); */
/* } */
namespace config
{
bool isspace(char c)
{
if (c == ' ' || c == '\t')
return (true);
else
return (false);
}
bool istomlkey(char c)
{
if (isalnum(c) || c == '-' || c == '_')
return (true);
else
return (false);
}
Tokenizer::Tokenizer(std::string filename)
{
file.open(filename.c_str(), std::ios::in);
if (!file.good())
{
std::cerr << "file didn't open" << std::endl;
}
}
bool Tokenizer::firstToken()
{
// doesn't account for indent!
if (file.tellg() == 0 || file.tellg() == 1 || (last_token == NEWLINE))
return (true);
else
return (false);
}
struct s_token Tokenizer::getToken(void)
{
char c;
struct s_token token;
if (file.eof())
{
std::cout << "Tokens exhausted" << std::endl;
}
prev_pos = file.tellg();
c = getWithoutWhiteSpace();
if (firstToken() && config::istomlkey(c))
{
token.type = KEY;
while (config::istomlkey(c))
{
token.value += c;
file.get(c);
}
}
else if (c == '"')
{
token.type = STRING;
token.value = "";
/* TODO: maybe do-while? */
file.get(c);
while (c != '"')
{
token.value += c;
file.get(c);
}
}
else if (c == '[' && firstToken())
{
/* std::streampos prev_pos = file.tellg(); */
file.get(c);
if (c == '[')
{
token.type = MAPARRAY_DECL;
file.get(c);
while (c != ']')
{
token.value += c;
file.get(c);
}
if (c == ']')
file.get(c);
if (c != ']')
throw std::logic_error("error in MAPARRAY_DECL");
}
else
{
token.type = MAP_DECL;
token.value += c;
file.get(c);
while (c != ']')
{
token.value += c;
file.get(c);
}
if (c != ']')
throw std::logic_error("malformed MAP_DECL");
}
}
else if (c == '[')
{
token.type = OPEN_BRACKET;
}
else if (c == ']')
token.type = CLOSE_BRACKET;
else if (c == '=')
token.type = ASSIGN;
else if (c == '\n')
token.type = NEWLINE;
else if (c == '-' || isdigit(c))
{
std::streampos prevCharPos;
token.type = NUMBER;
token.value = "";
token.value += c;
/* prevCharPos = file.tellg(); */
while (c == '-' || c == '.' || isdigit(c))
{
prevCharPos = file.tellg();
file.get(c);
if (file.eof())
break;
else
{
if (c == '-' || c == '.' || isdigit(c))
token.value += c;
else
file.seekg(prevCharPos);
}
}
}
else if (c == 'f')
{
token.type = BOOL;
token.value = "false";
file.seekg(4, std::ios_base::cur);
/* token.value = ""; */
/* while (std::isalpha(c)) */
/* { */
/* token.value += c; */
/* file.get(c); */
/* } */
std::cerr << "value is: " << token.value << std::endl;
std::cerr << "c is: " << c << std::endl;
}
else if (c == 't')
{
token.type = BOOL;
while (std::isalpha(c))
{
token.value += c;
file.get(c);
}
/* file.seekg(3, std::ios_base::cur); */
}
else if (c == 'n')
{
token.type = NIL;
file.seekg(3, std::ios_base::cur);
}
else if (c == ',')
token.type = COMMA;
last_token = token.type;
return (token);
}
char Tokenizer::getWithoutWhiteSpace(void)
{
char c = ' ';
while (config::isspace(c))
{
file.get(c);
if ((c == ' ') && !file.good())
{
throw std::logic_error("No more tokens!");
}
else if (!file.good())
return (c);
}
return (c);
}
bool Tokenizer::hasMoreTokens(void)
{
return (!file.eof());
}
void Tokenizer::rollBackToken(void)
{
if (file.eof())
file.clear();
file.seekg(prev_pos);
}
void Tokenizer::set_last(e_token type)
{
last_token = type;
}
}

View File

@@ -2,6 +2,8 @@
#define TOKENIZER_HPP #define TOKENIZER_HPP
#include "webserv.hpp" #include "webserv.hpp"
#include "tomlstuff.hpp"
#include <map> #include <map>
#include <vector> #include <vector>
#include <fstream> #include <fstream>
@@ -9,6 +11,7 @@
#include <iostream> #include <iostream>
#include <exception> #include <exception>
namespace config namespace config
{ {
enum e_token enum e_token
@@ -21,10 +24,9 @@ namespace config
COMMA, COMMA,
BOOL, BOOL,
NIL, NIL,
ARR_OPEN, OPEN_BRACKET,
ARR_CLOSE, CLOSE_BRACKET,
MAP_OPEN, MAP_DECL,
MAP_CLOSE,
MAPARRAY_DECL MAPARRAY_DECL
}; };
@@ -35,21 +37,9 @@ namespace config
/* std::string to_string(void); */ /* std::string to_string(void); */
}; };
bool isspace(char c) bool isspace(char c);
{
if (c == ' ' || c == '\t')
return (true);
else
return (false);
}
bool istomlkey(char c) bool istomlkey(char c);
{
if (isalnum(c) || c == '-' || c == '_')
return (true);
else
return (false);
}
class Tokenizer class Tokenizer
{ {
@@ -57,57 +47,18 @@ namespace config
std::fstream file; std::fstream file;
size_t prev_pos; size_t prev_pos;
e_token last_token; e_token last_token;
public: public:
Tokenizer(std::string filename) Tokenizer(std::string filename);
{
file.open(filename.c_str(), std::ios::in);
if (!file.good())
{
std::cerr << "file didn't open" << std::endl;
}
}
char getWithoutWhiteSpace(); char getWithoutWhiteSpace();
struct s_token getToken(); struct s_token getToken();
bool hasMoreTokens(); bool hasMoreTokens();
bool firstToken() bool firstToken();
{
// doesn't account for indent!
if (file.tellg() == 0 || file.tellg() == 1 || (last_token == NEWLINE))
return (true);
else
return (false);
}
void rollBackToken(); void rollBackToken();
void set_last(e_token type);
}; };
char Tokenizer::getWithoutWhiteSpace(void)
{
char c = ' ';
while (config::isspace(c))
{
file.get(c);
if ((c == ' ') && !file.good())
{
throw std::logic_error("No more tokens!");
}
else if (!file.good())
return (c);
}
return (c);
}
bool Tokenizer::hasMoreTokens(void)
{
return (!file.eof());
}
void Tokenizer::rollBackToken(void)
{
if (file.eof())
file.clear();
file.seekg(prev_pos);
}
/* struct s_token Tokenizer::getKey(void) */ /* struct s_token Tokenizer::getKey(void) */
/* { */ /* { */
/* char c; */ /* char c; */
@@ -118,128 +69,6 @@ namespace config
/* } */ /* } */
/* } */ /* } */
struct s_token Tokenizer::getToken(void)
{
char c;
struct s_token token;
if (file.eof())
{
std::cout << "Tokens exhausted" << std::endl;
}
prev_pos = file.tellg();
c = getWithoutWhiteSpace();
if (firstToken() && config::istomlkey(c))
{
token.type = KEY;
while (config::istomlkey(c))
{
token.value += c;
file.get(c);
}
}
else if (c == '"')
{
token.type = STRING;
token.value = "";
/* TODO: maybe do-while? */
file.get(c);
while (c != '"')
{
token.value += c;
file.get(c);
}
}
else if (c == '[')
{
std::streampos prev_pos = file.tellg();
file.get(c);
if (c == '[')
{
token.type = MAPARRAY_DECL;
file.get(c);
while (c != ']')
{
token.value += c;
file.get(c);
}
if (c == ']')
file.get(c);
if (c != ']')
throw std::logic_error("error in MAPARRAY_DECL");
}
else
{
token.type = ARR_OPEN;
file.seekg(prev_pos);
}
}
else if (c == ']')
token.type = ARR_CLOSE;
else if (c == '=')
token.type = ASSIGN;
else if (c == '\n')
token.type = NEWLINE;
else if (c == '-' || isdigit(c))
{
std::streampos prevCharPos;
token.type = NUMBER;
token.value = "";
token.value += c;
/* prevCharPos = file.tellg(); */
while (c == '-' || c == '.' || isdigit(c))
{
prevCharPos = file.tellg();
file.get(c);
if (file.eof())
break;
else
{
if (c == '-' || c == '.' || isdigit(c))
token.value += c;
else
file.seekg(prevCharPos);
}
}
}
else if (c == 'f')
{
token.type = BOOL;
token.value = "false";
file.seekg(4, std::ios_base::cur);
/* token.value = ""; */
/* while (std::isalpha(c)) */
/* { */
/* token.value += c; */
/* file.get(c); */
/* } */
std::cerr << "value is: " << token.value << std::endl;
std::cerr << "c is: " << c << std::endl;
}
else if (c == 't')
{
token.type = BOOL;
while (std::isalpha(c))
{
token.value += c;
file.get(c);
}
/* file.seekg(3, std::ios_base::cur); */
}
else if (c == 'n')
{
token.type = NIL;
file.seekg(3, std::ios_base::cur);
}
else if (c == ',')
token.type = COMMA;
last_token = token.type;
return (token);
}
} }
#endif #endif

View File

@@ -6,10 +6,7 @@
#include <iostream> #include <iostream>
#include <exception> #include <exception>
#include "TOMLNode.hpp" #include "parse.hpp"
#include "TOMLParser.hpp"
#include "Tokenizer.hpp"
/* struct location */ /* struct location */
/* { */ /* { */
/* std::string location; */ /* std::string location; */
@@ -41,10 +38,11 @@ namespace config
} }
} }
void parse(void) TOMLMap *parse(void)
{ {
std::string filename = "config/simple.toml"; std::string filename = "config/simple.toml";
config::TOMLParser parser(filename); config::TOMLParser parser(filename);
TOMLMap *root = parser.parse(); TOMLMap *root = parser.parse();
config::display(root); config::display(root);
return (root);
} }

13
src/config/parse.hpp Normal file
View File

@@ -0,0 +1,13 @@
#ifndef PARSE_HPP
#define PARSE_HPP
#include "tomlstuff.hpp"
#include "Tokenizer.hpp"
#include "TOMLNode.hpp"
#include "TOMLParser.hpp"
TOMLMap *parse(void);
void display(TOMLMap *config);
#endif

11
src/config/tomlstuff.hpp Normal file
View File

@@ -0,0 +1,11 @@
#ifndef TOMLSTUFF_HPP
#define TOMLSTUFF_HPP
class toml_node;
typedef std::map<std::string, toml_node *> TOMLMap; // = JSONObject
typedef std::vector<TOMLMap *> TOMLMapArray;
typedef std::vector<toml_node *> TOMLArray;
#endif

View File

@@ -1,4 +1,6 @@
#include "webserv.hpp" #include "webserv.hpp"
#include "parse.hpp"
#include "Server.hpp"
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
@@ -8,10 +10,8 @@ int main(int argc, char **argv)
Server server; Server server;
server.readConfig(); server.readConfig();
server.setupConfig(); // server.setupConfig();
server.start(); // server.start();
// parse();
return (0); return (0);