From 95e4823ebaf1e9beb3142d6954ca8bc33189a1a9 Mon Sep 17 00:00:00 2001 From: samih713 Date: Wed, 17 Apr 2024 05:47:17 +0400 Subject: [PATCH] chore(CGI.cpp): clean-up old commented code --- sources/CGI/Cgi.cpp | 307 ++++++++++---------------------------------- 1 file changed, 70 insertions(+), 237 deletions(-) diff --git a/sources/CGI/Cgi.cpp b/sources/CGI/Cgi.cpp index bc5e25f..839a2b9 100644 --- a/sources/CGI/Cgi.cpp +++ b/sources/CGI/Cgi.cpp @@ -1,176 +1,34 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* Cgi.cpp :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: hmohamed +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* 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 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(res.substr(0,qu).c_str()); -// cout << *resn << endl; -// cout<< "test" << qu <(resn->c_str()); -// cout<< "result : " << result <(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 <(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(geturi(request.get_resource())->c_str()); - filePath = (geturi(resource)); - queryString = getStingQuery(resource); - //filePath = const_cast (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(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(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 @@ -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(filePath.c_str()), arguments, environment) == -1) { + if (execve(const_cast(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; } @@ -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(filePath.c_str()), arguments, environment) == -1) - { + if (execve(const_cast(filePath.c_str()), arguments, environment) == -1) { std::cerr << "Error executing execve: " << strerror(errno) << std::endl; _exit(EXIT_FAILURE); } @@ -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 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(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); -// } \ No newline at end of file + qu = res.find('?', 0); + length = res.length(); + resn = res.substr(qu + 1, length); + return (resn); +} \ No newline at end of file