-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig.cpp
More file actions
292 lines (253 loc) · 7.55 KB
/
Copy pathConfig.cpp
File metadata and controls
292 lines (253 loc) · 7.55 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
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
#include "Config.hpp"
Config::Config(void):temp_index(""), temp_root(""),
_port(DEFAULT_PORT),
_c_body_limit(1),
_ip(DEFAULT_IP),
_server_name(DEFAULT_SERVERNAME)
{
for (int i = 0; i < SERV_VAL_COUNT; i++)
dir_count[i] = false;
// std::cout << "Config constructor called" << std::endl;
}
Config::Config(const Config& other)
: temp_index(other.temp_index),
temp_root(other.temp_root),
_port(other._port),
_c_body_limit(other._c_body_limit),
_ip(other._ip),
_server_name(other._server_name)
{
// std::cout << "Config: Copy Constructor called" << std::endl;
for (int i = 0; i < SERV_VAL_COUNT; i++)
dir_count[i] = other.dir_count[i];
// Deep copy the error pages map
if (!temp_error_pages.empty())
temp_error_pages.clear();
temp_error_pages = other.temp_error_pages;
// //Create clean copies of all Location objects
// std::map<std::string, Location> sourceLocations = other.get_all_Locations();
// clear it just to be safe
if (!_locations.empty())
_locations.clear();
_locations = other.get_all_Locations();
// // Create fresh copies of all locations using the copy constructor
// for (std::map<std::string, Location>::const_iterator it = sourceLocations.begin();
// it != sourceLocations.end(); ++it) {
// Location locCopy(it->second);
// // Insert directly
// _locations[it->first] = locCopy;
// }
}
Config& Config::operator=(const Config& other) {
// std::cout << "Config: Copy Assignment Operator called" << std::endl;
// Defensive self-assignment check
if (this != &other) {
// Explicit deep copy of ALL members
_port = other._port;
_c_body_limit = other._c_body_limit;
_ip = other._ip;
_server_name = other._server_name;
// Deep copy temporary elements
temp_index = other.temp_index;
temp_root = other.temp_root;
for (int i = 0; i < SERV_VAL_COUNT; i++)
dir_count[i] = other.dir_count[i];
// Deep copy the error pages map
if (!temp_error_pages.empty())
temp_error_pages.clear();
temp_error_pages = other.temp_error_pages;
// // Deep copy error pages
// temp_error_pages.clear();
// for (std::map<std::string, std::string>::const_iterator it =
// other.temp_error_pages.begin();
// it != other.temp_error_pages.end(); ++it) {
// temp_error_pages[it->first] = it->second;
// }
// clear it just to be safe
//_locations.clear();
// // Deep copy locations
// const std::map<std::string, Location>& srcLocations = other.get_all_Locations();
// for (std::map<std::string, Location>::const_iterator it = srcLocations.begin();
// it != srcLocations.end(); ++it) {
// // Explicit deep copy of each location
// Location locCopy(it->second);
// _locations[it->first] = locCopy;
// }
// clear it just to be safe
if (!_locations.empty())
_locations.clear();
_locations = other.get_all_Locations();
}
return *this;
}
bool is_ipv4(std::string ip)
{
std::stringstream ss(ip);
std::string block;
int blocks = 0;
while(std::getline(ss,block,'.'))
{
if (block.empty() || std::atoi(block.c_str()) < 0 || std::atoi(block.c_str()) > 255)
return (false);
blocks++;
}
if (blocks != 4)
return (false);
return (true);
}
//LISTEN
void Config::set_port(int port)
{
// min max values?
if (dir_count[LISTEN] == true)
return;
_port = port;
dir_count[LISTEN] = true;
}
//HOST
bool Config::set_ip(std::string ip)
{
if (dir_count[HOST] == true)
return (false);
if (ip == std::string("localhost"))
_ip = DEFAULT_IP;
else if (ip.empty())
return (false);
else if (!is_ipv4(ip))
return (false);
else
_ip = ip;
dir_count[HOST] = true;
return (true);
}
//C_BODY_LIM
bool Config::set_c_body_limit(int limit)
{
//min max values
if (dir_count[C_BODY_LIM] == true)
return (false);
if (limit >= 0 && limit <= INT_MAX)
_c_body_limit = limit;
else
return (false);
dir_count[C_BODY_LIM] = true;
return (true);
}
//SERVER_NAME
void Config::set_server_name(std::string server_name)
{
if (dir_count[SERVER_NAME] == true)
return;
_server_name = server_name;
dir_count[SERVER_NAME] = true;
}
std::map<std::string, Location> &Config::UNIT_get_locations(void)
{
return (_locations);
}
bool Config::add_location(std::string path, Location loc)
{
std::pair< std::map<std::string, Location>::iterator, bool > ret;
// if path is not already in _locations
// Assume Location loc was able to initialize because everything in the location exists and works
std::map<std::string, Location>::iterator it = _locations.find(path);
if (it == _locations.end())
{
ret = _locations.insert(std::pair<std::string, Location>(path, loc));
if (ret.second)
return (true);
else
std::cerr << "_locations: map insertion error" << std::endl;
}
std::cout << path << " already exists" << std::endl;
return (false);
}
int Config::get_port(void) const
{
return (_port);
}
int Config::get_c_body_limit(void) const
{
return (_c_body_limit);
}
std::string Config::get_ip(void) const
{
return (_ip);
}
std::string Config::get_server_name(void) const
{
return (_server_name);
}
bool Config::location_exists(std::string path)
{
std::map<std::string, Location>::iterator it = _locations.find(path);
if (it != _locations.end())
return (true);
// std::cout << "not end" << std::endl;
return (false);
}
/*
Always use find_location() if location exists == true ONLY
*/
Location Config::find_location(std::string path)
{
std::map<std::string, Location>::iterator it = _locations.find(path);
// +++++++ add debugging and a proper alternative here ++++++++++++
// if (it == _locations.end()) {
// debug("[find_location]: No such location");
// return _locations[0];
// }
return (it->second);
}
std::map<std::string, Location> Config::get_all_Locations(void) const
{
return (_locations);
}
bool Config::update_location(Location loc, std::string path, std::string new_path)
{
if (!location_exists(path))
{
std::cout << path << "is not in map, add location instead.\n";
return (false);
}
std::pair< std::map<std::string, Location>::iterator, bool > ret;
if (_locations.erase(path) != 1)
return (false);
ret = _locations.insert(std::pair<std::string, Location>(new_path, loc));
return (ret.second);
}
/*
If error page does not exist, the page is aded to map as key, value pair
If error page already exists, old page is removed and new one added
*/
bool Config::set_temp_error_page(std::string key, std::string value)
{
std::pair<std::map<std::string, std::string>::iterator, bool> ret;
ret = temp_error_pages.insert(std::pair<std::string,std::string>(key, value));
if (ret.second == true)
return (true);
else if (ret.first != temp_error_pages.end())
{
temp_error_pages.erase(ret.first);
ret = temp_error_pages.insert(std::pair<std::string,std::string>(key, value));
if (ret.second == true)
return (true);
else
return (false);
}
return (false);
}
std::ostream& operator<<(std::ostream& streamRef, const Config& conf)
{
streamRef << "host: " << conf.get_ip() << "port: " << conf.get_port() << "\tserver_name: " << conf.get_server_name();
streamRef << "\ttemp root: " << conf.temp_root << "\ttemp index: " << conf.temp_index << std::endl;
int i = 1;
std::map<std::string, Location> loc = conf.get_all_Locations();
std::map<std::string, Location>::iterator it;
for (it = loc.begin(); it != loc.end(); it++)
{
streamRef << "Loc "<< i << ": " << it->first << it->second << std::endl;
i++;
}
return (streamRef);
}