forked from tgrasset/webserv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConfParser.cpp
319 lines (288 loc) · 11.5 KB
/
ConfParser.cpp
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ConfParser.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tgrasset <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/12 15:35:55 by mbocquel #+# #+# */
/* Updated: 2023/09/12 10:10:38 by tgrasset ### ########.fr */
/* */
/* ************************************************************************** */
#include "ConfParser.hpp"
bool ConfParser::_verbose = false;
/* ************************************************************************** */
/* Constructeurs et destructeurs */
/* ************************************************************************** */
ConfParser::ConfParser(std::string path) : _path(path)
{
if (ConfParser::_verbose)
std::cout << "ConfParser default constructor called" << std::endl;
_serverNb = 0;
}
ConfParser::ConfParser(ConfParser const &src)
{
if (ConfParser::_verbose)
std::cout << "ConfParser copy constructor called" << std::endl;
if (this != &src)
*this = src;
}
ConfParser::~ConfParser()
{
if (ConfParser::_verbose)
std::cout << "ConfParser destructor called" << std::endl;
}
/* ************************************************************************** */
/* Surcharge d'operateur */
/* ************************************************************************** */
ConfParser &ConfParser::operator=(ConfParser const &rhs)
{
if (this != &rhs)
{
_path = rhs.getPath();
_servers = rhs.getServers();
_serverConf = rhs.getServerConf();
_serverNb = rhs.getServerNb();
}
return (*this);
}
/* ************************************************************************** */
/* Methodes */
/* ************************************************************************** */
std::string ConfParser::getPath() const
{
return (_path);
}
std::vector<Server> ConfParser::getServers() const
{
return (_servers);
}
std::vector<std::string> ConfParser::getServerConf() const
{
return (_serverConf);
}
int ConfParser::getServerNb() const
{
return (_serverNb);
}
std::string ConfParser::extractContent(std::string const &path)
{
struct stat buf;
int ret = stat(path.c_str(), &buf);
std::ifstream fileStream;
if (ret < 0 || !S_ISREG(buf.st_mode))
throw ConfParserException("Invalid path to configuration file");
if (access(path.c_str(), R_OK) != 0)
throw ConfParserException("No reading rights on configuration file");
fileStream.open(path.c_str());
if (fileStream.fail())
throw ConfParserException("Configuration file could not be opened");
std::stringstream result;
result << fileStream.rdbuf();
fileStream.close();
return (result.str());
}
void ConfParser::cleanComments(std::string &content) const {
size_t sharp = content.find('#');
while (sharp != std::string::npos)
{
size_t nl;
nl = content.find('\n', sharp);
content.erase(sharp, nl - sharp);
sharp = content.find('#');
}
}
size_t ConfParser::getBlockStart(size_t blockStart, std::string &content) const {
size_t i;
for (i = blockStart; content[i] != '\0'; i++)
{
if (std::isspace(content[i]))
continue ;
else if (content[i] == 's')
break;
else
throw ConfParserException("Unknown directive or unexpected symbol in main context");
}
if (content[i] == '\0')
return (blockStart);
if (content.compare(i, 6, "server") != 0)
throw ConfParserException("Unknown directive or unexpected symbol in main context");
i += 6;
while (content[i] != '\0' && std::isspace(content[i]))
i++;
if (content[i] == '{')
return (i);
throw ConfParserException("Expected '{' symbol after server directive");
}
size_t ConfParser::getBlockEnd(size_t blockStart, std::string &content) const {
size_t i;
int subContext = 0;
for (i = blockStart + 1; content[i] != '\0'; i++)
{
if (content[i] == '{')
subContext++;
else if (content[i] == '}' && subContext > 0)
subContext--;
else if (content[i] == '}' && subContext == 0)
return (i);
}
return (blockStart);
}
void ConfParser::splitServerBlocks(std::string &content) {
size_t blockStart = 0;
size_t blockEnd = 1;
size_t i = 0;
if (content.find("server") == std::string::npos)
throw ConfParserException("No server block found in configuration file");
while (blockStart != blockEnd && blockStart < content.length())
{
blockStart = getBlockStart(blockStart, content);
blockEnd = getBlockEnd(blockStart, content);
if (blockStart == blockEnd)
{
for (i = blockStart; content[i] != '\0'; i++)
{
if (std::isspace(content[i]) == 0) //en cas de whitespaces en fin de fichier
throw ConfParserException("Something seems wrong with curved brackets scopes");
}
break ;
}
_serverConf.push_back(content.substr(blockStart, blockEnd - blockStart + 1));
_serverNb++;
blockStart = blockEnd + 1;
}
}
void ConfParser::configurateServer(Server &server, std::string &config) const {
std::vector<std::string> params = cpp_split(config, " \n\t");
std::vector<std::string> errorPages;
int size = params.size();
if (size < 3)
throw ConfParserException("Server context can't be left empty");
bool inLocations = false;
for (int i = 0; i < size; i++)
{
if (params[i].compare("listen") == 0 && i + 1 < size && inLocations == false)
{
if (server.getPort() != 0)
throw ConfParserException("Each 'server' context can't have more than one 'listen' directive");
server.setPort(params[++i]);
}
else if (params[i] == "server_name" && i + 1 < size && inLocations == false)
{
if (server.getServerName() != "")
throw ConfParserException("Each 'server' context can't have more than one 'server_name' directive");
server.setServerName(params[++i]);
}
else if (params[i] == "host" && i + 1 < size && inLocations == false)
{
if (server.getHost() != 0)
throw ConfParserException("Each 'server' context can't have more than one 'host' directive");
server.setHost(params[++i]);
}
else if (params[i] == "root" && i + 1 < size && inLocations == false)
{
if (server.getRoot() != "")
throw ConfParserException("Each 'server' context can't have more than one 'root' directive");
server.setRoot(params[++i]);
}
else if (params[i] == "client_max_body_size" && i + 1 < size && inLocations == false)
{
if (server.getClientMaxBodySize() != 0)
throw ConfParserException("Each 'server' context can't have more than one 'client_max_body_size' directive");
server.setClientMaxBodySize(params[++i]);
}
else if (params[i] == "index" && i + 1 < size && inLocations == false)
{
if (server.getIndex() != "")
throw ConfParserException("Each 'server' context can't have more than one 'index' directive");
server.setIndex(params[++i]);
}
else if (params[i] == "error_page" && i + 1 < size && inLocations == false)
{
i++;
while (i < size)
{
errorPages.push_back(params[i]);
if (params[i].find(';') != std::string::npos)
break ;
i++;
}
if (i == size)
throw ConfParserException("Missing ';' symbol after error_page line");
}
else if (params[i] == "location" && i + 1 < size)
{
if (server.getRoot() == "")
throw ConfParserException("Please define server 'root' before 'location' blocks");
std::string path;
std::vector<std::string> content;
i++;
if (params[i] == "{" || params[i] == "}")
throw ConfParserException("Missing path after 'location' directive");
path = params[i];
i++;
if (params[i] != "{")
throw ConfParserException("Missing '{' symbol after 'location' directive");
i++;
for (; i < size && params[i] != "}"; i++)
content.push_back(params[i]);
if (i == size)
throw ConfParserException("Missing '}' symbol at the end of 'location' block");
server.setLocation(path, content);
inLocations = true;
}
else if (params[i] == "timeout" && i + 1 < size && inLocations == false)
{
if (server.getTimeout() != -1)
throw ConfParserException("Each 'server' context can't have more than one 'timeout' directive");
server.setTimeout(params[++i]);
}
else if (params[i] != "{" && params[i] != "}")
{
if (inLocations == true)
throw ConfParserException("'location' blocks must be at the end of 'server' context");
else
throw ConfParserException("Unknown or incomplete directive : " + params[i]);
}
}
server.setErrorPages(errorPages);
server.checkDoubleLocations();
}
void ConfParser::checkServerConfig(Server &server) const {
if (server.getRoot() == "")
throw ConfParserException("Please define 'root' in all 'server' contexts");
if (server.getHost() == 0)
server.setHost("localhost;");
if (server.getIndex() == "")
throw ConfParserException("Server must have an index");
if (server.getPort() == 0)
server.setPort("80;");
if (checkFile(server.getIndex(), server.getRoot()) == false)
throw ConfParserException("Index file doesn't exist or couldn't be read");
if (server.getTimeout() < 0)
server.setTimeout(MAX_TIME_CLIENT_S);
}
void ConfParser::parse() {
std::string content = extractContent(_path);
int i, j;
if (content.empty() == true)
throw ConfParserException("Configuration file is empty");
cleanComments(content);
splitServerBlocks(content);
for (i = 0; i < _serverNb; i++)
{
Server newServer;
configurateServer(newServer, _serverConf[i]);
checkServerConfig(newServer);
_servers.push_back(newServer);
}
for (i = 0; i < _serverNb - 1; i++)
{
for (j = i + 1; j < _serverNb; j++)
{
if (_servers[i].getPort() == _servers[j].getPort() && _servers[i].getHost() == _servers[j].getHost() && _servers[i].getServerName() == _servers[j].getServerName() && _servers[i].getServerName() != "")
throw ConfParserException("Two server blocks can't share same port, host and server name");
}
}
std::cout << TXT_GREEN << getTimestamp() << " Configuration file parsed successfully" << TXT_END << std::endl;
}