From 27f6ac4348e953ac46c5ad96a8757ae2b193299a Mon Sep 17 00:00:00 2001 From: josephcheel Date: Thu, 27 Jun 2024 13:10:40 +0200 Subject: [PATCH 01/31] Added cgi for multiple locations and cookies --- conf/eval.conf | 43 ++++++++- mandatory/inc/Header.hpp | 2 + mandatory/inc/Location.hpp | 19 ++-- mandatory/inc/LocationParser.hpp | 5 +- mandatory/inc/Parser.hpp | 3 +- mandatory/src/FileContent.cpp | 1 + mandatory/src/Header.cpp | 10 +- mandatory/src/ListeningSocket.cpp | 3 +- mandatory/src/Location.cpp | 100 ++++++++++++------- mandatory/src/LocationParser.cpp | 38 +++++++- mandatory/src/Parser.cpp | 53 +--------- mandatory/src/utils.cpp | 2 +- www/web3/index6.html | 101 ++++++++++++++++++++ www/web7/set-cookie.html | 146 ++++++++++++++++++++++++++++ www/web7/set-cookie2.html | 154 ++++++++++++++++++++++++++++++ www/web7/test.html | 34 +++++++ www/web7/welcome.html | 76 +++++++++++++++ 17 files changed, 683 insertions(+), 107 deletions(-) create mode 100644 www/web3/index6.html create mode 100644 www/web7/set-cookie.html create mode 100644 www/web7/set-cookie2.html create mode 100644 www/web7/test.html create mode 100644 www/web7/welcome.html diff --git a/conf/eval.conf b/conf/eval.conf index b8b42b4..dec4347 100644 --- a/conf/eval.conf +++ b/conf/eval.conf @@ -11,7 +11,7 @@ server:{ */ server:{ - port:8080; # listening port, mandatory parameter + port:8080:8084; # listening port, mandatory parameter host:0.0.0.0; # host or 127.0.0.1 by default server_name:example.com; # specify server_name, need to be added into /etc/hosts to work error_page:./www/errors; # default error page @@ -33,7 +33,7 @@ server:{ return:/uploads; # redirection } location:{ - name:/post; + name:/post/; allow_methods: GET; #allowed methods in location, GET only by default index:index5.html; # default page when requesting a directory, copies root index by default alias: ./www/web3; # replaces location part of URI. @@ -46,11 +46,11 @@ server:{ } location:{ name:/cgi-bin; - alias:./www/web6; + cgi_enabled: trusde; + alias:./www/web4; allow_methods: GET, POST, DELETE; autoindex:on; - cgi_path:/usr/bin/python3, /bin/sh; - cgi_extension:.py, .sh; + cgi_extension:cgi, py, sh; } location:{ name:/cgi-no-bin; @@ -64,4 +64,37 @@ server:{ allow_methods: GET; autoindex:on; } + location:{ + name:/set-cookie/; + # set-cookie:id, name, lastname; + + alias:./www/web7; + index:set-cookie.html; + allow_methods: GET, POST; + autoindex:on; + } + location:{ + name:/post-cookie/; + allow_methods: GET, POST; + set-cookie:id, name, lastname, theme; + alias:./www/web7; + index:welcome.html; + # index:cookie_get.html; + autoindex:off; + } + location:{ + name:/get-cookie/; + set-cookie:id, name, lastname, theme; + alias:./www/web7; + index:welcome.html; + allow_methods: GET, POST; + # autoindex:on; + } + location:{ + name:/cookie/; + alias:./www/web7; + allow_methods: GET, POST; + # index:welcome.html; + autoindex:on; + } }; diff --git a/mandatory/inc/Header.hpp b/mandatory/inc/Header.hpp index d7d8b77..e9e0509 100644 --- a/mandatory/inc/Header.hpp +++ b/mandatory/inc/Header.hpp @@ -22,6 +22,7 @@ class Header size_t contentLength; std::string contentType; std::map attributes; + std::multimap cookies; public: Header(); @@ -46,4 +47,5 @@ class Header std::string getContentType(); std::string getAttribute(std::string key); void printReceivedHeader(); + void setCookie(std::string value); }; diff --git a/mandatory/inc/Location.hpp b/mandatory/inc/Location.hpp index 02fc795..9e1bab9 100644 --- a/mandatory/inc/Location.hpp +++ b/mandatory/inc/Location.hpp @@ -15,8 +15,9 @@ #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 STR_START "location:{" @@ -38,15 +39,17 @@ class Location std::string autoindexStr; bool autoindex; std::string alias; - std::string cgiPathStr; - std::vector cgiPath; std::string cgiExtensionStr; std::vector cgiExtension; + std::string cookiesStr; + std::vector cookies; enum LocationType LocationType; + std::string cgiEnabledStr; bool isGetAllowed; bool isPostAllowed; bool isDeleteAllowed; bool isCgi; + bool isCookie; int loadData(const std::string &data); public: @@ -75,7 +78,10 @@ class Location const std::string& getAlias() const ; enum LocationType getLocationType(); bool getIsCgi() const; + bool getIsCookie() const; + std::vector getCookies() const; // Setter methods + void setName(const std::string&); void setRoot(const std::string&); void setReturn(const std::string&); @@ -84,15 +90,16 @@ 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); // Load data from a string configuration void print(); void checkVariables(bool serverAutoIndex); std::vector getCgiExtension(); - std::vector getCgiPath(); }; //typedef void (Location::*location)(std::string); diff --git a/mandatory/inc/LocationParser.hpp b/mandatory/inc/LocationParser.hpp index 54a577f..f845ccc 100644 --- a/mandatory/inc/LocationParser.hpp +++ b/mandatory/inc/LocationParser.hpp @@ -32,12 +32,12 @@ class LocationParser Header request; Header response; Server *server; - // FileContent *fileContent; Receive *receiver; - //std::string path; + std::vector cookies; std::string query; bool isAutoIndex; bool isCGI; + bool isCookie; size_t startRange; size_t endRange; std::string cgiPath; @@ -52,6 +52,7 @@ class LocationParser bool getIsAutoIndex(); bool getIsCGI(); void checks(); + void setCookies(); size_t getStartRange(); size_t getEndRange(); std::string getQuery(); diff --git a/mandatory/inc/Parser.hpp b/mandatory/inc/Parser.hpp index 4a6e44d..a9a1b84 100644 --- a/mandatory/inc/Parser.hpp +++ b/mandatory/inc/Parser.hpp @@ -31,7 +31,6 @@ class Parser static int checkLocationName(std::string name); static int checkRootAliasReturn(std::string root, std::string alias, std::string return_); static bool checkReturnIgnore(std::string allowMethods, std::string autoindex, std::string index); - static bool checkCgiString(std::string cgiPath, std::string cgiExtension); - static bool checkCgi(std::vector paths, std::vector extensions); + static bool checkCgi(std::string cgiExtension); static bool checkAllowedMethods(std::string allowMethods); }; \ No newline at end of file diff --git a/mandatory/src/FileContent.cpp b/mandatory/src/FileContent.cpp index 5b3a0f2..493d6d1 100644 --- a/mandatory/src/FileContent.cpp +++ b/mandatory/src/FileContent.cpp @@ -114,6 +114,7 @@ bool FileContent::setFileName(const std::string &file_name, const std::string &f // else if (this->isCgi) else if (cgiModule->getIsCGI() ) { + std::cout << "CGI" << std::endl; cgiModule->setFileName(file_name, fileArgs); this->setIsFileOpen(true); } diff --git a/mandatory/src/Header.cpp b/mandatory/src/Header.cpp index 6736334..61dbb97 100644 --- a/mandatory/src/Header.cpp +++ b/mandatory/src/Header.cpp @@ -96,7 +96,9 @@ std::string Header::generateHeader() const header += "Content-Type: " + contentType + "\r\n"; for (std::map::const_iterator it = attributes.begin(); it != attributes.end(); ++it) header += it->first + ": " + it->second + "\r\n"; - + for (std::multimap::const_iterator it = cookies.begin(); it != cookies.end(); ++it) + header += it->first + ": " + it->second + "\r\n"; + // response.setAttribute("Set-Cookie", "id=123; name=Joseph; lastname=Cheel; theme=light;"); header += "\r\n"; return header; @@ -141,6 +143,12 @@ void Header::setDate() this->date = buffer; } +void Header::setCookie(std::string value) +{ + this->cookies.insert(std::pair("Set-Cookie", value)); +} + + void Header::setLastModified(std::string lastModified) { this->lastModified = lastModified; diff --git a/mandatory/src/ListeningSocket.cpp b/mandatory/src/ListeningSocket.cpp index d4a9a05..162e04a 100644 --- a/mandatory/src/ListeningSocket.cpp +++ b/mandatory/src/ListeningSocket.cpp @@ -171,6 +171,7 @@ void ListeningSocket::loadRequest(std::vector servers) { this->getFileContentForStatusCode(e); } + Parser.setCookies(); this->request = Parser.getRequest(); this->response = Parser.getResponse(); this->setIsAutoIndex(Parser.getIsAutoIndex()); @@ -178,6 +179,6 @@ void ListeningSocket::loadRequest(std::vector servers) this->setStartRange(Parser.getStartRange()); this->setEndRange(Parser.getEndRange()); - + this->setFileName(this->request.getPath(), Parser.getQuery()); } diff --git a/mandatory/src/Location.cpp b/mandatory/src/Location.cpp index 1a8f593..46f58d4 100644 --- a/mandatory/src/Location.cpp +++ b/mandatory/src/Location.cpp @@ -6,7 +6,7 @@ /* By: jcheel-n 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; return varnames; } @@ -38,7 +39,8 @@ std::map 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; return locationMethods; } @@ -53,13 +55,14 @@ Location::Location() allowMethodsStr = ""; autoindex = ""; alias = ""; - cgiPathStr = ""; + // cgiPathStr = ""; cgiExtensionStr = ""; isCgi = false; } Location::Location(std::string const &content) { this->isCgi = false; + this->cgiEnabledStr = ""; this->loadData(content); } @@ -91,6 +94,8 @@ 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 Location::getCookies() const { return cookies; } // Setter methods void Location::setName(const std::string &n) { name = n; } void Location::setRoot(const std::string &r) { root = r; } @@ -99,7 +104,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::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) @@ -132,17 +156,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) { @@ -207,24 +231,31 @@ 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 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_)) @@ -271,6 +302,13 @@ void Location::checkVariables(bool serverAutoIndex) this->isGetAllowed = true; if (LocationType != RETURN) Parser::checkIndex(this->getIndex(), this->getRoot()); + if (this->isCookie && this->cookiesStr.empty()) + { + printLog("WARNING", "cookie\t\t\tnot defined."); + this->isCookie = false; + } + else if (this->isCookie && !this->cookiesStr.empty()) + this->setCookies(this->cookiesStr); } @@ -278,8 +316,4 @@ void Location::checkVariables(bool serverAutoIndex) { return this->cgiExtension; } - - std::vector Location::getCgiPath() - { - return this->cgiPath; - } \ No newline at end of file + \ No newline at end of file diff --git a/mandatory/src/LocationParser.cpp b/mandatory/src/LocationParser.cpp index d89ca91..d4840bc 100644 --- a/mandatory/src/LocationParser.cpp +++ b/mandatory/src/LocationParser.cpp @@ -72,16 +72,19 @@ int LocationParser::matchingLocation() { if (isAllowedMethod(locations[i]) == NOT_ALLOWED) return NOT_ALLOWED; + std::string rawPath = this->request.getPath(); this->isAutoIndex = locations[i]->getAutoIndex(); + if ((this->isCookie = locations[i]->getIsCookie()) == true) + this->cookies = locations[i]->getCookies(); if (locations[i]->getIsCgi() == true) { std::string extension; if (rawPath.rfind(".") != std::string::npos) extension = rawPath.substr(rawPath.rfind(".") + 1, rawPath.size()); - for (size_t y = 0; y < locations[i]->getCgiExtension().size(); y++) { + if (locations[i]->getCgiExtension()[y] == extension) this->isCGI = true; } @@ -192,12 +195,28 @@ bool isMethodNotStandard(std::string method) return false; } +void LocationParser::setCookies() +{ + if (isCookie) + { + std::string setcookie; + std::vector querylines = splitString(this->query, '&'); + for (size_t i = 0; i < querylines.size(); i++) + { + std::vector queryline = splitString(querylines[i], '='); + if (std::find(this->cookies.begin(), this->cookies.end(), queryline[0]) != this->cookies.end()) + response.setCookie(queryline[0] + "=" + queryline[1] + "; path=/"); + } + } +} + void LocationParser::checks() { std::string path; if (this->request.getPath().find("?") != std::string::npos) { this->query = this->request.getPath().substr(this->request.getPath().find("?") + 1); + this->query = decodeURL(this->query); this->request.setPath(this->request.getPath().substr(0, this->request.getPath().find("?"))); } switch (this->matchingLocation()) @@ -218,7 +237,6 @@ void LocationParser::checks() } this->request.setPath(decodeURL(this->request.getPath())); - // std::cout << request.getPath() << std::endl; if (isBadRequest(receiver->getRequest()))//|| isURIMalformed(this->request.getPath()) { response.setStatus("400 Bad Request"); @@ -332,8 +350,12 @@ void LocationParser::checks() { std::cout << "filename: " << lines[i] << std::endl; std::string filename = lines[i].substr(lines[i].find("filename=") + 10, lines[i].size()); + std::string path = this->request.getPath(); filename = filename.substr(0, filename.find("\"")); - std::string path = this->request.getPath() + "/" + filename; + if (path.find(filename) == std::string::npos) + { + path += "/" + filename; + } if (access(path.c_str(), F_OK) == 0) { response.setStatus("403 Forbidden"); @@ -349,8 +371,11 @@ void LocationParser::checks() } else if (receiver->getisform()) { - this->query = body; - response.setStatus("200 Created"); + this->query = decodeURL(body); + std::cout << this->query << std::endl; + + response.setStatus("201 Created"); + throw CREATED_CODE; } response.setServer(server->getServerName()); } @@ -359,7 +384,10 @@ void LocationParser::checks() if (std::remove(this->request.getPath().c_str()) == 0) response.setStatus("200 OK"); else + { response.setStatus("404 Not Found"); + throw NOT_FOUND_CODE; + } } else { diff --git a/mandatory/src/Parser.cpp b/mandatory/src/Parser.cpp index 6f053d0..1dffdcf 100644 --- a/mandatory/src/Parser.cpp +++ b/mandatory/src/Parser.cpp @@ -304,11 +304,6 @@ int Parser::checkLocationName(std::string name) printLog("ERROR", "location name\t\t\t\tmust start with /" ); exit(1); } - std::string tmp; - tmp = name; - tmp.erase(std::remove(tmp.begin(), tmp.end(), '/'), tmp.end()); - if (tmp == "cgi-bin") - return 2; return 1; } @@ -358,20 +353,9 @@ bool Parser::checkReturnIgnore(std::string allowMethods, std::string autoindex, return true; } -bool Parser::checkCgiString(std::string cgiPath, std::string cgiExtension) +bool Parser::checkCgi(std::string cgiExtension) { - if (cgiPath.empty() && cgiExtension.empty()) - { - printLog("ERROR", "cgi_path\t\t\tis not defined."); - printLog("ERROR", "cgi_extension\t\t\tis not defined."); - return false; - } - else if (cgiPath.empty()) - { - printLog("ERROR", "cgi_path\t\t\tis not defined."); - return false; - } - else if (cgiExtension.empty()) + if (cgiExtension.empty()) { printLog("ERROR", "cgi_extension\t\t\tis not defined."); return false; @@ -380,39 +364,6 @@ bool Parser::checkCgiString(std::string cgiPath, std::string cgiExtension) return true; } -bool Parser::checkCgi(std::vector paths, std::vector extensions) -{ - if (paths.size() != extensions.size()) - { - printLog("ERROR", "cgi_path and cgi_extension must have the same number of elements."); - return false; - } - for (size_t size = 0; size < paths.size(); size++) - { - switch (isFilePermissions(paths[size], X_OK)) - { - case -2: - printLog("ERROR", "cgi_path\t\t<" + paths[size] + ">\tempty path"); - return false; - case -1: - printLog("ERROR", "cgi_path\t\t<" + paths[size] + ">\terror getting file metadata"); - return false; - case -3: - printLog("ERROR", "cgi_path\t\t<" + paths[size] + ">\tnot a regular file"); - return false; - case -4: - printLog("ERROR", "cgi_path\t\t<" + paths[size] + ">\tpermission denied"); - return false; - case -5: - printLog("ERROR", "cgi_path\t\t<" + paths[size] + ">\tfile not found"); - return false; - default: - break ; - } - } - return true; -} - bool Parser::checkAllowedMethods(std::string allowMethods) { if (allowMethods.length() == 0) diff --git a/mandatory/src/utils.cpp b/mandatory/src/utils.cpp index e5e082c..8826a5a 100644 --- a/mandatory/src/utils.cpp +++ b/mandatory/src/utils.cpp @@ -86,7 +86,7 @@ size_t stringToSizeT(const std::string& str) { std::vector splitString(const std::string& str, char delimiter) { std::vector result; std::string token; - std::cout << "str: " << str << std::endl; + // std::cout << "str: " << str << std::endl; std::istringstream tokenStream(str); while (std::getline(tokenStream, token, delimiter)) { diff --git a/www/web3/index6.html b/www/web3/index6.html new file mode 100644 index 0000000..7fed569 --- /dev/null +++ b/www/web3/index6.html @@ -0,0 +1,101 @@ + + + + + + File Upload and Delete + + + +
+

Upload File

+
+ + +
+ +

Delete File

+
+ + + +
+
+ + + + diff --git a/www/web7/set-cookie.html b/www/web7/set-cookie.html new file mode 100644 index 0000000..a48082e --- /dev/null +++ b/www/web7/set-cookie.html @@ -0,0 +1,146 @@ + + + + + + Themed Form + + + + +
+
+ + +
+

Enter your details

+
+
+ + +
+
+ + +
+ + +
+
+ + + + diff --git a/www/web7/set-cookie2.html b/www/web7/set-cookie2.html new file mode 100644 index 0000000..402206d --- /dev/null +++ b/www/web7/set-cookie2.html @@ -0,0 +1,154 @@ + + + + + + Themed Form + + + + +
+
+ + +
+

Enter your details

+
+
+ + +
+
+ + +
+ + +
+
+ + + + diff --git a/www/web7/test.html b/www/web7/test.html new file mode 100644 index 0000000..c617ef7 --- /dev/null +++ b/www/web7/test.html @@ -0,0 +1,34 @@ + + + + + Set Cookie Example + + +

Set Cookie Example

+
+ + +

+ + +

+ +
+ + + + diff --git a/www/web7/welcome.html b/www/web7/welcome.html new file mode 100644 index 0000000..7925a54 --- /dev/null +++ b/www/web7/welcome.html @@ -0,0 +1,76 @@ + + + + + + Welcome Back! + + + +
+

Welcome Back, !

+

You are using the theme.

+
+ + + + From 4f781bd6f4d3203f614a837dfd40fb506ada5797 Mon Sep 17 00:00:00 2001 From: josephcheel Date: Thu, 27 Jun 2024 13:41:45 +0200 Subject: [PATCH 02/31] Added check for path for alias too --- mandatory/src/Location.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/mandatory/src/Location.cpp b/mandatory/src/Location.cpp index 46f58d4..3ce1d2c 100644 --- a/mandatory/src/Location.cpp +++ b/mandatory/src/Location.cpp @@ -6,7 +6,7 @@ /* By: jcheel-n 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()); + if (this->isCookie && this->cookiesStr.empty()) { printLog("WARNING", "cookie\t\t\tnot defined."); From ba7b327803c3e6c7452b70846d281d5cc7c87bde Mon Sep 17 00:00:00 2001 From: josephcheel Date: Thu, 27 Jun 2024 13:46:35 +0200 Subject: [PATCH 03/31] normalized multilple // --- mandatory/src/LocationParser.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mandatory/src/LocationParser.cpp b/mandatory/src/LocationParser.cpp index d4840bc..80da4b3 100644 --- a/mandatory/src/LocationParser.cpp +++ b/mandatory/src/LocationParser.cpp @@ -237,6 +237,9 @@ void LocationParser::checks() } this->request.setPath(decodeURL(this->request.getPath())); + ExtendedString tmp = this->request.getPath(); + tmp.replaceFirstString("//", "/"); + this->request.setPath(tmp); if (isBadRequest(receiver->getRequest()))//|| isURIMalformed(this->request.getPath()) { response.setStatus("400 Bad Request"); @@ -348,7 +351,6 @@ void LocationParser::checks() { if (lines[i].find("filename=") != std::string::npos) { - std::cout << "filename: " << lines[i] << std::endl; std::string filename = lines[i].substr(lines[i].find("filename=") + 10, lines[i].size()); std::string path = this->request.getPath(); filename = filename.substr(0, filename.find("\"")); From ddfa60dd1294b64c8491b6c6e2f3678f8d020e88 Mon Sep 17 00:00:00 2001 From: josephcheel Date: Thu, 27 Jun 2024 23:53:22 +0200 Subject: [PATCH 04/31] added hash function to cookie management --- conf/eval.conf | 26 ++++---------------------- mandatory/src/utils.cpp | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/conf/eval.conf b/conf/eval.conf index dec4347..d92afb8 100644 --- a/conf/eval.conf +++ b/conf/eval.conf @@ -11,7 +11,7 @@ server:{ */ server:{ - port:8080:8084; # listening port, mandatory parameter + port:8080, 8081; # listening port, mandatory parameter host:0.0.0.0; # host or 127.0.0.1 by default server_name:example.com; # specify server_name, need to be added into /etc/hosts to work error_page:./www/errors; # default error page @@ -19,11 +19,6 @@ server:{ root:./www/web1; # root folder of site directory, full or relative path, mandatory parameter index:index.html; # default page when requesting a directory, index.html by default autoindex: on; -# location:{ -# name:/; -# root:./www/web1; # root folder of site directory, full or relative path, mandatory parameter -# index:index.html; # default page when requesting a directory, index.html by default -# } location:{ name:/redirect; return:https://www.42barcelona.com/; # redirection @@ -46,8 +41,8 @@ server:{ } location:{ name:/cgi-bin; - cgi_enabled: trusde; - alias:./www/web4; + cgi_enabled: true; + alias:./www/web6; allow_methods: GET, POST, DELETE; autoindex:on; cgi_extension:cgi, py, sh; @@ -66,21 +61,10 @@ server:{ } location:{ name:/set-cookie/; - # set-cookie:id, name, lastname; - alias:./www/web7; - index:set-cookie.html; + index:set-cookie2.html; allow_methods: GET, POST; autoindex:on; - } - location:{ - name:/post-cookie/; - allow_methods: GET, POST; - set-cookie:id, name, lastname, theme; - alias:./www/web7; - index:welcome.html; - # index:cookie_get.html; - autoindex:off; } location:{ name:/get-cookie/; @@ -88,13 +72,11 @@ server:{ alias:./www/web7; index:welcome.html; allow_methods: GET, POST; - # autoindex:on; } location:{ name:/cookie/; alias:./www/web7; allow_methods: GET, POST; - # index:welcome.html; autoindex:on; } }; diff --git a/mandatory/src/utils.cpp b/mandatory/src/utils.cpp index 8826a5a..9c2eea7 100644 --- a/mandatory/src/utils.cpp +++ b/mandatory/src/utils.cpp @@ -198,3 +198,17 @@ void replaceFirstString(std::string& mainString, const std::string& searchString pos += replaceString.length(); } } + +std::string getRandomHash(int lenght) +{ + std::string characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + + std::string hash; + int x; + std::srand(static_cast(std::time(0)) ^ reinterpret_cast(&x)); + + // std::srand(static_cast(std::time(0)) ^ static_cast(getpid())); + for (int i = 0 ; i < lenght; i++) + hash += characters[rand() % characters.size()]; + return hash; +} \ No newline at end of file From 05152f2ee2c645d330cc79e3ba0f3f07cf6edec7 Mon Sep 17 00:00:00 2001 From: josephcheel Date: Fri, 28 Jun 2024 00:00:04 +0200 Subject: [PATCH 05/31] set-cookie corrected html file --- conf/eval.conf | 2 +- www/web7/set-cookie3.html | 170 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 www/web7/set-cookie3.html diff --git a/conf/eval.conf b/conf/eval.conf index d92afb8..14e5e80 100644 --- a/conf/eval.conf +++ b/conf/eval.conf @@ -62,7 +62,7 @@ server:{ location:{ name:/set-cookie/; alias:./www/web7; - index:set-cookie2.html; + index:set-cookie3.html; allow_methods: GET, POST; autoindex:on; } diff --git a/www/web7/set-cookie3.html b/www/web7/set-cookie3.html new file mode 100644 index 0000000..604bc7e --- /dev/null +++ b/www/web7/set-cookie3.html @@ -0,0 +1,170 @@ + + + + + + Themed Form + + + + +
+
+ + +
+

Enter your details

+
+
+ + +
+
+ + +
+ + +
+
+ + + + From 17b5f2757652ef1d89bab1d4d941ca32c47e280e Mon Sep 17 00:00:00 2001 From: Joseph Cheel nakashima Date: Sat, 29 Jun 2024 12:30:04 +0200 Subject: [PATCH 06/31] now error pages are shwn --- mandatory/src/ListeningSocket.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mandatory/src/ListeningSocket.cpp b/mandatory/src/ListeningSocket.cpp index 162e04a..bfd2934 100644 --- a/mandatory/src/ListeningSocket.cpp +++ b/mandatory/src/ListeningSocket.cpp @@ -170,10 +170,13 @@ void ListeningSocket::loadRequest(std::vector servers) catch (int e) { this->getFileContentForStatusCode(e); + } Parser.setCookies(); this->request = Parser.getRequest(); this->response = Parser.getResponse(); + if (this->getCode() >= 400) + return ; this->setIsAutoIndex(Parser.getIsAutoIndex()); this->setIsCGI(Parser.getIsCGI()); From db7a19cd642a8ac96d2a1d677d68302e61cbbf27 Mon Sep 17 00:00:00 2001 From: "ernesto.avedillo" Date: Sat, 29 Jun 2024 12:33:26 +0200 Subject: [PATCH 07/31] Templates only in one folder --- {StatusCodes => Templates}/ErrorTemplate.html | 0 {Master => Templates}/dir_list.html | 0 mandatory/inc/ListDir.hpp | 5 +++-- mandatory/inc/StatusCodesDefinition.hpp | 2 +- mandatory/src/ListDir.cpp | 2 +- 5 files changed, 5 insertions(+), 4 deletions(-) rename {StatusCodes => Templates}/ErrorTemplate.html (100%) rename {Master => Templates}/dir_list.html (100%) diff --git a/StatusCodes/ErrorTemplate.html b/Templates/ErrorTemplate.html similarity index 100% rename from StatusCodes/ErrorTemplate.html rename to Templates/ErrorTemplate.html diff --git a/Master/dir_list.html b/Templates/dir_list.html similarity index 100% rename from Master/dir_list.html rename to Templates/dir_list.html diff --git a/mandatory/inc/ListDir.hpp b/mandatory/inc/ListDir.hpp index 0811886..b5f6bbf 100644 --- a/mandatory/inc/ListDir.hpp +++ b/mandatory/inc/ListDir.hpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* ListDir.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: eavedill +#+ +:+ +#+ */ +/* By: eavedill +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/07 19:03:57 by eavedill #+# #+# */ -/* Updated: 2024/06/21 14:35:44 by eavedill ### ########.fr */ +/* Updated: 2024/06/29 12:32:21 by eavedill ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,6 +20,7 @@ #include "Attributes.hpp" #include "utils.hpp" #include "colors.h" +#define TEMPLATE_LIST_DIR "./Template/dir_list.html" class ListDir { private: diff --git a/mandatory/inc/StatusCodesDefinition.hpp b/mandatory/inc/StatusCodesDefinition.hpp index 73681e3..0cc0ccb 100644 --- a/mandatory/inc/StatusCodesDefinition.hpp +++ b/mandatory/inc/StatusCodesDefinition.hpp @@ -1,4 +1,4 @@ -#define DEFAULT_ERROR_FILE "./StatusCodes/ErrorTemplate.html" +#define DEFAULT_ERROR_FILE "./Template/ErrorTemplate.html" #define CONTINUE_CODE 100 #define SWITCHING_PROTOCOLS_CODE 101 #define OK_CODE 200 diff --git a/mandatory/src/ListDir.cpp b/mandatory/src/ListDir.cpp index 0195795..1f609c5 100644 --- a/mandatory/src/ListDir.cpp +++ b/mandatory/src/ListDir.cpp @@ -116,7 +116,7 @@ bool ListDir::getIsSendComlete() void ListDir::openMasterListFile() { - std::string filename = "./Master/dir_list.html"; + std::string filename = TEMPLATE_LIST_DIR; file.open(filename.c_str(), std::ios::out | std::ios::binary); } From 51c12b0a7d9a2060b523078cc9f13e25bab16c46 Mon Sep 17 00:00:00 2001 From: "ernesto.avedillo" Date: Sat, 29 Jun 2024 12:33:51 +0200 Subject: [PATCH 08/31] Return when error detedted --- conf/eval.conf | 1 + mandatory/src/ListeningSocket.cpp | 1 + 2 files changed, 2 insertions(+) diff --git a/conf/eval.conf b/conf/eval.conf index 14e5e80..7b77f8d 100644 --- a/conf/eval.conf +++ b/conf/eval.conf @@ -69,6 +69,7 @@ server:{ location:{ name:/get-cookie/; set-cookie:id, name, lastname, theme; + set-id: true; alias:./www/web7; index:welcome.html; allow_methods: GET, POST; diff --git a/mandatory/src/ListeningSocket.cpp b/mandatory/src/ListeningSocket.cpp index 162e04a..905a421 100644 --- a/mandatory/src/ListeningSocket.cpp +++ b/mandatory/src/ListeningSocket.cpp @@ -170,6 +170,7 @@ void ListeningSocket::loadRequest(std::vector servers) catch (int e) { this->getFileContentForStatusCode(e); + return ; } Parser.setCookies(); this->request = Parser.getRequest(); From 57e9e45b7910198764ff09eed0afe2a189e82445 Mon Sep 17 00:00:00 2001 From: Joseph Cheel nakashima Date: Sat, 29 Jun 2024 12:38:16 +0200 Subject: [PATCH 09/31] now seems to work, error codes pages --- mandatory/src/ListeningSocket.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mandatory/src/ListeningSocket.cpp b/mandatory/src/ListeningSocket.cpp index bfd2934..817d9ee 100644 --- a/mandatory/src/ListeningSocket.cpp +++ b/mandatory/src/ListeningSocket.cpp @@ -170,13 +170,13 @@ void ListeningSocket::loadRequest(std::vector servers) catch (int e) { this->getFileContentForStatusCode(e); - + this->request = Parser.getRequest(); + this->response = Parser.getResponse(); + return ; } Parser.setCookies(); this->request = Parser.getRequest(); this->response = Parser.getResponse(); - if (this->getCode() >= 400) - return ; this->setIsAutoIndex(Parser.getIsAutoIndex()); this->setIsCGI(Parser.getIsCGI()); From 978e8848cc6f00f87681c8711bccbdaaa61f7b9f Mon Sep 17 00:00:00 2001 From: Joseph Cheel nakashima Date: Sat, 29 Jun 2024 12:41:53 +0200 Subject: [PATCH 10/31] solved wrong path for template statuscodes --- mandatory/inc/StatusCodesDefinition.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mandatory/inc/StatusCodesDefinition.hpp b/mandatory/inc/StatusCodesDefinition.hpp index 0cc0ccb..149cbd9 100644 --- a/mandatory/inc/StatusCodesDefinition.hpp +++ b/mandatory/inc/StatusCodesDefinition.hpp @@ -1,4 +1,4 @@ -#define DEFAULT_ERROR_FILE "./Template/ErrorTemplate.html" +#define DEFAULT_ERROR_FILE "./Templates/ErrorTemplate.html" #define CONTINUE_CODE 100 #define SWITCHING_PROTOCOLS_CODE 101 #define OK_CODE 200 From b1f90cc0db97547e0d584b5f1a259355d813eeec Mon Sep 17 00:00:00 2001 From: Joseph Cheel nakashima Date: Sat, 29 Jun 2024 12:53:13 +0200 Subject: [PATCH 11/31] Wrong listDir path solved --- mandatory/inc/ListDir.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mandatory/inc/ListDir.hpp b/mandatory/inc/ListDir.hpp index b5f6bbf..964c3be 100644 --- a/mandatory/inc/ListDir.hpp +++ b/mandatory/inc/ListDir.hpp @@ -20,7 +20,7 @@ #include "Attributes.hpp" #include "utils.hpp" #include "colors.h" -#define TEMPLATE_LIST_DIR "./Template/dir_list.html" +#define TEMPLATE_LIST_DIR "./Templates/dir_list.html" class ListDir { private: From 2ccca8fabfccbf91086f30f791001672e2ea3d51 Mon Sep 17 00:00:00 2001 From: "ernesto.avedillo" Date: Sat, 29 Jun 2024 13:34:01 +0200 Subject: [PATCH 12/31] correct error 500 in CGI --- conf/simple.conf | 2 +- mandatory/src/CGI.cpp | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/conf/simple.conf b/conf/simple.conf index b412eff..2e7d479 100644 --- a/conf/simple.conf +++ b/conf/simple.conf @@ -67,7 +67,7 @@ server:{ alias:./www/web4; allow_methods: GET, POST, DELETE; autoindex:on; - cgi_path:/bin/sh; + cgi_enabled: true; cgi_extension:cgi; } }; diff --git a/mandatory/src/CGI.cpp b/mandatory/src/CGI.cpp index d48ba56..611e661 100644 --- a/mandatory/src/CGI.cpp +++ b/mandatory/src/CGI.cpp @@ -120,7 +120,6 @@ std::string CGI::execute() signal(SIGALRM, &CGI::alarm_handler); int fd[2], tmp_fd; tmp_fd = dup(STDOUT_FILENO); - std::cout << "Executable: " << Executable << std::endl; std::vector ExecArray; ExecArray.push_back(const_cast(Executable.c_str())); std::vector::iterator itb = args.begin(); @@ -149,7 +148,7 @@ std::string CGI::execute() { // Handle error executing file std::cerr << "Error executing file " << Executable << " with errno " << errno << std::endl; - exit(EXIT_FAILURE); + exit(errno); } } // Parent process @@ -171,9 +170,10 @@ std::string CGI::execute() // } // else // { - if (WIFEXITED(status)) + if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { // Read the output from the file descriptor + std::cerr << "Error executing file 3 " << Executable << " with errno " << WIFEXITED(status) << std::endl; char buffer[1024]; ssize_t bytesRead; while ((bytesRead = read(fd[0], buffer, sizeof(buffer))) > 0) { @@ -183,7 +183,10 @@ std::string CGI::execute() fd[1] = tmp_fd; } else + { + std::cerr << "Error executing file 2 " << Executable << " with errno " << WIFEXITED(status) << std::endl; throw INTERNAL_SERVER_ERROR_CODE; + } // } return output; } From 08ccd8ee3eb29faf75a93b3b978203a8455d9b4b Mon Sep 17 00:00:00 2001 From: "ernesto.avedillo" Date: Sat, 29 Jun 2024 13:35:29 +0200 Subject: [PATCH 13/31] add env in CGI header --- mandatory/inc/CGI.hpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mandatory/inc/CGI.hpp b/mandatory/inc/CGI.hpp index 7ea9076..db7d92c 100644 --- a/mandatory/inc/CGI.hpp +++ b/mandatory/inc/CGI.hpp @@ -19,6 +19,7 @@ class CGI std::string CGIFolder; std::string fileName; std::string fileArgs; + char **env; bool isCGI; std::vector args; std::map CGIExtensions; @@ -38,6 +39,7 @@ class CGI //bool setIsCGI(const std::string &str); void setArgs(const std::string &); void setCGIMapExtensions(std::string const &cgi_extension); + void setEnv(char **); //getters std::string getFileName(); std::string getCGIFolder(); @@ -45,6 +47,7 @@ class CGI std::string getCGIExtension(const std::string &); std::string getFileExtension(); std::map::iterator findCGIExtension(const std::string &); + char **getEnv(); //------ OTHER Methods ------// std::string execute(); CGI *clone(); From 571b138328c542f070a641b4d85032849a52fbd5 Mon Sep 17 00:00:00 2001 From: "ernesto.avedillo" Date: Sat, 29 Jun 2024 16:31:31 +0200 Subject: [PATCH 14/31] Remove comments --- mandatory/src/CGI.cpp | 45 ++++++++++++++---------------------------- www/web4/cgi/test.cgi | Bin 17096 -> 17400 bytes www/web4/cgi/test.cpp | 18 ++++++++++++++++- 3 files changed, 32 insertions(+), 31 deletions(-) diff --git a/mandatory/src/CGI.cpp b/mandatory/src/CGI.cpp index 611e661..0e1b45b 100644 --- a/mandatory/src/CGI.cpp +++ b/mandatory/src/CGI.cpp @@ -147,48 +147,33 @@ std::string CGI::execute() if (execve(Executable.c_str(), ExecArray.data(), NULL) == -1) { // Handle error executing file - std::cerr << "Error executing file " << Executable << " with errno " << errno << std::endl; exit(errno); } } // Parent process close(fd[1]); - // Wait for the child process to finish CGI::ChildPID = pid; alarm(timeout); int status; - // pid_t result = waitpid(pid, &status, 0); waitpid(pid, &status, 0); std::string output; - // Check if the child process exited normally - // std::cout << "Result: " << static_cast (result) << "errno "<< EINTR << "vs" << errno << std::endl; - // if (result == -1 && errno == EINTR) - // { - // kill(pid, SIGKILL); - // throw GATEWAY_TIME_OUT_CODE; - // } - // else - // { - if (WIFEXITED(status) && WEXITSTATUS(status) == 0) - { - // Read the output from the file descriptor - std::cerr << "Error executing file 3 " << Executable << " with errno " << WIFEXITED(status) << std::endl; - char buffer[1024]; - ssize_t bytesRead; - while ((bytesRead = read(fd[0], buffer, sizeof(buffer))) > 0) { - output += std::string(buffer, bytesRead); - } - close(fd[0]); - fd[1] = tmp_fd; - } - else - { - std::cerr << "Error executing file 2 " << Executable << " with errno " << WIFEXITED(status) << std::endl; - throw INTERNAL_SERVER_ERROR_CODE; + if (WIFEXITED(status) && WEXITSTATUS(status) == 0) + { + // Read the output from the file descriptor + char buffer[1024]; + ssize_t bytesRead; + while ((bytesRead = read(fd[0], buffer, sizeof(buffer))) > 0) { + output += std::string(buffer, bytesRead); } - // } - return output; + close(fd[0]); + fd[1] = tmp_fd; + } + else + { + throw INTERNAL_SERVER_ERROR_CODE; + } + return output; } void CGI::setCGIMapExtensions(std::string const &cgi_extension) diff --git a/www/web4/cgi/test.cgi b/www/web4/cgi/test.cgi index 29f5a9843928fb34a4a47af028ecb45b449bf7a4..be7201cca025640cc65bebc5e996f30b06130772 100755 GIT binary patch delta 4281 zcmZu!eNa@_6~Av;7IqQYWihZIur7W#?gH`^<>R4jd`^;f4aQJgL^K#tgQ!6KLNscG zQl_h?Hl(8+^^eqOo05@4b6vzx3dM6ii7Br^<{>l#D%?e{l9&Hf~c%#%%}x=CvK2D7>Q+AyM*L zx0;2!6&6cA+n`B!Gh^U2>As`NBit&Ft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t+t*chp@ z^yI?6NQreq4m;|*}v_(7?01VkQ%{1E~g zEdfu|MA#?{)@)@b;4S#6{~l%)8qXl*BPC z;$EgpY2X^F9*axrT%rg;f8NklhlSKaDPz2I@pi)Sy)}M!0gmj!6^dJX1+X=xtcx^A zt|_j50i$W;g)@GQ`2TaoouJ5V4(!!M>oE=?F)g~CyD%z=KelEI8vMdhD&XoGf8g8t z9RK&)ypL2o2B{vVIwy&qVfekpD^D3QY1g=1JHjBd?rjKc*foA^!|yDRve?GgZw@mS zgB^7dS?t@46^80+-C~=DZG4J*;BsBWBR2CfNe++(n~ zdxz?4WtXwHaZmrbzSb?adMnOnX!F9!hQ%JS3+L#fkM>d?%r;cX;`QR! z=25n=3fQ4UoRwt?xk!~TsH}4yEK{f<9fXV65?{#4ad+cZ#^T_v_CkBpVyAd5aTsh` w=M+irfs0KIm`-" << "Argumento de entrada N." << i << "= " << ac[i] << "\n"; + cout << "" << "Argumento de entrada NĀŗ" << i << "= " << ac[i] << "\n"; + } + for (int i = 0; i < 24; i++) + { + cout << "" << ENV[i] << ""; + + // attempt to retrieve value of environment variable + char *value = getenv(ENV[i].c_str()); + if (value != 0) + { + cout << value; + } + else + { + cout << "Environment variable does not exist."; + } + cout << "\n"; } cout << "<\n"; From 465b35997bd6dd43e73a6a3cb70362a5ee35f03e Mon Sep 17 00:00:00 2001 From: "ernesto.avedillo" Date: Sat, 29 Jun 2024 16:45:19 +0200 Subject: [PATCH 15/31] implement environment variables --- mandatory/inc/CGI.hpp | 5 ++-- mandatory/inc/Environment.hpp | 31 +++++++++++++++++++++++ mandatory/src/CGI.cpp | 2 +- mandatory/src/Environment.cpp | 47 +++++++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 mandatory/inc/Environment.hpp create mode 100644 mandatory/src/Environment.cpp diff --git a/mandatory/inc/CGI.hpp b/mandatory/inc/CGI.hpp index db7d92c..539bae8 100644 --- a/mandatory/inc/CGI.hpp +++ b/mandatory/inc/CGI.hpp @@ -12,8 +12,9 @@ #include "StatusCodesDefinition.hpp" #include #include "ExtendedString.hpp" +#include "Environment.hpp" -class CGI +class CGI : public Environment { private: std::string CGIFolder; @@ -39,7 +40,6 @@ class CGI //bool setIsCGI(const std::string &str); void setArgs(const std::string &); void setCGIMapExtensions(std::string const &cgi_extension); - void setEnv(char **); //getters std::string getFileName(); std::string getCGIFolder(); @@ -47,7 +47,6 @@ class CGI std::string getCGIExtension(const std::string &); std::string getFileExtension(); std::map::iterator findCGIExtension(const std::string &); - char **getEnv(); //------ OTHER Methods ------// std::string execute(); CGI *clone(); diff --git a/mandatory/inc/Environment.hpp b/mandatory/inc/Environment.hpp new file mode 100644 index 0000000..f90079b --- /dev/null +++ b/mandatory/inc/Environment.hpp @@ -0,0 +1,31 @@ +#pragma once +#include +#include +#include +#include +#include "ExtendedString.hpp" + +#define KEYS_DEFINED "COMSPEC,DOCUMENT_ROOT,GATEWAY_INTERFACE,\\ + HTTP_ACCEPT,HTTP_ACCEPT_ENCODING,\\ + HTTP_ACCEPT_LANGUAGE,HTTP_CONNECTION,HTTP_HOST,\\ + HTTP_USER_AGENT,PATH,QUERY_STRING,REMOTE_ADDR,\\ + REMOTE_PORT,REQUEST_METHOD,REQUEST_URI,\\ + SCRIPT_FILENAME,SCRIPT_NAME,SERVER_ADDR,\\ + SERVER_ADMIN,SERVER_NAME,SERVER_PORT,\\ + SERVER_PROTOCOL,SERVER_SIGNATURE,SERVER_SOFTWARE" + +class Environment +{ + private: + std::map _env; + std::vector _envKeys; + void DefineKeys(); + public: + Environment(); + ~Environment(); + void setEnv(const std::string $key, const std::string $value); + char **getEnv(); + std::string getEnv(std::string key); + std::vector getEnvKeys(); + +}; diff --git a/mandatory/src/CGI.cpp b/mandatory/src/CGI.cpp index 0e1b45b..686262a 100644 --- a/mandatory/src/CGI.cpp +++ b/mandatory/src/CGI.cpp @@ -144,7 +144,7 @@ std::string CGI::execute() close(fd[0]); // Convert the arguments vector to a null-terminated array // Execute the file with its parameters - if (execve(Executable.c_str(), ExecArray.data(), NULL) == -1) + if (execve(Executable.c_str(), ExecArray.data(), this->getEnv()) == -1) { // Handle error executing file exit(errno); diff --git a/mandatory/src/Environment.cpp b/mandatory/src/Environment.cpp new file mode 100644 index 0000000..52e7156 --- /dev/null +++ b/mandatory/src/Environment.cpp @@ -0,0 +1,47 @@ +#include "../inc/Environment.hpp" + +Environment::Environment() +{ + this->DefineKeys(); +} + +Environment::~Environment() +{ +} + +void Environment::DefineKeys() +{ + ExtendedString keys(KEYS_DEFINED); + std::vector _envKeys = keys.splitString(','); +} + +void Environment::setEnv(const std::string key, const std::string value) +{ + this->_env[key] = value; +} + +char **Environment::getEnv() +{ + std::vector RetornoEnv; + int i = 0; + std::map::iterator itb = this->_env.begin(); + std::map::iterator ite = this->_env.end(); + while (itb != ite) + { + std::string envVar = itb->first + "=" + itb->second; + RetornoEnv.push_back(const_cast(envVar.c_str())); + ++itb; + } + RetornoEnv.push_back(NULL); + return RetornoEnv.data(); +} + +std::string Environment::getEnv(std::string key) +{ + return this->_env[key]; +} + +std::vector Environment::getEnvKeys() +{ + return this->_envKeys; +} \ No newline at end of file From 1e4cad56cbebd16cc4988161c5ba7d2938591e2d Mon Sep 17 00:00:00 2001 From: "ernesto.avedillo" Date: Sat, 29 Jun 2024 17:02:04 +0200 Subject: [PATCH 16/31] Define Keys removed --- mandatory/inc/Environment.hpp | 5 +++-- mandatory/src/Environment.cpp | 7 ------- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/mandatory/inc/Environment.hpp b/mandatory/inc/Environment.hpp index f90079b..1c237f5 100644 --- a/mandatory/inc/Environment.hpp +++ b/mandatory/inc/Environment.hpp @@ -5,7 +5,9 @@ #include #include "ExtendedString.hpp" -#define KEYS_DEFINED "COMSPEC,DOCUMENT_ROOT,GATEWAY_INTERFACE,\\ +#define HOST_KEY "HOST" +#define POR_ADDRESS_KEY "HOST" + #define OT "GATEWAY_INTERFACE,\\ HTTP_ACCEPT,HTTP_ACCEPT_ENCODING,\\ HTTP_ACCEPT_LANGUAGE,HTTP_CONNECTION,HTTP_HOST,\\ HTTP_USER_AGENT,PATH,QUERY_STRING,REMOTE_ADDR,\\ @@ -19,7 +21,6 @@ class Environment private: std::map _env; std::vector _envKeys; - void DefineKeys(); public: Environment(); ~Environment(); diff --git a/mandatory/src/Environment.cpp b/mandatory/src/Environment.cpp index 52e7156..5079bc4 100644 --- a/mandatory/src/Environment.cpp +++ b/mandatory/src/Environment.cpp @@ -2,19 +2,12 @@ Environment::Environment() { - this->DefineKeys(); } Environment::~Environment() { } -void Environment::DefineKeys() -{ - ExtendedString keys(KEYS_DEFINED); - std::vector _envKeys = keys.splitString(','); -} - void Environment::setEnv(const std::string key, const std::string value) { this->_env[key] = value; From cd9907674d5f570ab66a16133e86ae7e3f3fd23e Mon Sep 17 00:00:00 2001 From: Joseph Cheel nakashima Date: Sat, 29 Jun 2024 17:02:42 +0200 Subject: [PATCH 17/31] CGI environment variables --- mandatory/inc/ListeningSocket.hpp | 35 +++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/mandatory/inc/ListeningSocket.hpp b/mandatory/inc/ListeningSocket.hpp index 29a0d55..ec000b8 100644 --- a/mandatory/inc/ListeningSocket.hpp +++ b/mandatory/inc/ListeningSocket.hpp @@ -29,6 +29,41 @@ #include "Header.hpp" #include "LocationParser.hpp" +// SCRIPT_FILENAME +// QUERY_STRING +// REQUEST_METHOD +// CONTENT_TYPE + +// CONTENT_LENGTH + +// GATEWAY_INTERFACE CGI/1.1 +// SERVER_SOFTWARE webservcheelave + +// REMOTE_ADDR 127.0.0.1 +// REMOTE_PORT 8080 + +// SERVER_ADDR localhost +// SERVER_PORT 8080 +// The name of the authentication scheme used to protect the servlet. For example, BASIC, SSL, or null if the servlet was not protected. + +// CONTENT_LENGTH + +// The length of the request body in bytes made available by the input stream or -1 if the length is not known. For HTTP servlets, the value returned is the same as the value of the CGI variable CONTENT_LENGTH. + +// CONTENT_TYPE + +// The MIME type of the body of the request, or null if the type is not known. For HTTP servlets, the value returned is the same as the value of the CGI variable CONTENT_TYPE. + +// SERVER_NAME webserver +// HTTP_HOST +// HTTP_USER_AGENT +// HTTP_ACCEPT +// HTTP_ACCEPT_LANGUAGE +// HTTP_ACCEPT_ENCODING +// HTTP_COOKIE +// HTTP_CONNECTION +// HTTP_UPGRADE_INSECURE_REQUESTS + #define MAX_CONNECTIONS 10 From b4aab3f39222a197765a6a518038f589fe2cb2df Mon Sep 17 00:00:00 2001 From: "ernesto.avedillo" Date: Sat, 29 Jun 2024 17:17:25 +0200 Subject: [PATCH 18/31] remove vector keys --- mandatory/inc/CGI.hpp | 1 - mandatory/inc/Environment.hpp | 51 ++++++++++++++++++++--------------- mandatory/src/Environment.cpp | 10 +++---- 3 files changed, 33 insertions(+), 29 deletions(-) diff --git a/mandatory/inc/CGI.hpp b/mandatory/inc/CGI.hpp index 539bae8..493ecbc 100644 --- a/mandatory/inc/CGI.hpp +++ b/mandatory/inc/CGI.hpp @@ -20,7 +20,6 @@ class CGI : public Environment std::string CGIFolder; std::string fileName; std::string fileArgs; - char **env; bool isCGI; std::vector args; std::map CGIExtensions; diff --git a/mandatory/inc/Environment.hpp b/mandatory/inc/Environment.hpp index 1c237f5..eadca78 100644 --- a/mandatory/inc/Environment.hpp +++ b/mandatory/inc/Environment.hpp @@ -5,28 +5,37 @@ #include #include "ExtendedString.hpp" -#define HOST_KEY "HOST" -#define POR_ADDRESS_KEY "HOST" - #define OT "GATEWAY_INTERFACE,\\ - HTTP_ACCEPT,HTTP_ACCEPT_ENCODING,\\ - HTTP_ACCEPT_LANGUAGE,HTTP_CONNECTION,HTTP_HOST,\\ - HTTP_USER_AGENT,PATH,QUERY_STRING,REMOTE_ADDR,\\ - REMOTE_PORT,REQUEST_METHOD,REQUEST_URI,\\ - SCRIPT_FILENAME,SCRIPT_NAME,SERVER_ADDR,\\ - SERVER_ADMIN,SERVER_NAME,SERVER_PORT,\\ - SERVER_PROTOCOL,SERVER_SIGNATURE,SERVER_SOFTWARE" +#define SCRIPT_FILENAME_KEY "SCRIPT_FILENAME" +#define QUERY_STRING_KEY "QUERY_STRING" +#define REQUEST_METHOD_KEY "REQUEST_METHOD" +#define CONTENT_TYPE_KEY "CONTENT_TYPE" +#define CONTENT_LENGTH_KEY "CONTENT_LENGTH" +#define GATEWAY_INTERFACE_KEY "GATEWAY_INTERFACE" +#define SERVER_SOFTWARE_KEY "SERVER_SOFTWARE" +#define REMOTE_ADDR_KEY "REMOTE_ADDR" +#define REMOTE_PORT_KEY "REMOTE_PORT" +#define SERVER_ADDR_KEY "SERVER_ADDR" +#define SERVER_PORT_KEY "SERVER_PORT" +#define CONTENT_LENGTH_KEY "CONTENT_LENGTH" +#define CONTENT_TYPE_KEY "CONTENT_TYPE" +#define SERVER_NAME_KEY "SERVER_NAME" +#define HTTP_HOST_KEY "HTTP_HOST" +#define HTTP_USER_AGENT_KEY "HTTP_USER_AGENT" +#define HTTP_ACCEPT_KEY "HTTP_ACCEPT" +#define HTTP_ACCEPT_LANGUAGE_KEY "HTTP_ACCEPT_LANGUAGE" +#define HTTP_ACCEPT_ENCODING_KEY "HTTP_ACCEPT_ENCODING" +#define HTTP_COOKIE_KEY "HTTP_COOKIE" +#define HTTP_CONNECTION_KEY "HTTP_CONNECTION" +#define HTTP_UPGRADE_INSECURE_REQUESTS_KEY "HTTP_UPGRADE_INSECURE_REQUESTS" class Environment { - private: - std::map _env; - std::vector _envKeys; - public: - Environment(); - ~Environment(); - void setEnv(const std::string $key, const std::string $value); - char **getEnv(); - std::string getEnv(std::string key); - std::vector getEnvKeys(); - + private: + std::map _env; + public: + Environment(); + ~Environment(); + void setEnv(const std::string $key, const std::string $value); + char **getEnv(); + std::string getEnv(std::string key); }; diff --git a/mandatory/src/Environment.cpp b/mandatory/src/Environment.cpp index 5079bc4..6236edf 100644 --- a/mandatory/src/Environment.cpp +++ b/mandatory/src/Environment.cpp @@ -16,7 +16,7 @@ void Environment::setEnv(const std::string key, const std::string value) char **Environment::getEnv() { std::vector RetornoEnv; - int i = 0; + char **env; std::map::iterator itb = this->_env.begin(); std::map::iterator ite = this->_env.end(); while (itb != ite) @@ -26,15 +26,11 @@ char **Environment::getEnv() ++itb; } RetornoEnv.push_back(NULL); - return RetornoEnv.data(); + env = RetornoEnv.data(); + return env; } std::string Environment::getEnv(std::string key) { return this->_env[key]; } - -std::vector Environment::getEnvKeys() -{ - return this->_envKeys; -} \ No newline at end of file From d61826262bfd2c8beee91fd6b5b248ae1511deca Mon Sep 17 00:00:00 2001 From: Joseph Cheel nakashima Date: Sat, 29 Jun 2024 17:19:44 +0200 Subject: [PATCH 19/31] CGI constructor with environment --- mandatory/inc/CGI.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mandatory/inc/CGI.hpp b/mandatory/inc/CGI.hpp index 539bae8..ba1f409 100644 --- a/mandatory/inc/CGI.hpp +++ b/mandatory/inc/CGI.hpp @@ -20,7 +20,7 @@ class CGI : public Environment std::string CGIFolder; std::string fileName; std::string fileArgs; - char **env; + // char **env; bool isCGI; std::vector args; std::map CGIExtensions; From 3f835fe2ce4922e7a37ef1a9758fac5eb34c4416 Mon Sep 17 00:00:00 2001 From: Joseph Cheel nakashima Date: Sat, 29 Jun 2024 17:30:20 +0200 Subject: [PATCH 20/31] setting some environment variables --- mandatory/inc/Environment.hpp | 1 + mandatory/inc/FileContent.hpp | 3 ++- mandatory/inc/ListeningSocket.hpp | 3 ++- mandatory/src/CGI.cpp | 6 +++--- mandatory/src/ListeningSocket.cpp | 21 ++++++++++++++++++++- mandatory/src/Server.cpp | 1 + 6 files changed, 29 insertions(+), 6 deletions(-) diff --git a/mandatory/inc/Environment.hpp b/mandatory/inc/Environment.hpp index eadca78..d17f3d9 100644 --- a/mandatory/inc/Environment.hpp +++ b/mandatory/inc/Environment.hpp @@ -27,6 +27,7 @@ #define HTTP_COOKIE_KEY "HTTP_COOKIE" #define HTTP_CONNECTION_KEY "HTTP_CONNECTION" #define HTTP_UPGRADE_INSECURE_REQUESTS_KEY "HTTP_UPGRADE_INSECURE_REQUESTS" +#define PATH_INFO_KEY "PATH_INFO" class Environment { diff --git a/mandatory/inc/FileContent.hpp b/mandatory/inc/FileContent.hpp index ab1cda7..c55806b 100644 --- a/mandatory/inc/FileContent.hpp +++ b/mandatory/inc/FileContent.hpp @@ -20,6 +20,8 @@ //#define MAX_SENT_BYTES 8192 class FileContent : public StatusCode { + protected: + CGI *cgiModule; private: std::string fileName; std::string indexName; @@ -35,7 +37,6 @@ class FileContent : public StatusCode long long startRange; long long currentSendingPosition; long long lastSendingPosition; - CGI *cgiModule; ListDir *listDir; //Server *server; size_t completeContentSize; diff --git a/mandatory/inc/ListeningSocket.hpp b/mandatory/inc/ListeningSocket.hpp index ec000b8..08ba1c5 100644 --- a/mandatory/inc/ListeningSocket.hpp +++ b/mandatory/inc/ListeningSocket.hpp @@ -28,7 +28,7 @@ #include "FileContent.hpp" #include "Header.hpp" #include "LocationParser.hpp" - +#include "Environment.hpp" // SCRIPT_FILENAME // QUERY_STRING // REQUEST_METHOD @@ -99,4 +99,5 @@ class ListeningSocket : public FileContent std::string getAnswerToSend(); bool receive(); void matchServerName(std::vector servers); + void setCgiEnviroment(); }; diff --git a/mandatory/src/CGI.cpp b/mandatory/src/CGI.cpp index 686262a..cb1b0cf 100644 --- a/mandatory/src/CGI.cpp +++ b/mandatory/src/CGI.cpp @@ -1,6 +1,6 @@ #include "../inc/CGI.hpp" -CGI::CGI() +CGI::CGI() : Environment() { this->fileName = ""; this->fileArgs = ""; @@ -9,7 +9,7 @@ CGI::CGI() this->setCGIMapExtensions("cgi"); } -CGI::CGI(const std::string &folder, const std::string &extensions) +CGI::CGI(const std::string &folder, const std::string &extensions) : Environment() { this->fileName = ""; this->isCGI = false; @@ -17,7 +17,7 @@ CGI::CGI(const std::string &folder, const std::string &extensions) this->setCGIMapExtensions(extensions); } -CGI::CGI(const CGI &src) +CGI::CGI(const CGI &src) : Environment() { this->fileName = ""; this->isCGI = false; diff --git a/mandatory/src/ListeningSocket.cpp b/mandatory/src/ListeningSocket.cpp index 817d9ee..89de875 100644 --- a/mandatory/src/ListeningSocket.cpp +++ b/mandatory/src/ListeningSocket.cpp @@ -158,11 +158,29 @@ void ListeningSocket::matchServerName(std::vector 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 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(); @@ -174,6 +192,7 @@ void ListeningSocket::loadRequest(std::vector servers) this->response = Parser.getResponse(); return ; } + this->cgiModule->setEnv(SCRIPT_FILENAME_KEY, this->request.getPath()); Parser.setCookies(); this->request = Parser.getRequest(); this->response = Parser.getResponse(); diff --git a/mandatory/src/Server.cpp b/mandatory/src/Server.cpp index e528e3b..0a31276 100644 --- a/mandatory/src/Server.cpp +++ b/mandatory/src/Server.cpp @@ -32,6 +32,7 @@ std::map ServerSetters() Server::Server(std::string &str) { this->cgiModule = new CGI(); + this->cgiModule->setEnv("SERVER_HOST", this->getHost()); while (str.find("location") != std::string::npos) { std::string aux = str.substr(str.find("location:{")); From 818e0f3491fbefc1628996dfe706ea35fddfe560 Mon Sep 17 00:00:00 2001 From: "ernesto.avedillo" Date: Sat, 29 Jun 2024 17:33:21 +0200 Subject: [PATCH 21/31] add in test ENV var --- www/web4/cgi/test.cgi | Bin 17400 -> 17504 bytes www/web4/cgi/test.cpp | 18 +++++++++--------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/www/web4/cgi/test.cgi b/www/web4/cgi/test.cgi index be7201cca025640cc65bebc5e996f30b06130772..6df911ee55bf66e061131f20ce36327ba4d60b5b 100755 GIT binary patch delta 4655 zcmZ8l4Om;%89p}&ftwa?QfTwTPXpN6pGlyl1-4Rd8Uh#F0uAX3`dIMUR%z#E?PI6) zv5VNEM8pNo>e#e>a88HoY?;)FI&Ra|Rov`M-JVIE+i>c3I<=y$m3GeT`|i2Hn8S0= zeZTK}-|zh8oa8WbpD@)UL>%+nxiZ1RYeG8K^e=nD(mF?TlGZu>ZG$LTzJmvKpc zHju3F2=L6+1ccc1cP2eywA--#dGKIc51siyT_!t?rJ3k{Q;twXH;#mpMOJF| zSem0sG+@7SNmQ1hEocop45a|x*t?ijMsv29b-$I0=GR6aHA{w&fuzA-Iy?eAW_Yj& z@OL95$4PL*)4X^Oj5wX_0Us8D7|1PpycF!+uI(aDMwjk8*8dR9gOT*j@{L5%W2pbr z(QiXTW-{p3owqF=$RN=8W4e9o%#Q#W$WhuLE_dP#CrVF+4JRTR6m;5u+AbSogCEVu*r_#4~S_zLY0mycs z-8-(idk>&XHMwQNK8C#KFsHH%GFaDvxNq+is2r$TrKt`eR&#)U90}|={iDsEXGMa6 z6A-I~NGL_Z15}u24`m?XRmiXO9zd*?BB2=xZL_UHqdR${6VNC@$s=5XSgl4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MNtd0#XZgyPSqdflDbx=r&ii za4#JKuBn)FO?2EA`%Pr^*n=w;IyzbjVX~w3hMGn>woTmT88U&?Vkk{|C7|D?b1L diff --git a/www/web4/cgi/test.cpp b/www/web4/cgi/test.cpp index f47ee7e..7211917 100644 --- a/www/web4/cgi/test.cpp +++ b/www/web4/cgi/test.cpp @@ -3,15 +3,15 @@ using namespace std; const string ENV[24] = { - "COMSPEC", "DOCUMENT_ROOT", "GATEWAY_INTERFACE", - "HTTP_ACCEPT", "HTTP_ACCEPT_ENCODING", - "HTTP_ACCEPT_LANGUAGE", "HTTP_CONNECTION", - "HTTP_HOST", "HTTP_USER_AGENT", "PATH", - "QUERY_STRING", "REMOTE_ADDR", "REMOTE_PORT", - "REQUEST_METHOD", "REQUEST_URI", "SCRIPT_FILENAME", - "SCRIPT_NAME", "SERVER_ADDR", "SERVER_ADMIN", - "SERVER_NAME", "SERVER_PORT", "SERVER_PROTOCOL", - "SERVER_SIGNATURE", "SERVER_SOFTWARE"}; + "SCRIPT_FILENAME", "QUERY_STRING", "REQUEST_METHOD", + "CONTENT_TYPE", "CONTENT_LENGTH", + "GATEWAY_INTERFACE", "SERVER_SOFTWARE", + "REMOTE_ADDR", "REMOTE_PORT", "SERVER_ADDR", + "SERVER_PORT", "CONTENT_LENGTH", "CONTENT_TYPE", + "SERVER_NAME", "HTTP_HOST", "HTTP_USER_AGENT", + "HTTP_ACCEPT", "HTTP_ACCEPT_LANGUAGE", "HTTP_ACCEPT_ENCODING", + "HTTP_COOKIE", "HTTP_CONNECTION", "HTTP_UPGRADE_INSECURE_REQUESTS", + "PATH_INFO"}; int main(int av, char **ac) { From 5aac907c65bff5d60db2d54eddf62fd2dffbb820 Mon Sep 17 00:00:00 2001 From: Joseph Cheel nakashima Date: Sat, 29 Jun 2024 18:50:42 +0200 Subject: [PATCH 22/31] Location adding set-sessionn-id --- mandatory/inc/Location.hpp | 24 ++++++++++++++++++------ mandatory/src/Location.cpp | 20 ++++++++++++++++---- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/mandatory/inc/Location.hpp b/mandatory/inc/Location.hpp index 9e1bab9..fbdeb54 100644 --- a/mandatory/inc/Location.hpp +++ b/mandatory/inc/Location.hpp @@ -18,6 +18,7 @@ #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:{" @@ -34,22 +35,31 @@ class Location std::string root; std::string return_; std::string index; + std::string alias; + enum LocationType LocationType; + std::string allowMethodsStr; std::vector allowMethods; - std::string autoindexStr; - bool autoindex; - std::string alias; + std::string cgiExtensionStr; std::vector cgiExtension; + + bool isCookie; std::string cookiesStr; std::vector cookies; - enum LocationType LocationType; - std::string cgiEnabledStr; + bool isGetAllowed; bool isPostAllowed; bool isDeleteAllowed; + + bool autoindex; + std::string autoindexStr; + bool isCgi; - bool isCookie; + std::string cgiEnabledStr; + + bool isSessionId; + std::string sessionIdStr; int loadData(const std::string &data); public: @@ -79,6 +89,7 @@ class Location enum LocationType getLocationType(); bool getIsCgi() const; bool getIsCookie() const; + bool getIsSessionId() const; std::vector getCookies() const; // Setter methods @@ -96,6 +107,7 @@ class Location 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); diff --git a/mandatory/src/Location.cpp b/mandatory/src/Location.cpp index 3ce1d2c..eea1308 100644 --- a/mandatory/src/Location.cpp +++ b/mandatory/src/Location.cpp @@ -25,6 +25,7 @@ std::map var_names_location() 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; } @@ -42,6 +43,7 @@ std::map getLocationMethods 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; } @@ -58,6 +60,8 @@ Location::Location() // cgiPathStr = ""; cgiExtensionStr = ""; isCgi = false; + isSessionId = false; + isCookie = false; } Location::Location(std::string const &content) { @@ -104,10 +108,10 @@ 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::setCgiEnabledStr(const std::string &cgiEnabled) -{ +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() { @@ -231,7 +235,6 @@ 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; } @@ -312,7 +315,16 @@ void Location::checkVariables(bool serverAutoIndex) } else if (this->isCookie && !this->cookiesStr.empty()) this->setCookies(this->cookiesStr); - + +// if (this->isSessionId && this->sessionIdStr.empty()) +// { +// printLog("WARNING", "set-sesion-id\t\t\tnot defined."); +// this->isSessionId = false; +// } +// else if (this->isSessionId && !this->sessionIdStr.empty()) +// { + +// } } std::vector Location::getCgiExtension() From b2a0561f65ed7e9d60a35e2eed1b4a78f741e388 Mon Sep 17 00:00:00 2001 From: "ernesto.avedillo" Date: Sat, 29 Jun 2024 18:53:50 +0200 Subject: [PATCH 23/31] add variable environment --- mandatory/inc/Environment.hpp | 3 +-- mandatory/src/CGI.cpp | 15 ++++++++++++++- mandatory/src/Environment.cpp | 35 +++++++++++++++++++++++++--------- www/web4/cgi/test.cgi | Bin 17504 -> 16384 bytes www/web4/cgi/test.cpp | 35 +++++++--------------------------- 5 files changed, 48 insertions(+), 40 deletions(-) diff --git a/mandatory/inc/Environment.hpp b/mandatory/inc/Environment.hpp index d17f3d9..e34bf48 100644 --- a/mandatory/inc/Environment.hpp +++ b/mandatory/inc/Environment.hpp @@ -27,7 +27,6 @@ #define HTTP_COOKIE_KEY "HTTP_COOKIE" #define HTTP_CONNECTION_KEY "HTTP_CONNECTION" #define HTTP_UPGRADE_INSECURE_REQUESTS_KEY "HTTP_UPGRADE_INSECURE_REQUESTS" -#define PATH_INFO_KEY "PATH_INFO" class Environment { @@ -37,6 +36,6 @@ class Environment Environment(); ~Environment(); void setEnv(const std::string $key, const std::string $value); - char **getEnv(); + std::vectorgetEnv(); std::string getEnv(std::string key); }; diff --git a/mandatory/src/CGI.cpp b/mandatory/src/CGI.cpp index cb1b0cf..f8c7541 100644 --- a/mandatory/src/CGI.cpp +++ b/mandatory/src/CGI.cpp @@ -144,9 +144,21 @@ std::string CGI::execute() close(fd[0]); // Convert the arguments vector to a null-terminated array // Execute the file with its parameters - if (execve(Executable.c_str(), ExecArray.data(), this->getEnv()) == -1) + std::vector envp = this->getEnv(); + // std::vector::iterator itb2 = envp.begin(); + // std::vector::iterator ite2 = envp.end(); + // while(itb2 != ite2) + // { + // std::cout << *itb2 << std::endl; + // itb2++; + // } + // std::cout<< "-------11------------------" << std::endl; + // std::cout<< "----------222---------------" << std::endl; + + if (execve(Executable.c_str(), ExecArray.data(), envp.data()) == -1) { // Handle error executing file + std::cerr << "Error: " << errno << std::endl; exit(errno); } } @@ -158,6 +170,7 @@ std::string CGI::execute() waitpid(pid, &status, 0); std::string output; + std::cout << "Status: " << status << std::endl; if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { // Read the output from the file descriptor diff --git a/mandatory/src/Environment.cpp b/mandatory/src/Environment.cpp index 6236edf..1706005 100644 --- a/mandatory/src/Environment.cpp +++ b/mandatory/src/Environment.cpp @@ -1,4 +1,5 @@ #include "../inc/Environment.hpp" +#include Environment::Environment() { @@ -13,20 +14,36 @@ void Environment::setEnv(const std::string key, const std::string value) this->_env[key] = value; } -char **Environment::getEnv() +std::vector Environment::getEnv() { - std::vector RetornoEnv; - char **env; + std::vector aux; + std::vector env; std::map::iterator itb = this->_env.begin(); std::map::iterator ite = this->_env.end(); - while (itb != ite) + while(itb != ite) { - std::string envVar = itb->first + "=" + itb->second; - RetornoEnv.push_back(const_cast(envVar.c_str())); - ++itb; + aux.push_back(itb->first + "=" + itb->second); + itb++; } - RetornoEnv.push_back(NULL); - env = RetornoEnv.data(); + // std::cout<< "-------------------------" << std::endl; + std::vector::iterator itb1 = aux.begin(); + std::vector::iterator ite1 = aux.end(); + while(itb1 != ite1) + { + // std::cout << itb1->c_str() << std::endl; + env.push_back(const_cast(itb1->c_str())); + itb1++; + } + env.push_back(NULL); + // std::vector::iterator itb2 = env.begin(); + // std::vector::iterator ite2 = env.end(); + // while(itb2 != ite2) + // { + // std::cout << *itb2 << std::endl; + // itb2++; + // } + + // std::cout<< "-------------------------" << std::endl; return env; } diff --git a/www/web4/cgi/test.cgi b/www/web4/cgi/test.cgi index 6df911ee55bf66e061131f20ce36327ba4d60b5b..b99ffab9bef613ae4d2fd2782884307f42c61050 100755 GIT binary patch literal 16384 zcmeHOeQX@X6`woXNq{82m;ffBWfMXbKtB5tlbYf<>)2D!CtOEikPuC>(-TX`#69&Ahi> zZ)~aJ52UI)*6y44d+#?NZ)f*ry)$1<_I5=h5y2@c+5~E6W4eUYSqQyFs)V(O6+#zR zicgCrfKuC32A0dnuL>mc>ML1Iys8E*d=hden8Iq<;!SX|D7)w77V{@iKxu4mpcq|{LxSXZ_GY?_pO%iXC zcr0VYW6ANv=Ht;q<+W7lprNj$h6*H2o`neV4ifJm`LpaKf0kGWrhRX?>5o(2ZsJum z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AGbVaeJV0s5Z#veNX{sh}kPOFC6@$zxEU^tVJZOZ#tvLme4 z4OO~>sGSn0Rab7h$R>S~D@5Brcsq+TG4uiI`P9$U4i4&cG1ZAXZ>TXGdp$B+gPa+;cBB}*bp+06^_gQYr2Gwem#l!w#B-AU5PmD2}<=kpe@}|+FKY6Yl!x)EX5!y-P?o7cHs;6#wZIV zQwbHL-)gfAi{j{>34)-tu8x%O$>GsQn{G(Qs6}ycIez#e?RuPQ4-=OyN|g59I7e`m zh8n_D8mv=R!Om!iC~-=iPCl8XN}jH;@X<($-=;E;i%+UxK1r`I^iHTkNr531-Z#wi zNgXYHS%64p}B{q21;_?sc{D(Dvucq?6l*`ZQY@&Et$imb4%)GinJK%RS1UFxyLIg=U zlMc?G=iPDzH{qjs%2UmcrswrM|BX^KipqSR$Cv4eJkQ%zD(YIZ4*5AcFlS~y&y(NL z73KFwE=Y7!qCLJxu)ND^~90Cw~&hreBIf8loTi5tKXNs zJn_`mkNp{D`3*N;&mX;dGu31KK}aXSJWdbuMQ8pz-x#?_dAR7Zt(Xh>PjrCK?a%Wh z;U$nlO(sIw?)pDVu#<0=8q$&XpVv!k`sC)nKv7Qq-cmz4?=6)=V4mp=eEk{w5h+eS ze-C)^QswbC?5L$X|2GN8W(0k^XXd}WpWWTwoIlHViA%HT44>!Q$CnaFf&G*_YkryQf*DNI@?xF8cF)8z|Aq{r6mplrDP3J$#-G#) v8hRp*+i9lp1H%lLT@v-F0uMF>p2#apUMsCif!lmW{&mXhN-n4-xlHkYiNg9^ diff --git a/www/web4/cgi/test.cpp b/www/web4/cgi/test.cpp index 7211917..00b7abd 100644 --- a/www/web4/cgi/test.cpp +++ b/www/web4/cgi/test.cpp @@ -2,18 +2,7 @@ #include using namespace std; -const string ENV[24] = { - "SCRIPT_FILENAME", "QUERY_STRING", "REQUEST_METHOD", - "CONTENT_TYPE", "CONTENT_LENGTH", - "GATEWAY_INTERFACE", "SERVER_SOFTWARE", - "REMOTE_ADDR", "REMOTE_PORT", "SERVER_ADDR", - "SERVER_PORT", "CONTENT_LENGTH", "CONTENT_TYPE", - "SERVER_NAME", "HTTP_HOST", "HTTP_USER_AGENT", - "HTTP_ACCEPT", "HTTP_ACCEPT_LANGUAGE", "HTTP_ACCEPT_ENCODING", - "HTTP_COOKIE", "HTTP_CONNECTION", "HTTP_UPGRADE_INSECURE_REQUESTS", - "PATH_INFO"}; - -int main(int av, char **ac) +int main(int av, char **ac, char **envp) { (void)av; cout << "\n"; @@ -27,25 +16,15 @@ int main(int av, char **ac) for (int i = 1; i < av; i++) { - cout << "" << "Argumento de entrada NĀŗ" << i << "= " << ac[i] << "\n"; + cout << "" << "Argumento de entrada N." << i << "= " << ac[i] << "\n"; } - for (int i = 0; i < 24; i++) + cout << "" << "Variables de entorno" << "\n"; + int i = 0; + while(envp[i] != NULL) { - cout << "" << ENV[i] << ""; - - // attempt to retrieve value of environment variable - char *value = getenv(ENV[i].c_str()); - if (value != 0) - { - cout << value; - } - else - { - cout << "Environment variable does not exist."; - } - cout << "\n"; + cout << "" << "N." << i << "= " << envp[i] << "\n"; + i++; } - cout << "<\n"; cout << "\n"; cout << "\n"; From 77f16ab339e64ea8d12f67b4615d68a0c7993d26 Mon Sep 17 00:00:00 2001 From: "ernesto.avedillo" Date: Sun, 30 Jun 2024 12:05:43 +0200 Subject: [PATCH 24/31] convert environment to pointer of chars --- mandatory/inc/Environment.hpp | 1 + mandatory/src/CGI.cpp | 13 +------------ mandatory/src/Environment.cpp | 34 ++++++++++++---------------------- 3 files changed, 14 insertions(+), 34 deletions(-) diff --git a/mandatory/inc/Environment.hpp b/mandatory/inc/Environment.hpp index e34bf48..3add8e5 100644 --- a/mandatory/inc/Environment.hpp +++ b/mandatory/inc/Environment.hpp @@ -32,6 +32,7 @@ class Environment { private: std::map _env; + std::vector _envArray; public: Environment(); ~Environment(); diff --git a/mandatory/src/CGI.cpp b/mandatory/src/CGI.cpp index f8c7541..3b7400d 100644 --- a/mandatory/src/CGI.cpp +++ b/mandatory/src/CGI.cpp @@ -87,11 +87,9 @@ std::string CGI::getCGIFolder() void CGI::setArgs(const std::string &str) { - std::cout << "Args: " << str << std::endl; std::vector vec = splitString(str, '&'); for (size_t i = 0; i < vec.size(); i++) { - std::cout << "Args: " << vec[i] << std::endl; args.push_back(vec[i]); } @@ -145,15 +143,7 @@ std::string CGI::execute() // Convert the arguments vector to a null-terminated array // Execute the file with its parameters std::vector envp = this->getEnv(); - // std::vector::iterator itb2 = envp.begin(); - // std::vector::iterator ite2 = envp.end(); - // while(itb2 != ite2) - // { - // std::cout << *itb2 << std::endl; - // itb2++; - // } - // std::cout<< "-------11------------------" << std::endl; - // std::cout<< "----------222---------------" << std::endl; + if (execve(Executable.c_str(), ExecArray.data(), envp.data()) == -1) { @@ -170,7 +160,6 @@ std::string CGI::execute() waitpid(pid, &status, 0); std::string output; - std::cout << "Status: " << status << std::endl; if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { // Read the output from the file descriptor diff --git a/mandatory/src/Environment.cpp b/mandatory/src/Environment.cpp index 1706005..28955d0 100644 --- a/mandatory/src/Environment.cpp +++ b/mandatory/src/Environment.cpp @@ -7,6 +7,13 @@ Environment::Environment() Environment::~Environment() { + std::vector::iterator itb = this->_envArray.begin(); + std::vector::iterator ite = this->_envArray.end(); + while(itb != ite) + { + delete *itb; + itb++; + } } void Environment::setEnv(const std::string key, const std::string value) @@ -16,35 +23,18 @@ void Environment::setEnv(const std::string key, const std::string value) std::vector Environment::getEnv() { - std::vector aux; - std::vector env; std::map::iterator itb = this->_env.begin(); std::map::iterator ite = this->_env.end(); while(itb != ite) { - aux.push_back(itb->first + "=" + itb->second); + std::string *tmp = new std::string(itb->first + "=" + itb->second + '\0'); + _envArray.push_back(const_cast(tmp->c_str())); itb++; } - // std::cout<< "-------------------------" << std::endl; - std::vector::iterator itb1 = aux.begin(); - std::vector::iterator ite1 = aux.end(); - while(itb1 != ite1) - { - // std::cout << itb1->c_str() << std::endl; - env.push_back(const_cast(itb1->c_str())); - itb1++; - } - env.push_back(NULL); - // std::vector::iterator itb2 = env.begin(); - // std::vector::iterator ite2 = env.end(); - // while(itb2 != ite2) - // { - // std::cout << *itb2 << std::endl; - // itb2++; - // } + _envArray.push_back(NULL); + + return _envArray; - // std::cout<< "-------------------------" << std::endl; - return env; } std::string Environment::getEnv(std::string key) From 01bd03953a435db0c2fd51d9acbc654b0f8c8303 Mon Sep 17 00:00:00 2001 From: Joseph Cheel nakashima Date: Sun, 30 Jun 2024 12:11:34 +0200 Subject: [PATCH 25/31] initialize uninitialize variables --- mandatory/src/Location.cpp | 18 +++++------------- mandatory/src/LocationParser.cpp | 1 + 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/mandatory/src/Location.cpp b/mandatory/src/Location.cpp index 3ce1d2c..5a9aea0 100644 --- a/mandatory/src/Location.cpp +++ b/mandatory/src/Location.cpp @@ -55,14 +55,18 @@ Location::Location() allowMethodsStr = ""; autoindex = ""; alias = ""; - // cgiPathStr = ""; cgiExtensionStr = ""; isCgi = false; + isCookie = false; + cookiesStr = ""; + std::cout << "INITITALIZE" << std::endl; } Location::Location(std::string const &content) { this->isCgi = false; this->cgiEnabledStr = ""; + isCookie = false; + cookiesStr = ""; this->loadData(content); } @@ -156,17 +160,6 @@ 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 ::setCgiExtension(const std::string &extensions) { @@ -231,7 +224,6 @@ 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; } diff --git a/mandatory/src/LocationParser.cpp b/mandatory/src/LocationParser.cpp index 80da4b3..5b170fe 100644 --- a/mandatory/src/LocationParser.cpp +++ b/mandatory/src/LocationParser.cpp @@ -409,6 +409,7 @@ LocationParser::LocationParser(Header request_, Server *server_, Receive *receiv this->isAutoIndex = server->getAutoIndex(); this->startRange = 0; this->endRange = 0; + this->isCookie = false; } LocationParser::~LocationParser() From 2e8088beb8ab971cb734de23466bd1b7cb2f1a15 Mon Sep 17 00:00:00 2001 From: Joseph Cheel nakashima Date: Sun, 30 Jun 2024 13:26:03 +0200 Subject: [PATCH 26/31] solved not permission delete --- mandatory/src/ListeningSocket.cpp | 5 ++++- mandatory/src/LocationParser.cpp | 9 +++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/mandatory/src/ListeningSocket.cpp b/mandatory/src/ListeningSocket.cpp index 89de875..452634b 100644 --- a/mandatory/src/ListeningSocket.cpp +++ b/mandatory/src/ListeningSocket.cpp @@ -105,8 +105,11 @@ bool ListeningSocket::sendData(int clientSocketFd) ExtendedString answer = this->getAnswerToSend(); // std::cout << "answer: " << answer.size() << std::endl; // std::cout << "My answer: " << answer << std::endl; - if ((send(clientSocketFd, answer.c_str(), answer.size(), 0)) < 0) + int ret; + if ((ret = send(clientSocketFd, answer.c_str(), answer.size(), 0)) < 0) std::cerr << "Failed to write to client" << std::endl; + else if (ret == 0) + printLog("WARNING", "send 0 bytes"); return this->getIsSendComplete(); } diff --git a/mandatory/src/LocationParser.cpp b/mandatory/src/LocationParser.cpp index 5b170fe..e982ab5 100644 --- a/mandatory/src/LocationParser.cpp +++ b/mandatory/src/LocationParser.cpp @@ -383,6 +383,15 @@ void LocationParser::checks() } else if (this->request.getMethod() == "DELETE") { + switch (isFilePermissions(this->request.getPath(), W_OK)) + { + case -5: + response.setStatus("404 Not Found"); + throw NOT_FOUND_CODE; + case -4: + response.setStatus("403 Forbidden"); + throw FORBIDDEN_CODE; + } if (std::remove(this->request.getPath().c_str()) == 0) response.setStatus("200 OK"); else From f1f65b4323a22500471b2d8a03ec164ec2ad8e0c Mon Sep 17 00:00:00 2001 From: "ernesto.avedillo" Date: Sun, 30 Jun 2024 13:39:27 +0200 Subject: [PATCH 27/31] UNKNOWN request solved --- mandatory/inc/Environment.hpp | 2 ++ mandatory/inc/FileContent.hpp | 4 ---- mandatory/src/FileContent.cpp | 1 + mandatory/src/ListeningSocket.cpp | 12 +++++------- 4 files changed, 8 insertions(+), 11 deletions(-) diff --git a/mandatory/inc/Environment.hpp b/mandatory/inc/Environment.hpp index 3add8e5..afd5719 100644 --- a/mandatory/inc/Environment.hpp +++ b/mandatory/inc/Environment.hpp @@ -27,6 +27,8 @@ #define HTTP_COOKIE_KEY "HTTP_COOKIE" #define HTTP_CONNECTION_KEY "HTTP_CONNECTION" #define HTTP_UPGRADE_INSECURE_REQUESTS_KEY "HTTP_UPGRADE_INSECURE_REQUESTS" +#define PATH_INFO_KEY "PATH_INFO" + class Environment { diff --git a/mandatory/inc/FileContent.hpp b/mandatory/inc/FileContent.hpp index c55806b..7e8f9b4 100644 --- a/mandatory/inc/FileContent.hpp +++ b/mandatory/inc/FileContent.hpp @@ -38,16 +38,12 @@ class FileContent : public StatusCode long long currentSendingPosition; long long lastSendingPosition; ListDir *listDir; - //Server *server; size_t completeContentSize; std::string splitFileFromArgs(const std::string &); bool FileOrFolerExtists(const std::string &); size_t endRange; - // bool isCgi; public: - // FileContent(Server *); FileContent(); - // FileContent(const std::string &, Server *); ~FileContent(); int openFile(); bool setFileName(const std::string &, const std::string &); diff --git a/mandatory/src/FileContent.cpp b/mandatory/src/FileContent.cpp index 493d6d1..3733ff5 100644 --- a/mandatory/src/FileContent.cpp +++ b/mandatory/src/FileContent.cpp @@ -98,6 +98,7 @@ std::string FileContent::getContent() bool FileContent::setFileName(const std::string &file_name, const std::string &fileArgs) { + std::cout << file_name << std::endl; bool fileOrFolderExists = this->FileOrFolerExtists(file_name); if (fileOrFolderExists) { diff --git a/mandatory/src/ListeningSocket.cpp b/mandatory/src/ListeningSocket.cpp index 89de875..1794287 100644 --- a/mandatory/src/ListeningSocket.cpp +++ b/mandatory/src/ListeningSocket.cpp @@ -163,9 +163,9 @@ 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(PATH_INFO_KEY, this->request.getPath()); + // // this->cgiModule->setEnv(PROT, this->request.getProtocol()); + this->cgiModule->setEnv(SERVER_PORT_KEY, toString(this->port)); 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")); @@ -192,15 +192,13 @@ void ListeningSocket::loadRequest(std::vector servers) this->response = Parser.getResponse(); return ; } - this->cgiModule->setEnv(SCRIPT_FILENAME_KEY, this->request.getPath()); Parser.setCookies(); this->request = Parser.getRequest(); + this->cgiModule->setEnv(PATH_INFO_KEY, this->request.getPath().substr(0, this->request.getPath().rfind("/"))); + this->cgiModule->setEnv(SCRIPT_FILENAME_KEY, this->request.getPath()); 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()); } From 4ff20e26f931b323c1791e6111ab011af740754a Mon Sep 17 00:00:00 2001 From: eavedill Date: Sun, 30 Jun 2024 13:53:08 +0200 Subject: [PATCH 28/31] add header --- mandatory/inc/CGI.hpp | 12 ++++++++++++ mandatory/inc/Environment.hpp | 12 ++++++++++++ mandatory/inc/ExtendedString.hpp | 12 ++++++++++++ mandatory/inc/FileContent.hpp | 12 ++++++++++++ mandatory/inc/Header.hpp | 12 ++++++++++++ mandatory/inc/ListDir.hpp | 4 ++-- mandatory/inc/ListeningSocket.hpp | 12 ++++++++++++ mandatory/inc/Location.hpp | 12 ++++++++++++ mandatory/inc/LocationParser.hpp | 12 ++++++++++++ mandatory/inc/Parser.hpp | 12 ++++++++++++ mandatory/inc/Receive.hpp | 12 ++++++++++++ mandatory/inc/Server.hpp | 12 ++++++++++++ mandatory/inc/StatusCode.hpp | 12 ++++++++++++ mandatory/inc/StatusCodesDefinition.hpp | 12 ++++++++++++ mandatory/inc/utils.hpp | 12 ++++++++++++ mandatory/src/CGI.cpp | 12 ++++++++++++ mandatory/src/Environment.cpp | 12 ++++++++++++ mandatory/src/ExtendedString.cpp | 12 ++++++++++++ mandatory/src/FileContent.cpp | 12 ++++++++++++ mandatory/src/Header.cpp | 12 ++++++++++++ mandatory/src/ListDir.cpp | 12 ++++++++++++ mandatory/src/ListeningSocket.cpp | 12 ++++++++++++ mandatory/src/Location.cpp | 4 ++-- mandatory/src/LocationParser.cpp | 12 ++++++++++++ mandatory/src/Parser.cpp | 12 ++++++++++++ mandatory/src/Recieve.cpp | 12 ++++++++++++ mandatory/src/Server.cpp | 12 ++++++++++++ mandatory/src/ServerGetters.cpp | 12 ++++++++++++ mandatory/src/ServerSetters.cpp | 12 ++++++++++++ mandatory/src/StatusCode.cpp | 12 ++++++++++++ mandatory/src/WebServer.cpp | 12 ++++++++++++ mandatory/src/WebServerLNX.cpp | 12 ++++++++++++ mandatory/src/WebServerMAC.cpp | 12 ++++++++++++ mandatory/src/WebservParser.cpp | 12 ++++++++++++ mandatory/src/main.cpp | 12 ++++++++++++ mandatory/src/utils.cpp | 12 ++++++++++++ 36 files changed, 412 insertions(+), 4 deletions(-) diff --git a/mandatory/inc/CGI.hpp b/mandatory/inc/CGI.hpp index 493ecbc..48bba02 100644 --- a/mandatory/inc/CGI.hpp +++ b/mandatory/inc/CGI.hpp @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* CGI.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: eavedill +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/30 13:47:27 by eavedill #+# #+# */ +/* Updated: 2024/06/30 13:47:29 by eavedill ### ########.fr */ +/* */ +/* ************************************************************************** */ + #pragma once #include #include diff --git a/mandatory/inc/Environment.hpp b/mandatory/inc/Environment.hpp index afd5719..67cc31b 100644 --- a/mandatory/inc/Environment.hpp +++ b/mandatory/inc/Environment.hpp @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Environment.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: eavedill +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/30 13:47:48 by eavedill #+# #+# */ +/* Updated: 2024/06/30 13:47:52 by eavedill ### ########.fr */ +/* */ +/* ************************************************************************** */ + #pragma once #include #include diff --git a/mandatory/inc/ExtendedString.hpp b/mandatory/inc/ExtendedString.hpp index 008910e..fea10a2 100644 --- a/mandatory/inc/ExtendedString.hpp +++ b/mandatory/inc/ExtendedString.hpp @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ExtendedString.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: eavedill +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/30 13:47:57 by eavedill #+# #+# */ +/* Updated: 2024/06/30 13:47:58 by eavedill ### ########.fr */ +/* */ +/* ************************************************************************** */ + #pragma once #include #include diff --git a/mandatory/inc/FileContent.hpp b/mandatory/inc/FileContent.hpp index 7e8f9b4..05cf750 100644 --- a/mandatory/inc/FileContent.hpp +++ b/mandatory/inc/FileContent.hpp @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* FileContent.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: eavedill +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/30 13:48:24 by eavedill #+# #+# */ +/* Updated: 2024/06/30 13:48:25 by eavedill ### ########.fr */ +/* */ +/* ************************************************************************** */ + #pragma once #include #include diff --git a/mandatory/inc/Header.hpp b/mandatory/inc/Header.hpp index e9e0509..ded226b 100644 --- a/mandatory/inc/Header.hpp +++ b/mandatory/inc/Header.hpp @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Header.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: eavedill +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/30 13:48:01 by eavedill #+# #+# */ +/* Updated: 2024/06/30 13:48:02 by eavedill ### ########.fr */ +/* */ +/* ************************************************************************** */ + #pragma once #include #include diff --git a/mandatory/inc/ListDir.hpp b/mandatory/inc/ListDir.hpp index 964c3be..4e4ee6e 100644 --- a/mandatory/inc/ListDir.hpp +++ b/mandatory/inc/ListDir.hpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* ListDir.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: eavedill +#+ +:+ +#+ */ +/* By: eavedill +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/07 19:03:57 by eavedill #+# #+# */ -/* Updated: 2024/06/29 12:32:21 by eavedill ### ########.fr */ +/* Updated: 2024/06/30 13:48:17 by eavedill ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/mandatory/inc/ListeningSocket.hpp b/mandatory/inc/ListeningSocket.hpp index 08ba1c5..175ddd5 100644 --- a/mandatory/inc/ListeningSocket.hpp +++ b/mandatory/inc/ListeningSocket.hpp @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ListeningSocket.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: eavedill +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/30 13:48:33 by eavedill #+# #+# */ +/* Updated: 2024/06/30 13:48:34 by eavedill ### ########.fr */ +/* */ +/* ************************************************************************** */ + #pragma once #include #include diff --git a/mandatory/inc/Location.hpp b/mandatory/inc/Location.hpp index fbdeb54..1ad7d44 100644 --- a/mandatory/inc/Location.hpp +++ b/mandatory/inc/Location.hpp @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Location.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: eavedill +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/30 13:48:41 by eavedill #+# #+# */ +/* Updated: 2024/06/30 13:48:42 by eavedill ### ########.fr */ +/* */ +/* ************************************************************************** */ + #ifndef LOCATION_HPP #define LOCATION_HPP diff --git a/mandatory/inc/LocationParser.hpp b/mandatory/inc/LocationParser.hpp index f845ccc..e7df33a 100644 --- a/mandatory/inc/LocationParser.hpp +++ b/mandatory/inc/LocationParser.hpp @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* LocationParser.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: eavedill +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/30 13:48:49 by eavedill #+# #+# */ +/* Updated: 2024/06/30 13:48:50 by eavedill ### ########.fr */ +/* */ +/* ************************************************************************** */ + #pragma once #include #include diff --git a/mandatory/inc/Parser.hpp b/mandatory/inc/Parser.hpp index a9a1b84..bcf8a3e 100644 --- a/mandatory/inc/Parser.hpp +++ b/mandatory/inc/Parser.hpp @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Parser.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: eavedill +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/30 13:48:59 by eavedill #+# #+# */ +/* Updated: 2024/06/30 13:49:00 by eavedill ### ########.fr */ +/* */ +/* ************************************************************************** */ + #pragma once # include # include diff --git a/mandatory/inc/Receive.hpp b/mandatory/inc/Receive.hpp index 6ce5bee..fbf5385 100644 --- a/mandatory/inc/Receive.hpp +++ b/mandatory/inc/Receive.hpp @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Receive.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: eavedill +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/30 13:49:11 by eavedill #+# #+# */ +/* Updated: 2024/06/30 13:49:12 by eavedill ### ########.fr */ +/* */ +/* ************************************************************************** */ + #pragma once #include #include diff --git a/mandatory/inc/Server.hpp b/mandatory/inc/Server.hpp index 6d7b554..373a1ae 100644 --- a/mandatory/inc/Server.hpp +++ b/mandatory/inc/Server.hpp @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Server.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: eavedill +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/30 13:49:18 by eavedill #+# #+# */ +/* Updated: 2024/06/30 13:49:19 by eavedill ### ########.fr */ +/* */ +/* ************************************************************************** */ + #ifndef SERVER_HPP # define SERVER_HPP # include diff --git a/mandatory/inc/StatusCode.hpp b/mandatory/inc/StatusCode.hpp index b36f50e..2b7eb5d 100644 --- a/mandatory/inc/StatusCode.hpp +++ b/mandatory/inc/StatusCode.hpp @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* StatusCode.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: eavedill +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/30 13:49:24 by eavedill #+# #+# */ +/* Updated: 2024/06/30 13:49:24 by eavedill ### ########.fr */ +/* */ +/* ************************************************************************** */ + #pragma once #include #include diff --git a/mandatory/inc/StatusCodesDefinition.hpp b/mandatory/inc/StatusCodesDefinition.hpp index 149cbd9..fa9c517 100644 --- a/mandatory/inc/StatusCodesDefinition.hpp +++ b/mandatory/inc/StatusCodesDefinition.hpp @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* StatusCodesDefinition.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: eavedill +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/30 13:49:29 by eavedill #+# #+# */ +/* Updated: 2024/06/30 13:49:29 by eavedill ### ########.fr */ +/* */ +/* ************************************************************************** */ + #define DEFAULT_ERROR_FILE "./Templates/ErrorTemplate.html" #define CONTINUE_CODE 100 #define SWITCHING_PROTOCOLS_CODE 101 diff --git a/mandatory/inc/utils.hpp b/mandatory/inc/utils.hpp index c380178..9123d75 100644 --- a/mandatory/inc/utils.hpp +++ b/mandatory/inc/utils.hpp @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* utils.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: eavedill +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/30 13:49:35 by eavedill #+# #+# */ +/* Updated: 2024/06/30 13:49:39 by eavedill ### ########.fr */ +/* */ +/* ************************************************************************** */ + #pragma once #include #include diff --git a/mandatory/src/CGI.cpp b/mandatory/src/CGI.cpp index 3b7400d..f057228 100644 --- a/mandatory/src/CGI.cpp +++ b/mandatory/src/CGI.cpp @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* CGI.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: eavedill +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/30 13:49:55 by eavedill #+# #+# */ +/* Updated: 2024/06/30 13:49:56 by eavedill ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "../inc/CGI.hpp" CGI::CGI() : Environment() diff --git a/mandatory/src/Environment.cpp b/mandatory/src/Environment.cpp index 28955d0..ede41de 100644 --- a/mandatory/src/Environment.cpp +++ b/mandatory/src/Environment.cpp @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Environment.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: eavedill +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/30 13:50:02 by eavedill #+# #+# */ +/* Updated: 2024/06/30 13:50:03 by eavedill ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "../inc/Environment.hpp" #include diff --git a/mandatory/src/ExtendedString.cpp b/mandatory/src/ExtendedString.cpp index 48f1864..1d0d023 100644 --- a/mandatory/src/ExtendedString.cpp +++ b/mandatory/src/ExtendedString.cpp @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ExtendedString.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: eavedill +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/30 13:50:11 by eavedill #+# #+# */ +/* Updated: 2024/06/30 13:50:11 by eavedill ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "../inc/ExtendedString.hpp" ExtendedString::ExtendedString() : std::string() {} diff --git a/mandatory/src/FileContent.cpp b/mandatory/src/FileContent.cpp index 3733ff5..4b0e282 100644 --- a/mandatory/src/FileContent.cpp +++ b/mandatory/src/FileContent.cpp @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* FileContent.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: eavedill +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/30 13:50:28 by eavedill #+# #+# */ +/* Updated: 2024/06/30 13:50:29 by eavedill ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "../inc/FileContent.hpp" // FileContent::FileContent(Server *srv) : StatusCode() diff --git a/mandatory/src/Header.cpp b/mandatory/src/Header.cpp index 61dbb97..22bc4c1 100644 --- a/mandatory/src/Header.cpp +++ b/mandatory/src/Header.cpp @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Header.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: eavedill +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/30 13:52:00 by eavedill #+# #+# */ +/* Updated: 2024/06/30 13:52:00 by eavedill ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "../inc/Header.hpp" diff --git a/mandatory/src/ListDir.cpp b/mandatory/src/ListDir.cpp index 1f609c5..41e50d4 100644 --- a/mandatory/src/ListDir.cpp +++ b/mandatory/src/ListDir.cpp @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ListDir.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: eavedill +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/30 13:51:56 by eavedill #+# #+# */ +/* Updated: 2024/06/30 13:51:56 by eavedill ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "../inc/ListDir.hpp" diff --git a/mandatory/src/ListeningSocket.cpp b/mandatory/src/ListeningSocket.cpp index 5d063bf..fe0db09 100644 --- a/mandatory/src/ListeningSocket.cpp +++ b/mandatory/src/ListeningSocket.cpp @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ListeningSocket.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: eavedill +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/30 13:51:47 by eavedill #+# #+# */ +/* Updated: 2024/06/30 13:51:52 by eavedill ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "../inc/ListeningSocket.hpp" ListeningSocket::ListeningSocket(int myPort, Server *srv): FileContent() diff --git a/mandatory/src/Location.cpp b/mandatory/src/Location.cpp index d4d8446..1d8e44a 100644 --- a/mandatory/src/Location.cpp +++ b/mandatory/src/Location.cpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* Location.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jcheel-n +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/29 17:38:18 by eavedill #+# #+# */ -/* Updated: 2024/06/27 13:39:19 by jcheel-n ### ########.fr */ +/* Updated: 2024/06/30 13:51:43 by eavedill ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/mandatory/src/LocationParser.cpp b/mandatory/src/LocationParser.cpp index e982ab5..1e5e173 100644 --- a/mandatory/src/LocationParser.cpp +++ b/mandatory/src/LocationParser.cpp @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* LocationParser.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: eavedill +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/30 13:51:38 by eavedill #+# #+# */ +/* Updated: 2024/06/30 13:51:38 by eavedill ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "../inc/LocationParser.hpp" static std::string getMimeType(std::string contentType) diff --git a/mandatory/src/Parser.cpp b/mandatory/src/Parser.cpp index 1dffdcf..7f96f59 100644 --- a/mandatory/src/Parser.cpp +++ b/mandatory/src/Parser.cpp @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Parser.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: eavedill +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/30 13:51:31 by eavedill #+# #+# */ +/* Updated: 2024/06/30 13:51:31 by eavedill ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "../inc/Parser.hpp" #include diff --git a/mandatory/src/Recieve.cpp b/mandatory/src/Recieve.cpp index 6c7f646..8305453 100644 --- a/mandatory/src/Recieve.cpp +++ b/mandatory/src/Recieve.cpp @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Recieve.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: eavedill +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/30 13:51:26 by eavedill #+# #+# */ +/* Updated: 2024/06/30 13:51:26 by eavedill ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "../inc/Receive.hpp" #include #include diff --git a/mandatory/src/Server.cpp b/mandatory/src/Server.cpp index 0a31276..7878fea 100644 --- a/mandatory/src/Server.cpp +++ b/mandatory/src/Server.cpp @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Server.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: eavedill +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/30 13:51:20 by eavedill #+# #+# */ +/* Updated: 2024/06/30 13:51:20 by eavedill ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "../inc/Server.hpp" std::map var_names_server() diff --git a/mandatory/src/ServerGetters.cpp b/mandatory/src/ServerGetters.cpp index 39912e6..4041693 100644 --- a/mandatory/src/ServerGetters.cpp +++ b/mandatory/src/ServerGetters.cpp @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ServerGetters.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: eavedill +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/30 13:51:14 by eavedill #+# #+# */ +/* Updated: 2024/06/30 13:51:14 by eavedill ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "../inc/Server.hpp" diff --git a/mandatory/src/ServerSetters.cpp b/mandatory/src/ServerSetters.cpp index 51c5b0a..80c2565 100644 --- a/mandatory/src/ServerSetters.cpp +++ b/mandatory/src/ServerSetters.cpp @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ServerSetters.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: eavedill +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/30 13:51:10 by eavedill #+# #+# */ +/* Updated: 2024/06/30 13:51:11 by eavedill ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "../inc/Server.hpp" void Server::setHost(std::string const &host) { this->Host = host; } diff --git a/mandatory/src/StatusCode.cpp b/mandatory/src/StatusCode.cpp index e46be16..98b3fac 100644 --- a/mandatory/src/StatusCode.cpp +++ b/mandatory/src/StatusCode.cpp @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* StatusCode.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: eavedill +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/30 13:51:07 by eavedill #+# #+# */ +/* Updated: 2024/06/30 13:51:08 by eavedill ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "../inc/StatusCode.hpp" StatusCode::StatusCode() diff --git a/mandatory/src/WebServer.cpp b/mandatory/src/WebServer.cpp index 4a7f254..b1ddb83 100644 --- a/mandatory/src/WebServer.cpp +++ b/mandatory/src/WebServer.cpp @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* WebServer.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: eavedill +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/30 13:50:54 by eavedill #+# #+# */ +/* Updated: 2024/06/30 13:50:59 by eavedill ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "../inc/WebServer.hpp" WebServer::WebServer() diff --git a/mandatory/src/WebServerLNX.cpp b/mandatory/src/WebServerLNX.cpp index 788785d..01b544d 100644 --- a/mandatory/src/WebServerLNX.cpp +++ b/mandatory/src/WebServerLNX.cpp @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* WebServerLNX.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: eavedill +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/30 13:50:51 by eavedill #+# #+# */ +/* Updated: 2024/06/30 13:50:51 by eavedill ### ########.fr */ +/* */ +/* ************************************************************************** */ + #ifdef __linux__ #include "../inc/WebServer.hpp" diff --git a/mandatory/src/WebServerMAC.cpp b/mandatory/src/WebServerMAC.cpp index 0ff6662..7fde14d 100644 --- a/mandatory/src/WebServerMAC.cpp +++ b/mandatory/src/WebServerMAC.cpp @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* WebServerMAC.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: eavedill +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/30 13:50:44 by eavedill #+# #+# */ +/* Updated: 2024/06/30 13:50:44 by eavedill ### ########.fr */ +/* */ +/* ************************************************************************** */ + #ifdef __APPLE__ #include "../inc/WebServer.hpp" diff --git a/mandatory/src/WebservParser.cpp b/mandatory/src/WebservParser.cpp index ff18163..8095250 100644 --- a/mandatory/src/WebservParser.cpp +++ b/mandatory/src/WebservParser.cpp @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* WebservParser.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: eavedill +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/30 13:50:32 by eavedill #+# #+# */ +/* Updated: 2024/06/30 13:50:39 by eavedill ### ########.fr */ +/* */ +/* ************************************************************************** */ + # include "../inc/WebServer.hpp" std::string removeComments(std::string &str) diff --git a/mandatory/src/main.cpp b/mandatory/src/main.cpp index f03a3af..5e71001 100644 --- a/mandatory/src/main.cpp +++ b/mandatory/src/main.cpp @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: eavedill +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/30 13:51:34 by eavedill #+# #+# */ +/* Updated: 2024/06/30 13:51:34 by eavedill ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "../inc/WebServer.hpp" int main(int av, char **ac) diff --git a/mandatory/src/utils.cpp b/mandatory/src/utils.cpp index 9c2eea7..e5acde7 100644 --- a/mandatory/src/utils.cpp +++ b/mandatory/src/utils.cpp @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* utils.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: eavedill +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/30 13:51:03 by eavedill #+# #+# */ +/* Updated: 2024/06/30 13:51:03 by eavedill ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "../inc/utils.hpp" template From aa43510152182d3988d8d02c427b4b4ba078ed7a Mon Sep 17 00:00:00 2001 From: "ernesto.avedillo" Date: Sun, 30 Jun 2024 15:31:25 +0200 Subject: [PATCH 29/31] Norm --- mandatory/inc/Attributes.hpp | 8 +-- mandatory/inc/CGI.hpp | 54 +++++++-------- mandatory/inc/Environment.hpp | 10 +-- mandatory/inc/ExtendedString.hpp | 44 ++++++------ mandatory/inc/FileContent.hpp | 107 +++++++++++++++--------------- mandatory/inc/Header.hpp | 63 +++++++++--------- mandatory/inc/ListDir.hpp | 52 ++++++++------- mandatory/inc/ListeningSocket.hpp | 70 ++++--------------- mandatory/inc/LocationParser.hpp | 52 +++++++-------- mandatory/inc/Parser.hpp | 35 +++++----- mandatory/inc/Receive.hpp | 67 +++++++++---------- mandatory/inc/Server.hpp | 103 ++++++++++++++-------------- mandatory/inc/colors.h | 6 +- mandatory/inc/utils.hpp | 42 ++++++------ mandatory/src/Attributes.cpp | 6 +- mandatory/src/CGI.cpp | 38 ++++------- mandatory/src/Environment.cpp | 6 +- mandatory/src/ExtendedString.cpp | 32 ++------- mandatory/src/FileContent.cpp | 25 +------ mandatory/src/Header.cpp | 22 +----- mandatory/src/ListDir.cpp | 11 +-- mandatory/src/ListeningSocket.cpp | 29 +------- mandatory/src/Location.cpp | 40 +++-------- mandatory/src/LocationParser.cpp | 44 +----------- mandatory/src/Parser.cpp | 21 +----- mandatory/src/Recieve.cpp | 11 +-- mandatory/src/Server.cpp | 16 ++--- mandatory/src/ServerSetters.cpp | 7 +- mandatory/src/StatusCode.cpp | 7 +- mandatory/src/WebServer.cpp | 5 +- mandatory/src/WebServerLNX.cpp | 6 +- mandatory/src/WebServerMAC.cpp | 6 +- mandatory/src/WebservParser.cpp | 15 +---- mandatory/src/main.cpp | 5 +- mandatory/src/utils.cpp | 31 +++------ 35 files changed, 405 insertions(+), 691 deletions(-) diff --git a/mandatory/inc/Attributes.hpp b/mandatory/inc/Attributes.hpp index 1ffc60b..8178ba0 100644 --- a/mandatory/inc/Attributes.hpp +++ b/mandatory/inc/Attributes.hpp @@ -6,7 +6,7 @@ /* By: eavedill +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/07 18:22:35 by eavedill #+# #+# */ -/* Updated: 2024/06/15 14:34:07 by eavedill ### ########.fr */ +/* Updated: 2024/06/30 14:29:47 by eavedill ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,10 +31,10 @@ class Attributes Attributes(); Attributes(const std::string& filename); ~Attributes(); - void setAttributesFromFile(const std::string& filename); - + + void setAttributesFromFile(const std::string& filename); std::string getName() ; - bool getIsDir() ; + bool getIsDir() ; std::string getSize() ; std::string getDevice() ; std::string getCreated() ; diff --git a/mandatory/inc/CGI.hpp b/mandatory/inc/CGI.hpp index 48bba02..775c28d 100644 --- a/mandatory/inc/CGI.hpp +++ b/mandatory/inc/CGI.hpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* CGI.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: eavedill +#+ +:+ +#+ */ +/* By: eavedill +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/30 13:47:27 by eavedill #+# #+# */ -/* Updated: 2024/06/30 13:47:29 by eavedill ### ########.fr */ +/* Updated: 2024/06/30 15:00:16 by eavedill ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,39 +29,33 @@ class CGI : public Environment { private: - std::string CGIFolder; - std::string fileName; - std::string fileArgs; - bool isCGI; - std::vector args; - std::map CGIExtensions; + std::string CGIFolder; + std::string fileName; + std::string fileArgs; + bool isCGI; + std::vector args; + std::map CGIExtensions; public: CGI(); CGI(const std::string &, const std::string &); CGI(const CGI &); ~CGI(); - //------ Setters and Getters ------// - //setters - void setCGIFolder(const std::string &); - void setFileName(const std::string&, const std::string&); - void setIsCGI(bool); - bool setIdentifyCGIFromFileName(const std::string &str); - bool getIsCGI(); - //bool setIsCGI(const std::string &str); - void setArgs(const std::string &); - void setCGIMapExtensions(std::string const &cgi_extension); - //getters - std::string getFileName(); - std::string getCGIFolder(); - std::vector getArgs(); - std::string getCGIExtension(const std::string &); - std::string getFileExtension(); + void setCGIFolder(const std::string &); + void setFileName(const std::string&, const std::string&); + void setIsCGI(bool); + bool setIdentifyCGIFromFileName(const std::string &str); + bool getIsCGI(); + void setArgs(const std::string &); + void setCGIMapExtensions(std::string const &cgi_extension); + std::string getFileName(); + std::string getCGIFolder(); + std::vector getArgs(); + std::string getCGIExtension(const std::string &); + std::string getFileExtension(); + std::string execute(); + CGI *clone(); + static int ChildPID; + static void alarm_handler(int); std::map::iterator findCGIExtension(const std::string &); - //------ OTHER Methods ------// - std::string execute(); - CGI *clone(); - - static int ChildPID; - static void alarm_handler(int); }; diff --git a/mandatory/inc/Environment.hpp b/mandatory/inc/Environment.hpp index 67cc31b..59d32ab 100644 --- a/mandatory/inc/Environment.hpp +++ b/mandatory/inc/Environment.hpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* Environment.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: eavedill +#+ +:+ +#+ */ +/* By: eavedill +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/30 13:47:48 by eavedill #+# #+# */ -/* Updated: 2024/06/30 13:47:52 by eavedill ### ########.fr */ +/* Updated: 2024/06/30 14:57:33 by eavedill ### ########.fr */ /* */ /* ************************************************************************** */ @@ -50,7 +50,7 @@ class Environment public: Environment(); ~Environment(); - void setEnv(const std::string $key, const std::string $value); - std::vectorgetEnv(); - std::string getEnv(std::string key); + void setEnv(const std::string $key, const std::string $value); + std::vector getEnv(); + std::string getEnv(std::string key); }; diff --git a/mandatory/inc/ExtendedString.hpp b/mandatory/inc/ExtendedString.hpp index fea10a2..1431fde 100644 --- a/mandatory/inc/ExtendedString.hpp +++ b/mandatory/inc/ExtendedString.hpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* ExtendedString.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: eavedill +#+ +:+ +#+ */ +/* By: eavedill +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/30 13:47:57 by eavedill #+# #+# */ -/* Updated: 2024/06/30 13:47:58 by eavedill ### ########.fr */ +/* Updated: 2024/06/30 14:56:55 by eavedill ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,27 +17,23 @@ #include #include "utils.hpp" -class ExtendedString : public std::string { -public: - //using std::string::string; // Inherit constructors from std::string - ExtendedString(); - ExtendedString(const std::string &str); - ~ExtendedString(); - ExtendedString &operator=(const std::string &str); - - bool isNumber() const; - bool firstCharIsSign() const; - void replaceString(const std::string& , const std::string& ); - bool validIPAddress(); - size_t stringToSizeT(); - std::vector splitString(char ); - //***To be used in case do not want to modify the original string - //ExtendedString removeCharFromString(char); - void removeCharFromString(char); - //***To be used in case do not want to modify the original string - // ExtendedString removeBlanksAndTabs() ; - void removeBlanksAndTabs() ; - int count_chars(char c); - void replaceFirstString(const std::string& searchString, const std::string& replaceString); +class ExtendedString : public std::string +{ + public: + ExtendedString(); + ExtendedString(const std::string &str); + ~ExtendedString(); + ExtendedString &operator=(const std::string &str); + + bool isNumber() const; + bool firstCharIsSign() const; + void replaceString(const std::string& , const std::string& ); + bool validIPAddress(); + size_t stringToSizeT(); + std::vector splitString(char ); + void removeCharFromString(char); + void removeBlanksAndTabs() ; + int count_chars(char c); + void replaceFirstString(const std::string& searchString, const std::string& replaceString); }; \ No newline at end of file diff --git a/mandatory/inc/FileContent.hpp b/mandatory/inc/FileContent.hpp index 05cf750..a240c36 100644 --- a/mandatory/inc/FileContent.hpp +++ b/mandatory/inc/FileContent.hpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* FileContent.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: eavedill +#+ +:+ +#+ */ +/* By: eavedill +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/30 13:48:24 by eavedill #+# #+# */ -/* Updated: 2024/06/30 13:48:25 by eavedill ### ########.fr */ +/* Updated: 2024/06/30 14:55:08 by eavedill ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,60 +33,59 @@ class FileContent : public StatusCode { protected: - CGI *cgiModule; + CGI *cgiModule; private: - std::string fileName; - std::string indexName; - std::vector args; - ExtendedString content; - std::ifstream file; - bool isFistFragment; - bool isFileOpen; - bool sendComplete; - bool isAutoIndex; - std::string homeFolder; - struct stat fileStat; - long long startRange; - long long currentSendingPosition; - long long lastSendingPosition; - ListDir *listDir; - size_t completeContentSize; - std::string splitFileFromArgs(const std::string &); - bool FileOrFolerExtists(const std::string &); - size_t endRange; + std::string fileName; + std::string indexName; + std::vector args; + ExtendedString content; + std::ifstream file; + bool isFistFragment; + bool isFileOpen; + bool sendComplete; + bool isAutoIndex; + std::string homeFolder; + struct stat fileStat; + long long startRange; + long long currentSendingPosition; + long long lastSendingPosition; + ListDir *listDir; + size_t completeContentSize; + size_t endRange; + + std::string splitFileFromArgs(const std::string &); + bool FileOrFolerExtists(const std::string &); public: FileContent(); ~FileContent(); - int openFile(); - bool setFileName(const std::string &, const std::string &); - void setIsCGI(bool isCgi); - void setStartRange(long long); - void setEndRange(long long); - long long getStartRange(); - std::string getFileName(); - std::string getContent(); - void setIsFileOpen(bool); - void setIsSendComplete(bool); - bool getIsSendComplete(); - void setIsCgi(bool ); - bool getIsCgi(); - - bool getIsFileOpen(); - void setFirstFragment(bool); - bool getFirstFragment(); - std::string getLastModified(); - size_t getCompleteContentSize(); - void setCompleteContentSize(size_t); - bool isInputDirectory(); - void setRange(size_t ); - void setIsAutoIndex(bool); - bool getIsAutoIndex(); - void setIndexName(const std::string &); - std::string getIndexName(); - void setHomeFolder(const std::string &); - std::string getHomeFolder(); - void setCGIModule(CGI *); - long long getFileSize(); - long long getCurrentSendingPosition(); - long long getLastSendingPosition(); + int openFile(); + bool setFileName(const std::string &, const std::string &); + void setIsCGI(bool isCgi); + void setStartRange(long long); + void setEndRange(long long); + long long getStartRange(); + std::string getFileName(); + std::string getContent(); + void setIsFileOpen(bool); + void setIsSendComplete(bool); + bool getIsSendComplete(); + void setIsCgi(bool ); + bool getIsFileOpen(); + void setFirstFragment(bool); + bool getFirstFragment(); + std::string getLastModified(); + size_t getCompleteContentSize(); + void setCompleteContentSize(size_t); + bool isInputDirectory(); + void setRange(size_t ); + void setIsAutoIndex(bool); + bool getIsAutoIndex(); + void setIndexName(const std::string &); + std::string getIndexName(); + void setHomeFolder(const std::string &); + std::string getHomeFolder(); + void setCGIModule(CGI *); + long long getFileSize(); + long long getCurrentSendingPosition(); + long long getLastSendingPosition(); }; diff --git a/mandatory/inc/Header.hpp b/mandatory/inc/Header.hpp index ded226b..383c50a 100644 --- a/mandatory/inc/Header.hpp +++ b/mandatory/inc/Header.hpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* Header.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: eavedill +#+ +:+ +#+ */ +/* By: eavedill +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/30 13:48:01 by eavedill #+# #+# */ -/* Updated: 2024/06/30 13:48:02 by eavedill ### ########.fr */ +/* Updated: 2024/06/30 14:50:34 by eavedill ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,41 +23,38 @@ class Header { private: /* Receive Header*/ - std::string method; - ExtendedString path; - - std::string protocol; - std::string status; - std::string server; - std::string date; - std::string lastModified; - size_t contentLength; - std::string contentType; - std::map attributes; - std::multimap cookies; + std::string method; + ExtendedString path; + std::string protocol; + std::string status; + std::string server; + std::string date; + std::string lastModified; + size_t contentLength; + std::string contentType; + std::map attributes; + std::multimap cookies; public: Header(); Header(std::string receiveHeader); ~Header(); - std::string generateHeader() const; - void setVersion(std::string version); - void setStatus(std::string status); - void setServer(std::string server); - void setDate(); - void setLastModified(std::string lastModified); - void setContentLength(size_t contentLength); - void setContentType(std::string contentType); - void setAttribute(std::string key, std::string value); - void setMethod(std::string method); - void setPath(std::string path); - void setProtocol(std::string protocol); - std::string getMethod(); - ExtendedString getPath(); - std::string getProtocol(); + std::string generateHeader() const; + void setStatus(std::string status); + void setServer(std::string server); + void setDate(); + void setLastModified(std::string lastModified); + void setContentLength(size_t contentLength); + void setContentType(std::string contentType); + void setAttribute(std::string key, std::string value); + void setPath(std::string path); + void setProtocol(std::string protocol); + std::string getMethod(); + ExtendedString getPath(); + std::string getProtocol(); + std::string getContentType(); + std::string getAttribute(std::string key); + void printReceivedHeader(); + void setCookie(std::string value); std::map getAttributes(); - std::string getContentType(); - std::string getAttribute(std::string key); - void printReceivedHeader(); - void setCookie(std::string value); }; diff --git a/mandatory/inc/ListDir.hpp b/mandatory/inc/ListDir.hpp index 4e4ee6e..acb1ffa 100644 --- a/mandatory/inc/ListDir.hpp +++ b/mandatory/inc/ListDir.hpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* ListDir.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: eavedill +#+ +:+ +#+ */ +/* By: eavedill +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/07 19:03:57 by eavedill #+# #+# */ -/* Updated: 2024/06/30 13:48:17 by eavedill ### ########.fr */ +/* Updated: 2024/06/30 14:47:57 by eavedill ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,30 +21,32 @@ #include "utils.hpp" #include "colors.h" #define TEMPLATE_LIST_DIR "./Templates/dir_list.html" + class ListDir { - private: - std::ifstream file; - std::string path; - std::string homePath; - std::string contentToSend; - std::size_t posToSend; - std::map files; - bool isSendComplete; - public: - ListDir(); - ListDir(const std::string &, const std::string &); - ~ListDir(); - void setListOfFiles(); - std::map ::iterator getBeginOfFiles(); - std::map ::iterator getEndOfFiles(); - std::string getDirFileList(); - void setContentToList(); - void setIsSendComlete(); - bool getIsSendComlete(); - void openMasterListFile(); - std::string getContentToSend(); - size_t getSizeToSend(); - void setSubdirectory(const std::string &); + private: + std::ifstream file; + std::string path; + std::string homePath; + std::string contentToSend; + std::size_t posToSend; + std::map files; + bool isSendComplete; + + public: + ListDir(); + ListDir(const std::string &, const std::string &); + ~ListDir(); + std::map ::iterator getBeginOfFiles(); + std::map ::iterator getEndOfFiles(); + void setListOfFiles(); + std::string getDirFileList(); + void setContentToList(); + void setIsSendComlete(); + bool getIsSendComlete(); + void openMasterListFile(); + std::string getContentToSend(); + size_t getSizeToSend(); + void setSubdirectory(const std::string &); }; diff --git a/mandatory/inc/ListeningSocket.hpp b/mandatory/inc/ListeningSocket.hpp index 175ddd5..311df75 100644 --- a/mandatory/inc/ListeningSocket.hpp +++ b/mandatory/inc/ListeningSocket.hpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* ListeningSocket.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: eavedill +#+ +:+ +#+ */ +/* By: eavedill +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/30 13:48:33 by eavedill #+# #+# */ -/* Updated: 2024/06/30 13:48:34 by eavedill ### ########.fr */ +/* Updated: 2024/06/30 14:46:10 by eavedill ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,13 +21,11 @@ #include #include #include - #ifdef __APPLE__ -#include + #include #else -#include + #include #endif - #include #include #include @@ -41,50 +39,12 @@ #include "Header.hpp" #include "LocationParser.hpp" #include "Environment.hpp" -// SCRIPT_FILENAME -// QUERY_STRING -// REQUEST_METHOD -// CONTENT_TYPE - -// CONTENT_LENGTH - -// GATEWAY_INTERFACE CGI/1.1 -// SERVER_SOFTWARE webservcheelave - -// REMOTE_ADDR 127.0.0.1 -// REMOTE_PORT 8080 - -// SERVER_ADDR localhost -// SERVER_PORT 8080 -// The name of the authentication scheme used to protect the servlet. For example, BASIC, SSL, or null if the servlet was not protected. - -// CONTENT_LENGTH - -// The length of the request body in bytes made available by the input stream or -1 if the length is not known. For HTTP servlets, the value returned is the same as the value of the CGI variable CONTENT_LENGTH. - -// CONTENT_TYPE - -// The MIME type of the body of the request, or null if the type is not known. For HTTP servlets, the value returned is the same as the value of the CGI variable CONTENT_TYPE. - -// SERVER_NAME webserver -// HTTP_HOST -// HTTP_USER_AGENT -// HTTP_ACCEPT -// HTTP_ACCEPT_LANGUAGE -// HTTP_ACCEPT_ENCODING -// HTTP_COOKIE -// HTTP_CONNECTION -// HTTP_UPGRADE_INSECURE_REQUESTS - - #define MAX_CONNECTIONS 10 class Server; class Receive; - -// #define MAX_EVENTS 10 class ListeningSocket : public FileContent { private: @@ -95,21 +55,19 @@ class ListeningSocket : public FileContent Header response; Header request; - //void handleConnection(int clientSocketFd); - public: ListeningSocket(int port, Server *srv); ListeningSocket(Server *srv); ~ListeningSocket(); - bool startListening(); - void stopListening(); - int getFd(); - bool sendData(int); - void loadRequest(std::vector servers); - ListeningSocket *clone(int fd); - std::string getAnswerToSend(); - bool receive(); - void matchServerName(std::vector servers); - void setCgiEnviroment(); + bool startListening(); + void stopListening(); + int getFd(); + bool sendData(int); + void loadRequest(std::vector servers); + ListeningSocket *clone(int fd); + std::string getAnswerToSend(); + bool receive(); + void matchServerName(std::vector servers); + void setCgiEnviroment(); }; diff --git a/mandatory/inc/LocationParser.hpp b/mandatory/inc/LocationParser.hpp index e7df33a..4d67010 100644 --- a/mandatory/inc/LocationParser.hpp +++ b/mandatory/inc/LocationParser.hpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* LocationParser.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: eavedill +#+ +:+ +#+ */ +/* By: eavedill +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/30 13:48:49 by eavedill #+# #+# */ -/* Updated: 2024/06/30 13:48:50 by eavedill ### ########.fr */ +/* Updated: 2024/06/30 14:43:58 by eavedill ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,31 +41,31 @@ class Server; class LocationParser { private: - Header request; - Header response; - Server *server; - Receive *receiver; - std::vector cookies; - std::string query; - bool isAutoIndex; - bool isCGI; - bool isCookie; - size_t startRange; - size_t endRange; - std::string cgiPath; - // std::string + Header request; + Header response; + Server *server; + Receive *receiver; + std::vector cookies; + std::string query; + bool isAutoIndex; + bool isCGI; + bool isCookie; + size_t startRange; + size_t endRange; + std::string cgiPath; + public: LocationParser(Header request, Server *server, Receive *receiver); ~LocationParser(); - int matchingLocation(); - int isAllowedMethod(Location *location); - Header getRequest(); - Header getResponse(); - bool getIsAutoIndex(); - bool getIsCGI(); - void checks(); - void setCookies(); - size_t getStartRange(); - size_t getEndRange(); - std::string getQuery(); + int matchingLocation(); + int isAllowedMethod(Location *location); + Header getRequest(); + Header getResponse(); + bool getIsAutoIndex(); + bool getIsCGI(); + void checks(); + void setCookies(); + size_t getStartRange(); + size_t getEndRange(); + std::string getQuery(); }; \ No newline at end of file diff --git a/mandatory/inc/Parser.hpp b/mandatory/inc/Parser.hpp index bcf8a3e..e2b04b3 100644 --- a/mandatory/inc/Parser.hpp +++ b/mandatory/inc/Parser.hpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* Parser.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: eavedill +#+ +:+ +#+ */ +/* By: eavedill +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/30 13:48:59 by eavedill #+# #+# */ -/* Updated: 2024/06/30 13:49:00 by eavedill ### ########.fr */ +/* Updated: 2024/06/30 14:41:40 by eavedill ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,21 +28,20 @@ class Parser { private: public: - static bool checkPorts(std::vector const &ports); - static bool checkPort(std::string port); - static bool checkHost(std::string host); - static bool checkServerName(std::string serverName); - static bool checkErrorPage(std::string errorPage); - static bool checkRoot(std::string root); - static bool checkPath(std::string root, std::string attributeName); - static bool checkIndex(std::string index, std::string root); + static bool checkPorts(std::vector const &ports); + static bool checkPort(std::string port); + static bool checkHost(std::string host); + static bool checkServerName(std::string serverName); + static bool checkErrorPage(std::string errorPage); + static bool checkRoot(std::string root); + static bool checkPath(std::string root, std::string attributeName); + static bool checkIndex(std::string index, std::string root); static in_addr_t isValidHost(std::string hostname); - static long long checkClientBodySize(std::string maxClientBodySize); - - static bool checkAutoIndex(std::string autoindex); - static int checkLocationName(std::string name); - static int checkRootAliasReturn(std::string root, std::string alias, std::string return_); - static bool checkReturnIgnore(std::string allowMethods, std::string autoindex, std::string index); - static bool checkCgi(std::string cgiExtension); - static bool checkAllowedMethods(std::string allowMethods); + static long long checkClientBodySize(std::string maxClientBodySize); + static bool checkAutoIndex(std::string autoindex); + static int checkLocationName(std::string name); + static int checkRootAliasReturn(std::string root, std::string alias, std::string return_); + static bool checkReturnIgnore(std::string allowMethods, std::string autoindex, std::string index); + static bool checkCgi(std::string cgiExtension); + static bool checkAllowedMethods(std::string allowMethods); }; \ No newline at end of file diff --git a/mandatory/inc/Receive.hpp b/mandatory/inc/Receive.hpp index fbf5385..04b4003 100644 --- a/mandatory/inc/Receive.hpp +++ b/mandatory/inc/Receive.hpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* Receive.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: eavedill +#+ +:+ +#+ */ +/* By: eavedill +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/30 13:49:11 by eavedill #+# #+# */ -/* Updated: 2024/06/30 13:49:12 by eavedill ### ########.fr */ +/* Updated: 2024/06/30 14:37:50 by eavedill ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,39 +16,38 @@ #include #include #include +#define MAX_MSG_SIZE 8192 - -#define MAX_MSG_SIZE 8192//2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 class Receive { - private: - std::string buffer; - std::string request; - std::string body; - bool isbody; - size_t maxSize; - size_t sizeSent; - std::ofstream file; - std::string boundary; - bool isform; - std::string postHeader; + private: + std::string buffer; + std::string request; + std::string body; + bool isbody; + size_t maxSize; + size_t sizeSent; + std::ofstream file; + std::string boundary; + bool isform; + std::string postHeader; - public: - Receive(); - ~Receive(); - Receive(Receive const ©); - Receive &operator=(Receive const ©); - - bool receiveHeader(int fd); - bool receiveBody(int fd); - bool receive(int fd); - void setBuffer(std::string buffer); - void setRequest(std::string request); - void setBody(std::string body); - void setmaxSize(size_t size); - - std::string getBuffer(); - std::string getRequest(); - std::string getBody(); - bool getisform(); - std::string getPostHeader(); + public: + Receive(); + ~Receive(); + Receive(Receive const ©); + Receive &operator=(Receive const ©); + + bool receiveHeader(int fd); + bool receiveBody(int fd); + bool receive(int fd); + void setBuffer(std::string buffer); + void setRequest(std::string request); + void setBody(std::string body); + void setmaxSize(size_t size); + + std::string getBuffer(); + std::string getRequest(); + std::string getBody(); + bool getisform(); + std::string getPostHeader(); }; \ No newline at end of file diff --git a/mandatory/inc/Server.hpp b/mandatory/inc/Server.hpp index 373a1ae..79dd3ad 100644 --- a/mandatory/inc/Server.hpp +++ b/mandatory/inc/Server.hpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* Server.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: eavedill +#+ +:+ +#+ */ +/* By: eavedill +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/30 13:49:18 by eavedill #+# #+# */ -/* Updated: 2024/06/30 13:49:19 by eavedill ### ########.fr */ +/* Updated: 2024/06/30 14:35:38 by eavedill ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,15 +26,15 @@ # include "Parser.hpp" # include "ExtendedString.hpp" -#define VAR_PORT "port" -#define VAR_HOST "host" -#define VAR_SERVER_NAME "server_name" -#define VAR_ERROR_PAGE "error_page" -#define VAR_ROOT "root" -#define VAR_INDEX "index" +#define VAR_PORT "port" +#define VAR_HOST "host" +#define VAR_SERVER_NAME "server_name" +#define VAR_ERROR_PAGE "error_page" +#define VAR_ROOT "root" +#define VAR_INDEX "index" #define VAR_CLIENT_MAX_BODY_SIZE "client_max_body_size" -#define VAR_LOCATIONS "location" -#define VAR_AUTOINDEX "autoindex" +#define VAR_LOCATIONS "location" +#define VAR_AUTOINDEX "autoindex" #ifdef __APPLE__ #define READ_EVENT EVFILT_READ @@ -53,30 +53,27 @@ class ListeningSocket; class Server { protected: - bool isDefault; - std::map port; - std::vector ports; - long long maxBodySize; - std::string maxBodySizeStr; - std::string Host; - std::string serverName; - ExtendedString errorPage; - std::string root; - std::string index; - std::string autoIndexStr; - bool autoIndex; - std::vector locations; - CGI *cgiModule; - in_addr_t hostAddr; - // void setDefaultData(); + bool isDefault; + std::map port; + std::vector ports; + long long maxBodySize; + std::string maxBodySizeStr; + std::string Host; + std::string serverName; + ExtendedString errorPage; + std::string root; + std::string index; + std::string autoIndexStr; + bool autoIndex; + std::vector locations; + CGI *cgiModule; + in_addr_t hostAddr; public: - // Server(); Server(std::string &); ~Server(); Server(Server const &); - Server &operator=(Server const &); - //---- Server Methods ------// + Server &operator=(Server const &); //---- Server Methods ------// int loadData(std::string const &); CGI *cgiModuleClone(); @@ -95,34 +92,30 @@ class Server void setPorts(std::string const &ports); void setHostAddr(in_addr_t Addr); void setAutoindex(std::string const &); - //---- Server getters ------// - ListeningSocket *getListening(int i); - std::vector getPorts(); - std::vector getServerFds(); - - ListeningSocket * getPort(int i); - bool getIsDefault(); - std::string getHost(); - std::string getServerName(); - std::string getErrorPage(); - std::string getRoot(); - std::string getIndex(); - std::string getCGIExtension(const std::string &); - std::string getCGIFolder(); - in_addr_t getHostAddr(); - std::string getMaxClientBodySizeStr(); - long long getMaxClientBodySize(); - std::vector getLocations(); - bool getAutoIndex(); - void print(); - void createListeningSockets(); - void checkVariables(); - - //std::vector getLocations(); + ListeningSocket *getListening(int i); + std::vector getPorts(); + std::vector getServerFds(); + ListeningSocket * getPort(int i); + bool getIsDefault(); + std::string getHost(); + std::string getServerName(); + std::string getErrorPage(); + std::string getRoot(); + std::string getIndex(); + std::string getCGIExtension(const std::string &); + std::string getCGIFolder(); + in_addr_t getHostAddr(); + std::string getMaxClientBodySizeStr(); + long long getMaxClientBodySize(); + std::vector getLocations(); + bool getAutoIndex(); + void print(); + void createListeningSockets(); + void checkVariables(); }; -typedef void (Server::*server)(std::string); -typedef std::map server_methods_t; +typedef void (Server::*server)(std::string); +typedef std::map server_methods_t; #endif diff --git a/mandatory/inc/colors.h b/mandatory/inc/colors.h index 059d8f7..2b8072b 100644 --- a/mandatory/inc/colors.h +++ b/mandatory/inc/colors.h @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* colors.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: eavedill +#+ +:+ +#+ */ +/* By: eavedill +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/05/06 11:16:47 by eavedill #+# #+# */ -/* Updated: 2024/02/09 11:18:46 by eavedill ### ########.fr */ +/* Updated: 2024/06/30 14:58:03 by eavedill ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,6 @@ # define COLORS_H // Standard color # define RESET "\033[0m" -//# define RESET "" // Background colors over black # define BCK_BLACK "\033[0;40m" # define BCK_RED "\033[0;41m" @@ -33,7 +32,6 @@ # define CHR_MGENTA "\033[0;35m" # define CHR_CYAN "\033[0;36m" # define CHR_WHITE "\033[0;37m" - # define BLACK "\033[30m" # define RED "\033[31m" # define GREEN "\033[32m" diff --git a/mandatory/inc/utils.hpp b/mandatory/inc/utils.hpp index 9123d75..f455030 100644 --- a/mandatory/inc/utils.hpp +++ b/mandatory/inc/utils.hpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* utils.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: eavedill +#+ +:+ +#+ */ +/* By: eavedill +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/30 13:49:35 by eavedill #+# #+# */ -/* Updated: 2024/06/30 13:49:39 by eavedill ### ########.fr */ +/* Updated: 2024/06/30 15:03:42 by eavedill ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,41 +30,22 @@ #include #include #include -#define MAX_SENT_BYTES 16048 //8024 +#define MAX_SENT_BYTES 16048 template bool isrange(T value, T min, T max); -bool isNumber(std::string str); -bool validIPAddress(std::string ip); -bool isDirPermissions(std::string path, int mode); -int isFilePermissions(std::string path, int mode); template std::string toString (T n) { std::string str; std::stringstream ss; - ss << n; ss >> str; - return str; } -std::string removeBlanksAndTabs(const std::string& input); -std::string removeCharFromString(const std::string& input, char c); -std::vector splitString(const std::string &str, char delimiter); -size_t stringToSizeT(const std::string& str); -std::map create_filetypes(); -void replaceString(std::string &mainString, const std::string &searchString, const std::string &replaceString); -int count_chars(const std::string &str, char c); - -std::string getTime(); -std::string getLocalTime(); -void printLog(std::string type ,std::string message); -std::string decodeURL(const std::string& url); - template T minimum(T a, T b) { @@ -79,4 +60,19 @@ T maximum(T a, T b) if (a > b) return a; return b; -}; \ No newline at end of file +}; + +bool isNumber(std::string str); +bool validIPAddress(std::string ip); +bool isDirPermissions(std::string path, int mode); +int isFilePermissions(std::string path, int mode); +std::string removeBlanksAndTabs(const std::string& input); +std::string removeCharFromString(const std::string& input, char c); +std::vector splitString(const std::string &str, char delimiter); +size_t stringToSizeT(const std::string& str); +std::map create_filetypes(); +void replaceString(std::string &mainString, const std::string &searchString, const std::string &replaceString); +int count_chars(const std::string &str, char c); +std::string getLocalTime(); +void printLog(std::string type ,std::string message); +std::string decodeURL(const std::string& url); diff --git a/mandatory/src/Attributes.cpp b/mandatory/src/Attributes.cpp index b832a8a..042e9cf 100644 --- a/mandatory/src/Attributes.cpp +++ b/mandatory/src/Attributes.cpp @@ -6,7 +6,7 @@ /* By: eavedill +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/07 18:22:26 by eavedill #+# #+# */ -/* Updated: 2024/06/15 14:34:16 by eavedill ### ########.fr */ +/* Updated: 2024/06/30 15:10:47 by eavedill ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,7 +24,8 @@ Attributes::Attributes(const std::string& filename) this->setAttributesFromFile(filename); } -Attributes::~Attributes() { +Attributes::~Attributes() +{ } void Attributes::setAttributesFromFile(const std::string& filename) @@ -65,7 +66,6 @@ std::string Attributes::getCreated() std::string Attributes::getModified() { std::tm *timeInfo = std::gmtime(&this->attributes.st_mtime); - char buffer[80]; std::strftime(buffer, sizeof(buffer), "%A, %d-%b-%y %H:%M:%S GMT", timeInfo); return buffer; diff --git a/mandatory/src/CGI.cpp b/mandatory/src/CGI.cpp index f057228..a44673b 100644 --- a/mandatory/src/CGI.cpp +++ b/mandatory/src/CGI.cpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* CGI.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: eavedill +#+ +:+ +#+ */ +/* By: eavedill +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/30 13:49:55 by eavedill #+# #+# */ -/* Updated: 2024/06/30 13:49:56 by eavedill ### ########.fr */ +/* Updated: 2024/06/30 15:28:58 by eavedill ### ########.fr */ /* */ /* ************************************************************************** */ @@ -53,15 +53,6 @@ void CGI::setFileName(const std::string &Name, const std::string &Args) fileName = Name; this->setArgs(Args); tmp = splitString(Args, '&'); - // std::map::iterator it = this->findCGIExtension(this->getFileExtension()); - // if (it != this->CGIExtensions.end()) - // { - // if (it->second.size() != 0) - // { - // fileName =it->second; - // this->setArgs(tmp[1]); - // } - // } } void CGI::setIsCGI(bool valCGI) @@ -112,9 +103,15 @@ std::vector CGI::getArgs() return args; } -std::map::iterator CGI::findCGIExtension(const std::string &str) { return this->CGIExtensions.find(str); } +std::map::iterator CGI::findCGIExtension(const std::string &str) +{ + return this->CGIExtensions.find(str); +} -std::string CGI::getCGIExtension(const std::string &str) { return this->CGIExtensions[str]; } +std::string CGI::getCGIExtension(const std::string &str) +{ + return this->CGIExtensions[str]; +} std::string CGI::getFileExtension() { @@ -144,40 +141,31 @@ std::string CGI::execute() } pid_t pid = fork(); if (pid == -1) { - // Handle error forking process throw INTERNAL_SERVER_ERROR_CODE; return ""; } if (pid == 0) { - // Child process dup2(fd[1], STDOUT_FILENO); close(fd[0]); - // Convert the arguments vector to a null-terminated array - // Execute the file with its parameters std::vector envp = this->getEnv(); - - if (execve(Executable.c_str(), ExecArray.data(), envp.data()) == -1) { - // Handle error executing file std::cerr << "Error: " << errno << std::endl; exit(errno); } } - // Parent process close(fd[1]); CGI::ChildPID = pid; alarm(timeout); int status; waitpid(pid, &status, 0); - std::string output; if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { - // Read the output from the file descriptor char buffer[1024]; ssize_t bytesRead; - while ((bytesRead = read(fd[0], buffer, sizeof(buffer))) > 0) { + while ((bytesRead = read(fd[0], buffer, sizeof(buffer))) > 0) + { output += std::string(buffer, bytesRead); } close(fd[0]); @@ -217,7 +205,7 @@ void CGI::setCGIMapExtensions(std::string const &cgi_extension) } else if (n == 0) { - this->CGIExtensions[aux[i]] = ""; // Si no hay programa de ejecuciĆ³n, se asume que es un CGI ejecutable. + this->CGIExtensions[aux[i]] = ""; } else { diff --git a/mandatory/src/Environment.cpp b/mandatory/src/Environment.cpp index ede41de..86dcdc2 100644 --- a/mandatory/src/Environment.cpp +++ b/mandatory/src/Environment.cpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* Environment.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: eavedill +#+ +:+ +#+ */ +/* By: eavedill +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/30 13:50:02 by eavedill #+# #+# */ -/* Updated: 2024/06/30 13:50:03 by eavedill ### ########.fr */ +/* Updated: 2024/06/30 15:27:37 by eavedill ### ########.fr */ /* */ /* ************************************************************************** */ @@ -44,9 +44,7 @@ std::vector Environment::getEnv() itb++; } _envArray.push_back(NULL); - return _envArray; - } std::string Environment::getEnv(std::string key) diff --git a/mandatory/src/ExtendedString.cpp b/mandatory/src/ExtendedString.cpp index 1d0d023..92edb7c 100644 --- a/mandatory/src/ExtendedString.cpp +++ b/mandatory/src/ExtendedString.cpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* ExtendedString.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: eavedill +#+ +:+ +#+ */ +/* By: eavedill +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/30 13:50:11 by eavedill #+# #+# */ -/* Updated: 2024/06/30 13:50:11 by eavedill ### ########.fr */ +/* Updated: 2024/06/30 15:27:27 by eavedill ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,7 +37,8 @@ bool ExtendedString::isNumber() const { return true; } -bool ExtendedString::firstCharIsSign() const { +bool ExtendedString::firstCharIsSign() const +{ return this->at(0) == '-' || this->at(0) == '+'; } @@ -64,7 +65,6 @@ bool ExtendedString::validIPAddress() size_t pos = 0; ExtendedString token; int count = 0; - while ((pos = this->find('.')) != std::string::npos) { token = this->substr(0, pos); @@ -73,10 +73,8 @@ bool ExtendedString::validIPAddress() this->erase(0, pos + 1); count++; } - if (count != 4 || !this->isNumber() || !isrange(std::atoi(this->c_str()), 0, 255)) return false; - return count == 4; } @@ -100,17 +98,6 @@ std::vector ExtendedString::splitString(char delimiter) { return result; } -//***To be used in case do not want to modify the original string -// ExtendedString ExtendedString::removeCharFromString(char charToRemove) { -// ExtendedString result; -// for (size_t i = 0; i < this->size(); i++) { -// if (this->at(i) != charToRemove) { -// result += this->at(i); -// } -// } -// return result; -// } - void ExtendedString::removeCharFromString(char charToRemove) { size_t pos = 0; while ((pos = this->find(charToRemove, pos)) != std::string::npos) { @@ -118,17 +105,6 @@ void ExtendedString::removeCharFromString(char charToRemove) { } } -//***To be used in case do not want to modify the original string -// ExtendedString ExtendedString::removeBlanksAndTabs() { -// ExtendedString result; -// for (size_t i = 0; i < this->size(); i++) { -// if (this->at(i) != ' ' && this->at(i) != '\t') { -// result += this->at(i); -// } -// } -// return result; -// } - void ExtendedString::removeBlanksAndTabs() { size_t pos = 0; while ((pos = this->find(' ', pos)) != std::string::npos) { diff --git a/mandatory/src/FileContent.cpp b/mandatory/src/FileContent.cpp index 4b0e282..bbcaadf 100644 --- a/mandatory/src/FileContent.cpp +++ b/mandatory/src/FileContent.cpp @@ -3,16 +3,15 @@ /* ::: :::::::: */ /* FileContent.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: eavedill +#+ +:+ +#+ */ +/* By: eavedill +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/30 13:50:28 by eavedill #+# #+# */ -/* Updated: 2024/06/30 13:50:29 by eavedill ### ########.fr */ +/* Updated: 2024/06/30 15:26:47 by eavedill ### ########.fr */ /* */ /* ************************************************************************** */ #include "../inc/FileContent.hpp" -// FileContent::FileContent(Server *srv) : StatusCode() FileContent::FileContent() : StatusCode() { fileName = ""; @@ -78,12 +77,10 @@ std::string FileContent::getContent() if (this->startRange != 0 && this->getFirstFragment()) { - //file.seekg(0, std::ios::beg); file.seekg(startRange, std::ios::beg); lastSendingPosition = currentSendingPosition; file.read(buffer, MAX_SENT_BYTES); content.append(buffer, file.gcount()); - //this->setIsSendComplete(true); } else { @@ -94,7 +91,6 @@ std::string FileContent::getContent() currentSendingPosition = file.tellg(); if (file.eof()) { -// std::cout << "Cierro fichero " << fileName << std::endl; file.close(); this->setIsSendComplete(true); } @@ -110,7 +106,6 @@ std::string FileContent::getContent() bool FileContent::setFileName(const std::string &file_name, const std::string &fileArgs) { - std::cout << file_name << std::endl; bool fileOrFolderExists = this->FileOrFolerExtists(file_name); if (fileOrFolderExists) { @@ -123,30 +118,17 @@ bool FileContent::setFileName(const std::string &file_name, const std::string &f listDir->setSubdirectory(file_name); listDir->setContentToList(); } - // else if (cgiModule->setIdentifyCGIFromFileName(file_name)) - // else if (this->isCgi) else if (cgiModule->getIsCGI() ) { - std::cout << "CGI" << std::endl; cgiModule->setFileName(file_name, fileArgs); this->setIsFileOpen(true); } else { - // if (this->isInputDirectory()) - // { - // fileName = homeFolder + indexName; - // } - // else - // { - fileName = file_name; - // } + fileName = file_name; stat(fileName.c_str(), &fileStat); completeContentSize = fileStat.st_size; this->setIsFileOpen(this->openFile()); - // isFileOpen = true; - // if (this->getIsFileOpen()) - // file.seekg(startRange, std::ios::beg); } } return this->getIsFileOpen(); @@ -195,7 +177,6 @@ std::string FileContent::getLastModified() return ""; } char buffer[80]; - strftime(buffer, sizeof(buffer), "%A, %d-%b-%y %H:%M:%S GMT", localtime(&fileStat.st_mtime)); return buffer; } diff --git a/mandatory/src/Header.cpp b/mandatory/src/Header.cpp index 22bc4c1..64c6de8 100644 --- a/mandatory/src/Header.cpp +++ b/mandatory/src/Header.cpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* Header.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: eavedill +#+ +:+ +#+ */ +/* By: eavedill +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/30 13:52:00 by eavedill #+# #+# */ -/* Updated: 2024/06/30 13:52:00 by eavedill ### ########.fr */ +/* Updated: 2024/06/30 15:26:03 by eavedill ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,7 +26,6 @@ Header::Header(std::string receiveHeader) { std::istringstream iss(receiveHeader); std::string line; - while (std::getline(iss, line)) { size_t colonPos = line.find(':'); @@ -55,11 +54,6 @@ Header::Header(std::string receiveHeader) } } } - // std::cout << "HEADER" << std::endl; - // for (std::map::const_iterator it = attributes.begin(); it != attributes.end(); ++it) - // { - // std::cout << it->first << " => " << it->second << std::endl; - // } } void Header::setAttribute(std::string key, std::string value) @@ -96,8 +90,6 @@ void Header::setPath(std::string path) std::string Header::generateHeader() const { std::string header; - - // std::cout << "Version: $" << Version << "$" << std::endl; header = protocol + " " + status + "\r\n"; header += "Server: " + server + "\r\n"; header += "Date: " + date + "\r\n"; @@ -110,7 +102,6 @@ std::string Header::generateHeader() const header += it->first + ": " + it->second + "\r\n"; for (std::multimap::const_iterator it = cookies.begin(); it != cookies.end(); ++it) header += it->first + ": " + it->second + "\r\n"; - // response.setAttribute("Set-Cookie", "id=123; name=Joseph; lastname=Cheel; theme=light;"); header += "\r\n"; return header; @@ -143,15 +134,10 @@ void Header::setServer(std::string server) void Header::setDate() { - // Get the current time std::time_t currentTime = std::time(NULL); - - // Convert the current time to a string in the desired format std::tm* timeInfo = std::gmtime(¤tTime); char buffer[80]; std::strftime(buffer, sizeof(buffer), "%A, %d-%b-%y %H:%M:%S GMT", timeInfo); - - // Set the date in the header this->date = buffer; } @@ -175,15 +161,11 @@ void Header::setContentType(std::string contentType) { size_t point = contentType.find_last_of("."); std::string extension = contentType.substr(point + 1, contentType.size()); - - /* Create once only */ std::map Mimetype = create_filetypes(); - if (Mimetype.find(extension) != Mimetype.end()) this->contentType = Mimetype[extension]; else this->contentType = "text/html"; - } void Header::printReceivedHeader() diff --git a/mandatory/src/ListDir.cpp b/mandatory/src/ListDir.cpp index 41e50d4..e08e376 100644 --- a/mandatory/src/ListDir.cpp +++ b/mandatory/src/ListDir.cpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* ListDir.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: eavedill +#+ +:+ +#+ */ +/* By: eavedill +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/30 13:51:56 by eavedill #+# #+# */ -/* Updated: 2024/06/30 13:51:56 by eavedill ### ########.fr */ +/* Updated: 2024/06/30 15:25:36 by eavedill ### ########.fr */ /* */ /* ************************************************************************** */ @@ -74,14 +74,12 @@ std::string ListDir::getDirFileList() { std::string content = ""; std::map ::iterator itb = files.begin(); - //do not add ./ and ../ itb++; itb++; std::map::iterator ite = files.end(); while (itb != ite) { content = content + "\n"); contentToSend += "\n"; } @@ -123,7 +118,7 @@ void ListDir::setIsSendComlete() bool ListDir::getIsSendComlete() { - return isSendComplete; + return isSendComplete; } void ListDir::openMasterListFile() diff --git a/mandatory/src/ListeningSocket.cpp b/mandatory/src/ListeningSocket.cpp index fe0db09..c027891 100644 --- a/mandatory/src/ListeningSocket.cpp +++ b/mandatory/src/ListeningSocket.cpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* ListeningSocket.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: eavedill +#+ +:+ +#+ */ +/* By: eavedill +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/30 13:51:47 by eavedill #+# #+# */ -/* Updated: 2024/06/30 13:51:52 by eavedill ### ########.fr */ +/* Updated: 2024/06/30 15:25:07 by eavedill ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,10 +16,7 @@ ListeningSocket::ListeningSocket(int myPort, Server *srv): FileContent() { this->port = myPort; this->server = srv; - // this->client = new Client(srv); this->receiver = new Receive(); - // this->fileContent = new FileContent(); - this->socketFd = -1; if (this->startListening()) { @@ -35,9 +32,7 @@ ListeningSocket::ListeningSocket(int myPort, Server *srv): FileContent() ListeningSocket::ListeningSocket(Server *srv): FileContent() { - // this->client = new Client(srv); this->receiver = new Receive(); - // this->fileContent = new FileContent(); this->server = srv; this->loadErrorPageFromDir(srv->getErrorPage()); this->setIsAutoIndex(srv->getAutoIndex()); @@ -50,7 +45,6 @@ ListeningSocket::~ListeningSocket() { stopListening(); delete this->receiver; - // delete this->fileContent; } bool ListeningSocket::startListening() @@ -61,13 +55,11 @@ bool ListeningSocket::startListening() std::cerr << "Failed to create socket" << std::endl; return false; } - if (fcntl(socketFd, F_SETFL, O_NONBLOCK) < 0) { std::cerr << CHR_RED"Error" << std::endl; exit(1); } - int enable = 1; setsockopt(socketFd, SOL_SOCKET, SO_KEEPALIVE, &enable, sizeof(enable)); if (setsockopt(socketFd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0) @@ -75,21 +67,15 @@ bool ListeningSocket::startListening() std::cerr << "setsockopt(SO_REUSEADDR) failed" << std::endl; exit(1); } - - // Set up the server address sockaddr_in serverAddress; serverAddress.sin_family = AF_INET; serverAddress.sin_addr.s_addr = this->server->getHostAddr(); serverAddress.sin_port = htons(port); - - // Bind the socket to the server address if (bind(socketFd, (struct sockaddr *)&serverAddress, sizeof(serverAddress)) < 0) { printLog("ERROR", "Failed to bind socket to address of port " CHR_RED + toString(port) + RESET); return false; } - - // Start listening for incoming connections if (listen(socketFd, SOMAXCONN) < 0) { std::cerr << "Failed to start listening of port " << port << std::endl; @@ -115,8 +101,6 @@ int ListeningSocket::getFd() bool ListeningSocket::sendData(int clientSocketFd) { ExtendedString answer = this->getAnswerToSend(); - // std::cout << "answer: " << answer.size() << std::endl; - // std::cout << "My answer: " << answer << std::endl; int ret; if ((ret = send(clientSocketFd, answer.c_str(), answer.size(), 0)) < 0) std::cerr << "Failed to write to client" << std::endl; @@ -140,17 +124,13 @@ ListeningSocket *ListeningSocket::clone(int fd) std::string ListeningSocket::getAnswerToSend() { - // long long sendingValue; std::string answer; std::string filePath = this->getFileName(); std::string file_content = this->getContent(); - if (this->getFirstFragment()) { answer = response.generateHeader() + file_content; this->setFirstFragment(false); - // std::cout << "Answer: " << answer.substr(0, 200) << std::endl; - // std::cout << "------------------------------------------------" << std::endl; } else answer = file_content; @@ -178,16 +158,12 @@ 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_KEY, this->request.getPath()); - // // this->cgiModule->setEnv(PROT, this->request.getProtocol()); this->cgiModule->setEnv(SERVER_PORT_KEY, toString(this->port)); 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 servers) @@ -214,6 +190,5 @@ void ListeningSocket::loadRequest(std::vector servers) this->response = Parser.getResponse(); this->setIsAutoIndex(Parser.getIsAutoIndex()); this->setIsCGI(Parser.getIsCGI()); - this->setFileName(this->request.getPath(), Parser.getQuery()); } diff --git a/mandatory/src/Location.cpp b/mandatory/src/Location.cpp index 1d8e44a..68fc9b8 100644 --- a/mandatory/src/Location.cpp +++ b/mandatory/src/Location.cpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* Location.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: eavedill +#+ +:+ +#+ */ +/* By: eavedill +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/29 17:38:18 by eavedill #+# #+# */ -/* Updated: 2024/06/30 13:51:43 by eavedill ### ########.fr */ +/* Updated: 2024/06/30 15:24:13 by eavedill ### ########.fr */ /* */ /* ************************************************************************** */ @@ -47,7 +47,6 @@ std::map getLocationMethods return locationMethods; } - Location::Location() { name = ""; @@ -63,6 +62,7 @@ Location::Location() isCookie = false; cookiesStr = ""; } + Location::Location(std::string const &content) { this->isCgi = false; @@ -84,10 +84,8 @@ Location::Location(const Location& other) setAlias(other.alias); } - Location::~Location() {} -// Getter methods const std::string &Location::getName() const { return name; } const std::string &Location::getRoot() const { return root; } const std::string &Location::getReturn() const { return return_; } @@ -103,7 +101,6 @@ enum LocationType Location::getLocationType() { return this->LocationType; } bool Location::getIsCgi() const { return isCgi; } bool Location::getIsCookie() const { return isCookie; } std::vector Location::getCookies() const { return cookies; } -// Setter methods void Location::setName(const std::string &n) { name = n; } void Location::setRoot(const std::string &r) { root = r; } void Location::setReturn(const std::string &ret) { return_ = ret; } @@ -111,11 +108,10 @@ 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::setCgiEnabledStr(const std::string &cgiEnabled) { - this->cgiEnabledStr = cgiEnabled; -} +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") @@ -131,6 +127,7 @@ bool Location::setCgiEnabled() isCgi = false; return isCgi; } + void Location::setCgiExtensionStr(const std::string &extensions) { this->cgiExtensionStr = extensions; } void Location::setAllowMethods(const std::string& methods) @@ -141,7 +138,7 @@ void Location::setAllowMethods(const std::string& methods) { if (line.length() == 0) continue; - if (line == "GET")// || line == "POST" || line == "DELETE") + if (line == "GET") { this->allowMethods.push_back(line); this->isGetAllowed = true; @@ -178,14 +175,12 @@ void Location ::setCgiExtension(const std::string &extensions) } } -// Load data from a string configuration int Location::loadData(const std::string &content) { std::string start_string = STR_START; std::string line; std::string straux; std::map varnames = var_names_location(); - std::istringstream fileContentStream(content); while (std::getline(fileContentStream, line, ';')) { @@ -252,7 +247,6 @@ void Location::checkVariables(bool serverAutoIndex) if (!Parser::checkCgi(this->cgiExtensionStr)) exit(1); } - switch (Parser::checkRootAliasReturn(this->root, this->alias,this->return_)) { case ROOT: @@ -273,7 +267,6 @@ void Location::checkVariables(bool serverAutoIndex) default: break; } - switch (Parser::checkAutoIndex(this->getAutoIndexStr())) { case true: @@ -291,7 +284,6 @@ void Location::checkVariables(bool serverAutoIndex) this->autoindex = false; break; } - this->setAllowMethods(this->allowMethodsStr); if (!Parser::checkAllowedMethods(this->allowMethodsStr)) this->isGetAllowed = true; @@ -307,20 +299,10 @@ void Location::checkVariables(bool serverAutoIndex) } else if (this->isCookie && !this->cookiesStr.empty()) this->setCookies(this->cookiesStr); - -// if (this->isSessionId && this->sessionIdStr.empty()) -// { -// printLog("WARNING", "set-sesion-id\t\t\tnot defined."); -// this->isSessionId = false; -// } -// else if (this->isSessionId && !this->sessionIdStr.empty()) -// { - -// } } - std::vector Location::getCgiExtension() - { - return this->cgiExtension; - } +std::vector Location::getCgiExtension() +{ + return this->cgiExtension; +} \ No newline at end of file diff --git a/mandatory/src/LocationParser.cpp b/mandatory/src/LocationParser.cpp index 1e5e173..375ca00 100644 --- a/mandatory/src/LocationParser.cpp +++ b/mandatory/src/LocationParser.cpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* LocationParser.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: eavedill +#+ +:+ +#+ */ +/* By: eavedill +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/30 13:51:38 by eavedill #+# #+# */ -/* Updated: 2024/06/30 13:51:38 by eavedill ### ########.fr */ +/* Updated: 2024/06/30 15:22:31 by eavedill ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,8 +16,6 @@ static std::string getMimeType(std::string contentType) { size_t point = contentType.find_last_of("."); std::string extension = contentType.substr(point + 1, contentType.size()); - - /* Create once only */ std::map Mimetype = create_filetypes(); if (Mimetype.find(extension) != Mimetype.end()) return(Mimetype[extension]); @@ -62,7 +60,6 @@ int LocationParser::isAllowedMethod(Location *location) response.setStatus("405 Method Not Allowed"); return NOT_ALLOWED; } - } else if (this->request.getMethod() == "DELETE") { @@ -84,7 +81,6 @@ int LocationParser::matchingLocation() { if (isAllowedMethod(locations[i]) == NOT_ALLOWED) return NOT_ALLOWED; - std::string rawPath = this->request.getPath(); this->isAutoIndex = locations[i]->getAutoIndex(); if ((this->isCookie = locations[i]->getIsCookie()) == true) @@ -96,7 +92,6 @@ int LocationParser::matchingLocation() extension = rawPath.substr(rawPath.rfind(".") + 1, rawPath.size()); for (size_t y = 0; y < locations[i]->getCgiExtension().size(); y++) { - if (locations[i]->getCgiExtension()[y] == extension) this->isCGI = true; } @@ -127,7 +122,6 @@ int LocationParser::matchingLocation() } } return NO_LOCATION; - } off_t getFileSize(const std::string &filename) @@ -165,7 +159,6 @@ bool isBadRequest(std::string request) std::istringstream iss(request); std::string line; bool firstLine = true; - while (std::getline(iss, line)) { if (firstLine) @@ -247,12 +240,11 @@ void LocationParser::checks() case REDIRECT: return ; } - this->request.setPath(decodeURL(this->request.getPath())); ExtendedString tmp = this->request.getPath(); tmp.replaceFirstString("//", "/"); this->request.setPath(tmp); - if (isBadRequest(receiver->getRequest()))//|| isURIMalformed(this->request.getPath()) + if (isBadRequest(receiver->getRequest())) { response.setStatus("400 Bad Request"); throw BAD_REQUEST_CODE; @@ -280,31 +272,6 @@ void LocationParser::checks() { if (getMimeType(this->request.getPath()).find("video") != std::string::npos) { - // std::string attr = request.getAttribute("Range"); - // int tt = isFilePermissions(this->request.getPath(), F_OK | R_OK); - // (void)tt; - // if (isFilePermissions(this->request.getPath(), F_OK | R_OK) == 1 && request.getAttribute("Range") != "") - // { - - // response.setStatus("206 Partial Content"); - // response.setContentType(this->request.getPath()); - - // if (this->request.getAttribute("Range") != "") - // { - // this->startRange = stringToSizeT(this->request.getAttribute("Range").substr(6, this->request.getAttribute("Range").find("-"))); - // std::cout << "STARTRANGE " << this->startRange << std::endl; - // std::string endRangeStr = this->request.getAttribute("Range").substr(this->request.getAttribute("Range").find("-") + 1, this->request.getAttribute("Range").size()); - // std::cout << "ENDRANGE " << endRangeStr << std::endl; - // if (endRangeStr.empty()) - // { - // this->endRange = getFileSize(this->request.getPath()); - // } - // else - // { - // this->endRange = stringToSizeT(endRangeStr); - // } - // } - // } if (isFilePermissions(this->request.getPath(), F_OK | R_OK) == 1) { response.setContentType(this->request.getPath()); @@ -335,7 +302,6 @@ void LocationParser::checks() } else if (this->request.getMethod() == "POST") { - std::string body = receiver->getBody(); if (body.size() > (size_t)this->server->getMaxClientBodySize()) { @@ -386,8 +352,6 @@ void LocationParser::checks() else if (receiver->getisform()) { this->query = decodeURL(body); - std::cout << this->query << std::endl; - response.setStatus("201 Created"); throw CREATED_CODE; } @@ -435,8 +399,6 @@ LocationParser::LocationParser(Header request_, Server *server_, Receive *receiv LocationParser::~LocationParser() { - - } Header LocationParser::getResponse() diff --git a/mandatory/src/Parser.cpp b/mandatory/src/Parser.cpp index 7f96f59..016384c 100644 --- a/mandatory/src/Parser.cpp +++ b/mandatory/src/Parser.cpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* Parser.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: eavedill +#+ +:+ +#+ */ +/* By: eavedill +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/30 13:51:31 by eavedill #+# #+# */ -/* Updated: 2024/06/30 13:51:31 by eavedill ### ########.fr */ +/* Updated: 2024/06/30 15:20:52 by eavedill ### ########.fr */ /* */ /* ************************************************************************** */ @@ -60,13 +60,11 @@ in_addr_t Parser::isValidHost(std::string hostname) struct addrinfo hints, *res; struct sockaddr_in *addr; in_addr_t in_addr; - std::memset(&hints, 0, sizeof(hints)); std::memset(&in_addr, 0, sizeof(in_addr)); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; - int status = getaddrinfo(hostname.c_str(), NULL, &hints, &res); if (status != 0) return 0; @@ -106,8 +104,6 @@ bool Parser::checkErrorPage(std::string errorPage) printLog("WARNING", "error_page\t<" + errorPage + ">\tnot a valid directory." ); return false; } - - std::vector foundFiles; DIR *errorDirectory = opendir(errorPage.c_str()); struct dirent *entry; @@ -122,13 +118,10 @@ bool Parser::checkErrorPage(std::string errorPage) break; } } - } closedir(errorDirectory); - std::sort(foundFiles.begin(), foundFiles.end()); std::cout << CHR_GREEN << getLocalTime() << " [" << "NOTICE" << "]" RESET << "\t\terror_pages found\t" << RESET ; - if (foundFiles.size() == 0) { printLog("WARNING", "error_page\t<" + errorPage + ">\tno error files found." ); @@ -177,7 +170,6 @@ bool Parser::checkIndex(std::string index, std::string root) { if (index.length() == 0) return true; - switch (isFilePermissions(root + "/" + index, R_OK)) { case -1: @@ -198,7 +190,7 @@ bool Parser::checkIndex(std::string index, std::string root) return true; } -const long long MAX_BYTES = 1099511627776; // 1 Terabyte for example +const long long MAX_BYTES = 1099511627776; long long convertToBytes(const std::string& sizeStr) { @@ -206,7 +198,6 @@ long long convertToBytes(const std::string& sizeStr) while (pos < sizeStr.size() && (std::isdigit(sizeStr[pos]) || sizeStr[pos] == '.')) { pos++; } - if (pos == 0) { throw std::invalid_argument("Invalid size format"); } @@ -217,13 +208,11 @@ long long convertToBytes(const std::string& sizeStr) for (size_t i = 0; i < unit.size(); ++i) { unit[i] = std::tolower(unit[i]); } - const long long BYTES_IN_KB = 1024; const long long BYTES_IN_MB = BYTES_IN_KB * 1024; const long long BYTES_IN_GB = BYTES_IN_MB * 1024; const long long BYTES_IN_TB = BYTES_IN_GB * 1024; const long long BITS_IN_BYTE = 8; - long long bytes; if (unit.empty() || unit == "b") { bytes = static_cast(sizeValue); @@ -248,11 +237,9 @@ long long convertToBytes(const std::string& sizeStr) } else { throw std::invalid_argument("Unknown size unit"); } - if (bytes > MAX_BYTES) { throw std::overflow_error("Size exceeds the maximum allowed bytes"); } - return bytes; } @@ -287,7 +274,6 @@ long long Parser::checkClientBodySize(std::string maxClientBodySize) return nbr; } - bool Parser::checkAutoIndex(std::string autoindex) { if (autoindex.length() == 0) @@ -372,7 +358,6 @@ bool Parser::checkCgi(std::string cgiExtension) printLog("ERROR", "cgi_extension\t\t\tis not defined."); return false; } - return true; } diff --git a/mandatory/src/Recieve.cpp b/mandatory/src/Recieve.cpp index 8305453..2004ef7 100644 --- a/mandatory/src/Recieve.cpp +++ b/mandatory/src/Recieve.cpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* Recieve.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: eavedill +#+ +:+ +#+ */ +/* By: eavedill +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/30 13:51:26 by eavedill #+# #+# */ -/* Updated: 2024/06/30 13:51:26 by eavedill ### ########.fr */ +/* Updated: 2024/06/30 15:19:01 by eavedill ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,12 +19,10 @@ Receive::Receive() : buffer(""), request(""), body(""), isbody(false), maxSize(0), sizeSent(0), isform(false) { - // std::cerr << "Receive created" << std::endl; } Receive::~Receive() { - // std::cerr << "Receive destroyed" << std::endl; } Receive::Receive(Receive const ©) @@ -43,13 +41,11 @@ Receive &Receive::operator=(Receive const ©) return *this; } - /* Setters */ void Receive::setBuffer(std::string buffer) { this->buffer = buffer; } void Receive::setRequest(std::string request) { this->request = request; } void Receive::setBody(std::string body) { this->body = body; } void Receive::setmaxSize(size_t size) { this->maxSize = size; } - /* Getters */ std::string Receive::getBuffer() { return this->buffer; } std::string Receive::getRequest() { return this->request; } std::string Receive::getBody() { return this->body; } @@ -114,7 +110,7 @@ bool Receive::receiveHeader(int fd) std::memset(buf, 0, MAX_MSG_SIZE); } - if (ret < 0) // This is not handle as an error + if (ret < 0) return false; else if (ret == 0) { @@ -128,7 +124,6 @@ bool Receive::receiveBody(int fd) { char buf[MAX_MSG_SIZE] = {0}; int ret = 0; - while ((ret = recv(fd, buf, MAX_MSG_SIZE, 0)) > 0) { this->sizeSent += ret; diff --git a/mandatory/src/Server.cpp b/mandatory/src/Server.cpp index 7878fea..4962fb8 100644 --- a/mandatory/src/Server.cpp +++ b/mandatory/src/Server.cpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* Server.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: eavedill +#+ +:+ +#+ */ +/* By: eavedill +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/30 13:51:20 by eavedill #+# #+# */ -/* Updated: 2024/06/30 13:51:20 by eavedill ### ########.fr */ +/* Updated: 2024/06/30 15:17:30 by eavedill ### ########.fr */ /* */ /* ************************************************************************** */ @@ -66,11 +66,13 @@ Server::~Server() delete this->locations[i]; } -Server::Server(Server const ©) { +Server::Server(Server const ©) +{ *this = copy; } -Server &Server::operator=(Server const ©) { +Server &Server::operator=(Server const ©) +{ if (this != ©) { this->isDefault = copy.isDefault; this->port = copy.port; @@ -85,11 +87,11 @@ Server &Server::operator=(Server const ©) { return *this; } -int Server::loadData(std::string const &content) { +int Server::loadData(std::string const &content) +{ std::string line; std::string straux; std::map varnames = var_names_server(); - std::istringstream fileContentStream(content.substr(8, content.length() - 1)); while (std::getline(fileContentStream, line,';')) { @@ -154,7 +156,6 @@ void Server::createListeningSockets() } } - void Server::checkVariables() { if (Parser::checkPorts(this->getPorts()) == false) @@ -185,6 +186,5 @@ void Server::checkVariables() printLog("NOTICE", "OK! Location " + toString(i + 1)); std::cout << CHR_MGENTA"------------------------------" RESET << std::endl; } - std::cout << std::endl; } \ No newline at end of file diff --git a/mandatory/src/ServerSetters.cpp b/mandatory/src/ServerSetters.cpp index 80c2565..ca4de88 100644 --- a/mandatory/src/ServerSetters.cpp +++ b/mandatory/src/ServerSetters.cpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* ServerSetters.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: eavedill +#+ +:+ +#+ */ +/* By: eavedill +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/30 13:51:10 by eavedill #+# #+# */ -/* Updated: 2024/06/30 13:51:11 by eavedill ### ########.fr */ +/* Updated: 2024/06/30 15:16:21 by eavedill ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,9 +21,7 @@ void Server::setRoot(std::string const &root) { this->root = root; } void Server::setIndex(std::string const &index) { this->index = index; } void Server::setCGIFolder(std::string const &str) { this->cgiModule->setCGIFolder(str); } void Server::addLocation(std::string const &content) { this->locations.push_back(new Location(content)); } - void Server::setHostAddr(in_addr_t Addr) { this->hostAddr = Addr; } - void Server::setCGIExtension(std::string const &str) { this->cgiModule->setCGIMapExtensions(str); @@ -75,7 +73,6 @@ void Server::setPorts(std::string const &portsLine) { this->ports.push_back(toString(i)); } - } else { diff --git a/mandatory/src/StatusCode.cpp b/mandatory/src/StatusCode.cpp index 98b3fac..7a13dfd 100644 --- a/mandatory/src/StatusCode.cpp +++ b/mandatory/src/StatusCode.cpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* StatusCode.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: eavedill +#+ +:+ +#+ */ +/* By: eavedill +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/30 13:51:07 by eavedill #+# #+# */ -/* Updated: 2024/06/30 13:51:08 by eavedill ### ########.fr */ +/* Updated: 2024/06/30 15:15:28 by eavedill ### ########.fr */ /* */ /* ************************************************************************** */ @@ -77,7 +77,6 @@ void StatusCode::setFileContentForStatusCode(int CodeNumber, const std::string & ExtendedString content(""); std::ifstream fileStream; fileStream.open(CodeFileContent.c_str(), std::ios::out | std::ios::binary); //| std::ios::app | std::ios::ate - if(fileStream.is_open()) { char buffer[MAX_SENT_BYTES]; @@ -89,7 +88,6 @@ void StatusCode::setFileContentForStatusCode(int CodeNumber, const std::string & { printLog("WARNING", CodeFileContent + " File does not exist.\t Set error page to default " CHR_GREEN "GET" RESET " value"); } - mapCodesFileContent[CodeNumber] = content; } @@ -132,7 +130,6 @@ void StatusCode::loadErrorPageFromDir(const ExtendedString &dir) } while ((dirp = readdir(dp)) != NULL) { - // std::cout << "dir " << dir << std::endl; if (dirp->d_type == DT_REG) { std::string fileName = dir + "/" + dirp->d_name; diff --git a/mandatory/src/WebServer.cpp b/mandatory/src/WebServer.cpp index b1ddb83..d572a49 100644 --- a/mandatory/src/WebServer.cpp +++ b/mandatory/src/WebServer.cpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* WebServer.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: eavedill +#+ +:+ +#+ */ +/* By: eavedill +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/30 13:50:54 by eavedill #+# #+# */ -/* Updated: 2024/06/30 13:50:59 by eavedill ### ########.fr */ +/* Updated: 2024/06/30 15:14:05 by eavedill ### ########.fr */ /* */ /* ************************************************************************** */ @@ -65,7 +65,6 @@ void WebServer::launchServers() { std::cout << CHR_YELLOW "Launching servers..." RESET << std::endl << std::endl; std::cout << "\e[4;37m Date & time \t\t\t\t\t\t\tport\tfd" << RESET << std::endl; - this->createListeningSockets(); std::cout << std::endl; try diff --git a/mandatory/src/WebServerLNX.cpp b/mandatory/src/WebServerLNX.cpp index 01b544d..cfb6abb 100644 --- a/mandatory/src/WebServerLNX.cpp +++ b/mandatory/src/WebServerLNX.cpp @@ -31,7 +31,7 @@ void WebServer::addEventSet() { struct epoll_event event; memset(&event, 0, sizeof(event)); - event.events = EPOLLIN | EPOLLET; // Edge-triggered mode + event.events = EPOLLIN | EPOLLET; event.data.fd = serverFds[j]; if (epoll_ctl(this->kq, EPOLL_CTL_ADD, serverFds[j], &event) == -1) { @@ -50,8 +50,6 @@ int WebServer::waitEvent(struct epoll_event *evList) void WebServer::modifEvent(struct epoll_event eventList, int type) { - // struct epoll_event evSet; - eventList.events = type; if (epoll_ctl(this->kq, EPOLL_CTL_MOD, eventList.data.fd, &eventList) == -1) { @@ -95,7 +93,6 @@ int WebServer::acceptNewEvent(int curfd) std::cerr << "Error listening" << std::endl; exit(1); } - while (1) { struct sockaddr_storage addr; @@ -117,5 +114,4 @@ int WebServer::acceptNewEvent(int curfd) } return fd; } - #endif diff --git a/mandatory/src/WebServerMAC.cpp b/mandatory/src/WebServerMAC.cpp index 7fde14d..cdbd5a9 100644 --- a/mandatory/src/WebServerMAC.cpp +++ b/mandatory/src/WebServerMAC.cpp @@ -30,8 +30,6 @@ void WebServer::addEventSet() for (size_t j = 0; j < serverFds.size(); j++) { struct kevent evSet; - - // EV_SET(&evSet, serverFds[j], EVFILT_READ, EV_ADD | EV_CLEAR, NOTE_WRITE, 0, NULL); EV_SET(&evSet, serverFds[j], EVFILT_READ, EV_ADD | EV_CLEAR, 0, 0, NULL); if (kevent(this->kq, &evSet, 1, NULL, 0, NULL) == -1) { @@ -53,7 +51,6 @@ void WebServer::modifEvent(struct kevent event, int typeRem, int typeAdd) struct kevent evSet; (void) typeRem; EV_SET(&evSet, event.ident, typeAdd, EV_ADD, 0, 0, NULL); - //removeEventFd(event.ident, typeRem); if (kevent(this->kq, &evSet, 1, NULL, 0, NULL) == -1) { std::cerr << "Error: could not add event" << std::endl; @@ -100,7 +97,7 @@ int WebServer::acceptNewEvent(int curfd) else { std::cerr << "Error accepting connection" << std::endl; - return fd; // Continue to the next event + return fd; } } fcntl(curfd, F_SETFL, O_NONBLOCK, FD_CLOEXEC); @@ -111,5 +108,4 @@ int WebServer::acceptNewEvent(int curfd) } return fd; } - #endif diff --git a/mandatory/src/WebservParser.cpp b/mandatory/src/WebservParser.cpp index 8095250..7c44a3a 100644 --- a/mandatory/src/WebservParser.cpp +++ b/mandatory/src/WebservParser.cpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* WebservParser.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: eavedill +#+ +:+ +#+ */ +/* By: eavedill +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/30 13:50:32 by eavedill #+# #+# */ -/* Updated: 2024/06/30 13:50:39 by eavedill ### ########.fr */ +/* Updated: 2024/06/30 15:11:42 by eavedill ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,7 +18,6 @@ std::string removeComments(std::string &str) std::string aux; std::istringstream fileContentStream(str); std::string serverContentConfig = ""; - while (std::getline(fileContentStream, line)) { line = line.substr(0, line.find_first_of("#")); @@ -57,10 +56,8 @@ static bool mismatchedBraces(std::string fileString) static bool endWithSemicolon(std::string &fileString) { std::string line; - fileString = removeComments(fileString); fileString = removeBlanksAndTabs(fileString); - std::istringstream fileStream(fileString); while (std::getline(fileStream, line)) { @@ -94,7 +91,6 @@ static bool hasDoublePoints(std::string &fileString) std::istringstream fileStream(fileString); while (std::getline(fileStream, line)) { - if ((line.length() == 0 || line == "{" || line == "}" || line == "};") || (!line.empty() && (line.find(":") != std::string::npos))) continue; else @@ -110,16 +106,12 @@ bool WebServer::checkSyntax() { if (!isServerBlock(this->configFileString)) return false; - if (!mismatchedBraces(this->configFileString)) return false; - if (!endWithSemicolon(this->configFileString)) return false; - if (!hasDoublePoints(this->configFileString)) return false; - return true; } @@ -163,7 +155,6 @@ void WebServer::processConfigFile() while (this->configFileString.find("server:{") != std::string::npos) { - configFileString.erase(std::remove(configFileString.begin(), configFileString.end(), '\n'), configFileString.end()); pos = this->configFileString.find("server:{", 8); if(pos == std::string::npos) @@ -187,11 +178,9 @@ bool WebServer::parseInfo() { std::cout << CHR_CYAN"-------------Checking Server [" << i + 1 << "]------------" RESET<< std::endl; this->servers[i]->checkVariables(); - // this->servers[i]->print(); // print all Server parameters for debug printLog("NOTICE", "OK! Server " + toString(i + 1)); std::cout << CHR_CYAN"--------------------------------------------" RESET<< std::endl; } - std::vector ports; for (size_t i = 0; i < this->servers.size(); i++) { diff --git a/mandatory/src/main.cpp b/mandatory/src/main.cpp index 5e71001..2f4c189 100644 --- a/mandatory/src/main.cpp +++ b/mandatory/src/main.cpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* main.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: eavedill +#+ +:+ +#+ */ +/* By: eavedill +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/30 13:51:34 by eavedill #+# #+# */ -/* Updated: 2024/06/30 13:51:34 by eavedill ### ########.fr */ +/* Updated: 2024/06/30 15:21:02 by eavedill ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,7 +23,6 @@ int main(int av, char **ac) webServer.loadConfigFile(ac[1]); if (!webServer.checkSyntax()) return 1; - webServer.processConfigFile(); if (!webServer.parseInfo()) return 1; diff --git a/mandatory/src/utils.cpp b/mandatory/src/utils.cpp index e5acde7..cbefb01 100644 --- a/mandatory/src/utils.cpp +++ b/mandatory/src/utils.cpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* utils.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: eavedill +#+ +:+ +#+ */ +/* By: eavedill +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/30 13:51:03 by eavedill #+# #+# */ -/* Updated: 2024/06/30 13:51:03 by eavedill ### ########.fr */ +/* Updated: 2024/06/30 15:15:09 by eavedill ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,7 +40,6 @@ bool validIPAddress(std::string ip) size_t pos = 0; std::string token; int count = 0; - while ((pos = ip.find('.')) != std::string::npos) { token = ip.substr(0, pos); @@ -49,10 +48,8 @@ bool validIPAddress(std::string ip) ip.erase(0, pos + 1); count++; } - if (count != 4 || !isNumber(ip) || !isrange(std::atoi(ip.c_str()), 0, 255)) return false; - return count == 4; } @@ -87,7 +84,8 @@ int isFilePermissions(std::string path, int mode) return 1; } -size_t stringToSizeT(const std::string& str) { +size_t stringToSizeT(const std::string& str) +{ std::stringstream ss(str); size_t result = 0; ss >> result; @@ -98,14 +96,10 @@ size_t stringToSizeT(const std::string& str) { std::vector splitString(const std::string& str, char delimiter) { std::vector result; std::string token; - // std::cout << "str: " << str << std::endl; std::istringstream tokenStream(str); - while (std::getline(tokenStream, token, delimiter)) { - // if (token != "") - result.push_back(token); + result.push_back(token); } - return result; } @@ -129,7 +123,8 @@ std::string removeBlanksAndTabs(const std::string& input) { return result; } -void replaceString(std::string& mainString, const std::string& searchString, const std::string& replaceString) { +void replaceString(std::string& mainString, const std::string& searchString, const std::string& replaceString) +{ size_t pos = 0; while ((pos = mainString.find(searchString, pos)) != std::string::npos) { mainString.replace(pos, searchString.length(), replaceString); @@ -137,7 +132,8 @@ void replaceString(std::string& mainString, const std::string& searchString, con } } -int count_chars(const std::string& str, char c) { +int count_chars(const std::string& str, char c) +{ int count = 0; for (size_t i = 0; i < str.size(); i++) { if (str[i] == c) { @@ -150,11 +146,9 @@ int count_chars(const std::string& str, char c) { std::string getLocalTime() { std::time_t currentTime = std::time(NULL); - std::tm* timeInfo = std::localtime(¤tTime); char buffer[20]; std::strftime(buffer, sizeof(buffer), "%Y/%m/%d %H:%M:%S", timeInfo); - return std::string(buffer); } @@ -173,10 +167,8 @@ void printLog(std::string type ,std::string message) static int hexStringToInt(const std::string& hexStr) { int value; std::stringstream ss; - ss << std::hex << hexStr; ss >> value; - return value; } @@ -203,7 +195,8 @@ std::string decodeURL(const std::string& url) return decoded; } -void replaceFirstString(std::string& mainString, const std::string& searchString, const std::string& replaceString) { +void replaceFirstString(std::string& mainString, const std::string& searchString, const std::string& replaceString) +{ size_t pos = 0; if ((pos = mainString.find(searchString, pos)) != std::string::npos) { mainString.replace(pos, searchString.length(), replaceString); @@ -218,8 +211,6 @@ std::string getRandomHash(int lenght) std::string hash; int x; std::srand(static_cast(std::time(0)) ^ reinterpret_cast(&x)); - - // std::srand(static_cast(std::time(0)) ^ static_cast(getpid())); for (int i = 0 ; i < lenght; i++) hash += characters[rand() % characters.size()]; return hash; From 40edf39d535bd3c41b5874bb8b64e3476cfc7722 Mon Sep 17 00:00:00 2001 From: "ernesto.avedillo" Date: Sun, 30 Jun 2024 15:40:45 +0200 Subject: [PATCH 30/31] copy port number in clone --- mandatory/src/ListeningSocket.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mandatory/src/ListeningSocket.cpp b/mandatory/src/ListeningSocket.cpp index c027891..b9c7c82 100644 --- a/mandatory/src/ListeningSocket.cpp +++ b/mandatory/src/ListeningSocket.cpp @@ -6,7 +6,7 @@ /* By: eavedill +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/30 13:51:47 by eavedill #+# #+# */ -/* Updated: 2024/06/30 15:25:07 by eavedill ### ########.fr */ +/* Updated: 2024/06/30 15:38:42 by eavedill ### ########.fr */ /* */ /* ************************************************************************** */ @@ -118,6 +118,7 @@ bool ListeningSocket::receive() ListeningSocket *ListeningSocket::clone(int fd) { ListeningSocket *newSocket = new ListeningSocket(this->server); + newSocket->port = this->port; newSocket->socketFd = fd; return newSocket; } From f17db99eb8418af08a21947a32125a3e8bd81dcf Mon Sep 17 00:00:00 2001 From: Joseph Cheel nakashima Date: Sun, 30 Jun 2024 16:11:14 +0200 Subject: [PATCH 31/31] post short body fixed --- mandatory/inc/LocationParser.hpp | 2 - mandatory/src/LocationParser.cpp | 89 +++----------------------------- mandatory/src/Recieve.cpp | 5 +- mandatory/src/WebservParser.cpp | 2 +- 4 files changed, 9 insertions(+), 89 deletions(-) diff --git a/mandatory/inc/LocationParser.hpp b/mandatory/inc/LocationParser.hpp index f845ccc..cf1310c 100644 --- a/mandatory/inc/LocationParser.hpp +++ b/mandatory/inc/LocationParser.hpp @@ -53,7 +53,5 @@ class LocationParser bool getIsCGI(); void checks(); void setCookies(); - size_t getStartRange(); - size_t getEndRange(); std::string getQuery(); }; \ No newline at end of file diff --git a/mandatory/src/LocationParser.cpp b/mandatory/src/LocationParser.cpp index e982ab5..4a975d7 100644 --- a/mandatory/src/LocationParser.cpp +++ b/mandatory/src/LocationParser.cpp @@ -13,25 +13,11 @@ static std::string getMimeType(std::string contentType) return ("text/html"); } -bool LocationParser::getIsAutoIndex() -{ - return this->isAutoIndex; -} - -bool LocationParser::getIsCGI() -{ - return this->isCGI; -} - -size_t LocationParser::getStartRange() -{ - return this->startRange; -} - -size_t LocationParser::getEndRange() -{ - return this->endRange; -} +bool LocationParser::getIsAutoIndex() { return this->isAutoIndex; } +bool LocationParser::getIsCGI(){ return this->isCGI; } +Header LocationParser::getResponse() { return this->response; } +Header LocationParser::getRequest() { return this->request; } +std::string LocationParser::getQuery() { return this->query; } int LocationParser::isAllowedMethod(Location *location) { @@ -125,27 +111,6 @@ off_t getFileSize(const std::string &filename) return rc == 0 ? stat_buf.st_size : -1; } -std::string escapeNonPrintableChars(const std::string& str) { - std::string escapedStr; - for (size_t i = 0; i < str.size(); ++i) { - switch (str[i]) { - case '\n': - escapedStr += "\\n"; - break; - case '\r': - escapedStr += "\\r"; - break; - case '\t': - escapedStr += "\\t"; - break; - default: - escapedStr += str[i]; - break; - } - } - return escapedStr; -} - bool isBadRequest(std::string request) { if (request.empty()) @@ -268,31 +233,6 @@ void LocationParser::checks() { if (getMimeType(this->request.getPath()).find("video") != std::string::npos) { - // std::string attr = request.getAttribute("Range"); - // int tt = isFilePermissions(this->request.getPath(), F_OK | R_OK); - // (void)tt; - // if (isFilePermissions(this->request.getPath(), F_OK | R_OK) == 1 && request.getAttribute("Range") != "") - // { - - // response.setStatus("206 Partial Content"); - // response.setContentType(this->request.getPath()); - - // if (this->request.getAttribute("Range") != "") - // { - // this->startRange = stringToSizeT(this->request.getAttribute("Range").substr(6, this->request.getAttribute("Range").find("-"))); - // std::cout << "STARTRANGE " << this->startRange << std::endl; - // std::string endRangeStr = this->request.getAttribute("Range").substr(this->request.getAttribute("Range").find("-") + 1, this->request.getAttribute("Range").size()); - // std::cout << "ENDRANGE " << endRangeStr << std::endl; - // if (endRangeStr.empty()) - // { - // this->endRange = getFileSize(this->request.getPath()); - // } - // else - // { - // this->endRange = stringToSizeT(endRangeStr); - // } - // } - // } if (isFilePermissions(this->request.getPath(), F_OK | R_OK) == 1) { response.setContentType(this->request.getPath()); @@ -374,10 +314,8 @@ void LocationParser::checks() else if (receiver->getisform()) { this->query = decodeURL(body); - std::cout << this->query << std::endl; - response.setStatus("201 Created"); - throw CREATED_CODE; + return ; } response.setServer(server->getServerName()); } @@ -425,19 +363,4 @@ LocationParser::~LocationParser() { -} - -Header LocationParser::getResponse() -{ - return this->response; -} - -Header LocationParser::getRequest() -{ - return this->request; -} - -std::string LocationParser::getQuery() -{ - return this->query; } \ No newline at end of file diff --git a/mandatory/src/Recieve.cpp b/mandatory/src/Recieve.cpp index 6c7f646..845716c 100644 --- a/mandatory/src/Recieve.cpp +++ b/mandatory/src/Recieve.cpp @@ -86,10 +86,9 @@ bool Receive::receiveHeader(int fd) this->isform = true; else { - this->body += this->buffer.substr(0, this->buffer.rfind(this->boundary) - 4); - this->body = this->body.substr(0, this->body.find(this->boundary) - 4); - this->postHeader = this->body.substr(this->body.find(this->boundary) + this->boundary.size() + 4, this->body.find("\r\n\r\n")); + this->postHeader = this->body.substr(this->boundary.size() + 4, this->body.find("\r\n\r\n") - this->boundary.size() - 4); this->body = this->body.substr(this->body.find("\r\n\r\n") + 4); + this->body = this->body.substr(0, this->body.rfind(this->boundary) - 2); } this->isbody = false; return true; diff --git a/mandatory/src/WebservParser.cpp b/mandatory/src/WebservParser.cpp index ff18163..74aae4f 100644 --- a/mandatory/src/WebservParser.cpp +++ b/mandatory/src/WebservParser.cpp @@ -59,7 +59,7 @@ static bool endWithSemicolon(std::string &fileString) continue; else { - printLog("ERROR", "Missing semicolon " CHR_YELLOW ":" RESET " at the end: \"" YELLOW + line + RESET"\""); + printLog("ERROR", "Missing semicolon " CHR_YELLOW ";" RESET " at the end: \"" YELLOW + line + RESET"\""); return false; } }