diff --git a/.vscode/launch.json b/.vscode/launch.json index f454299..dee2d77 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,7 +10,7 @@ "request": "launch", "program": "${workspaceFolder}/bin/webserv", "args": [], - "stopAtEntry": true, + "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false, diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..39549b0 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,53 @@ +{ + "files.associations": { + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "*.tcc": "cpp", + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "compare": "cpp", + "concepts": "cpp", + "csignal": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "map": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "random": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "fstream": "cpp", + "initializer_list": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "new": "cpp", + "numbers": "cpp", + "ostream": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "typeinfo": "cpp" + } +} diff --git a/en.subject.pdf b/en.subject.pdf new file mode 100644 index 0000000..66b091c Binary files /dev/null and b/en.subject.pdf differ diff --git a/include/Parser.hpp b/include/Request.hpp similarity index 86% rename from include/Parser.hpp rename to include/Request.hpp index 4bd8a3a..a9270dc 100644 --- a/include/Parser.hpp +++ b/include/Request.hpp @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* Parser.hpp :+: :+: :+: */ +/* Request.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: bmoretti +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ @@ -10,8 +10,8 @@ /* */ /* ************************************************************************** */ -#ifndef PARSER_HPP -# define PARSER_HPP +#ifndef REQUEST_HPP +# define REQUEST_HPP # include "common.hpp" @@ -22,11 +22,11 @@ typedef struct s_request std::map headers; } t_request; -class Parser +class Request { public: - Parser(const char * str); - ~Parser(); + Request(const char * str); + ~Request(); t_request getRequest() const; diff --git a/include/Response.hpp b/include/Response.hpp new file mode 100644 index 0000000..3918b99 --- /dev/null +++ b/include/Response.hpp @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Response.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bmoretti +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/13 13:06:55 by bmoretti #+# #+# */ +/* Updated: 2024/07/13 14:02:44 by bmoretti ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef RESPONSE_HPP +# define RESPONSE_HPP + +# include "common.hpp" +# include "Request.hpp" + +typedef struct s_response +{ + std::string statusLine; + std::map headers; + std::string body; +} t_response; + +class Response +{ +public: + Response(Request & request); + ~Response(); + + std::string getResponse() const; + +private: + Request & _request; + t_response _response; + + void _generateStatusLine(); + void _generateHeaders(); + void _generateBody(); + std::string _generateResponse() const; + +}; + +#endif diff --git a/include/Server.hpp b/include/Server.hpp index 6d31bd6..a4cfb53 100644 --- a/include/Server.hpp +++ b/include/Server.hpp @@ -6,15 +6,16 @@ /* By: bmoretti +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/27 10:40:41 by bmoretti #+# #+# */ -/* Updated: 2024/07/11 19:09:59 by bmoretti ### ########.fr */ +/* Updated: 2024/07/13 14:14:47 by bmoretti ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef SERVER_HPP -#define SERVER_HPP +# define SERVER_HPP -#include "common.hpp" -#include "Parser.hpp" +# include "common.hpp" +# include "Request.hpp" +# include "Response.hpp" class Server { diff --git a/include/common.hpp b/include/common.hpp index a42839a..88de052 100644 --- a/include/common.hpp +++ b/include/common.hpp @@ -6,7 +6,7 @@ /* By: bmoretti +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/27 11:24:48 by bmoretti #+# #+# */ -/* Updated: 2024/07/11 18:40:00 by bmoretti ### ########.fr */ +/* Updated: 2024/07/13 15:19:10 by bmoretti ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,6 +35,8 @@ # include # include # include +# include +# include # include # include # include diff --git a/src/Parser.cpp b/src/Request.cpp similarity index 82% rename from src/Parser.cpp rename to src/Request.cpp index bc092ba..ea543db 100644 --- a/src/Parser.cpp +++ b/src/Request.cpp @@ -1,33 +1,33 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* Parser.cpp :+: :+: :+: */ +/* Request.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: bmoretti +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/11 18:29:18 by bmoretti #+# #+# */ -/* Updated: 2024/07/11 19:00:26 by bmoretti ### ########.fr */ +/* Updated: 2024/07/13 13:29:09 by bmoretti ### ########.fr */ /* */ /* ************************************************************************** */ -#include "Parser.hpp" +#include "Request.hpp" -Parser::Parser(const char * str) : _str(str) +Request::Request(const char * str) : _str(str) { this->_parseHTTPRequest(); } -Parser::~Parser() +Request::~Request() { } -t_request Parser::getRequest() const +t_request Request::getRequest() const { return this->_request; } -std::string Parser::_trim(const std::string & str) +std::string Request::_trim(const std::string & str) { std::string::size_type first = str.find_first_not_of(' '); std::string::size_type last = str.find_last_not_of(' '); @@ -38,7 +38,7 @@ std::string Parser::_trim(const std::string & str) return str.substr(first, last - first + 1); } -void Parser::_parseHTTPRequest() +void Request::_parseHTTPRequest() { std::istringstream requestStream(this->_str); std::string line; diff --git a/src/Response.cpp b/src/Response.cpp new file mode 100644 index 0000000..1e235aa --- /dev/null +++ b/src/Response.cpp @@ -0,0 +1,79 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Response.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bmoretti +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/13 13:07:40 by bmoretti #+# #+# */ +/* Updated: 2024/07/13 15:41:19 by bmoretti ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Response.hpp" + +Response::Response(Request & request) : _request(request) +{ + this->_generateStatusLine(); + this->_generateHeaders(); + this->_generateBody(); +} + +Response::~Response() +{ +} + +std::string Response::getResponse() const +{ + return this->_generateResponse(); +} + +void Response::_generateStatusLine() +{ + // mocking the status line + this->_response.statusLine = "HTTP/1.1 200 OK"; +} + +void Response::_generateHeaders() +{ + // mocking the headers + this->_response.headers["Content-Type"] = "text/html"; +} + +std::string itoa(int value) +{ + std::string str; + std::stringstream ss; + ss << value; + ss >> str; + return str; +} + +void Response::_generateBody() +{ + std::ifstream file("./web/index.html"); + + if (file.is_open()) { + std::stringstream buffer; + std::string bufferStr; + buffer << file.rdbuf(); + bufferStr = buffer.str(); + this->_response.body = bufferStr; + this->_response.headers["Content-Length"] = itoa(bufferStr.size()); + file.close(); + } else { + // [TODO] mensagem de arquivo não encontrado + } +} + +std::string Response::_generateResponse() const +{ + std::string response = this->_response.statusLine + "\r\n"; + for (std::map::const_iterator it = + this->_response.headers.begin(); it != this->_response.headers.end(); + ++it) { + response += it->first + ": " + it->second + "\r\n"; + } + response += "\r\n" + this->_response.body; + return response; +} diff --git a/src/Server.cpp b/src/Server.cpp index 9f25b9c..01ab2bc 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -6,7 +6,7 @@ /* By: bmoretti +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/27 10:40:35 by bmoretti #+# #+# */ -/* Updated: 2024/07/11 19:12:55 by bmoretti ### ########.fr */ +/* Updated: 2024/07/13 15:31:38 by bmoretti ### ########.fr */ /* */ /* ************************************************************************** */ @@ -229,11 +229,13 @@ void Server::handleConnection(int client_fd) buffer[bytes_read] = '\0'; // std::cout << "From client: " << client_fd << " | Received: " << buffer; - Parser parser(buffer); - t_request request = parser.getRequest(); + Request request(buffer); + Response response(request); + std::string responseStr = response.getResponse(); + const char *respStr = responseStr.c_str(); - const char *response = "HTTP/1.1 200 OK\r\nContent-Length: 13\r\n\r\nHello, World!"; - ssize_t bytes_written = write(client_fd, response, strlen(response)); + // const char *response = "HTTP/1.1 200 OK\r\nContent-Length: 13\r\n\r\nHello, World!"; + ssize_t bytes_written = write(client_fd, respStr, strlen(respStr)); if (bytes_written == -1) { std::cerr << "Write error" << std::endl; diff --git a/web/index.html b/web/index.html new file mode 100644 index 0000000..ee5d379 --- /dev/null +++ b/web/index.html @@ -0,0 +1,10 @@ + + + + Webserv Project + + +

Web serv ou não?

+

Bruno, Luiza e Vini!

+ +