Skip to content

Commit

Permalink
Solve some minor leaks issues.
Browse files Browse the repository at this point in the history
I will come back to rewrite the code in a cleaner way.
  • Loading branch information
Sepulven committed May 31, 2024
1 parent 4dc98c3 commit 7083fef
Show file tree
Hide file tree
Showing 14 changed files with 144 additions and 110 deletions.
1 change: 1 addition & 0 deletions cgi-bin/py/auth/db/ids.csv
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ ape2jWKjXf0bX39kOYTyxGTMZIiLG3sx
N4Pixq0Kvu8KBfRJrywTFHZGhuRqX9Kh
cj7pQ92PiQsmWv4UGqGIfpK3V6UZiMge
F12trNY5d6uETjtAK3eFiFEUj77r1ueP
GKOhkOImkiqVdaMBHJWWqWUQ23pZEBMB
34 changes: 17 additions & 17 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,23 @@ server:
index: register.py
http_methods: GET POST

# server:
# listen:
# host: localhost
# port: 2020
# server_name: example2.com
# root: /
# dir_listing: on
# http_methods: GET
# index: xedni.html
# error_pages:
# 400: error/400.html
# max_cbsize: 2000m
# max_conn: 5
# route /s:
# root: /srcs
# dir_listing: on
# http_methods: GET
server:
listen:
host: localhost
port: 8081
server_name: example2.com
root: /
dir_listing: on
http_methods: GET
index: xedni.html
error_pages:
400: error/400.html
max_cbsize: 2000m
max_conn: 5
route /s:
root: /srcs
dir_listing: on
http_methods: GET

# server:
# listen:
Expand Down
1 change: 1 addition & 0 deletions inc/WebServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class WebServer

std::vector<t_event> events; // epoll_events

int failed;
// * Connections
std::map<int, ConnStream *> streams;
public:
Expand Down
2 changes: 1 addition & 1 deletion srcs/conn/Res.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ int Res::exec_get(void)
throw HttpError("404", "Not Found");
}
else
throw HttpError("403", "Forbidden");
throw HttpError("404", "Not Found");
}
if (stream->req->path_type == _NONE)
throw HttpError("404", "Not Found");
Expand Down
5 changes: 3 additions & 2 deletions srcs/server_engine/ServerContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ ServerContext::ServerContext(t_server serverNode)
std::list<std::pair<int, std::string> >::iterator _it = serverNode.errorPages.begin();
std::list<std::string>::iterator tmp;

epoll_event_info = NULL;
if (this->ip == "localhost" || this->ip != "localhost")
this->domain = INADDR_ANY;

Expand Down Expand Up @@ -83,7 +84,6 @@ ServerContext::ServerContext(t_server serverNode)
server_location.http_methods.push_back(*it);
server_location.redirect = serverNode.redir;
server_location.dir_listing = serverNode.dirListing;
// std::cout << "dir listing root: " << server_location.dir_listing << std::endl;

this->routes.push_back(server_location);
}
Expand All @@ -93,5 +93,6 @@ ServerContext::ServerContext(t_server serverNode)
*/
ServerContext::~ServerContext()
{
delete epoll_event_info;
if (epoll_event_info)
delete epoll_event_info;
}
43 changes: 29 additions & 14 deletions srcs/server_engine/WebServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ WebServer::WebServer(std::list<t_server> serverNodes)
std::vector<ServerContext *> vec;

this->max_events = 0;
this->failed = 0;

signal(SIGINT, &WebServer::sig_handler);
for (; it != serverNodes.end(); it++)
vec.push_back(new ServerContext(*it));
this->init_servers(vec);

// * Logs the online logo;
std::cout << "\r" << "\033[32m" << std::endl
<< " " << std::endl
Expand All @@ -41,8 +41,9 @@ WebServer::~WebServer()
keys_to_del.push_back(it->second->fd);
for (size_t i = 0; i < keys_to_del.size(); i++)
close_conn(epoll_fd, keys_to_del[i]);
for (; _it != servers.end(); _it++)
delete _it->second;
if (failed == 0)
for (; _it != servers.end(); _it++)
delete _it->second;
std::cout << "*************************************" << std::endl;

std::cout << "\r" << "\033[31m" << std::endl
Expand Down Expand Up @@ -81,18 +82,29 @@ void WebServer::init_servers(std::vector<ServerContext *>& vec)
server_fd = socket(AF_INET, SOCK_STREAM, 0);
vec[i]->socket = server_fd;
this->servers[server_fd] = vec[i];

