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,11 +1,8 @@
IN 42 SUBJECT AND/OR PRIORITY : IN 42 SUBJECT AND/OR PRIORITY :
- Fix "href" in autoindex
- CGI - CGI
- chunked request (response not mandatory it seems) - chunked request (response not mandatory it seems)
- fix need for index and autoindex config
- Ecrire des tests ! - Ecrire des tests !
- "root" need to replace "location->path" part of "client.path"
replace up to the last "/" of the "location->path" part
(if its a folder this will be in fact the entire path)
- handle redirection (Work, but weird behavior need deeper test) - handle redirection (Work, but weird behavior need deeper test)
- upload files with config "upload_dir" - upload files with config "upload_dir"
- _determine_location() review (New version to complete and test) - _determine_location() review (New version to complete and test)

View File

@@ -1,14 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ConfigParser.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/13 22:11:17 by me #+# #+# */
/* Updated: 2022/08/03 17:51:35 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
#include "ConfigParser.hpp" #include "ConfigParser.hpp"
@@ -231,9 +220,9 @@ void ConfigParser::_set_server_values(ServerConfig *server, \
} }
else if (key == "root" && size == 1 && server->root == "") else if (key == "root" && size == 1 && server->root == "")
{ {
// remove trailing / // append '/' if missing
if (tmp_val[0][tmp_val[0].size() - 1] == '/') if (tmp_val[0][tmp_val[0].size() - 1] != '/')
tmp_val[0].erase(tmp_val[0].size() - 1, 1); tmp_val[0].push_back('/');
server->root = tmp_val[0]; server->root = tmp_val[0];
} }
else if (key == "client_body_limit" && size == 1 \ else if (key == "client_body_limit" && size == 1 \
@@ -280,9 +269,9 @@ void ConfigParser::_set_location_values(LocationConfig *location, \
throw std::invalid_argument("missing value"); throw std::invalid_argument("missing value");
else if (key == "root" && size == 1 && location->root == "") else if (key == "root" && size == 1 && location->root == "")
{ {
// remove trailing / // append '/' if missing
if (tmp_val[0][tmp_val[0].size() - 1] == '/') if (tmp_val[0][tmp_val[0].size() - 1] != '/')
tmp_val[0].erase(tmp_val[0].size() - 1, 1); tmp_val[0].push_back('/');
location->root = tmp_val[0]; location->root = tmp_val[0];
} }
else if (key == "autoindex" && size == 1) else if (key == "autoindex" && size == 1)

View File

@@ -52,7 +52,7 @@ void ConfigParser::_post_processing(std::vector<ServerConfig> *servers)
if (it_l->allow_methods == UNKNOWN) if (it_l->allow_methods == UNKNOWN)
it_l->allow_methods = ANY_METHODS; it_l->allow_methods = ANY_METHODS;
if (it_l->index.empty()) if (it_l->index.empty() && it_l->autoindex == false)
it_l->index = it->index; it_l->index = it->index;
// nothing to be done for cgi_ext, error_pages, redirect // nothing to be done for cgi_ext, error_pages, redirect

View File

@@ -1,61 +1,16 @@
#include "Webserv.hpp" #include "Webserv.hpp"
// TODO : path_is_valid() Macro for return value
void Webserv::_get(Client *client) 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(); std::string path = client->get_rq_abs_path();
// this might not be the best thing, a voir std::cerr << "path before = " << path << "\n"; // DEBUG
path.insert(0, client->assigned_location->root); path.replace(0, client->assigned_location->path.size(), client->assigned_location->root);
std::cerr << "path after = " << path << "\n"; // DEBUG
std::cerr << "path = " << path << "\n"; // TMP HUGO ( We move this in process_switch() )
// 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
// //
std::string script_output; std::string script_output;
if (_is_cgi(client)) if (_is_cgi(client))
@@ -71,7 +26,27 @@ Where does cgi fit in in all this ???
// //
// END TMP HUGO // END TMP HUGO
// 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); _get_file(client, path);
return ;
}
}
if (client->assigned_location->autoindex == true)
_autoindex(client, path);
}
else
_get_file(client, path);
} }
# define MAX_FILESIZE 1000000 // (1Mo) # define MAX_FILESIZE 1000000 // (1Mo)
@@ -142,7 +117,6 @@ 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;
@@ -150,21 +124,20 @@ void Webserv::_autoindex(Client *client, std::string &path)
// std::cout << "location root: " << client->assigned_location->root << " location path: " // std::cout << "location root: " << client->assigned_location->root << " location path: "
// << client->assigned_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 ((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(client->assigned_location->path); dir_list.append(path);
dir_list.append(AUTOINDEX_MID1); dir_list.append(AUTOINDEX_MID1);
dir_list.append(client->assigned_location->path); dir_list.append(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(client->assigned_location->path.c_str()); dir_list.append(path);
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);