more script tests

+ add cgi checks for error in script output
+ fix script_path relative to absolute
+ cgi makefile more efficient
This commit is contained in:
Hugo LAMY
2022-08-16 18:24:49 +02:00
parent ff443c80b1
commit 1ed4128afc
21 changed files with 485 additions and 109 deletions

View File

@@ -18,7 +18,7 @@ bool Webserv::_is_cgi(Client *client, std::string path)
if (pos == NPOS)
break;
client->fill_script_path(path, pos);
script_path = "." + 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;
@@ -128,7 +128,7 @@ void Webserv::_set_env_vector(Client *client, std::vector<std::string> &env_vect
env_vector.push_back(_dup_env("REMOTE_IDENT")); // authentification not supported
env_vector.push_back(_dup_env("REMOTE_USER")); // authentification not supported
env_vector.push_back(_dup_env("REQUEST_METHOD" , client->get_rq_method_str()));
env_vector.push_back(_dup_env("SCRIPT_NAME" , client->get_rq_script_path())); // LUKE: To Check
env_vector.push_back(_dup_env("SCRIPT_NAME" , "/" + client->get_rq_script_path())); // LUKE: To Check
env_vector.push_back(_dup_env("SERVER_NAME" , client->get_cl_lsocket()->host));
env_vector.push_back(_dup_env("SERVER_PORT" , client->get_cl_lsocket()->port));
env_vector.push_back(_dup_env("SERVER_PROTOCOL" , "HTTP/1.1"));
@@ -228,7 +228,7 @@ std::string Webserv::_exec_script(Client *client, char *env[])
::close(FD_RD_FR_PRNT);
::close(FD_WR_TO_PRNT);
path = "." + client->get_rq_script_path(); // Wut ? Only relative path ?
path = client->get_rq_script_path(); // Wut ? Only relative path ?
/*DEBUG*/std::cerr << "execve:[" << path << "]\n";
if (execve(path.c_str(), nll, env) == -1) // replace path for debug error forcing
{
@@ -278,16 +278,14 @@ void Webserv::_check_script_output(Client *client, std::string & output)
_check_script_status(client, output);
if (client->status >= 400 && client->status < 600)
return;
///*DEBUG*/ std::cout << "\n" B_PURPLE "[script status]:" RESET "\n"; ::print_special(output); std::cout << B_PURPLE "-----------" RESET "\n\n";
_check_script_fields(client, output);
///*DEBUG*/ std::cout << "\n" B_PURPLE "[script fields]:" RESET "\n"; ::print_special(output); std::cout << B_PURPLE "-----------" RESET "\n\n";
//*DEBUG*/ std::cout << "\n" B_PURPLE "[script status]:" RESET "\n"; ::print_special(output); std::cout << B_PURPLE "-----------" RESET "\n\n";
client->status = _check_script_fields(output, client->status);
_check_fields_duplicates(client, output);
//*DEBUG*/ std::cout << "\n" B_PURPLE "[script fields]:" RESET "\n"; ::print_special(output); std::cout << B_PURPLE "-----------" RESET "\n\n";
_remove_body_leading_empty_lines(output);
///*DEBUG*/ std::cout << "\n" B_PURPLE "[script empty lines]:" RESET "\n"; ::print_special(output); std::cout << B_PURPLE "-----------" RESET "\n\n";
//*DEBUG*/ std::cout << "\n" B_PURPLE "[script empty lines]:" RESET "\n"; ::print_special(output); std::cout << B_PURPLE "-----------" RESET "\n\n";
_add_script_body_length_header(output);
///*DEBUG*/ std::cout << "\n" B_PURPLE "[script content length]:" RESET "\n"; ::print_special(output); std::cout << B_PURPLE "-----------" RESET "\n\n";
// _check_script_empty_lines(client, output);
// _check_script_space_colons(client, output);
// _check_script_new_lines(client, output);
//*DEBUG*/ std::cout << "\n" B_PURPLE "[script content length]:" RESET "\n"; ::print_special(output); std::cout << B_PURPLE "-----------" RESET "\n\n";
}
void Webserv::_check_script_status(Client *client, std::string & output)
@@ -306,7 +304,35 @@ void Webserv::_check_script_status(Client *client, std::string & output)
client->status = 200;
}
void Webserv::_check_script_fields(Client *client, std::string & output)
size_t Webserv::_check_script_fields(const std::string & output, size_t status)
{
std::string headers;
std::string body;
size_t pos;
pos = output.find(CRLF CRLF);
if (pos == NPOS) // there is not empty line
return 500;
headers = output.substr(0, pos);
body = output.substr(pos + CRLF_SIZE * 2);
headers = str_tolower(headers);
pos = headers.find("content-type");
if (pos == NPOS) // there is no content-type field
{
if (!body.empty()) // there is body
return 500;
if (headers.find("location") == NPOS) // there is no location field
return 500;
}
else if (headers.find("location") != NPOS) // there is a location field
{
if (body.empty()) // there is no body
return 500;
}
return status;
}
void Webserv::_check_fields_duplicates(Client *client, std::string & output)
{
std::map<std::string, std::string> srv_fld; // server_field
std::map<std::string, std::string> scr_fld; // script_field