litle fix in utils trim
+ added 2 functions to check script output + created a html page with 4 forms for tests + created 2 cpp forms, with and without creating header content-length
This commit is contained in:
@@ -140,8 +140,6 @@ std::string Webserv::_exec_script(Client *client, char **env)
|
||||
std::string body = client->get_rq_body();
|
||||
int fd_in[2];
|
||||
int fd_out[2];
|
||||
int save_in = dup(STDIN_FILENO);
|
||||
int save_out = dup(STDOUT_FILENO);
|
||||
std::string path;
|
||||
|
||||
pipe(fd_in);
|
||||
@@ -150,7 +148,7 @@ std::string Webserv::_exec_script(Client *client, char **env)
|
||||
pid = fork();
|
||||
if (pid == -1)
|
||||
std::cerr << "fork crashed" << std::endl;
|
||||
else if (pid == 0)
|
||||
else if (pid == 0) // child
|
||||
{
|
||||
close(FD_WR_TO_CHLD);
|
||||
close(FD_RD_FR_CHLD);
|
||||
@@ -162,9 +160,10 @@ std::string Webserv::_exec_script(Client *client, char **env)
|
||||
// for tests execve crash :
|
||||
//execve("wrong", nll, env);
|
||||
std::cerr << "execve crashed.\n";
|
||||
|
||||
// TODO HUGO : check errno
|
||||
}
|
||||
else
|
||||
else //parent
|
||||
{
|
||||
close(FD_RD_FR_PRNT);
|
||||
close(FD_WR_TO_PRNT);
|
||||
@@ -182,9 +181,7 @@ std::string Webserv::_exec_script(Client *client, char **env)
|
||||
}
|
||||
if (script_output.empty())
|
||||
script_output = "Status: 500\r\n\r\n";
|
||||
|
||||
dup2(save_in, STDIN_FILENO);
|
||||
dup2(save_out, STDOUT_FILENO);
|
||||
|
||||
return script_output;
|
||||
}
|
||||
|
||||
@@ -192,6 +189,8 @@ void Webserv::_check_script_output(Client *client, std::string & output)
|
||||
{
|
||||
_check_script_status(client, output);
|
||||
_check_script_fields(client, output);
|
||||
_add_script_body_length_header(output);
|
||||
_remove_body_leading_empty_lines(output);
|
||||
// _check_script_empty_lines(client, output);
|
||||
// _check_script_space_colons(client, output);
|
||||
// _check_script_new_lines(client, output);
|
||||
@@ -248,3 +247,58 @@ void Webserv::_check_script_fields(Client *client, std::string & output)
|
||||
}
|
||||
}
|
||||
|
||||
void Webserv::_remove_body_leading_empty_lines(std::string & output)
|
||||
{
|
||||
size_t pos;
|
||||
size_t pos_empty;
|
||||
|
||||
pos = output.find(CRLF CRLF);
|
||||
if (pos == NPOS)
|
||||
return;
|
||||
pos += CRLF_SIZE * 2;
|
||||
pos_empty = pos;
|
||||
while (pos_empty == pos)
|
||||
{
|
||||
pos = output.find(CRLF, pos);
|
||||
if (pos == pos_empty)
|
||||
extract_line(output, pos, CRLF);
|
||||
}
|
||||
}
|
||||
|
||||
void Webserv::_add_script_body_length_header(std::string & output)
|
||||
{
|
||||
std::map<std::string, std::string> field;
|
||||
std::map<std::string, std::string>::iterator it;
|
||||
std::stringstream str_len;
|
||||
std::string tmp;
|
||||
size_t pos;
|
||||
size_t len;
|
||||
|
||||
pos = output.find(CRLF CRLF);
|
||||
if (pos != NPOS)
|
||||
tmp = output.substr(pos + CRLF_SIZE);
|
||||
len = tmp.size();
|
||||
str_len << len;
|
||||
|
||||
// put script headers in map
|
||||
tmp = output;
|
||||
pos = tmp.find(CRLF CRLF);
|
||||
if (pos != NPOS)
|
||||
tmp.erase(pos);
|
||||
::parse_http_headers(tmp, field);
|
||||
// case insensitive search in map for "Content-Length"
|
||||
tmp = "Content-Length";
|
||||
for (it = field.begin(); it != field.end(); ++it)
|
||||
{
|
||||
if (str_tolower(it->first) == str_tolower(tmp))
|
||||
{
|
||||
pos = output.find(it->first);
|
||||
::extract_line(output, pos, CRLF);
|
||||
}
|
||||
}
|
||||
tmp += ": ";
|
||||
tmp += str_len.str();
|
||||
tmp += CRLF;
|
||||
output.insert(0, tmp);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user