Skip to content

Commit

Permalink
chore(CGI.cpp): clean-up old commented code
Browse files Browse the repository at this point in the history
  • Loading branch information
samih713 committed Apr 17, 2024
1 parent 1a3c793 commit 95e4823
Showing 1 changed file with 70 additions and 237 deletions.
307 changes: 70 additions & 237 deletions sources/CGI/Cgi.cpp
Original file line number Diff line number Diff line change
@@ -1,176 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Cgi.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hmohamed <hmohamed@student.42abudhabi.ae> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/23 12:44:51 by hmohamed #+# #+# */
/* Updated: 2024/04/16 01:59:01 by hmohamed ### ########.fr */
/* */
/* ************************************************************************** */
#include "Cgi.hpp"

#include "Cgi.hpp"
static char** headers_to_env(const vsp& headers);
static string get_uri(string res);
static string get_query_string(string res);


// Cgi::Cgi(char* filePath, char** arguments, char** environment): filePath(filePath), arguments(arguments), environment(environment)
// {

// }

char **headersToEnv(const vsp &headers)
{
std::vector<char *> envVector;

// Iterate through headers
vsp::const_iterator end = headers.end();
for (vsp::const_iterator it = headers.begin(); it != end; ++it)
{
size_t len = it->first.size() + it->second.size() + 2;
char *envEntry = new char[len];
std::snprintf(envEntry, len, "%s=%s", it->first.c_str(), it->second.c_str());
envVector.push_back(envEntry);
}

// Allocate memory for char* array
char **envp = new char *[envVector.size() + 1];

// Copy pointers from vector to char* array
for (size_t i = 0; i < envVector.size(); ++i)
{
envp[i] = envVector[i];
}
envp[envVector.size()] = NULL;

return envp;
}

// string *geturi(string res)
// {
// char *result;
// string *resn;
// size_t qu;

// result = NULL;
// qu = res.find('?', 0);
// resn = new string(res.substr(0, qu));
// //result = const_cast<char *>(res.substr(0,qu).c_str());
// cout << *resn << endl;
// cout<< "test" << qu <<endl;
// result = const_cast<char *>(resn->c_str());
// cout<< "result : " << result <<endl;
// return (resn);
// }

string geturi(string res)
{
string resn;
size_t qu;

qu = res.find('?', 0);
resn = res.substr(0, qu);
//result = const_cast<char *>(res.substr(0,qu).c_str());
return (resn);
}

string getStingQuery(string res)
Cgi::Cgi(const Request& request)
{
string resn;
size_t qu;
size_t length;

qu = res.find('?', 0);
length = res.length();
cout<< "the ? :" << qu <<endl;
cout << "lingth:" << length << endl;
resn = res.substr(qu + 1, length);
//result = const_cast<char *>(res.substr(0,qu).c_str());
cout << resn << endl;
return (resn);
}


Cgi::Cgi(const Request &request)
{
string resource;

resource = request.get_resource();
headers = request.get_headers();
environment = headersToEnv(headers);
//filePath = const_cast<char *>(geturi(request.get_resource())->c_str());
filePath = (geturi(resource));
queryString = getStingQuery(resource);
//filePath = const_cast<char *> (request.get_resource().c_str());
// // Check if the Python script exists
// if (access(filePath, X_OK) == -1)
// {
// std::cerr << "Error: Python script not found or does not have execution permission." << std::endl;
// return ;
// }
cout<< "file path :" << filePath << endl;
arguments = new char *[2];
arguments[0] = const_cast<char *>(filePath.c_str());
arguments[1] = NULL;

// Print out the environment variables
for (int i = 0; environment[i] != NULL; ++i)
{
std::cout << environment[i] << std::endl;
}
string resource = request.get_resource();
headers = request.get_headers();
environment = headers_to_env(headers);
filePath = get_uri(resource);
queryString = get_query_string(resource);

arguments = new char*[2];
arguments[0] = const_cast<char*>(filePath.c_str());
arguments[1] = NULL;
}

Cgi::~Cgi()
{
for (int i = 0; environment[i] != NULL; ++i)
{
delete[] environment[i];
}
delete[] environment;
delete[] arguments;
delete[] arguments;
}

// void Cgi::execute()
// {
// int fd[2];
// int id;
// char *res_body;
// int a;

// res_body = (char *)malloc(100);
// res_body[99] = '\0';
// pipe(fd);
// id = fork();
// if (id == 0)
// {
// // Child process
// dup2(fd[1], STDOUT_FILENO);
// close(fd[0]);
// close(fd[1]);

// if (execve(filePath, arguments, environment) == -1)
// {
// std::cerr << "Error executing execve: " << strerror(errno) << std::endl;
// _exit(EXIT_FAILURE);
// }
// }

// // Wait for the child process to finish
// int status;
// waitpid(id, &status, 0);
// a = read(fd[0], res_body, 90);
// res_body[a] = '\0';
// cout << res_body << endl;
// close(fd[0]);
// close(fd[1]);


// }


