diff --git a/includes/Route.hpp b/includes/Route.hpp index a4f81d5..ed39f74 100644 --- a/includes/Route.hpp +++ b/includes/Route.hpp @@ -3,19 +3,23 @@ class Route { protected: + Server *_server; string _location; string _root; string _ret; std::vector _indexs; + std::vector _headers; bool _autoindex; public: - Route(string location, JSONNode *datas); + Route(Server *server, string location, JSONNode *datas); ~Route(void); string getLocation(void); string getRoot(void); string getReturn(void); std::vector getIndexsLst(void); + std::vector getHeadersLst(void); + Server *getServer(void); string getIndex(string uri, string path); string correctUri(string uri); }; diff --git a/includes/Socket.hpp b/includes/Socket.hpp index 62ba30b..ce1fc17 100644 --- a/includes/Socket.hpp +++ b/includes/Socket.hpp @@ -6,21 +6,26 @@ class Socket { //int _clients_amount; Socket *_parent; std::vector _childs; - string _request; - public: struct sockaddr_in _address; + string tmp; + string _header; + string _content; + + int answer(Env *env, string request); + void send_answer(string msg); + bool waitHeader(); + public: + Socket(listen_t listen); + Socket(int fd, Socket *parent); + ~Socket(void); + listen_t _listen; static fd_set _readfds; static int _max_fd; static int _min_fd; static int _amount; - Socket(listen_t listen); - Socket(int fd, Socket *parent); - ~Socket(void); + int launch(void); void set_fds(void); void refresh(Env *env); - bool isRequestValid(string request); - int answer(Env *env, string request); - void send_answer(string msg); }; diff --git a/srcs/load/Route.cpp b/srcs/load/Route.cpp index c12247a..489e946 100644 --- a/srcs/load/Route.cpp +++ b/srcs/load/Route.cpp @@ -1,6 +1,6 @@ #include "webserv.hpp" -Route::Route(string location, JSONNode *datas) : _location(location) { +Route::Route(Server *server, string location, JSONNode *datas) : _server(server), _location(location){ JSONObject object = datas->obj(); JSONNode *tmp; if ((tmp = object["root"])) @@ -15,6 +15,12 @@ Route::Route(string location, JSONNode *datas) : _location(location) { _indexs.push_back((*it)->str()); } } + if ((tmp = object["add_header"])) { + JSONList headers = tmp->lst(); + for (JSONList::iterator it = headers.begin(); it < headers.end(); it++) { + _headers.push_back((*it)->str()); + } + } } Route::~Route(void) {} @@ -23,6 +29,7 @@ string Route::getLocation(void) { return _location; } string Route::getRoot(void) { return _root; } string Route::getReturn(void) { return _ret; } std::vector Route::getIndexsLst(void) { return _indexs; } +std::vector Route::getHeadersLst(void) { return _headers; } string Route::getIndex(string uri, string path) { std::stringstream content; diff --git a/srcs/load/Server.cpp b/srcs/load/Server.cpp index caf5839..c6c03f0 100644 --- a/srcs/load/Server.cpp +++ b/srcs/load/Server.cpp @@ -1,6 +1,6 @@ #include "webserv.hpp" -Server::Server(JSONNode *server) : Route("/", server) { +Server::Server(JSONNode *server) : Route(NULL, "/", server) { JSONObject datas = server->obj(); if (datas["server_name"]) _name = datas["server_name"]->str(); @@ -8,7 +8,7 @@ Server::Server(JSONNode *server) : Route("/", server) { JSONObject locations = datas["locations"]->obj(); for (JSONObject::iterator it = locations.begin(); it != locations.end(); it++) { - Route *route = new Route((*it).first, (*it).second); + Route *route = new Route(this, (*it).first, (*it).second); _routes[(*it).first] = route; } } diff --git a/srcs/load/Socket.cpp b/srcs/load/Socket.cpp index 6d89f3b..d1a0325 100644 --- a/srcs/load/Socket.cpp +++ b/srcs/load/Socket.cpp @@ -99,15 +99,15 @@ void Socket::refresh(Env *env) { } } -bool Socket::isRequestValid(string request) { - _request += request; - if (_request.length() < 1) +bool Socket::waitHeader() { + if (_header.length() < 1) return false; - std::vector lines = split(_request, '\n'); + std::vector lines = split(header, '\r\n'); bool is_valid = false; for (std::vector::iterator it = lines.begin(); it < lines.end(); it++) { - if (*it == "\r" || *it == "") is_valid = true; + if (*it == "") + is_valid = true; } if (!is_valid || lines.at(0) == "") return false; @@ -115,14 +115,16 @@ bool Socket::isRequestValid(string request) { if ((head.at(0) != "GET" && head.at(0) != "POST" && head.at(0) != "DELETE") || head.size() < 2) return false; + return true; } int Socket::answer(Env *env, string request) { - cout << "|===|Request|===|\n"<< request << "\n|===|===|===|\n"; - if (!isRequestValid(request)) { + tmp += request; + cout << "|===|request|===>"<< _request << "|===||\n"; + if (_header == "") { + waitHeader(); cout << "Bad request recieved\n"; - send_answer("HTTP/1.1 400 Bad Request\n\n"); return EXIT_FAILURE; } std::vector lines = split(_request, '\n'); @@ -135,6 +137,15 @@ 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 headers; + 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"); + } 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"); + } + string path = route->correctUri(uri); cout << "Path: " << path << "\n"; ret = route->getIndex(uri, path); @@ -143,7 +154,7 @@ int Socket::answer(Env *env, string request) { ret = read_file(path); } answer << (ret == "" ? " 404 Not Found\nContent-length: 0\n\n" : " 200 OK\n") << ret; - cout << "|===|Answer|===|\n" << answer.str() << "\n|===|===|===|\n"; + cout << "|===|Answer|===>" << answer.str() << "|===||\n"; send_answer(answer.str()); _request = ""; return EXIT_SUCCESS; diff --git a/srcs/tools.cpp b/srcs/tools.cpp index 54832fb..96ca5bf 100644 --- a/srcs/tools.cpp +++ b/srcs/tools.cpp @@ -207,14 +207,14 @@ string read_file(string path) { string content; std::stringstream ret; struct stat info; - if (stat(path.c_str(), &info) != 0) { + if (stat(path.c_str(), &info) != 0 || S_ISDIR(info.st_mode)) { std::cerr << "stat() error on " << path << ": " << strerror(errno) << "\n"; return ""; } std::ifstream file(path.c_str()); - if (file.fail()) - return ""; + //if (!file.good()) + // return ""; while (file) { std::getline(file, str); content += str + "\n"; @@ -224,5 +224,3 @@ string read_file(string path) { ret << "\n\n" << content; return (ret.str()); } - -