autoindex is working, a few things to iron out be we well on our way
This commit is contained in:
@@ -12,7 +12,8 @@ server {
|
|||||||
index index.html; # this is another comment
|
index index.html; # this is another comment
|
||||||
# i think root requires leading /
|
# i think root requires leading /
|
||||||
# root goop;
|
# root goop;
|
||||||
root /www;
|
# root ./www/;
|
||||||
|
root ./www;
|
||||||
# root www/test; # also works
|
# root www/test; # also works
|
||||||
# root /www; # stat does not like this kind of definition
|
# root /www; # stat does not like this kind of definition
|
||||||
# root /usr; # because it's looking at / aka absolute path
|
# root /usr; # because it's looking at / aka absolute path
|
||||||
@@ -21,7 +22,7 @@ server {
|
|||||||
|
|
||||||
location /test {
|
location /test {
|
||||||
# root /www/test;
|
# root /www/test;
|
||||||
index index.html;
|
autoindex on;
|
||||||
}
|
}
|
||||||
# If not explicitly set, ConfigParser need to genererate a location block
|
# If not explicitly set, ConfigParser need to genererate a location block
|
||||||
# like this for path "/" (based on field "root" and "index" of the server)
|
# like this for path "/" (based on field "root" and "index" of the server)
|
||||||
|
|||||||
@@ -60,6 +60,7 @@ public:
|
|||||||
|
|
||||||
std::cout << "Path: " << path << '\n';
|
std::cout << "Path: " << path << '\n';
|
||||||
std::cout << "root: " << root << '\n';
|
std::cout << "root: " << root << '\n';
|
||||||
|
std::cout << "autoindex: " << autoindex << '\n';
|
||||||
|
|
||||||
std::cout << "Skipping index...\n";
|
std::cout << "Skipping index...\n";
|
||||||
|
|
||||||
|
|||||||
@@ -147,8 +147,8 @@ LocationConfig ConfigParser::_parse_location(size_t *start)
|
|||||||
ret.allow_methods = 0;
|
ret.allow_methods = 0;
|
||||||
|
|
||||||
ret.path = _get_first_word(&curr);
|
ret.path = _get_first_word(&curr);
|
||||||
if (ret.path[0] != '/')
|
// if (ret.path[0] != '/')
|
||||||
throw std::invalid_argument("Location path require a leading /");
|
// throw std::invalid_argument("Location path require a leading /");
|
||||||
// in theory now curr should be right after the "path"
|
// in theory now curr should be right after the "path"
|
||||||
|
|
||||||
curr = _content.find_first_not_of(" \t\n", curr);
|
curr = _content.find_first_not_of(" \t\n", curr);
|
||||||
@@ -232,14 +232,15 @@ void ConfigParser::_set_server_values(ServerConfig *server, \
|
|||||||
}
|
}
|
||||||
else if (key == "root" && size == 1 && server->root == "")
|
else if (key == "root" && size == 1 && server->root == "")
|
||||||
{
|
{
|
||||||
if (tmp_val[0][0] != '/')
|
// if (tmp_val[0][0] != '/')
|
||||||
throw std::invalid_argument("Root requires leading /");
|
// throw std::invalid_argument("Root requires leading /");
|
||||||
|
|
||||||
// std::cout << "root: " << tmp_val[0] << '\n';
|
// std::cout << "root: " << tmp_val[0] << '\n';
|
||||||
if (path_is_valid(tmp_val[0]) == 1)
|
//might not even do these checks here...
|
||||||
|
// if (path_is_valid(tmp_val[0]) == 1)
|
||||||
server->root = tmp_val[0];
|
server->root = tmp_val[0];
|
||||||
else
|
// else
|
||||||
throw std::invalid_argument("Root dir invalid 1");
|
// throw std::invalid_argument("Root dir invalid 1");
|
||||||
}
|
}
|
||||||
else if (key == "client_body_limit" && size == 1 \
|
else if (key == "client_body_limit" && size == 1 \
|
||||||
&& server->client_body_limit == 0)
|
&& server->client_body_limit == 0)
|
||||||
@@ -303,17 +304,18 @@ void ConfigParser::_set_location_values(LocationConfig *location, \
|
|||||||
else if (key == "root" && size == 1 && location->root == "")
|
else if (key == "root" && size == 1 && location->root == "")
|
||||||
{
|
{
|
||||||
// std::cout << "location root: " << tmp_val[0] << '\n';
|
// std::cout << "location root: " << tmp_val[0] << '\n';
|
||||||
if (tmp_val[0][0] != '/')
|
// if (tmp_val[0][0] != '/')
|
||||||
throw std::invalid_argument("Root requires leading /");
|
// throw std::invalid_argument("Root requires leading /");
|
||||||
|
|
||||||
if (path_is_valid(tmp_val[0]) == 1)
|
// if (path_is_valid(tmp_val[0]) == 1)
|
||||||
location->root = tmp_val[0];
|
location->root = tmp_val[0];
|
||||||
else
|
// else
|
||||||
throw std::invalid_argument("Root dir invalid");
|
// throw std::invalid_argument("Root dir invalid");
|
||||||
}
|
}
|
||||||
else if (key == "autoindex" && size == 1)
|
else if (key == "autoindex" && size == 1)
|
||||||
{
|
{
|
||||||
location->autoindex = (tmp_val[0] == "on" ? true : false);
|
location->autoindex = (tmp_val[0] == "on" ? true : false);
|
||||||
|
std::cout << "in parser " << location->path << " autoindex: " << location->autoindex << '\n';
|
||||||
}
|
}
|
||||||
else if (key == "index")
|
else if (key == "index")
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -65,11 +65,12 @@ void ConfigParser::_post_processing(std::vector<ServerConfig> *servers)
|
|||||||
// maybe do something for Cgi_info?
|
// maybe do something for Cgi_info?
|
||||||
|
|
||||||
// std::cout << "In Post, Root + Path: " << it_l->root + it_l->path << '\n';
|
// std::cout << "In Post, Root + Path: " << it_l->root + it_l->path << '\n';
|
||||||
if (path_is_valid(it_l->root + it_l->path) == 0)
|
/* if (path_is_valid(it_l->root + it_l->path) == 0)
|
||||||
{
|
{
|
||||||
//either we throw or we erase
|
//either we throw or we erase
|
||||||
throw std::invalid_argument("location path is invalid");
|
throw std::invalid_argument("location path is invalid");
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
++it_l;
|
++it_l;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -88,11 +88,14 @@ std::string http_methods_to_str(unsigned int methods)
|
|||||||
return (str);
|
return (str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# include <iostream>
|
||||||
|
|
||||||
// you could make this &path...
|
// you could make this &path...
|
||||||
int path_is_valid(std::string path)
|
int path_is_valid(std::string path)
|
||||||
{
|
{
|
||||||
std::string i_path = path.substr(1);
|
// std::string i_path = path.substr(1);
|
||||||
const char *tmp_path = i_path.c_str();
|
// const char *tmp_path = i_path.c_str();
|
||||||
|
const char *tmp_path = path.c_str();
|
||||||
struct stat s;
|
struct stat s;
|
||||||
|
|
||||||
if (stat(tmp_path, &s) == 0)
|
if (stat(tmp_path, &s) == 0)
|
||||||
|
|||||||
@@ -10,13 +10,20 @@
|
|||||||
"<head>"\
|
"<head>"\
|
||||||
"<title> Index of "
|
"<title> Index of "
|
||||||
|
|
||||||
# define AUTOINDEX_MID \
|
# define AUTOINDEX_MID1 \
|
||||||
"</title>"\
|
"</title>"\
|
||||||
"</head>"\
|
"</head>"\
|
||||||
"<body>"
|
"<body>" \
|
||||||
|
"<h1>Index of "
|
||||||
|
|
||||||
|
# define AUTOINDEX_MID2 \
|
||||||
|
"</h1>"\
|
||||||
|
"<hr>"\
|
||||||
|
"<pre>"
|
||||||
|
|
||||||
# define AUTOINDEX_END \
|
# define AUTOINDEX_END \
|
||||||
|
"</pre>"\
|
||||||
|
"<hr>"\
|
||||||
"</body>"\
|
"</body>"\
|
||||||
"</html>"
|
"</html>"
|
||||||
|
|
||||||
|
|||||||
@@ -145,11 +145,22 @@ void Webserv::_get(Client *client, ServerConfig &server, LocationConfig &locatio
|
|||||||
|
|
||||||
std::cout << "ERIC path: " << path << '\n';
|
std::cout << "ERIC path: " << path << '\n';
|
||||||
|
|
||||||
/* if (path_is_valid(path) == 1 && location.autoindex == true)
|
/* 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!
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
// std::cout << "location: " << location.path << " autoindex: " << location.autoindex << '\n';
|
||||||
|
if (path_is_valid(path) == 1 && location.autoindex == true)
|
||||||
{
|
{
|
||||||
|
std::cout << "about to call autoindex\n";
|
||||||
_autoindex(client, location, path);
|
_autoindex(client, location, path);
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
// TMP HUGO
|
// TMP HUGO
|
||||||
//
|
//
|
||||||
@@ -182,17 +193,45 @@ void Webserv::_autoindex(Client *client, LocationConfig &location, std::string &
|
|||||||
// Let's try the 2nd one first.
|
// Let's try the 2nd one first.
|
||||||
|
|
||||||
std::cout << "made it to _autoindex\n";
|
std::cout << "made it to _autoindex\n";
|
||||||
|
std::string dir_list;
|
||||||
|
|
||||||
DIR *dir;
|
DIR *dir;
|
||||||
struct dirent *ent;
|
struct dirent *ent;
|
||||||
|
|
||||||
if ((dir = opendir ((location.root + location.path).c_str())) != NULL) {
|
if ((dir = opendir ((location.root + location.path).c_str())) != NULL)
|
||||||
|
{
|
||||||
/* print all the files and directories within directory */
|
/* print all the files and directories within directory */
|
||||||
while ((ent = readdir (dir)) != NULL) {
|
dir_list.append(AUTOINDEX_START);
|
||||||
printf ("%s\n", ent->d_name);
|
dir_list.append(location.path);
|
||||||
|
dir_list.append(AUTOINDEX_MID1);
|
||||||
|
dir_list.append(location.path);
|
||||||
|
dir_list.append(AUTOINDEX_MID2);
|
||||||
|
while ((ent = readdir (dir)) != NULL)
|
||||||
|
{
|
||||||
|
// printf ("%s\n", ent->d_name);
|
||||||
|
if (strcmp(".", ent->d_name) == 0)
|
||||||
|
continue ;
|
||||||
|
dir_list.append("<a href=\"");
|
||||||
|
dir_list.append(location.path.c_str());
|
||||||
|
// if no / at end of location path...
|
||||||
|
if (location.path.find_last_of("/") != location.path.size())
|
||||||
|
dir_list.append("/");
|
||||||
|
dir_list.append(ent->d_name);
|
||||||
|
dir_list.append("\">");
|
||||||
|
dir_list.append(ent->d_name);
|
||||||
|
dir_list.append("</a>");
|
||||||
|
dir_list.append("\r\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// <a href="http://nginx.org/">nginx.org</a>.<br/>
|
||||||
|
|
||||||
|
dir_list.append(AUTOINDEX_END);
|
||||||
|
_append_body(client, dir_list.c_str(), dir_list.size(), "html");
|
||||||
|
|
||||||
closedir (dir);
|
closedir (dir);
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
/* could not open directory */
|
/* could not open directory */
|
||||||
// perror ("");
|
// perror ("");
|
||||||
std::cout << "could not open dir\n";
|
std::cout << "could not open dir\n";
|
||||||
@@ -201,19 +240,7 @@ void Webserv::_autoindex(Client *client, LocationConfig &location, std::string &
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
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");
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -453,10 +480,14 @@ ServerConfig &Webserv::_determine_process_server(Client *client)
|
|||||||
|
|
||||||
LocationConfig &Webserv::_determine_location(ServerConfig &server, std::string &path)
|
LocationConfig &Webserv::_determine_location(ServerConfig &server, std::string &path)
|
||||||
{
|
{
|
||||||
|
// std::cout << "determin location path sent: " << path << '\n';
|
||||||
|
|
||||||
std::vector<LocationConfig>::iterator it = server.locations.begin();
|
std::vector<LocationConfig>::iterator it = server.locations.begin();
|
||||||
while (it != server.locations.end())
|
while (it != server.locations.end())
|
||||||
{
|
{
|
||||||
if (it->path.compare(0, path.size(), path))
|
// std::cout << it->path << " -- ";
|
||||||
|
// if (it->path.compare(0, path.size(), path) == 0)
|
||||||
|
if (it->path.compare(0, it->path.size(), path) == 0)
|
||||||
break;
|
break;
|
||||||
++it;
|
++it;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user