|
@ -1,17 +1,8 @@ |
|
|
|
|
|
|
|
|
#include "webserv.hpp" |
|
|
#include "webserv.hpp" |
|
|
|
|
|
|
|
|
Socket::Socket(Server *server, string def) : _server(server) { |
|
|
Socket::Socket(string def) { |
|
|
size_t sep_pos = def.rfind(':'); |
|
|
_listen = get_listen_t(def); |
|
|
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()); |
|
|
|
|
|
_clients_amount = 0; |
|
|
_clients_amount = 0; |
|
|
} |
|
|
} |
|
|
Socket::~Socket() { |
|
|
Socket::~Socket() { |
|
@ -21,6 +12,8 @@ Socket::~Socket() { |
|
|
|
|
|
|
|
|
int Socket::launch() { |
|
|
int Socket::launch() { |
|
|
int opt = 1; |
|
|
int opt = 1; |
|
|
|
|
|
string ip = _listen.ip; |
|
|
|
|
|
int port = _listen.port; |
|
|
_master_socket = socket(AF_INET, SOCK_STREAM, 0); |
|
|
_master_socket = socket(AF_INET, SOCK_STREAM, 0); |
|
|
if (_master_socket == 0) { |
|
|
if (_master_socket == 0) { |
|
|
cout << "Socket creation: " << strerror(errno) << "\n"; |
|
|
cout << "Socket creation: " << strerror(errno) << "\n"; |
|
@ -33,19 +26,19 @@ int Socket::launch() { |
|
|
return (EXIT_FAILURE); |
|
|
return (EXIT_FAILURE); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (_ip.at(0) == '[') |
|
|
if (ip.at(0) == '[') |
|
|
_address.sin_family = AF_INET6; |
|
|
_address.sin_family = AF_INET6; |
|
|
else |
|
|
else |
|
|
_address.sin_family = AF_INET; |
|
|
_address.sin_family = AF_INET; |
|
|
_address.sin_addr.s_addr = inet_addr(_ip.c_str()); |
|
|
_address.sin_addr.s_addr = inet_addr(ip.c_str()); |
|
|
_address.sin_port = htons(_port); |
|
|
_address.sin_port = htons(port); |
|
|
|
|
|
|
|
|
if (bind(_master_socket, (struct sockaddr *)&_address, sizeof(_address)) < |
|
|
if (bind(_master_socket, (struct sockaddr *)&_address, sizeof(_address)) < |
|
|
0) { |
|
|
0) { |
|
|
cout << "Bind: " << strerror(errno) << "\n"; |
|
|
cout << "Bind: " << strerror(errno) << "\n"; |
|
|
return (EXIT_FAILURE); |
|
|
return (EXIT_FAILURE); |
|
|
} |
|
|
} |
|
|
cout << "Listener " << _ip << " on port " << _port << "\n"; |
|
|
cout << "Listener " << ip << " on port " << port << "\n"; |
|
|
|
|
|
|
|
|
if (listen(_master_socket, 3) < 0) { |
|
|
if (listen(_master_socket, 3) < 0) { |
|
|
cout << "Listen: " << strerror(errno) << "\n"; |
|
|
cout << "Listen: " << strerror(errno) << "\n"; |
|
@ -69,7 +62,7 @@ void Socket::set_fds() { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void Socket::refresh() { |
|
|
void Socket::refresh(Env *env) { |
|
|
std::vector<int>::iterator it; |
|
|
std::vector<int>::iterator it; |
|
|
int valread; |
|
|
int valread; |
|
|
int addrlen = sizeof(_address); |
|
|
int addrlen = sizeof(_address); |
|
@ -101,21 +94,23 @@ void Socket::refresh() { |
|
|
_clients.erase(it); |
|
|
_clients.erase(it); |
|
|
} else { |
|
|
} else { |
|
|
buffer[valread] = '\0'; |
|
|
buffer[valread] = '\0'; |
|
|
answer(*it, buffer); |
|
|
answer(env, *it, buffer); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void Socket::answer(int fd, string request) { |
|
|
void Socket::answer(Env *env, int fd, string request) { |
|
|
string uri = split(request, ' ').at(1); |
|
|
std::vector<string> lines = split(request, '\n'); |
|
|
|
|
|
string uri = split(lines.at(0), ' ').at(1); |
|
|
cout << uri << "\n"; |
|
|
cout << uri << "\n"; |
|
|
|
|
|
|
|
|
cout << request << "\n|===|===|===|\n"; |
|
|
cout << request << "\n|===|===|===|\n"; |
|
|
std::stringstream answer; |
|
|
std::stringstream answer; |
|
|
answer << "HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: "; |
|
|
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); |
|
|
answer << route->getAutoindex(uri); |
|
|
cout << answer.str() << "\n|===|===|===|\n"; |
|
|
cout << answer.str() << "\n|===|===|===|\n"; |
|
|
send_answer(fd, answer.str()); |
|
|
send_answer(fd, answer.str()); |
|
|