Location sorter works, fixed the compilation error i was having on this branch, now working on autoindex

This commit is contained in:
Me
2022-08-07 03:24:49 +02:00
parent 4b1baca126
commit 4870b2c05d
6 changed files with 45 additions and 53 deletions

View File

@@ -71,16 +71,12 @@ public:
}
// works a lot better than using a compare function...
// but it still doesn't work...
// problem is the logic
bool operator<(const LocationConfig& rhs) const
{
int comp_lhs = 0;
int comp_rhs = 0;
size_t tmp = 0;
// consider adding 1 to path that ends in a file not folder.
// What are the rules...
// / is smaller than /test
// and /test/test1 is bigger than test
@@ -106,61 +102,25 @@ public:
// ok now we need to add a thing where we check for files vs folders
if (comp_lhs == comp_rhs)
{
if (path_is_valid(root + path) == 2)
--comp_lhs;
if (path_is_valid(rhs.root + rhs.path) == 2)
--comp_rhs;
/*
std::cout << "locations are equal\n";
std::string tmp_path_lhs = root + path;
std::cout << "root + path: " << tmp_path_lhs << '\n';
// const char *c_path_lhs = (root + path).substr(1).c_str();
// could the problem be that i'm in the .hpp ?
const char *c_path_lhs = tmp_path_lhs.substr(1).c_str();
const char *c_path_rhs = (rhs.root + rhs.path).substr(1).c_str();
printf("lhs path: %s\n", c_path_lhs);
printf("rhs path: %s\n", c_path_rhs);
struct stat sl;
struct stat sr;
if (stat(c_path_lhs, &sl) == 0 && S_ISREG(sl.st_mode))
{
std::cout << "lhs is a file\n";
--comp_lhs;
}
if (stat(c_path_rhs, &sr) == 0 && S_ISREG(sr.st_mode))
{
std::cout << "rhs is a file\n";
--comp_rhs;
}
// do i need to free c_path's ???
*/
}
std::cout << "comp_lhs: " << comp_lhs << " comp_rhs: " << comp_rhs \
<< " bool res: " << (comp_lhs > comp_rhs) << "\n";
// std::cout << "comp_lhs: " << comp_lhs << " comp_rhs: " << comp_rhs
// << " bool res: " << (comp_lhs > comp_rhs) << "\n";
// i honestly can't tell you how or why but using > rather than <
// fixed all my problems
// the fact that the bool was always 0 before, and the correct order
// i want... super weird...
// return (comp_lhs > comp_rhs); // right comparison ? not <= ?
// return (comp_lhs > comp_rhs);
return (comp_lhs < comp_rhs); // right comparison ? not <= ?
};
};
// ok it needs to go somewhere else
#endif

View File

@@ -29,7 +29,8 @@ void ConfigParser::_post_processing(std::vector<ServerConfig> *servers)
// if Allow methodes not specified we set to ALL
if (it->allow_methods == UNKNOWN) // in this case that means nothing...
it->allow_methods = ALL_METHODS;
it->allow_methods = ANY_METHODS;
// would prefer ALL_METHODS
if (it->index.empty())
throw std::invalid_argument("Config file needs an Index");

View File

@@ -61,7 +61,8 @@ http_method str_to_http_method(std::string &str)
else if (str == "DELETE")
return DELETE;
else if (str == "ALL_METHODS")
return ALL_METHODS;
return ANY_METHODS;
// would prefere ALL_METHODS
return UNKNOWN;
}
@@ -87,6 +88,7 @@ std::string http_methods_to_str(unsigned int methods)
return (str);
}
// you could make this &path...
int path_is_valid(std::string path)
{
std::string i_path = path.substr(1);

View File

@@ -28,6 +28,8 @@ enum http_method
POST = 1 << 1,
DELETE = 1 << 2,
ANY_METHODS = 0b11111111,
// ALL_METHODS = 0b11111111,
// i would prefer this...
};
struct listen_socket
@@ -47,6 +49,6 @@ std::string trim(std::string str, char c);
http_method str_to_http_method(std::string &str);
std::string http_methods_to_str(unsigned int methods);
int path_is_valid(std::string path);
void replace_all_substr(std::string &str, const std::string &ori_substr, const std::string &new_substr)
void replace_all_substr(std::string &str, const std::string &ori_substr, const std::string &new_substr);
#endif

View File

@@ -81,6 +81,11 @@ class Webserv
void _append_body(Client *client, const char *body, size_t body_size, const std::string &file_extension = "");
void _get(Client *client, ServerConfig &server, LocationConfig &location);
// in progress
void _autoindex(Client *client, std::string &path);
void _get_file(Client *client, const std::string &path);
void _post(Client *client, ServerConfig &server, LocationConfig &location);

View File

@@ -143,6 +143,10 @@ void Webserv::_get(Client *client, ServerConfig &server, LocationConfig &locatio
std::cerr << "path = " << path << "\n";
std::cout << "ERIC path: " << path << '\n';
// if (path_is_valid(
// TMP HUGO
//
if (_is_cgi(client))
@@ -156,6 +160,30 @@ void Webserv::_get(Client *client, ServerConfig &server, LocationConfig &locatio
_get_file(client, path);
}
// i might need some sort of _generate_autoindex()
void Webserv::_autoindex(Client *client, std::string &path)
{
// i think the plan is to generate an html file and return it
// it should be filled with the stuff that is found in that repo
// either i create a tmp file and put the html in it an call
// the _get_file on it with a new path...
// or i make the html and do what _get_file does if it found
// a valid file...
// Let's try the 2nd one first.
// if successful we call _append_body
}
void Webserv::_get_file(Client *client, const std::string &path)
{
std::ifstream ifd; // For chunk, ifstream directly in struct CLient for multiples read without close() ?
@@ -223,6 +251,7 @@ void Webserv::_get_file(Client *client, const std::string &path)
}
}
void Webserv::_append_body(Client *client, const char *body, size_t body_size, const std::string &file_extension)
{
/*
@@ -385,13 +414,6 @@ ServerConfig &Webserv::_determine_process_server(Client *client)
LocationConfig &Webserv::_determine_location(ServerConfig &server, std::string &path)
{
/*
Assume there is at least one location in vector<LocationConfig> for path "/"
TODO in ConfigParser :
If no location block in config file, one need to be generated
for path "/", and filled with fields "root" and "index" based on parent server block
*/
std::vector<LocationConfig>::iterator it = server.locations.begin();
while (it != server.locations.end())
{