#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"); } // 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 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(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::_serve_file(int fd, 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(fd, to_send.c_str(), to_send.size(), 0) == -1) std::perror("err send()"); } void Webserv::_exec_cgi_script(int fd) { 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(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); } void Webserv::_send_response(int fd) { // TMP HUGO test cgi // if "POST" found in _buf, execve a cgi if (std::string(_buf).find("POST") != std::string::npos) _exec_cgi_script(fd); // if "index.html" found in _buf, send the page if (std::string(_buf).find("index.html") != std::string::npos) _serve_file(fd, "index.html"); // TMP HUGO end test cgi std::cout << "send()\n"; std::cout << "buf size: " << strlen(_buf) << "\n"; std::cout << "_buf: " << _buf << "\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); }