From f17bc9fa58a84ce39bbcf17db4fd10f74a080cd0 Mon Sep 17 00:00:00 2001 From: hugogogo Date: Fri, 12 Aug 2022 23:31:59 +0200 Subject: [PATCH 1/5] wip test path cgi : - _cgi_pos seems to be ok - construct url from return is not implemented yet + renamed script with extension .php and .cpp --- default.config | 5 +++++ srcs/cgi-bin/cgi.cpp | Bin 873 -> 14576 bytes srcs/cgi-bin/{php-cgi => cgi.php} | 0 srcs/cgi-bin/cgi_cpp.cgi | Bin 14576 -> 0 bytes srcs/webserv/Webserv.hpp | 1 + srcs/webserv/cgi_script.cpp | 36 ++++++++++++++++++++++-------- srcs/webserv/response.cpp | 21 +++++++++-------- 7 files changed, 45 insertions(+), 18 deletions(-) mode change 100644 => 100755 srcs/cgi-bin/cgi.cpp rename srcs/cgi-bin/{php-cgi => cgi.php} (100%) delete mode 100755 srcs/cgi-bin/cgi_cpp.cgi diff --git a/default.config b/default.config index 088b63d..a5dc5f7 100644 --- a/default.config +++ b/default.config @@ -19,6 +19,11 @@ server { autoindex on; } + location /cgi-bin { + root ./srcs/cgi-bin/; + cgi_ext cgi php txt; + } + location /redirect { redirect 307 https://fr.wikipedia.org/wiki/Ketchup; # redirect 307 https://www.youtube.com/watch?v=rG6b8gjMEkw; diff --git a/srcs/cgi-bin/cgi.cpp b/srcs/cgi-bin/cgi.cpp old mode 100644 new mode 100755 index 3882239dbd5a205c4338311a4717a06be24ccd32..95013cbab183e1dadb657b89fd40e3c70bfc5ed6 GIT binary patch literal 14576 zcmeHOeQ;FQb-$}cNPI~PH3Tq*2Qd!VVbKZ%0*s@tg(o9Gm4t&G!(+9(lD0@%dH1a? zF(HK!VzMQ2700AZ+D=I8anq?gPAC1K;}*-sHMXZS#YyWhj?+?)O|||QhMTq&`Fw(tU)&>z3zS<(jVkC1sSHSs zs20=ky;$5Prh=a$F{`}BA}F=exH8SORN;P5(yK1A05@HvvN?#}`>RQ;i%#sv)Oj(&RU}M|y$VBnzs^Or9$L1B!C{$P9hj6z--C(ux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L26 literal 873 zcmZ`%$!@|h5Ix&pF%6eO3IR@4Vz|&%tptbkP?ZX$i3^s%p|(M#h=1=G6Lt<+^ybZ* zH%keGOvkf~0~5J6iYJpcE$u*rgd8411&s=6V3G(4*Y+<-Zy+5d3M!RkUWO$l+GJtq z6bK(K-koMO&yYWB302FB2~yNa3?fBd?Pm5LKUN0wB>*D=03IplB76;}r? fbq_<0qJUlV`p(mQW^Co{ncEJ`Oi8FmbVdIGW1R>M diff --git a/srcs/cgi-bin/php-cgi b/srcs/cgi-bin/cgi.php similarity index 100% rename from srcs/cgi-bin/php-cgi rename to srcs/cgi-bin/cgi.php diff --git a/srcs/cgi-bin/cgi_cpp.cgi b/srcs/cgi-bin/cgi_cpp.cgi deleted file mode 100755 index 95013cbab183e1dadb657b89fd40e3c70bfc5ed6..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 14576 zcmeHOeQ;FQb-$}cNPI~PH3Tq*2Qd!VVbKZ%0*s@tg(o9Gm4t&G!(+9(lD0@%dH1a? zF(HK!VzMQ2700AZ+D=I8anq?gPAC1K;}*-sHMXZS#YyWhj?+?)O|||QhMTq&`Fw(tU)&>z3zS<(jVkC1sSHSs zs20=ky;$5Prh=a$F{`}BA}F=exH8SORN;P5(yK1A05@HvvN?#}`>RQ;i%#sv)Oj(&RU}M|y$VBnzs^Or9$L1B!C{$P9hj6z--C(uxpF zNtROcTV{;%Z_btMB-&=df+_3$HuT8P>rDYA*rMw5>X%a0FHCtHR(B`b*R5UM9b46% z=*{%6>aSk6YTepkIvK2#?I!yqyLHDdSreI2T_MYF0Z$l}|Khug|NaY&Pag@dZ|@5( z-|)=iKU(k|lD!ol9yg4D%o9dw?unW8R<7`O7>aWW6#WS*Erj2Nj$Bqi{u~@BB>%35 z{rw*DEgtnQM!kjXe-pkG!UsL-{keyHwTJvHk9wzj_(`K!$e%ks+WVA;ol_q2Kk@K$ z3F_7Gai@&rFp8kjf3Chfj{fvrw)H+iv>c{ENcPC$%N-5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GvZdzS0P)!W;k0RllkyAMLkc2F~A(Jsy<@EcmH zPXv3DW;}?XwN+-MQ*oWWnP7V+fw=NSOazJRiln{K%zr^n6U<{O{ z;@uHaP`SQtQv~If8bmhOnFLEe+=-yj*@t{vl8E{Fy;G0RV-yW zPxSNn;OLasZ&QYtYACZO9G4v_1kSE+Dt)H6JEZBI(wQ2-UdEjr`_K29Oj#etVww1T zN3miDGIaIG`lBkt^rYf(ER}6%eVoW;x)0xYg~{vl{SZ^WH?^d%y-zd07a6*f3)>c9VhdA-^6r8>68EDC;P8paR3?WQ`YBuDW)1t4kWIh^|}2=P)7Q7 zB!tBGS4{cdm-OkC->v^BFsg$5qr~^#Ox@Q6RF^$v!PxcrK8$IN@}G71o}B69E`7Y} zT1ip%m-U%v`o}JPelKPEA$OcI!1c1dKXvKzJseZM?`C;-`~O_&^Z4<70@EL}qN40K z!++({=X(~WZ7is$yZ&cg`h5S#bi~#F?)qPH>GM4y(=WK{cbOAEMLFHM(cGrQ_dERm zNACXS`kDSWbZDqqpYM%I)&FO)9NDJCJkyVW@v}wce7~Rn|1LE!Jil3w{W<|1Y7^Jb zznf@F$eIur!*SdaN4C=-^iU(;mKe2a+rWZafCTb{Bd zC?}B$7tZ=CT{xSwQ=YqaQC+jRuPcXXAEG+ge{R<_VC=i4VSS%lVbLsYF1Ez=GBg{R RLi+cs2IcQ_l!9CTe*xzN5cL26 diff --git a/srcs/webserv/Webserv.hpp b/srcs/webserv/Webserv.hpp index 12964a3..ee058d3 100644 --- a/srcs/webserv/Webserv.hpp +++ b/srcs/webserv/Webserv.hpp @@ -31,6 +31,7 @@ # include "utils.hpp" # include "http_status.hpp" # include "autoindex.hpp" +# include "colors.h" extern bool g_run; extern int g_last_signal; diff --git a/srcs/webserv/cgi_script.cpp b/srcs/webserv/cgi_script.cpp index fe81ff4..e928180 100644 --- a/srcs/webserv/cgi_script.cpp +++ b/srcs/webserv/cgi_script.cpp @@ -4,18 +4,36 @@ // TODO HUGO : go ameliorer la recherche comme on a dit. size_t Webserv::_cgi_pos(Client *client, std::string &path) { - size_t pos = NPOS; + std::vector v_ext; std::vector::const_iterator it; - it = client->assigned_location->cgi_ext.begin(); - while (it != client->assigned_location->cgi_ext.end()) + std::vector::const_iterator it_end; + size_t pos = 0; + + /*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"; + it_end = client->assigned_location->cgi_ext.end(); + while (pos < path.size()) { - pos = std::min(path.find(*it) + it->size(), pos); - ++it; + /*DEBUG*/ std::cout << "\nwhile\n"; + if (path.compare(pos, 2, "./") == 0) + 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"; + if (path.compare(pos + 1, (*it).size(), *it) == 0) + return pos; + } + pos++; } - if (pos == NPOS) - return false; - else - return true; + return pos; } std::string Webserv::_exec_cgi(Client *client) diff --git a/srcs/webserv/response.cpp b/srcs/webserv/response.cpp index 436e478..302e20b 100644 --- a/srcs/webserv/response.cpp +++ b/srcs/webserv/response.cpp @@ -71,15 +71,18 @@ void Webserv::_construct_response(Client *client) { std::string path = _replace_url_root(client, client->get_rq_abs_path()); -/* size_t pos = _cgi_pos(client, path); - 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; - } */ + size_t pos = _cgi_pos(client, path); +//debug +std::cout << "pos:" << pos << B_YELLOW << "\nfin debug _cgi_pos()\n\n" << RESET; + (void)pos; +// 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; +// } _process_method(client, path); } From defb2ada613266f0b80dad921d35161b48fe1482 Mon Sep 17 00:00:00 2001 From: hugogogo Date: Sat, 13 Aug 2022 12:39:05 +0200 Subject: [PATCH 2/5] fixed pbm in extract_line : delete the line and the newline sequence character + fixed pbm n _cgi_pos : last return is npos, not pos --- README.md | 4 ++++ srcs/utils.cpp | 6 +++--- srcs/webserv/cgi_script.cpp | 15 +++++---------- srcs/webserv/response.cpp | 5 ++--- 4 files changed, 14 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 831a28f..ebc9a05 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,10 @@ ## 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 + #### TODO hugo - `_is_cgi()` and `_fill_cgi_path()` - two cgi tests : diff --git a/srcs/utils.cpp b/srcs/utils.cpp index c4536e0..d531b1b 100644 --- a/srcs/utils.cpp +++ b/srcs/utils.cpp @@ -198,8 +198,8 @@ std::string str_tolower(std::string str) } // identify a line in a string, by delim (ex. '\n') -// delete this line from the string -// and return the deleted line +// delete this line from the string (and the following nl sequence characters) +// and return the deleted line (without the followinf nl sequence characters) std::string extract_line(std::string & str, size_t pos, std::string delim) { @@ -220,7 +220,7 @@ std::string len = end - begin; del_str = str.substr(begin, len); - str.erase(begin, len); + str.erase(begin, len + delim.size()); return del_str; } diff --git a/srcs/webserv/cgi_script.cpp b/srcs/webserv/cgi_script.cpp index e928180..daf7ddb 100644 --- a/srcs/webserv/cgi_script.cpp +++ b/srcs/webserv/cgi_script.cpp @@ -8,32 +8,27 @@ size_t Webserv::_cgi_pos(Client *client, std::string &path) std::vector::const_iterator it; std::vector::const_iterator it_end; size_t pos = 0; - /*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"; if (path.compare(pos + 1, (*it).size(), *it) == 0) return pos; } pos++; } - return pos; + return NPOS; } std::string Webserv::_exec_cgi(Client *client) diff --git a/srcs/webserv/response.cpp b/srcs/webserv/response.cpp index 302e20b..398ed76 100644 --- a/srcs/webserv/response.cpp +++ b/srcs/webserv/response.cpp @@ -71,10 +71,9 @@ void Webserv::_construct_response(Client *client) { std::string path = _replace_url_root(client, client->get_rq_abs_path()); - size_t pos = _cgi_pos(client, path); -//debug -std::cout << "pos:" << pos << B_YELLOW << "\nfin debug _cgi_pos()\n\n" << RESET; + size_t pos = _cgi_pos(client, path); /*DEBUG*/ std::cout << "pos:" << pos << B_YELLOW << "\nfin debug _cgi_pos()\n\n" << RESET; (void)pos; + // if (pos != NPOS) // { // client->fill_script_path(path, pos); From 285f2d049f12f0e8d70718f71561ea432e3209f4 Mon Sep 17 00:00:00 2001 From: hugogogo Date: Sat, 13 Aug 2022 14:38:50 +0200 Subject: [PATCH 3/5] + 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 --- README.md | 18 +++++++++++------- default.config | 2 +- srcs/Client.cpp | 31 +++++++++++++++---------------- srcs/Client.hpp | 3 ++- srcs/cgi-bin/cgi.sh | 4 ++++ srcs/webserv/Webserv.hpp | 1 + srcs/webserv/cgi_script.cpp | 21 ++++++++++++++------- srcs/webserv/response.cpp | 23 ++++++++++++----------- 8 files changed, 60 insertions(+), 43 deletions(-) create mode 100644 srcs/cgi-bin/cgi.sh 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); } From db4c7468cc32a6f5f5d887185ed30f4ed4a7bb9d Mon Sep 17 00:00:00 2001 From: Hugo LAMY Date: Sun, 14 Aug 2022 01:05:20 +0200 Subject: [PATCH 4/5] is_cgi function is back and is working + in utils added function eval_file_mode(), it returns http errors codes about file --- README.md | 10 ++----- srcs/Client.cpp | 16 ++++------ srcs/cgi-bin/cgi.sh | 2 +- srcs/cgi-bin/cgi_second/cgi.php | 29 ++++++++++++++++++ srcs/utils.cpp | 15 ++++++++++ srcs/utils.hpp | 2 ++ srcs/webserv/Webserv.hpp | 3 +- srcs/webserv/cgi_script.cpp | 52 ++++++++++++++++++++++++++------- srcs/webserv/response.cpp | 6 ++-- 9 files changed, 100 insertions(+), 35 deletions(-) mode change 100644 => 100755 srcs/cgi-bin/cgi.sh create mode 100755 srcs/cgi-bin/cgi_second/cgi.php diff --git a/README.md b/README.md index 8a8e866..3f0b40e 100644 --- a/README.md +++ b/README.md @@ -2,19 +2,13 @@ ## work together #### next commit -+ 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 -- 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 ? +-> - for now, execve would crash, but that doesn't produce a 404 error, rather a 500, is it bad ? + - could we use errno after execve to choose an appropriate http error ? subject says : "Checking the value of errno is strictly forbidden after a read or a write operation" - 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/srcs/Client.cpp b/srcs/Client.cpp index 70d288b..9759af2 100644 --- a/srcs/Client.cpp +++ b/srcs/Client.cpp @@ -202,26 +202,22 @@ void Client::parse_request_body() status = 413; // HTTP Client Errors } -// TODO HUGO : what if the root doesn't start with leading "." ? void Client::fill_script_path(std::string &path, size_t pos) { std::string tmp; - // 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] == '.') { path.erase(0, 1); pos--; - } /*DEBUG*/ std::cout << "path:[" << path << "]\n" << "&path[pos]:[" << &path[pos] << "]\n"; + } + /*DEBUG*/ std::cout << "path:[" << path << "]\n" << "&path[pos]:[" << &path[pos] << "]\n"; - _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"; + _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/cgi-bin/cgi.sh b/srcs/cgi-bin/cgi.sh old mode 100644 new mode 100755 index bdfc2b3..14e0d29 --- a/srcs/cgi-bin/cgi.sh +++ b/srcs/cgi-bin/cgi.sh @@ -1,4 +1,4 @@ -#! /usr/bash +#! /bin/bash echo "status: 100\r\n" echo "\r\n\r\n" echo "hiii" diff --git a/srcs/cgi-bin/cgi_second/cgi.php b/srcs/cgi-bin/cgi_second/cgi.php new file mode 100755 index 0000000..fa20b65 --- /dev/null +++ b/srcs/cgi-bin/cgi_second/cgi.php @@ -0,0 +1,29 @@ +#! /usr/bin/php + diff --git a/srcs/utils.cpp b/srcs/utils.cpp index d531b1b..c1dc152 100644 --- a/srcs/utils.cpp +++ b/srcs/utils.cpp @@ -171,6 +171,21 @@ file_type eval_file_type(const std::string &path) return (IS_OTHER); } +size_t eval_file_mode(std::string path, int mode) +{ + if (access(path.c_str(), F_OK) == -1) + { + std::perror("err access()"); + return 404; // NOT_FOUND, file doesn't exist + } + + if (access(path.c_str(), mode) == -1) + { + std::perror("err access()"); + return 403; // FORBIDDEN, file doesn't have execution permission + } + return 0; +} void replace_all_substr( diff --git a/srcs/utils.hpp b/srcs/utils.hpp index 90e734f..893b355 100644 --- a/srcs/utils.hpp +++ b/srcs/utils.hpp @@ -13,6 +13,7 @@ # include // tolower # include // transform # include // perror, fflush +# include // close, access # include "colors.h" // for debug print_special # define CR "\r" @@ -63,6 +64,7 @@ std::string trim(std::string str, char del); http_method str_to_http_method(std::string &str); std::string http_methods_to_str(unsigned int methods); file_type eval_file_type(const std::string &path); +size_t eval_file_mode(std::string path, int mode); void replace_all_substr(std::string &str, const std::string &ori_substr, const std::string &new_substr); std::string str_tolower(std::string str); std::string extract_line(std::string & str, size_t pos = 0, std::string delim = "\n"); diff --git a/srcs/webserv/Webserv.hpp b/srcs/webserv/Webserv.hpp index bb2a644..f55024c 100644 --- a/srcs/webserv/Webserv.hpp +++ b/srcs/webserv/Webserv.hpp @@ -104,7 +104,8 @@ class Webserv void _delete(Client *client, const std::string &path); void _delete_file(Client *client, const std::string &path); // cgi_script.cpp - size_t _cgi_pos(Client *client, std::string &path); + bool _is_cgi(Client *client, std::string path); + size_t _cgi_pos(Client *client, std::string &path, size_t pos); std::string _exec_cgi(Client *client); char** _set_env(Client *client); char* _dup_env(std::string var, std::string val); diff --git a/srcs/webserv/cgi_script.cpp b/srcs/webserv/cgi_script.cpp index 7c3082d..c0cc85b 100644 --- a/srcs/webserv/cgi_script.cpp +++ b/srcs/webserv/cgi_script.cpp @@ -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 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()) - 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 { diff --git a/srcs/webserv/response.cpp b/srcs/webserv/response.cpp index 98e9c11..d773bb4 100644 --- a/srcs/webserv/response.cpp +++ b/srcs/webserv/response.cpp @@ -67,17 +67,15 @@ void Webserv::_append_base_headers(Client *client) client->response.append("Connection: keep-alive" CRLF); } +// TODO HUGO : wip void Webserv::_construct_response(Client *client) { std::string path; std::string script_output; 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) + if (_is_cgi(client, path)) { - client->fill_script_path(path, pos); script_output = _exec_cgi(client); _check_script_output(client, script_output); client->response += script_output; From be499328f6834c3ba5935fdd28d17b077be9f382 Mon Sep 17 00:00:00 2001 From: Hugo LAMY Date: Sun, 14 Aug 2022 01:17:55 +0200 Subject: [PATCH 5/5] fixed error in is_cgi, path was missing dot --- srcs/webserv/cgi_script.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/srcs/webserv/cgi_script.cpp b/srcs/webserv/cgi_script.cpp index c0cc85b..e1b4d52 100644 --- a/srcs/webserv/cgi_script.cpp +++ b/srcs/webserv/cgi_script.cpp @@ -3,9 +3,10 @@ bool Webserv::_is_cgi(Client *client, std::string path) { - size_t file_type; - size_t file_mode; - size_t pos = 0; + std::string script_path; + size_t file_type; + size_t file_mode; + size_t pos = 0; while (pos != NPOS) { @@ -14,17 +15,19 @@ bool Webserv::_is_cgi(Client *client, std::string path) if (pos == NPOS) break; client->fill_script_path(path, pos); - file_type = ::eval_file_type(client->get_rq_script_path()); + script_path = "." + client->get_rq_script_path(); + file_type = ::eval_file_type(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 ); + file_mode = ::eval_file_mode( script_path, X_OK ); if (!file_mode) return true; } } client->clear_script(); + client->clear_script(); client->status = file_mode; // 404 not_found OR 403 forbidden return false; }