forked from tgrasset/webserv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Launcher.cpp
384 lines (352 loc) · 11.9 KB
/
Launcher.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Launcher.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tgrasset <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/09 18:38:41 by mbocquel #+# #+# */
/* Updated: 2023/09/12 10:22:19 by tgrasset ### ########.fr */
/* */
/* ************************************************************************** */
#include "Launcher.hpp"
/* ************************************************************************** */
/* Constructeurs et destructeurs */
/* ************************************************************************** */
Launcher::Launcher(std::string path)
{
this->_pathConf = path;
_breakPollLoop = false;
_pollEvent.clear();
return ;
}
Launcher::Launcher(Launcher const & copy)
{
*this = copy;
}
Launcher::~Launcher(void)
{
for (std::list<Client>::iterator it = this->_clients.begin();
it != this->_clients.end(); ++it)
close(it->getComSocket());
for (std::vector<Server>::iterator it = this->_servers.begin();
it != this->_servers.end(); ++it)
close(it->getListenSocket());
}
/* ************************************************************************** */
/* Surcharge d'operateur */
/* ************************************************************************** */
Launcher & Launcher::operator=(Launcher const & launcher)
{
if (this != &launcher)
{
_servers = launcher._servers;
_clients = launcher._clients;
_pathConf = launcher._pathConf;
_pollEvent = launcher._pollEvent;
_breakPollLoop = launcher._breakPollLoop;
}
return (*this);
}
/* ************************************************************************** */
/* Methodes */
/* ************************************************************************** */
void Launcher::parse(void)
{
ConfParser conf_parser(this->_pathConf);
conf_parser.parse();
this->_servers = conf_parser.getServers();
}
void Launcher::launchServers(void)
{
_pollEvent.reserve(this->_servers.size() * MAX_EVENTS);
int nfds = 0;
this->initiateServersListening();
/* Global loop for the Webserv */
while (1)
{
_breakPollLoop = false;
this->checkTimeoutClients();
if ((nfds = poll(_pollEvent.data(), _pollEvent.size() , -1)) == -1)
throw LauncherException("Problem with poll !");
if (nfds > 0)
{
for (std::vector< struct pollfd >::iterator it = _pollEvent.begin(); it != _pollEvent.end(); ++it)
{
if (it->revents & POLLIN && !_breakPollLoop) //Read.
{
if (checkIfListenSocket(it->fd)) //New connexion
this->processNewClient(it->fd);
else
this->processReadingFd(it->fd);
}
else if (it->revents & POLLOUT && !_breakPollLoop) //Write
{
this->processWritingFd(it->fd);
}
else if (((it->revents & POLLERR) || (it->revents & POLLHUP)) && !_breakPollLoop)
{
this->processCloseConnexionOrError(it->fd);
}
if (_breakPollLoop)
break;
}
}
}
}
void Launcher::initiateServersListening(void)
{
for (std::vector<Server>::iterator it = this->_servers.begin();
it != this->_servers.end(); ++it)
{
if (this->getServerWithSameHostPort(it) != this->_servers.end())
it->setListenSocket(this->getServerWithSameHostPort(it)->getListenSocket());
else
{
int listensocket = socket(AF_INET, SOCK_STREAM, 0);
if (listensocket < 0)
throw LauncherException("Problem with socket !");
const int enable = 1;
if (setsockopt(listensocket, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0)
throw LauncherException("Problem with setsockopt !");
it->setListenSocket(listensocket);
it->setServaddr();
it->bind_server();
if (listen(it->getListenSocket(), MAX_WAIT) < 0)
throw LauncherException("Impossible to listen !");
}
addFdToPollIn(it->getListenSocket());
char str_ip_server[INET_ADDRSTRLEN];
struct sockaddr_in server_addr = it->getServerAddr();
inet_ntop (AF_INET, &(server_addr.sin_addr), str_ip_server, sizeof (str_ip_server));
}
}
void Launcher::processNewClient(int fd)
{
int fd_new;
socklen_t addrlen;
struct sockaddr_in client_addr;
char str_ip_client[INET_ADDRSTRLEN];
addrlen = sizeof (struct sockaddr_in);
fd_new = accept(fd, (struct sockaddr *) &client_addr, &addrlen);
if (fd_new == -1)
throw LauncherException("Problem with accept !");
addFdToPollIn(fd_new);
Client new_client(this, client_addr, fd_new);
addServerOfClient(fd, &new_client);
_clients.push_back(new_client);
inet_ntop (AF_INET, &(client_addr.sin_addr), str_ip_client, sizeof (str_ip_client));
std::cout << TXT_MAG << TXT_B << getTimestamp() << "Client " << new_client.getId() << ": New client connexion from " << str_ip_client << TXT_END << std::endl;
}
/*
- Launch the function that recieve the client request
- If the client is done sending request, move if to the EPOLLOUT list
*/
void Launcher::processReadingFd(int fd)
{
std::list<Client>::iterator client = this->findClient(fd);
switch (client->getSocketType(fd))
{
case COM_SOCKET:
if (client->receiveRequest())
this->removeClient(client);
break;
case CGI_PIPE:
if (client->addCgiToBuff())
{
std::cout << TXT_RED << getTimestamp() << "Client " << client->getId() << ": Error while adding CGI to Res buffer... Removing the client !" << TXT_END << std::endl;
this->removeClient(client);
}
break;
case RES_FILE_FD:
if (client->addBodyFileToBuff())
{
std::cout << TXT_RED << getTimestamp() << "Client " << client->getId() << ": Error while adding Body File to Res buffer... Removing the client !" << TXT_END << std::endl;
this->removeClient(client);
}
break;
case UPLOAD_TMP_IN:
client->resetLastActivity();
client->transferUploadFileInSide();
break;
default:
std::cout << TXT_RED << TXT_B << "Problem reading fd is not a correct category. Removing client number :" << client->getId() << " FD number: " << fd << " categorie = " << client->getSocketType(fd) << TXT_END << std::endl;
client->printClientFd();
this->removeClient(client);
}
}
void Launcher::processWritingFd(int fd)
{
std::list<Client>::iterator client = this->findClient(fd);
switch (client->getSocketType(fd))
{
case COM_SOCKET:
client->sendResponse();
if (client->getStatus() == RES_SENT && client->getKeepAlive() && client->getHttpRes() && client->getHttpRes()->getStatusCode() < 300)
{
std::cout << TXT_GREEN << getTimestamp() << "Client " << client->getId() << ": Writing finised. Adding him to reading monitored sockets" << TXT_END << std::endl;
client->resetClient();
removeFdFromPoll(fd);
addFdToPollIn(fd);
}
else if (client->getStatus() == RES_SENT || client->getStatus() == ERROR_WHILE_SENDING)
{
if (client->getStatus() == RES_SENT)
std::cout << TXT_GREEN << getTimestamp() << "Client " << client->getId() << ": Process finished. Client removed" << TXT_END << std::endl;
else if (client->getStatus() == ERROR_WHILE_SENDING)
std::cout << TXT_RED << getTimestamp() << "Client " << client->getId() << ": An error happend while sending the response. Client removed" << TXT_END << std::endl;
this->removeClient(client);
}
break;
case REQ_FILE_FD:
if (client->writeReqBodyFile())
{
std::cout << TXT_RED << getTimestamp() << "Client " << client->getId() << ": An error happend while writing on the Req Tmp Body File. Client removed" << TXT_END << std::endl;
this->removeClient(client);
}
break;
case UPLOAD_OUT:
client->resetLastActivity();
client->transferUploadFileOutSide();
break;
default:
std::cout << TXT_RED << TXT_B << "Problem writing fd is not a correct category. Removing client number :" << client->getId() << " FD number: " << fd << " categorie = " << client->getSocketType(fd) << TXT_END << std::endl;
client->printClientFd();
this->removeClient(client);
}
}
void Launcher::processCloseConnexionOrError(int fd)
{
std::list<Client>::iterator client = this->findClient(fd);
switch (client->getSocketType(fd))
{
case COM_SOCKET:
std::cout << TXT_RED << getTimestamp() << "Client " << client->getId() << ": Connexion has been closed" << TXT_END <<std::endl;
this->removeClient(client);
break;
case CGI_PIPE:
client->cgiPipeFinishedWriting();
break;
default:
break;
}
}
std::vector<Server>::iterator Launcher::getServerWithSameHostPort(std::vector<Server>::iterator it_find)
{
for (std::vector<Server>::iterator it = this->_servers.begin(); it != this->_servers.end(); ++it)
{
if (it == it_find)
return (this->_servers.end());
if (it->getHost() == it_find->getHost() && it->getPort() == it_find->getPort())
return (it);
}
return (this->_servers.end());
}
std::list<Client>::iterator Launcher::findClient(int socket)
{
std::list<Client>::iterator it = this->_clients.begin();
while (it != this->_clients.end())
{
if (it->getSocketType(socket) != NOT_MINE)
break;
++it;
}
if (it == this->_clients.end())
{
throw LauncherException("Client not found !");
}
return (it);
}
bool Launcher::checkIfListenSocket(int socket)
{
for (std::vector<Server>::iterator it = this->_servers.begin();
it != this->_servers.end(); ++it)
{
if (socket == it->getListenSocket())
return (true);
}
return (false);
}
void Launcher::addServerOfClient(int listen_sock, Client *client)
{
for (std::vector<Server>::iterator it = this->_servers.begin();
it != this->_servers.end(); ++it)
{
if (listen_sock == it->getListenSocket())
client->AddServerPtr(&(*it));
}
}
void Launcher::checkTimeoutClients(void)
{
unsigned long time = 0;
for(std::list<Client>::iterator it = this->_clients.begin(); it != this->_clients.end();++it)
{
time = it->timeSinceLastActivityUs() / 1000000;
if (time > static_cast<unsigned long>(it->getTimeOut()))
{
std::cout << TXT_MAG << getTimestamp() << "Client " << it->getId() << ": Timeout, time = " << time << ". Client removed" << TXT_END << std::endl;
std::list<Client>::iterator it_tmp = it;
--it;
this->removeClient(it_tmp);
}
}
}
void Launcher::removeClient(std::list<Client>::iterator client)
{
removeFdFromPoll(client->getComSocket());
close(client->getComSocket());
this->_clients.erase(client);
}
void Launcher::testFolderTmp(void) const
{
std::string body_tmp_folder(BODY_TMP_FOLDER);
struct stat stats_dossier;
if (stat(body_tmp_folder.c_str(), &stats_dossier) != 0
|| access(body_tmp_folder.c_str(), W_OK | R_OK) != 0
|| !S_ISDIR(stats_dossier.st_mode))
throw LauncherException("Temp body folder does not exist");
}
void Launcher::removeFdFromPoll(int fd)
{
std::vector <struct pollfd >::iterator it = _pollEvent.begin();
while (it != _pollEvent.end())
{
if (it->fd == fd)
break;
++it;
}
if (it != _pollEvent.end())
{
_pollEvent.erase(it);
_breakPollLoop = true;
}
}
void Launcher::printPollEvent(void) const
{
for(std::vector < struct pollfd >::const_iterator it = this->_pollEvent.begin(); it != _pollEvent.end(); ++it)
{
std::cout << " FD: " << it->fd << " | event: " << it->events << " | revent: " << it->revents << std::endl;
}
}
void Launcher::addFdToPollIn(int fd)
{
struct pollfd poll_struct;
poll_struct.fd = fd;
poll_struct.events = POLLIN | POLLHUP | POLLERR;
_pollEvent.push_back(poll_struct);
_breakPollLoop = true;
}
void Launcher::addFdToPollOut(int fd)
{
struct pollfd poll_struct;
poll_struct.fd = fd;
poll_struct.events = POLLOUT | POLLERR;
_pollEvent.push_back(poll_struct);
_breakPollLoop = true;
}
std::vector<Server> &Launcher::getServers(void) {
return (this->_servers);
}
std::list<Client> &Launcher::getClients(void) {
return (this->_clients);
}