Skip to content

Commit

Permalink
Connect parser to Res.cpp file and exec_cgi function.
Browse files Browse the repository at this point in the history
Protects in case the cgi bin doesn't exist, throws an error;
Creates new ServerError class so as we are able to throw error in the ServerContext class;
  • Loading branch information
Sepulven committed May 23, 2024
1 parent 91042a5 commit cac2082
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 57 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ UTILS = __raw_data_utils.cpp __file_manager_utils.cpp __webserv_utils.cpp

CONN = Req.cpp Res.cpp ConnStream.cpp HttpError.cpp

SERVER_ENG = ServerContext.cpp WebServer.cpp
SERVER_ENG = ServerContext.cpp WebServer.cpp ServerError.cpp

SRC = $(addprefix srcs/__utils/,$(UTILS)) \
$(addprefix srcs/server_engine/,$(SERVER_ENG)) \
$(addprefix srcs/parser/,$(PARSER)) \
$(addprefix srcs/conn/,$(CONN)) \
$(addprefix srcs/,main.cpp) \
$(addprefix srcs/conn/,$(CONN)) \
$(addprefix srcs/,main.cpp) \

OBJ = $(SRC:.cpp=.o)

Expand Down
2 changes: 0 additions & 2 deletions inc/Req.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ class Req
std::string file_ext;
std::string query_string;

std::vector<std::string> cgi_path;

std::string is_route;
int route_id;

Expand Down
5 changes: 3 additions & 2 deletions inc/ServerContext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* ServerContext.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ratavare <ratavare@student.42.fr> +#+ +:+ +#+ */
/* By: asepulve <asepulve@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/09 12:31:02 by asepulve #+# #+# */
/* Updated: 2024/05/22 17:11:52 by ratavare ### ########.fr */
/* Updated: 2024/05/23 01:13:07 by asepulve ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -22,6 +22,7 @@

/* Classes */
#include "Parser.hpp"
#include <ServerError.hpp>

/* Utils */
#include <__webserv_utils.hpp>
Expand Down
15 changes: 15 additions & 0 deletions inc/ServerError.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include <exception>
#include <iostream>
#include <string>

class ServerError : public std::exception
{
private:
const char *msg;
virtual const char *what() const throw();
public:
const char * get_msg(void) const;
ServerError(const char *_msg);
};
15 changes: 3 additions & 12 deletions inc/WebServer.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#pragma once

#include <ServerContext.hpp>

/* C headers*/
#include <string.h>
#include <sys/epoll.h>
Expand All @@ -23,6 +21,9 @@

/* Classes */
#include <ConnStream.hpp>
#include <ServerError.hpp>
#include <ServerContext.hpp>


/* Utils */
#include <__webserv_utils.hpp>
Expand Down Expand Up @@ -55,15 +56,5 @@ class WebServer
void close_conn(int, int);
void time_out(int);

class Error : public std::exception
{
private:
const char *msg;
virtual const char *what() const throw();

public:
Error(const char *_msg);
};

static void sig_handler(int sig); // * Handles Ctrl + c
};
2 changes: 1 addition & 1 deletion inc/__file_manager_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace FileManager

// * Get method
std::string read_file(const std::string);
std::string directory_listing(const std::string, std::string, int);
std::string directory_listing(const std::string, std::string, int);

// * Post method
std::string create_files(const std::vector<uint8_t> &, const std::string&, const std::string);
Expand Down
5 changes: 2 additions & 3 deletions srcs/conn/Req.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#include <Req.hpp>

