-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocation.cpp
More file actions
391 lines (349 loc) · 8.96 KB
/
Copy pathLocation.cpp
File metadata and controls
391 lines (349 loc) · 8.96 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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#include "Location.hpp"
#include "Debug.hpp"
Location::Location(void): _get(true),
_post(true),
_delete(true),
_root(""),
_index(""),
_return(0),
_autoindex(false),
_c_body_limit(1),
_upload_path(""),
_cgi_path("")
//_root(DEFAULT_ROOT), _index(DEFAULT_INDEX)
{
for (int i = 0; i < LOC_VAL_COUNT; i++)
dir_count[i] = false;
// check if root exists?
// if root/index.html exists add to index else throw exception? find another index?
// std::cout << "Location default constructor called." << std::endl;
}
Location::Location(std::string root, std::string index):_get(true),
_post(true),
_delete(true),
_root(root),
_index(index),
_return(0),
_autoindex(false),
_c_body_limit(1),
_upload_path(""),
_cgi_path("")
{
for (int i = 0; i < LOC_VAL_COUNT; i++)
dir_count[i] = false;
// std::cout << "Location constructor called." << std::endl;
}
/*
Copy constructor
usage: for server level error pages
*/
Location::Location(const Location &rhs)
{
// std::cout << "Location: Copy Constructor called" << std::endl;
for (int i = 0; i < LOC_VAL_COUNT; i++)
dir_count[i] = rhs.dir_count[i];
_get = rhs.get_allowed();
_post = rhs.post_allowed();
_delete = rhs.delete_allowed();
_root = rhs.get_root();
_index = rhs.get_index();
if (!_error_pages.empty())
_error_pages.clear();
_error_pages = rhs.get_all_error_pages();
_return = rhs.get_return_type();
std::pair<int, std::string> ret = rhs.get_return();
if (ret.first != -1)
{
_return_code = ret.first;
_return_url = ret.second;
}
_autoindex = rhs.is_autoindex();
_c_body_limit = rhs.get_c_body_limit();
_upload_path = rhs.get_upload_path();
_cgi_path = rhs.get_cgi_path();
}
/* Copy assignment operator overload */
Location& Location::operator=(const Location &rhs)
{
// std::cout << "Location: Copy Assignment Operator called" << std::endl;
for (int i = 0; i < LOC_VAL_COUNT; i++)
dir_count[i] = rhs.dir_count[i];
_get = rhs.get_allowed();
_post = rhs.post_allowed();
_delete = rhs.delete_allowed();
_root = rhs.get_root();
_index = rhs.get_index();
if (!_error_pages.empty())
_error_pages.clear();
_error_pages = rhs.get_all_error_pages();
_return = rhs.get_return_type();
std::pair<int, std::string> ret = rhs.get_return();
if (ret.first != -1)
{
_return_code = ret.first;
_return_url = ret.second;
}
_autoindex = rhs.is_autoindex();
_c_body_limit = rhs.get_c_body_limit();
_upload_path = rhs.get_upload_path();
_cgi_path = rhs.get_cgi_path();
return (*this);
}
std::string Location::get_upload_path(void) const
{
return (_upload_path);
}
int Location::get_c_body_limit(void) const
{
return (_c_body_limit);
}
void Location::set_get(bool permission)
{
_get = permission;
}
void Location::set_post(bool permission)
{
_post = permission;
}
void Location::set_delete(bool permission)
{
_delete = permission;
}
bool Location::set_limit_except(bool get, bool post, bool del)
{
if (dir_count[LIMIT_EXCEPT] == true)
return (false);
dir_count[LIMIT_EXCEPT] = true;
this->set_get(get);
this->set_post(post);
this->set_delete(del);
return (true);
}
bool Location::set_upload_path(std::string upload_path)
{
if (dir_count[UPLOAD_PATH] == true)
return (false);
dir_count[UPLOAD_PATH] = true;
_upload_path = upload_path;
return (true);
}
bool Location::set_cgi_path(std::string cgi_path)
{
if (dir_count[CGI] == true)
return (false);
dir_count[CGI] = true;
_cgi_path = cgi_path;
return (true);
}
bool Location::is_cgi(void) const
{
if (dir_count[CGI] == true)
return (true);
return (false);
}
std::string Location::get_cgi_path(void) const
{
return (_cgi_path);
}
bool Location::set_root(std::string root)
{
// check if root exists?
if (dir_count[ROOT] == true)
return (false);
dir_count[ROOT] = true;
_root = root;
return (true);
}
int Location::get_return_type(void) const
{
return (_return);
}
int Location::set_return(int code, std::string url)
{
if (dir_count[RETURN] == true)
return (-1);
dir_count[RETURN] = true;
_return_url = url;
_return_code = code;
if (url[0] == std::string("\\")[0])
_return = 2;
else
_return = 1;
return (_return);
}
bool Location::is_return(void) const
{
if (_return == 1 || _return == 2)
return (true);
return (false);
}
std::pair<int, std::string> Location::get_return(void) const
{
std::pair<int, std::string> ret;
if (this->is_return())
ret = std::pair<int, std::string>(_return_code, _return_url);
else
ret = std::pair<int, std::string>(-1, "");
return (ret);
}
/*
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 Location::set_error_page(std::string key, std::string value)
{
std::pair<std::map<std::string, std::string>::iterator, bool> ret;
ret = _error_pages.insert(std::pair<std::string,std::string>(key, value));
if (ret.second == true)
return (true);
else if (ret.first != _error_pages.end())
{
_error_pages.erase(ret.first);
ret = _error_pages.insert(std::pair<std::string,std::string>(key, value));
if (ret.second == true)
return (true);
else
return (false);
}
return (false);
}
bool Location::set_index(std::string index)
{
// if index file exists in root or given location?
if (dir_count[INDEX] == true)
return (false);
dir_count[INDEX] = true;
_index = index;
return (true);
}
bool Location::set_c_body_limit(int limit)
{
if (dir_count[C_BODY_LIMIT] == true)
return (false);
else
dir_count[C_BODY_LIMIT] = true;
if (limit >= 0 && limit <= INT_MAX)
_c_body_limit = limit;
else
return (false);
return (true);
}
bool Location::set_autoindex(bool val)
{
if (dir_count[AUTOINDEX] == true)
return (false);
dir_count[AUTOINDEX] = true;
_autoindex = val;
return (true);
}
bool Location::is_autoindex(void) const
{
if (_autoindex)
return (true);
return (false);
}
bool Location::get_allowed(void) const
{
if (_get)
return (true);
return (false);
}
bool Location::post_allowed(void) const
{
if (_post)
return (true);
return (false);
}
bool Location::delete_allowed(void) const
{
if (_delete)
return (true);
return (false);
}
std::string Location::get_root(void) const
{
return (_root);
}
std::string Location::get_index(void) const
{
return (_index);
}
/*
Fist digit match of key
*/
std::pair<std::map<std::string, std::string>::iterator, bool> find_partial(std::map<std::string, std::string> pages, std::string key)
{
// std::string gen_code(key[0] + std::string("xx"))
for (std::map<std::string,std::string>::iterator it = pages.begin();
it != pages.end();
it++)
{
if (it->first[0] == key[0])
return (std::pair<std::map<std::string, std::string>::iterator, bool>(it, true));
}
return (std::pair<std::map<std::string, std::string>::iterator, bool>(pages.end(), false));
}
/*
If error code match cannot be found : Look for partial match
If error code partial match cannot be found:
return : std::pair<map.end(),false>
If error code partial match can be found:
return : std::pair<it,true>
If error code match can be found
return : std::pair<it,true>
*/
std::pair<std::map<std::string, std::string>::iterator, bool> Location::get_error_page(std::string err_no)
{
std::map<std::string,std::string>::iterator it = _error_pages.find(err_no);
if (it == _error_pages.end())
{
std::pair<std::map<std::string, std::string>::iterator, bool> ret = find_partial(_error_pages, err_no);
return (ret);
}
return (std::pair<std::map<std::string, std::string>::iterator, bool>(it,true));
}
std::map<std::string, std::string> Location::get_all_error_pages(void) const
{
return (_error_pages);
// std::map<std::string, std::string> ret;
// for (std::map<std::string, std::string>::const_iterator it = _error_pages.begin(); it != _error_pages.end(); it++)
// {
// ret.insert(std::pair<std::string, std::string>(it->first, it->second));
// }
// return (ret);
}
std::map<std::string, std::string> &Location::UNIT_get_error_pages(void)
{
return (_error_pages);
}
std::ostream& operator<<(std::ostream& streamRef, const Location& loc)
{
streamRef << " root: " << loc.get_root();
streamRef << " index: " << loc.get_index();
streamRef << " autoindex " << loc.is_autoindex();
streamRef << " c_body_limit " << loc.get_c_body_limit();
streamRef << " upload_path " << loc.get_upload_path() << std::endl;
streamRef << " permissions: (Get Post Delete): ";
if (loc.get_allowed())
streamRef << "1";
else
streamRef << "0";
if (loc.post_allowed())
streamRef << "1";
else
streamRef << "0";
if (loc.delete_allowed())
streamRef << "1";
else
streamRef << "0";
streamRef << std::endl;
streamRef << "return: " << loc.get_return_type() << " code: " << loc.get_return().first;
streamRef << " url: " << loc.get_return().second << std::endl;
streamRef << "Error pages: " << std::endl;
std::map<std::string, std::string> ret = loc.get_all_error_pages();
for (std::map<std::string, std::string>::iterator i = ret.begin(); i!= ret.end(); i++)
{
streamRef << i->first << " " << i->second << std::endl;
}
return (streamRef);
}