Skip to content

Commit

Permalink
Session management
Browse files Browse the repository at this point in the history
  • Loading branch information
josephcheel committed Jun 30, 2024
1 parent 8d59d93 commit 18de093
Show file tree
Hide file tree
Showing 14 changed files with 973 additions and 59 deletions.
40 changes: 30 additions & 10 deletions mandatory/inc/Location.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
#define VAR_LOC_ALLOW_METHODS "allow_methods"
#define VAR_LOC_AUTOINDEX "autoindex"
#define VAR_LOC_ALIAS "alias"
#define VAR_LOC_CGI_PATH "cgi_path"
#define VAR_LOC_CGI_ENABLED "cgi_enabled"
#define VAR_LOC_CGI_EXTENSION "cgi_extension"
#define VAR_LOC_COOKIE "set-cookie"
#define VAR_LOC_SESSION_ID "set-session-id"
#define STR_START "location:{"


Expand All @@ -33,20 +35,31 @@ class Location
std::string root;
std::string return_;
std::string index;
std::string alias;
enum LocationType LocationType;

std::string allowMethodsStr;
std::vector<std::string> allowMethods;
std::string autoindexStr;
bool autoindex;
std::string alias;
std::string cgiPathStr;
std::vector<std::string> cgiPath;

std::string cgiExtensionStr;
std::vector<std::string> cgiExtension;
enum LocationType LocationType;

bool isCookie;
std::string cookiesStr;
std::vector<std::string> cookies;

bool isGetAllowed;
bool isPostAllowed;
bool isDeleteAllowed;

bool autoindex;
std::string autoindexStr;

bool isCgi;
std::string cgiEnabledStr;

bool isSessionId;
std::string sessionIdStr;
int loadData(const std::string &data);

public:
Expand Down Expand Up @@ -75,7 +88,12 @@ class Location
const std::string& getAlias() const ;
enum LocationType getLocationType();
bool getIsCgi() const;
bool getIsCookie() const;
bool getIsSessionId() const;
std::vector<std::string> getCookies() const;
std::string getSessionId() const;
// Setter methods

void setName(const std::string&);
void setRoot(const std::string&);
void setReturn(const std::string&);
Expand All @@ -84,15 +102,17 @@ class Location
void setAllowMethods(const std::string& );
void setAutoindex(const std::string&);
void setAlias(const std::string&);
void setCgiPathStr(const std::string &paths);
void setCgiPath(const std::string &paths);
void setCgiEnabledStr(const std::string &option);
bool setCgiEnabled();
void setCgiExtensionStr(const std::string &extensions);
void setCgiExtension(const std::string &extensions);
void setCookieStr(const std::string &cookie);
void setCookies(const std::string &cookie);
void setSessionIdStr(const std::string &name);
// Load data from a string configuration
void print();
void checkVariables(bool serverAutoIndex);
std::vector<std::string> getCgiExtension();
std::vector<std::string> getCgiPath();
};

//typedef void (Location::*location)(std::string);
Expand Down
9 changes: 7 additions & 2 deletions mandatory/inc/LocationParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,19 @@ class LocationParser
Header request;
Header response;
Server *server;
// FileContent *fileContent;
Receive *receiver;
//std::string path;
std::string query;
bool isAutoIndex;
bool isCGI;
size_t startRange;
size_t endRange;
std::string cgiPath;
// std::string
bool isCookie;
std::vector<std::string> cookies;

bool isSessionId;
std::string sessionId;
public:
LocationParser(Header request, Server *server, Receive *receiver);
~LocationParser();
Expand All @@ -52,7 +55,9 @@ class LocationParser
bool getIsAutoIndex();
bool getIsCGI();
void checks();
void setCookies();
size_t getStartRange();
size_t getEndRange();
std::string getQuery();
void setSessionId();
};
4 changes: 3 additions & 1 deletion mandatory/inc/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,6 @@ T maximum(T a, T b)
if (a > b)
return a;
return b;
};
};

std::string getRandomHash(int lenght);
28 changes: 26 additions & 2 deletions mandatory/src/ListeningSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,26 +158,50 @@ void ListeningSocket::matchServerName(std::vector<Server *> servers)
}
}

void ListeningSocket::setCgiEnviroment()
{
this->cgiModule->setEnv(SERVER_ADDR_KEY, server->getHost());
this->cgiModule->setEnv(SERVER_NAME_KEY, server->getServerName());
this->cgiModule->setEnv(REQUEST_METHOD_KEY, this->request.getMethod());
// this->cgiModule->setEnv(PATH_INFO, this->request.getPath());
// this->cgiModule->setEnv(PROT, this->request.getProtocol());
this->cgiModule->setEnv(SERVER_PORT_KEY, this->request.getAttribute("Host").substr(this->request.getAttribute("Host").find(":")));
this->cgiModule->setEnv(CONTENT_LENGTH_KEY, this->request.getAttribute("Content-Lenght"));
this->cgiModule->setEnv(CONTENT_TYPE_KEY, this->request.getAttribute("Content-Type"));
this->cgiModule->setEnv(HTTP_USER_AGENT_KEY, this->request.getAttribute("User-Agent"));
// this->cgiModule->setEnv(, this->request.getAttribute("Host"));
this->cgiModule->setEnv(SERVER_SOFTWARE_KEY, "ws_cheelave/1.0");
this->cgiModule->setEnv(GATEWAY_INTERFACE_KEY, "CGI/1.1");
// this->cgiModule->setEnv(QUERY_STRING_KEY,this->);
}

