diff --git a/README.md b/README.md index ebc9a05..8a8e866 100644 --- a/README.md +++ b/README.md @@ -2,17 +2,21 @@ ## work together #### next commit -+ fixed pbm in extract_line : delete the line and the newline sequence character -+ fixed pbm n _cgi_pos : last return is npos, not pos ++ in fill_script_path() : scipt_path and script_info are filled, removing leadin dot ++ in _cgi_pos() : check validity of extension if it's only alpha characters ++ severall pbm appears #### TODO hugo -- `_is_cgi()` and `_fill_cgi_path()` -- two cgi tests : -? - a basic form with "name" and "something", that return a html page with that -? - for GET and POST -? - a script called by a file extension in URI +- pbm : `http://localhost:4040/cgi-bin/cgi.sh` #### questions +- does the root always start with leading "." ? +- how should we handle a wrong url like `http://localhost/cgi-bin/wrong.phpp/good.php` ? + - do we serve `./srcs/cgi-bin/good.php` ? + - or do we return 404 "not found" ? + - for now, execve would crash, but that doesn't produce a 404 error, rather a 500, is it bad ? +- if a url has a file with extension, but it's not a cgi extension, is it necessary to look further ? + - ex. `http://localhost/file.php/file.py` for `cgi_ext py;` ? - the response page is received long after the cgi-script is done, why ? --- diff --git a/default.config b/default.config index a5dc5f7..e5cab48 100644 --- a/default.config +++ b/default.config @@ -21,7 +21,7 @@ server { location /cgi-bin { root ./srcs/cgi-bin/; - cgi_ext cgi php txt; + cgi_ext cpp php sh; } location /redirect { diff --git a/srcs/Client.cpp b/srcs/Client.cpp index f064337..70d288b 100644 --- a/srcs/Client.cpp +++ b/srcs/Client.cpp @@ -202,27 +202,26 @@ void Client::parse_request_body() status = 413; // HTTP Client Errors } -// TODO HUGO : faire la fonction, mdr. -void Client::fill_script_path(const std::string &path, size_t pos) +// TODO HUGO : what if the root doesn't start with leading "." ? +void Client::fill_script_path(std::string &path, size_t pos) { - (void)path; - (void)pos; -/* size_t pos; - size_t len = path.size(); - std::string path = this->get_rq_abs_path(); std::string tmp; - pos = path.find(script); - if (pos == 0) + // should start with "/" + // and being relative to the root : + // if the path to the script is "root/cgi-bin/cgi.php" + // the path relative to the root would be : /cgi-bin/cgi.php + // here we receive the path as : "./cgi-bin/cgi.php/optionnal" + // so we should remove the leading "." + /*DEBUG*/ std::cout << "\n" << B_PURPLE << "debug path dot" << RESET << "\npath:[" << path << "]\n" << "&path[pos]:[" << &path[pos] << "]\n"; + if (path[0] == '.') { - tmp = path.substr(0, pos + len); - _request.script.path = "./srcs" + tmp; // TODO: root path ? + path.erase(0, 1); + pos--; + } /*DEBUG*/ std::cout << "path:[" << path << "]\n" << "&path[pos]:[" << &path[pos] << "]\n"; - _request.script.path = "./srcs" + tmp; // TODO: root path ? - _request.script.info = path.substr(pos + len); - return true; - } - return false; */ + _request.script.path = path.substr(0, pos); /*DEBUG*/ std::cout << "script_path:[" << _request.script.path << "]\n"; + _request.script.info = path.substr(pos); /*DEBUG*/ std::cout << "script_info:[" << _request.script.info << "]\n" << B_PURPLE << "end debug path dot" << RESET << "\n"; } void Client::clear() diff --git a/srcs/Client.hpp b/srcs/Client.hpp index d7dfc8b..1c69f49 100644 --- a/srcs/Client.hpp +++ b/srcs/Client.hpp @@ -11,6 +11,7 @@ # include // htonl, htons, ntohl, ntohs, inet_addr, inet_ntoa # include "utils.hpp" # include "ServerConfig.hpp" +# include "colors.h" struct Script { @@ -76,7 +77,7 @@ class Client void clear(); void clear_request(); void clear_script(); - void fill_script_path(const std::string &path, size_t pos); + void fill_script_path(std::string &path, size_t pos); // DEBUG void print_client(std::string message = ""); diff --git a/srcs/cgi-bin/cgi.sh b/srcs/cgi-bin/cgi.sh new file mode 100644 index 0000000..bdfc2b3 --- /dev/null +++ b/srcs/cgi-bin/cgi.sh @@ -0,0 +1,4 @@ +#! /usr/bash +echo "status: 100\r\n" +echo "\r\n\r\n" +echo "hiii" diff --git a/srcs/webserv/Webserv.hpp b/srcs/webserv/Webserv.hpp index ee058d3..bb2a644 100644 --- a/srcs/webserv/Webserv.hpp +++ b/srcs/webserv/Webserv.hpp @@ -25,6 +25,7 @@ # include // perror, remove # include // strtol, strtoul # include // opendir() +# include // isalpha, local # include "Client.hpp" # include "ServerConfig.hpp" diff --git a/srcs/webserv/cgi_script.cpp b/srcs/webserv/cgi_script.cpp index daf7ddb..7c3082d 100644 --- a/srcs/webserv/cgi_script.cpp +++ b/srcs/webserv/cgi_script.cpp @@ -1,13 +1,17 @@ #include "Webserv.hpp" -// TODO HUGO : go ameliorer la recherche comme on a dit. +// 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) { std::vector v_ext; std::vector::const_iterator it; std::vector::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()) @@ -23,8 +27,10 @@ size_t Webserv::_cgi_pos(Client *client, std::string &path) 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"; - if (path.compare(pos + 1, (*it).size(), *it) == 0) - return pos; + len = (*it).size(); + if (path.compare(pos + 1, len, *it) == 0) + if ( !std::isalpha(path[pos + 1 + len], loc) ) + return pos + 1 + len; } pos++; } @@ -111,6 +117,7 @@ std::string Webserv::_exec_script(Client *client, char **env) int fd_out[2]; int save_in = dup(STDIN_FILENO); int save_out = dup(STDOUT_FILENO); + std::string path; pipe(fd_in); pipe(fd_out); @@ -123,10 +130,10 @@ 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"; - execve(client->get_rq_script_path().c_str(), nll, env); + dup2(FD_WR_TO_PRNT, STDOUT_FILENO); /*DEBUG*/std::cerr << "execve:\n"; + + path = "." + client->get_rq_script_path(); + execve(path.c_str(), nll, env); // for tests execve crash : //execve("wrong", nll, env); std::cerr << "execve crashed.\n"; diff --git a/srcs/webserv/response.cpp b/srcs/webserv/response.cpp index 398ed76..98e9c11 100644 --- a/srcs/webserv/response.cpp +++ b/srcs/webserv/response.cpp @@ -69,19 +69,20 @@ void Webserv::_append_base_headers(Client *client) void Webserv::_construct_response(Client *client) { - std::string path = _replace_url_root(client, client->get_rq_abs_path()); + std::string path; + std::string script_output; - size_t pos = _cgi_pos(client, path); /*DEBUG*/ std::cout << "pos:" << pos << B_YELLOW << "\nfin debug _cgi_pos()\n\n" << RESET; - (void)pos; + path = _replace_url_root(client, client->get_rq_abs_path()); + size_t pos = _cgi_pos(client, path); /*DEBUG*/ std::cout << "pos:" << pos << "\n&path[pos]:" << &path[pos] << "\n" << B_YELLOW << "fin debug _cgi_pos()\n\n" << RESET; -// if (pos != NPOS) -// { -// client->fill_script_path(path, pos); -// std::string script_output = _exec_cgi(client); -// _check_script_output(client, script_output); -// client->response += script_output; -// return; -// } + if (pos != NPOS) + { + client->fill_script_path(path, pos); + script_output = _exec_cgi(client); + _check_script_output(client, script_output); + client->response += script_output; + return; + } _process_method(client, path); }