#include "Webserv.hpp"
// const?
void Webserv::_get(Client *client)
{
std::string path = client->get_rq_abs_path();
std::cerr << "path before = " << path << "\n"; // DEBUG
if (client->assigned_location->path == "/")
path.insert(0, client->assigned_location->root);
else
path.replace(0, client->assigned_location->path.size(), client->assigned_location->root);
std::cerr << "path after = " << path << "\n"; // DEBUG
// TMP HUGO ( We move this in process_switch() )
//
std::string script_output;
if (_is_cgi(client))
{
script_output = _exec_cgi(client);
// DEBUG
std::cout << "\n____script_output____\n" << script_output << "\n_______________\n";
// wip check output of script
_check_script_output(client, script_output);
client->response += script_output;
return;
}
//
// END TMP HUGO
// Index/Autoindex block
if (path_is_valid(path) == IS_DIR)
{
std::cout << "made it to Index/Autoindex\n";
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);
return ;
}
}
if (client->assigned_location->autoindex == true)
_autoindex(client, path);
}
else
_get_file(client, path);
}
# define MAX_FILESIZE 1000000 // (1Mo)
void Webserv::_get_file(Client *client, const std::string &path)
{
/*
std::ios::binary
https://gcc.gnu.org/onlinedocs/libstdc++/manual/fstreams.html#std.io.filestreams.binary
tldr : its seems to not be so simple to do read/write of binary file in a portable way.
*/
std::ifstream ifd; // For chunk, ifstream directly in struct CLient for multiples read without close() ?
std::stringstream buf;
std::cout << "made it to get_file\n";
if (access(path.c_str(), F_OK) == -1)
{
std::perror("err access()");
client->status = 404;
return ;
}
if (access(path.c_str(), R_OK) == -1)
{
std::perror("err access()");
client->status = 403;
return ;
}
ifd.open(path.c_str(), std::ios::ate);
if (!ifd)
{
std::cerr << path << ": ifd.open fail" << '\n';
client->status = 500;
}
else
{
/* std::streampos size = ifd.tellg();
// WIP Low priority : Chunk or not chunk (if filesize too big)
if (size > MAX_FILESIZE)
{
// Then chunk
client->status = 500; // WIP temp
std::cerr << "File too large for non chunk body\n";
return ;
} */
ifd.seekg(0, std::ios::beg);
buf << ifd.rdbuf();
if (!ifd || !buf)
{
std::cerr << path << ": ifd.read fail" << '\n';
client->status = 500;
}
else
{
client->status = 200;
std::string file_ext = _determine_file_extension(path);
_append_body(client, buf.str(), file_ext);
}
}
}
// const?
void Webserv::_autoindex(Client *client, std::string &path)
{
std::cout << "made it to _autoindex\n";
std::string dir_list;
DIR *dir;
struct dirent *ent;
std::cout << "location root: " << client->assigned_location->root << " location path: "
<< client->assigned_location->path << '\n';
std::cout << "Path in auto is: " << path << '\n';
if ( (dir = opendir(path.c_str()) ) != NULL)
{
dir_list.append(AUTOINDEX_START);
dir_list.append(path);
dir_list.append(AUTOINDEX_MID1);
dir_list.append(path);
dir_list.append(AUTOINDEX_MID2);
/* print all the files and directories within directory */
while ((ent = readdir (dir)) != NULL)
{
std::cout << "ent: " << ent->d_name << '\n';
if (strcmp(".", ent->d_name) == 0)
continue ;
dir_list.append("assigned_location->path + "/");
dir_list.append(ent->d_name);
dir_list.append("\">");
dir_list.append(ent->d_name);
dir_list.append("");
dir_list.append("\r\n"); // is this right?
}
// nginx.org.
// index1.html
// apparently this is more than good enough!
// ..
dir_list.append(AUTOINDEX_END);
// std::cout << "\n\n" << dir_list << '\n';
closedir (dir);
_append_body(client, dir_list, "html");
}
else
{
// in theory not possible cuz we already checked...
std::cerr << "could not open dir\n";
// throw?
return ;
}
}