mirror of
https://github.com/KeyZox71/webserv.git
synced 2025-05-10 16:38:46 +02:00
「🔨」 fix: fixed port issue and main loop issue for reasons only gods or kanel knows
This commit is contained in:
@ -37,4 +37,5 @@ port = 9090
|
||||
methods = { "GET", "DELETE" }
|
||||
root = "/var/lib/docker/volumes/test_data/_data"
|
||||
dirlist = false
|
||||
cgi.py = "/bin/python"
|
||||
cgi.py = "/bin/python
|
||||
|
||||
|
83
flake.nix
83
flake.nix
@ -1,39 +1,50 @@
|
||||
{
|
||||
inputs = {
|
||||
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
|
||||
pogit = {
|
||||
url = "github:y-syo/pogit";
|
||||
inputs.nixpkgs.follows = "nixpkgs";
|
||||
};
|
||||
};
|
||||
inputs = {
|
||||
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
|
||||
pogit = {
|
||||
url = "github:y-syo/pogit";
|
||||
inputs.nixpkgs.follows = "nixpkgs";
|
||||
};
|
||||
};
|
||||
|
||||
outputs = inputs@{ nixpkgs, ... }:
|
||||
let
|
||||
supportedSystems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ];
|
||||
forEachSupportedSystem = f: nixpkgs.lib.genAttrs supportedSystems (system: f {
|
||||
pkgs = import nixpkgs { inherit system; };
|
||||
});
|
||||
in {
|
||||
devShells = forEachSupportedSystem ({ pkgs }: {
|
||||
default = pkgs.mkShell.override
|
||||
{}
|
||||
{
|
||||
buildInputs = with pkgs;[
|
||||
|
||||
];
|
||||
hardeningDisable = [ "all" ];
|
||||
packages = with pkgs; [
|
||||
gcc11
|
||||
clang_12
|
||||
norminette
|
||||
valgrind
|
||||
git
|
||||
gdb
|
||||
inputs.pogit.packages.${pkgs.system}.default
|
||||
compiledb
|
||||
];
|
||||
};
|
||||
});
|
||||
};
|
||||
outputs =
|
||||
inputs@{ nixpkgs, ... }:
|
||||
let
|
||||
supportedSystems = [
|
||||
"x86_64-linux"
|
||||
"aarch64-linux"
|
||||
"x86_64-darwin"
|
||||
"aarch64-darwin"
|
||||
];
|
||||
forEachSupportedSystem =
|
||||
f:
|
||||
nixpkgs.lib.genAttrs supportedSystems (
|
||||
system:
|
||||
f {
|
||||
pkgs = import nixpkgs { inherit system; };
|
||||
}
|
||||
);
|
||||
in
|
||||
{
|
||||
devShells = forEachSupportedSystem (
|
||||
{ pkgs }:
|
||||
{
|
||||
default = pkgs.mkShell.override { } {
|
||||
buildInputs = with pkgs; [
|
||||
];
|
||||
hardeningDisable = [ "all" ];
|
||||
packages = with pkgs; [
|
||||
gcc11
|
||||
clang_12
|
||||
norminette
|
||||
valgrind
|
||||
git
|
||||
gdb
|
||||
inputs.pogit.packages.${pkgs.system}.default
|
||||
compiledb
|
||||
];
|
||||
};
|
||||
}
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -6,13 +6,17 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/14 12:20:06 by adjoly #+# #+# */
|
||||
/* Updated: 2025/04/22 15:25:39 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/01 12:48:12 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "cppeleven.hpp"
|
||||
#include <config/Server.hpp>
|
||||
#include <netinet/in.h>
|
||||
#include <stdexcept>
|
||||
#include <sys/socket.h>
|
||||
|
||||
namespace webserv {
|
||||
namespace config {
|
||||
@ -25,7 +29,14 @@ class Config {
|
||||
Logger *getLogger(void) { return _log; }
|
||||
|
||||
std::vector<Server *> getServers(void) { return _servers; }
|
||||
Server *getServer(const std::string &);
|
||||
Server *getServer(size_t i) {
|
||||
try {
|
||||
Server *srv = _servers.at(i);
|
||||
return srv;
|
||||
} catch (std::out_of_range &e) {
|
||||
return not_nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<Server *> _servers;
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/03/19 14:11:28 by adjoly #+# #+# */
|
||||
/* Updated: 2025/04/22 15:25:58 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/04/30 17:10:53 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -61,6 +61,7 @@ class Server {
|
||||
// @brief Can be used to check if a servername is present in this config
|
||||
bool isServerName(const std::string &);
|
||||
|
||||
// @brief Can be used to get the route correcponding
|
||||
Route *whatRoute(const URL &);
|
||||
|
||||
protected:
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/14 14:14:39 by adjoly #+# #+# */
|
||||
/* Updated: 2025/04/30 14:51:39 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/01 12:51:51 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -24,7 +24,7 @@ namespace server {
|
||||
|
||||
class Client {
|
||||
public:
|
||||
Client(struct pollfd *, sockaddr_in, config::Config *);
|
||||
Client(struct pollfd *, config::Server *);
|
||||
Client(const Client &cpy);
|
||||
virtual ~Client(void);
|
||||
|
||||
@ -54,12 +54,10 @@ class Client {
|
||||
}
|
||||
|
||||
struct pollfd *_pfd;
|
||||
struct sockaddr_in _client_addr;
|
||||
http::ARequest *_request;
|
||||
// http::Response *_response;
|
||||
config::Server *_conf;
|
||||
config::Route *_route;
|
||||
config::Config *_Gconf;
|
||||
};
|
||||
|
||||
} // namespace server
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/14 12:53:54 by adjoly #+# #+# */
|
||||
/* Updated: 2025/04/23 17:42:07 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/01 11:21:47 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -15,6 +15,7 @@
|
||||
#include "cppeleven.hpp"
|
||||
#include "node/ANode.hpp"
|
||||
#include <config/default.hpp>
|
||||
#include <netinet/in.h>
|
||||
#include <stdexcept>
|
||||
|
||||
using namespace webserv::config;
|
||||
@ -60,12 +61,3 @@ Config::~Config(void) {
|
||||
delete *it;
|
||||
}
|
||||
}
|
||||
|
||||
Server *Config::getServer(const std::string &server_name) {
|
||||
for (auto it = range(_servers)) {
|
||||
if ((*it)->isServerName(server_name)) {
|
||||
return (*it);
|
||||
}
|
||||
}
|
||||
return (not_nullptr);
|
||||
}
|
||||
|
@ -6,13 +6,16 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/03/24 15:10:07 by adjoly #+# #+# */
|
||||
/* Updated: 2025/04/30 15:37:18 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/01 10:08:15 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "config/URL.hpp"
|
||||
#include "cppeleven.hpp"
|
||||
#include "log.hpp"
|
||||
#include <config/default.hpp>
|
||||
#include <cstddef>
|
||||
#include <utility>
|
||||
#include <webserv.hpp>
|
||||
|
||||
using namespace webserv::config;
|
||||
@ -75,9 +78,10 @@ Server::Server(toml::ANode *node) : _table(node) {
|
||||
std::map<std::string, toml::ANode *> *location_table =
|
||||
it->second->getTable();
|
||||
for (it = location_table->begin(); it != location_table->end(); it++) {
|
||||
if (_routes->find(URL(it->first)) != _routes->end())
|
||||
if (_routes->find(URL(it->first)) != _routes->end()) {
|
||||
continue;
|
||||
(*_routes)[URL(it->first)] = new Route(it->second);
|
||||
}
|
||||
_routes->insert(std::make_pair(URL(it->first), new Route(it->second)));
|
||||
}
|
||||
}
|
||||
// delete _table;
|
||||
@ -129,8 +133,8 @@ Route *Server::whatRoute(const URL &url) {
|
||||
}
|
||||
}
|
||||
std::map<URL, Route *>::iterator it = _routes->find(URL("/"));
|
||||
if (it == _routes->end()) {
|
||||
return not_nullptr;
|
||||
if (it != _routes->end()) {
|
||||
return it->second;
|
||||
}
|
||||
return it->second;
|
||||
return not_nullptr;
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/17 11:12:41 by mmoussou #+# #+# */
|
||||
/* Updated: 2025/04/30 15:54:43 by mmoussou ### ########.fr */
|
||||
/* Updated: 2025/05/01 12:53:33 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -18,8 +18,8 @@
|
||||
|
||||
using namespace webserv::server;
|
||||
|
||||
Client::Client(struct pollfd *pfd, sockaddr_in socket, config::Config *conf)
|
||||
: _pfd(pfd), _client_addr(socket), _Gconf(conf) {
|
||||
Client::Client(struct pollfd *pfd, config::Server *conf)
|
||||
: _pfd(pfd), _conf(conf) {
|
||||
_request = not_nullptr;
|
||||
log("➕", "Client", "constructor called");
|
||||
}
|
||||
@ -47,18 +47,17 @@ void Client::parse(void) {
|
||||
|
||||
_getRequest(received_data);
|
||||
|
||||
this->_conf = this->_Gconf->getServer(this->_request->getHeaders()["Host"]);
|
||||
this->_route = this->_conf->whatRoute(URL(this->_request->getTarget()));
|
||||
std::cout << "_route is " << (_route ? "not null" : "NULL") << std::endl;
|
||||
if(!this->_route || this->_route == not_nullptr)
|
||||
{
|
||||
_route = _conf->whatRoute(URL(this->_request->getTarget()));
|
||||
/* std::cout << "_route is " << (_route ? "not null" : "NULL") << std::endl;
|
||||
*/
|
||||
if (!this->_route || this->_route == not_nullptr) {
|
||||
this->_request->setMethod("404");
|
||||
return ;
|
||||
return;
|
||||
}
|
||||
|
||||
if ((this->_request->getMethod() == "GET" && !_route->getMethods()[0])
|
||||
|| (this->_request->getMethod() == "POST" && !_route->getMethods()[1])
|
||||
|| (this->_request->getMethod() == "DELETE" && !_route->getMethods()[2]))
|
||||
if ((this->_request->getMethod() == "GET" && !_route->getMethods()[0]) ||
|
||||
(this->_request->getMethod() == "POST" && !_route->getMethods()[1]) ||
|
||||
(this->_request->getMethod() == "DELETE" && !_route->getMethods()[2]))
|
||||
this->_request->setMethod("405");
|
||||
|
||||
if (received_data.length() > (unsigned long)(_route->getMaxBody()))
|
||||
@ -75,23 +74,16 @@ void Client::_getRequest(std::string request_str) {
|
||||
std::string method = request_str.substr(
|
||||
0, request_str.substr(0, 4).find_last_not_of(" ") + 1);
|
||||
|
||||
if (method == "GET")
|
||||
{
|
||||
if (method == "GET") {
|
||||
this->_request = new http::Get(request_str);
|
||||
_log->info("get request received");
|
||||
}
|
||||
else if (method == "DELETE")
|
||||
{
|
||||
} else if (method == "DELETE") {
|
||||
this->_request = new http::Delete(request_str);
|
||||
_log->info("delete request received");
|
||||
}
|
||||
else if (method == "POST")
|
||||
{
|
||||
} else if (method == "POST") {
|
||||
this->_request = new http::Post(request_str);
|
||||
_log->info("post request received");
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
this->_request = new http::Get();
|
||||
this->_request->setMethod("501");
|
||||
_log->info("unsupported request received");
|
||||
@ -100,7 +92,6 @@ void Client::_getRequest(std::string request_str) {
|
||||
}
|
||||
|
||||
void Client::answer(void) {
|
||||
(void)_client_addr;
|
||||
std::string response;
|
||||
|
||||
if (_request == not_nullptr) {
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/11 16:11:40 by adjoly #+# #+# */
|
||||
/* Updated: 2025/04/30 15:41:46 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/01 12:52:46 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -121,7 +121,6 @@ void Server::_run(void) {
|
||||
size_t i = 0;
|
||||
for (auto it = range(_fds_server), i++) {
|
||||
if (_client_fds[i].revents & POLLIN) {
|
||||
_log->info("polliiiiiiiiiiiiiiiinnnnnnnnnnnnnnnn");
|
||||
struct sockaddr_in client_addr;
|
||||
socklen_t addrlen = sizeof(client_addr);
|
||||
int client_fd =
|
||||
@ -137,8 +136,15 @@ void Server::_run(void) {
|
||||
_log->error(str.str());
|
||||
continue;
|
||||
}
|
||||
|
||||
_log->info("connection accepted");
|
||||
config::Server *conf_srv = _conf->getServer(i);
|
||||
if (conf_srv == not_nullptr) {
|
||||
_log->warn(
|
||||
"where the f does he come from"); // does can't actually
|
||||
// happen but just in
|
||||
// case
|
||||
close(client_fd);
|
||||
continue;
|
||||
}
|
||||
|
||||
struct pollfd pfd;
|
||||
pfd.fd = client_fd;
|
||||
@ -147,7 +153,7 @@ void Server::_run(void) {
|
||||
_client_fds.push_back(pfd);
|
||||
struct pollfd *ppfd =
|
||||
_client_fds.data() + _client_fds.size() - 1;
|
||||
Client *new_client = new Client(ppfd, client_addr, _conf);
|
||||
Client *new_client = new Client(ppfd, conf_srv);
|
||||
if (new_client == NULL) {
|
||||
continue;
|
||||
}
|
||||
@ -195,7 +201,7 @@ void Server::_run(void) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
i++;
|
||||
i--;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -214,7 +220,7 @@ Server::Server(config::Config *conf) : _conf(conf) {
|
||||
|
||||
Server::~Server(void) {
|
||||
log("➖", "Server::Server", "destructor called");
|
||||
for (std::vector<struct pollfd>::iterator it = _client_fds.begin();
|
||||
it != _client_fds.end(); it++)
|
||||
for (auto it = range(_client_fds)) {
|
||||
close(it->fd);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user