forked from tgrasset/webserv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.cpp
384 lines (333 loc) · 11.4 KB
/
Server.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
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Server.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tgrasset <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/12 15:48:32 by mbocquel #+# #+# */
/* Updated: 2023/09/12 10:03:29 by tgrasset ### ########.fr */
/* */
/* ************************************************************************** */
#include "Server.hpp"
bool Server::_verbose = false;
/* ************************************************************************** */
/* Constructeurs et destructeurs */
/* ************************************************************************** */
Server::Server(void)
{
if (Server::_verbose)
std::cout << "Server default constructor called" << std::endl;
_port = 0;
_host = 0;
_serverName = "";
_root = "";
_clientMaxBodySize = 0;
_index = "";
_errorPages.clear();
_listenSocket = 0;
_timeout = -1;
}
Server::Server(Server const &src)
{
if (Server::_verbose)
std::cout << "Server copy constructor called" << std::endl;
if (this != &src)
*this = src;
}
Server::~Server()
{
if (Server::_verbose)
std::cout << "Server destructor called" << std::endl;
}
/* ************************************************************************** */
/* Surcharge d'operateur */
/* ************************************************************************** */
Server &Server::operator=(Server const &rhs)
{
if (this != &rhs)
{
_port = rhs.getPort();
_host = rhs.getHost();
_servaddr = rhs.getServerAddr();
_serverName = rhs.getServerName();
_root = rhs.getRoot();
_clientMaxBodySize = rhs.getClientMaxBodySize();
_index = rhs.getIndex();
_errorPages = rhs.getErrorPages();
_locations = rhs.getLocations();
_listenSocket = rhs.getListenSocket();
_timeout = rhs.getTimeout();
}
return (*this);
}
/* ************************************************************************** */
/* Methodes */
/* ************************************************************************** */
in_port_t Server::getPort(void) const {
return (_port);
}
in_addr_t Server::getHost(void) const {
return (_host);
}
std::string Server::getServerName(void) const {
return (_serverName);
}
std::string Server::getRoot(void) const {
return (_root);
}
unsigned long Server::getClientMaxBodySize(void) const {
return (_clientMaxBodySize);
}
std::string Server::getIndex(void) const {
return (_index);
}
std::map<int, std::string> Server::getErrorPages(void) const {
return (_errorPages);
}
std::vector<Location> Server::getLocations(void) const {
return (_locations);
}
int Server::getListenSocket(void) const {
return (_listenSocket);
}
struct sockaddr_in Server::getServerAddr(void) const {
return (_servaddr);
}
int Server::getTimeout(void) const {
return (_timeout);
}
void Server::setPort(std::string port) {
if (isValidConfValue(port) == false)
throw ServerException("';' symbol needed after 'listen' line");
int n = stringToInt(port);
if (n < 1 || n > 65535)
throw ServerException("Invalid port value in 'listen' line");
u_int16_t nShort = n;
_port = htons(nShort);
}
void Server::setListenSocket(int listen_socket)
{
_listenSocket = listen_socket;
}
void Server::setHost(std::string host) {
if (isValidConfValue(host) == false)
throw ServerException("';' symbol needed after 'host' line");
if (host == "localhost")
host = "127.0.0.1";
struct sockaddr_in test;
if (inet_pton(AF_INET, host.c_str(), &test.sin_addr) == 0)
throw ServerException("IPv4 address format could not be recognised in 'host' line");
_host = inet_addr(host.c_str());
}
void Server::setServerName(std::string name) {
if (isValidConfValue(name) == true)
_serverName = name;
else
throw ServerException("';' symbol needed after 'server_name' line");
}
void Server::setRoot(std::string root) {
if (isValidConfValue(root) == false)
throw ServerException("';' symbol needed after 'root' line");
struct stat test;
if (stat(root.c_str(), &test) == 0 && test.st_mode & S_IFDIR)
{
_root = root;
return ;
}
char buffer[4096];
if (getcwd(buffer, 4096) == false)
throw ServerException("getcwd error");
std::string root2 = buffer + root;
if (stat(root2.c_str(), &test) == 0 && test.st_mode & S_IFDIR)
_root = root2;
else
throw ServerException("Invalid root path");
}
void Server::setClientMaxBodySize(std::string clientMaxBodySize) {
if (isValidConfValue(clientMaxBodySize) == false)
throw ServerException("';' symbol needed after 'client_max_body_size' line");
int n = stringToInt(clientMaxBodySize);
if (n < 1)
throw ServerException("Invalid value in 'client_max_body_size' line");
_clientMaxBodySize = n;
}
void Server::setIndex(std::string index) {
if (isValidConfValue(index) == true)
_index = index;
else
throw ServerException("';' symbol needed after 'index' line");
}
void Server::setErrorPages(std::vector<std::string> errorPages) {
if (errorPages.empty() == true)
return ;
size_t size = errorPages.size();
int code;
std::string path;
std::map<int, std::string>::iterator it;
if (size % 2 != 0)
throw ServerException("Listing after 'error_page' directive must follow pattern : <error_number> <file_path>");
for (size_t i = 0 ; i < size ; i++)
{
if (i % 2 == 0)
{
if (errorPages[i].length() != 3)
throw ServerException("Invalid http error code : " + errorPages[i]);
code = stringToInt(errorPages[i]);
if (getStatus(code) == "Unknown")
throw ServerException("Invalid http error code : " + errorPages[i]);
}
else
{
path = errorPages[i];
if (i == errorPages.size() - 1)
{
if (isValidConfValue(path) == false)
throw ServerException("';' symbol must be at the end of 'error_page' list");
}
if (checkFile(path, _root) == false)
throw ServerException("Invalid error file path : " + path);
it = _errorPages.find(code);
if (it == _errorPages.end())
_errorPages.insert(std::make_pair(code, path));
else
_errorPages[code] = path;
}
}
}
void Server::setLocation(std::string path, std::vector<std::string> content) {
Location loc;
std::vector<std::string> methods;
std::vector<std::string> cgiInfo;
size_t size = content.size();
bool autoindexAlreadySet = false;
loc.setPath(path);
if (path == "/cgi-bin")
loc.setCgiBool(true);
for (size_t i = 0; i < size; i++)
{
if (content[i] == "root" && i + 1 < size)
{
if (loc.getRoot() != "")
throw ServerException("Each 'location' context can't have more than one 'root' directive");
loc.setRoot(content[++i], _root);
}
else if (content[i] == "allow_methods" && i + 1 < size)
{
if (loc.getMethods().empty() == false)
throw ServerException("Each 'location' context can't have more than one 'allow_methods' directive");
while (++i < size)
{
if (content[i].find(";") != std::string::npos)
{
if (isValidConfValue(content[i]) == false)
throw ServerException("';' symbol needed after 'allow_methods' line in 'location' context");
methods.push_back(content[i]);
break;
}
else
methods.push_back(content[i]);
if (i + 1 == size)
throw ServerException("';' symbol needed after 'allow_methods' line in 'location' context");
}
loc.setMethods(methods);
}
else if (content[i] == "autoindex" && i + 1 < size)
{
if (autoindexAlreadySet == true)
throw ServerException("Each 'location' context can't have more than one 'autoindex' directive");
if (loc.getCgiBool() == true)
throw ServerException("'autoindex' directive is incompatible with cgi-bin 'location'");
i++;
if (content[i] == "on;")
loc.setAutoIndex(true);
else if (content[i] != "off;")
throw ServerException("autoindex must be set to 'on' or 'off'");
autoindexAlreadySet = true;
}
else if(content[i] == "index" && i + 1 < size)
{
if (loc.getIndex() != "")
throw ServerException("Each 'location' context can't have more than one 'index' directive");
loc.setIndex(content[++i]);
}
else if (content[i] == "return" && i + 1 < size)
{
if (loc.getRedirectionCode() != 0)
throw ServerException("Each 'location' context can't have more than one 'return' directive");
if (loc.getCgiBool() == true)
throw ServerException("'return' directive is incompatible with cgi-bin 'location'");
i++;
int code = stringToInt(content[i]);
if (code != 300 && code != 301 && code != 302 && code != 303 && code != 304 && code != 307 && code != 308)
throw ServerException("Unknown redirection code after 'return' directive : " + content[i]);
loc.setRedirectionCode(code);
i++;
if (isValidConfValue(content[i]) == false)
throw ServerException("';' symbol needed after 'return' line in 'location' context");
loc.setRedirectionPath(content[i]);
}
else if (content[i] == "cgi" && i + 1 < size)
{
i++;
while (i < size)
{
cgiInfo.push_back(content[i]);
if (content[i].find(';') != std::string::npos)
break ;
i++;
}
if (i == size)
throw ServerException("Missing ';' symbol after 'cgi' line \n" + static_cast<std::string>(strerror(errno)));
}
else if (content[i] == "upload_dir" && i + 1 < size)
{
if (loc.getUploadDir() != "")
throw ServerException("Each 'location' context can't have more than one 'upload_dir' directive");
i++;
if (isValidConfValue(content[i]) == false)
throw ServerException("';' symbol needed after 'upload_dir' line in 'location' context");
loc.setUploadDir(content[i]);
}
else
throw ServerException("Unknown or incomplete directive in 'location' context : " + content[i]
+ "\n" + static_cast<std::string>(strerror(errno)));
}
if (loc.getCgiBool() == true)
loc.setCgiExtensionAndPath(cgiInfo);
loc.checkConfig(_root);
_locations.push_back(loc);
}
void Server::setServaddr(void)
{
_servaddr.sin_family = AF_INET;
_servaddr.sin_addr.s_addr = _host;
_servaddr.sin_port = _port;
}
void Server::setTimeout(std::string timeout) {
if (isValidConfValue(timeout) == false)
throw ServerException("';' symbol needed after 'timeout' line");
int n = stringToInt(timeout);
if (n < 1 || n > 120)
throw ServerException("Invalid value in 'timeout' line. Please set timeout between 1 and 120 seconds");
_timeout = n;
}
void Server::bind_server(void)
{
if (bind(this->_listenSocket, (struct sockaddr *)&(this->_servaddr), sizeof(this->_servaddr)) < 0)
throw ServerException("bind: " + static_cast<std::string>(strerror(errno)));
}
void Server::checkDoubleLocations(void) {
if (_locations.empty())
return ;
size_t i, j;
for (i = 0; i < _locations.size() - 1; i++)
{
for (j = i + 1; j < _locations.size(); j++)
{
if (_locations[i].getPath() == _locations[j].getPath())
throw ServerException("Two 'location' contexts can't be defined for the same path : "
+ _locations[i].getPath() + "\n" + static_cast<std::string>(strerror(errno)));
}
}
}