autoindex is working, a few things to iron out be we well on our way

This commit is contained in:
Me
2022-08-07 22:25:24 +02:00
parent f777441edf
commit 94852babc6
7 changed files with 85 additions and 39 deletions

View File

@@ -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)

View File

@@ -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";

View File

@@ -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")
{

View File

@@ -65,11 +65,12 @@ void ConfigParser::_post_processing(std::vector<ServerConfig> *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;
}

View File

@@ -88,11 +88,14 @@ std::string http_methods_to_str(unsigned int methods)
return (str);
}
# include <iostream>
// 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)

View File

@@ -10,13 +10,20 @@
"<head>"\
"<title> Index of "
# define AUTOINDEX_MID \
# define AUTOINDEX_MID1 \
"</title>"\
"</head>"\
"<body>"
"<body>" \
"<h1>Index of "
# define AUTOINDEX_MID2 \
"</h1>"\
"<hr>"\
"<pre>"
# define AUTOINDEX_END \
"</pre>"\
"<hr>"\
"</body>"\
"</html>"

View File

@@ -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("<a href=\"");
dir_list.append(location.path.c_str());
// if no / at end of location path...
if (location.path.find_last_of("/") != location.path.size())
dir_list.append("/");
dir_list.append(ent->d_name);
dir_list.append("\">");
dir_list.append(ent->d_name);
dir_list.append("</a>");
dir_list.append("\r\n");
}
// <a href="http://nginx.org/">nginx.org</a>.<br/>
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<LocationConfig>::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;
}