Skip to content

Commit

Permalink
Corrections in executable CGI
Browse files Browse the repository at this point in the history
  • Loading branch information
ErnestoAvedillo committed Jun 25, 2024
1 parent 3f15649 commit 7cb302f
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 43 deletions.
12 changes: 9 additions & 3 deletions conf/simple.conf
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,15 @@ server:{
root:./www/web4; # root folder of site directory, full or relative path, mandatory parameter
autoindex: on; # cgi-bin folder
index:index.html; # default page when requesting a directory, index.html by default
# CGI_folder:./www/web4/cgi/; # cgi-bin folder
# CGI_extension:cgi; # cgi-bin folder
};
location:{
name:/cgi-bin;
alias:./www/web4;
allow_methods: GET, POST, DELETE;
autoindex:on;
cgi_path:/bin/sh;
cgi_extension:cgi;
}
};

server:{
port:9002; # listening port, mandatory parameter
Expand Down
5 changes: 3 additions & 2 deletions mandatory/inc/CGI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
#include "colors.h"
#include "StatusCodesDefinition.hpp"
#include <signal.h>
#include "ExtendedString.hpp"

class CGI {
private:
std::string CGIFolder;
std::string fileName;
std::string fileArgs;
bool isCGI;
std::vector <std::string> args;
std::vector <ExtendedString> args;
std::map<std::string, std::string> CGIExtensions;

public:
Expand All @@ -39,7 +40,7 @@ class CGI {
//getters
std::string getFileName();
std::string getCGIFolder();
std::vector <std::string> getArgs();
std::vector <ExtendedString> getArgs();
std::string getCGIExtension(const std::string &);
std::string getFileExtension();
std::map<std::string, std::string>::iterator findCGIExtension(const std::string &);
Expand Down
46 changes: 26 additions & 20 deletions mandatory/src/CGI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,17 @@ void CGI::setFileName(const std::string &Name, const std::string &Args)
std::vector<std::string> tmp;

fileName = Name;
tmp = splitString(Args, '?');
std::map<std::string, std::string>::iterator it = this->findCGIExtension(this->getFileExtension());
if (it != this->CGIExtensions.end())
{
if (it->second.size() != 0)
{
fileName =it->second;
this->setArgs(tmp[1]);
}
}
this->setArgs(Args);
tmp = splitString(Args, '&');
// std::map<std::string, std::string>::iterator it = this->findCGIExtension(this->getFileExtension());
// if (it != this->CGIExtensions.end())
// {
// if (it->second.size() != 0)
// {
// fileName =it->second;
// this->setArgs(tmp[1]);
// }
// }
}

void CGI::setIsCGI(bool valCGI)
Expand Down Expand Up @@ -86,14 +87,17 @@ std::string CGI::getCGIFolder()

void CGI::setArgs(const std::string &str)
{
std::cout << "Args: " << str << std::endl;
std::vector<std::string> vec = splitString(str, '&');
for (size_t i = 0; i < vec.size(); i++)
{
std::cout << "Args: " << vec[i] << std::endl;
args.push_back(vec[i]);
}

}

std::vector <std::string> CGI::getArgs()
std::vector <ExtendedString> CGI::getArgs()
{
return args;
}
Expand All @@ -111,19 +115,21 @@ std::string CGI::getFileExtension()
std::string CGI::execute()
{
int timeout = 5;

ExtendedString Executable = this->getFileName();
Executable.replaceString("//", "/");
signal(SIGALRM, &CGI::alarm_handler);
int fd[2], tmp_fd;
tmp_fd = dup(STDOUT_FILENO);
std::vector<char*> argsArray;
argsArray.push_back(const_cast<char *>(fileName.c_str()));
std::vector<std::string>::iterator itb = args.begin();
std::vector<std::string>::iterator ite = args.end();
std::cout << "Executable: " << Executable << std::endl;
std::vector<char*> ExecArray;
ExecArray.push_back(const_cast<char *>(Executable.c_str()));
std::vector<ExtendedString>::iterator itb = args.begin();
std::vector<ExtendedString>::iterator ite = args.end();
while (itb != ite) {
argsArray.push_back(const_cast<char *>(itb->c_str()));
ExecArray.push_back(const_cast<char *>(itb->c_str()));
++itb;
}
argsArray.push_back(NULL);
ExecArray.push_back(NULL);
if (pipe(fd) == -1) {
throw INTERNAL_SERVER_ERROR_CODE;
}
Expand All @@ -139,10 +145,10 @@ std::string CGI::execute()
close(fd[0]);
// Convert the arguments vector to a null-terminated array
// Execute the file with its parameters
if (execve(fileName.c_str(), argsArray.data(), NULL) == -1)
if (execve(Executable.c_str(), ExecArray.data(), NULL) == -1)
{
// Handle error executing file
std::cerr << "Error executing file " << fileName << " with errno " << errno << std::endl;
std::cerr << "Error executing file " << Executable << " with errno " << errno << std::endl;
exit(EXIT_FAILURE);
}
}
Expand Down
2 changes: 1 addition & 1 deletion mandatory/src/LocationParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ void LocationParser::checks()
std::string path;
if (this->request.getPath().find("?") != std::string::npos)
{
this->query = this->request.getPath().substr(this->request.getPath().find("?"));
this->query = this->request.getPath().substr(this->request.getPath().find("?") + 1);
this->request.setPath(this->request.getPath().substr(0, this->request.getPath().find("?")));
}
switch (this->matchingLocation())
Expand Down
Binary file modified www/web4/cgi/test.cgi
Binary file not shown.
18 changes: 1 addition & 17 deletions www/web4/cgi/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,7 @@ int main(int av, char **ac)

for (int i = 1; i < av; i++)
{
cout << "<tr><td>" << "Argumento de entrada Nº" << i << "= " << ac[i] << "</td></tr>\n";
}
for (int i = 0; i < 24; i++)
{
cout << "<tr><td>" << ENV[i] << "</td><td>";

// attempt to retrieve value of environment variable
char *value = getenv(ENV[i].c_str());
if (value != 0)
{
cout << value;
}
else
{
cout << "Environment variable does not exist.";
}
cout << "</td></tr>\n";
cout << "<tr><td>" << "Argumento de entrada N." << i << "= " << ac[i] << "</td></tr>\n";
}

cout << "</table><\n";
Expand Down

0 comments on commit 7cb302f

Please sign in to comment.