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

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