From dcf94c2c6156bc5e53b71bc0d99fb6aadfd7e81b Mon Sep 17 00:00:00 2001 From: LuckyLaszlo Date: Mon, 25 Jul 2022 05:28:44 +0200 Subject: [PATCH 1/3] Client struct, signal wip, refactoring --- Makefile | 2 +- srcs/Client.hpp | 27 +++++ srcs/Server.hpp | 32 ++++++ srcs/Webserv.cpp | 262 +++++++++++++++++++++++++++++++++-------------- srcs/Webserv.hpp | 59 ++++++++--- srcs/main.cpp | 2 +- 6 files changed, 289 insertions(+), 95 deletions(-) create mode 100644 srcs/Client.hpp create mode 100644 srcs/Server.hpp diff --git a/Makefile b/Makefile index 1666787..a93ff01 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ VPATH = $(DIR_SRCS) DIR_SRCS = srcs HEADERS_D = ./srcs -HEADERS = Webserv.hpp +HEADERS = Webserv.hpp Client.hpp Server.hpp DEPENDENCIES = $(HEADERS:%=$(HEADERS_D)/%) diff --git a/srcs/Client.hpp b/srcs/Client.hpp new file mode 100644 index 0000000..9092f31 --- /dev/null +++ b/srcs/Client.hpp @@ -0,0 +1,27 @@ + +#ifndef CLIENT_HPP +# define CLIENT_HPP + +# include +# include +# include + +class Client +{ + public: + // Client(Placeholder); + // Client(); + // Client(Client const &src); + // ~Client(); + // Client &operator=(Client const &rhs); + + int fd; + std::string raw_request; + std::map request; + std::map response; + + private: + +}; + +#endif \ No newline at end of file diff --git a/srcs/Server.hpp b/srcs/Server.hpp new file mode 100644 index 0000000..a9063d1 --- /dev/null +++ b/srcs/Server.hpp @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Server.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lperrey +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/07/24 12:11:07 by lperrey #+# #+# */ +/* Updated: 2022/07/24 12:11:13 by lperrey ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef SERVER_HPP +# define SERVER_HPP + +# include +# include + +class Server +{ + public: + // Server(Placeholder); + // Server(); + // Server(Server const &src); + // ~Server(); + // Server &operator=(Server const &rhs); + + private: + +}; + +#endif \ No newline at end of file diff --git a/srcs/Webserv.cpp b/srcs/Webserv.cpp index 92a85c0..8dc2cd3 100644 --- a/srcs/Webserv.cpp +++ b/srcs/Webserv.cpp @@ -1,9 +1,15 @@ #include "Webserv.hpp" +int g_last_signal; +void signal_handler(int signum) +{ + g_last_signal = signum; +} + Webserv::Webserv() { - std::cout << "Server init\n"; + std::cerr << "Server init\n"; _epfd = ::epoll_create1(0); // (EPOLL_CLOEXEC) for CGI fork ? if (_epfd == -1) @@ -11,6 +17,9 @@ Webserv::Webserv() 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) @@ -22,7 +31,8 @@ Webserv::~Webserv() { close(_socket_fd); close(_epfd); - std::cout << "Server destroyed\n"; + _close_all_clients(); + std::cerr << "Server destroyed\n"; } /* Webserv & Webserv::operator=(Webserv const &rhs) @@ -45,45 +55,44 @@ void Webserv::init_virtual_servers() // ADD config param _bind(_socket_fd, 80); _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(): "); + if (_epoll_update(_socket_fd, EPOLLIN, EPOLL_CTL_ADD) == -1) throw std::runtime_error("Socket init"); - } } void Webserv::start() { - std::cout << "Server started\n"; + std::cerr << "Server started\n"; struct epoll_event events[MAX_EVENTS]; int nfds; int i; int count_loop = 0; - std::cout << ++count_loop << "----loop epoll()\n"; + std::cerr << ++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 + 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(events[i].data.fd); + _read_request(static_cast(events[i].data.ptr)); else if (events[i].events & EPOLLOUT) - _send_response(events[i].data.fd); + _send_response(static_cast(events[i].data.ptr)); ++i; + _actual_client = NULL; } - std::cout << ++count_loop << "----loop epoll()\n"; + std::cerr << ++count_loop << "----loop epoll()\n"; } if (nfds == -1) @@ -97,6 +106,164 @@ void Webserv::start() /////////////////////// // 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(): "); + return; + } + /* + if (ret == BUFSIZE) + // send error like "request too long" to client + */ + + buf[ret] = '\0'; + client->raw_request.append(buf); + + _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"; + + 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(); + return ; + } + + // 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); +} + +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_last_signal = 0; +} + +void Webserv::_close_client(int fd) +{ + std::vector::iterator it = _clients.begin(); + while (it != _clients.end()) + { + if (it->fd == fd) + { + 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()) + { + 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 ? @@ -123,64 +290,3 @@ void Webserv::_listen(int socket_fd, unsigned int max_connections) 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) -{ - 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); -} diff --git a/srcs/Webserv.hpp b/srcs/Webserv.hpp index c9acb5f..b1fc02f 100644 --- a/srcs/Webserv.hpp +++ b/srcs/Webserv.hpp @@ -2,14 +2,15 @@ #ifndef WEBSERV_HPP # define WEBSERV_HPP +# include // cout, cin # include # include +# include # include // errno # include // perror # include # include # include // close -# include // cout, cin # include // memset # include // socket, accept, listen, send, recv, bind, connect, setsockopt, getsockname @@ -20,11 +21,33 @@ # include // epoll # include // fcntl -#define BUFSIZE 8192 -#define TIMEOUT 3000 -#define MAX_EVENTS 42 // arbitrary -#define MSG_TEST "Le Webserv / 20 =D\n" -#define MSG_BOUNCE "bounced properly ;)\n" // placeholder +# include "Client.hpp" +# include "Server.hpp" +# include // signal + +# define BUFSIZE 8192 +# define TIMEOUT 3000 +# define MAX_EVENTS 42 // arbitrary +# define MSG_TEST "Le Webserv / 20 =D\n" + + +// ev.data.u32 filled with tag constant "SERVER_FD/CLIENT_FD" +// dont work because ev.data is an "union", +// so we can only use one variable (fd, ptr, u32 or u64) + // ev.data.u32 = SERVER_FD; + +/* enum // WIP test +{ + SERVER_FD = 1, + CLIENT_FD +}; + +struct s // WIP test +{ + int fd; + Client *ptr; +}; + */ class Webserv { @@ -38,20 +61,26 @@ class Webserv void start(); private: - int _socket_fd; // TODO: replace with vector of "Server" struct int _epfd; + int _socket_fd; // temp, to replace with std::vector + // std::vector _servers; + std::vector _clients; - // WIP global buffer. Need one variable set per "Client" - char _buf[BUFSIZE+1]; - ssize_t _read_ret; - std::map _request; - std::map _response; + void _accept_connection(int fd); + void _read_request(Client *client); + void _send_response(Client *client); + + int _epoll_update(int fd, uint32_t events, int op); + int _epoll_update(int fd, uint32_t events, int op, void *ptr); + + void _handle_last_signal(); + // void _signal_handler(int signum); // invalide dans une class + Client* _actual_client; + void _close_client(int fd); + void _close_all_clients(); void _bind(int socket_fd, in_port_t port); void _listen(int socket_fd, unsigned int max_connections); - void _accept_connection(int fd); - void _read_request(int fd); - void _send_response(int fd); }; #endif diff --git a/srcs/main.cpp b/srcs/main.cpp index 5121ac7..c577bde 100644 --- a/srcs/main.cpp +++ b/srcs/main.cpp @@ -2,7 +2,7 @@ #include #include #include -#include +#include "Webserv.hpp" int main(void) { From 26dad07a87e9c1986a8814ffcacf76c6b377f68c Mon Sep 17 00:00:00 2001 From: LuckyLaszlo Date: Mon, 25 Jul 2022 07:40:41 +0200 Subject: [PATCH 2/3] SIGINT handling + bug with persistent connection, recv() try to read fd 0 (why?) --- srcs/Webserv.cpp | 50 +++++++++++++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/srcs/Webserv.cpp b/srcs/Webserv.cpp index 8dc2cd3..eaf285d 100644 --- a/srcs/Webserv.cpp +++ b/srcs/Webserv.cpp @@ -2,6 +2,7 @@ #include "Webserv.hpp" int g_last_signal; +bool g_run; void signal_handler(int signum) { g_last_signal = signum; @@ -19,7 +20,7 @@ Webserv::Webserv() } std::signal(SIGPIPE, signal_handler); - // std::signal(SIGINT, signal_handler); + std::signal(SIGINT, signal_handler); } /* Webserv::Webserv(Webserv const &src) @@ -66,11 +67,18 @@ void Webserv::start() int nfds; int i; int count_loop = 0; - std::cerr << ++count_loop << "----loop epoll()\n"; - while ( (nfds = ::epoll_wait(_epfd, events, MAX_EVENTS, TIMEOUT)) != -1) + g_run = true; + while (g_run) { - if (nfds == 0) + 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()) { @@ -78,7 +86,6 @@ void Webserv::start() _close_all_clients(); } } - i = 0; while (i < nfds) { @@ -92,14 +99,8 @@ void Webserv::start() ++i; _actual_client = NULL; } - std::cerr << ++count_loop << "----loop epoll()\n"; } - if (nfds == -1) - { - std::perror("err epoll_wait(): "); - throw std::runtime_error("Epoll wait"); - } } @@ -118,7 +119,7 @@ void Webserv::_accept_connection(int fd) if (accepted_fd == -1) { std::perror("err accept(): "); - return; + return ; } ::fcntl(accepted_fd, F_SETFL, O_NONBLOCK); @@ -141,7 +142,14 @@ void Webserv::_read_request(Client *client) if (ret == -1) { std::perror("err recv(): "); - return; + // 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) @@ -170,13 +178,16 @@ void Webserv::_send_response(Client *client) 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); + // _close_client(client->fd); // else - // _epoll_update(client->fd, EPOLLIN, EPOLL_CTL_MOD, client); + // _epoll_update(client->fd, EPOLLIN, EPOLL_CTL_MOD, client); client->raw_request.clear(); } @@ -224,9 +235,10 @@ void Webserv::_handle_last_signal() _actual_client = NULL; } } - // else if (g_last_signal == SIGINT) - // { - // } + else if (g_last_signal == SIGINT) + { + g_run = false; + } g_last_signal = 0; } @@ -237,6 +249,7 @@ void Webserv::_close_client(int fd) { if (it->fd == fd) { + // _epoll_update(fd, 0, EPOLL_CTL_DEL); // normalement superflu, DEBUG if (::close(fd) == -1) std::perror("err close(): "); else @@ -252,6 +265,7 @@ 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 From 04589e123b1d32a3817550e9bcba3ea7e59270b1 Mon Sep 17 00:00:00 2001 From: hugogogo Date: Fri, 29 Jul 2022 11:04:21 +0200 Subject: [PATCH 3/3] Makefile work with dependencies --- Makefile | 53 ++++++++++++++++++++++++++--------------------------- 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/Makefile b/Makefile index 1666787..af4cf75 100644 --- a/Makefile +++ b/Makefile @@ -1,26 +1,27 @@ -NAME = webserv -CXX = c++ +NAME = webserv +CXX = c++ -CXXFLAGS = -Wall -Wextra #-Werror -CXXFLAGS += -std=c++98 -CXXFLAGS += -I$(HEADERS_D) -CXXFLAGS += -g -#CXXFLAGS += -O3 +CXXFLAGS = -Wall -Wextra #-Werror +CXXFLAGS += $(HEADERS) +CXXFLAGS += -std=c++98 +CXXFLAGS += -g +CXXFLAGS += -MMD -MP #header dependencie +#CXXFLAGS += -O3 -#SHELL = /bin/zsh -VPATH = $(DIR_SRCS) -DIR_SRCS = srcs +#SHELL = /bin/zsh +VPATH = $(SRCS_D) -HEADERS_D = ./srcs -HEADERS = Webserv.hpp +HEADERS = $(HEADERS_D:%=-I%) +HEADERS_D = headers -DEPENDENCIES = $(HEADERS:%=$(HEADERS_D)/%) +SRCS_D = srcs +SRCS = main.cpp \ + Webserv.cpp -SRCS = main.cpp Webserv.cpp - -DIR_OBJS = builds -OBJS = $(SRCS:%.cpp=$(DIR_OBJS)/%.o) +OBJS_D = builds +OBJS = $(SRCS:%.cpp=$(OBJS_D)/%.o) +DEPS = $(OBJS:.o=.d) #header dependencie # -------------------- # ------ RULES ------- @@ -28,26 +29,24 @@ OBJS = $(SRCS:%.cpp=$(DIR_OBJS)/%.o) all: $(NAME) -$(DIR_OBJS)/%.o: %.cpp | $(DIR_OBJS) +$(OBJS_D)/%.o: %.cpp | $(OBJS_D) $(CXX) $(CXXFLAGS) -c $< -o $@ -$(DIR_OBJS): +$(OBJS_D): mkdir $@ -$(OBJS): $(DEPENDENCIES) -#$(OBJS): $(DEPENDENCIES) Makefile - -$(NAME) : $(OBJS) - $(CXX) $(OBJS) -o $(NAME) +$(NAME): $(OBJS) + $(CXX) $^ -o $(NAME) clean: - rm -f $(OBJS) + rm -rf $(OBJS_D) fclean: clean rm -f $(NAME) re: fclean all -#run: all +.PHONY : all clean fclean re + +-include $(DEPS) # header dependencie -.PHONY : all clean fclean re run