/*
*/
Req::Req(ConnStream * _stream) : stream(_stream), out_of_bound(std::string::npos)
{
this->cgi_path.push_back("a.py");
this->cgi_path.push_back("a.php");

this->content_length = 0;
this->referer = std::string();
this->file_path = std::string();
Expand Down
14 changes: 4 additions & 10 deletions srcs/conn/Res.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,16 @@ int Res::check_method(void)
*/
int Res::send(void)
{
std::map<std::string, std::string> &cgi_path = stream->server->cgi_path;
Req *req = stream->req;

std::vector<std::string> &cgi_path = req->cgi_path;
std::vector<std::string>::iterator it = std::find(cgi_path.begin(), cgi_path.end(), req->file_path);

if (!this->status_code.empty() && !this->error_msg.empty())
return (build_http_response());
try
{
if (this->check_method() == -1)
throw HttpError("403" , "Forbidden");
if (it != req->cgi_path.end())
if (cgi_path.find(req->file_ext) != cgi_path.end())
return (this->exec_CGI());
if (req->method == "GET")
exec_get();
Expand Down Expand Up @@ -111,13 +109,9 @@ int Res::exec_CGI(void)
int pipe_fd_aux[2];
pid_t pid;

char *argv0 = const_cast<char *>("");

if (req->file_ext == ".py")
argv0 = const_cast<char *>("/usr/bin/python3");
else if (req->file_ext == ".php")
argv0 = const_cast<char *>("/usr/bin/php");
ServerContext * server = stream->server;

char *argv0 = const_cast<char *>(server->cgi_path[req->file_ext].c_str());
char *argv1 = const_cast<char *>(req->file_path.c_str());
char *const argv[] = {argv0, argv1, NULL};

Expand Down
1 change: 1 addition & 0 deletions srcs/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <Lexer.hpp>
#include <Parser.hpp>


const char* RED_TEXT = "\033[31m";
const char* RESET_COLOR = "\033[0m";

Expand Down
10 changes: 8 additions & 2 deletions srcs/server_engine/ServerContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
ServerContext::ServerContext(t_server serverNode)
{
std::list<std::pair<std::string, std::string> >::iterator cgi_it = serverNode.cgi.begin();
std::list<t_route>::iterator it = serverNode.route.begin();
std::list<std::pair<int, std::string> >::iterator _it = serverNode.errorPages.begin();
std::list<std::string>::iterator tmp;
Expand All @@ -18,8 +19,13 @@ ServerContext::ServerContext(t_server serverNode)
this->port = serverNode.port;
this->name = serverNode.serverName;
this->ip = serverNode.host;
for (std::list<std::pair<std::string, std::string> > ::iterator tmp = serverNode.cgi.begin(); tmp != serverNode.cgi.end(); tmp++)
this->cgi_path.insert(std::make_pair(tmp->first, tmp->second));

for (; cgi_it != serverNode.cgi.end(); cgi_it++)
{
if (!fopen(cgi_it->second.c_str(), "r"))
throw ServerError("CGI path doesn't exist. Maybe the cgi is not installed");
this->cgi_path.insert(std::make_pair(cgi_it->first, cgi_it->second));
}

// * Error pages
for (;_it != serverNode.errorPages.end(); _it++)
Expand Down
13 changes: 13 additions & 0 deletions srcs/server_engine/ServerError.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <ServerError.hpp>

ServerError::ServerError(const char *_msg) : msg(_msg) {}


const char *ServerError::get_msg(void) const
{
return (msg);
}
const char *ServerError::ServerError::what() const throw()
{
return (msg);
}
36 changes: 14 additions & 22 deletions srcs/server_engine/WebServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void WebServer::init_servers(std::vector<ServerContext *>& vec)

this->epoll_fd = epoll_create1(0);
if (this->epoll_fd < 0)
throw Error("Epoll_create1 failed.");
throw ServerError("Epoll_create1 failed.");
for (size_t i = 0; i < vec.size(); i++)
{
memset(&server_addr, 0, sizeof(struct sockaddr_in));
Expand All @@ -81,23 +81,23 @@ void WebServer::init_servers(std::vector<ServerContext *>& vec)
this->servers[server_fd] = vec[i];

if (server_fd < 0)
throw Error("Socket failed.");
throw ServerError("Socket failed.");
if (sfd_non_blocking(server_fd) < 0)
throw Error("Couldn't make the server socket non-blocking.");
throw ServerError("Couldn't make the server socket non-blocking.");
if (set_reuseaddr(server_fd) < 0)
throw Error("Setsockopt failed");
throw ServerError("Setsockopt failed");
if (bind(server_fd, &server_addr) < 0)
throw Error("Bind failed.");
throw ServerError("Bind failed.");
if (::listen(server_fd, vec[i]->max_events) < 0)
throw Error("Listen failed.");
throw ServerError("Listen failed.");

memset(&event, 0, sizeof(struct epoll_event));
event.events = EPOLLIN;
event.data.ptr = new t_event_data(server_fd, SERVER);
this->servers[server_fd]->epoll_event_info = static_cast<t_event_data*>(event.data.ptr);

if (epoll_add_fd(this->epoll_fd, server_fd, event))
throw Error("epoll_ctl failed.");
throw ServerError("epoll_ctl failed.");

this->max_events += vec[i]->max_events;
std::cout << "[" << vec[i]->max_events << "] Listening on port: " << vec[i]->port << std::endl;
Expand All @@ -117,15 +117,15 @@ void WebServer::accept_connection(int epoll_fd, int fd)
int client_fd = accept(fd, (struct sockaddr *)&client_addr, &client_addr_len);

if (client_fd < 0)
throw Error("accept failed.");
throw ServerError("accept failed.");
if (sfd_non_blocking(client_fd) < 0)
throw Error("Couln't make socket fd non-blocking.");
throw ServerError("Couln't make socket fd non-blocking.");

event.events = EPOLLIN | EPOLLET;
event.data.ptr = new t_event_data(client_fd, CLIENT);

if (epoll_add_fd(epoll_fd, client_fd, event) < 0)
throw Error("Epoll_ctl failed");
throw ServerError("Epoll_ctl failed");
this->streams[client_fd] = new ConnStream(client_fd, servers[fd]);
this->streams[client_fd]->epoll_event_info = static_cast<t_event_data*>(event.data.ptr);
}
Expand All @@ -139,7 +139,7 @@ void WebServer::send_response(int epoll_fd, int fd, t_event event)
int status = this->streams[fd]->res->send();
this->streams[fd]->set_time(); // * Update last action;
if ((status > 0) && epoll_in_fd(epoll_fd, fd, event) < 0)
throw Error("Epoll_ctl failed");
throw ServerError("Epoll_ctl failed");
this->streams[fd]->clean_conn();
if (status == -1)
close_conn(epoll_fd, fd);
Expand All @@ -156,7 +156,7 @@ void WebServer::read_request(int epoll_fd, int fd, t_event event)

this->streams[fd]->set_time();
if ((status == 1) && epoll_out_fd(epoll_fd, fd, event))
throw Error("Epoll_ctl failed");
throw ServerError("Epoll_ctl failed");
if (status == -1)
this->close_conn(epoll_fd, fd);
}
Expand Down Expand Up @@ -230,7 +230,7 @@ void WebServer::listen(void)
time_out(epoll_fd);
}
if (num_events < 0 && is_running)
throw Error("Epoll_wait failed.");
throw ServerError("Epoll_wait failed.");
}

/*Signal Handler*/
Expand All @@ -248,18 +248,10 @@ void WebServer::sig_handler(int sig)
void WebServer::close_conn(int epoll_fd, int sfd)
{
if (epoll_del_fd(epoll_fd, sfd) < 0)
throw Error("Epoll_ctl failed");
throw ServerError("Epoll_ctl failed");
close(sfd);
if (waitpid(streams[sfd]->cgi_pid, NULL, WNOHANG) == 0)
kill(this->streams[sfd]->cgi_pid, SIGKILL);
delete this->streams[sfd];
this->streams.erase(sfd);
}

/*Exception class*/
WebServer::Error::Error(const char *_msg) : msg(_msg) {}

const char *WebServer::Error::what() const throw()
{
return (msg);
}

0 comments on commit cac2082

Please sign in to comment.