is_cgi function is back and is working

+ in utils added function eval_file_mode(), it returns http errors codes about file
This commit is contained in:
Hugo LAMY
2022-08-14 01:05:20 +02:00
parent 285f2d049f
commit db4c7468cc
9 changed files with 100 additions and 35 deletions

View File

@@ -1,32 +1,61 @@
#include "Webserv.hpp"
// TODO HUGO : for the moment, this url valid a cgi, it shouldn't :
// http://localhost:4040/cgi-bin/cgi.phpp
// check the output for an alpha value maybe ? check rfc
size_t Webserv::_cgi_pos(Client *client, std::string &path)
bool Webserv::_is_cgi(Client *client, std::string path)
{
size_t file_type;
size_t file_mode;
size_t pos = 0;
while (pos != NPOS)
{
pos = _cgi_pos(client, path, pos);
/*DEBUG*/ std::cout << "pos:" << pos << "\n&path[pos]:" << &path[pos] << "\n" << B_YELLOW << "fin debug _cgi_pos()\n\n" << RESET;
if (pos == NPOS)
break;
client->fill_script_path(path, pos);
file_type = ::eval_file_type(client->get_rq_script_path());
if (file_type == IS_DIR) // but what if it's a symlink ?
continue;
if (file_type == IS_FILE)
{
file_mode = ::eval_file_mode( client->get_rq_script_path(), X_OK );
if (!file_mode)
return true;
}
}
client->clear_script();
client->status = file_mode; // 404 not_found OR 403 forbidden
return false;
}
size_t Webserv::_cgi_pos(Client *client, std::string &path, size_t pos)
{
std::vector<std::string> v_ext;
std::vector<std::string>::const_iterator it;
std::vector<std::string>::const_iterator it_end;
size_t pos = 0;
size_t len;
std::locale loc; // for isalpha()
/*DEBUG*/ it = client->assigned_location->cgi_ext.begin(); std::cout << B_YELLOW << "\nDEBUG _cgi_pos()\n" << RESET << "vector_ext.size():[" << client->assigned_location->cgi_ext.size() << "]\n\n";
v_ext = client->assigned_location->cgi_ext;
if (v_ext.empty())
return NPOS; /*DEBUG*/ std::cout << "ext:[" << *it << "]\n" << "path:[" << path << "]\n\n";
return NPOS;
/*DEBUG*/ std::cout << "ext:[" << *it << "]\n" << "path:[" << path << "]\n\n";
it_end = client->assigned_location->cgi_ext.end();
while (pos < path.size())
{ /*DEBUG*/ std::cout << "\nwhile\n";
{
/*DEBUG*/ std::cout << "\nwhile\n";
if (path.compare(pos, 2, "./") == 0)
pos += 2; /*DEBUG*/ std::cout << "&path[pos]:[" << &path[pos] << "]\n";
pos += 2;
/*DEBUG*/ std::cout << "&path[pos]:[" << &path[pos] << "]\n";
pos = path.find('.', pos);
if (pos == NPOS)
return pos;
it = client->assigned_location->cgi_ext.begin();
for ( ; it != it_end; ++it)
{ /*DEBUG*/ std::cout << " for\n"; std::cout << " &path[pos]:[" << &path[pos] << "]\n" << " *it:[" << *it << "]\n" << " (*it).size():[" << (*it).size() << "]\n" << " path.substr(pos, (*it).size()):[" << path.substr(pos + 1, (*it).size()) << "]\n\n";
{
/*DEBUG*/ std::cout << " for\n"; std::cout << " &path[pos]:[" << &path[pos] << "]\n" << " *it:[" << *it << "]\n" << " (*it).size():[" << (*it).size() << "]\n" << " path.substr(pos, (*it).size()):[" << path.substr(pos + 1, (*it).size()) << "]\n\n";
len = (*it).size();
if (path.compare(pos + 1, len, *it) == 0)
if ( !std::isalpha(path[pos + 1 + len], loc) )
@@ -130,13 +159,14 @@ std::string Webserv::_exec_script(Client *client, char **env)
close(FD_WR_TO_CHLD);
close(FD_RD_FR_CHLD);
dup2(FD_RD_FR_PRNT, STDIN_FILENO);
dup2(FD_WR_TO_PRNT, STDOUT_FILENO); /*DEBUG*/std::cerr << "execve:\n";
dup2(FD_WR_TO_PRNT, STDOUT_FILENO);
path = "." + client->get_rq_script_path();
/*DEBUG*/std::cerr << "execve\n" << "path:[" << path << "]\n";
execve(path.c_str(), nll, env);
// for tests execve crash :
//execve("wrong", nll, env);
std::cerr << "execve crashed.\n";
// TODO HUGO : check errno
}
else
{