Index and Autoindex work nicely, standarized way paths for root and location path are stored in config, still needs a little polishing prolly
This commit is contained in:
@@ -134,32 +134,64 @@ void Webserv::_error_html_response(Client *client, ServerConfig &server)
|
||||
#define INDEX "index.html" // temp wip
|
||||
void Webserv::_get(Client *client, ServerConfig &server, LocationConfig &location)
|
||||
{
|
||||
(void)server; // To remove from arg if we determine its useless
|
||||
std::string path = client->get_path();
|
||||
|
||||
if (path == "/") // TODO : index and autoindex
|
||||
path.append(INDEX);
|
||||
path.insert(0, location.root);
|
||||
|
||||
std::cerr << "path = " << path << "\n";
|
||||
|
||||
std::cout << "ERIC path: " << path << '\n';
|
||||
|
||||
/* 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 ???
|
||||
|
||||
THIS NEEDS WORK...
|
||||
|
||||
*/
|
||||
(void)server; // To remove from arg if we determine its useless
|
||||
std::string path = client->get_path();
|
||||
|
||||
/* if (path == "/") // TODO : index and autoindex
|
||||
path.append(INDEX);
|
||||
path.insert(0, location.root);
|
||||
*/
|
||||
|
||||
// that was actually a horrible idea...
|
||||
path.insert(0, location.root);
|
||||
std::cerr << "path = " << path << "\n";
|
||||
|
||||
// std::cout << "location: " << location.path << " autoindex: " << location.autoindex << '\n';
|
||||
if (path_is_valid(path) == 1 && location.autoindex == true)
|
||||
// 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 << "about to call autoindex\n";
|
||||
_autoindex(client, location, path);
|
||||
std::cout << "path is valid\n";
|
||||
if (path[path.size() - 1] != '/')
|
||||
path.push_back('/');
|
||||
for (size_t i = 0; i < location.index.size(); i++)
|
||||
{
|
||||
std::cout << "location path: " << location.path << '\n';
|
||||
std::cout << "location index: " << location.index[i] << '\n';
|
||||
// std::cout << "path with index: " << path + location.index[i] << '\n';
|
||||
if (path_is_valid(path + location.index[i]) == 2)
|
||||
{
|
||||
std::cout << "found a valid index\n";
|
||||
path.append(location.index[i]);
|
||||
break ; // what if index and autoindex in a single location?
|
||||
// does this completely fail?
|
||||
// do this instead of break?
|
||||
//_get_file(client, path);
|
||||
}
|
||||
}
|
||||
if (location.autoindex == true)
|
||||
{
|
||||
_autoindex(client, location, path);
|
||||
return ;
|
||||
}
|
||||
}
|
||||
// else
|
||||
// _get_file(client, path);
|
||||
// what about cgi ???
|
||||
|
||||
|
||||
|
||||
// TMP HUGO
|
||||
@@ -175,29 +207,21 @@ if file, server that!
|
||||
_get_file(client, path);
|
||||
}
|
||||
|
||||
// i might need some sort of _generate_autoindex()
|
||||
|
||||
#include <stdio.h>
|
||||
#include <dirent.h>
|
||||
|
||||
// i only sort of need &path...
|
||||
// def can improve but works for now...
|
||||
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
|
||||
|
||||
// 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.
|
||||
|
||||
std::cout << "made it to _autoindex\n";
|
||||
std::string dir_list;
|
||||
|
||||
DIR *dir;
|
||||
struct dirent *ent;
|
||||
std::string dir_list;
|
||||
DIR *dir;
|
||||
struct dirent *ent;
|
||||
|
||||
std::cout << "location root: " << location.root << " location path: " \
|
||||
<< location.path << '\n';
|
||||
|
||||
// if ((dir = opendir (path.c_str())) != NULL)
|
||||
if ((dir = opendir ((location.root + location.path).c_str())) != NULL)
|
||||
{
|
||||
/* print all the files and directories within directory */
|
||||
@@ -208,44 +232,36 @@ void Webserv::_autoindex(Client *client, LocationConfig &location, std::string &
|
||||
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");
|
||||
dir_list.append("\r\n"); // is this right?
|
||||
}
|
||||
|
||||
// <a href="http://nginx.org/">nginx.org</a>.<br/>
|
||||
// <a href="/test/test_deeper/index1.html">index1.html</a>
|
||||
|
||||
// apparently this is more than good enough!
|
||||
// <a href="/test/test_deeper/..">..</a>
|
||||
|
||||
dir_list.append(AUTOINDEX_END);
|
||||
_append_body(client, dir_list.c_str(), dir_list.size(), "html");
|
||||
|
||||
std::cout << "\n\n" << dir_list << '\n';
|
||||
closedir (dir);
|
||||
_append_body(client, dir_list.c_str(), dir_list.size(), "html");
|
||||
}
|
||||
else
|
||||
{
|
||||
// in theory not possible cuz we already checked...
|
||||
/* could not open directory */
|
||||
// perror ("");
|
||||
std::cout << "could not open dir\n";
|
||||
return ;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// if successful we call _append_body
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -255,6 +271,8 @@ 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() ?
|
||||
// char buf[MAX_FILESIZE+1];
|
||||
|
||||
std::cout << "made it to get_file\n";
|
||||
|
||||
if (access(path.c_str(), F_OK) == -1)
|
||||
{
|
||||
std::perror("err access()");
|
||||
@@ -489,6 +507,10 @@ LocationConfig &Webserv::_determine_location(ServerConfig &server, std::string &
|
||||
// if (it->path.compare(0, path.size(), path) == 0)
|
||||
if (it->path.compare(0, it->path.size(), path) == 0)
|
||||
break;
|
||||
// kinda gross i know but... have to deal with when there's a / at the end
|
||||
if (it->path[it->path.size() - 1] == '/' \
|
||||
&& it->path.compare(0, it->path.size() - 1, path) == 0)
|
||||
break;
|
||||
++it;
|
||||
}
|
||||
if (it != server.locations.end())
|
||||
|
||||
Reference in New Issue
Block a user