diff --git a/default.json b/default.json index 66d707d..9611322 100644 --- a/default.json +++ b/default.json @@ -4,24 +4,24 @@ "servers": [ { "server_name": "localhost", - "listens": ["localhost:8080", "localhost"], + "listens": ["localhost:8080"], "return": "301 https://$host$uri" }, { "server_name": "localhost", - "listens": ["192.168.62.61:8080"], - "root": "./public", + "listens": ["192.168.62.61:8080", "localhost", "555"], + "root": "public/", "indexs": ["index.html"], "locations": { "docs/": { - "root": "documents/", + "root": "public/documents/", "autoindex": true }, "img/": { - "root": "images/", + "root": "public/images/", "autoindex": true } } diff --git a/includes/Route.hpp b/includes/Route.hpp index 85725ca..81455e8 100644 --- a/includes/Route.hpp +++ b/includes/Route.hpp @@ -16,6 +16,7 @@ class Route { string getRoot(void); string getReturn(void); std::vector getIndexs(void); - string getAutoindex(string uri); + string getAutoindex(string path); + string read_file(string path); string correctUri(string uri); }; diff --git a/includes/Socket.hpp b/includes/Socket.hpp index 4755fb0..f43df40 100644 --- a/includes/Socket.hpp +++ b/includes/Socket.hpp @@ -18,6 +18,6 @@ class Socket { int launch(void); void set_fds(void); void refresh(Env *env); - void answer(Env *env, int fd, string request); + int answer(Env *env, int fd, string request); void send_answer(int fd, string msg); }; diff --git a/srcs/load/Env.cpp b/srcs/load/Env.cpp index 90284a5..082932e 100644 --- a/srcs/load/Env.cpp +++ b/srcs/load/Env.cpp @@ -25,14 +25,14 @@ Env::Env(JSONNode *conf) { Server *Env::choose_server(Socket *sock, string host) { std::vector exact; std::vector inrange; - std::vector ip_list; - std::vector ip_requ; + std::vector ip_listen; + std::vector ip_required; // string ip = inet_ntoa(sock->_address.sin_addr); // int port = ntohs(sock->_address.sin_port); // cout << "Which server for " << ip << ":" << port << "?\n"; cout << "Socket: " << sock->_listen.ip << ":" << sock->_listen.port << "\n"; - ip_requ = split(sock->_listen.ip, '.'); + ip_required = split(sock->_listen.ip, '.'); for (std::vector::iterator sit = _servers.begin(); sit < _servers.end(); sit++) { @@ -40,7 +40,6 @@ Server *Env::choose_server(Socket *sock, string host) { for (std::vector::iterator it = serv_listens.begin(); it < serv_listens.end(); it++) { - ip_list = split((*it).ip, '.'); if (sock->_listen.port != (*it).port) continue; if (sock->_listen.ip == (*it).ip) { @@ -48,16 +47,15 @@ Server *Env::choose_server(Socket *sock, string host) { continue; } bool is_inrange = true; - ip_list = split((*it).ip, '.'); - std::vector::iterator r = ip_requ.begin(); - for (std::vector::iterator l = ip_list.begin(); - l < ip_list.end(); l++) { + ip_listen = split((*it).ip, '.'); + std::vector::iterator r = ip_required.begin(); + for (std::vector::iterator l = ip_listen.begin(); + l < ip_listen.end(); l++) { if (*l != *r && *l != "0") is_inrange = false; } if (is_inrange == true) inrange.push_back(*sit); - // else if (is_ip_into(sock->_listen.ip, (*it).ip)) } } if (exact.size() == 0) { diff --git a/srcs/load/Route.cpp b/srcs/load/Route.cpp index 35cb04f..d3b0220 100644 --- a/srcs/load/Route.cpp +++ b/srcs/load/Route.cpp @@ -24,18 +24,16 @@ string Route::getRoot(void) { return _root; } string Route::getReturn(void) { return _ret; } std::vector Route::getIndexs(void) { return _indexs; } -string Route::getAutoindex(string uri) { +string Route::getAutoindex(string path) { if (!_autoindex) - return "4\n\n404!"; + return ""; std::stringstream page; - std::stringstream ret; - string path = correctUri(uri); DIR *dir; struct dirent *entry; struct stat info; if ((dir = opendir(path.c_str())) == NULL) - ret << " 19\n\nFolder unaccesible."; + return ""; else { page << path << " files :\n"; while ((entry = readdir(dir)) != NULL) { @@ -48,15 +46,28 @@ string Route::getAutoindex(string uri) { } closedir(dir); } - ret << page.str().length() << "\n\n" << page.str(); - return ret.str(); + return page.str(); +} + +string Route::read_file(string path) { + string str; + std::stringstream ret; + struct stat info; + if (stat(path.c_str(), &info) != 0) { + std::cerr << "stat() error on " << path << ": " + << strerror(errno) << "\n"; + return ""; + } + std::ifstream file(path.c_str()); + while (file) { + std::getline(file, str); + ret << str << "\n"; + } + return (ret.str()); } string Route::correctUri(string uri) { std::stringstream ret; - // int slash_pos; - // string root = _root; - // int i = 0; std::vector::iterator it; std::vector::iterator it2; @@ -76,19 +87,6 @@ string Route::correctUri(string uri) { while (it2 < uri_split.end()) { ret << "/" << *(it2++); } - /* - int i = 0; - while (uri.at(i) == '/') - i++; - uri.erase(0, i); - while ((slash_pos = root.find('/')) > 0) { - ret << root.substr(0, slash_pos); - root.erase(0, slash_pos); - if (uri.find('/')) - uri = uri.substr(uri.find('/'), uri.length()); - } - ret << uri; - */ cout << "resutlt: " << ret.str() << "\n"; return ret.str(); } diff --git a/srcs/load/Socket.cpp b/srcs/load/Socket.cpp index 98aa176..0e21409 100644 --- a/srcs/load/Socket.cpp +++ b/srcs/load/Socket.cpp @@ -1,9 +1,7 @@ #include "webserv.hpp" -Socket::Socket(listen_t listen) : _listen(listen) { - _clients_amount = 0; -} +Socket::Socket(listen_t listen) : _listen(listen) { _clients_amount = 0; } Socket::~Socket(void) { close(_master_socket); cout << "Socket destroyed!\n"; @@ -91,27 +89,46 @@ void Socket::refresh(Env *env) { _clients.erase(it); } else { buffer[valread] = '\0'; - answer(env, *it, buffer); + if (answer(env, *it, buffer) == EXIT_FAILURE) { + _clients.erase(it); + close(*it); + } + } } } } -void Socket::answer(Env *env, int fd, string request) { +int Socket::answer(Env *env, int fd, string request) { + cout << request << "\n|===|===|===|\n"; std::vector lines = split(request, '\n'); - string uri = split(lines.at(0), ' ').at(1); + std::vector head = split(lines.at(0), ' '); + string uri; + if (head.size() < 2) + return EXIT_FAILURE; + uri = head.at(1); cout << uri << "\n"; + string ret; - cout << request << "\n|===|===|===|\n"; std::stringstream answer; answer << "HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: "; Server *server = env->choose_server(this, split(lines.at(1), ' ').at(1)); Route *route = server->get_route(uri); - cout << "Route find: " << route->getLocation() << "\n"; - answer << route->getAutoindex(uri); + /* + cout << "Route find: " << route->getLocation() << "->" << route->getRoot() << "\n"; + if ((ret = route->read_file(uri)) == "") + answer << route->getAutoindex(uri); + else + answer << ret; + */ + string path = route->correctUri(uri); + if ((ret = route->getAutoindex(path)) == "") + ret = route->read_file(path); + answer << ret.length() << "\n\n" << ret; cout << answer.str() << "\n|===|===|===|\n"; send_answer(fd, answer.str()); + return EXIT_SUCCESS; } void Socket::send_answer(int fd, string msg) {