client variables are const ref

This commit is contained in:
hugogogo
2022-08-08 15:36:16 +02:00
parent da1f4b6e37
commit 972f52ebc8
7 changed files with 80 additions and 68 deletions

View File

@@ -93,10 +93,10 @@ class Webserv
ServerConfig &_determine_process_server(Client *client);
LocationConfig &_determine_location(ServerConfig &server, std::string const &path);
void _response_correction(Client *client);
// cgi_script.cpp
bool _is_cgi(Client *client);
void _exec_cgi(Client *client);
void _construct_client(Client *client);
char** _set_env(Client *client);
char* _dup_env(std::string var, std::string val);
char* _dup_env(std::string var, int i);

View File

@@ -17,9 +17,8 @@ void Webserv::_exec_cgi(Client *client)
env = _set_env(client);
_exec_script(client, env);
// _construct_response(client);
for (size_t i = 0; env[i]; i++)
for (int i = 0; env[i]; i++)
delete[] env[i];
delete[] env;
}
@@ -39,6 +38,7 @@ char* Webserv::_dup_env(std::string var, int i)
val = ::itos(i);
str = var + "=" + val;
// TODO change strdup for something with new
return ( strdup(str.c_str()) );
}
@@ -74,46 +74,29 @@ void Webserv::_exec_script(Client *client, char **env)
#define RD 0
#define WR 1
#define CGI_BUF_SIZE 10
/*1*/ #define FD_WR_TO_CHLD fd_in[WR]
/* */ #define FD_WR_TO_PRNT fd_out[WR]
/* */ #define FD_RD_FR_CHLD fd_out[RD]
/* */ #define FD_RD_FR_PRNT fd_in[RD]
/*2*/// #define FD_WR_TO_CHLD fdIn
/* */// #define FD_WR_TO_PRNT fdOut
/* */// #define FD_RD_FR_CHLD fdOut
/* */// #define FD_RD_FR_PRNT fdIn
#define FD_WR_TO_CHLD fd_in[WR]
#define FD_WR_TO_PRNT fd_out[WR]
#define FD_RD_FR_CHLD fd_out[RD]
#define FD_RD_FR_PRNT fd_in[RD]
pid_t pid;
char buf[CGI_BUF_SIZE]; // WIP define buffer
char * const * nll = NULL;
std::string response;
std::string body = client->get_rq_body();
int fd_in[2];
int fd_out[2];
/*1*/ int fd_in[2];
/* */ int fd_out[2];
/*2*/// FILE *fIn;
/* */// FILE *fOut;
/* */// long fdIn;
/* */// long fdOut;
/*1*/ pipe(fd_in);
/* */ pipe(fd_out);
/*2*/// fIn = tmpfile();
/* */// fOut = tmpfile();
/* */// fdIn = fileno(fIn);
/* */// fdOut = fileno(fOut);
pipe(fd_in);
pipe(fd_out);
pid = fork();
if (pid == -1)
std::cerr << "fork crashed" << std::endl;
else if (pid == 0)
{
/*1*/ close(FD_WR_TO_CHLD);
/* */ close(FD_RD_FR_CHLD);
/*2*/
close(FD_WR_TO_CHLD);
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);
@@ -121,15 +104,11 @@ void Webserv::_exec_script(Client *client, char **env)
}
else
{
/*1*/ close(FD_RD_FR_PRNT);
/* */ close(FD_WR_TO_PRNT);
/*2*/
close(FD_RD_FR_PRNT);
close(FD_WR_TO_PRNT);
write(FD_WR_TO_CHLD, body.c_str(), body.size());
close(FD_WR_TO_CHLD);
waitpid(-1, NULL, 0);
/*1*/
/*2*/// lseek(fdOut, 0, SEEK_SET);
memset(buf, '\0', CGI_BUF_SIZE);
while (read(FD_RD_FR_CHLD, buf, CGI_BUF_SIZE - 1) > 0)
@@ -141,20 +120,12 @@ void Webserv::_exec_script(Client *client, char **env)
if (response.empty())
response = "Status: 500\r\n\r\n";
/*1*/
/*2*/// fclose(fIn);
/* */// fclose(fOut);
/* */// close(fdIn);
/* */// close(fdOut);
// DEBUG
std::cout << "\n______response________\n" << response << "\n________________________\n";
std::cout << "\n_______response_______\n"
<< response
<< "\n_____end response_____\n";
// TODO: see how this must be handled
client->response += response;
}
void Webserv::_construct_client(Client *client)
{
(void)client;
}

View File

@@ -147,13 +147,8 @@ void Webserv::_get(Client *client, ServerConfig &server, LocationConfig &locatio
//
if (_is_cgi(client))
{
// DEBUG
std::cout << "\nSCRIPT PATH _________________"
<< "\npath:" << client->get_rq_script_path()
<< "\npath_info:" << client->get_rq_script_info()
<< "\n\n";
_exec_cgi(client);
_response_correction(client);
return;
}
//
@@ -229,6 +224,24 @@ void Webserv::_get_file(Client *client, const std::string &path)
}
}
// WIP HUGO
void Webserv::_response_correction(Client *client)
{
// TODO : change all the '\n' by '\r\n'
// TODO : there is at least one header field followed by '\r\n\r\n' :
// - "Content-Type"
// - "Location"
// - "Status"
// TODO : there is no field duplicate (resolve conflicts)
// TODO : there is no space between filed name and ":"
// TODO : if no Location field && no Status field -> status code = 200
// TODO?: handle Location field, either :
// - local : start with '/' --> rerun the request with new uri
// - client : start with '<scheme>:' --> send back status code 302
(void)client;
}
void Webserv::_append_body(Client *client, const char *body, size_t body_size, const std::string &file_extension)
{
/*