From 94852babc6f9a444258d4b3cfde5024af955e254 Mon Sep 17 00:00:00 2001 From: Me Date: Sun, 7 Aug 2022 22:25:24 +0200 Subject: [PATCH] autoindex is working, a few things to iron out be we well on our way --- default.config | 5 ++- srcs/config/LocationConfig.hpp | 1 + srcs/config/parser.cpp | 26 +++++++------ srcs/config/postProcessing.cpp | 3 +- srcs/utils.cpp | 7 +++- srcs/webserv/autoindex.hpp | 11 +++++- srcs/webserv/response.cpp | 71 ++++++++++++++++++++++++---------- 7 files changed, 85 insertions(+), 39 deletions(-) diff --git a/default.config b/default.config index 5bcea6c..ed483e0 100644 --- a/default.config +++ b/default.config @@ -12,7 +12,8 @@ server { index index.html; # this is another comment # i think root requires leading / # root goop; - root /www; +# root ./www/; + root ./www; # root www/test; # also works # root /www; # stat does not like this kind of definition # root /usr; # because it's looking at / aka absolute path @@ -21,7 +22,7 @@ server { location /test { # root /www/test; - index index.html; + autoindex on; } # If not explicitly set, ConfigParser need to genererate a location block # like this for path "/" (based on field "root" and "index" of the server) diff --git a/srcs/config/LocationConfig.hpp b/srcs/config/LocationConfig.hpp index 09ea756..cc09353 100644 --- a/srcs/config/LocationConfig.hpp +++ b/srcs/config/LocationConfig.hpp @@ -60,6 +60,7 @@ public: std::cout << "Path: " << path << '\n'; std::cout << "root: " << root << '\n'; + std::cout << "autoindex: " << autoindex << '\n'; std::cout << "Skipping index...\n"; diff --git a/srcs/config/parser.cpp b/srcs/config/parser.cpp index 601f471..38a5c4b 100644 --- a/srcs/config/parser.cpp +++ b/srcs/config/parser.cpp @@ -147,8 +147,8 @@ LocationConfig ConfigParser::_parse_location(size_t *start) ret.allow_methods = 0; ret.path = _get_first_word(&curr); - if (ret.path[0] != '/') - throw std::invalid_argument("Location path require a leading /"); +// if (ret.path[0] != '/') +// throw std::invalid_argument("Location path require a leading /"); // in theory now curr should be right after the "path" curr = _content.find_first_not_of(" \t\n", curr); @@ -232,14 +232,15 @@ void ConfigParser::_set_server_values(ServerConfig *server, \ } else if (key == "root" && size == 1 && server->root == "") { - if (tmp_val[0][0] != '/') - throw std::invalid_argument("Root requires leading /"); +// if (tmp_val[0][0] != '/') +// throw std::invalid_argument("Root requires leading /"); // std::cout << "root: " << tmp_val[0] << '\n'; - if (path_is_valid(tmp_val[0]) == 1) +//might not even do these checks here... +// if (path_is_valid(tmp_val[0]) == 1) server->root = tmp_val[0]; - else - throw std::invalid_argument("Root dir invalid 1"); +// else +// throw std::invalid_argument("Root dir invalid 1"); } else if (key == "client_body_limit" && size == 1 \ && server->client_body_limit == 0) @@ -303,17 +304,18 @@ void ConfigParser::_set_location_values(LocationConfig *location, \ else if (key == "root" && size == 1 && location->root == "") { // std::cout << "location root: " << tmp_val[0] << '\n'; - if (tmp_val[0][0] != '/') - throw std::invalid_argument("Root requires leading /"); +// if (tmp_val[0][0] != '/') +// throw std::invalid_argument("Root requires leading /"); - if (path_is_valid(tmp_val[0]) == 1) +// if (path_is_valid(tmp_val[0]) == 1) location->root = tmp_val[0]; - else - throw std::invalid_argument("Root dir invalid"); +// else +// throw std::invalid_argument("Root dir invalid"); } else if (key == "autoindex" && size == 1) { location->autoindex = (tmp_val[0] == "on" ? true : false); + std::cout << "in parser " << location->path << " autoindex: " << location->autoindex << '\n'; } else if (key == "index") { diff --git a/srcs/config/postProcessing.cpp b/srcs/config/postProcessing.cpp index 0ca5e5e..62c63b5 100644 --- a/srcs/config/postProcessing.cpp +++ b/srcs/config/postProcessing.cpp @@ -65,11 +65,12 @@ void ConfigParser::_post_processing(std::vector *servers) // maybe do something for Cgi_info? // std::cout << "In Post, Root + Path: " << it_l->root + it_l->path << '\n'; - if (path_is_valid(it_l->root + it_l->path) == 0) +/* if (path_is_valid(it_l->root + it_l->path) == 0) { //either we throw or we erase throw std::invalid_argument("location path is invalid"); } +*/ ++it_l; } diff --git a/srcs/utils.cpp b/srcs/utils.cpp index 2c3c15a..f8c498d 100644 --- a/srcs/utils.cpp +++ b/srcs/utils.cpp @@ -88,11 +88,14 @@ std::string http_methods_to_str(unsigned int methods) return (str); } +# include + // you could make this &path... int path_is_valid(std::string path) { - std::string i_path = path.substr(1); - const char *tmp_path = i_path.c_str(); +// std::string i_path = path.substr(1); +// const char *tmp_path = i_path.c_str(); + const char *tmp_path = path.c_str(); struct stat s; if (stat(tmp_path, &s) == 0) diff --git a/srcs/webserv/autoindex.hpp b/srcs/webserv/autoindex.hpp index 32d9d11..88911e3 100644 --- a/srcs/webserv/autoindex.hpp +++ b/srcs/webserv/autoindex.hpp @@ -10,13 +10,20 @@ ""\ " Index of " -# define AUTOINDEX_MID \ +# define AUTOINDEX_MID1 \ ""\ ""\ -"" +"" \ + "

Index of " +# define AUTOINDEX_MID2 \ +"

"\ +"
"\ +"
"
 
 # define AUTOINDEX_END \
+"
"\ +"
"\ ""\ "" diff --git a/srcs/webserv/response.cpp b/srcs/webserv/response.cpp index c658461..5a4b0eb 100644 --- a/srcs/webserv/response.cpp +++ b/srcs/webserv/response.cpp @@ -145,11 +145,22 @@ void Webserv::_get(Client *client, ServerConfig &server, LocationConfig &locatio std::cout << "ERIC path: " << path << '\n'; -/* if (path_is_valid(path) == 1 && location.autoindex == true) +/* RULES ** + +if path is a valid dir check if index is specified and serve that +if no index and autoindex, server that +if file, server that! + +*/ + + +// std::cout << "location: " << location.path << " autoindex: " << location.autoindex << '\n'; + if (path_is_valid(path) == 1 && location.autoindex == true) { + std::cout << "about to call autoindex\n"; _autoindex(client, location, path); } -*/ + // TMP HUGO // @@ -182,17 +193,45 @@ void Webserv::_autoindex(Client *client, LocationConfig &location, std::string & // Let's try the 2nd one first. std::cout << "made it to _autoindex\n"; + std::string dir_list; DIR *dir; struct dirent *ent; - if ((dir = opendir ((location.root + location.path).c_str())) != NULL) { + if ((dir = opendir ((location.root + location.path).c_str())) != NULL) + { /* print all the files and directories within directory */ - while ((ent = readdir (dir)) != NULL) { - printf ("%s\n", ent->d_name); - } + dir_list.append(AUTOINDEX_START); + dir_list.append(location.path); + dir_list.append(AUTOINDEX_MID1); + dir_list.append(location.path); + dir_list.append(AUTOINDEX_MID2); + while ((ent = readdir (dir)) != NULL) + { +// printf ("%s\n", ent->d_name); + if (strcmp(".", ent->d_name) == 0) + continue ; + dir_list.append("d_name); + dir_list.append("\">"); + dir_list.append(ent->d_name); + dir_list.append(""); + dir_list.append("\r\n"); + } + +// nginx.org.
+ + dir_list.append(AUTOINDEX_END); + _append_body(client, dir_list.c_str(), dir_list.size(), "html"); + closedir (dir); - } else { + } + else + { /* could not open directory */ // perror (""); std::cout << "could not open dir\n"; @@ -201,19 +240,7 @@ void Webserv::_autoindex(Client *client, LocationConfig &location, std::string & - std::string dir_list; - dir_list.append(AUTOINDEX_START); - dir_list.append(location.path); - dir_list.append(AUTOINDEX_MID); - // loop something - dir_list.append(location.path); - - - - dir_list.append(AUTOINDEX_END); - - _append_body(client, dir_list.c_str(), dir_list.size(), "html"); @@ -453,10 +480,14 @@ ServerConfig &Webserv::_determine_process_server(Client *client) LocationConfig &Webserv::_determine_location(ServerConfig &server, std::string &path) { +// std::cout << "determin location path sent: " << path << '\n'; + std::vector::iterator it = server.locations.begin(); while (it != server.locations.end()) { - if (it->path.compare(0, path.size(), path)) +// std::cout << it->path << " -- "; + // if (it->path.compare(0, path.size(), path) == 0) + if (it->path.compare(0, it->path.size(), path) == 0) break; ++it; }