a bit messy but merged index and autoindex work

This commit is contained in:
Me
2022-08-08 17:13:55 +02:00
parent 0a72778c8c
commit 7fdc81f5f4
2 changed files with 25 additions and 30 deletions

View File

@@ -88,7 +88,7 @@ class Webserv
// void _get(Client *client, ServerConfig &server, LocationConfig &location); // void _get(Client *client, ServerConfig &server, LocationConfig &location);
// in progress // in progress
void _autoindex(Client *client, LocationConfig &location, std::string &path); void _autoindex(Client *client, std::string &path);
void _get_file(Client *client, const std::string &path); void _get_file(Client *client, const std::string &path);

View File

@@ -128,21 +128,13 @@ if file, server that!
WHere does cgi fit in in all this ??? WHere does cgi fit in in all this ???
THIS NEEDS WORK...
*/ */
(void)server; // To remove from arg if we determine its useless
std::string path = client->get_path(); std::string path = client->get_path();
/* if (path == "/") // TODO : index and autoindex // this might not be the best thing, a voir
path.append(INDEX);
<<<<<<< HEAD
path.insert(0, location.root);
*/
path.insert(0, client->assigned_location->root); path.insert(0, client->assigned_location->root);
// that was actually a horrible idea...
// path.insert(0, location.root);
std::cerr << "path = " << path << "\n"; std::cerr << "path = " << path << "\n";
// path = root + location.path // path = root + location.path
@@ -152,27 +144,28 @@ THIS NEEDS WORK...
if (path_is_valid(path) == 1) if (path_is_valid(path) == 1)
{ {
std::cout << "path is valid\n"; // std::cout << "path is valid\n";
if (path[path.size() - 1] != '/') if (path[path.size() - 1] != '/')
path.push_back('/'); path.push_back('/');
for (size_t i = 0; i < location.index.size(); i++) for (size_t i = 0; i < client->assigned_location->index.size(); i++)
{ {
std::cout << "location path: " << location.path << '\n'; // std::cout << "location path: " << client->assigned_location->path << '\n';
std::cout << "location index: " << location.index[i] << '\n'; // std::cout << "location index: " << client->assigned_location->index[i] << '\n';
// std::cout << "path with index: " << path + location.index[i] << '\n'; // std::cout << "path with index: " << path + assigned_location->index[i] << '\n';
if (path_is_valid(path + location.index[i]) == 2) if (path_is_valid(path + client->assigned_location->index[i]) == 2)
{ {
std::cout << "found a valid index\n"; // std::cout << "found a valid index\n";
path.append(location.index[i]); path.append(client->assigned_location->index[i]);
break ; // what if index and autoindex in a single location? break ; // what if index and autoindex in a single location?
// does this completely fail? // does this completely fail?
// do this instead of break? // do this instead of break?
//_get_file(client, path); //_get_file(client, path);
} }
} }
if (location.autoindex == true) if (client->assigned_location->autoindex == true)
{ {
_autoindex(client, location, path); // _autoindex(client, client->assigned_location, path);
_autoindex(client, path);
return ; return ;
} }
} }
@@ -198,32 +191,34 @@ THIS NEEDS WORK...
// i only sort of need &path... // i only sort of need &path...
// def can improve but works for now... // def can improve but works for now...
void Webserv::_autoindex(Client *client, LocationConfig &location, std::string &path) //void Webserv::_autoindex(Client *client, LocationConfig &location, std::string &path)
void Webserv::_autoindex(Client *client, std::string &path)
{ {
std::cout << "made it to _autoindex\n"; // std::cout << "made it to _autoindex\n";
(void)path;
std::string dir_list; std::string dir_list;
DIR *dir; DIR *dir;
struct dirent *ent; struct dirent *ent;
std::cout << "location root: " << location.root << " location path: " \ // std::cout << "location root: " << client->assigned_location->root << " location path: "
<< location.path << '\n'; // << client->assigned_location->path << '\n';
// if ((dir = opendir (path.c_str())) != NULL) // if ((dir = opendir (path.c_str())) != NULL)
if ((dir = opendir ((location.root + location.path).c_str())) != NULL) if ((dir = opendir ((client->assigned_location->root + client->assigned_location->path).c_str())) != NULL)
{ {
/* print all the files and directories within directory */ /* print all the files and directories within directory */
dir_list.append(AUTOINDEX_START); dir_list.append(AUTOINDEX_START);
dir_list.append(location.path); dir_list.append(client->assigned_location->path);
dir_list.append(AUTOINDEX_MID1); dir_list.append(AUTOINDEX_MID1);
dir_list.append(location.path); dir_list.append(client->assigned_location->path);
dir_list.append(AUTOINDEX_MID2); dir_list.append(AUTOINDEX_MID2);
while ((ent = readdir (dir)) != NULL) while ((ent = readdir (dir)) != NULL)
{ {
if (strcmp(".", ent->d_name) == 0) if (strcmp(".", ent->d_name) == 0)
continue ; continue ;
dir_list.append("<a href=\""); dir_list.append("<a href=\"");
dir_list.append(location.path.c_str()); dir_list.append(client->assigned_location->path.c_str());
dir_list.append(ent->d_name); dir_list.append(ent->d_name);
dir_list.append("\">"); dir_list.append("\">");
dir_list.append(ent->d_name); dir_list.append(ent->d_name);
@@ -238,9 +233,9 @@ void Webserv::_autoindex(Client *client, LocationConfig &location, std::string &
// <a href="/test/test_deeper/..">..</a> // <a href="/test/test_deeper/..">..</a>
dir_list.append(AUTOINDEX_END); dir_list.append(AUTOINDEX_END);
std::cout << "\n\n" << dir_list << '\n'; // std::cout << "\n\n" << dir_list << '\n';
closedir (dir); closedir (dir);
_append_body(client, dir_list.c_str(), dir_list.size(), "html"); _append_body(client, dir_list, "html");
} }
else else
{ {