void ListeningSocket::loadRequest(std::vector<Server *> servers)
{
this->request = Header(this->receiver->getRequest());
matchServerName(servers);
LocationParser Parser(this->request, this->server, this->receiver);
LocationParser Parser(this->request, this->server, this->receiver);
this->setCgiEnviroment();
try
{
Parser.checks();
}
catch (int e)
{
this->getFileContentForStatusCode(e);
this->request = Parser.getRequest();
this->response = Parser.getResponse();
return ;
}
this->cgiModule->setEnv(SCRIPT_FILENAME_KEY, this->request.getPath());
Parser.setCookies();
Parser.setSessionId();
this->request = Parser.getRequest();
this->response = Parser.getResponse();
this->setIsAutoIndex(Parser.getIsAutoIndex());
this->setIsCGI(Parser.getIsCGI());

this->setStartRange(Parser.getStartRange());
this->setEndRange(Parser.getEndRange());

this->setFileName(this->request.getPath(), Parser.getQuery());
}
125 changes: 87 additions & 38 deletions mandatory/src/Location.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: jcheel-n <jcheel-n@student.42barcelona. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/29 17:38:18 by eavedill #+# #+# */
/* Updated: 2024/06/24 23:41:29 by jcheel-n ### ########.fr */
/* Updated: 2024/06/29 21:31:29 by jcheel-n ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -22,8 +22,10 @@ std::map<std::string, int> var_names_location()
varnames[VAR_LOC_ALLOW_METHODS] = 0;
varnames[VAR_LOC_AUTOINDEX] = 0;
varnames[VAR_LOC_ALIAS] = 0;
varnames[VAR_LOC_CGI_PATH] = 0;
varnames[VAR_LOC_CGI_ENABLED] = 0;
varnames[VAR_LOC_COOKIE] = 0;
varnames[VAR_LOC_CGI_EXTENSION] = 0;
varnames[VAR_LOC_SESSION_ID] = 0;
return varnames;
}

Expand All @@ -38,8 +40,10 @@ std::map<std::string, void (Location::*)(const std::string&)> getLocationMethods
locationMethods[VAR_LOC_ALLOW_METHODS] = &Location::setAllowMethodsStr;
locationMethods[VAR_LOC_AUTOINDEX] = &Location::setAutoindex;
locationMethods[VAR_LOC_ALIAS] = &Location::setAlias;
locationMethods[VAR_LOC_CGI_PATH] = &Location::setCgiPathStr;
locationMethods[VAR_LOC_CGI_ENABLED] = &Location::setCgiEnabledStr;
locationMethods[VAR_LOC_COOKIE] = &Location::setCookieStr;
locationMethods[VAR_LOC_CGI_EXTENSION] = &Location::setCgiExtensionStr;
locationMethods[VAR_LOC_SESSION_ID] = &Location::setSessionIdStr;
return locationMethods;
}

Expand All @@ -53,13 +57,16 @@ Location::Location()
allowMethodsStr = "";
autoindex = "";
alias = "";
cgiPathStr = "";
// cgiPathStr = "";
cgiExtensionStr = "";
isCgi = false;
isSessionId = false;
isCookie = false;
}
Location::Location(std::string const &content)
{
this->isCgi = false;
this->cgiEnabledStr = "";
this->loadData(content);
}

Expand Down Expand Up @@ -91,6 +98,10 @@ bool Location::getPostAllowed() const { return isPostAllowed; }
bool Location::getDeleteAllowed() const { return isDeleteAllowed; }
enum LocationType Location::getLocationType() { return this->LocationType; }
bool Location::getIsCgi() const { return isCgi; }
bool Location::getIsCookie() const { return isCookie; }
std::vector<std::string> Location::getCookies() const { return cookies; }
bool Location::getIsSessionId() const { return isSessionId; }
std::string Location::getSessionId() const { return sessionIdStr; }
// Setter methods
void Location::setName(const std::string &n) { name = n; }
void Location::setRoot(const std::string &r) { root = r; }
Expand All @@ -99,7 +110,26 @@ void Location::setIndex(const std::string &idx) { index = idx; }
void Location::setAllowMethodsStr(const std::string &allow) { allowMethodsStr = allow; }
void Location::setAutoindex(const std::string &autoidx) { autoindexStr = autoidx; }
void Location::setAlias(const std::string &als) { alias = als; }
void Location::setCgiPathStr(const std::string &paths) { this->cgiPathStr = paths; }
void Location::setCgiEnabledStr(const std::string &cgiEnabled) {
this->cgiEnabledStr = cgiEnabled;
}
void Location::setSessionIdStr(const std::string &name){ this->isSessionId = true ; this->sessionIdStr = name; }
void Location::setCookieStr(const std::string &cookie) {this->isCookie = true ; this->cookiesStr = cookie; }
bool Location::setCgiEnabled()
{
if (this->cgiEnabledStr == "true")
isCgi = true;
else if (cgiEnabledStr == "false")
isCgi = false;
else if (!this->cgiEnabledStr.empty() && (this->cgiEnabledStr != "true" || this->cgiEnabledStr != "false"))
{
printLog("WARNING", "cgi_enabled\t\tis not defined correctly. Set to default " CHR_GREEN "false" RESET);
isCgi = false;
}
else if (this->cgiEnabledStr.empty())
isCgi = false;
return isCgi;
}
void Location::setCgiExtensionStr(const std::string &extensions) { this->cgiExtensionStr = extensions; }

void Location::setAllowMethods(const std::string& methods)
Expand Down Expand Up @@ -132,17 +162,17 @@ void Location::setAllowMethods(const std::string& methods)
}
}
}
void Location ::setCgiPath(const std::string &paths)
{
std::string line;
std::istringstream cgiPathStream(paths);
while (std::getline(cgiPathStream, line, ','))
{
if (line.length() == 0)
continue;
this->cgiPath.push_back(line);
}
}
// void Location ::setCgiPath(const std::string &paths)
// {
// std::string line;
// std::istringstream cgiPathStream(paths);
// while (std::getline(cgiPathStream, line, ','))
// {
// if (line.length() == 0)
// continue;
// this->cgiPath.push_back(line);
// }
// }

void Location ::setCgiExtension(const std::string &extensions)
{
Expand Down Expand Up @@ -207,24 +237,30 @@ void Location::print()
std::cout << "Allow Methods: " << allowMethodsStr << std::endl;
std::cout << "Autoindex: " << autoindex << std::endl;
std::cout << "Alias: " << alias << std::endl;
std::cout << "Cgi Path: " << cgiPathStr << std::endl;
std::cout << "Cgi Extension: " << cgiExtensionStr << std::endl;
}

void Location::setCookies(const std::string &cookie)
{
std::string line;
std::istringstream cookieStream(cookie);
while (std::getline(cookieStream, line, ','))
{
if (line.length() == 0)
continue;
this->cookies.push_back(line);
}
}


void Location::checkVariables(bool serverAutoIndex)
{
switch (Parser::checkLocationName(this->name))
Parser::checkLocationName(this->name);
if (this->setCgiEnabled())
{
case 2:
if (!Parser::checkCgiString(this->cgiPathStr, this->cgiExtensionStr))
exit(1);
this->setCgiPath(this->cgiPathStr);
this->setCgiExtension(this->cgiExtensionStr);
Parser::checkCgi(this->cgiPath, this->cgiExtension);
this->isCgi = true;
break ;
case 1:
break;
this->setCgiExtension(this->cgiExtensionStr);
if (!Parser::checkCgi(this->cgiExtensionStr))
exit(1);
}

switch (Parser::checkRootAliasReturn(this->root, this->alias,this->return_))
Expand Down Expand Up @@ -269,17 +305,30 @@ void Location::checkVariables(bool serverAutoIndex)
this->setAllowMethods(this->allowMethodsStr);
if (!Parser::checkAllowedMethods(this->allowMethodsStr))
this->isGetAllowed = true;
if (LocationType != RETURN)
Parser::checkIndex(this->getIndex(), this->getRoot());

}
if (LocationType == ROOT)
Parser::checkIndex(this->getIndex(), this->getRoot() + "/" + this->getName());
else if (LocationType == ALIAS)
Parser::checkIndex(this->getIndex(), this->getAlias());

std::vector<std::string> Location::getCgiExtension()
if (this->isCookie && this->cookiesStr.empty())
{
return this->cgiExtension;
printLog("WARNING", "cookie\t\t\tnot defined.");
this->isCookie = false;
}

std::vector<std::string> Location::getCgiPath()
else if (this->isCookie && !this->cookiesStr.empty())
this->setCookies(this->cookiesStr);

if (this->isSessionId && this->sessionIdStr.empty())
{
return this->cgiPath;
}
printLog("WARNING", "set-sesion-id\t\t\tnot defined.");
this->isSessionId = false;
}
else if (this->isSessionId && !this->sessionIdStr.empty())
this->setSessionIdStr(this->sessionIdStr);
}

std::vector<std::string> Location::getCgiExtension()
{
return this->cgiExtension;
}

Loading

0 comments on commit 18de093

Please sign in to comment.