|
|
@ -1,8 +1,8 @@ |
|
|
|
|
|
|
|
#include "webserv.hpp" |
|
|
|
|
|
|
|
Socket::Socket(listen_t listen) : _listen(listen) {} |
|
|
|
Socket::Socket(int fd, Socket *parent) : _fd(fd), _parent(parent) {} |
|
|
|
|
|
|
|
Socket::~Socket(void) { |
|
|
|
close(_fd); |
|
|
|
cout << "Socket destroyed!\n"; |
|
|
@ -18,8 +18,8 @@ int Socket::launch(void) { |
|
|
|
cout << "Socket creation: " << strerror(errno) << "\n"; |
|
|
|
return (EXIT_FAILURE); |
|
|
|
} |
|
|
|
int opt_ret = setsockopt(_fd, SOL_SOCKET, SO_REUSEADDR, |
|
|
|
(char *)&opt, sizeof(opt)); |
|
|
|
int opt_ret = |
|
|
|
setsockopt(_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt)); |
|
|
|
if (opt_ret < 0) { |
|
|
|
cout << "Sockopt: " << strerror(errno) << "\n"; |
|
|
|
return (EXIT_FAILURE); |
|
|
@ -29,8 +29,7 @@ int Socket::launch(void) { |
|
|
|
_address.sin_addr.s_addr = inet_addr(ip.c_str()); |
|
|
|
_address.sin_port = htons(port); |
|
|
|
|
|
|
|
if (bind(_fd, (struct sockaddr *)&_address, sizeof(_address)) < |
|
|
|
0) { |
|
|
|
if (bind(_fd, (struct sockaddr *)&_address, sizeof(_address)) < 0) { |
|
|
|
cout << "Bind: " << strerror(errno) << "\n"; |
|
|
|
return (EXIT_FAILURE); |
|
|
|
} |
|
|
@ -64,8 +63,8 @@ void Socket::refresh(Env *env) { |
|
|
|
int addrlen = sizeof(_address); |
|
|
|
char buffer[10000]; |
|
|
|
if (FD_ISSET(_fd, &_readfds)) { |
|
|
|
int new_socket = accept(_fd, (struct sockaddr *)&_address, |
|
|
|
(socklen_t *)&addrlen); |
|
|
|
int new_socket = |
|
|
|
accept(_fd, (struct sockaddr *)&_address, (socklen_t *)&addrlen); |
|
|
|
if (new_socket < 0) { |
|
|
|
cout << "Accept: " << strerror(errno) << "\n"; |
|
|
|
exit(EXIT_FAILURE); |
|
|
@ -80,7 +79,8 @@ void Socket::refresh(Env *env) { |
|
|
|
_childs.push_back(new Socket(new_socket, this)); |
|
|
|
} |
|
|
|
int child_fd; |
|
|
|
for (std::vector<Socket *>::iterator it = _childs.begin(); it < _childs.end(); it++) { |
|
|
|
for (std::vector<Socket *>::iterator it = _childs.begin(); it < _childs.end(); |
|
|
|
it++) { |
|
|
|
child_fd = (*it)->_fd; |
|
|
|
if (FD_ISSET(child_fd, &_readfds)) { |
|
|
|
valread = read(child_fd, buffer, 10000); |
|
|
@ -100,34 +100,29 @@ void Socket::refresh(Env *env) { |
|
|
|
} |
|
|
|
|
|
|
|
bool Socket::waitHeader() { |
|
|
|
if (_header.length() < 1) |
|
|
|
if (_tmp.length() < 1) |
|
|
|
return false; |
|
|
|
std::vector<string> lines = split(header, '\r\n'); |
|
|
|
std::vector<string> lines = split(_tmp, '\n'); |
|
|
|
bool is_valid = false; |
|
|
|
for (std::vector<string>::iterator it = lines.begin(); |
|
|
|
it < lines.end(); it++) { |
|
|
|
if (*it == "") |
|
|
|
for (std::vector<string>::iterator it = lines.begin(); it < lines.end(); |
|
|
|
it++) { |
|
|
|
if (*it == "\r") |
|
|
|
is_valid = true; |
|
|
|
} |
|
|
|
if (!is_valid || lines.at(0) == "") |
|
|
|
return false; |
|
|
|
std::vector<string> head = split(lines.at(0), ' '); |
|
|
|
if ((head.at(0) != "GET" && head.at(0) != "POST" && head.at(0) != "DELETE") |
|
|
|
|| head.size() < 2) |
|
|
|
if (!is_valid) |
|
|
|
return false; |
|
|
|
|
|
|
|
_header = _tmp; |
|
|
|
_tmp = ""; |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
int Socket::answer(Env *env, string request) { |
|
|
|
tmp += request; |
|
|
|
cout << "|===|request|===>"<< _request << "|===||\n"; |
|
|
|
_tmp += request; |
|
|
|
cout << "|===|request|===>" << _tmp << "|===||\n"; |
|
|
|
if (_header == "") { |
|
|
|
waitHeader(); |
|
|
|
cout << "Bad request recieved\n"; |
|
|
|
return EXIT_FAILURE; |
|
|
|
} |
|
|
|
std::vector<string> lines = split(_request, '\n'); |
|
|
|
std::vector<string> lines = split(_header, '\n'); |
|
|
|
std::vector<string> head = split(lines.at(0), ' '); |
|
|
|
string uri = head.at(1); |
|
|
|
cout << uri << "\n"; |
|
|
@ -138,12 +133,16 @@ int Socket::answer(Env *env, string request) { |
|
|
|
Server *server = env->choose_server(_parent, split(lines.at(1), ' ').at(1)); |
|
|
|
Route *route = server->get_route(uri); |
|
|
|
std::vector<string> headers; |
|
|
|
if ((headers = route->getHeadersLst()).size() > 0) { |
|
|
|
|
|
|
|
if ((head.at(0) != "GET" && head.at(0) != "POST" && head.at(0) != "DELETE") || |
|
|
|
head.size() < 2) |
|
|
|
send_answer("HTTP/1.1 405 Method Not Allowed\r\nContent-length: 0\r\n\r\n"); |
|
|
|
else if ((headers = route->getHeadersLst()).size() > 0) { |
|
|
|
if (std::find(headers.begin(), headers.end(), head.at(0)) == headers.end()) |
|
|
|
send_answer("HTTP/1.1 405 Method Not Allowed"); |
|
|
|
send_answer("HTTP/1.1 405 Method Not Allowed\r\nContent-length: 0\r\n\r\n"); |
|
|
|
} else if ((headers = server->getHeadersLst()).size() > 0) { |
|
|
|
if (std::find(headers.begin(), headers.end(), head.at(0)) == headers.end()) |
|
|
|
send_answer("HTTP/1.1 405 Method Not Allowed"); |
|
|
|
send_answer("HTTP/1.1 405 Method Not Allowed\r\nContent-length: 0\r\n\r\n"); |
|
|
|
} |
|
|
|
|
|
|
|
string path = route->correctUri(uri); |
|
|
@ -153,10 +152,12 @@ int Socket::answer(Env *env, string request) { |
|
|
|
cout << "No index: lf file\n"; |
|
|
|
ret = read_file(path); |
|
|
|
} |
|
|
|
answer << (ret == "" ? " 404 Not Found\nContent-length: 0\n\n" : " 200 OK\n") << ret; |
|
|
|
answer << (ret == "" ? " 404 Not Found\r\nContent-length: 0\r\n\r\n" : " 200 OK\r\n") |
|
|
|
<< ret; |
|
|
|
cout << "|===|Answer|===>" << answer.str() << "|===||\n"; |
|
|
|
send_answer(answer.str()); |
|
|
|
_request = ""; |
|
|
|
_content = ""; |
|
|
|
_header = ""; |
|
|
|
return EXIT_SUCCESS; |
|
|
|
} |
|
|
|
|
|
|
|