forked from amenesca/Weeaboos-WebServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigParser.cpp
More file actions
176 lines (152 loc) · 4.21 KB
/
Copy pathConfigParser.cpp
File metadata and controls
176 lines (152 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "ConfigParser.hpp"
ConfigParser::ConfigParser() {}
ConfigParser::~ConfigParser() {
_configFileFstream.close();
}
ConfigParser::ConfigParser(const ConfigParser& copy) {
*this = copy;
}
ConfigParser& ConfigParser::operator=(const ConfigParser& copy) {
if (this != ©) {
this->_configFilePath = copy._configFilePath;
this->_vServers = copy._vServers;
}
return *this;
}
std::vector<std::string> split(const std::string& input) {
std::vector<std::string> tokens;
std::istringstream iss(input);
std::string token;
while (iss >> token) {
tokens.push_back(token);
}
return tokens;
}
int ConfigParser::openConfig() {
_configFileFstream.open(_configFilePath.c_str(), std::ios::in);
if (!_configFileFstream.is_open()) {
throw OpenError();
}
return (0);
}
void ConfigParser::treatLocation(VirtualServer* currentServer, std::string locationPath)
{
std::string buff;
Location location;
location._locationPath = locationPath;
bool rightBrace = false;
while (std::getline(this->_configFileFstream >> std::ws, buff)) {
if (buff.find("root", 0) != std::string::npos) {
location._root = split(buff)[1];
}
else if (buff.find("cgi_extension", 0) != std::string::npos) {
location._cgi_extension = split(buff)[1];
}
else if (buff.find("upload", 0) != std::string::npos) {
location._upload = split(buff)[1];
}
else if (buff.find("index", 0) != std::string::npos) {
location._index = split(buff);
}
else if (buff.find("autoindex", 0) != std::string::npos) {
location._autoindex = split(buff)[1];
}
else if (buff.find("methods", 0) != std::string::npos) {
location._methods = split(buff);
}
else if (buff.find("return", 0) != std::string::npos) {
location._return = split(buff)[1];
}
else if (buff.find("}", 0) != std::string::npos) {
rightBrace = true;
break;
}
else {
throw InvalidSyntax();
}
}
if (rightBrace == false) {
throw InvalidSyntax();
}
currentServer->getLocationAddress()->push_back(location);
}
void ConfigParser::configServer(VirtualServer* currentServer)
{
std::string buff;
bool rightBrace = false;
while (std::getline(this->_configFileFstream >> std::ws, buff)) {
if (buff == "}") {
rightBrace = true;
break ;
}
else if (buff.find("error_page", 0) != std::string::npos) {
currentServer->setErrorPage(split(buff));
}
else if (buff.find("listen", 0) != std::string::npos || buff.find("port", 0) != std::string::npos) {
currentServer->setPort(strtod(split(buff)[1].c_str(), NULL));
}
else if (buff.find("server_name", 0) != std::string::npos) {
currentServer->setServerName(split(buff)[1]);
}
else if (buff.find("body_size", 0) != std::string::npos) {
currentServer->setBodySize(split(buff)[1]);
}
else if (buff.find("location", 0) != std::string::npos)
{
if (buff.find("{", buff.size() - 1) == std::string::npos || split(buff).size() != 3)
{
throw InvalidSyntax();
}
treatLocation(currentServer, split(buff)[1]);
}
else {
throw InvalidSyntax();
}
}
if (rightBrace == false) {
throw InvalidSyntax();
}
}
void ConfigParser::setVServers()
{
std::string buff;
size_t i = 0;
while(std::getline(this->_configFileFstream >> std::ws, buff)) {
std::istringstream iss(buff);
std::string token;
iss >> token;
if (token == "server" && buff.find("{", buff.size() - 1) != std::string::npos && split(buff).size() == 2)
{
VirtualServer serverInstance;
this->_vServers.push_back(serverInstance);
configServer(&_vServers[i]);
std::cout << "Virtual Server Adicionado" << std::endl;
serverInstance.~VirtualServer();
i++;
}
else
{
throw InvalidSyntax();
}
}
}
int ConfigParser::initConfig() {
openConfig();
setVServers();
return (0);
}
void ConfigParser::setConfigFilePath(std::string configFilePath) {
this->_configFilePath = configFilePath;
}
std::string ConfigParser::getConfigFilePath() const {
return this->_configFilePath;
}
std::vector<VirtualServer> ConfigParser::getVServers() const {
return (_vServers);
}
const char *ConfigParser::OpenError::what() const throw() {
return ("WebServer: error: can't open configuration file.");
}
const char *ConfigParser::InvalidSyntax::what() const throw() {
return ("WebServer: error: invalid syntax.");
}