From 41efd2c19effb707313f3e3913acd34c006118f5 Mon Sep 17 00:00:00 2001 From: adjoly Date: Wed, 30 Apr 2025 09:37:21 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D=20wip:?= =?UTF-8?q?=20reformated=20request=20include=20folder?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/config/URL.hpp | 27 +- includes/log.hpp | 8 +- includes/requests/ARequest.hpp | 61 ++++ includes/requests/Cgi.hpp | 7 +- includes/requests/Errors.hpp | 4 +- includes/requests/HttpRequest.hpp | 93 ------ .../{HttpIMessage.hpp => IMessage.hpp} | 0 includes/requests/RequestImplement.hpp | 49 +++ .../{HttpResponse.hpp => Response.hpp} | 31 +- includes/requests/default.hpp | 16 +- .../server/{Resource.hpp => AResource.hpp} | 6 +- src/requests_handling/HttpRequests.cpp | 311 ++++++++---------- src/server/{Resource.cpp => AResource.cpp} | 25 +- src/server/Client.cpp | 7 +- src/server/Server.cpp | 13 +- 15 files changed, 315 insertions(+), 343 deletions(-) create mode 100644 includes/requests/ARequest.hpp delete mode 100644 includes/requests/HttpRequest.hpp rename includes/requests/{HttpIMessage.hpp => IMessage.hpp} (100%) create mode 100644 includes/requests/RequestImplement.hpp rename includes/requests/{HttpResponse.hpp => Response.hpp} (64%) rename includes/server/{Resource.hpp => AResource.hpp} (90%) rename src/server/{Resource.cpp => AResource.cpp} (64%) diff --git a/includes/config/URL.hpp b/includes/config/URL.hpp index 2b4d6ea..00710d7 100644 --- a/includes/config/URL.hpp +++ b/includes/config/URL.hpp @@ -6,13 +6,14 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/22 12:17:48 by adjoly #+# #+# */ -/* Updated: 2025/04/24 14:01:15 by adjoly ### ########.fr */ +/* Updated: 2025/04/29 15:48:19 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #pragma once #include +#include #include #include #include @@ -26,17 +27,21 @@ class URL { return comparePathSegments(other); } - bool operator<(const URL &other) const { return _full_url < other._full_url; } + bool operator<(const URL &other) const { + return _full_url < other._full_url; + } std::vector getSegments(void) { return _path_segments; } - std::string getFullUrl(void) { return _full_url; } - std::string getQueryString(void) const { return _query_string; } + std::string getFullUrl(void) const { return _full_url; } + std::string getQueryString(void) const { return _query_string; } private: void parse() { size_t scheme_pos = _full_url.find("://"); size_t path_start = 0; size_t query_start = _full_url.find('?'); + size_t port_start = _full_url.find(':', scheme_pos + 3); + size_t port_end = _full_url.find('/', port_start); if (scheme_pos != std::string::npos) { path_start = _full_url.find('/', scheme_pos + 3); @@ -47,7 +52,8 @@ class URL { if (query_start != std::string::npos) { _query_string = _full_url.substr(query_start + 1); if (path_start != std::string::npos) { - std::string path = _full_url.substr(path_start, query_start - path_start); + std::string path = + _full_url.substr(path_start, query_start - path_start); splitPath(path, _path_segments); } } else { @@ -56,6 +62,10 @@ class URL { splitPath(path, _path_segments); } } + + if (port_start != std::string::npos && port_end != std::string::npos) { + _port = _full_url.substr(port_start + 1, port_end - port_start - 1); + } } void splitPath(const std::string &path, @@ -83,4 +93,11 @@ class URL { std::string _full_url; std::vector _path_segments; std::string _query_string; + std::string _port; }; + +inline std::ostream &operator<<(std::ostream &os, const URL &URL) { + + os << URL.getFullUrl(); + return os; +} diff --git a/includes/log.hpp b/includes/log.hpp index f17b09a..6d44ca4 100644 --- a/includes/log.hpp +++ b/includes/log.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/20 09:28:27 by adjoly #+# #+# */ -/* Updated: 2025/04/28 14:29:54 by adjoly ### ########.fr */ +/* Updated: 2025/04/29 17:28:36 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,10 +40,10 @@ inline void log(std::string emoji, std::string who, std::string str) { class Logger { public: Logger(void) : _ttyOnly(true) { - //log("➕", "Logger", "default constructor called"); + log("➕", "Logger", "default constructor called"); } Logger(const std::string &fileName) : _fileName(fileName) { - //log("➕", "Logger", "filename constructor called"); + log("➕", "Logger", "filename constructor called"); _file.open(fileName.c_str(), std::ios::app); if (!_file.is_open() && !_ttyOnly) { _ttyOnly = true; @@ -53,7 +53,7 @@ class Logger { } Logger(const Logger &other) : _ttyOnly(other._ttyOnly) { - //log("➕", "Logger", "copy constructor called"); + log("➕", "Logger", "copy constructor called"); if (!other._ttyOnly) { _file.open(other._fileName.c_str(), std::ios::app); if (!_file.is_open()) { diff --git a/includes/requests/ARequest.hpp b/includes/requests/ARequest.hpp new file mode 100644 index 0000000..aa81bd6 --- /dev/null +++ b/includes/requests/ARequest.hpp @@ -0,0 +1,61 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ARequest.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mmoussou +#include +#include +#include + +#include +#include +#include + +#include + +namespace webserv { +namespace http { + +class ARequest : public http::IMessage { + public: + virtual ~ARequest(void) { + log("➖", "ARequest", "destructor called"); + delete _url; + } + + virtual void parse(std::string const &data) = 0; + virtual Response execute(void) = 0; + + std::string str(void) const; + + std::string getMethod(void) const; + std::string getTarget(void) const; + std::string getProtocol(void) const; + config::Server *getConfig(void) 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); + + protected: + std::string _method; + std::string _target; + std::string _protocol; + config::Server *_conf; + URL *_url; +}; + +} // namespace http +} // namespace webserv diff --git a/includes/requests/Cgi.hpp b/includes/requests/Cgi.hpp index 5d61120..c50e473 100644 --- a/includes/requests/Cgi.hpp +++ b/includes/requests/Cgi.hpp @@ -6,14 +6,13 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/24 14:17:34 by adjoly #+# #+# */ -/* Updated: 2025/04/25 13:40:10 by adjoly ### ########.fr */ +/* Updated: 2025/04/30 09:36:02 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #pragma once -#include "requests/HttpIMessage.hpp" -#include "requests/HttpRequest.hpp" +#include #include #include #include @@ -22,7 +21,7 @@ namespace webserv { class Cgi { public: - Cgi(http::IRequest *, config::Server *); + Cgi(http::ARequest *, config::Server *); ~Cgi(void); std::string getEnv(std::string &); diff --git a/includes/requests/Errors.hpp b/includes/requests/Errors.hpp index 83b6fe5..7914465 100644 --- a/includes/requests/Errors.hpp +++ b/includes/requests/Errors.hpp @@ -6,7 +6,7 @@ /* By: mmoussou #include -#include +#include namespace webserv { namespace http { diff --git a/includes/requests/HttpRequest.hpp b/includes/requests/HttpRequest.hpp deleted file mode 100644 index 16f3851..0000000 --- a/includes/requests/HttpRequest.hpp +++ /dev/null @@ -1,93 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* HttpRequest.hpp :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: mmoussou -#include -#include -#include - -#include -#include -#include - -#include - -namespace webserv { -namespace http { - -class IRequest: public http::IMessage { -public: - virtual ~IRequest(void); - - virtual void parse(std::string const &data) = 0; - virtual http::Response execute(void) = 0; - - std::string str(void) const; - - std::string getMethod(void) const; - std::string getTarget(void) const; - std::string getProtocol(void) const; - config::Server *getConfig(void) 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); - -protected: - std::string _method; - std::string _target; - std::string _protocol; - config::Server *_conf; - -}; - -class Get: public http::IRequest { -public: - Get(void); - ~Get(void); - Get(std::string &data); - - void parse(std::string const &data); - - http::Response execute(void); - -}; - -class Post: public http::IRequest { -public: - Post(void); - ~Post(void); - Post(std::string &data); - - void parse(std::string const &data); - - http::Response execute(void); - -}; - -class Delete: public http::IRequest { -public: - Delete(void); - ~Delete(void); - Delete(std::string &data); - - void parse(std::string const &data); - - http::Response execute(void); - -}; - -} // -namespace http -} // -namespace webserv diff --git a/includes/requests/HttpIMessage.hpp b/includes/requests/IMessage.hpp similarity index 100% rename from includes/requests/HttpIMessage.hpp rename to includes/requests/IMessage.hpp diff --git a/includes/requests/RequestImplement.hpp b/includes/requests/RequestImplement.hpp new file mode 100644 index 0000000..5c72556 --- /dev/null +++ b/includes/requests/RequestImplement.hpp @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* RequestImplement.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/30 09:30:15 by adjoly #+# #+# */ +/* Updated: 2025/04/30 09:34:17 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +namespace webserv { +namespace http { + +class Get : public ARequest { + public: + Get(void) {} + Get(std::string &data); + + void parse(std::string const &data); + + Response execute(void); +}; + +class Post : public ARequest { + public: + Post(void) {} + Post(std::string &data); + + void parse(std::string const &data); + + Response execute(void); +}; + +class Delete : public http::ARequest { + public: + Delete(void) {} + Delete(std::string &data); + + void parse(std::string const &data); + + Response execute(void); +}; + +}; // namespace http +}; // namespace webserv diff --git a/includes/requests/HttpResponse.hpp b/includes/requests/Response.hpp similarity index 64% rename from includes/requests/HttpResponse.hpp rename to includes/requests/Response.hpp index 8ac7e33..b6bc37a 100644 --- a/includes/requests/HttpResponse.hpp +++ b/includes/requests/Response.hpp @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* HttpResponse.hpp :+: :+: :+: */ +/* Response.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: mmoussou -#include +#include typedef unsigned int uint; namespace webserv { namespace http { -class Response: public http::IMessage { -public: +class Response : public http::IMessage { + public: Response(void); ~Response(void); - std::string getProtocol(void) const; + std::string getProtocol(void) const; uint getStatusCode(void) const; - std::string getStatusText(void) const; + std::string getStatusText(void) const; - void setProtocol(std::string const protocol); - void setStatusCode(uint const status_code); + void setProtocol(std::string const protocol); + void setStatusCode(uint const status_code); - std::string str(void) const; + std::string str(void) const; -private: - std::string _protocol; + private: + std::string _protocol; uint _status_code; - std::string _status_text; - + std::string _status_text; }; -} // -namespace http -} // -namespace webserv +} // namespace http +} // namespace webserv diff --git a/includes/requests/default.hpp b/includes/requests/default.hpp index 83c4ad2..c0226f9 100644 --- a/includes/requests/default.hpp +++ b/includes/requests/default.hpp @@ -6,15 +6,21 @@ /* By: mmoussou -#include -#include +#include #include +#include +#include +#include +#include +#include + +namespace webserv { + +}; -using namespace webserv; diff --git a/includes/server/Resource.hpp b/includes/server/AResource.hpp similarity index 90% rename from includes/server/Resource.hpp rename to includes/server/AResource.hpp index 6a874c0..6944ee8 100644 --- a/includes/server/Resource.hpp +++ b/includes/server/AResource.hpp @@ -6,7 +6,7 @@ /* By: mmoussou #include +#include -#include -#include -#include +#include #include +#include +#include +#include #include using namespace webserv; -http::IRequest::~IRequest(void) { - -} - -std::string http::IRequest::str(void) const -{ +std::string http::IRequest::str(void) const { std::ostringstream response; response << this->_method << " " << this->_target << " " << this->_protocol; response << "\r\n"; - for (std::map::const_iterator it = this->_headers.begin(); it != this->_headers.end(); ++it) + for (std::map::const_iterator it = + this->_headers.begin(); + it != this->_headers.end(); ++it) response << it->first << ": " << it->second << "\r\n"; response << "\r\n"; @@ -42,71 +40,45 @@ std::string http::IRequest::str(void) const return (response.str()); } -void parse(std::string const &data) { (void) data; } -http::Response execute(void) { return (http::Response()); } +void parse(std::string const &data) { (void)data; } +http::Response execute(void) { return (http::Response()); } -std::string http::IRequest::getMethod(void) const -{ - return (this->_method); -} +std::string http::IRequest::getMethod(void) const { return (this->_method); } -std::string http::IRequest::getTarget(void) const -{ - return (this->_target); -} +std::string http::IRequest::getTarget(void) const { return (this->_target); } -std::string http::IRequest::getProtocol(void) const -{ +std::string http::IRequest::getProtocol(void) const { return (this->_protocol); } -void http::IRequest::setMethod(std::string const method) -{ +void http::IRequest::setMethod(std::string const method) { this->_method = method; } -void http::IRequest::setTarget(std::string const target) -{ +void http::IRequest::setTarget(std::string const target) { this->_target = target; } -void http::IRequest::setProtocol(std::string const protocol) -{ +void http::IRequest::setProtocol(std::string const protocol) { this->_protocol = protocol; } // ------------------------------------------------------------------ +http::Get::Get(std::string &data) { this->parse(data); } -http::Get::Get(void) -{ -} +void http::Get::parse(std::string const &data) { + std::istringstream stream(data); + std::string line; -http::Get::~Get(void) -{ -} - -http::Get::Get(std::string &data) -{ - this->parse(data); -} - -void http::Get::parse(std::string const &data) -{ - std::istringstream stream(data); - std::string line; - - if (std::getline(stream, line)) - { + if (std::getline(stream, line)) { std::istringstream line_stream(line); line_stream >> this->_method >> this->_target >> this->_protocol; - this->_target.insert(this->_target.begin(), '.'); + /* this->_target.insert(this->_target.begin(), '.'); */ } - while (std::getline(stream, line) && line != "\r") - { + while (std::getline(stream, line) && line != "\r") { size_t delimiter_index = line.find(':'); - if (delimiter_index != std::string::npos) - { + if (delimiter_index != std::string::npos) { std::string key = line.substr(0, delimiter_index); std::string value = line.substr(delimiter_index + 2); this->_headers.insert(std::make_pair(key, value)); @@ -118,54 +90,59 @@ void http::Get::parse(std::string const &data) body_stream << line << "\n"; this->_body = body_stream.str(); + _url = new URL("http://" + _headers["Host"] + _target); + std::cout << *_url << std::endl; + /* std::cout << "-- start-line --" << std::endl; std::cout << "method: " << this->_method << std::endl; - std::cout << "target: " << this->_target << std::endl; + std::cout << "target: " << this->_target << std::You can use every macro and +define like FD_SET, FD_CLR, FD_ISSET and, FD_ZERO (understanding what they do +and how they work is very useful). • A request to your server should never hang +indefinitely. • Your server must be compatible with standard web browsers of +your choice. • We will consider that NGINX is HTTP 1.1 compliant and may be used +to compare headers and answer behaviors. • Your HTTP response status codes must +be accurate. • Your server must have default error pages if none are provided. +• You can’t use fork for anything other than CGI (like PHP, or Python, and so +forth). • You must be able to serve a fully static website. • Clients must be +able to upload files. • You need at least the GET, POST, and DELETE methodendl; std::cout << "protocol: " << this->_protocol << std::endl; std::cout << std::endl; std::cout << "-- headers --" << std::endl; - for (std::map::const_iterator it = this->_headers.begin(); it != this->_headers.end(); ++it) - std::cout << it->first << ": " << it->second << std::endl; - std::cout << std::endl; - std::cout << "-- body --" << std::endl << this->_body << std::endl; + for (std::map::const_iterator it = +this->_headers.begin(); it != this->_headers.end(); ++it) std::cout << it->first +<< ": " << it->second << std::endl; std::cout << std::endl; std::cout << "-- +body --" << std::endl << this->_body << std::endl; */ } -char isDirectory(const std::string& path) -{ +char isDirectory(const std::string &path) { struct stat file_stat; - if (stat(path.c_str(), &file_stat) != 0) - { + if (stat(path.c_str(), &file_stat) != 0) { throw std::runtime_error("can't open file (non-existant ?)"); } return S_ISDIR(file_stat.st_mode); } -http::Response http::Get::execute(void) -{ - http::Response response; +http::Response http::Get::execute(void) { + http::Response response; - try - { - if (isDirectory(this->_target)) - { - DIR *dir; - struct dirent *entry; - struct stat file_stat; + try { + if (isDirectory(this->_target)) { + DIR *dir; + struct dirent *entry; + struct stat file_stat; std::vector files; - + if ((dir = opendir(this->_target.c_str())) == NULL) throw; - while ((entry = readdir(dir)) != NULL) - { + 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 (stat(file_path.c_str(), &file_stat) == 0) { if (S_ISDIR(file_stat.st_mode)) files.push_back(file_name + "/"); else @@ -201,43 +178,43 @@ body {\n\ body += ""; response.setProtocol(this->_protocol); response.setStatusCode(200); - std::stringstream length; + std::stringstream length; length << body.length(); response.addHeader("Content-Length", length.str()); response.addHeader("Content-Type", "text/html"); response.setBody(body); - - } - else - { - std::ifstream file(this->_target.c_str(), std::ios::binary); + + } 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)), std::istreambuf_iterator())); - std::stringstream length; + 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)); + response.addHeader("Content-Type", + http::Mime::getType(this->_target)); #ifdef VERBOSE //_log->debug(response.str().c_str()); #endif } - } - catch (...) - { + } catch (...) { // TODO: replace with a predefined array of error pages response.setProtocol(this->_protocol); response.setStatusCode(404); response.addHeader("Content-Type", "text/html"); - response.setBody(http::Errors::getResponseBody(response.getStatusCode())); + response.setBody( + http::Errors::getResponseBody(response.getStatusCode())); } return (response); @@ -245,36 +222,22 @@ body {\n\ // ------------------------------------------------------------------ -http::Delete::Delete(void) -{ -} -http::Delete::~Delete(void) -{ -} +http::Delete::Delete(std::string &data) { this->parse(data); } -http::Delete::Delete(std::string &data) -{ - this->parse(data); -} +void http::Delete::parse(std::string const &data) { + std::istringstream stream(data); + std::string line; -void http::Delete::parse(std::string const &data) -{ - std::istringstream stream(data); - std::string line; - - if (std::getline(stream, line)) - { + if (std::getline(stream, line)) { std::istringstream line_stream(line); line_stream >> this->_method >> this->_target >> this->_protocol; this->_target.insert(this->_target.begin(), '.'); } - while (std::getline(stream, line) && line != "\r") - { + while (std::getline(stream, line) && line != "\r") { size_t delimiter_index = line.find(':'); - if (delimiter_index != std::string::npos) - { + if (delimiter_index != std::string::npos) { std::string key = line.substr(0, delimiter_index); std::string value = line.substr(delimiter_index + 2); this->_headers.insert(std::make_pair(key, value)); @@ -286,6 +249,9 @@ void http::Delete::parse(std::string const &data) body_stream << line << "\n"; this->_body = body_stream.str(); + _url = new URL(_target); + std::cout << *_url << std::endl; + /* std::cout << "-- start-line --" << std::endl; std::cout << "method: " << this->_method << std::endl; @@ -293,34 +259,33 @@ void http::Delete::parse(std::string const &data) std::cout << "protocol: " << this->_protocol << std::endl; std::cout << std::endl; std::cout << "-- headers --" << std::endl; - for (std::map::const_iterator it = this->_headers.begin(); it != this->_headers.end(); ++it) - std::cout << it->first << ": " << it->second << std::endl; - std::cout << std::endl; + for (std::map::const_iterator it = + this->_headers.begin(); it != this->_headers.end(); ++it) std::cout << + it->first << ": " << it->second << std::endl; std::cout << std::endl; std::cout << "-- body --" << std::endl << this->_body << std::endl; */ } -http::Response http::Delete::execute(void) -{ - http::Response response; +http::Response http::Delete::execute(void) { + http::Response response; - try - { + try { if (std::remove(this->_target.c_str())) throw std::runtime_error("can't remove file, FF"); response.setProtocol(this->_protocol); response.setStatusCode(204); time_t now = std::time(NULL); response.addHeader("Date", std::string(std::ctime(&now))); - } - catch (...) - { - // TODO: check errno value and get corresponding error page, check for corresponding error code : https://cdn.discordapp.com/attachments/784779058407014403/1350841524778307586/image.png?ex=67d8dd74&is=67d78bf4&hm=c030468d3862627d6402bf200960d1a15249ba2f8dac772af3283b368a77f2f5& + } catch (...) { + // TODO: check errno value and get corresponding error page, check for + // corresponding error code : + // https://cdn.discordapp.com/attachments/784779058407014403/1350841524778307586/image.png?ex=67d8dd74&is=67d78bf4&hm=c030468d3862627d6402bf200960d1a15249ba2f8dac772af3283b368a77f2f5& response.setProtocol(this->_protocol); response.setStatusCode(404); response.addHeader("Content-Type", "text/html"); - response.setBody(http::Errors::getResponseBody(response.getStatusCode())); + response.setBody( + http::Errors::getResponseBody(response.getStatusCode())); } return (response); @@ -328,36 +293,21 @@ http::Response http::Delete::execute(void) // ------------------------------------------------------------------ -http::Post::Post(void) -{ -} +http::Post::Post(std::string &data) { this->parse(data); } -http::Post::~Post(void) -{ -} +void http::Post::parse(std::string const &data) { + std::istringstream stream(data); + std::string line; -http::Post::Post(std::string &data) -{ - this->parse(data); -} - -void http::Post::parse(std::string const &data) -{ - std::istringstream stream(data); - std::string line; - - if (std::getline(stream, line)) - { + if (std::getline(stream, line)) { std::istringstream line_stream(line); line_stream >> this->_method >> this->_target >> this->_protocol; this->_target.insert(this->_target.begin(), '.'); } - while (std::getline(stream, line) && line != "\r") - { + while (std::getline(stream, line) && line != "\r") { size_t delimiter_index = line.find(':'); - if (delimiter_index != std::string::npos) - { + if (delimiter_index != std::string::npos) { std::string key = line.substr(0, delimiter_index); std::string value = line.substr(delimiter_index + 2); this->_headers.insert(std::make_pair(key, value)); @@ -369,6 +319,9 @@ void http::Post::parse(std::string const &data) body_stream << line << "\n"; this->_body = body_stream.str(); + _url = new URL(_target); + std::cout << *_url << std::endl; + /* std::cout << "-- start-line --" << std::endl; std::cout << "method: " << this->_method << std::endl; @@ -376,45 +329,40 @@ void http::Post::parse(std::string const &data) std::cout << "protocol: " << this->_protocol << std::endl; std::cout << std::endl; std::cout << "-- headers --" << std::endl; - for (std::map::const_iterator it = this->_headers.begin(); it != this->_headers.end(); ++it) - std::cout << it->first << ": " << it->second << std::endl; - std::cout << std::endl; + for (std::map::const_iterator it = + this->_headers.begin(); it != this->_headers.end(); ++it) std::cout << + it->first << ": " << it->second << std::endl; std::cout << std::endl; //std::cout << "-- body --" << std::endl << this->_body << std::endl; */ } -std::string extractFilename(const std::string &header) -{ +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); } -void handleMultipartData(const std::string &body, const std::string &boundary) -{ +void handleMultipartData(const std::string &body, const std::string &boundary) { size_t i = 0; - std::string delim = "--" + boundary; + std::string delim = "--" + boundary; delim.erase(delim.size() - 1); - while ((i = body.find(delim, i)) != std::string::npos) - { - size_t start = i + delim.length(); + while ((i = body.find(delim, i)) != std::string::npos) { + size_t start = i + delim.length(); size_t end = body.find("\r\n\r\n", start); - if (end != std::string::npos) - { + if (end != std::string::npos) { std::string part_header = body.substr(start, end - start); - //std::cout << std::endl << std::endl << std::endl << std::endl; - std::string part_content = body.substr(end + 4, body.find(delim, end) - end - 4); + // std::cout << std::endl << std::endl << std::endl << std::endl; + std::string part_content = + body.substr(end + 4, body.find(delim, end) - end - 4); - std::ofstream outfile(extractFilename(part_header).c_str(), std::ios::binary); - if (outfile.is_open()) - { + std::ofstream outfile(extractFilename(part_header).c_str(), + std::ios::binary); + if (outfile.is_open()) { outfile.write(part_content.c_str(), part_content.length()); outfile.close(); - } - else - { + } else { std::cerr << "open failed" << std::endl; } } @@ -423,25 +371,28 @@ void handleMultipartData(const std::string &body, const std::string &boundary) } } -http::Response http::Post::execute(void) -{ - http::Response response; +http::Response http::Post::execute(void) { + http::Response response; - try - { - handleMultipartData(this->_body, this->getHeaders()["Content-Type"].substr(this->getHeaders()["Content-Type"].find("=", this->getHeaders()["Content-Type"].find(";")) + 1)); + try { + handleMultipartData( + this->_body, + this->getHeaders()["Content-Type"].substr( + this->getHeaders()["Content-Type"].find( + "=", this->getHeaders()["Content-Type"].find(";")) + + 1)); response.setProtocol(this->_protocol); response.setStatusCode(200); response.addHeader("Content-Type", "text/html"); - response.setBody(http::Errors::getResponseBody(response.getStatusCode())); - } - catch (...) - { + response.setBody( + http::Errors::getResponseBody(response.getStatusCode())); + } catch (...) { response.setProtocol(this->_protocol); response.setStatusCode(500); response.addHeader("Content-Type", "text/html"); - response.setBody(http::Errors::getResponseBody(response.getStatusCode())); + response.setBody( + http::Errors::getResponseBody(response.getStatusCode())); } return (response); } diff --git a/src/server/Resource.cpp b/src/server/AResource.cpp similarity index 64% rename from src/server/Resource.cpp rename to src/server/AResource.cpp index 2d92030..5b0387e 100644 --- a/src/server/Resource.cpp +++ b/src/server/AResource.cpp @@ -1,42 +1,27 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* Resource.cpp :+: :+: :+: */ +/* AResource.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: mmoussou - -/* -class ClientResource { -public: - virtual void handleResource() = 0; - virtual ~HttpClientResource() {} - - void addFileDescriptor(int fd); - void getFileDescriptors(); - -private: - struct pollfd fd; - -} -*/ +#include using namespace webserv; using namespace server; -void ClientResource::addFileDescriptor(struct pollfd fd) +void AClientResource::addFileDescriptor(struct pollfd fd) { this->_fd = fd; } -struct pollfd ClientResource::getFileDescriptor() +struct pollfd AClientResource::getFileDescriptor() { return (this->_fd); } diff --git a/src/server/Client.cpp b/src/server/Client.cpp index a1307be..b62841c 100644 --- a/src/server/Client.cpp +++ b/src/server/Client.cpp @@ -6,7 +6,7 @@ /* By: mmoussou fd, buffer, BUFFER_SIZE - 1, 0); @@ -102,6 +101,6 @@ void Client::answer(void) { } Client::~Client(void) { - //log("➖", "Client", "destructor called"); + log("➖", "Client", "destructor called"); delete (http::Get *)(this->_request); } diff --git a/src/server/Server.cpp b/src/server/Server.cpp index 8df5c19..daf1136 100644 --- a/src/server/Server.cpp +++ b/src/server/Server.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/11 16:11:40 by adjoly #+# #+# */ -/* Updated: 2025/04/29 14:27:42 by adjoly ### ########.fr */ +/* Updated: 2025/04/29 17:28:17 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -102,7 +102,7 @@ void Server::_run(void) { fd.fd = *it; fd.events = POLLIN; _client_fds.push_back(fd); - //_log->debug("new socket in poll"); + _log->debug("new socket in poll"); } // to add signal instead of 727 @@ -172,10 +172,9 @@ void Server::_run(void) { _log->error("client does not exist"); continue; } - if (client->requestParsed() == false) { - continue; + if (client->requestParsed() == true) { + client->answer(); } - client->answer(); _client_data.erase(std::find(_client_data.begin(), _client_data.end(), client)); delete client; @@ -194,7 +193,7 @@ void Server::_run(void) { } Server::Server(config::Config *conf) : _conf(conf) { - // log("➕", "Server::Server", "config constructor called"); + log("➕", "Server::Server", "config constructor called"); _log = conf->getLogger(); try { _setup(); @@ -205,7 +204,7 @@ Server::Server(config::Config *conf) : _conf(conf) { } Server::~Server(void) { - // log("➖", "Server::Server", "destructor called"); + log("➖", "Server::Server", "destructor called"); for (std::vector::iterator it = _client_fds.begin(); it != _client_fds.end(); it++) close(it->fd);