+ 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
This commit is contained in:
hugogogo
2022-08-13 14:38:50 +02:00
parent defb2ada61
commit 285f2d049f
8 changed files with 60 additions and 43 deletions

View File

@@ -2,17 +2,21 @@
## work together ## work together
#### next commit #### next commit
+ fixed pbm in extract_line : delete the line and the newline sequence character + in fill_script_path() : scipt_path and script_info are filled, removing leadin dot
+ fixed pbm n _cgi_pos : last return is npos, not pos + in _cgi_pos() : check validity of extension if it's only alpha characters
+ severall pbm appears
#### TODO hugo #### TODO hugo
- `_is_cgi()` and `_fill_cgi_path()` - pbm : `http://localhost:4040/cgi-bin/cgi.sh`
- 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
#### questions #### 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 ? - the response page is received long after the cgi-script is done, why ?
--- ---

View File

@@ -21,7 +21,7 @@ server {
location /cgi-bin { location /cgi-bin {
root ./srcs/cgi-bin/; root ./srcs/cgi-bin/;
cgi_ext cgi php txt; cgi_ext cpp php sh;
} }
location /redirect { location /redirect {

View File

@@ -202,27 +202,26 @@ void Client::parse_request_body()
status = 413; // HTTP Client Errors status = 413; // HTTP Client Errors
} }
// TODO HUGO : faire la fonction, mdr. // TODO HUGO : what if the root doesn't start with leading "." ?
void Client::fill_script_path(const std::string &path, size_t pos) 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; std::string tmp;
pos = path.find(script); // should start with "/"
if (pos == 0) // 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); path.erase(0, 1);
_request.script.path = "./srcs" + tmp; // TODO: root path ? pos--;
} /*DEBUG*/ std::cout << "path:[" << path << "]\n" << "&path[pos]:[" << &path[pos] << "]\n";
_request.script.path = "./srcs" + tmp; // TODO: root path ? _request.script.path = path.substr(0, pos); /*DEBUG*/ std::cout << "script_path:[" << _request.script.path << "]\n";
_request.script.info = path.substr(pos + len); _request.script.info = path.substr(pos); /*DEBUG*/ std::cout << "script_info:[" << _request.script.info << "]\n" << B_PURPLE << "end debug path dot" << RESET << "\n";
return true;
}
return false; */
} }
void Client::clear() void Client::clear()

View File

@@ -11,6 +11,7 @@
# include <arpa/inet.h> // htonl, htons, ntohl, ntohs, inet_addr, inet_ntoa # include <arpa/inet.h> // htonl, htons, ntohl, ntohs, inet_addr, inet_ntoa
# include "utils.hpp" # include "utils.hpp"
# include "ServerConfig.hpp" # include "ServerConfig.hpp"
# include "colors.h"
struct Script struct Script
{ {
@@ -76,7 +77,7 @@ class Client
void clear(); void clear();
void clear_request(); void clear_request();
void clear_script(); 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 // DEBUG
void print_client(std::string message = ""); void print_client(std::string message = "");

4
srcs/cgi-bin/cgi.sh Normal file
View File

@@ -0,0 +1,4 @@
#! /usr/bash
echo "status: 100\r\n"
echo "\r\n\r\n"
echo "hiii"

View File

@@ -25,6 +25,7 @@
# include <cstdio> // perror, remove # include <cstdio> // perror, remove
# include <cstdlib> // strtol, strtoul # include <cstdlib> // strtol, strtoul
# include <dirent.h> // opendir() # include <dirent.h> // opendir()
# include <locale> // isalpha, local
# include "Client.hpp" # include "Client.hpp"
# include "ServerConfig.hpp" # include "ServerConfig.hpp"

View File

@@ -1,13 +1,17 @@
#include "Webserv.hpp" #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) size_t Webserv::_cgi_pos(Client *client, std::string &path)
{ {
std::vector<std::string> v_ext; std::vector<std::string> v_ext;
std::vector<std::string>::const_iterator it; std::vector<std::string>::const_iterator it;
std::vector<std::string>::const_iterator it_end; std::vector<std::string>::const_iterator it_end;
size_t pos = 0; 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"; /*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; v_ext = client->assigned_location->cgi_ext;
if (v_ext.empty()) 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(); it = client->assigned_location->cgi_ext.begin();
for ( ; it != it_end; ++it) 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) len = (*it).size();
return pos; if (path.compare(pos + 1, len, *it) == 0)
if ( !std::isalpha(path[pos + 1 + len], loc) )
return pos + 1 + len;
} }
pos++; pos++;
} }
@@ -111,6 +117,7 @@ std::string Webserv::_exec_script(Client *client, char **env)
int fd_out[2]; int fd_out[2];
int save_in = dup(STDIN_FILENO); int save_in = dup(STDIN_FILENO);
int save_out = dup(STDOUT_FILENO); int save_out = dup(STDOUT_FILENO);
std::string path;
pipe(fd_in); pipe(fd_in);
pipe(fd_out); pipe(fd_out);
@@ -123,10 +130,10 @@ std::string Webserv::_exec_script(Client *client, char **env)
close(FD_WR_TO_CHLD); close(FD_WR_TO_CHLD);
close(FD_RD_FR_CHLD); close(FD_RD_FR_CHLD);
dup2(FD_RD_FR_PRNT, STDIN_FILENO); dup2(FD_RD_FR_PRNT, STDIN_FILENO);
dup2(FD_WR_TO_PRNT, STDOUT_FILENO); dup2(FD_WR_TO_PRNT, STDOUT_FILENO); /*DEBUG*/std::cerr << "execve:\n";
// DEBUG
std::cerr << "execve:\n"; path = "." + client->get_rq_script_path();
execve(client->get_rq_script_path().c_str(), nll, env); execve(path.c_str(), nll, env);
// for tests execve crash : // for tests execve crash :
//execve("wrong", nll, env); //execve("wrong", nll, env);
std::cerr << "execve crashed.\n"; std::cerr << "execve crashed.\n";

View File

@@ -69,19 +69,20 @@ void Webserv::_append_base_headers(Client *client)
void Webserv::_construct_response(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; path = _replace_url_root(client, client->get_rq_abs_path());
(void)pos; 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 (pos != NPOS)
// { {
// client->fill_script_path(path, pos); client->fill_script_path(path, pos);
// std::string script_output = _exec_cgi(client); script_output = _exec_cgi(client);
// _check_script_output(client, script_output); _check_script_output(client, script_output);
// client->response += script_output; client->response += script_output;
// return; return;
// } }
_process_method(client, path); _process_method(client, path);
} }