diff --git a/exemples/test.toml b/exemples/test.toml index f5cdb22..d031313 100644 --- a/exemples/test.toml +++ b/exemples/test.toml @@ -12,13 +12,14 @@ port = 8080 [server.location./] methods = { "GET" } -root = "/var/www/html" +root = "/sgoinfre/goinfre/Perso/mmoussou" dirlist = true client_max_body_size = "10M" +index = "banger.html" [server.location./api] methods = { "GET", "POST" } -root = "/var/www/api" +root = "/nfs/homes/mmoussou" upload_path = "/etc/webserv/up" cgi.go = "/bin/go" diff --git a/includes/requests/ARequest.hpp b/includes/requests/ARequest.hpp index ff736fe..925af3f 100644 --- a/includes/requests/ARequest.hpp +++ b/includes/requests/ARequest.hpp @@ -6,13 +6,14 @@ /* By: mmoussou +#include #include #include #include @@ -42,19 +43,20 @@ class ARequest : public http::IMessage { std::string getMethod(void) const; std::string getTarget(void) const; std::string getProtocol(void) const; - config::Server *getConfig(void) const; + webserv::config::Route *getRoute(void) const; URL getUrl() const; void setMethod(std::string const method); void setTarget(std::string const target); void setProtocol(std::string const protocol); void setServer(std::string const protocol); + void setRoute(config::Route *route); protected: std::string _method; std::string _target; std::string _protocol; - config::Server *_conf; + webserv::config::Route *_route; URL *_url; std::string _sanitizeStr(std::string &str) { diff --git a/src/requests_handling/ARequests.cpp b/src/requests_handling/ARequests.cpp index 0ea608e..96e1ab9 100644 --- a/src/requests_handling/ARequests.cpp +++ b/src/requests_handling/ARequests.cpp @@ -6,20 +6,21 @@ /* By: mmoussou -#include -#include #include +#include +#include +#include -#include #include +#include #include -using namespace webserv::http; +using namespace webserv; +using namespace http; std::string ARequest::str(void) const { std::ostringstream response; @@ -62,3 +63,13 @@ URL ARequest::getUrl() const else return URL(""); } + +config::Route *ARequest::getRoute(void) const +{ + return (_route); +} + +void ARequest::setRoute(config::Route *route) +{ + this->_route = route; +} diff --git a/src/requests_handling/requestImplementation/Delete.cpp b/src/requests_handling/requestImplementation/Delete.cpp index 0bef919..b7205c8 100644 --- a/src/requests_handling/requestImplementation/Delete.cpp +++ b/src/requests_handling/requestImplementation/Delete.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/30 09:42:18 by adjoly #+# #+# */ -/* Updated: 2025/04/30 15:17:15 by mmoussou ### ########.fr */ +/* Updated: 2025/05/02 14:53:08 by mmoussou ### ########.fr */ /* */ /* ************************************************************************** */ @@ -68,6 +68,8 @@ void Delete::parse(std::string const &data) { Response Delete::execute(void) { http::Response response; + this->_target = this->_route->getRootDir() + this->_target; + try { if (std::remove(this->_target.c_str())) throw std::runtime_error("can't remove file, FF"); diff --git a/src/requests_handling/requestImplementation/Get.cpp b/src/requests_handling/requestImplementation/Get.cpp index c8421ef..99aed32 100644 --- a/src/requests_handling/requestImplementation/Get.cpp +++ b/src/requests_handling/requestImplementation/Get.cpp @@ -6,14 +6,15 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/30 09:40:16 by adjoly #+# #+# */ -/* Updated: 2025/04/30 15:30:08 by mmoussou ### ########.fr */ +/* Updated: 2025/05/02 14:52:24 by mmoussou ### ########.fr */ /* */ /* ************************************************************************** */ -#include -#include -#include #include +#include +#include +#include +#include #include @@ -87,34 +88,57 @@ char isDirectory(const std::string &path) { Response Get::execute(void) { http::Response response; + this->_target = this->_route->getRootDir() + this->_target; + try { - if (isDirectory(this->_target)) { - DIR *dir; - struct dirent *entry; - struct stat file_stat; - std::vector files; + if (isDirectory(this->_target)) + { + if (!access((this->_target + this->_route->getIndex()).c_str(), R_OK)) + { + this->_target = this->_target + this->_route->getIndex(); - if ((dir = opendir(this->_target.c_str())) == NULL) - throw; - while ((entry = readdir(dir)) != NULL) { - std::string file_name = entry->d_name; - if (file_name == ".") - continue; - std::string file_path = this->_target + "/" + file_name; - if (stat(file_path.c_str(), &file_stat) == 0) { - if (S_ISDIR(file_stat.st_mode)) - files.push_back(file_name + "/"); - else - files.push_back(file_name); - } + std::ifstream file(this->_target.c_str(), std::ios::binary); + std::streampos file_start = file.tellg(); + response.setBody(std::string((std::istreambuf_iterator(file)), + std::istreambuf_iterator())); + std::stringstream length; + length << (file.tellg() - file_start); + response.addHeader("Content-Length", length.str()); + + response.setProtocol(this->_protocol); + response.setStatusCode(200); + response.addHeader("Content-Type", + http::Mime::getType(this->_target)); } - closedir(dir); + else if (this->_route->getDirList()) + { + DIR *dir; + struct dirent *entry; + struct stat file_stat; + std::vector files; - std::sort(files.begin(), files.end()); + if ((dir = opendir(this->_target.c_str())) == NULL) + throw; + while ((entry = readdir(dir)) != NULL) { + std::string file_name = entry->d_name; + if (file_name == ".") + continue; + std::string file_path = this->_target + "/" + file_name; + if (stat(file_path.c_str(), &file_stat) == 0) { + if (S_ISDIR(file_stat.st_mode)) + files.push_back(file_name + "/"); + else + files.push_back(file_name); + } + } - std::string body = ""; + closedir(dir); - body += ""; - body += "
    \n"; - for (size_t i = 0; i < files.size(); i++) - body += "
  • " + files[i] + - "
  • \n"; - body += "
"; + body += "
    \n"; + for (size_t i = 0; i < files.size(); i++) + body += "
  • " + files[i] + + "
  • \n"; + body += "
"; - response.setProtocol(this->_protocol); - response.setStatusCode(200); - std::stringstream length; - length << body.length(); - response.addHeader("Content-Length", length.str()); - response.addHeader("Content-Type", "text/html"); - response.setBody(body); - - } else { + response.setProtocol(this->_protocol); + response.setStatusCode(200); + std::stringstream length; + length << body.length(); + response.addHeader("Content-Length", length.str()); + response.addHeader("Content-Type", "text/html"); + response.setBody(body); + } + else + throw std::runtime_error("dir but no dirlist"); + } + else + { std::ifstream file(this->_target.c_str(), std::ios::binary); std::streampos file_start = file.tellg(); response.setBody(std::string((std::istreambuf_iterator(file)), @@ -161,7 +189,7 @@ body {\n\ response.setProtocol(this->_protocol); response.setStatusCode(200); response.addHeader("Content-Type", - http::Mime::getType(this->_target)); + http::Mime::getType(this->_target)); #ifdef VERBOSE //_log->debug(response.str().c_str()); diff --git a/src/requests_handling/requestImplementation/Post.cpp b/src/requests_handling/requestImplementation/Post.cpp index fc282c3..227ff5f 100644 --- a/src/requests_handling/requestImplementation/Post.cpp +++ b/src/requests_handling/requestImplementation/Post.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/30 09:50:20 by adjoly #+# #+# */ -/* Updated: 2025/04/30 15:18:01 by mmoussou ### ########.fr */ +/* Updated: 2025/05/02 15:02:56 by mmoussou ### ########.fr */ /* */ /* ************************************************************************** */ @@ -70,7 +70,7 @@ void Post::parse(std::string const &data) { std::string extractFilename(const std::string &header) { size_t start = header.find("filename=\"") + 10; size_t end = header.find("\"", start); - return header.substr(start, end - start); + return this->_route->getUpRoot() + header.substr(start, end - start); } void handleMultipartData(const std::string &body, const std::string &boundary) { @@ -104,7 +104,7 @@ void handleMultipartData(const std::string &body, const std::string &boundary) { Response Post::execute(void) { http::Response response; - + try { handleMultipartData( this->_body, diff --git a/src/server/Client.cpp b/src/server/Client.cpp index 74c899a..b3e2a53 100644 --- a/src/server/Client.cpp +++ b/src/server/Client.cpp @@ -6,7 +6,7 @@ /* By: mmoussou whatRoute(URL(this->_request->getTarget())); - if (_route == not_nullptr) { - _log->info("euuh"); - return; - } - /* std::cout << "_route is " << (_route ? "not null" : "NULL") << std::endl; - */ + this->_request->setRoute(_route); + if (!this->_route || this->_route == not_nullptr) { this->_request->setMethod("404"); return; @@ -67,6 +63,7 @@ void Client::parse(void) { if (received_data.length() > (unsigned long)(_route->getMaxBody())) this->_request->setMethod("413"); + } bool Client::requestParsed(void) {