From 6b2632534cceba1db0f2aff4032f9abf0c45193e Mon Sep 17 00:00:00 2001 From: y-syo Date: Wed, 12 Feb 2025 01:36:19 +0100 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D=20wip(r?= =?UTF-8?q?equests):=20from=20map=20to=20multimap=20for=20multiple=20heade?= =?UTF-8?q?rs=20with=20same=20key(still=20tmp=20commit,=20will=20redo=20th?= =?UTF-8?q?em=20correctly=20after=20i'm=20done)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/requests/HttpIMessage.hpp | 16 ++--- includes/requests/HttpRequest.hpp | 19 +++--- includes/requests/HttpResponse.hpp | 20 ++---- includes/requests/default.hpp | 4 +- includes/server/default.hpp | 6 +- includes/webserv.hpp | 4 +- src/requests_handling/HttpIMessage.cpp | 45 ++++++++++++++ src/requests_handling/HttpRequests.cpp | 85 +++++++++++++++++++++++++- src/requests_handling/HttpResponse.cpp | 68 ++++++++++++--------- 9 files changed, 197 insertions(+), 70 deletions(-) create mode 100644 src/requests_handling/HttpIMessage.cpp diff --git a/includes/requests/HttpIMessage.hpp b/includes/requests/HttpIMessage.hpp index 1b1cc19..e72e361 100644 --- a/includes/requests/HttpIMessage.hpp +++ b/includes/requests/HttpIMessage.hpp @@ -6,7 +6,7 @@ /* By: mmoussou getHeaders(void) const; - virtual std::string getBody(void) const; + virtual std::multimap getHeaders(void) const; + virtual std::string getBody(void) const; - virtual void setHeaders(std::map const headers); + virtual void setHeaders(std::multimap const headers); virtual void setBody(std::string const body); - virtual void addHeader(std::string const name, std::string const value); - virtual void rmHeader(std::string const name); + virtual void addHeader(std::string const key, std::string const value); + virtual void rmHeader(std::string const key); virtual std::string str(void) const = 0; protected: - std::map _headers; - std::string _body; + std::multimap _headers; + std::string _body; }; diff --git a/includes/requests/HttpRequest.hpp b/includes/requests/HttpRequest.hpp index 7b4911c..2f4d723 100644 --- a/includes/requests/HttpRequest.hpp +++ b/includes/requests/HttpRequest.hpp @@ -6,7 +6,7 @@ /* By: mmoussou + #include namespace webserv { @@ -22,16 +24,6 @@ namespace http { class Response: public http::IMessage { public: Response(void); - /* - * ? either : (templated) - Response(); - Response(); - - * or : (if too different ? idk tbh) - Response(Get const req); - Response(Post const req); - */ - ~Response(void); std::string getProtocol(void) const; size_t getStatusCode(void) const; @@ -44,9 +36,9 @@ public: std::string str(void) const; private: - std::string _protocol; - size_t _status_code; - std::string _status_text; + std::string _protocol; + size_t _status_code; + std::string _status_text; }; diff --git a/includes/requests/default.hpp b/includes/requests/default.hpp index 946cda1..3c8763d 100644 --- a/includes/requests/default.hpp +++ b/includes/requests/default.hpp @@ -6,7 +6,7 @@ /* By: mmoussou #include +using namespace webserv; + #endif // __WEBSERV_REQUESTS_DEFAULT_HPP__ diff --git a/includes/server/default.hpp b/includes/server/default.hpp index f648bec..1f956c6 100644 --- a/includes/server/default.hpp +++ b/includes/server/default.hpp @@ -6,7 +6,7 @@ /* By: mmoussou + +using namespace webserv; + +std::multimap http::IMessage::getHeaders(void) const +{ + return (this->_headers); +} + +std::string http::IMessage::getBody(void) const +{ + return (this->_body); +} + +void http::IMessage::setHeaders(std::multimap const headers) +{ + this->_headers = headers; +} + +void http::IMessage::setBody(std::string const body) +{ + this->_body = body; +} + +void http::IMessage::addHeader(std::string const key, std::string const value) +{ + this->_headers.insert(std::make_pair(key, value)); +} + +void http::IMessage::rmHeader(std::string const key) +{ + this->_headers.erase(key); +} diff --git a/src/requests_handling/HttpRequests.cpp b/src/requests_handling/HttpRequests.cpp index 09fc1f4..a0d0761 100644 --- a/src/requests_handling/HttpRequests.cpp +++ b/src/requests_handling/HttpRequests.cpp @@ -6,11 +6,11 @@ /* By: mmoussou /* HttpRequest parseRequest(const std::string &rawRequest) { @@ -54,3 +54,84 @@ HttpRequest parseRequest(const std::string &rawRequest) return request; } */ + +/* GET response creation +HttpResponse::HttpResponse(const HttpRequest &request): _protocol(request.protocol) +{ + std::ifstream file(request.target.c_str(), std::ios::binary); + if (file) + { + this->_status_code = 200; + this->_status_text = "OK"; + this->_headers["Content-Type"] = "text/html"; + this->_body = std::string((std::istreambuf_iterator(file)), std::istreambuf_iterator()); + } + else + { + // TODO: replace with the new predefined array of response + this->_status_code = 404; + this->_status_text = "Not Found"; + this->_headers["Content-Type"] = "text/html"; + this->_body = "nuh uh, get 404'd >:D"; + } +} +*/ + +/* +class IRequest: public http::IMessage { +public: + virtual void parse(const http::IRequest &request) = 0; + virtual http::Response execute(void) = 0; + + //std::string str(void) const; + + std::string getProtocol(void) const; + size_t getStatusCode(void) const; + std::string getStatusText(void) const; + + void setProtocol(std::string const protocol); + void setStatusCode(size_t const status_code); + void setStatusText(std::string const status_text); + +private: + std::string _method; + std::string _target; + std::string _protocol; + +}; +*/ + +using namespace webserv; + +void http::IRequest::parse(http::IRequest const &request) { (void) request; } +http::Response execute(void) { return (http::Response()); } + +std::string http::IRequest::getMethod(void) const +{ + return (this->_method); +} + +std::string http::IRequest::getTarget(void) const +{ + return (this->_target); +} + +std::string http::IRequest::getProtocol(void) const +{ + return (this->_protocol); +} + +void http::IRequest::setMethod(std::string const method) +{ + this->_method = method; +} + +void http::IRequest::setTarget(std::string const target) +{ + this->_target = target; +} + +void http::IRequest::setProtocol(std::string const protocol) +{ + this->_protocol = protocol; +} diff --git a/src/requests_handling/HttpResponse.cpp b/src/requests_handling/HttpResponse.cpp index 4445195..5857f28 100644 --- a/src/requests_handling/HttpResponse.cpp +++ b/src/requests_handling/HttpResponse.cpp @@ -6,53 +6,65 @@ /* By: mmoussou _headers; -std::string _body;*/ /* -HttpResponse::HttpResponse(const HttpRequest &request): _protocol(request.protocol) -{ - std::ifstream file(request.target.c_str(), std::ios::binary); - if (file) - { - this->_status_code = 200; - this->_status_text = "OK"; - this->_headers["Content-Type"] = "text/html"; - this->_body = std::string((std::istreambuf_iterator(file)), std::istreambuf_iterator()); - } - else - { - this->_status_code = 404; - this->_status_text = "Not Found"; - this->_headers["Content-Type"] = "text/html"; - this->_body = "nuh uh, get 404'd >:D"; - } -} +- do a map of all the status_text and get it from here, not storing them +- get error pages from an array of predefined response, that would be modified by the config +*/ -HttpResponse::~HttpResponse(void) +using namespace webserv; + +http::Response::Response(void) { } -std::string HttpResponse::str(void) const +std::string http::Response::str(void) const { std::ostringstream response; response << this->_protocol << " " << this->_status_code << " " << this->_status_text; response << "\r\n"; - for (std::map::const_iterator it = this->_headers.begin(); it != this->_headers.end(); ++it) + for (std::multimap::const_iterator it = this->_headers.begin(); it != this->_headers.end(); ++it) response << it->first << ": " << it->second << "\r\n"; response << "\r\n"; response << this->_body; return (response.str()); -}*/ +} + +std::string http::Response::getProtocol(void) const +{ + return (this->_protocol); +} + +size_t http::Response::getStatusCode(void) const +{ + return (this->_status_code); +} + +std::string http::Response::getStatusText(void) const +{ + return (this->_status_text); +} + +void http::Response::setProtocol(std::string const protocol) +{ + this->_protocol = protocol; +} + +void http::Response::setStatusCode(size_t const status_code) +{ + this->_status_code = status_code; +} + +void http::Response::setStatusText(std::string const status_text) +{ + this->_status_text = status_text; +}