forked from two-three-four-five/webserv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerConfig.cpp
More file actions
149 lines (121 loc) · 4.45 KB
/
ServerConfig.cpp
File metadata and controls
149 lines (121 loc) · 4.45 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
#include "Config/ServerConfig.hpp"
#include "Config/ConfigException.hpp"
using namespace Hafserv;
ServerConfig::ServerConfig() : AConfig(), AHttpConfigCore(), names(), ports(), locations(3) { ports.insert(80); }
ServerConfig::ServerConfig(const ServerConfig &other)
: AConfig(other), AHttpConfigCore(other), names(other.names), ports(other.ports), locations(other.locations)
{
}
ServerConfig::ServerConfig(const ConfigFile &block, const AHttpConfigCore &core) throw(std::logic_error)
: AConfig(), AHttpConfigCore(core), names(), ports(), locations(3)
{
ports.insert(80);
setHttpConfigCore(block.getDirectives());
setHttpConfigCore(block.getSubBlocks());
std::vector<std::string> params;
size_t numToken;
std::pair<unsigned short, bool> valueToUShort;
ConfigFile::directives_t::const_iterator it = block.getDirectives().begin();
for (; it != block.getDirectives().end(); it++)
{
const std::string &key = (*it).first;
const std::string &value = (*it).second;
params = util::string::split(value, ' ');
numToken = params.size();
valueToUShort = util::string::stous(value);
if (allBlockDirectives.count(key))
throw NoBraceError(key);
else if (!allSimpleDirectives.count(key))
throw UnknownDirectiveError(key);
else if (!serverSimpleDirectives.count(key))
throw DisallowDirectiveError(key);
if (key == "listen")
{
if (numToken != 1)
throw InvalidNumberArgumentError(key);
else if (!valueToUShort.second)
throw InvalidArgumentError(key, value);
if (ports.size() == 1 && *ports.begin() == 80)
ports.clear();
if (!ports.insert(util::string::stous(value).first).second)
throw DuplicateListenError(params[0]);
}
else if (key == "server_name")
{
for (size_t i = 0; i < params.size(); i++)
names.insert(params[i]);
}
}
for (size_t i = 0; i < block.getSubBlocks().size(); i++)
{
const ConfigFile &subBlock = block.getSubBlocks().at(i);
const std::string &subBlockName = subBlock.getBlockDirective();
if (allSimpleDirectives.count(subBlockName))
throw NoSemicolonError(subBlockName);
else if (!allBlockDirectives.count(subBlockName))
throw UnknownDirectiveError(subBlockName);
else if (!serverBlockDirectives.count(subBlockName))
throw DisallowDirectiveError(subBlockName);
if (subBlockName == "location")
{
LocationConfig conf = LocationConfig(block.getSubBlocks().at(i), *this);
size_t mIdx = 0;
if (conf.getModifier() == "=")
mIdx = 0;
else if (conf.getModifier() == "$")
mIdx = 1;
else if (conf.getModifier() == "^")
mIdx = 2;
for (size_t j = 0; j < locations[mIdx].size(); ++j)
if (locations[mIdx][j].getPattern() == conf.getPattern())
throw DuplicateLocationError(conf.getPattern());
locations[mIdx].push_back(conf);
}
}
bool hasDefault = false;
for (size_t i = 0; i < locations[2].size(); ++i)
if (locations[2][i].getPattern() == "/")
hasDefault = true;
if (!hasDefault)
{
LocationConfig defaultLocationConfig(*this);
defaultLocationConfig.setModifier("^");
defaultLocationConfig.setPattern("/");
locations[2].push_back(defaultLocationConfig);
}
}
ServerConfig &ServerConfig::operator=(const ServerConfig &other)
{
if (this != &other)
{
AConfig::operator=(other);
AHttpConfigCore::operator=(other);
names = other.names;
ports = other.ports;
locations = other.locations;
}
return *this;
}
ServerConfig::~ServerConfig() {}
const std::set<std::string> &ServerConfig::getNames() const { return names; }
const std::set<unsigned short> &ServerConfig::getPorts() const { return ports; }
const std::vector<std::vector<LocationConfig> > &ServerConfig::getLocations() const { return locations; }
bool ServerConfig::hasName(const std::string &name) const { return names.count(name) > 0; }
bool ServerConfig::hasPort(const unsigned short port) const { return ports.count(port) > 0; }
std::ostream &operator<<(std::ostream &os, const ServerConfig &conf)
{
os << "[ServerConfig]" << std::endl;
os << AHttpConfigCore(conf);
os << "\tnames: ";
for (std::set<std::string>::const_iterator it = conf.getNames().begin(); it != conf.getNames().end(); ++it)
os << *it << " ";
os << std::endl;
os << "\tports: ";
for (std::set<unsigned short>::const_iterator it = conf.getPorts().begin(); it != conf.getPorts().end(); ++it)
os << static_cast<int>(*it) << " ";
os << std::endl;
for (size_t i = 0; i < conf.getLocations().size(); i++)
for (size_t j = 0; j < conf.getLocations()[i].size(); j++)
os << conf.getLocations()[i][j] << std::endl;
return os;
}