wip script output verification working

+ trim secure pos segfault
+ concat script message with server message
This commit is contained in:
hugogogo
2022-08-09 14:57:05 +02:00
parent 3dad938e3c
commit 53a548e314
7 changed files with 65 additions and 17 deletions

View File

@@ -5,7 +5,7 @@ CXX = c++
CXXFLAGS = -Wall -Wextra #-Werror
CXXFLAGS += $(HEADERS_D:%=-I%)
CXXFLAGS += -std=c++98
CXXFLAGS += -g
CXXFLAGS += -g3
CXXFLAGS += -MMD -MP #header dependencie
#CXXFLAGS += -O3

View File

@@ -15,9 +15,19 @@ std::vector<std::string> split(std::string input, char delimiter)
std::string trim(std::string str, char c)
{
// TODO: protect substr
str = str.substr(str.find_first_not_of(c));
str = str.substr(0, str.find_last_not_of(c) + 1);
size_t pos;
// delete leadings c
pos = str.find_first_not_of(c);
if (pos == std::string::npos)
return str;
str = str.substr(pos);
// delete endings c
pos = str.find_last_not_of(c);
if (pos == std::string::npos)
return str;
str = str.substr(0, pos + 1);
return str;
}
@@ -107,7 +117,7 @@ std::string str_tolower(std::string str)
return str;
}
void delete_line_in_string(std::string * str, size_t pos, std::string delim)
void del_line_in_str(std::string * str, size_t pos, std::string delim)
{
size_t begin;
size_t end;

View File

@@ -49,6 +49,6 @@ http_method str_to_http_method(std::string &str);
std::string http_methods_to_str(unsigned int methods);
void replace_all_substr(std::string &str, const std::string &ori_substr, const std::string &new_substr);
std::string str_tolower(std::string str);
void delete_line_in_string(std::string * str, size_t pos, std::string delim);
void del_line_in_str(std::string * str, size_t pos, std::string delim);
#endif

View File

@@ -102,8 +102,11 @@ std::string Webserv::_exec_script(Client *client, char **env)
close(FD_RD_FR_CHLD);
dup2(FD_RD_FR_PRNT, STDIN_FILENO);
dup2(FD_WR_TO_PRNT, STDOUT_FILENO);
//execve(client->get_rq_script_path().c_str(), nll, env);
execve("truc", nll, env);
// DEBUG
std::cerr << "execve:\n";
execve(client->get_rq_script_path().c_str(), nll, env);
// for tests execve crash :
//execve("wrong", nll, env);
std::cerr << "execve crashed.\n";
}
else
@@ -144,18 +147,32 @@ void Webserv::_check_script_status(Client *client, std::string output)
{
status_pos = pos + std::string("Status:").size();
client->status = atoi(output.c_str() + status_pos);
::delete_line_in_string(&output, pos, CRLF);
::del_line_in_str(&output, pos, CRLF);
}
client->status = 200;
}
void Webserv::_check_script_fields(Client *client, std::string output)
{
std::map<std::string, std::string> server_fields;
std::map<std::string, std::string> script_fields;
std::map<std::string, std::string> srv_fld; // server_field
std::map<std::string, std::string> scr_fld; // script_field
std::map<std::string, std::string>::iterator it_srv;
std::map<std::string, std::string>::iterator it_scr;
size_t pos;
server_fields = parse_http_headers(client->response);
script_fields = parse_http_headers(output);
// TODO: compare both map to supress duplicates
srv_fld = parse_http_headers(client->response);
scr_fld = parse_http_headers(output);
// wip: compare both map to supress duplicates
for (it_srv = srv_fld.begin(); it_srv != srv_fld.end(); it_srv++)
{
for (it_scr = scr_fld.begin(); it_scr != scr_fld.end(); it_scr++)
{
if (it_srv->first == it_scr->first)
{
pos = client->response.find(it_srv->first);
::del_line_in_str(&client->response, pos, CRLF);
}
}
}
}

View File

@@ -19,7 +19,7 @@ size_t
return ret;
for (int i = 0; i < 3; i++)
{
tmp = ::trim(sline[0], ' ');
tmp = ::trim(sline[i], ' ');
tmp = ::trim(tmp, '\r');
line.push_back(tmp);
}
@@ -40,7 +40,7 @@ std::map<std::string, std::string>
pos = (message).find(CRLF CRLF);
sub = (message).substr(0, pos);
list = ::split(sub, '\n');
// TODO: if (list.begin() is "first line")
if ( maybe_http_first_line( *list.begin() ) )
list.erase(list.begin());
for (it = list.begin(); it != list.end(); it++)
@@ -73,3 +73,18 @@ std::string
return body;
}
bool maybe_http_first_line(std::string str)
{
// method SP target SP version https://www.rfc-editor.org/rfc/rfc7230.html#section-3.1.1
// version SP status SP reason https://www.rfc-editor.org/rfc/rfc7230.html#section-3.1.2
std::vector<std::string> sline;
sline = ::split(str, ' ');
if (sline.size() != 3)
return false;
if (sline[0].find(':') != std::string::npos)
return false;
return true;
}

View File

@@ -17,6 +17,9 @@ std::map<std::string, std::string>
std::string
parse_http_body(std::string message);
bool
maybe_http_first_line(std::string);
// http message structure :
//
// start-line

View File

@@ -149,8 +149,11 @@ void Webserv::_get(Client *client, ServerConfig &server, LocationConfig &locatio
if (_is_cgi(client))
{
script_output = _exec_cgi(client);
// DEBUG
std::cout << "\n____script_output____\n" << script_output << "\n_______________\n";
// wip check output of script
_check_script_output(client, script_output);
std::cout << "_____________status:" << client->status << "\n";
client->response += script_output;
return;
}
//