|
|
@ -1,18 +1,9 @@ |
|
|
|
|
|
|
|
#include "webserv.hpp" |
|
|
|
|
|
|
|
Socket::Socket(Server *server, string def) : _server(server) { |
|
|
|
size_t sep_pos = def.rfind(':'); |
|
|
|
size_t ip6_endpos = def.rfind(']'); |
|
|
|
|
|
|
|
string tmp = def.substr(0, sep_pos); |
|
|
|
if (ip6_endpos > sep_pos) |
|
|
|
_ip = def; |
|
|
|
else |
|
|
|
_ip = isInt(tmp) || tmp == "localhost" ? "127.0.0.1" : tmp; |
|
|
|
tmp = def.substr(sep_pos + 1, def.length() - sep_pos - 1).c_str(); |
|
|
|
_port = !isInt(tmp) ? 80 : std::atoi(tmp.c_str()); |
|
|
|
Socket::Socket(listen_t listen) : _listen(listen) { |
|
|
|
_clients_amount = 0; |
|
|
|
_type = AF_INET; |
|
|
|
} |
|
|
|
Socket::~Socket() { |
|
|
|
close(_master_socket); |
|
|
@ -21,7 +12,15 @@ Socket::~Socket() { |
|
|
|
|
|
|
|
int Socket::launch() { |
|
|
|
int opt = 1; |
|
|
|
_master_socket = socket(AF_INET, SOCK_STREAM, 0); |
|
|
|
string ip = _listen.ip; |
|
|
|
int port = _listen.port; |
|
|
|
|
|
|
|
_type = AF_INET; |
|
|
|
_addr.sin_family = AF_INET; |
|
|
|
_addr.sin_addr.s_addr = inet_addr(ip.c_str()); |
|
|
|
_addr.sin_port = htons(port); |
|
|
|
_master_socket = socket(_addr.sin_family, SOCK_STREAM, 0); |
|
|
|
|
|
|
|
if (_master_socket == 0) { |
|
|
|
cout << "Socket creation: " << strerror(errno) << "\n"; |
|
|
|
return (EXIT_FAILURE); |
|
|
@ -33,19 +32,11 @@ int Socket::launch() { |
|
|
|
return (EXIT_FAILURE); |
|
|
|
} |
|
|
|
|
|
|
|
if (_ip.at(0) == '[') |
|
|
|
_address.sin_family = AF_INET6; |
|
|
|
else |
|
|
|
_address.sin_family = AF_INET; |
|
|
|
_address.sin_addr.s_addr = inet_addr(_ip.c_str()); |
|
|
|
_address.sin_port = htons(_port); |
|
|
|
|
|
|
|
if (bind(_master_socket, (struct sockaddr *)&_address, sizeof(_address)) < |
|
|
|
0) { |
|
|
|
if (bind(_master_socket, (struct sockaddr *)&_addr, sizeof(_addr)) < 0) { |
|
|
|
cout << "Bind: " << strerror(errno) << "\n"; |
|
|
|
return (EXIT_FAILURE); |
|
|
|
} |
|
|
|
cout << "Listener " << _ip << " on port " << _port << "\n"; |
|
|
|
cout << "Listener " << ip << " on port " << port << "\n"; |
|
|
|
|
|
|
|
if (listen(_master_socket, 3) < 0) { |
|
|
|
cout << "Listen: " << strerror(errno) << "\n"; |
|
|
@ -69,13 +60,13 @@ void Socket::set_fds() { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void Socket::refresh() { |
|
|
|
void Socket::refresh(Env *env) { |
|
|
|
std::vector<int>::iterator it; |
|
|
|
int valread; |
|
|
|
int addrlen = sizeof(_address); |
|
|
|
int addrlen = sizeof(_addr); |
|
|
|
char buffer[10000]; |
|
|
|
if (FD_ISSET(_master_socket, &_readfds)) { |
|
|
|
int new_socket = accept(_master_socket, (struct sockaddr *)&_address, |
|
|
|
int new_socket = accept(_master_socket, (struct sockaddr *)&_addr, |
|
|
|
(socklen_t *)&addrlen); |
|
|
|
if (new_socket < 0) { |
|
|
|
cout << "Accept: " << strerror(errno) << "\n"; |
|
|
@ -85,37 +76,39 @@ void Socket::refresh() { |
|
|
|
fcntl(new_socket, F_GETNOSIGPIPE); |
|
|
|
#endif |
|
|
|
cout << "New connection, socket fd is " << new_socket |
|
|
|
<< ", ip is : " << inet_ntoa(_address.sin_addr) |
|
|
|
<< ", port : " << ntohs(_address.sin_port) << "\n"; |
|
|
|
<< ", ip is : " << inet_ntoa(_addr.sin_addr) |
|
|
|
<< ", port : " << ntohs(_addr.sin_port) << "\n"; |
|
|
|
_clients.push_back(new_socket); |
|
|
|
} |
|
|
|
for (it = _clients.begin(); it < _clients.end(); it++) { |
|
|
|
if (FD_ISSET(*it, &_readfds)) { |
|
|
|
valread = read(*it, buffer, 10000); |
|
|
|
if (valread == 0) { |
|
|
|
getpeername(*it, (struct sockaddr *)&_address, |
|
|
|
getpeername(*it, (struct sockaddr *)&_addr, |
|
|
|
(socklen_t *)&addrlen); |
|
|
|
cout << "Host disconnected, ip " << inet_ntoa(_address.sin_addr) |
|
|
|
<< ", port " << ntohs(_address.sin_port) << "\n"; |
|
|
|
cout << "Host disconnected, ip " << inet_ntoa(_addr.sin_addr) |
|
|
|
<< ", port " << ntohs(_addr.sin_port) << "\n"; |
|
|
|
close(*it); |
|
|
|
_clients.erase(it); |
|
|
|
} else { |
|
|
|
buffer[valread] = '\0'; |
|
|
|
answer(*it, buffer); |
|
|
|
answer(env, *it, buffer); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void Socket::answer(int fd, string request) { |
|
|
|
string uri = split(request, ' ').at(1); |
|
|
|
void Socket::answer(Env *env, int fd, string request) { |
|
|
|
std::vector<string> lines = split(request, '\n'); |
|
|
|
string uri = split(lines.at(0), ' ').at(1); |
|
|
|
cout << uri << "\n"; |
|
|
|
|
|
|
|
cout << request << "\n|===|===|===|\n"; |
|
|
|
std::stringstream answer; |
|
|
|
answer << "HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: "; |
|
|
|
|
|
|
|
Route *route = _server->get_route(uri); |
|
|
|
Server *server = env->choose_server(this, split(lines.at(1), ' ').at(1)); |
|
|
|
Route *route = server->get_route(uri); |
|
|
|
answer << route->getAutoindex(uri); |
|
|
|
cout << answer.str() << "\n|===|===|===|\n"; |
|
|
|
send_answer(fd, answer.str()); |
|
|
|