autoindex in progress but need to check stuff on master branch

This commit is contained in:
Me
2022-08-07 20:37:53 +02:00
parent 4870b2c05d
commit f777441edf
9 changed files with 90 additions and 69 deletions

View File

@@ -31,7 +31,6 @@ server {
# allow_methods DELETE;
# }
allow_methods GET POST;
# location /test/something.html {
location /test/something.html {

View File

@@ -38,12 +38,14 @@ public:
std::string path;
int client_body_limit;
std::string root;
std::vector<std::string> index;
unsigned int allow_methods;
std::map<std::string, std::string> cgi_info;
std::vector<std::string> cgi_ext; // php not .php
bool autoindex;
std::vector<std::string> upload_repo;
// wait if i can call several times, shouldn't it be a map?
// wait no there can only be 1 and i think it might have to be in
// location only...
@@ -57,7 +59,6 @@ public:
std::cout << "\nPRINTING A LOCATION\n";
std::cout << "Path: " << path << '\n';
std::cout << "client_body_limit: " << client_body_limit << '\n';
std::cout << "root: " << root << '\n';
std::cout << "Skipping index...\n";
@@ -100,13 +101,14 @@ public:
++comp_rhs;
// ok now we need to add a thing where we check for files vs folders
if (comp_lhs == comp_rhs)
/* 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 << "comp_lhs: " << comp_lhs << " comp_rhs: " << comp_rhs
// << " bool res: " << (comp_lhs > comp_rhs) << "\n";

View File

@@ -33,14 +33,12 @@ public:
unsigned int client_body_limit; // set to default max if none set
// might be the only one we let slide if bad input... It remains false...
bool autoindex;
// we will check the index in the post processing with access() ?
std::vector<std::string> index;
std::map<int, std::string> error_pages;
// fuck it, you can only call allow_methods once in Server
unsigned int allow_methods;
std::vector<LocationConfig> locations;
@@ -71,17 +69,12 @@ public:
std::cout << it->first << "--" << it->second << " ";
// for (size_t i = 0; i < error_pages.size(); i++)
// std::cout << error_pages->first << "--" << error_pages->second << " ";
std::cout << "\nallow_methods: ";
std::cout << ::http_methods_to_str(allow_methods) << "\n";
// std::cout << "skiping Locations for now...\n";
for (std::vector<LocationConfig>::iterator it = locations.begin(); it < locations.end(); it++)
it->print_all();
std::cout << "autoindex: " << autoindex << '\n';
std::cout << "client_body_limit: " << client_body_limit << '\n';
// std::cout << "redirect_status: " << redirect_status << '\n';
// std::cout << "redirect_uri: " << redirect_uri << '\n';
std::cout << "host: " << host << '\n';
std::cout << "port: " << port << '\n';

View File

@@ -105,8 +105,6 @@ ServerConfig ConfigParser::_parse_server(size_t *start)
size_t curr = _content.find_first_not_of(" \t\n", *start);
ret.client_body_limit = 0;
ret.autoindex = false;
ret.allow_methods = 0;
if (curr == std::string::npos || _content[curr] != '{')
throw std::invalid_argument("bad config file syntax 1");
@@ -144,7 +142,7 @@ LocationConfig ConfigParser::_parse_location(size_t *start)
size_t curr = *start;
// start is after the 1st word aka "location"
ret.client_body_limit = 0;
ret.autoindex = false;
ret.redirect_status = 0;
ret.allow_methods = 0;
@@ -243,12 +241,6 @@ void ConfigParser::_set_server_values(ServerConfig *server, \
else
throw std::invalid_argument("Root dir invalid 1");
}
else if (key == "autoindex" && size == 1)
{
// autoindex is a bool, there's no good way for me to see if it has
// bet set already
server->autoindex = (tmp_val[0] == "on" ? true : false);
}
else if (key == "client_body_limit" && size == 1 \
&& server->client_body_limit == 0)
{
@@ -265,16 +257,6 @@ void ConfigParser::_set_server_values(ServerConfig *server, \
for (unsigned long i = 0; i != tmp_val.size(); i++)
server->index.push_back(tmp_val[i]);
}
else if (key == "allow_methods" && server->allow_methods == 0)
{
for (unsigned long i = 0; i != tmp_val.size(); i++)
{
http_method m = ::str_to_http_method(tmp_val[i]);
if (m == UNKNOWN)
throw std::invalid_argument("not a valid method");
server->allow_methods |= m;
}
}
else if (key == "error_page")
{
@@ -329,12 +311,9 @@ void ConfigParser::_set_location_values(LocationConfig *location, \
else
throw std::invalid_argument("Root dir invalid");
}
else if (key == "client_body_limit" && size == 1 \
&& location->client_body_limit == 0)
else if (key == "autoindex" && size == 1)
{
if (!::isNumeric(tmp_val[0]))
throw std::invalid_argument("client_body_limit not a number");
location->client_body_limit = atoi(tmp_val[0].c_str());
location->autoindex = (tmp_val[0] == "on" ? true : false);
}
else if (key == "index")
{
@@ -352,18 +331,16 @@ void ConfigParser::_set_location_values(LocationConfig *location, \
location->allow_methods |= m;
}
}
else if (key == "cgi_info")
else if (key == "cgi_ext")
{
// you can call cgi_info several times i think.
// ok wtf is all this even doing, figure that out
unsigned long i = value.find_first_of(" ");
if (i == std::string::npos)
throw std::invalid_argument("bad config file arguments 8");
// ok why an int now, we gotta be more consistent!
int j = value.find_first_not_of(" ", i);
location->cgi_info[value.substr(0, i)] = value.substr(j, value.length());
for (size_t i = 0; i < tmp_val.size(); i++)
{
if (tmp_val[i][0] == '.')
throw std::invalid_argument("cgi_ext should not have a leading '.'");
location->cgi_ext.push_back(tmp_val[i]);
}
}
else if (key == "return" && location->redirect_status == 0 \
else if (key == "redirect" && location->redirect_status == 0 \
&& location->redirect_uri == "")
{
// actually i think there can only be one per location...

View File

@@ -5,11 +5,6 @@
void ConfigParser::_post_processing(std::vector<ServerConfig> *servers)
{
// make certain servers default
// fill out empty settings
// if special settings are empty throw
std::vector<ServerConfig>::iterator it = servers->begin();
while (it != servers->end())
@@ -25,12 +20,6 @@ void ConfigParser::_post_processing(std::vector<ServerConfig> *servers)
if (it->client_body_limit == 0)
it->client_body_limit = 5000; // what is the recomended size?
// autoindex is False by Default
// if Allow methodes not specified we set to ALL
if (it->allow_methods == UNKNOWN) // in this case that means nothing...
it->allow_methods = ANY_METHODS;
// would prefer ALL_METHODS
if (it->index.empty())
throw std::invalid_argument("Config file needs an Index");
@@ -48,10 +37,10 @@ void ConfigParser::_post_processing(std::vector<ServerConfig> *servers)
LocationConfig tmp;
tmp.path = "/";
tmp.client_body_limit = 5000; // figur out correct amount
tmp.root = it->root;
tmp.index = it->index;
tmp.allow_methods = it->allow_methods;
tmp.allow_methods = ANY_METHODS;
tmp.autoindex = false;
tmp.redirect_status = 0;
it->locations.push_back(tmp);
}
@@ -60,15 +49,11 @@ void ConfigParser::_post_processing(std::vector<ServerConfig> *servers)
while (it_l != it->locations.end())
{
if (it_l->client_body_limit == 0)
it_l->client_body_limit = 5000; // what is the recomended size?
if (it_l->root == "")
it_l->root = it->root;
// if Allow methodes not specified we set to Server methods
if (it_l->allow_methods == UNKNOWN) // in this case that means nothing...
it_l->allow_methods = it->allow_methods;
if (it_l->allow_methods == UNKNOWN)
it_l->allow_methods = ANY_METHODS;
// fill out index from Server?
// or do a bunch of checks on what is in there...
@@ -108,7 +93,7 @@ bool ConfigParser::_find_root_path_location(std::vector<LocationConfig> location
{
if (it->path.compare("/") == 0)
{
std::cout << "in compare: " << it->path << " -- ";
// std::cout << "in compare: " << it->path << " -- ";
return true;
}
++it;

View File

@@ -29,6 +29,7 @@
# include "ServerConfig.hpp"
# include "utils.hpp"
# include "http_status.hpp"
# include "autoindex.hpp"
extern bool g_run;
extern int g_last_signal;
@@ -83,7 +84,7 @@ class Webserv
void _get(Client *client, ServerConfig &server, LocationConfig &location);
// in progress
void _autoindex(Client *client, std::string &path);
void _autoindex(Client *client, LocationConfig &location, std::string &path);
void _get_file(Client *client, const std::string &path);

View File

@@ -0,0 +1,25 @@
#ifndef AUTOINDEX_HPP
# define AUTOINDEX_HPP
// # define HTML_ERROR(STATUS) "\r\n<!DOCTYPE html><html><head><title>"STATUS"</title></head><body><h1 style=\"text-align:center\">"STATUS"</h1><hr><p style=\"text-align:center\">Le Webserv/0.1</p></body></html>"
# define AUTOINDEX_START \
"<!DOCTYPE html>"\
"<html>"\
"<head>"\
"<title> Index of "
# define AUTOINDEX_MID \
"</title>"\
"</head>"\
"<body>"
# define AUTOINDEX_END \
"</body>"\
"</html>"
#endif

View File

@@ -145,7 +145,11 @@ void Webserv::_get(Client *client, ServerConfig &server, LocationConfig &locatio
std::cout << "ERIC path: " << path << '\n';
// if (path_is_valid(
/* if (path_is_valid(path) == 1 && location.autoindex == true)
{
_autoindex(client, location, path);
}
*/
// TMP HUGO
//
@@ -162,7 +166,10 @@ void Webserv::_get(Client *client, ServerConfig &server, LocationConfig &locatio
// i might need some sort of _generate_autoindex()
void Webserv::_autoindex(Client *client, std::string &path)
#include <stdio.h>
#include <dirent.h>
void Webserv::_autoindex(Client *client, LocationConfig &location, 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
@@ -174,7 +181,39 @@ void Webserv::_autoindex(Client *client, std::string &path)
// Let's try the 2nd one first.
std::cout << "made it to _autoindex\n";
DIR *dir;
struct dirent *ent;
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);
}
closedir (dir);
} else {
/* could not open directory */
// perror ("");
std::cout << "could not open dir\n";
return ;
}
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");