Skip to content

Commit

Permalink
beggining the parser class
Browse files Browse the repository at this point in the history
  • Loading branch information
brmoretti committed Jul 11, 2024
1 parent 604381c commit 387ac46
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 4 deletions.
42 changes: 42 additions & 0 deletions include/Parser.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Parser.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bmoretti <bmoretti@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/11 18:24:18 by bmoretti #+# #+# */
/* Updated: 2024/07/11 18:58:26 by bmoretti ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef PARSER_HPP
# define PARSER_HPP

# include "common.hpp"

typedef struct s_request
{
std::string method;
std::string uri;
std::map<std::string, std::string> headers;
} t_request;

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

t_request getRequest() const;

private:
const char * _str;
t_request _request;

std::string _trim(const std::string & str);
void _parseHTTPRequest();

};

#endif
3 changes: 2 additions & 1 deletion include/Server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
/* By: bmoretti <bmoretti@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/27 10:40:41 by bmoretti #+# #+# */
/* Updated: 2024/07/02 11:49:50 by bmoretti ### ########.fr */
/* Updated: 2024/07/11 19:09:59 by bmoretti ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef SERVER_HPP
#define SERVER_HPP

#include "common.hpp"
#include "Parser.hpp"

class Server
{
Expand Down
3 changes: 2 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/03 12:48:13 by vde-frei ### ########.fr */
/* Updated: 2024/07/11 18:40:00 by bmoretti ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -36,6 +36,7 @@
# include <cstring>
# include <csignal>
# include <iostream>
# include <sstream>
# include <stdexcept>
# include <string>
# include <vector>
Expand Down
59 changes: 59 additions & 0 deletions src/Parser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Parser.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 */
/* */
/* ************************************************************************** */

#include "Parser.hpp"

Parser::Parser(const char * str) : _str(str)
{
this->_parseHTTPRequest();
}

Parser::~Parser()
{

}

t_request Parser::getRequest() const
{
return this->_request;
}

std::string Parser::_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(' ');

if (first == std::string::npos || last == std::string::npos) {
return "";
}
return str.substr(first, last - first + 1);
}

void Parser::_parseHTTPRequest()
{
std::istringstream requestStream(this->_str);
std::string line;

std::getline(requestStream, line);
std::istringstream requestLineStream(line);
requestLineStream >> this->_request.method;
requestLineStream >> this->_request.uri;

while (std::getline(requestStream, line) && line != "\r") {
std::string::size_type pos = line.find(':');
if (pos != std::string::npos) {
std::string key = this->_trim(line.substr(0, pos));
std::string value = this->_trim(line.substr(pos + 1));
this->_request.headers[key] = value;
}
}
}
7 changes: 5 additions & 2 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/02 11:52:58 by bmoretti ### ########.fr */
/* Updated: 2024/07/11 19:12:55 by bmoretti ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -63,7 +63,7 @@ void Server::_initServer()

/* sin_famlily = Address Family Internet - sever para especificar que esta usando IPV4 */
server_addr.sin_family = AF_INET;

/* a funcao inet_addr recebe uma representacao de uma string(char *) com um endereco IPv4 e retorna
* a representacao binaria do ip para operacoes de rede.
* e essa linha so esta setando o endereco de ip para o ip passado via string para a inet_addr
Expand Down Expand Up @@ -229,6 +229,9 @@ 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();

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));
if (bytes_written == -1)
Expand Down

0 comments on commit 387ac46

Please sign in to comment.