From cfd3269196e10452a42584b783eea668ddae0617 Mon Sep 17 00:00:00 2001 From: nicolas-arnaud Date: Mon, 7 Nov 2022 23:10:11 +0100 Subject: [PATCH] save 22-11-7-2 --- includes/Route.hpp | 4 ++-- srcs/load/Route.cpp | 30 +++++++++++++++++++++--------- srcs/load/Socket.cpp | 13 +++---------- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/includes/Route.hpp b/includes/Route.hpp index 81455e8..eeda078 100644 --- a/includes/Route.hpp +++ b/includes/Route.hpp @@ -15,8 +15,8 @@ class Route { string getLocation(void); string getRoot(void); string getReturn(void); - std::vector getIndexs(void); - string getAutoindex(string path); + std::vector getIndexsLst(void); + string getIndex(string path); string read_file(string path); string correctUri(string uri); }; diff --git a/srcs/load/Route.cpp b/srcs/load/Route.cpp index d3b0220..df2b692 100644 --- a/srcs/load/Route.cpp +++ b/srcs/load/Route.cpp @@ -22,35 +22,44 @@ Route::~Route(void) {} string Route::getLocation(void) { return _location; } string Route::getRoot(void) { return _root; } string Route::getReturn(void) { return _ret; } -std::vector Route::getIndexs(void) { return _indexs; } +std::vector Route::getIndexsLst(void) { return _indexs; } -string Route::getAutoindex(string path) { - if (!_autoindex) - return ""; - std::stringstream page; +string Route::getIndex(string path) { + std::stringstream content; + std::stringstream ret; DIR *dir; struct dirent *entry; struct stat info; + std::vector indexs = getIndexsLst(); + std::vector::iterator it; if ((dir = opendir(path.c_str())) == NULL) return ""; else { - page << path << " files :\n"; + content << path << " files :\n"; while ((entry = readdir(dir)) != NULL) { if (entry->d_name[0] == '.') continue; - page << "- " << entry->d_name << "\n"; + for (it = indexs.begin(); it < indexs.end(); it++) { + if (entry->d_name == *it) + return (read_file(path + "/" + *it)); + } + content << "- " << entry->d_name << "\n"; if (stat(path.c_str(), &info) != 0) std::cerr << "stat() error on " << path << ": " << strerror(errno) << "\n"; } closedir(dir); } - return page.str(); + ret << "Content-type: text/html \n"; + ret << "Content-length: "<< content.str().length(); + ret << "\n\n" << content; + return ret.str(); } string Route::read_file(string path) { string str; + string content; std::stringstream ret; struct stat info; if (stat(path.c_str(), &info) != 0) { @@ -61,8 +70,11 @@ string Route::read_file(string path) { std::ifstream file(path.c_str()); while (file) { std::getline(file, str); - ret << str << "\n"; + content += str + "\n"; } + ret << "Content-type: " << getMime(path) << "\n"; + ret << "Content-length: " << content.length(); + ret << "\n\n" << content; return (ret.str()); } diff --git a/srcs/load/Socket.cpp b/srcs/load/Socket.cpp index 0e21409..7d0e50d 100644 --- a/srcs/load/Socket.cpp +++ b/srcs/load/Socket.cpp @@ -111,21 +111,14 @@ int Socket::answer(Env *env, int fd, string request) { string ret; std::stringstream answer; - answer << "HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: "; + answer << "HTTP/1.1 200 OK\n"; Server *server = env->choose_server(this, split(lines.at(1), ' ').at(1)); Route *route = server->get_route(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)) == "") + if ((ret = route->getIndex(path)) == "") ret = route->read_file(path); - answer << ret.length() << "\n\n" << ret; + answer << ret; cout << answer.str() << "\n|===|===|===|\n"; send_answer(fd, answer.str()); return EXIT_SUCCESS;