if (server_fd < 0)
throw ServerError("Socket failed.");
if (sfd_non_blocking(server_fd) < 0)
throw ServerError("Couldn't make the server socket non-blocking.");
if (set_reuseaddr(server_fd) < 0)
throw ServerError("Setsockopt failed");
if (bind(server_fd, &server_addr) < 0)
throw ServerError("Bind failed.");
if (::listen(server_fd, vec[i]->max_events) < 0)
throw ServerError("Listen failed.");
try
{
if (server_fd < 0)
throw ServerError("Socket failed.");
if (sfd_non_blocking(server_fd) < 0)
throw ServerError("Couldn't make the server socket non-blocking.");
if (set_reuseaddr(server_fd) < 0)
throw ServerError("Setsockopt failed");
if (bind(server_fd, &server_addr) < 0)
throw ServerError("Bind failed.");
if (::listen(server_fd, vec[i]->max_events) < 0)
throw ServerError("Listen failed.");
}
catch (const std::exception& e)
{
failed = 1;
std::cout << "ERROR: " << e.what() << std::endl;

for (size_t i = 0; i < vec.size(); i++)
delete vec[i];
vec.clear();
return ;
}
memset(&event, 0, sizeof(struct epoll_event));
event.events = EPOLLIN;
event.data.ptr = new t_event_data(server_fd, SERVER);
Expand Down Expand Up @@ -210,6 +222,9 @@ void WebServer::listen(void)
t_event *conn;
t_event_data *event_data;

if (failed)
return;

while (is_running)
{
num_events = epoll_wait(epoll_fd, this->events.data(), this->max_events, 100);
Expand Down
Empty file removed www/uploads/1717177986529_
Empty file.
53 changes: 53 additions & 0 deletions www/uploads/1717178844119_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <iostream>
#include <Array.hpp>

#define MAX_VAL 750
int main(int, char**)
{
Array<int> numbers(MAX_VAL);
int* mirror = new int[MAX_VAL];
srand(time(NULL));
for (int i = 0; i < MAX_VAL; i++)
{
const int value = rand();
numbers[i] = value;
mirror[i] = value;
}
//SCOPE
{
Array<int> tmp = numbers;
Array<int> test(tmp);
}

for (int i = 0; i < MAX_VAL; i++)
{
if (mirror[i] != numbers[i])
{
std::cerr << "didn't save the same value!!" << std::endl;
return 1;
}
}
try
{
numbers[-2] = 0;
}
catch(const std::exception& e)
{
std::cerr << e.what() << '\n';
}
try
{
numbers[MAX_VAL] = 0;
}
catch(const std::exception& e)
{
std::cerr << e.what() << '\n';
}

for (int i = 0; i < MAX_VAL; i++)
{
numbers[i] = rand();
}
delete [] mirror;//
return 0;
}
39 changes: 39 additions & 0 deletions www/uploads/1717178844126_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: asepulve <asepulve@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/06 12:39:54 by asepulve #+# #+# */
/* Updated: 2024/03/27 12:15:43 by asepulve ### ########.fr */
/* */
/* ************************************************************************** */

#include <BitcoinExchange.hpp>
#include <iostream>
#include <string>
#include <fstream>

/*map*/
int main(int argc, char **argv)
{
std::ifstream pricesFile;
std::ifstream bitcoinFile;

BitcoinExchange bitcoinExchange;

if (argc == 2 && argv[1])
{
pricesFile.open(argv[1]);
bitcoinFile.open("./data.csv");
}
if (argc != 2 || !pricesFile.is_open() || !bitcoinFile.is_open())
{
std::cerr << "Error: could not open file." << std::endl;
return (1);
}
bitcoinExchange.setBitcoin(bitcoinFile);
bitcoinExchange.valueTimesExchangeRate(pricesFile);
return (0);
}
Binary file added www/uploads/a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added www/uploads/b.webm
Binary file not shown.
3 changes: 0 additions & 3 deletions www/uploads/server.c

This file was deleted.

73 changes: 0 additions & 73 deletions www/uploads/tests.cpp

This file was deleted.

Binary file removed www/uploads/webserv-notes.docx
Binary file not shown.

0 comments on commit 7083fef

Please sign in to comment.