wip vector version
This commit is contained in:
4
Makefile
4
Makefile
@@ -45,11 +45,11 @@ HEADERS = Webserv.hpp
|
|||||||
DEPENDENCIES = $(HEADERS:%=$(HEADERS_D)/%)
|
DEPENDENCIES = $(HEADERS:%=$(HEADERS_D)/%)
|
||||||
|
|
||||||
SRCS = $(MAIN) Webserv.cpp
|
SRCS = $(MAIN) Webserv.cpp
|
||||||
#MAIN = main.cpp
|
MAIN = main.cpp
|
||||||
#MAIN = main_luke.cpp
|
#MAIN = main_luke.cpp
|
||||||
#MAIN = main_hugo.cpp
|
#MAIN = main_hugo.cpp
|
||||||
#MAIN = main_poll.cpp
|
#MAIN = main_poll.cpp
|
||||||
MAIN = main_select.cpp
|
#MAIN = main_select.cpp
|
||||||
|
|
||||||
DIR_OBJS = builds
|
DIR_OBJS = builds
|
||||||
OBJS = $(SRCS:%.cpp=$(DIR_OBJS)/%.o)
|
OBJS = $(SRCS:%.cpp=$(DIR_OBJS)/%.o)
|
||||||
|
|||||||
214
srcs/Webserv.cpp
214
srcs/Webserv.cpp
@@ -7,7 +7,7 @@ Webserv::Webserv()
|
|||||||
|
|
||||||
std::cout << "Server init\n";
|
std::cout << "Server init\n";
|
||||||
|
|
||||||
//_socket_fd = ::socket(AF_INET, SOCK_STREAM, 0);
|
// create socket descriptor
|
||||||
_socket_fd = ::socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
|
_socket_fd = ::socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
|
||||||
if (_socket_fd == -1)
|
if (_socket_fd == -1)
|
||||||
{
|
{
|
||||||
@@ -24,32 +24,18 @@ Webserv::Webserv()
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Webserv::Webserv(Webserv const &src)
|
|
||||||
//{
|
|
||||||
//
|
|
||||||
//}
|
|
||||||
|
|
||||||
Webserv::~Webserv()
|
Webserv::~Webserv()
|
||||||
{
|
{
|
||||||
std::cout << "Server destroyed\n";
|
std::cout << "Server destroyed\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
//Webserv & Webserv::operator=(Webserv const &rhs)
|
|
||||||
//{
|
|
||||||
//
|
|
||||||
//}
|
|
||||||
|
|
||||||
|
|
||||||
///////////////
|
|
||||||
// Functions //
|
|
||||||
|
|
||||||
void Webserv::bind(in_port_t port)
|
void Webserv::bind(in_port_t port)
|
||||||
{
|
{
|
||||||
struct sockaddr_in addr;
|
struct sockaddr_in addr;
|
||||||
addr.sin_family = AF_INET;
|
|
||||||
addr.sin_port = ::htons(port);
|
|
||||||
addr.sin_addr.s_addr = ::htonl(INADDR_ANY);
|
|
||||||
|
|
||||||
|
addr.sin_family = AF_INET;
|
||||||
|
addr.sin_port = ::htons(port);
|
||||||
|
addr.sin_addr.s_addr = ::htonl(INADDR_ANY);
|
||||||
if (::bind(_socket_fd, (const sockaddr*)&addr, sizeof addr) == -1)
|
if (::bind(_socket_fd, (const sockaddr*)&addr, sizeof addr) == -1)
|
||||||
{
|
{
|
||||||
::perror("err bind(): ");
|
::perror("err bind(): ");
|
||||||
@@ -69,83 +55,70 @@ void Webserv::listen(unsigned int max_connections)
|
|||||||
#define BUFSIZE 8192
|
#define BUFSIZE 8192
|
||||||
#define MSG_TEST "Le Webserv / 20 =D\n"
|
#define MSG_TEST "Le Webserv / 20 =D\n"
|
||||||
#define MSG_BOUNCE "bounced properly ;)\n" // placeholder
|
#define MSG_BOUNCE "bounced properly ;)\n" // placeholder
|
||||||
#define TRUE 1
|
|
||||||
#define FALSE 0
|
|
||||||
|
|
||||||
void Webserv::start()
|
void Webserv::_add_fd(int sd, short event)
|
||||||
{
|
{
|
||||||
int len, rc;
|
struct pollfd new_poll_fd;
|
||||||
int listen_sd = -1, new_sd = -1;
|
|
||||||
int end_server = FALSE, compress_array = FALSE;
|
|
||||||
int close_conn;
|
|
||||||
char buffer[80];
|
|
||||||
// struct sockaddr_in addr;
|
|
||||||
struct pollfd fds[200];
|
|
||||||
int nfds = 1, current_size = 0, i, j;
|
|
||||||
|
|
||||||
memset(fds, 0 , sizeof(fds));
|
new_poll_fd.fd = sd;
|
||||||
fds[0].fd = listen_sd;
|
new_poll_fd.events = event;
|
||||||
fds[0].events = POLLIN;
|
new_poll_fd.revents = 0;
|
||||||
|
_fds.push_back(new_poll_fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
//void Webserv::_accept_sockets(int sd, short event)
|
||||||
|
//{
|
||||||
|
//}
|
||||||
|
|
||||||
|
void Webserv::start(int timeout)
|
||||||
|
{
|
||||||
|
int len;
|
||||||
|
int rc;
|
||||||
|
int new_sd = -1;
|
||||||
|
char buffer[80];
|
||||||
|
int current_size = 0;
|
||||||
|
int end_server = FALSE;
|
||||||
|
int compress_array = FALSE;
|
||||||
|
int close_conn;
|
||||||
|
struct pollfd * fds_ptr;
|
||||||
|
|
||||||
|
// init first struct
|
||||||
|
_add_fd(_socket_fd, POLLIN);
|
||||||
|
// init pointer on first element of vector of struct pollfd
|
||||||
|
fds_ptr = &_fds[0];
|
||||||
|
|
||||||
std::cout << "Server started\n";
|
std::cout << "Server started\n";
|
||||||
while (end_server == FALSE)
|
while (end_server == FALSE)
|
||||||
{
|
{
|
||||||
// ***********************************************************
|
_nfds = _fds.size();
|
||||||
// * Call poll() *
|
poll(fds_ptr, _nfds, timeout);
|
||||||
// ***********************************************************
|
|
||||||
poll(fds, nfds, -1);
|
|
||||||
|
|
||||||
// ***********************************************************
|
current_size = _nfds;
|
||||||
// * One or more descriptors are readable. Need to *
|
for (int i = 0; i < current_size; i++)
|
||||||
// * determine which ones they are. *
|
|
||||||
// ***********************************************************
|
|
||||||
current_size = nfds;
|
|
||||||
for (i = 0; i < current_size; i++)
|
|
||||||
{
|
{
|
||||||
// *********************************************************
|
if(_fds[i].revents == 0)
|
||||||
// * Loop through to find the descriptors that returned *
|
|
||||||
// * POLLIN and determine whether it's the listening *
|
|
||||||
// * or the active connection. *
|
|
||||||
// *********************************************************
|
|
||||||
if(fds[i].revents == 0)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
// *********************************************************
|
|
||||||
// * If revents is not POLLIN, it's an unexpected result, *
|
|
||||||
// * log and end the server. *
|
|
||||||
// *********************************************************
|
|
||||||
if(fds[i].revents != POLLIN)
|
|
||||||
{
|
{
|
||||||
printf(" Error! revents = %d\n", fds[i].revents);
|
std::cout << "here\n";
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(_fds[i].revents != POLLIN)
|
||||||
|
{
|
||||||
|
printf(" Error! revents = %d\n", _fds[i].revents);
|
||||||
end_server = TRUE;
|
end_server = TRUE;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
if (fds[i].fd == listen_sd)
|
|
||||||
{
|
|
||||||
// *******************************************************
|
|
||||||
// * Listening descriptor is readable. *
|
|
||||||
// *******************************************************
|
|
||||||
printf(" Listening socket is readable\n");
|
|
||||||
|
|
||||||
// *******************************************************
|
if (_fds[i].fd == _socket_fd)
|
||||||
// * Accept all incoming connections that are *
|
{
|
||||||
// * queued up on the listening socket before we *
|
printf(" Listening socket is readable\n");
|
||||||
// * loop back and call poll again. *
|
|
||||||
// *******************************************************
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
// *****************************************************
|
new_sd = accept(_socket_fd, NULL, NULL);
|
||||||
// * Accept each incoming connection. If *
|
|
||||||
// * accept fails with EWOULDBLOCK, then we *
|
|
||||||
// * have accepted all of them. Any other *
|
|
||||||
// * failure on accept will cause us to end the *
|
|
||||||
// * server. *
|
|
||||||
// *****************************************************
|
|
||||||
new_sd = accept(listen_sd, NULL, NULL);
|
|
||||||
if (new_sd < 0)
|
if (new_sd < 0)
|
||||||
{
|
{
|
||||||
if (errno != EWOULDBLOCK)
|
printf(" new_sd < 0, = %i\n", new_sd);
|
||||||
|
if (errno != EWOULDBLOCK && errno != EAGAIN)
|
||||||
{
|
{
|
||||||
perror(" accept() failed");
|
perror(" accept() failed");
|
||||||
end_server = TRUE;
|
end_server = TRUE;
|
||||||
@@ -153,45 +126,19 @@ void Webserv::start()
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// *****************************************************
|
|
||||||
// * Add the new incoming connection to the *
|
|
||||||
// * pollfd structure *
|
|
||||||
// *****************************************************
|
|
||||||
printf(" New incoming connection - %d\n", new_sd);
|
printf(" New incoming connection - %d\n", new_sd);
|
||||||
fds[nfds].fd = new_sd;
|
_add_fd(new_sd, POLLIN);
|
||||||
fds[nfds].events = POLLIN;
|
|
||||||
nfds++;
|
|
||||||
|
|
||||||
// *****************************************************
|
|
||||||
// * Loop back up and accept another incoming *
|
|
||||||
// * connection *
|
|
||||||
// *****************************************************
|
|
||||||
} while (new_sd != -1);
|
} while (new_sd != -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// *********************************************************
|
|
||||||
// * This is not the listening socket, therefore an *
|
|
||||||
// * existing connection must be readable *
|
|
||||||
// *********************************************************
|
|
||||||
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
printf(" Descriptor %d is readable\n", fds[i].fd);
|
printf(" Descriptor %d is readable\n", _fds[i].fd);
|
||||||
close_conn = FALSE;
|
close_conn = FALSE;
|
||||||
// *******************************************************
|
|
||||||
// * Receive all incoming data on this socket *
|
|
||||||
// * before we loop back and call poll again. *
|
|
||||||
// *******************************************************
|
|
||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
// *****************************************************
|
rc = recv(_fds[i].fd, buffer, sizeof(buffer), 0);
|
||||||
// * Receive data on this connection until the *
|
|
||||||
// * recv fails with EWOULDBLOCK. If any other *
|
|
||||||
// * failure occurs, we will close the *
|
|
||||||
// * connection. *
|
|
||||||
// *****************************************************
|
|
||||||
rc = recv(fds[i].fd, buffer, sizeof(buffer), 0);
|
|
||||||
if (rc < 0)
|
if (rc < 0)
|
||||||
{
|
{
|
||||||
if (errno != EWOULDBLOCK)
|
if (errno != EWOULDBLOCK)
|
||||||
@@ -202,10 +149,6 @@ void Webserv::start()
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// *****************************************************
|
|
||||||
// * Check to see if the connection has been *
|
|
||||||
// * closed by the client *
|
|
||||||
// *****************************************************
|
|
||||||
if (rc == 0)
|
if (rc == 0)
|
||||||
{
|
{
|
||||||
printf(" Connection closed\n");
|
printf(" Connection closed\n");
|
||||||
@@ -213,16 +156,10 @@ void Webserv::start()
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// *****************************************************
|
|
||||||
// * Data was received *
|
|
||||||
// *****************************************************
|
|
||||||
len = rc;
|
len = rc;
|
||||||
printf(" %d bytes received\n", len);
|
printf(" %d bytes received\n", len);
|
||||||
|
|
||||||
// *****************************************************
|
rc = send(_fds[i].fd, buffer, len, 0);
|
||||||
// * Echo the data back to the client *
|
|
||||||
// *****************************************************
|
|
||||||
rc = send(fds[i].fd, buffer, len, 0);
|
|
||||||
if (rc < 0)
|
if (rc < 0)
|
||||||
{
|
{
|
||||||
perror(" send() failed");
|
perror(" send() failed");
|
||||||
@@ -232,48 +169,37 @@ void Webserv::start()
|
|||||||
|
|
||||||
} while(TRUE);
|
} while(TRUE);
|
||||||
|
|
||||||
// *******************************************************
|
|
||||||
// * If the close_conn flag was turned on, we need *
|
|
||||||
// * to clean up this active connection. This *
|
|
||||||
// * clean up process includes removing the *
|
|
||||||
// * descriptor. *
|
|
||||||
// *******************************************************
|
|
||||||
if (close_conn)
|
if (close_conn)
|
||||||
{
|
{
|
||||||
close(fds[i].fd);
|
close(_fds[i].fd);
|
||||||
fds[i].fd = -1;
|
_fds[i].fd = -1;
|
||||||
compress_array = TRUE;
|
compress_array = TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
} // End of existing connection is readable
|
} // else
|
||||||
} // End of loop through pollable descriptors
|
} // for (i = 0; i < current_size; i++)
|
||||||
|
|
||||||
// ***********************************************************
|
|
||||||
// * If the compress_array flag was turned on, we need *
|
|
||||||
// * to squeeze together the array and decrement the number *
|
|
||||||
// * of file descriptors. We do not need to move back the *
|
|
||||||
// * events and revents fields because the events will always*
|
|
||||||
// * be POLLIN in this case, and revents is output. *
|
|
||||||
// ***********************************************************
|
|
||||||
if (compress_array)
|
if (compress_array)
|
||||||
{
|
{
|
||||||
compress_array = FALSE;
|
compress_array = FALSE;
|
||||||
for (i = 0; i < nfds; i++)
|
for (int i = 0; i < _nfds; i++)
|
||||||
{
|
{
|
||||||
if (fds[i].fd == -1)
|
if (_fds[i].fd == -1)
|
||||||
{
|
{
|
||||||
for(j = i; j < nfds; j++)
|
// for(int j = i; j < nfds; j++)
|
||||||
{
|
// {
|
||||||
fds[j].fd = fds[j+1].fd;
|
// fds[j].fd = fds[j+1].fd;
|
||||||
}
|
// }
|
||||||
|
_fds.erase(_fds.begin() + i);
|
||||||
i--;
|
i--;
|
||||||
nfds--;
|
// nfds--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
} // while (end_server == FALSE)
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
||||||
@@ -322,5 +248,3 @@ void Webserv::start()
|
|||||||
|
|
||||||
::close(accepted_fd);
|
::close(accepted_fd);
|
||||||
*/
|
*/
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -4,19 +4,9 @@
|
|||||||
|
|
||||||
# include <string>
|
# include <string>
|
||||||
# include <map>
|
# include <map>
|
||||||
|
# include <vector>
|
||||||
# include <exception>
|
# include <exception>
|
||||||
# include <stdexcept>
|
# include <stdexcept>
|
||||||
# include <unistd.h> // close
|
|
||||||
# include <iostream> // cout, cin
|
|
||||||
|
|
||||||
# include <sys/socket.h> // socket, accept, listen, send, recv, bind, connect, setsockopt, getsockname
|
|
||||||
# include <netinet/in.h> // sockaddr_in
|
|
||||||
// # include <netinet/ip.h> // usefull for what ?
|
|
||||||
# include <arpa/inet.h> // htonl, htons, ntohl, ntohs, inet_addr
|
|
||||||
|
|
||||||
# include <poll.h> // poll
|
|
||||||
# include <fcntl.h> // fcntl
|
|
||||||
|
|
||||||
# include <unistd.h> // close
|
# include <unistd.h> // close
|
||||||
# include <stdlib.h> // exit
|
# include <stdlib.h> // exit
|
||||||
# include <iostream> // cout, cin
|
# include <iostream> // cout, cin
|
||||||
@@ -30,6 +20,9 @@
|
|||||||
# include <fcntl.h> // fcntl
|
# include <fcntl.h> // fcntl
|
||||||
# include <sys/ioctl.h> // ioctl
|
# include <sys/ioctl.h> // ioctl
|
||||||
|
|
||||||
|
# define TRUE 1
|
||||||
|
# define FALSE 0
|
||||||
|
|
||||||
class Webserv
|
class Webserv
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -40,13 +33,17 @@ class Webserv
|
|||||||
|
|
||||||
void bind(in_port_t port);
|
void bind(in_port_t port);
|
||||||
void listen(unsigned int max_connections);
|
void listen(unsigned int max_connections);
|
||||||
void start();
|
void start(int timeout);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int _socket_fd;
|
int _socket_fd;
|
||||||
std::map<std::string, std::string> _request;
|
std::map<std::string, std::string> _request;
|
||||||
std::map<std::string, std::string> _response;
|
std::map<std::string, std::string> _response;
|
||||||
|
|
||||||
|
std::vector<struct pollfd> _fds;
|
||||||
|
int _nfds;
|
||||||
|
void _add_fd(int sd, short event);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ int main(void)
|
|||||||
// https://security.stackexchange.com/questions/169213/how-to-chose-a-port-to-run-an-application-on-localhost
|
// https://security.stackexchange.com/questions/169213/how-to-chose-a-port-to-run-an-application-on-localhost
|
||||||
serv.bind(4040);
|
serv.bind(4040);
|
||||||
serv.listen(20);
|
serv.listen(20);
|
||||||
serv.start();
|
serv.start(3 * 60 * 1000);
|
||||||
}
|
}
|
||||||
catch (std::exception& e)
|
catch (std::exception& e)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user