Skip to content

Commit

Permalink
Merge pull request #10 from brmoretti/develop
Browse files Browse the repository at this point in the history
.
  • Loading branch information
0bvim authored Jul 13, 2024
2 parents 5e595e6 + e030666 commit 444d13f
Show file tree
Hide file tree
Showing 11 changed files with 217 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"request": "launch",
"program": "${workspaceFolder}/bin/webserv",
"args": [],
"stopAtEntry": true,
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
Expand Down
53 changes: 53 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
Binary file added en.subject.pdf
Binary file not shown.
12 changes: 6 additions & 6 deletions include/Parser.hpp → include/Request.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Parser.hpp :+: :+: :+: */
/* Request.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bmoretti <bmoretti@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
Expand All @@ -10,8 +10,8 @@
/* */
/* ************************************************************************** */

#ifndef PARSER_HPP
# define PARSER_HPP
#ifndef REQUEST_HPP
# define REQUEST_HPP

# include "common.hpp"

Expand All @@ -22,11 +22,11 @@ typedef struct s_request
std::map<std::string, std::string> headers;
} t_request;

class Parser
class Request
{
public:
Parser(const char * str);
~Parser();
Request(const char * str);
~Request();

t_request getRequest() const;

Expand Down
45 changes: 45 additions & 0 deletions include/Response.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Response.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bmoretti <bmoretti@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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<std::string, std::string> 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
9 changes: 5 additions & 4 deletions include/Server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
/* By: bmoretti <bmoretti@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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
{
Expand Down
4 changes: 3 additions & 1 deletion include/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: bmoretti <bmoretti@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -35,6 +35,8 @@
# include <cerrno>
# include <cstring>
# include <csignal>
# include <cstdlib>
# include <fstream>
# include <iostream>
# include <sstream>
# include <stdexcept>
Expand Down
16 changes: 8 additions & 8 deletions src/Parser.cpp → src/Request.cpp
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Parser.cpp :+: :+: :+: */
/* Request.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bmoretti <bmoretti@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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(' ');
Expand All @@ -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;
Expand Down
79 changes: 79 additions & 0 deletions src/Response.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Response.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bmoretti <bmoretti@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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<std::string, std::string>::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;
}
12 changes: 7 additions & 5 deletions src/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: bmoretti <bmoretti@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions web/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<title>Webserv Project</title>
</head>
<body>
<h1>Web serv ou não?</h1>
<p>Bruno, Luiza e Vini!</p>
</body>
</html>

0 comments on commit 444d13f

Please sign in to comment.