Skip to content

Commit

Permalink
Cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
mtriston committed Jun 14, 2021
1 parent a0652f1 commit 12fddfa
Show file tree
Hide file tree
Showing 15 changed files with 201 additions and 258 deletions.
96 changes: 43 additions & 53 deletions CGI_unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@ bool CGI_unit::_read_content_len_check(void)
{
size_t cont_len_pos;
size_t _cgi_out_body_pos;

_cgi_out_body_pos = _answer.find("\r\n\r\n");
cont_len_pos = _answer.find("Content-Length:");
if (_cgi_out_body_pos == std::string::npos || \
cont_len_pos == std::string::npos)
cont_len_pos == std::string::npos)
return false;
if (cont_len_pos < _cgi_out_body_pos)
{
if (cont_len_pos < _cgi_out_body_pos) {
_cgi_out_len = atoi(&_answer[cont_len_pos + 15]);
_cgi_out_len += (_cgi_out_body_pos + 4);
if (_cgi_out_len < (int)_answer.size())
if (_cgi_out_len < (int) _answer.size())
return true;
}
return false;
Expand All @@ -25,18 +24,18 @@ bool CGI_unit::_read_content_len_check(void)
void CGI_unit::_all_headers(void)
{
std::stringstream header;
header << "HTTP/1.1 200 OK\r\nContent-Length: " <<\
_answer.size() << "\r\nServer: ft_webserv\r\n\r\n";

header << "HTTP/1.1 200 OK\r\nContent-Length: " << \
_answer.size() << "\r\nServer: ft_webserv\r\n\r\n";
_answer.insert(0, header.str());
return ;
return;
}

std::string CGI_unit::_add_cont_len_header(int headers_len)
{
std::stringstream header;
header << "Content-Length: " << _answer.size() - headers_len << "\r\n";

header << "Content-Length: " << _answer.size() - headers_len << "\r\n";
return header.str();
}

Expand All @@ -45,13 +44,13 @@ bool CGI_unit::_recheck_frst_ln(void)
int cnt;
int border;
char const *str;

cnt = -1;
str = _answer.c_str();
border = _answer.find("\r\n");
while (++cnt < border)
if (str[cnt] == ':')
return false;
return false;
return true;
}

Expand All @@ -61,7 +60,7 @@ void CGI_unit::_checkHeaders(void)
size_t intr_pos;
std::string addon;
bool frst_ln;

body_pos = _answer.find("\r\n\r\n");
if (_read_content_len_check())
_answer.resize(_cgi_out_len);
Expand All @@ -83,11 +82,11 @@ void CGI_unit::_checkHeaders(void)

int CGI_unit::_cgi_read(void)
{
char buf[8193];
int len = 8192;
int status;
int wp;
char buf[8193];
int len = 8192;
int status;
int wp;

wp = waitpid(_pid, &status, WNOHANG);
if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
return -1;
Expand Down Expand Up @@ -126,8 +125,7 @@ bool CGI_unit::_check_path(std::string const &path)
--up;
++cnt;
}
if (up < 0)
{
if (up < 0) {
_status = CGI_DONE;
return false;
}
Expand All @@ -137,15 +135,15 @@ bool CGI_unit::_check_path(std::string const &path)
int CGI_unit::check_file(std::string const &file)
{
struct stat stats;

int len;

len = file.size() - 1;
if (file[len] == 'y' && file[len - 1] == 'p' && file[len - 2] == '.' && \
_python_location == "DONT_USE")
_python_location == "DONT_USE")
return 403;
else if (file[len] == 'p' && file[len - 1] == 'h' && file[len - 2] == 'p'\
&& file[len - 3] == '.' && _php_location == "DONT_USE")
&& file[len - 3] == '.' && _php_location == "DONT_USE")
return 403;
if (!_check_path(file))
return 403;
Expand All @@ -160,8 +158,7 @@ int CGI_unit::_cgi_write(bool post)
{
int len;

if (post)
{
if (post) {
len = write(_post_pipes[1], &_body[_body_pos], _body_len);
_body_pos += len;
if (_body_pos < _body_len)
Expand All @@ -175,19 +172,18 @@ int CGI_unit::_cgi_write(bool post)
}

int CGI_unit::_fork(cgi_preform &_env, char const **out_args)
{
{
pipe(_post_pipes);
fcntl(pipes[0], F_SETFL, O_NONBLOCK);
_pid = fork();
if (_pid == 0)
{
if (_pid == 0) {
close(pipes[0]);
dup2(pipes[1], 1);

close(_post_pipes[1]);
dup2(_post_pipes[0], 0);
execve(out_args[0], const_cast<char *const *>(out_args), \
const_cast<char *const *>(_env._env));
const_cast<char *const *>(_env._env));
exit(1);
}
close(pipes[1]);
Expand All @@ -198,7 +194,7 @@ int CGI_unit::_fork(cgi_preform &_env, char const **out_args)
CGI_unit::cgi_preform::cgi_preform()
{
_auth_type = "AUTH_TYPE=";
_content_len = "CONTENT_LENGTH=";
_content_len = "CONTENT_LENGTH=";
_content_type = "CONTENT_TYPE=";
_gateway_int = "GATEWAY_INTERFACE=CGI/1.1";
_path_info = "PATH_INFO=";
Expand Down Expand Up @@ -276,46 +272,40 @@ void CGI_unit::_getEnv(cgi_preform &e, Request &r)
e.refilling();
}

CGI_unit::CGI_unit(void){
CGI_unit::CGI_unit(void)
{
_body_pos = 0;
_cgi_out_len = -1;
}
CGI_unit::~CGI_unit(void){}

CGI_unit::~CGI_unit(void) {}

int CGI_unit::work(void)
{
if (_status == POST || _status == GET)
{
if (_status == POST || _status == GET) {
if (_status == GET)
return _cgi_write(false);
else
return _cgi_write(true);
}
else if (_status == SENDED)
} else if (_status == SENDED)
return _cgi_read();
else
return 0;
}


void CGI_unit::_checkType(std::string &name, char const**out_args)
void CGI_unit::_checkType(std::string &name, char const **out_args)
{
int len;

len = name.size() - 1;
if (name[len] == 'y' && name[len - 1] == 'p' && name[len - 2] == '.')
{
if (name[len] == 'y' && name[len - 1] == 'p' && name[len - 2] == '.') {
out_args[0] = _python_location.c_str();
out_args[1] = name.c_str();
}
else if (name[len] == 'p' && name[len - 1] == 'h' && name[len - 2] == 'p'\
&& name[len - 3] == '.')
{
} else if (name[len] == 'p' && name[len - 1] == 'h' && name[len - 2] == 'p'\
&& name[len - 3] == '.') {
out_args[0] = _php_location.c_str();
out_args[1] = name.c_str();
}
else
{
} else {
out_args[0] = name.c_str();
out_args[1] = NULL;
}
Expand Down Expand Up @@ -346,7 +336,7 @@ bool CGI_unit::checkDone(void)
int CGI_unit::init(Request &res, int port, std::string name)
{
cgi_preform _env;
char const* out_args[3];
char const *out_args[3];
std::stringstream portStr;

portStr << port;
Expand All @@ -373,12 +363,12 @@ std::string const &CGI_unit::Answer(void)
return _answer;
};

void CGI_unit::setPythonLoc(std::string const & loc)
void CGI_unit::setPythonLoc(std::string const &loc)
{
_python_location = loc;
};

void CGI_unit::setPhpLoc(std::string const & loc)
void CGI_unit::setPhpLoc(std::string const &loc)
{
_php_location = loc;
};
Loading

0 comments on commit 12fddfa

Please sign in to comment.