diff --git a/conf/simple.conf b/conf/simple.conf index 1bd3455..b412eff 100644 --- a/conf/simple.conf +++ b/conf/simple.conf @@ -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 diff --git a/mandatory/inc/CGI.hpp b/mandatory/inc/CGI.hpp index 1cef87b..31e1243 100644 --- a/mandatory/inc/CGI.hpp +++ b/mandatory/inc/CGI.hpp @@ -11,6 +11,7 @@ #include "colors.h" #include "StatusCodesDefinition.hpp" #include +#include "ExtendedString.hpp" class CGI { private: @@ -18,7 +19,7 @@ class CGI { std::string fileName; std::string fileArgs; bool isCGI; - std::vector args; + std::vector args; std::map CGIExtensions; public: @@ -39,7 +40,7 @@ class CGI { //getters std::string getFileName(); std::string getCGIFolder(); - std::vector getArgs(); + std::vector getArgs(); std::string getCGIExtension(const std::string &); std::string getFileExtension(); std::map::iterator findCGIExtension(const std::string &); diff --git a/mandatory/src/CGI.cpp b/mandatory/src/CGI.cpp index 3c9d440..d48ba56 100644 --- a/mandatory/src/CGI.cpp +++ b/mandatory/src/CGI.cpp @@ -39,16 +39,17 @@ void CGI::setFileName(const std::string &Name, const std::string &Args) std::vector tmp; fileName = Name; - tmp = splitString(Args, '?'); - std::map::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::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) @@ -86,14 +87,17 @@ std::string CGI::getCGIFolder() void CGI::setArgs(const std::string &str) { + std::cout << "Args: " << str << std::endl; std::vector 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 CGI::getArgs() +std::vector CGI::getArgs() { return args; } @@ -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 argsArray; - argsArray.push_back(const_cast(fileName.c_str())); - std::vector::iterator itb = args.begin(); - std::vector::iterator ite = args.end(); + std::cout << "Executable: " << Executable << std::endl; + std::vector ExecArray; + ExecArray.push_back(const_cast(Executable.c_str())); + std::vector::iterator itb = args.begin(); + std::vector::iterator ite = args.end(); while (itb != ite) { - argsArray.push_back(const_cast(itb->c_str())); + ExecArray.push_back(const_cast(itb->c_str())); ++itb; } - argsArray.push_back(NULL); + ExecArray.push_back(NULL); if (pipe(fd) == -1) { throw INTERNAL_SERVER_ERROR_CODE; } @@ -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); } } diff --git a/mandatory/src/LocationParser.cpp b/mandatory/src/LocationParser.cpp index b9db189..afab276 100644 --- a/mandatory/src/LocationParser.cpp +++ b/mandatory/src/LocationParser.cpp @@ -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()) diff --git a/www/web4/cgi/test.cgi b/www/web4/cgi/test.cgi index caabec9..29f5a98 100755 Binary files a/www/web4/cgi/test.cgi and b/www/web4/cgi/test.cgi differ diff --git a/www/web4/cgi/test.cpp b/www/web4/cgi/test.cpp index f47ee7e..d0728f3 100644 --- a/www/web4/cgi/test.cpp +++ b/www/web4/cgi/test.cpp @@ -27,23 +27,7 @@ int main(int av, char **ac) for (int i = 1; i < av; i++) { - cout << "" << "Argumento de entrada NÂș" << i << "= " << ac[i] << "\n"; - } - for (int i = 0; i < 24; i++) - { - cout << "" << ENV[i] << ""; - - // 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 << "\n"; + cout << "" << "Argumento de entrada N." << i << "= " << ac[i] << "\n"; } cout << "<\n";