done root substitution

+ autoindex/index adjustement
This commit is contained in:
LuckyLaszlo
2022-08-10 19:59:05 +02:00
parent 86f7740984
commit 360c27df5f
4 changed files with 38 additions and 79 deletions

View File

@@ -1,61 +1,16 @@
#include "Webserv.hpp"
// TODO : path_is_valid() Macro for return value
void Webserv::_get(Client *client)
{
/* 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!
Where does cgi fit in in all this ???
*/
std::string path = client->get_rq_abs_path();
// this might not be the best thing, a voir
path.insert(0, client->assigned_location->root);
std::cerr << "path before = " << path << "\n"; // DEBUG
path.replace(0, client->assigned_location->path.size(), client->assigned_location->root);
std::cerr << "path after = " << path << "\n"; // DEBUG
std::cerr << "path = " << path << "\n";
// path = root + location.path
// we will tack on an index if there is a valid one
// or autoindex if allowed
// or let _get_file sort out the error otherwise.
if (path_is_valid(path) == 1)
{
// std::cout << "path is valid\n";
if (path[path.size() - 1] != '/')
path.push_back('/');
for (size_t i = 0; i < client->assigned_location->index.size(); i++)
{
// std::cout << "location path: " << client->assigned_location->path << '\n';
// std::cout << "location index: " << client->assigned_location->index[i] << '\n';
// std::cout << "path with index: " << path + assigned_location->index[i] << '\n';
if (path_is_valid(path + client->assigned_location->index[i]) == 2)
{
// std::cout << "found a valid index\n";
path.append(client->assigned_location->index[i]);
_get_file(client, path);
return ;
}
}
if (client->assigned_location->autoindex == true)
{
_autoindex(client, path);
return ;
}
}
// else
// _get_file(client, path);
// what about cgi ???
// TMP HUGO
// TMP HUGO ( We move this in process_switch() )
//
std::string script_output;
if (_is_cgi(client))
@@ -71,7 +26,27 @@ Where does cgi fit in in all this ???
//
// END TMP HUGO
_get_file(client, path);
// Index/Autoindex block
if (path_is_valid(path) == 1)
{
if (path[path.size() - 1] != '/')
path.push_back('/');
for (size_t i = 0; i < client->assigned_location->index.size(); i++)
{
if (path_is_valid(path + client->assigned_location->index[i]) == 2)
{
path.append(client->assigned_location->index[i]);
_get_file(client, path);
return ;
}
}
if (client->assigned_location->autoindex == true)
_autoindex(client, path);
}
else
_get_file(client, path);
}
# define MAX_FILESIZE 1000000 // (1Mo)
@@ -142,7 +117,6 @@ void Webserv::_autoindex(Client *client, std::string &path)
{
// std::cout << "made it to _autoindex\n";
(void)path;
std::string dir_list;
DIR *dir;
struct dirent *ent;
@@ -150,21 +124,20 @@ void Webserv::_autoindex(Client *client, std::string &path)
// std::cout << "location root: " << client->assigned_location->root << " location path: "
// << client->assigned_location->path << '\n';
// if ((dir = opendir (path.c_str())) != NULL)
if ((dir = opendir ((client->assigned_location->root + client->assigned_location->path).c_str())) != NULL)
if ( (dir = opendir(path.c_str()) ) != NULL)
{
/* print all the files and directories within directory */
dir_list.append(AUTOINDEX_START);
dir_list.append(client->assigned_location->path);
dir_list.append(path);
dir_list.append(AUTOINDEX_MID1);
dir_list.append(client->assigned_location->path);
dir_list.append(path);
dir_list.append(AUTOINDEX_MID2);
while ((ent = readdir (dir)) != NULL)
{
if (strcmp(".", ent->d_name) == 0)
continue ;
dir_list.append("<a href=\"");
dir_list.append(client->assigned_location->path.c_str());
dir_list.append(path);
dir_list.append(ent->d_name);
dir_list.append("\">");
dir_list.append(ent->d_name);