forked from Hattaki-Hajar/webserv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Webserv.cpp
358 lines (334 loc) · 9.01 KB
/
Webserv.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
#include "Webserv.hpp"
#include <cstring>
#include <algorithm>
#include "response/response.hpp"
Webserv::Webserv()
{
// _event = new epoll_event;
this->size = 0;
}
void Webserv::new_connection(Server &s)
{
int i;
_Clients.push_back(new Client(s));
i = _Clients.size() - 1;
int new_socket = accept(s.get_socket(), (struct sockaddr *)&_Clients[i]->_addr, &_Clients[i]->_addr_size);
if (new_socket < 0)
{
perror("accept");
throw std::runtime_error("failed to connect1!");
}
_Clients[i]->set_socket(new_socket);
if (set_nonblocking(new_socket) < 0)
throw std::runtime_error("set_nonblocking failed!");
if (epoll_ctl(s.get_epfd(), EPOLL_CTL_ADD, _Clients[i]->get_socket(), _Clients[i]->get_event()) < 0)
{
perror("epoll_ctl");
throw std::runtime_error("epoll_ctl failed! 2");
}
// std::cout << "fd: " << new_socket << std::endl;
}
int is_server_socket(std::vector<Server *> &Servers, int socket)
{
size_t i = 0;
while (i < Servers.size())
{
if (Servers[i]->get_socket() == socket)
return (i);
i++;
}
return (-1);
}
void set_up_webserv(std::vector<Server *> &Servers, int epfd)
{
if (epfd < 0)
throw std::runtime_error("epoll_create1 failed!");
for (size_t i = 0; i < Servers.size(); i++)
set_up_Server(*Servers[i], epfd);
}
int find_client(std::vector<Client *> &Clients, int socket)
{
size_t i = 0;
while (i < Clients.size())
{
if (Clients[i]->get_socket() == socket)
return (i);
i++;
}
return (-1);
}
void Webserv::check_cgi()
{
double time;
clock_t end;
int status;
double client_time;
for (size_t i = 0; i < _Clients.size(); i++)
{
if ((_Clients[i]->_cgi && !_Clients[i]->_cgi->is_running) || !_Clients[i]->_cgi) {
client_time = (double)(clock() - _Clients[i]->start) / CLOCKS_PER_SEC;
// std::cout << "client time: " << client_time << std::endl;
if (client_time >= 30)
{
// std::cout << "client timeout" << std::endl;
this->_Clients[i]->set_reading_status(true);
this->_Clients[i]->timeout = true;
if (!_Clients[i]->_response)
_Clients[i]->generateResponse();
}
}
if (_Clients[i]->_cgi && _Clients[i]->_cgi->is_running)
{
end = clock();
if (waitpid(_Clients[i]->_cgi->_pid, &status, WNOHANG) > 0)
{
if (WIFEXITED(status)) {
_Clients[i]->_response->set_status_code(200);
}
else {
std::cout << "cgi exited abnormally" << std::endl;
_Clients[i]->_response->set_status_code(500);
}
_Clients[i]->_cgi->is_running = false;
_Clients[i]->_cgi->is_complete = true;
}
else
{
time = (double)(end - _Clients[i]->_cgi->_start) / CLOCKS_PER_SEC;
// std::cout << "time: " << time << std::endl;
if (time >= 30)
{
// std::cout << "killing cgi" << std::endl;
kill(_Clients[i]->_cgi->_pid, SIGKILL);
waitpid(_Clients[i]->_cgi->_pid, &status, 0);
_Clients[i]->_response->set_status_code(504);
_Clients[i]->_cgi->is_running = false;
_Clients[i]->_cgi->is_complete = true;
}
}
}
}
}
void Webserv::client_timeout() {
double time;
for(size_t i = 0; i < _Clients.size(); i++) {
if ((_Clients[i]->_cgi && !_Clients[i]->_cgi->is_running) || !_Clients[i]->_cgi) {
time = (double)(clock() - _Clients[i]->start) / CLOCKS_PER_SEC;
std::cout << "time: " << time << std::endl;
if (time >= 30) {
this->_Clients[i]->set_reading_status(true);
this->_Clients[i]->timeout = true;
if (!this->_Clients[i]->_response)
this->_Clients[i]->generateResponse();
}
}
}
}
void Webserv::start()
{
int i = 0, client_nb = 0;
ssize_t bytesread = 0;
// double time;
char buffer[BUFFER_SIZE + 1];
bzero(buffer, BUFFER_SIZE + 1);
int epfd = epoll_create(MAX_EVENTS), event_nb = 0, fd;
epoll_event events[MAX_EVENTS];
set_up_webserv(_Servers, epfd);
while (1)
{
try {
event_nb = epoll_wait(epfd, events, MAX_EVENTS, 0);
if (event_nb < 0)
throw std::runtime_error("epoll_wait failed!");
for (int j = 0; j < event_nb; j++)
{
fd = events[j].data.fd;
i = is_server_socket(_Servers, fd);
if (i >= 0)
{
new_connection(*_Servers[i]);
continue ;
}
client_nb = find_client(_Clients, fd);
if (events[j].events & EPOLLIN)
{
std::cout << "EPOLLIN" << std::endl;
_Clients[client_nb]->_EPOLL = true;
bzero(_Clients[client_nb]->get_buffer(), BUFFER_SIZE + 1);
bytesread = read(fd, _Clients[client_nb]->get_buffer(), BUFFER_SIZE);
size += bytesread;
_Clients[client_nb]->set_bytesread(bytesread);
_Clients[client_nb]->parse_request();
_Clients[client_nb]->clear_buffer();
}
if (events[j].events & EPOLLOUT)
{
if (!_Clients[client_nb]->get_request()->_headers_read && !_Clients[client_nb]->timeout) {
continue;
}
if (_Clients[client_nb]->get_done_reading()) {
if (!_Clients[client_nb]->_response)
_Clients[client_nb]->generateResponse();
if (_Clients[client_nb]->_cgi && !_Clients[client_nb]->_cgi->is_running)
write(fd, _Clients[client_nb]->_response->send(), _Clients[client_nb]->_response->getResponse_length());
if (_Clients[client_nb]->_response->getIs_complete())
{
epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL);
if (events[j].events & EPOLLHUP && _Clients[client_nb]->_cgi->_pid != -1) {
if (waitpid(_Clients[client_nb]->_cgi->_pid, NULL, 0) > 0) {
_Clients[client_nb]->_cgi->is_complete = true;
_Clients[client_nb]->_cgi->is_running = false;
}
else {
kill(_Clients[client_nb]->_cgi->_pid, SIGKILL);
}
}
close(fd);
delete _Clients[client_nb];
_Clients.erase(_Clients.begin() + client_nb);
}
}
}
}
// this->client_timeout();
this->check_cgi();
std::cout << "clients: " << _Clients.size() << std::endl;
}
catch (std::exception &e) {
(void)e;
}
}
}
void Webserv::bind_Servers()
{
size_t i = 0, j;
int port;
std::string host, server_name;
while (i < _Servers.size())
{
j = 0;
port = _Servers[i]->get_port();
host = _Servers[i]->get_ip();
server_name = _Servers[i]->get_name();
while (j < i)
{
if (_Servers[j]->get_port() == port && _Servers[j]->get_ip() == host)
throw std::runtime_error("Error: port and host are the same!");
j++;
}
i++;
_Servers[i - 1]->bind_Server();
}
}
/* setters */
void Webserv::set_new_Server(size_t s)
{
if (_Servers.size() < s + 1)
_Servers.push_back(new Server());
}
void Webserv::set_port(const std::string &port, int s)
{
int p;
int i = 0;
std::string nb;
if (port.empty())
return ;
while (port[i] && isdigit(port[i]))
nb += port[i++];
while (port[i] && isspace(port[i]))
i++;
p = atoi(nb.c_str());
if (p < 0 || p > 65535)
throw std::runtime_error("Error: port is not valid!");
_Servers[s]->set_port(p);
}
void Webserv::set_host(const std::string &h, int s)
{
int i, nb, j = 0;
if (h.empty())
return ;
std::string host = h;
i = std::count(host.begin(), host.end(), '.');
if (i != 3)
throw std::runtime_error("Error: host is not valid!");
if (h == "localhost")
{
_Servers[s]->set_host("127.0.0.1");
return ;
}
char *str = strtok((char *)host.c_str(), ".");
while (j < 4)
{
i = 0;
while (isdigit(str[i]))
i++;
if (str[i])
throw std::runtime_error("Error: host is not valid 1!");
nb = atoi(str);
if (nb < 0 || nb > 255)
throw std::runtime_error("Error: host is not valid 2!");
j++;
str = strtok(0, ".");
}
if (j == 4 && str)
throw std::runtime_error("Error: host is not valid 3!");
_Servers[s]->set_host(h);
}
void Webserv::set_name(const std::string &name, int Server)
{
_Servers[Server]->set_name(name);
}
void Webserv::set_error_page(int error, std::string const& path, int Server) {
_Servers[Server]->set_error_page(error, path);
}
void Webserv::set_body_size(size_t size, int Server) {
_Servers[Server]->set_body_size(size);
}
void Webserv::set_Server(int Server) {
_Servers[Server]->set_addr(_Servers[Server]->get_port(), _Servers[Server]->get_ip());
_Servers[Server]->set_len();
}
void Webserv::set_Server_socket(int socket, int Server) {
_Servers[Server]->set_socket(socket);
}
void Webserv::set_root(std::string const &path, int Server) {
_Servers[Server]->set_root(path);
}
void Webserv::set_index(std::string const &path, int server) {
_Servers[server]->set_index(path);
}
void Webserv::set_location(std::string const &path, location &loc, int Server) {
_Servers[Server]->set_location(path, loc);
}
/* getters */
const std::string &Webserv::get_name(int Server) {
return (_Servers[Server]->get_name());
}
const Server &Webserv::get_Server(int Server) const {
return (*_Servers[Server]);
}
size_t Webserv::get_size() const {
return (_Servers.size());
}
/* << overload for Webserv */
std::ostream& operator<<(std::ostream &os, const Webserv& s)
{
size_t i = 0;
while (i < s.get_size())
{
os << " ********* Server index: " << i << std::endl;
os << s.get_Server(i);
i++;
}
return os;
}
Webserv::~Webserv()
{
// std::cout << "Webserv destructor called" << std::endl;
for (size_t i = 0; i < _Servers.size(); i++)
delete _Servers[i];
for (size_t i = 0; i < _Clients.size(); i++)
delete _Clients[i];
// delete _event;
close(_epfd);
}