Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
whayter committed Nov 1, 2021
1 parent 5590b37 commit 60c18d8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
22 changes: 16 additions & 6 deletions includes/cgi/cgi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ void setEnvironment(http::Request& request, ServerBlock& sblock, fs::path path)
setenv("SCRIPT_FILENAME", path.c_str(), 0);
setenv("CONTENT_TYPE", request.getHeader("Content-type").c_str(), 0);
setenv("CONTENT_LENGTH", request.getHeader("Content-Length").c_str(), 0);

if (!request.getContent().empty())
{
std::string body = ft::stringifyVector(request.getContent());
setenv("HTTP_RAW_POST_DATA", body.c_str(), 0);
}
}

void unsetEnvironment()
Expand All @@ -69,6 +75,8 @@ void unsetEnvironment()
unsetenv("SCRIPT_FILENAME");
unsetenv("CONTENT_TYPE");
unsetenv("CONTENT_LENGTH");

unsetenv("HTTP_RAW_POST_DATA");
}

static void replacePipeEnd(int oldFd, int newFd)
Expand Down Expand Up @@ -96,24 +104,26 @@ std::pair<std::vector<unsigned char>, ft::error_code> getCgiResponse(std::string
http::content_type cgiResponse;
int status = 0;
int childToParent[2];
int parentToChild[2];
int parentToChild[2]; // useless ?
pipe(childToParent);
pipe(parentToChild);
pipe(parentToChild); // useless ?
pid_t pid = fork();
if (pid == 0) // child process
{
close(childToParent[0]); // close reading end of childToParent
close(parentToChild[1]); // close writing end of parentToChild
replacePipeEnd(parentToChild[0], 0); // replace stdin with incoming pipe
close(parentToChild[1]); // close writing end of parentToChild // useless ?
replacePipeEnd(parentToChild[0], 0); // replace stdin with incoming pipe // useless ?


replacePipeEnd(childToParent[1], 1); // replace stdout with outgoing pipe
char* arg = 0;
if (execve(cgiExecPath.c_str(), &arg, environ) == -1) // exec cgi binary
exit(errno);
}
else if (pid > 0) // main process
{
close(parentToChild[0]); // close reading end of parentToChild
close(parentToChild[1]); // close writing end of parentToChild
close(parentToChild[0]); // close reading end of parentToChild // useless ?
close(parentToChild[1]); // close writing end of parentToChild // useless ?
close(childToParent[1]); // close writing end of childToParent
waitpid(pid, &status, 0); // wait for child process to end.
replacePipeEnd(childToParent[0], 0); // replace stdin with incoming pipe
Expand Down
2 changes: 1 addition & 1 deletion www/cgiParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

$indicesServer = array('GATEWAY_INTERFACE','SERVER_NAME','SERVER_SOFTWARE','SERVER_PROTOCOL',
'REQUEST_METHOD','REQUEST_TIME','REQUEST_TIME_FLOAT','QUERY_STRING','DOCUMENT_ROOT','HTTP_ACCEPT',
'HTTP_ACCEPT_ENCODING','HTTP_ACCEPT_LANGUAGE','HTTP_CONNECTION','HTTP_HOST','SCRIPT_FILENAME','REQUEST_URI') ;
'HTTP_ACCEPT_ENCODING','HTTP_ACCEPT_LANGUAGE','HTTP_CONNECTION','HTTP_HOST','SCRIPT_FILENAME','REQUEST_URI','HTTP_RAW_POST_DATA') ;

echo '<html lang="en"><head><meta charset="utf-8"><title>Webserv - CGI params</title>' ;
echo '<style>body{margin: 2vh 8vw;}section{margin: 10vh 4vw;}' ;
Expand Down

0 comments on commit 60c18d8

Please sign in to comment.