Files
42_INT_12_webserv/srcs/Webserv.cpp
2022-07-27 20:47:13 +02:00

254 lines
5.5 KiB
C++

#include "Webserv.hpp"
Webserv::Webserv()
{
std::cout << "Server init\n";
_epfd = ::epoll_create1(0); // (EPOLL_CLOEXEC) for CGI fork ?
if (_epfd == -1)
{
std::perror("err epoll_create1(): ");
throw std::runtime_error("Epoll init");
}
}
/* Webserv::Webserv(Webserv const &src)
{
} */
Webserv::~Webserv()
{
close(_socket_fd);
close(_epfd);
std::cout << "Server destroyed\n";
}
/* Webserv & Webserv::operator=(Webserv const &rhs)
{
} */
///////////////
// Functions //
void Webserv::init_virtual_servers() // ADD config param
{
_socket_fd = ::socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0); // (SOCK_CLOEXEC) for CGI fork ?
if (_socket_fd == -1)
{
std::perror("err socket(): ");
throw std::runtime_error("Socket init");
}
_bind(_socket_fd, 4040);
_listen(_socket_fd, 512); // 512 arbitrary
struct epoll_event ev;
std::memset(&ev, 0, sizeof ev);
ev.events = EPOLLIN;
ev.data.fd = _socket_fd;
if (::epoll_ctl(_epfd, EPOLL_CTL_ADD, _socket_fd, &ev) == -1)
{
std::perror("err epoll_ctl(): ");
throw std::runtime_error("Socket init");
}
}
void Webserv::start()
{
std::cout << "Server started\n";
struct epoll_event events[MAX_EVENTS];
int nfds;
int i;
int count_loop = 0;
std::cout << ++count_loop << "----loop epoll()\n";
while ( (nfds = ::epoll_wait(_epfd, events, MAX_EVENTS, TIMEOUT)) != -1)
{
if (nfds == 0)
{
(void)0;
// TODO : parcourir les "Clients" encore ouvert et les close() tous
}
i = 0;
while (i < nfds)
{
if ((events[i].data.fd == _socket_fd) && (events[i].events & EPOLLIN))
_accept_connection(events[i].data.fd);
else if (events[i].events & EPOLLIN)
_read_request(events[i].data.fd);
else if (events[i].events & EPOLLOUT)
_send_response(events[i].data.fd);
++i;
}
std::cout << ++count_loop << "----loop epoll()\n";
}
if (nfds == -1)
{
std::perror("err epoll_wait(): ");
throw std::runtime_error("Epoll wait");
}
}
///////////////////////
// Private Functions //
void Webserv::_bind(int socket_fd, in_port_t port)
{
// cast invalid ? how to ?
// const struct sockaddr* cast_test = static_cast<const struct sockaddr*>(addr);
struct sockaddr_in addr;
std::memset(&addr, 0, sizeof addr);
addr.sin_family = AF_INET;
addr.sin_port = ::htons(port);
addr.sin_addr.s_addr = ::htonl(INADDR_ANY); // htonl useless with 0 value (INADDR_ANY) ?
if (::bind(socket_fd, (const sockaddr*)&addr, sizeof addr) == -1)
{
std::perror("err bind(): ");
throw std::runtime_error("Socket bind");
}
}
void Webserv::_listen(int socket_fd, unsigned int max_connections)
{
if (::listen(socket_fd, max_connections) == -1)
{
std::perror("err listen(): ");
throw std::runtime_error("Socket listen");
}
}
void Webserv::_accept_connection(int fd)
{
struct sockaddr_in addr;
socklen_t addr_len;
int accepted_fd;
std::cout << "accept()\n";
addr_len = sizeof addr;
accepted_fd = ::accept(fd, (sockaddr*)&addr, &addr_len);
if (accepted_fd == -1)
{
std::perror("err accept(): ");
return;
}
::fcntl(accepted_fd, F_SETFL, O_NONBLOCK);
struct epoll_event ev;
std::memset(&ev, 0, sizeof ev);
ev.events = EPOLLIN;
ev.data.fd = accepted_fd;
if (::epoll_ctl(_epfd, EPOLL_CTL_ADD, accepted_fd, &ev) == -1)
std::perror("err accept() epoll_ctl(): ");
}
void Webserv::_read_request(int fd)
{
std::cout << "recv()\n";
_read_ret = ::recv(fd, _buf, BUFSIZE, 0);
if (_read_ret == -1)
{
std::perror("err recv(): ");
if (::send(fd, MSG_BOUNCE, sizeof MSG_BOUNCE - 1, 0) == -1)
std::perror("err send(): ");
::close(fd);
return;
}
/*
if (_read_ret == BUFSIZE)
// send error like "request too long" to client
*/
_buf[_read_ret] = '\0';
struct epoll_event ev;
std::memset(&ev, 0, sizeof ev);
ev.events = EPOLLOUT;
ev.data.fd = fd;
if (::epoll_ctl(_epfd, EPOLL_CTL_MOD, fd, &ev) == -1)
std::perror("err accept() epoll_ctl(): ");
}
void Webserv::_send_response(int fd)
{
// TMP test cgi
// find POST in _buf
std::string tmpstr = _buf;
std::size_t found;
found = tmpstr.find("POST");
// if "POST" found, execve a cgi
if (found != std::string::npos)
{
int save_stdout;
char** env = new char*[4];
char * const * nll = NULL;
// set env
env[0] = strdup("PATH_INFO=/no");
env[1] = strdup("REQUEST_METHOD=POST");
env[2] = strdup("SERVER_PROTOCOL=HTTP/1.1");
env[3] = NULL;
// save STDOUT
save_stdout = dup(STDOUT_FILENO);
// inside chil process
if (fork() == 0)
{
dup2(fd, STDOUT_FILENO);
execve("./srcs/cgi-bin/cgi_cpp.cgi", nll, env);
}
// inside parent process
else
waitpid(-1, NULL, 0);
// restore stdout
dup2(save_stdout, STDOUT_FILENO);
// don't send the rest
::close(fd);
return;
}
else
found = tmpstr.find("index.html");
// if "index.html" found, send the page
if (found != std::string::npos)
{
int index_fd;
std::string to_send;
std::string end_header = "\r\n\r\n";
std::string body;
std::stringstream strs;
char buffer[1];
to_send = "HTTP/1.1 200 OK\nContent-Type: text/html; charset=UTF-8\nContent-Length: ";
index_fd = open("./index.html", O_RDONLY);
for (int ret = 1; ret > 0;)
{
ret = read(index_fd, buffer, 1);
body += buffer;
}
strs << body.size();
to_send += strs.str();
to_send += end_header;
to_send += body;
::send(fd, to_send.c_str(), to_send.size(), 0);
// don't send the rest
::close(fd);
return;
}
// TMP end test cgi
std::cout << "send()\n";
if (::send(fd, _buf, _read_ret, 0) == -1)
std::perror("err send(): ");
if (::send(fd, MSG_TEST, sizeof MSG_TEST - 1, 0) == -1)
std::perror("err send(): ");
::close(fd);
}