void Cgi::execute(const std::string& outputFile)
{
int fd[2];
int id;
int fd[2];
int id;
std::string res_body;

// Create a pipe for communication
Expand All @@ -184,38 +42,39 @@ void Cgi::execute(const std::string& outputFile)
if (id == -1) {
std::cerr << "Error forking process: " << strerror(errno) << std::endl;
return;
} else if (id == 0) {
}
else if (id == 0) {
// Child process
dup2(fd[1], STDOUT_FILENO);
close(fd[0]);
close(fd[1]);

if (execve(const_cast<char *>(filePath.c_str()), arguments, environment) == -1) {
if (execve(const_cast<char*>(filePath.c_str()), arguments, environment) == -1) {
std::cerr << "Error executing execve: " << strerror(errno) << std::endl;
_exit(EXIT_FAILURE);
}
}

// Parent process
close(fd[1]); // Close the write end of the pipe
close(fd[1]); // Close the write end of the pipe

// Wait for the child process to finish
int status;
waitpid(id, &status, 0);

// Read the response from the pipe
char buffer[91];
char buffer[91];
ssize_t bytesRead;
while ((bytesRead = read(fd[0], buffer, 90)) > 0) {
while ((bytesRead = read(fd[0], buffer, 90)) > 0)
res_body.append(buffer, bytesRead);
}
// Save the response to a file
std::ofstream outFile(outputFile.c_str());
if (outFile.is_open()) {
outFile << res_body;
outFile.close();
std::cout << "Response saved to: " << outputFile << std::endl;
} else {
}
else {
std::cerr << "Error opening output file: " << strerror(errno) << std::endl;
}

Expand All @@ -229,28 +88,25 @@ string Cgi::execute(void)
std::string res_body;

// Create a pipe for communication
if (pipe(fd) == -1)
{
if (pipe(fd) == -1) {
std::cerr << "Error creating pipe: " << strerror(errno) << std::endl;
return NULL;
}

// Fork the process
// TODO fix forking here for sleep
id = fork();
if (id == -1)
{
if (id == -1) {
std::cerr << "Error forking process: " << strerror(errno) << std::endl;
return NULL;
}
else if (id == 0)
{
else if (id == 0) {
// Child process
dup2(fd[1], STDOUT_FILENO);
close(fd[0]);
close(fd[1]);

if (execve(const_cast<char *>(filePath.c_str()), arguments, environment) == -1)
{
if (execve(const_cast<char*>(filePath.c_str()), arguments, environment) == -1) {
std::cerr << "Error executing execve: " << strerror(errno) << std::endl;
_exit(EXIT_FAILURE);
}
Expand All @@ -267,78 +123,55 @@ string Cgi::execute(void)
char buffer[91];
ssize_t bytesRead;
while ((bytesRead = read(fd[0], buffer, 90)) > 0)
{
res_body.append(buffer, bytesRead);
}
// Save the response to a file
// std::ofstream outFile(outputFile.c_str());
// if (outFile.is_open())
// {
// outFile << res_body;
// outFile.close();
// std::cout << "Response saved to: " << outputFile << std::endl;
// }
// else
// {
// std::cerr << "Error opening output file: " << strerror(errno) << std::endl;
// }

close(fd[0]);
return (res_body);
return (res_body);
}

// std::string Cgi::execute(void)
// {
// int fd[2];
// int id;
// std::string res_body;
static char** headers_to_env(const vsp& headers)
{
std::vector<char*> envVector;

// // Create a pipe for communication
// if (pipe(fd) == -1) {
// std::cerr << "Error creating pipe: " << strerror(errno) << std::endl;
// return (NULL);
// }
// Iterate through headers
vsp::const_iterator end = headers.end();
for (vsp::const_iterator it = headers.begin(); it != end; ++it) {
size_t len = it->first.size() + it->second.size() + 2;
char* envEntry = new char[len];
std::snprintf(envEntry, len, "%s=%s", it->first.c_str(), it->second.c_str());
envVector.push_back(envEntry);
}

// // Fork the process
// id = fork();
// if (id == -1) {
// std::cerr << "Error forking process: " << strerror(errno) << std::endl;
// return (NULL);
// } else if (id == 0) {
// // Child process
// dup2(fd[1], STDOUT_FILENO);
// close(fd[0]);
// close(fd[1]);
// Allocate memory for char* array
char** envp = new char*[envVector.size() + 1];

// Copy pointers from vector to char* array
for (size_t i = 0; i < envVector.size(); ++i)
envp[i] = envVector[i];
envp[envVector.size()] = NULL;

// if (execve(filePath, arguments, environment) == -1) {
// std::cerr << "Error executing execve: " << strerror(errno) << std::endl;
// _exit(EXIT_FAILURE);
// }
// }
return envp;
}

// // Parent process
// close(fd[1]); // Close the write end of the pipe
static string get_uri(string res)
{
string resn;
size_t qu;

// // Wait for the child process to finish
// int status;
// waitpid(id, &status, 0);
qu = res.find('?', 0);
resn = res.substr(0, qu);
// result = const_cast<char *>(res.substr(0,qu).c_str());
return (resn);
}

// // Read the response from the pipe
// char buffer[91];
// ssize_t bytesRead;
// while ((bytesRead = read(fd[0], buffer, 90)) > 0) {
// res_body.append(buffer, bytesRead);
// }
// // Save the response to a file
// // std::ofstream outFile(outputFile);
// // if (outFile.is_open()) {
// // outFile << res_body;
// // outFile.close();
// // std::cout << "Response saved to: " << outputFile << std::endl;
// // } else {
// // std::cerr << "Error opening output file: " << strerror(errno) << std::endl;
// // }
static string get_query_string(string res)
{
string resn;
size_t qu;
size_t length;

// close(fd[0]);
// return(res_body);
// }
qu = res.find('?', 0);
length = res.length();
resn = res.substr(qu + 1, length);
return (resn);
}

0 comments on commit 95e4823

Please sign in to comment.