Browse Source

save 22-11-3

master
nicolas-arnaud 2 years ago
parent
commit
8489ab2506
  1. 2
      includes/Socket.hpp
  2. 23
      srcs/load/Route.cpp
  3. 63
      srcs/load/Socket.cpp
  4. 6
      srcs/tools.cpp

2
includes/Socket.hpp

@ -7,7 +7,7 @@ class Socket {
Socket *_parent; Socket *_parent;
std::vector<Socket *> _childs; std::vector<Socket *> _childs;
struct sockaddr_in _address; struct sockaddr_in _address;
string tmp; string _tmp;
string _header; string _header;
string _content; string _content;

23
srcs/load/Route.cpp

@ -1,6 +1,7 @@
#include "webserv.hpp" #include "webserv.hpp"
Route::Route(Server *server, string location, JSONNode *datas) : _server(server), _location(location){ Route::Route(Server *server, string location, JSONNode *datas)
: _server(server), _location(location) {
JSONObject object = datas->obj(); JSONObject object = datas->obj();
JSONNode *tmp; JSONNode *tmp;
if ((tmp = object["root"])) if ((tmp = object["root"]))
@ -43,7 +44,8 @@ string Route::getIndex(string uri, string path) {
if ((dir = opendir(path.c_str())) == NULL) if ((dir = opendir(path.c_str())) == NULL)
return ""; return "";
else { else {
content << "<h3 style=\"text-align: center;\">" << path << " files :</h3>\n<ul>\n"; content << "<h3 style=\"text-align: center;\">" << path
<< " files :</h3>\n<ul>\n";
while ((entry = readdir(dir)) != NULL) { while ((entry = readdir(dir)) != NULL) {
if (entry->d_name[0] == '.') if (entry->d_name[0] == '.')
continue; continue;
@ -51,19 +53,20 @@ string Route::getIndex(string uri, string path) {
if (entry->d_name == *it) if (entry->d_name == *it)
return (read_file(path + "/" + *it)); return (read_file(path + "/" + *it));
} }
content << "<li><a href=\"" << uri + "/" + entry->d_name << "\">"<< entry->d_name << "</a></li>\n"; content << "<li><a href=\"" << uri + "/" + entry->d_name << "\">"
<< entry->d_name << "</a></li>\n";
if (stat(path.c_str(), &info) != 0) if (stat(path.c_str(), &info) != 0)
std::cerr << "stat() error on " << path << ": " std::cerr << "stat() error on " << path << ": " << strerror(errno)
<< strerror(errno) << "\n"; << "\n";
} }
content << "<ul>"; content << "<ul>";
closedir(dir); closedir(dir);
} }
if (!_autoindex) if (!_autoindex)
return ""; return "";
ret << "Content-type: text/html \n"; ret << "Content-type: text/html \r\n";
ret << "Content-length: "<< content.str().length(); ret << "Content-length: " << content.str().length() << "\r\n";
ret << "\n\n" << content.str(); ret << "\r\n" << content.str();
return ret.str(); return ret.str();
} }
@ -72,7 +75,7 @@ string Route::correctUri(string uri) {
std::vector<string>::iterator it; std::vector<string>::iterator it;
std::vector<string>::iterator it2; std::vector<string>::iterator it2;
//cout << "Correcting request: " << uri << " with root: " << _root << "\n"; // cout << "Correcting request: " << uri << " with root: " << _root << "\n";
ret << _root; ret << _root;
std::vector<string> loc_split = split(_location, '/'); std::vector<string> loc_split = split(_location, '/');
std::vector<string> uri_split = split(uri, '/'); std::vector<string> uri_split = split(uri, '/');
@ -89,6 +92,6 @@ string Route::correctUri(string uri) {
while (it2 < uri_split.end()) { while (it2 < uri_split.end()) {
ret << "/" << *(it2++); ret << "/" << *(it2++);
} }
//cout << "resutlt: " << ret.str() << "\n"; // cout << "resutlt: " << ret.str() << "\n";
return ret.str(); return ret.str();
} }

63
srcs/load/Socket.cpp

@ -1,8 +1,8 @@
#include "webserv.hpp" #include "webserv.hpp"
Socket::Socket(listen_t listen) : _listen(listen) {} Socket::Socket(listen_t listen) : _listen(listen) {}
Socket::Socket(int fd, Socket *parent) : _fd(fd),_parent(parent) {} Socket::Socket(int fd, Socket *parent) : _fd(fd), _parent(parent) {}
Socket::~Socket(void) { Socket::~Socket(void) {
close(_fd); close(_fd);
cout << "Socket destroyed!\n"; cout << "Socket destroyed!\n";
@ -18,8 +18,8 @@ int Socket::launch(void) {
cout << "Socket creation: " << strerror(errno) << "\n"; cout << "Socket creation: " << strerror(errno) << "\n";
return (EXIT_FAILURE); return (EXIT_FAILURE);
} }
int opt_ret = setsockopt(_fd, SOL_SOCKET, SO_REUSEADDR, int opt_ret =
(char *)&opt, sizeof(opt)); setsockopt(_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt));
if (opt_ret < 0) { if (opt_ret < 0) {
cout << "Sockopt: " << strerror(errno) << "\n"; cout << "Sockopt: " << strerror(errno) << "\n";
return (EXIT_FAILURE); return (EXIT_FAILURE);
@ -29,8 +29,7 @@ int Socket::launch(void) {
_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(_fd, (struct sockaddr *)&_address, sizeof(_address)) < if (bind(_fd, (struct sockaddr *)&_address, sizeof(_address)) < 0) {
0) {
cout << "Bind: " << strerror(errno) << "\n"; cout << "Bind: " << strerror(errno) << "\n";
return (EXIT_FAILURE); return (EXIT_FAILURE);
} }
@ -64,14 +63,14 @@ void Socket::refresh(Env *env) {
int addrlen = sizeof(_address); int addrlen = sizeof(_address);
char buffer[10000]; char buffer[10000];
if (FD_ISSET(_fd, &_readfds)) { if (FD_ISSET(_fd, &_readfds)) {
int new_socket = accept(_fd, (struct sockaddr *)&_address, int new_socket =
(socklen_t *)&addrlen); accept(_fd, (struct sockaddr *)&_address, (socklen_t *)&addrlen);
if (new_socket < 0) { if (new_socket < 0) {
cout << "Accept: " << strerror(errno) << "\n"; cout << "Accept: " << strerror(errno) << "\n";
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
#ifdef __APPLE__ #ifdef __APPLE__
//fcntl(new_socket, F_GETNOSIGPIPE); // fcntl(new_socket, F_GETNOSIGPIPE);
fcntl(new_socket, F_SETFL, O_NONBLOCK); fcntl(new_socket, F_SETFL, O_NONBLOCK);
#endif #endif
cout << "New connection, socket fd is " << new_socket cout << "New connection, socket fd is " << new_socket
@ -80,7 +79,8 @@ void Socket::refresh(Env *env) {
_childs.push_back(new Socket(new_socket, this)); _childs.push_back(new Socket(new_socket, this));
} }
int child_fd; 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; child_fd = (*it)->_fd;
if (FD_ISSET(child_fd, &_readfds)) { if (FD_ISSET(child_fd, &_readfds)) {
valread = read(child_fd, buffer, 10000); valread = read(child_fd, buffer, 10000);
@ -100,34 +100,29 @@ void Socket::refresh(Env *env) {
} }
bool Socket::waitHeader() { bool Socket::waitHeader() {
if (_header.length() < 1) if (_tmp.length() < 1)
return false; return false;
std::vector<string> lines = split(header, '\r\n'); std::vector<string> lines = split(_tmp, '\n');
bool is_valid = false; bool is_valid = false;
for (std::vector<string>::iterator it = lines.begin(); for (std::vector<string>::iterator it = lines.begin(); it < lines.end();
it < lines.end(); it++) { it++) {
if (*it == "") if (*it == "\r")
is_valid = true; is_valid = true;
} }
if (!is_valid || lines.at(0) == "") if (!is_valid)
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)
return false; return false;
_header = _tmp;
_tmp = "";
return true; return true;
} }
int Socket::answer(Env *env, string request) { int Socket::answer(Env *env, string request) {
tmp += request; _tmp += request;
cout << "|===|request|===>"<< _request << "|===||\n"; cout << "|===|request|===>" << _tmp << "|===||\n";
if (_header == "") { if (_header == "") {
waitHeader(); 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), ' '); std::vector<string> head = split(lines.at(0), ' ');
string uri = head.at(1); string uri = head.at(1);
cout << uri << "\n"; 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)); Server *server = env->choose_server(_parent, split(lines.at(1), ' ').at(1));
Route *route = server->get_route(uri); Route *route = server->get_route(uri);
std::vector<string> headers; 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()) 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) { } else if ((headers = server->getHeadersLst()).size() > 0) {
if (std::find(headers.begin(), headers.end(), head.at(0)) == headers.end()) 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); string path = route->correctUri(uri);
@ -153,10 +152,12 @@ int Socket::answer(Env *env, string request) {
cout << "No index: lf file\n"; cout << "No index: lf file\n";
ret = read_file(path); 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"; cout << "|===|Answer|===>" << answer.str() << "|===||\n";
send_answer(answer.str()); send_answer(answer.str());
_request = ""; _content = "";
_header = "";
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

6
srcs/tools.cpp

@ -219,8 +219,8 @@ string read_file(string path) {
std::getline(file, str); std::getline(file, str);
content += str + "\n"; content += str + "\n";
} }
ret << "Content-type: " << getMime(path) << "\n"; ret << "Content-type: " << getMime(path) << "\r\n";
ret << "Content-length: " << content.length(); ret << "Content-length: " << content.length() << "\r\n";
ret << "\n\n" << content; ret << "\r\n" << content;
return (ret.str()); return (ret.str());
} }

Loading…
Cancel
Save