forked from eel-moun/HTTP-WebServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
executable file
·87 lines (76 loc) · 2.18 KB
/
main.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
#include "./headers/parseConfig.hpp"
#include "headers/ServerUtils.hpp"
void checkDefault(ConfigFile& config)
{
int count = 0;
for (size_t i = 0; i < config.getSize(); i++)
{
for (size_t j = 0; j < config.getServer(i)->getSize(); j++)
{
if (!config.getServer(i)->getLocation(j)->getPath().compare("/"))
count++;
}
if (count != 1)
throw invalid_argument("config file syntax error in default location");
if (!config.getServer(i)->getValue("root").size())
throw invalid_argument("config file syntax error no root");
if (!config.getServer(i)->getValue("default_error").size())
throw invalid_argument("config file syntax error no default_error");
if (!config.getServer(i)->getValue("host").size())
throw invalid_argument("config file syntax error no host");
if (!config.getServer(i)->get_listens().size())
throw invalid_argument("config file syntax error no port to listen");
count = 0;
}
}
void checkConfigFile(int ac, char **av, ConfigFile& config)
{
ifstream &conf_file = openFileStream(ac ,av);
parseConfig(conf_file, config);
checkDefault(config);
delete &conf_file;
}
void parseConfig(ifstream& conf_file, ConfigFile& config)
{
string line;
while (getline(conf_file, line, '\n') && line.size() == 0)
continue;
if (!line.size())
return ;
if (line.substr(0, 7) != "server " || line.size() != 8)
throw invalid_argument("config file syntax error");
keyValue("server", config);
while (getline(conf_file, line, '\n'))
{
rmSpaces(line);
if (line.substr(0, 10) == "location {")
{
if (line.size() != 10)
throw invalid_argument("config file syntax error");
keyValue("location", config);
while (getline(conf_file, line, '\n'))
{
rmSpaces(line);
if (line.substr(0, 1) == "}" && line.size() == 1)
break;
keyValue(line, config);
}
continue;
}
else if (line.substr(0, 8) == "location")
throw invalid_argument("config file syntax error");
if (line.substr(0, 1) == "}" && line.size() == 1)
{
parseConfig(conf_file, config);
return ;
}
keyValue(line, config);
}
}
int main(int ac, char **av)
{
ConfigFile config;
checkConfigFile(ac, av, config);
config.run_servers();
return (0);
}