Skip to content

Commit

Permalink
parser
Browse files Browse the repository at this point in the history
  • Loading branch information
LuMedeir committed Jul 17, 2024
1 parent d36b046 commit ea0447f
Show file tree
Hide file tree
Showing 23 changed files with 1,153 additions and 380 deletions.
33 changes: 0 additions & 33 deletions .vscode/launch.json

This file was deleted.

56 changes: 0 additions & 56 deletions .vscode/settings.json

This file was deleted.

60 changes: 60 additions & 0 deletions arquivo.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
server {
server_name server.com;
listen 127.0.0.1:4242;
error_page 403 error_pages/403.html;
error_page 404 error_pages/404.html;
error_page 405 error_pages/405.html;
error_page 413 error_pages/413.html;
error_page 500 error_pages/500.html;
client_max_body_size 100000;

location / {
autoindex on;
allow_methods GET POST DELETE;
root web_root;
upload_dir web_root/uploads
index index.html index.php;
cgi .php cgi-bin/php-cgi;
}

location /potato/ {
autoindex on;
root web_root;
allow_methods GET POST DELETE;
}

location /redirect/ {
return 301 https://google.com;
}

}
server {
server_name server2.com;
listen 127.0.0.1:4242;
error_page 403 error_pages/403.html;
error_page 404 error_pages/404.html;
error_page 405 error_pages/405.html;
error_page 413 error_pages/413.html;
error_page 500 error_pages/500.html;
client_max_body_size 100;

location / {
autoindex off;
allow_methods GET POST;
root web_root;
upload_dir web_root/uploads
index index.html index.php;
cgi .php cgi-bin/php-cgi;
}

location /potato2/ {
autoindex on;
root web_root;
allow_methods GET;
}

location /redirect/ {
return 301 https://google.com;
}

}
14 changes: 3 additions & 11 deletions include/Client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* Client.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lumedeir < lumedeir@student.42sp.org.br +#+ +:+ +#+ */
/* By: bmoretti <bmoretti@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/27 14:40:02 by bmoretti #+# #+# */
/* Updated: 2024/07/06 16:09:27 by lumedeir ### ########.fr */
/* Updated: 2024/07/01 13:47:27 by bmoretti ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -21,22 +21,14 @@ class Client
Client();
~Client();
void connectToServer(const int serverSocket);
void _setNonBlock(void);
int getClientSocket(void) const;
int getPortClient(void) const;
bool _isConnected(void) const;
void _setConnected(bool status);
void _setClientSocket(int socket);

protected:
bool _connected;

private:
int _clientSocket;
struct sockaddr_in _address;

void _createClientSocket(void);

void _setNonBlock(void);

};

Expand Down
42 changes: 42 additions & 0 deletions include/Config.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef CONFIG_HPP
#define CONFIG_HPP

#include "common.hpp"

struct LocationConfig {
std::string path;
bool autoindex;
std::vector<std::string> allow_methods;
std::string root;
std::string upload_dir;
std::vector<std::string> index;
std::string cgi_extension;
std::string cgi_path;
std::string redirect;
};

struct ServerConfig {
std::string server_name;
std::string listen;
std::map<int, std::string> error_pages;
size_t client_max_body_size;
std::vector<LocationConfig> locations;
};

class Config {
public:
Config(const std::string &filePath);
const std::vector<ServerConfig>& getServers() const;
void printServers() const;

private:
std::string filePath;
std::vector<ServerConfig> servers;

void parseConfigFile();
void parseServerBlock(const std::vector<std::string> &lines, size_t &index);
void parseLocationBlock(const std::vector<std::string> &lines, size_t &index, ServerConfig &server);
std::string trim(const std::string& str);
};

#endif
42 changes: 42 additions & 0 deletions include/Request.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Request.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 REQUEST_HPP
# define REQUEST_HPP

# include "common.hpp"

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

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

t_request getRequest() const;

private:
const char * _str;
t_request _request;

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

};

#endif
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
46 changes: 23 additions & 23 deletions include/Server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,41 @@
/* ::: :::::::: */
/* Server.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lumedeir < lumedeir@student.42sp.org.br +#+ +:+ +#+ */
/* By: bmoretti <bmoretti@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/27 10:40:41 by bmoretti #+# #+# */
/* Updated: 2024/07/06 18:40:23 by lumedeir ### ########.fr */
/* Updated: 2024/07/13 14:14:47 by bmoretti ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef SERVER_HPP
#define SERVER_HPP
# define SERVER_HPP

#include "Client.hpp"
#include "common.hpp"
# include "common.hpp"
# include "Request.hpp"
# include "Response.hpp"
# include "Config.hpp"

class Server {
class Server
{
public:
Server();
Server(const std::string &address, int port);
~Server();

private:
int _serverSocket;
bool _isSigInt;
struct sockaddr_in _address;
std::vector<Client *> _clients;

// Server initialization
void _createServerSocket(void);
void _bindServer(void);
void _listen(void);
void _polling(void);
void _verifyClients(void);
static void _handleSignal(int c);

// Client management
void _acceptClient(int socket);
void run();

private:
/* Server initialization */
void _initServer();
void handleConnection(int client_fd);
void handleEvents();
void setNonBlocking(int fd);

std::string _address;
int _port;
int _server_fd;
int _epoll_fd;
struct epoll_event _events[MAX_EVENTS];
};

#endif
Loading

0 comments on commit ea0447f

Please sign in to comment.