adding first tests with cgi
This commit is contained in:
@@ -177,6 +177,73 @@ void Webserv::_read_request(int fd)
|
||||
|
||||
void Webserv::_send_response(int fd)
|
||||
{
|
||||
|
||||
|
||||
// TMP test cgi
|
||||
// find POST in _buf
|
||||
std::string tmpstr = _buf;
|
||||
std::size_t found;
|
||||
found = tmpstr.find("POST");
|
||||
// if "POST" found, execve a cgi
|
||||
if (found != std::string::npos)
|
||||
{
|
||||
int save_stdout;
|
||||
char** env = new char*[4];
|
||||
char * const * nll = NULL;
|
||||
|
||||
// set env
|
||||
env[0] = strdup("PATH_INFO=/no");
|
||||
env[1] = strdup("REQUEST_METHOD=POST");
|
||||
env[2] = strdup("SERVER_PROTOCOL=HTTP/1.1");
|
||||
env[3] = NULL;
|
||||
// save STDOUT
|
||||
save_stdout = dup(STDOUT_FILENO);
|
||||
// inside chil process
|
||||
if (fork() == 0)
|
||||
{
|
||||
dup2(fd, STDOUT_FILENO);
|
||||
execve("./srcs/cgi-bin/cgi_cpp.cgi", nll, env);
|
||||
}
|
||||
// inside parent process
|
||||
else
|
||||
waitpid(-1, NULL, 0);
|
||||
// restore stdout
|
||||
dup2(save_stdout, STDOUT_FILENO);
|
||||
// don't send the rest
|
||||
::close(fd);
|
||||
return;
|
||||
}
|
||||
else
|
||||
found = tmpstr.find("index.html");
|
||||
// if "index.html" found, send the page
|
||||
if (found != std::string::npos)
|
||||
{
|
||||
int index_fd;
|
||||
std::string to_send;
|
||||
std::string end_header = "\r\n\r\n";
|
||||
std::string body;
|
||||
std::stringstream strs;
|
||||
char buffer[1];
|
||||
|
||||
to_send = "HTTP/1.1 200 OK\nContent-Type: text/html; charset=UTF-8\nContent-Length: ";
|
||||
index_fd = open("./index.html", O_RDONLY);
|
||||
for (int ret = 1; ret > 0;)
|
||||
{
|
||||
ret = read(index_fd, buffer, 1);
|
||||
body += buffer;
|
||||
}
|
||||
strs << body.size();
|
||||
to_send += strs.str();
|
||||
to_send += end_header;
|
||||
to_send += body;
|
||||
::send(fd, to_send.c_str(), to_send.size(), 0);
|
||||
// don't send the rest
|
||||
::close(fd);
|
||||
return;
|
||||
}
|
||||
// TMP end test cgi
|
||||
|
||||
|
||||
std::cout << "send()\n";
|
||||
if (::send(fd, _buf, _read_ret, 0) == -1)
|
||||
std::perror("err send(): ");
|
||||
|
||||
Reference in New Issue
Block a user