forked from bbotfatimezzahra/Webserv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerConfig.cpp
More file actions
324 lines (259 loc) · 8.03 KB
/
Copy pathServerConfig.cpp
File metadata and controls
324 lines (259 loc) · 8.03 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
#include"includes/ServerConfig.hpp"
ServerConfig::ServerConfig(){
this->fd = -1;
this->client_body_size = -1;
this->name_err.insert(std::make_pair("200","OK"));
this->name_err.insert(std::make_pair("400","Bad Request"));
this->name_err.insert(std::make_pair("403","Forbidden"));
this->name_err.insert(std::make_pair("404"," Not Found"));
this->name_err.insert(std::make_pair("405","Method Not Allowed"));
this->name_err.insert(std::make_pair("408","Request Timeout"));
this->name_err.insert(std::make_pair("409","Conflict"));
this->name_err.insert(std::make_pair("411","Length Required"));
this->name_err.insert(std::make_pair("413","Content Too Large"));
this->name_err.insert(std::make_pair("414","URI Too Long"));
this->name_err.insert(std::make_pair("415","Unsupported Media Type"));
this->name_err.insert(std::make_pair("500","Internal Server Error"));
this->name_err.insert(std::make_pair("501","Not Implemented"));
this->name_err.insert(std::make_pair("502","Bad Gateway"));
this->name_err.insert(std::make_pair("504","Gateway Timeout"));
this->errors.insert(std::make_pair(400,"errors/400.html"));
this->errors.insert(std::make_pair(403,"errors/403.html"));
this->errors.insert(std::make_pair(404,"errors/404.html"));
this->errors.insert(std::make_pair(405,"errors/405.html"));
this->errors.insert(std::make_pair(408,"errors/408.html"));
this->errors.insert(std::make_pair(409,"errors/409.html"));
this->errors.insert(std::make_pair(411,"errors/411.html"));
this->errors.insert(std::make_pair(413,"errors/413.html"));
this->errors.insert(std::make_pair(414,"errors/414.html"));
this->errors.insert(std::make_pair(415,"errors/415.html"));
this->errors.insert(std::make_pair(500,"errors/500.html"));
this->errors.insert(std::make_pair(501,"errors/501.html"));
this->errors.insert(std::make_pair(502,"errors/502.html"));
this->errors.insert(std::make_pair(504,"errors/504.html"));
}
ServerConfig::~ServerConfig(){}
ServerConfig::ServerConfig(const ServerConfig &cnt){
*this = cnt;
}
ServerConfig &ServerConfig::operator=(const ServerConfig &cnt){
if(this != &cnt){
this->setIp(cnt.ip);
this->setFd(cnt.fd);
this->setPort(cnt.port);
this->setClientBodySize(cnt.client_body_size);
this->setServerName(cnt.server_name);
this->setLocation(cnt.location);
this->setName_err(cnt.name_err);
std::map<int,std::string>::const_iterator it = cnt.errors.begin();
std::map<int,std::string>::const_iterator ite = cnt.errors.end();
while(it != ite){
this->setError(it->first, it->second);
it++;
}
}
return *this;
}
std::map<int,std::string> ServerConfig::getError(void){
return this->errors;
}
std::vector<Location> &ServerConfig::getLocation(void){
return this->location;
}
std::string ServerConfig::getServerName(void){
return this->server_name;
}
std::string ServerConfig::getPort(void){
return this->port;
}
std::string ServerConfig::getIp(void){
return this->ip;
}
int ServerConfig::getFd(void) const
{
return this->fd;
}
std::map<std::string,std::string> ServerConfig::getName_err(void){
return this->name_err;
}
ssize_t ServerConfig::getClientBodySize(void){
return this->client_body_size;
}
void ServerConfig::setClientBodySize(ssize_t size){
this->client_body_size = size;
}
void ServerConfig::setServerName(std::string str){
this->server_name = str;
}
void ServerConfig::setError(int k, std::string val){
this->errors[k] = val;
}
void ServerConfig::setLocation(std::vector<Location> lc){
this->location = lc;
}
void ServerConfig::setPort(std::string i){
this->port = i ;
}
void ServerConfig::setIp(std::string Ip){
this->ip = Ip;
}
void ServerConfig::setFd(int Fd){
this->fd = Fd;
}
void ServerConfig::setName_err(std::map<std::string,std::string> err){
this->name_err = err;
}
///////////////////////////////////////////////////////////////////////////////////
Location::Location(){
this->autoindex = false;
}
Location::~Location(){}
Location::Location(const Location &lc){
*this = lc;
}
Location &Location::operator=(const Location &lc){
if(this != &lc){
this->setPath(lc.path);
this->setIndex(lc.index);
this->setRoot(lc.root);
this->setAutoindex(lc.autoindex);
this->setMethods(lc.methods);
this->setUpload(lc.upload);
this->setCgi(lc.cgi);
this->setRedirect(lc.redirect);
}
return *this;
}
bool Location::getAutoindex(void){
return this->autoindex;
}
std::vector<std::string> Location::getIndex(void){
return this->index;
}
std::vector<std::string> Location::getMethods(void){
return this->methods;
}
std::string Location::getRoot(void){
return this->root;
}
std::string Location::getPath(void){
return this->path;
}
std::string Location::getUpload(void){
return this->upload;
}
std::map<int,std::string> Location::getRedirect(void){
return this->redirect;
}
std::map<std::string,std::string> Location::getCgi(void){
return this->cgi;
}
void Location::setRedirect(std::map<int,std::string> cp){
this->redirect = cp;
}
void Location::setPath(std::string str){
this->path = str;
}
void Location::setAutoindex(bool i){
this->autoindex = i;
}
void Location::setIndex(std::vector<std::string> idx){
size_t s = idx.size();
for(size_t i = 0; i < s; i++){
this->index.push_back(idx[i]);
}
}
void Location::setRoot(std::string str){
this->root = str;
}
void Location::setMethods(std::vector<std::string> str){
size_t s = str.size();
for(size_t i = 0; i < s; i++){
this->methods.push_back(str[i]);
}
}
void Location::setUpload(std::string str){
this->upload = str;
}
void Location::setCgi(std::map<std::string,std::string> cg){
this->cgi = cg;
}
int ServerConfig::start(void)
{
//filling the setup of the socket
struct addrinfo setup;
memset(&setup, 0, sizeof(setup));
setup.ai_family = AF_INET;
setup.ai_socktype = SOCK_STREAM;
setup.ai_protocol = IPPROTO_TCP;
setup.ai_flags = AI_PASSIVE;
struct addrinfo *res;
int r;
//getting the needed infos in the correct format to create the socket
r = getaddrinfo(ip.c_str(), port.c_str(), &setup, &res);
if (r)
{
std::cerr << "GetAddrInfo failed for " << ip << ":"<< port<<std::endl;
gai_strerror(r);
return 0;
}
//creating the socket
setFd(socket(res->ai_family, res->ai_socktype, res->ai_protocol));
if (fd == -1)
{
std::cerr << "Socket failed for " << ip << ":"<< port<<std::endl;
freeaddrinfo(res);
throw MyError(strerror(errno));
}
//setting socket options to make addr and ports shareable and reusable without time wait
int enable = 1;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)))
std::cerr <<"SetSockOpt SO_REUSEADDR failed" << std::endl;
if(setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &enable, sizeof(int)))
std::cerr <<"SetSockOpt SO_REUSEPORT failed" << std::endl;
//binding the socket to the ip and port using the struct of getaddrinfo
if (bind(fd, res->ai_addr, res->ai_addrlen) == -1)
{
freeaddrinfo(res);
close(fd);
fd = -1;
std::cerr << strerror(errno)<< std::endl;
return 0;
}
//setting the socket to be nonblocking for epoll
if (!set_nonBlock(fd))
{
freeaddrinfo(res);
close(fd);
fd = -1;
std::cerr << strerror(errno)<< std::endl;
return 0;
}
//listening on the socket for client requests
if (listen(fd, SOMAXCONN) == -1)
{
freeaddrinfo(res);
close(fd);
fd = -1;
std::cerr << strerror(errno)<< std::endl;
return 0;
}
freeaddrinfo(res);
std::cout << "Listening on : "<< ip << ":"<< port <<std::endl;
std::cout << "=========================================="<<std::endl;
return 1;
}
int ServerConfig::addToEpoll(int epoll_fd)
{
struct epoll_event events;
events.events = EPOLLIN;
events.data.fd = fd;
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD,fd, &events) < 0)
{
close(fd);
fd = -1;
std::cerr << strerror(errno)<< std::endl;
return 0;
}
return 1;
}