mirror of
https://github.com/KeyZox71/webserv.git
synced 2025-06-25 09:33:36 +02:00
「✨」 feat: implemented succecfully that f*cking pfdmanager
This commit is contained in:
@ -39,7 +39,7 @@
|
||||
fetchSubmodules = true; # need it for tomlpp
|
||||
};
|
||||
buildInputs = with pkgs; [
|
||||
clang
|
||||
clang_12
|
||||
];
|
||||
buildPhase = ''
|
||||
PKGS=true make -j
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/03/19 14:59:41 by adjoly #+# #+# */
|
||||
/* Updated: 2025/05/26 17:02:12 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/27 09:37:23 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -38,6 +38,14 @@ class Route {
|
||||
std::string getIndex(void) { return _index; }
|
||||
std::vector<std::string> *getCgi(void) { return _cgi; }
|
||||
bool * getMethods(void) { return _methods; }
|
||||
bool isCgi(std::string target) {
|
||||
std::string target_ext = target.substr(target.find('.'));
|
||||
for (auto it = prange(_cgi)) {
|
||||
if (target_ext == *it)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected:
|
||||
private:
|
||||
|
@ -6,13 +6,17 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/30 09:30:15 by adjoly #+# #+# */
|
||||
/* Updated: 2025/05/03 12:07:57 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/27 16:49:00 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <requests/ARequest.hpp>
|
||||
|
||||
namespace webserv {
|
||||
namespace server {
|
||||
class Cgi;
|
||||
class CgiIn;
|
||||
}; // namespace server
|
||||
namespace http {
|
||||
|
||||
class Get : public ARequest {
|
||||
@ -23,6 +27,9 @@ class Get : public ARequest {
|
||||
void parse(std::string const &data);
|
||||
|
||||
Response execute(void);
|
||||
|
||||
// private:
|
||||
// server::Cgi *_cgi;
|
||||
};
|
||||
|
||||
class Post : public ARequest {
|
||||
@ -37,9 +44,12 @@ class Post : public ARequest {
|
||||
const std::string &boundary);
|
||||
|
||||
Response execute(void);
|
||||
|
||||
// private:
|
||||
// server::Cgi *_cgi;
|
||||
};
|
||||
|
||||
class Delete : public http::ARequest {
|
||||
class Delete : public ARequest {
|
||||
public:
|
||||
Delete(void) {}
|
||||
Delete(std::string &data);
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/29 14:20:09 by mmoussou #+# #+# */
|
||||
/* Updated: 2025/05/24 10:09:33 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/27 13:12:03 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -25,19 +25,22 @@ class AClientResource {
|
||||
virtual ~AClientResource() {}
|
||||
|
||||
bool operator==(int i) const {
|
||||
if (i == _fd->fd)
|
||||
if (i == _fd)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
void setFileDescriptor(struct pollfd *fd) { _fd = fd; }
|
||||
struct pollfd getFileDescriptor(void) const { return *_fd; }
|
||||
|
||||
virtual clientResType type(void) const = 0;
|
||||
int getId(void) const { return _fd->fd; }
|
||||
int getId(void) const { return _fd; }
|
||||
|
||||
virtual void process(void) = 0;
|
||||
bool isProcessed(void) const { return _processed; }
|
||||
virtual short event(void) const = 0;
|
||||
|
||||
protected:
|
||||
struct pollfd *_fd;
|
||||
int _fd;
|
||||
bool _processed;
|
||||
short _pfd_event;
|
||||
};
|
||||
|
||||
} // namespace server
|
||||
|
@ -6,23 +6,26 @@
|
||||
/* By: gadelbes <gadelbes@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/24 13:46:34 by gadelbes #+# #+# */
|
||||
/* Updated: 2025/05/26 17:19:01 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/27 16:16:08 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <config/Route.hpp>
|
||||
#include "server/AResource.hpp"
|
||||
#include <config/default.hpp>
|
||||
#include <cstdio>
|
||||
#include <requests/default.hpp>
|
||||
#include <server/AResource.hpp>
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace webserv {
|
||||
namespace http {
|
||||
class Get;
|
||||
class Post;
|
||||
class ARequest;
|
||||
}; // namespace http
|
||||
namespace server {
|
||||
|
||||
#define PIPE_READ 0
|
||||
@ -34,19 +37,13 @@ class Cgi : public server::AClientResource {
|
||||
Cgi(webserv::http::Post *, webserv::config::Route *);
|
||||
~Cgi(void);
|
||||
|
||||
clientResType type(void) const { return CGI; }
|
||||
|
||||
/**
|
||||
* @brief Can be used to process the Cgi script
|
||||
*/
|
||||
void process(void);
|
||||
|
||||
/**
|
||||
* @brief Can be used to check if the Cgi have been executed
|
||||
*
|
||||
* @note Need to be used after the process() function and checked before
|
||||
* using str()
|
||||
*/
|
||||
bool isProcessed(void) { return _executed; }
|
||||
|
||||
/**
|
||||
* @brief Can be used to get the output of the Cgi
|
||||
*
|
||||
@ -54,9 +51,10 @@ class Cgi : public server::AClientResource {
|
||||
*/
|
||||
std::string str(void);
|
||||
|
||||
short event(void) const { return POLLIN; }
|
||||
|
||||
protected:
|
||||
private:
|
||||
bool _executed;
|
||||
bool _is_post;
|
||||
|
||||
void _initEnvp(void);
|
||||
@ -77,8 +75,7 @@ class Cgi : public server::AClientResource {
|
||||
|
||||
std::map<std::string, std::string> _envp; // The envp filled with _initEnvp
|
||||
webserv::config::Route *_conf; // The configuration for the route used
|
||||
webserv::http::ARequest
|
||||
*_request; // The requests that will be used for the cgi
|
||||
http::ARequest *_request; // The requests that will be used for the cgi
|
||||
|
||||
int _stdin_pipe[2]; // The pipefd for the stdin of the cgi in the case of a
|
||||
// post
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/05/13 18:14:45 by adjoly #+# #+# */
|
||||
/* Updated: 2025/05/24 11:15:25 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/27 13:08:08 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -14,6 +14,7 @@
|
||||
|
||||
#include "server/AResource.hpp"
|
||||
#include <log.hpp>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace webserv {
|
||||
namespace server {
|
||||
@ -22,16 +23,23 @@ class CgiIn : public AClientResource {
|
||||
public:
|
||||
CgiIn(std::string body, int id) : _body(body) {
|
||||
log("➕", "CgiIn", "constructor called");
|
||||
_fd->fd = id;
|
||||
_fd->events = POLLOUT;
|
||||
_processed = false;
|
||||
_fd = id;
|
||||
_pfd_event = POLLOUT;
|
||||
}
|
||||
~CgiIn(void) {
|
||||
log("➖", "CgiIn", "destructor called");
|
||||
close(_fd);
|
||||
}
|
||||
~CgiIn(void) { log("➖", "CgiIn", "destructor called"); }
|
||||
|
||||
void send(void) {
|
||||
void process(void) {
|
||||
_processed = true;
|
||||
// TODO: send the body
|
||||
}
|
||||
clientResType type(void) const { return CGI_IN; }
|
||||
|
||||
short event(void) const { return POLLIN; }
|
||||
|
||||
protected:
|
||||
private:
|
||||
std::string _body;
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/14 14:14:39 by adjoly #+# #+# */
|
||||
/* Updated: 2025/05/02 13:21:10 by mmoussou ### ########.fr */
|
||||
/* Updated: 2025/05/27 16:47:20 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -24,7 +24,7 @@ namespace server {
|
||||
|
||||
class Client {
|
||||
public:
|
||||
Client(struct pollfd *, config::Server *);
|
||||
Client(int, config::Server *);
|
||||
Client(const Client &cpy);
|
||||
virtual ~Client(void);
|
||||
|
||||
@ -33,15 +33,15 @@ class Client {
|
||||
|
||||
bool requestParsed(void);
|
||||
|
||||
struct pollfd *getPollfd(void) const { return _pfd; }
|
||||
int getPollfd(void) const { return _fd; }
|
||||
|
||||
bool operator==(int fd) {
|
||||
if (fd != _pfd->fd)
|
||||
if (fd != _fd)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool isReadyToClose() const;
|
||||
bool isReadyToClose() const;
|
||||
|
||||
private:
|
||||
void _getRequest(std::string);
|
||||
@ -55,11 +55,11 @@ class Client {
|
||||
return newStr;
|
||||
}
|
||||
|
||||
struct pollfd *_pfd;
|
||||
int _fd;
|
||||
http::ARequest *_request;
|
||||
http::Response _response;
|
||||
config::Server *_conf;
|
||||
config::Route *_route;
|
||||
config::Route * _route;
|
||||
size_t _bytes_sent;
|
||||
std::string _response_str;
|
||||
bool _response_done;
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/05/13 18:14:45 by adjoly #+# #+# */
|
||||
/* Updated: 2025/05/24 11:06:33 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/27 13:07:50 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -20,8 +20,8 @@ namespace server {
|
||||
class FileUpload : public AClientResource {
|
||||
public:
|
||||
FileUpload(int id) {
|
||||
_fd->fd = id;
|
||||
_fd->events = POLLOUT;
|
||||
_fd = id;
|
||||
_pfd_event = POLLOUT;
|
||||
}
|
||||
~FileUpload(void) {}
|
||||
|
||||
|
103
includes/server/PfdManager.hpp
Normal file
103
includes/server/PfdManager.hpp
Normal file
@ -0,0 +1,103 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* PfdManager.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/05/27 17:01:01 by adjoly #+# #+# */
|
||||
/* Updated: 2025/05/27 17:59:37 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <iterator>
|
||||
#include <stdexcept>
|
||||
#include <webserv.hpp>
|
||||
|
||||
#include <poll.h>
|
||||
#include <vector>
|
||||
|
||||
namespace webserv {
|
||||
namespace server {
|
||||
|
||||
enum pfd_type { SRV, CLIENT, RES };
|
||||
|
||||
class ComparePfd {
|
||||
public:
|
||||
ComparePfd(int id) : _id(id) {}
|
||||
|
||||
bool operator()(const struct pollfd res) const { return res.fd == _id; }
|
||||
|
||||
private:
|
||||
int _id;
|
||||
};
|
||||
|
||||
class PfdManager {
|
||||
public:
|
||||
static struct pollfd at(int i) { return _pfd_vec[i]; }
|
||||
|
||||
static struct pollfd *get(int id) {
|
||||
auto it =
|
||||
std::find_if(_pfd_vec.begin(), _pfd_vec.end(), ComparePfd(id));
|
||||
|
||||
if (it != _pfd_vec.end())
|
||||
return &*it;
|
||||
throw std::out_of_range("pollfd does not exist for client D:");
|
||||
}
|
||||
|
||||
static pfd_type getType(int id) {
|
||||
auto it =
|
||||
std::find_if(_pfd_vec.begin(), _pfd_vec.end(), ComparePfd(id));
|
||||
|
||||
if (it != _pfd_vec.end()) {
|
||||
size_t i = std::distance(_pfd_vec.begin(), it);
|
||||
try {
|
||||
auto it = _pfd_type.at(i);
|
||||
return it;
|
||||
} catch (std::out_of_range &) {
|
||||
throw std::out_of_range("pollfd does not exist for client D:");
|
||||
}
|
||||
}
|
||||
throw std::out_of_range("pollfd does not exist for client D:");
|
||||
}
|
||||
|
||||
static void append(struct pollfd pfd, pfd_type type) {
|
||||
_pfd_vec.push_back(pfd);
|
||||
_pfd_type.push_back(type);
|
||||
}
|
||||
|
||||
static void remove(int id) {
|
||||
auto it =
|
||||
std::find_if(_pfd_vec.begin(), _pfd_vec.end(), ComparePfd(id));
|
||||
|
||||
if (it != _pfd_vec.end()) {
|
||||
auto type = _pfd_type.begin() + std::distance(_pfd_vec.begin(), it);
|
||||
_pfd_type.erase(type);
|
||||
close(it->fd);
|
||||
_pfd_vec.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
static struct pollfd *data(void) { return _pfd_vec.data(); }
|
||||
static size_t size(void) { return _pfd_vec.size(); }
|
||||
|
||||
static void clear(void) {
|
||||
for (auto it = range(_pfd_vec)) {
|
||||
close(it->fd);
|
||||
}
|
||||
_pfd_vec.clear();
|
||||
_pfd_type.clear();
|
||||
}
|
||||
|
||||
protected:
|
||||
private:
|
||||
static std::vector<struct pollfd> _pfd_vec;
|
||||
static std::vector<pfd_type> _pfd_type;
|
||||
};
|
||||
|
||||
} // namespace server
|
||||
} // namespace webserv
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/05/12 17:13:39 by adjoly #+# #+# */
|
||||
/* Updated: 2025/05/24 11:00:26 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/27 17:24:51 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -15,6 +15,7 @@
|
||||
#include <algorithm>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <sys/poll.h>
|
||||
#include <vector>
|
||||
|
||||
#include <cppeleven.hpp>
|
||||
@ -37,6 +38,7 @@ class CompareId {
|
||||
int _id;
|
||||
};
|
||||
|
||||
|
||||
class ResourceManager {
|
||||
public:
|
||||
/**
|
||||
@ -79,6 +81,13 @@ class ResourceManager {
|
||||
}
|
||||
}
|
||||
|
||||
static void process(void) {
|
||||
for (auto it = range(_res)) {
|
||||
(*it)->process();
|
||||
// TODO: check for event and if isProcessed() and process
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
private:
|
||||
static std::vector<AClientResource *> _res;
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/11 17:45:43 by adjoly #+# #+# */
|
||||
/* Updated: 2025/04/26 16:36:05 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/27 17:43:21 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -77,7 +77,7 @@ class Server {
|
||||
*_conf; // Pointer to the configuration class (with all config in)
|
||||
Logger *_log; // Pointer to the log class
|
||||
std::vector<int> _fds_server; // The fds of the sockets
|
||||
std::vector<struct pollfd> _client_fds; // A vector of all the poll fd
|
||||
// std::vector<struct pollfd> _client_fds; // A vector of all the poll fd
|
||||
std::vector<Client *> _client_data; // vector of all the client sockaddr_in
|
||||
};
|
||||
|
||||
|
@ -6,11 +6,12 @@
|
||||
/* By: gadelbes <gadelbes@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/24 13:46:34 by gadelbes #+# #+# */
|
||||
/* Updated: 2025/05/26 17:22:12 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/27 13:08:36 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <help.hpp>
|
||||
#include <ios>
|
||||
#include <requests/default.hpp>
|
||||
#include <server/default.hpp>
|
||||
|
||||
@ -22,6 +23,7 @@
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <sys/poll.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
@ -30,18 +32,20 @@ using namespace webserv::server;
|
||||
|
||||
// WARN: construtor will probably be changed and practicly do nothing
|
||||
Cgi::Cgi(http::Get *req, config::Route *conf)
|
||||
: _executed(false), _is_post(false), _conf(conf), _request(req) {
|
||||
: _is_post(false), _conf(conf), _request(req) {
|
||||
_processed = false;
|
||||
log("➕", "Cgi", "GET constructor called");
|
||||
_initEnvp();
|
||||
_prep();
|
||||
}
|
||||
|
||||
Cgi::Cgi(http::Post *req, config::Route *conf)
|
||||
: _executed(false), _is_post(true), _conf(conf), _request(req) {
|
||||
: _is_post(true), _conf(conf), _request(req) {
|
||||
_processed = false;
|
||||
log("➕", "Cgi", "POST constructor called");
|
||||
_initEnvp();
|
||||
_prep();
|
||||
CgiIn *in = new CgiIn(_request->getBody(), _stdin_pipe[STDOUT_FILENO]);
|
||||
AClientResource *in = new CgiIn(_request->getBody(), _stdin_pipe[PIPE_WRITE]);
|
||||
ResourceManager::append(in);
|
||||
}
|
||||
|
||||
@ -53,8 +57,8 @@ void Cgi::_prep(void) {
|
||||
throw std::runtime_error("stdin pipe failed for cgi D:");
|
||||
if (pipe(_stdout_pipe) == -1)
|
||||
throw std::runtime_error("stdout pipe failed for cgi D:");
|
||||
_fd->fd = _stdout_pipe[STDIN_FILENO];
|
||||
_fd->events = POLLIN;
|
||||
_fd= _stdout_pipe[STDIN_FILENO];
|
||||
_pfd_event = POLLIN;
|
||||
}
|
||||
|
||||
void Cgi::_initEnvp(void) {
|
||||
@ -118,19 +122,23 @@ char **Cgi::_genEnv(void) {
|
||||
}
|
||||
|
||||
void Cgi::process(void) {
|
||||
_processed = true;
|
||||
pid_t forkPid;
|
||||
|
||||
if (access(_script_path.c_str(), X_OK))
|
||||
throw std::runtime_error(
|
||||
"script is not executable please run : chmod +x " + _script_path);
|
||||
forkPid = fork();
|
||||
if (forkPid < 0)
|
||||
throw std::runtime_error("fork failed D:");
|
||||
else if (forkPid == 0) {
|
||||
dup2(_stdin_pipe[0], STDIN_FILENO);
|
||||
close(_stdin_pipe[0]);
|
||||
close(_stdin_pipe[1]);
|
||||
dup2(_stdin_pipe[PIPE_READ], STDIN_FILENO);
|
||||
close(_stdin_pipe[PIPE_READ]);
|
||||
close(_stdin_pipe[PIPE_WRITE]);
|
||||
|
||||
dup2(_stdout_pipe[1], STDOUT_FILENO);
|
||||
close(_stdout_pipe[0]);
|
||||
close(_stdout_pipe[1]);
|
||||
dup2(_stdout_pipe[PIPE_WRITE], STDOUT_FILENO);
|
||||
close(_stdout_pipe[PIPE_READ]);
|
||||
close(_stdout_pipe[PIPE_WRITE]);
|
||||
|
||||
char * argv[] = {const_cast<char *>(_script_path.c_str()), NULL};
|
||||
char **env = _genEnv();
|
||||
@ -146,10 +154,9 @@ void Cgi::process(void) {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
close(_stdin_pipe[0]);
|
||||
close(_stdout_pipe[1]);
|
||||
close(_stdin_pipe[PIPE_READ]);
|
||||
close(_stdout_pipe[PIPE_WRITE]);
|
||||
waitpid(forkPid, NULL, 0);
|
||||
_executed = true;
|
||||
}
|
||||
|
||||
std::string Cgi::str(void) {
|
||||
@ -165,5 +172,6 @@ std::string Cgi::str(void) {
|
||||
break;
|
||||
}
|
||||
str.append("\0");
|
||||
ResourceManager::remove(_stdin_pipe[PIPE_WRITE]);
|
||||
return str;
|
||||
}
|
||||
|
@ -6,21 +6,26 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/30 09:40:16 by adjoly #+# #+# */
|
||||
/* Updated: 2025/05/04 12:25:43 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/27 16:49:05 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "cppeleven.hpp"
|
||||
#include <algorithm>
|
||||
#include <dirent.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <vector>
|
||||
|
||||
#include <server/default.hpp>
|
||||
#include <requests/default.hpp>
|
||||
|
||||
using namespace webserv::http;
|
||||
|
||||
Get::Get(std::string &data) { this->parse(data); }
|
||||
Get::Get(std::string &data) {
|
||||
// _cgi = not_nullptr;
|
||||
this->parse(data);
|
||||
}
|
||||
|
||||
void Get::parse(std::string const &data) {
|
||||
std::istringstream stream(data);
|
||||
@ -50,8 +55,10 @@ void Get::parse(std::string const &data) {
|
||||
|
||||
_url = new URL("http://" + _headers["Host"] + _target);
|
||||
|
||||
/*std::cout << "wtf = " << _headers["Host"] << std::endl;
|
||||
std::cout << *_url << std::endl;*/
|
||||
// if (_route->isCgi(_target)) {
|
||||
// _cgi = new server::Cgi(this, _route);
|
||||
// server::ResourceManager::append(_cgi);
|
||||
// }
|
||||
|
||||
/*
|
||||
std::cout << "-- start-line --" << std::endl;
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/30 09:50:20 by adjoly #+# #+# */
|
||||
/* Updated: 2025/05/03 09:44:41 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/27 09:23:54 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -84,7 +84,6 @@ void Post::handleMultipartData(const std::string &body, const std::string &bound
|
||||
|
||||
if (end != std::string::npos) {
|
||||
std::string part_header = body.substr(start, end - start);
|
||||
// std::cout << std::endl << std::endl << std::endl << std::endl;
|
||||
std::string part_content =
|
||||
body.substr(end + 4, body.find(delim, end) - end - 4);
|
||||
|
||||
@ -94,7 +93,7 @@ void Post::handleMultipartData(const std::string &body, const std::string &bound
|
||||
outfile.write(part_content.c_str(), part_content.length());
|
||||
outfile.close();
|
||||
} else {
|
||||
std::cerr << "open failed" << std::endl;
|
||||
_log->error("open failed D:");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/17 11:12:41 by mmoussou #+# #+# */
|
||||
/* Updated: 2025/05/22 17:36:36 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/27 16:48:09 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -19,8 +19,8 @@
|
||||
|
||||
using namespace webserv::server;
|
||||
|
||||
Client::Client(struct pollfd *pfd, config::Server *conf)
|
||||
: _pfd(pfd), _conf(conf) {
|
||||
Client::Client(int fd, config::Server *conf)
|
||||
: _fd(fd), _conf(conf) {
|
||||
_request = not_nullptr;
|
||||
log("➕", "Client", "constructor called");
|
||||
_response_done = false;
|
||||
@ -28,7 +28,7 @@ Client::Client(struct pollfd *pfd, config::Server *conf)
|
||||
|
||||
Client::Client(const Client &cpy) {
|
||||
if (this != &cpy) {
|
||||
_pfd->fd = cpy._pfd->fd;
|
||||
_fd = cpy._fd;
|
||||
}
|
||||
}
|
||||
|
||||
@ -39,7 +39,7 @@ void Client::parse(void) {
|
||||
|
||||
do {
|
||||
std::memset(buffer, 0, BUFFER_SIZE);
|
||||
bytes_received = recv(_pfd->fd, buffer, BUFFER_SIZE - 1, 0);
|
||||
bytes_received = recv(_fd, buffer, BUFFER_SIZE - 1, 0);
|
||||
if (bytes_received == -1) {
|
||||
_log->error("failed to receive request");
|
||||
throw std::runtime_error("failed to receive request");
|
||||
@ -135,7 +135,7 @@ void Client::answer(void) {
|
||||
_bytes_sent = 0;
|
||||
}
|
||||
|
||||
ssize_t sent = send(_pfd->fd, _response_str.c_str() + _bytes_sent,
|
||||
ssize_t sent = send(_fd, _response_str.c_str() + _bytes_sent,
|
||||
_response_str.length() - _bytes_sent, 0);
|
||||
|
||||
if (sent == -1) {
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/11 16:11:40 by adjoly #+# #+# */
|
||||
/* Updated: 2025/05/22 17:36:13 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/27 18:03:35 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -22,6 +22,7 @@
|
||||
#include <netinet/in.h>
|
||||
#include <poll.h>
|
||||
#include <requests/default.hpp>
|
||||
#include <server/PfdManager.hpp>
|
||||
#include <server/default.hpp>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
@ -29,8 +30,12 @@
|
||||
#include <sys/socket.h>
|
||||
#include <webserv.hpp>
|
||||
|
||||
|
||||
using namespace webserv::server;
|
||||
|
||||
std::vector<struct pollfd> PfdManager::_pfd_vec;
|
||||
std::vector<pfd_type> PfdManager::_pfd_type;
|
||||
|
||||
extern int _sig;
|
||||
|
||||
Client *Server::_getClient(int fd) {
|
||||
@ -62,7 +67,7 @@ std::string getMethod(std::string &data) {
|
||||
}
|
||||
|
||||
int Server::_fillHostsPorts(std::vector<std::string> &hosts,
|
||||
std::vector<int> &ports) {
|
||||
std::vector<int> & ports) {
|
||||
std::vector<config::Server *> config = _conf->getServers();
|
||||
|
||||
for (auto it = range(config)) {
|
||||
@ -101,13 +106,13 @@ void Server::_run(void) {
|
||||
it != _fds_server.end(); it++) {
|
||||
fd.fd = *it;
|
||||
fd.events = POLLIN;
|
||||
_client_fds.push_back(fd);
|
||||
PfdManager::append(fd, SRV);
|
||||
_log->debug("new socket in poll");
|
||||
}
|
||||
|
||||
// to add signal instead of 727
|
||||
while (727 - sigHandling()) {
|
||||
if (poll(_client_fds.data(), _client_fds.size(), 5000) < 0) {
|
||||
if (poll(PfdManager::data(), PfdManager::size(), 5000) < 0) {
|
||||
if (errno == EINTR) {
|
||||
continue;
|
||||
}
|
||||
@ -120,7 +125,7 @@ void Server::_run(void) {
|
||||
|
||||
size_t i = 0;
|
||||
for (auto it = range(_fds_server), i++) {
|
||||
if (_client_fds[i].revents & POLLIN) {
|
||||
if (PfdManager::at(i).revents & POLLIN) {
|
||||
struct sockaddr_in client_addr;
|
||||
socklen_t addrlen = sizeof(client_addr);
|
||||
int client_fd =
|
||||
@ -150,10 +155,8 @@ void Server::_run(void) {
|
||||
pfd.fd = client_fd;
|
||||
pfd.events = POLLIN | POLLOUT;
|
||||
pfd.revents = 0;
|
||||
_client_fds.push_back(pfd);
|
||||
struct pollfd *ppfd =
|
||||
_client_fds.data() + _client_fds.size() - 1;
|
||||
Client *new_client = new Client(ppfd, conf_srv);
|
||||
PfdManager::append(pfd, CLIENT);
|
||||
Client *new_client = new Client(pfd.fd, conf_srv);
|
||||
if (new_client == NULL) {
|
||||
continue;
|
||||
}
|
||||
@ -162,16 +165,16 @@ void Server::_run(void) {
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t i = _fds_server.size(); i < _client_fds.size(); ++i) {
|
||||
if (_client_fds[i].revents & POLLERR) {
|
||||
for (size_t i = _fds_server.size(); i < PfdManager::size(); ++i) {
|
||||
if (PfdManager::at(i).revents & POLLERR) {
|
||||
_log->debug("pollerr");
|
||||
close(_client_fds[i].fd);
|
||||
_client_fds.erase(_client_fds.begin() + i);
|
||||
close(PfdManager::at(i).fd);
|
||||
PfdManager::remove(PfdManager::at(i).fd);
|
||||
delete _client_data[i - _fds_server.size()];
|
||||
_client_data.erase(_client_data.begin() + i);
|
||||
} else if (_client_fds[i].revents & POLLIN) {
|
||||
} else if (PfdManager::at(i).revents & POLLIN) {
|
||||
_log->debug("pollin");
|
||||
Client *client = _getClient(_client_fds[i].fd);
|
||||
Client *client = _getClient(PfdManager::at(i).fd);
|
||||
if (client == not_nullptr) {
|
||||
_log->error("client does not exist");
|
||||
continue;
|
||||
@ -183,21 +186,24 @@ void Server::_run(void) {
|
||||
_client_data.erase(std::find(_client_data.begin(),
|
||||
_client_data.end(), client));
|
||||
delete client;
|
||||
for (auto it = range(_client_fds)) {
|
||||
if (_client_fds[i].fd == (*it).fd) {
|
||||
_log->debug("client fds erased");
|
||||
close(it.base()->fd);
|
||||
_client_fds.erase(it);
|
||||
break;
|
||||
}
|
||||
}
|
||||
// for (auto it = range(_client_fds)) {
|
||||
// if (_client_fds[i].fd == (*it).fd) {
|
||||
// _log->debug("client fds erased");
|
||||
// close(it.base()->fd);
|
||||
// _client_fds.erase(it);
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
close(PfdManager::at(i).fd);
|
||||
PfdManager::remove(PfdManager::at(i).fd);
|
||||
_log->debug("client removed");
|
||||
i--;
|
||||
}
|
||||
} else if (_client_fds[i].revents & POLLOUT) {
|
||||
} else if (PfdManager::at(i).revents & POLLOUT) {
|
||||
std::stringstream str;
|
||||
str << _client_fds[i].fd;
|
||||
str << PfdManager::at(i).fd;
|
||||
_log->debug("pollout = " + str.str());
|
||||
Client *client = _getClient(_client_fds[i].fd);
|
||||
Client *client = _getClient(PfdManager::at(i).fd);
|
||||
|
||||
if (client == not_nullptr) {
|
||||
_log->error("client does not exist");
|
||||
@ -213,14 +219,18 @@ void Server::_run(void) {
|
||||
_client_data.erase(std::find(_client_data.begin(),
|
||||
_client_data.end(), client));
|
||||
delete client;
|
||||
for (auto it = range(_client_fds)) {
|
||||
if (_client_fds[i].fd == (*it).fd) {
|
||||
_log->debug("client fds erased");
|
||||
close(it.base()->fd);
|
||||
_client_fds.erase(it);
|
||||
break;
|
||||
}
|
||||
}
|
||||
// for (auto it = range(_client_fds)) {
|
||||
// if (_client_fds[i].fd == (*it).fd) {
|
||||
// _log->debug("client fds erased");
|
||||
// close(it.base()->fd);
|
||||
// _client_fds.erase(it);
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
close(PfdManager::at(i).fd);
|
||||
PfdManager::remove(PfdManager::at(i).fd);
|
||||
_log->debug("client removed");
|
||||
|
||||
i--;
|
||||
}
|
||||
}
|
||||
@ -241,7 +251,5 @@ Server::Server(config::Config *conf) : _conf(conf) {
|
||||
|
||||
Server::~Server(void) {
|
||||
log("➖", "Server::Server", "destructor called");
|
||||
for (auto it = range(_client_fds)) {
|
||||
close(it->fd);
|
||||
}
|
||||
PfdManager::clear();
|
||||
}
|
||||
|
25
test.py
Normal file
25
test.py
Normal file
@ -0,0 +1,25 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
# Import modules for CGI handling
|
||||
import cgi
|
||||
import cgitb
|
||||
|
||||
# Enable error reporting
|
||||
cgitb.enable()
|
||||
|
||||
# Create instance of FieldStorage
|
||||
form = cgi.FieldStorage()
|
||||
|
||||
# Set the content type to HTML
|
||||
print("Content-Type: text/html\n")
|
||||
|
||||
# Output a simple HTML page
|
||||
print("<html>")
|
||||
print("<head>")
|
||||
print("<title>CGI Script Test</title>")
|
||||
print("</head>")
|
||||
print("<body>")
|
||||
print("<h1>CGI Script is Running</h1>")
|
||||
print("<p>Your web server is working correctly!</p>")
|
||||
print("</body>")
|
||||
print("</html>")
|
Reference in New Issue
Block a user