From b30d54d9cc06bf84000b1a510a554085785033de Mon Sep 17 00:00:00 2001 From: y-syo Date: Wed, 12 Feb 2025 11:28:52 +0100 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=E2=9C=A8=E3=80=8D=20feat(requests):?= =?UTF-8?q?=20GET=20done,=20time=20for=20the=20two=20others=20D:?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/requests/HttpRequest.hpp | 14 +- src/main.cpp | 22 +-- src/requests_handling/HttpRequests.cpp | 189 +++++++++++++------------ 3 files changed, 116 insertions(+), 109 deletions(-) diff --git a/includes/requests/HttpRequest.hpp b/includes/requests/HttpRequest.hpp index 2f4d723..830bfaa 100644 --- a/includes/requests/HttpRequest.hpp +++ b/includes/requests/HttpRequest.hpp @@ -6,7 +6,7 @@ /* By: mmoussou -/* -HttpRequest parseRequest(const std::string &rawRequest) -{ - std::istringstream stream(rawRequest); - HttpRequest request; - std::string line; - - if (std::getline(stream, line)) - { - std::istringstream line_stream(line); - line_stream >> request.method >> request.target >> request.protocol; - request.target.insert(request.target.begin(), '.'); - } - - while (std::getline(stream, line) && line != "\r") - { - size_t delimiter_index = line.find(':'); - if (delimiter_index != std::string::npos) - { - std::string key = line.substr(0, delimiter_index); - std::string value = line.substr(delimiter_index + 2); - request.headers[key] = value; - } - } - - std::ostringstream body_stream; - while (std::getline(stream, line)) - body_stream << line << "\n"; - request.body = body_stream.str(); - - std::cout << "method: " << request.method << std::endl; - std::cout << "target: " << request.target << std::endl; - std::cout << "protocol: " << request.protocol << std::endl; - std::cout << std::endl; - std::cout << "-- headers --" << std::endl; - for (std::map::const_iterator it = request.headers.begin(); it != request.headers.end(); ++it) - std::cout << it->first << ": " << it->second << std::endl; - std::cout << std::endl; - std::cout << "-- body --" << std::endl << request.body << std::endl; - - 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; } +std::string http::IRequest::str(void) const +{ + std::ostringstream response; + + response << this->_method << " " << this->_target << " " << this->_protocol; + response << "\r\n"; + + 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()); +} + +void parse(std::string const &data) { (void) data; } http::Response execute(void) { return (http::Response()); } std::string http::IRequest::getMethod(void) const @@ -135,3 +62,83 @@ void http::IRequest::setProtocol(std::string const protocol) { this->_protocol = protocol; } + +// ------------------------------------------------------------------ + +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)) + { + 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") + { + size_t delimiter_index = line.find(':'); + 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)); + } + } + + std::ostringstream body_stream; + while (std::getline(stream, line)) + body_stream << line << "\n"; + this->_body = body_stream.str(); + + /* + std::cout << "method: " << this->_method << std::endl; + std::cout << "target: " << this->_target << std::endl; + 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; + */ +} + +http::Response http::Get::execute(void) +{ + http::Response response; + + try + { + std::ifstream file(this->_target.c_str(), std::ios::binary); + response.setProtocol(this->_protocol); + response.setStatusCode(200); + response.setStatusText("OK"); + response.addHeader("Content-Type", "text/html"); + response.setBody(std::string((std::istreambuf_iterator(file)), std::istreambuf_iterator())); + } + catch (...) + { + // TODO: replace with a predefined array of error pages + response.setProtocol(this->_protocol); + response.setStatusCode(404); + response.setStatusText("Not Found"); + response.addHeader("Content-Type", "text/html"); + response.setBody("nuh uh, get 404'd >:D"); + } + + return (response); +} + +// ------------------------------------------------------------------