Files
42_INT_12_webserv/srcs/Webserv_hugo.cpp
hugogogo 2a69e14db2 wip atoi
2022-07-31 12:30:35 +02:00

448 lines
9.8 KiB
C++

#include "Webserv.hpp"
int g_last_signal;
bool g_run;
void signal_handler(int signum)
{
g_last_signal = signum;
}
Webserv::Webserv()
{
std::cerr << "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");
}
std::signal(SIGPIPE, signal_handler);
std::signal(SIGINT, signal_handler);
}
/* Webserv::Webserv(Webserv const &src)
{
} */
Webserv::~Webserv()
{
close(_socket_fd);
close(_epfd);
_close_all_clients();
std::cerr << "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");
}
// HUGO ADD
// allow socket descriptor to be reuseable
int on = 1;
if (setsockopt(_socket_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
{
::perror("setsockopt() failed");
throw std::runtime_error("Socket init");
}
// HUGO ADD END
_bind(_socket_fd, 4040);
_listen(_socket_fd, 512); // 512 arbitrary
if (_epoll_update(_socket_fd, EPOLLIN, EPOLL_CTL_ADD) == -1)
throw std::runtime_error("Socket init");
}
void Webserv::start()
{
std::cerr << "Server started\n";
struct epoll_event events[MAX_EVENTS];
int nfds;
int i;
int count_loop = 0;
g_run = true;
while (g_run)
{
std::cerr << ++count_loop << "----loop epoll()\n";
nfds = ::epoll_wait(_epfd, events, MAX_EVENTS, TIMEOUT);
if (nfds == -1)
{
std::perror("err epoll_wait(): ");
throw std::runtime_error("Epoll wait");
}
else if (nfds == 0)
{
if (!_clients.empty())
{
std::cerr << "Timeout " << TIMEOUT << "ms\n";
_close_all_clients();
}
}
i = 0;
while (i < nfds)
{
// if ((events[i].data.u32 == SERVER_FD) && (events[i].events & EPOLLIN)) // Dont work, see "SERVER_FD" define
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(static_cast<Client*>(events[i].data.ptr));
else if (events[i].events & EPOLLOUT)
_send_response(static_cast<Client*>(events[i].data.ptr));
++i;
_actual_client = NULL;
}
}
}
///////////////////////
// Private Functions //
void Webserv::_accept_connection(int fd)
{
struct sockaddr_in addr;
socklen_t addr_len;
int accepted_fd;
std::cerr << "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);
_clients.push_back(Client());
_clients.back().fd = accepted_fd;
_epoll_update(accepted_fd, EPOLLIN, EPOLL_CTL_ADD, &_clients.back());
}
//////////
// READ //
void Webserv::_read_request(Client *client)
{
char buf[BUFSIZE+1];
ssize_t ret;
_actual_client = client;
std::cerr << "recv()\n";
ret = ::recv(client->fd, buf, BUFSIZE, 0);
if (ret == -1)
{
std::perror("err recv(): ");
// if (g_last_signal)
// _handle_last_signal();
// else
// _close_client(client->fd);
std::cerr << "client ptr =" << client << "\n"; // DEBUG
std::cerr << "client.fd =" << client->fd << "\n"; // DEBUG
return ;
}
/*
if (ret == BUFSIZE)
// send error like "request too long" to client
*/
buf[ret] = '\0';
client->raw_request.append(buf);
// HUGO TMP
//
_parse_request(client);
//
// HUGO TMP END
_epoll_update(client->fd, EPOLLOUT, EPOLL_CTL_MOD, client);
}
///////////
// WRITE //
void Webserv::_send_response(Client *client)
{
ssize_t ret;
_actual_client = client;
std::cerr << "send()\n";
std::cerr << "RAW_REQUEST\n|\n" << client->raw_request << "|\n";
// TMP HUGO test cgi
//
// if "POST" found in _buf, execve a cgi
if (client->raw_request.find("POST") != std::string::npos)
_exec_cgi_script(client);
// if "index.html" found in _buf, send the page
if (client->raw_request.find("index.html") != std::string::npos)
_serve_file(client, "index.html");
//
// TMP HUGO end test cgi
ret = ::send(client->fd, MSG_TEST, sizeof MSG_TEST - 1, 0);
if (ret == -1)
{
std::perror("err send(): ");
if (g_last_signal)
_handle_last_signal();
// else
// _close_client(client->fd);
return ;
}
_close_client(client->fd);
// if (client->raw_request.find("Connection: keep-alive") == std::string::npos)
// _close_client(client->fd);
// else
// _epoll_update(client->fd, EPOLLIN, EPOLL_CTL_MOD, client);
client->raw_request.clear();
}
////////////////////
// Misc functions //
int Webserv::_epoll_update(int fd, uint32_t events, int op)
{
struct epoll_event ev;
std::memset(&ev, 0, sizeof ev);
ev.events = events;
ev.data.fd = fd;
if (::epoll_ctl(_epfd, op, fd, &ev) == -1)
{
std::perror("err _epoll_update(): ");
return (-1);
}
return (0);
}
// TMP HUGO
//
void _parse_request(Client *client)
{
std::string request = client->raw_request;
std::map<std::string, std::string> field = client->request;
// size_t size = request.size();
size_t begin = 0;
size_t end;
size_t len;
// std::string str ("test un: deux\ntest deux: trois quatre\ntest :trois quatre cinq");
// std::string sub;
std::cout << str << "\n\n";
int i = 0;
while (end != std::string::npos)
{
// find first portion, before ':'
end = str.find(':', begin);
len = end - begin;
if (end == std::string::npos)
len = end;
sub = str.substr(begin, len);
std::cout << i << "|" << sub << "\n";
// std::cout << "[begin:" << begin << " - end:" << end << " - len:" << len << "] " << sub << "\n";
begin = end + 1;
// find second portion, until '\n'
end = str.find('\n', begin);
len = end - begin;
if (end == std::string::npos)
len = end;
sub = str.substr(begin, len);
std::cout << i << "|" << sub << "\n";
begin = end + 1;
i++;
}
// for (size_t i = 0; i < size; i++)
// {
// field.insert(request);
// }
// GET /home.html HTTP/1.1
// Host: developer.mozilla.org
// User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:50.0) Gecko/20100101 Firefox/50.0
// Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
// Accept-Language: en-US,en;q=0.5
// Accept-Encoding: gzip, deflate, br
// Referer: https://developer.mozilla.org/testpage.html
// Connection: keep-alive
// Upgrade-Insecure-Requests: 1
// If-Modified-Since: Mon, 18 Jul 2016 02:36:04 GMT
// If-None-Match: "c561c68d0ba92bbeb8b0fff2a9199f722e3a621a"
// Cache-Control: max-age=0
}
void Webserv::_serve_file(Client *client, std::string page)
{
int page_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\n";
to_send += "Content-Type: text/html;\n";
to_send += "Content-Length: ";
page_fd = open(page.c_str(), O_RDONLY);
for (int ret = 1; ret > 0;)
{
ret = read(page_fd, buffer, 1);
body += buffer;
}
strs << body.size();
to_send += strs.str();
to_send += end_header;
to_send += body;
if (::send(client->fd, to_send.c_str(), to_send.size(), 0) == -1)
std::perror("err send()");
}
void Webserv::_exec_cgi_script(Client *client)
{
int save_stdout;
char** env = new char*[5];
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] = strdup("CONTENT_LENGTH=665");
env[4] = NULL;
// save STDOUT
save_stdout = dup(STDOUT_FILENO);
// inside child process
if (fork() == 0)
{
dup2(client->fd, STDOUT_FILENO);
// execve("./srcs/cgi-bin/cgi_cpp.cgi", nll, env);
execve("./srcs/cgi-bin/php-cgi", nll, env);
}
// inside parent process
else
waitpid(-1, NULL, 0);
// restore stdout
dup2(save_stdout, STDOUT_FILENO);
}
//
// END TMP HUGO
int Webserv::_epoll_update(int fd, uint32_t events, int op, void *ptr)
{
struct epoll_event ev;
std::memset(&ev, 0, sizeof ev);
ev.events = events;
ev.data.ptr = ptr;
if (::epoll_ctl(_epfd, op, fd, &ev) == -1)
{
std::perror("err _epoll_update(): ");
return (-1);
}
return (0);
}
void Webserv::_handle_last_signal()
{
if (g_last_signal == SIGPIPE)
{
std::cerr << "SIGPIPE\n";
if (_actual_client)
{
_close_client(_actual_client->fd);
_actual_client = NULL;
}
}
else if (g_last_signal == SIGINT)
{
g_run = false;
}
g_last_signal = 0;
}
void Webserv::_close_client(int fd)
{
std::vector<Client>::iterator it = _clients.begin();
while (it != _clients.end())
{
if (it->fd == fd)
{
// _epoll_update(fd, 0, EPOLL_CTL_DEL); // normalement superflu, DEBUG
if (::close(fd) == -1)
std::perror("err close(): ");
else
std::cerr << "close fd " << fd << "\n";
_clients.erase(it);
break;
}
++it;
}
}
void Webserv::_close_all_clients()
{
while (!_clients.empty())
{
// _epoll_update(_clients.back().fd, 0, EPOLL_CTL_DEL); // normalement superflu, DEBUG
if (::close(_clients.back().fd) == -1)
std::perror("err close(): ");
else
std::cerr << "close fd " << _clients.back().fd << "\n";
_clients.pop_back();
}
}
////////////////////
// Init 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");
}
}