Skip to content

Commit e030666

Browse files
committed
server response beggining
1 parent 387ac46 commit e030666

11 files changed

+217
-25
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"request": "launch",
1111
"program": "${workspaceFolder}/bin/webserv",
1212
"args": [],
13-
"stopAtEntry": true,
13+
"stopAtEntry": false,
1414
"cwd": "${workspaceFolder}",
1515
"environment": [],
1616
"externalConsole": false,

.vscode/settings.json

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
"files.associations": {
3+
"array": "cpp",
4+
"atomic": "cpp",
5+
"bit": "cpp",
6+
"*.tcc": "cpp",
7+
"cctype": "cpp",
8+
"clocale": "cpp",
9+
"cmath": "cpp",
10+
"compare": "cpp",
11+
"concepts": "cpp",
12+
"csignal": "cpp",
13+
"cstdarg": "cpp",
14+
"cstddef": "cpp",
15+
"cstdint": "cpp",
16+
"cstdio": "cpp",
17+
"cstdlib": "cpp",
18+
"cstring": "cpp",
19+
"cwchar": "cpp",
20+
"cwctype": "cpp",
21+
"deque": "cpp",
22+
"map": "cpp",
23+
"string": "cpp",
24+
"unordered_map": "cpp",
25+
"vector": "cpp",
26+
"exception": "cpp",
27+
"algorithm": "cpp",
28+
"functional": "cpp",
29+
"iterator": "cpp",
30+
"memory": "cpp",
31+
"memory_resource": "cpp",
32+
"numeric": "cpp",
33+
"random": "cpp",
34+
"string_view": "cpp",
35+
"system_error": "cpp",
36+
"tuple": "cpp",
37+
"type_traits": "cpp",
38+
"utility": "cpp",
39+
"fstream": "cpp",
40+
"initializer_list": "cpp",
41+
"iosfwd": "cpp",
42+
"iostream": "cpp",
43+
"istream": "cpp",
44+
"limits": "cpp",
45+
"new": "cpp",
46+
"numbers": "cpp",
47+
"ostream": "cpp",
48+
"sstream": "cpp",
49+
"stdexcept": "cpp",
50+
"streambuf": "cpp",
51+
"typeinfo": "cpp"
52+
}
53+
}

en.subject.pdf

1.51 MB
Binary file not shown.

include/Parser.hpp renamed to include/Request.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* ************************************************************************** */
22
/* */
33
/* ::: :::::::: */
4-
/* Parser.hpp :+: :+: :+: */
4+
/* Request.hpp :+: :+: :+: */
55
/* +:+ +:+ +:+ */
66
/* By: bmoretti <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
@@ -10,8 +10,8 @@
1010
/* */
1111
/* ************************************************************************** */
1212

13-
#ifndef PARSER_HPP
14-
# define PARSER_HPP
13+
#ifndef REQUEST_HPP
14+
# define REQUEST_HPP
1515

1616
# include "common.hpp"
1717

@@ -22,11 +22,11 @@ typedef struct s_request
2222
std::map<std::string, std::string> headers;
2323
} t_request;
2424

