Skip to content

Commit ffa20cd

Browse files
author
Abdelaziz Chakkaf
committed
fix query and remove system env
1 parent aa48313 commit ffa20cd

3 files changed

Lines changed: 12 additions & 17 deletions

File tree

headers/Cgi.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
class Cgi {
1717
private:
18-
char **createEnvironmentVariables(Request &request, char **systemEnv);
18+
char **createEnvironmentVariables(Request &request);
1919
std::vector<std::string> extractBinaryPaths(char **environmentVariables);
2020
std::string locateExecutable(const std::vector<std::string> &searchPaths,
2121
const std::string &executableName);

srcs/models/Cgi.cpp

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,17 @@ Cgi::Cgi() {}
2626

2727
Cgi::~Cgi() {}
2828

29-
char **Cgi::createEnvironmentVariables(Request &request, char **systemEnv)
29+
char **Cgi::createEnvironmentVariables(Request &request)
3030
{
31-
size_t systemEnvCount = 0;
32-
while (systemEnv[systemEnvCount])
33-
++systemEnvCount;
34-
35-
char **envVariables = new char *[systemEnvCount + request.getEnvSize() + 1]; // free this
3631
size_t index = 0;
32+
char **envVariables = new char *[request.getEnvSize() + 1];
3733

38-
while (systemEnv[index])
39-
{
40-
envVariables[index] = strdup(systemEnv[index]);
34+
while (index < request.getEnvSize()){
35+
envVariables[index] = strdup(request.getEnv(index).c_str());
4136
++index;
4237
}
4338

44-
for (size_t j = 0; !request.getEnv(j).empty(); ++j)
45-
envVariables[index++] = strdup(request.getEnv(j).c_str());
46-
47-
envVariables[index - 1] = NULL;
39+
envVariables[index] = NULL;
4840
return envVariables;
4941
}
5042

@@ -87,7 +79,7 @@ std::string Cgi::locateExecutable(const std::vector<std::string> &searchPaths, c
8779
std::string Cgi::executeCgiScript(Request &request, char **systemEnv)
8880
{
8981
request.convertToEnv();
90-
char **envVariables = createEnvironmentVariables(request, systemEnv);
82+
char **envVariables = createEnvironmentVariables(request);
9183
std::vector<std::string> binaryPaths = extractBinaryPaths(systemEnv);
9284
std::string scriptExtension;
9385
std::string interpreterPath;

srcs/models/Request.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Request::Request(const std::string &requestString, ClientData& c) :client(c)
3636

3737
if (this->path.find_first_of("?") != std::string::npos)
3838
{
39-
this->query = this->path.substr(this->path.find_first_of("?"));
39+
this->query = this->path.substr(this->path.find_first_of("?") + 1);
4040
size_t queryStart = this->path.find_first_of("?") + 1;
4141
while (true)
4242
{
@@ -104,10 +104,13 @@ void Request::convertToEnv(void)
104104
if (!headerPairs["Host"].empty())
105105
vEnv.push_back("HTTP_HOST="+ headerPairs["Host"]);
106106

107+
// vEnv.push_back(("DOCUMENT_ROOT="+ client.server->getRootPath()));
108+
vEnv.push_back("DOCUMENT_ROOT=/Users/achakkaf/Documents/webserv/var/www/html");
109+
107110
if (!headerPairs["User-Agent"].empty())
108111
vEnv.push_back("HTTP_USER_AGENT="+ headerPairs["User-Agent"]);
109112
if (!headerPairs["Cookie"].empty())
110-
vEnv.push_back("HTTP_COOKIE="+ headerPairs["Cookie"]); // is this correct HTTP_COOKIE=session=0c4982e7b7ef3dca??
113+
vEnv.push_back("HTTP_COOKIE="+ headerPairs["Cookie"]); // is this correct HTTP_COOKIE=session=0c4982e7b7ef3dca ??
111114
if (!headerPairs["Authorization"].empty())
112115
vEnv.push_back("HTTP_AUTHORIZATION="+ headerPairs["Authorization"]);
113116
printMap(vEnv); // remove this

0 commit comments

Comments
 (0)