changes var in client

This commit is contained in:
hugogogo
2022-08-08 18:06:41 +02:00
parent f10931042f
commit e1a68bc3e4
6 changed files with 44 additions and 39 deletions

View File

@@ -11,16 +11,19 @@ bool Webserv::_is_cgi(Client *client)
return false;
}
void Webserv::_exec_cgi(Client *client)
std::string Webserv::_exec_cgi(Client *client)
{
char** env;
char** env;
std::string script_output;
env = _set_env(client);
_exec_script(client, env);
script_output = _exec_script(client, env);
for (int i = 0; env[i]; i++)
delete[] env[i];
delete[] env;
return script_output;
}
char* Webserv::_dup_env(std::string var, std::string val = "")
@@ -49,7 +52,7 @@ char** Webserv::_set_env(Client *client)
env[0] = _dup_env("AUTH_TYPE"); // authentification not supported
env[1] = _dup_env("CONTENT_LENGTH" , client->get_rq_body().size());
env[2] = _dup_env("CONTENT_TYPE" , client->get_rq_headers("Content-Type"));
env[3] = _dup_env("GATEWAY_INTERFACE" , "CGI/1.0");
env[3] = _dup_env("GATEWAY_INTERFACE" , "CGI/1.1"); // https://www.rfc-editor.org/rfc/rfc3875
env[4] = _dup_env("PATH_INFO" , client->get_rq_script_info());
env[5] = _dup_env("PATH_TRANSLATED"); // not supported
env[6] = _dup_env("QUERY_STRING" , client->get_rq_query());
@@ -69,7 +72,7 @@ char** Webserv::_set_env(Client *client)
return env;
}
void Webserv::_exec_script(Client *client, char **env)
std::string Webserv::_exec_script(Client *client, char **env)
{
#define RD 0
#define WR 1
@@ -82,7 +85,7 @@ void Webserv::_exec_script(Client *client, char **env)
pid_t pid;
char buf[CGI_BUF_SIZE]; // WIP define buffer
char * const * nll = NULL;
std::string response;
std::string script_output;
std::string body = client->get_rq_body();
int fd_in[2];
int fd_out[2];
@@ -113,19 +116,16 @@ void Webserv::_exec_script(Client *client, char **env)
memset(buf, '\0', CGI_BUF_SIZE);
while (read(FD_RD_FR_CHLD, buf, CGI_BUF_SIZE - 1) > 0)
{
response += buf;
script_output += buf;
memset(buf, '\0', CGI_BUF_SIZE);
}
}
if (response.empty())
response = "Status: 500\r\n\r\n";
if (script_output.empty())
script_output = "Status: 500\r\n\r\n";
// DEBUG
std::cout << "\n_______response_______\n"
<< response
<< "\n_____end response_____\n";
std::cout << "\n______response______\n" << script_output << "\n____end response____\n";
// TODO: see how this must be handled
client->response += response;
return script_output;
}