25-
class Parser
25+
class Request
2626
{
2727
public:
28-
Parser(const char * str);
29-
~Parser();
28+
Request(const char * str);
29+
~Request();
3030

3131
t_request getRequest() const;
3232

include/Response.hpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* Response.hpp :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: bmoretti <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2024/07/13 13:06:55 by bmoretti #+# #+# */
9+
/* Updated: 2024/07/13 14:02:44 by bmoretti ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#ifndef RESPONSE_HPP
14+
# define RESPONSE_HPP
15+
16+
# include "common.hpp"
17+
# include "Request.hpp"
18+
19+
typedef struct s_response
20+
{
21+
std::string statusLine;
22+
std::map<std::string, std::string> headers;
23+
std::string body;
24+
} t_response;
25+
26+
class Response
27+
{
28+
public:
29+
Response(Request & request);
30+
~Response();
31+
32+
std::string getResponse() const;
33+
34+
private:
35+
Request & _request;
36+
t_response _response;
37+
38+
void _generateStatusLine();
39+
void _generateHeaders();
40+
void _generateBody();
41+
std::string _generateResponse() const;
42+
43+
};
44+
45+
#endif

include/Server.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66
/* By: bmoretti <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/06/27 10:40:41 by bmoretti #+# #+# */
9-
/* Updated: 2024/07/11 19:09:59 by bmoretti ### ########.fr */
9+
/* Updated: 2024/07/13 14:14:47 by bmoretti ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

1313
#ifndef SERVER_HPP
14-
#define SERVER_HPP
14+
# define SERVER_HPP
1515

16-
#include "common.hpp"
17-
#include "Parser.hpp"
16+
# include "common.hpp"
17+
# include "Request.hpp"
18+
# include "Response.hpp"
1819

1920
class Server
2021
{

include/common.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: bmoretti <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/06/27 11:24:48 by bmoretti #+# #+# */
9-
/* Updated: 2024/07/11 18:40:00 by bmoretti ### ########.fr */
9+
/* Updated: 2024/07/13 15:19:10 by bmoretti ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -35,6 +35,8 @@
3535
# include <cerrno>
3636
# include <cstring>
3737
# include <csignal>
38+
# include <cstdlib>
39+
# include <fstream>
3840
# include <iostream>
3941
# include <sstream>
4042
# include <stdexcept>

src/Parser.cpp renamed to src/Request.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
/* ************************************************************************** */
22
/* */
33
/* ::: :::::::: */
4-
/* Parser.cpp :+: :+: :+: */
4+
/* Request.cpp :+: :+: :+: */
55
/* +:+ +:+ +:+ */
66
/* By: bmoretti <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/07/11 18:29:18 by bmoretti #+# #+# */
9-
/* Updated: 2024/07/11 19:00:26 by bmoretti ### ########.fr */
9+
/* Updated: 2024/07/13 13:29:09 by bmoretti ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

13-
#include "Parser.hpp"
13+
#include "Request.hpp"
1414

15-
Parser::Parser(const char * str) : _str(str)
15+
Request::Request(const char * str) : _str(str)
1616
{
1717
this->_parseHTTPRequest();
1818
}
1919

20-
Parser::~Parser()
20+
Request::~Request()
2121
{
2222

2323
}
2424

25-
t_request Parser::getRequest() const
25+
t_request Request::getRequest() const
2626
{
2727
return this->_request;
2828
}
2929

30-
std::string Parser::_trim(const std::string & str)
30+
std::string Request::_trim(const std::string & str)
3131
{
3232
std::string::size_type first = str.find_first_not_of(' ');
3333
std::string::size_type last = str.find_last_not_of(' ');
@@ -38,7 +38,7 @@ std::string Parser::_trim(const std::string & str)
3838
return str.substr(first, last - first + 1);
3939
}
4040

41-
void Parser::_parseHTTPRequest()
41+
void Request::_parseHTTPRequest()
4242
{
4343
std::istringstream requestStream(this->_str);
4444
std::string line;

src/Response.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* Response.cpp :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: bmoretti <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2024/07/13 13:07:40 by bmoretti #+# #+# */
9+
/* Updated: 2024/07/13 15:41:19 by bmoretti ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "Response.hpp"
14+
15+
Response::Response(Request & request) : _request(request)
16+
{
17+
this->_generateStatusLine();
18+
this->_generateHeaders();
19+
this->_generateBody();
20+
}
21+
22+
Response::~Response()
23+
{
24+
}
25+
26+
std::string Response::getResponse() const
27+
{
28+
return this->_generateResponse();
29+
}
30+
31+
void Response::_generateStatusLine()
32+
{
33+
// mocking the status line
34+
this->_response.statusLine = "HTTP/1.1 200 OK";
35+
}
36+
37+
void Response::_generateHeaders()
38+
{
39+
// mocking the headers
40+
this->_response.headers["Content-Type"] = "text/html";
41+
}
42+
43+
std::string itoa(int value)
44+
{
45+
std::string str;
46+
std::stringstream ss;
47+
ss << value;
48+
ss >> str;
49+
return str;
50+
}
51+
52+
void Response::_generateBody()
53+
{
54+
std::ifstream file("./web/index.html");
55+
56+
if (file.is_open()) {
57+
std::stringstream buffer;
58+
std::string bufferStr;
59+
buffer << file.rdbuf();
60+
bufferStr = buffer.str();
61+
this->_response.body = bufferStr;
62+
this->_response.headers["Content-Length"] = itoa(bufferStr.size());
63+
file.close();
64+
} else {
65+
// [TODO] mensagem de arquivo não encontrado
66+
}
67+
}
68+
69+
std::string Response::_generateResponse() const
70+
{
71+
std::string response = this->_response.statusLine + "\r\n";
72+
for (std::map<std::string, std::string>::const_iterator it =
73+
this->_response.headers.begin(); it != this->_response.headers.end();
74+
++it) {
75+
response += it->first + ": " + it->second + "\r\n";
76+
}
77+
response += "\r\n" + this->_response.body;
78+
return response;
79+
}

src/Server.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: bmoretti <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/06/27 10:40:35 by bmoretti #+# #+# */
9-
/* Updated: 2024/07/11 19:12:55 by bmoretti ### ########.fr */
9+
/* Updated: 2024/07/13 15:31:38 by bmoretti ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -229,11 +229,13 @@ void Server::handleConnection(int client_fd)
229229
buffer[bytes_read] = '\0';
230230
// std::cout << "From client: " << client_fd << " | Received: " << buffer;
231231

232-
Parser parser(buffer);
233-
t_request request = parser.getRequest();
232+
Request request(buffer);
233+
Response response(request);
234+
std::string responseStr = response.getResponse();
235+
const char *respStr = responseStr.c_str();
234236

235-
const char *response = "HTTP/1.1 200 OK\r\nContent-Length: 13\r\n\r\nHello, World!";
236-
ssize_t bytes_written = write(client_fd, response, strlen(response));
237+
// const char *response = "HTTP/1.1 200 OK\r\nContent-Length: 13\r\n\r\nHello, World!";
238+
ssize_t bytes_written = write(client_fd, respStr, strlen(respStr));
237239
if (bytes_written == -1)
238240
{
239241
std::cerr << "Write error" << std::endl;

web/index.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Webserv Project</title>
5+
</head>
6+
<body>
7+
<h1>Web serv ou não?</h1>
8+
<p>Bruno, Luiza e Vini!</p>
9+
</body>
10+
</html>

0 commit comments

Comments
 (0)