mirror of
https://github.com/KeyZox71/webserv.git
synced 2025-06-25 09:33:36 +02:00
「🏗️」 wip: cgi should be able to be implemented
This commit is contained in:
@ -6,7 +6,7 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/29 14:20:09 by mmoussou #+# #+# */
|
||||
/* Updated: 2025/05/23 18:27:06 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/24 10:09:33 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -25,7 +25,7 @@ class AClientResource {
|
||||
virtual ~AClientResource() {}
|
||||
|
||||
bool operator==(int i) const {
|
||||
if (i == _res_id)
|
||||
if (i == _fd->fd)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
@ -34,12 +34,10 @@ class AClientResource {
|
||||
struct pollfd getFileDescriptor(void) const { return *_fd; }
|
||||
|
||||
virtual clientResType type(void) const = 0;
|
||||
int getId(void) const { return _res_id; }
|
||||
int getId(void) const { return _fd->fd; }
|
||||
|
||||
protected:
|
||||
struct pollfd *_fd;
|
||||
|
||||
int _res_id;
|
||||
};
|
||||
|
||||
} // namespace server
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: gadelbes <gadelbes@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/24 13:46:34 by gadelbes #+# #+# */
|
||||
/* Updated: 2025/05/23 18:24:29 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/24 11:18:49 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -25,27 +25,15 @@
|
||||
namespace webserv {
|
||||
namespace server {
|
||||
|
||||
#define PIPE_READ 0
|
||||
#define PIPE_WRITE 1
|
||||
|
||||
class Cgi : public server::AClientResource {
|
||||
public:
|
||||
Cgi(webserv::http::Get *, webserv::config::Route *, int);
|
||||
Cgi(webserv::http::Post *, webserv::config::Route *, int);
|
||||
Cgi(webserv::http::Get *, webserv::config::Route *);
|
||||
Cgi(webserv::http::Post *, webserv::config::Route *);
|
||||
~Cgi(void);
|
||||
|
||||
int getFdOut(void) const { return _stdout_pipe[0]; }
|
||||
|
||||
/**
|
||||
* @brief Can be used to prepare the Cgi execution
|
||||
*
|
||||
* @note To be used after the main fd is in POLLOUT state
|
||||
*/
|
||||
void prepare(void);
|
||||
|
||||
/**
|
||||
* @brief Can be used to know if the prepare function has already been
|
||||
* called
|
||||
*/
|
||||
bool isPrepared(void) { return _prepared; }
|
||||
|
||||
/**
|
||||
* @brief Can be used to process the Cgi script
|
||||
*/
|
||||
@ -68,12 +56,12 @@ class Cgi : public server::AClientResource {
|
||||
|
||||
protected:
|
||||
private:
|
||||
bool _prepared;
|
||||
bool _executed;
|
||||
bool _is_post;
|
||||
|
||||
void _initEnvp(void);
|
||||
|
||||
void _prepPost(void);
|
||||
void _prep(void);
|
||||
|
||||
/**
|
||||
* @brief Can be used to convert the _envp to a char** usable in execve
|
||||
@ -90,13 +78,13 @@ 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
|
||||
webserv::http::ARequest
|
||||
*_request; // The requests that will be used for the cgi
|
||||
|
||||
int _stdin_pipe[2]; // The pipefd for the stdin of the cgi
|
||||
int _stdin_pipe[2]; // The pipefd for the stdin of the cgi in the case of a
|
||||
// post
|
||||
int _stdout_pipe[2]; // The pipefd for the stdout of the cgi
|
||||
|
||||
int _in_res; // The id of the stdin resource
|
||||
};
|
||||
|
||||
}; // namespace http
|
||||
}; // namespace server
|
||||
}; // namespace webserv
|
||||
|
@ -6,26 +6,35 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/05/13 18:14:45 by adjoly #+# #+# */
|
||||
/* Updated: 2025/05/23 12:51:22 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/24 11:15:25 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "server/AResource.hpp"
|
||||
#include <log.hpp>
|
||||
|
||||
namespace webserv {
|
||||
namespace server {
|
||||
|
||||
class CgiIn : public AClientResource {
|
||||
public:
|
||||
CgiIn(int id) { _res_id = id; }
|
||||
~CgiIn(void) {}
|
||||
CgiIn(std::string body, int id) : _body(body) {
|
||||
log("➕", "CgiIn", "constructor called");
|
||||
_fd->fd = id;
|
||||
_fd->events = POLLOUT;
|
||||
}
|
||||
~CgiIn(void) { log("➖", "CgiIn", "destructor called"); }
|
||||
|
||||
void send(void) {
|
||||
// TODO: send the body
|
||||
}
|
||||
clientResType type(void) const { return CGI_IN; }
|
||||
|
||||
protected:
|
||||
private:
|
||||
std::string _body;
|
||||
};
|
||||
|
||||
} // namespace server
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/05/13 18:14:45 by adjoly #+# #+# */
|
||||
/* Updated: 2025/05/13 18:56:25 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/24 11:06:33 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -19,7 +19,10 @@ namespace server {
|
||||
|
||||
class FileUpload : public AClientResource {
|
||||
public:
|
||||
FileUpload(int id) { _res_id = id; }
|
||||
FileUpload(int id) {
|
||||
_fd->fd = id;
|
||||
_fd->events = POLLOUT;
|
||||
}
|
||||
~FileUpload(void) {}
|
||||
|
||||
clientResType type(void) const { return UP_FILE; }
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/05/12 17:13:39 by adjoly #+# #+# */
|
||||
/* Updated: 2025/05/16 10:18:25 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/24 11:00:26 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -77,7 +77,6 @@ class ResourceManager {
|
||||
delete (*it);
|
||||
_res.erase(it);
|
||||
}
|
||||
// NOTE: throw or not - to see
|
||||
}
|
||||
|
||||
protected:
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/03 15:45:07 by mmoussou #+# #+# */
|
||||
/* Updated: 2025/05/13 10:07:53 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/24 11:20:28 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -19,16 +19,17 @@
|
||||
#include <help.hpp>
|
||||
#include <log.hpp>
|
||||
#include <requests/default.hpp>
|
||||
#include <server/ResourceManager.hpp>
|
||||
#include <server/default.hpp>
|
||||
#include <sstream>
|
||||
#include <tomlpp.hpp>
|
||||
#include <unistd.h>
|
||||
#include <webserv.hpp>
|
||||
#include <server/ResourceManager.hpp>
|
||||
|
||||
namespace webserv {
|
||||
Logger *_log = not_nullptr;
|
||||
}
|
||||
std::vector<server::AClientResource *> server::ResourceManager::_res;
|
||||
} // namespace webserv
|
||||
|
||||
int _sig = 0;
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: gadelbes <gadelbes@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/24 13:46:34 by gadelbes #+# #+# */
|
||||
/* Updated: 2025/05/23 18:26:45 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/24 11:17:22 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -20,6 +20,7 @@
|
||||
#include <cstring>
|
||||
#include <fcntl.h>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
@ -28,32 +29,42 @@
|
||||
using namespace webserv::server;
|
||||
|
||||
// WARN: construtor will probably be changed and practicly do nothing
|
||||
Cgi::Cgi(http::Get *req, config::Route *conf, int id)
|
||||
: _prepared(false), _executed(false), _conf(conf), _request(req) {
|
||||
Cgi::Cgi(http::Get *req, config::Route *conf)
|
||||
: _executed(false), _is_post(false), _conf(conf), _request(req) {
|
||||
log("➕", "Cgi", "GET constructor called");
|
||||
_initEnvp();
|
||||
_res_id = id;
|
||||
_cgi_path = _conf->getCgiPath(req->getTarget());
|
||||
if (_cgi_path == "") {
|
||||
throw;
|
||||
// TODO: need to make something probably will be checked before by
|
||||
// client
|
||||
}
|
||||
if (_cgi_path == "")
|
||||
throw std::runtime_error("Executable path does not exist");
|
||||
if (!access(_cgi_path.c_str(), X_OK))
|
||||
throw std::runtime_error("Executable is not executable " + _cgi_path);
|
||||
_prep();
|
||||
}
|
||||
|
||||
Cgi::Cgi(http::Post *req, config::Route *conf, int id)
|
||||
: _prepared(false), _executed(false), _conf(conf), _request(req) {
|
||||
Cgi::Cgi(http::Post *req, config::Route *conf)
|
||||
: _executed(false), _is_post(true), _conf(conf), _request(req) {
|
||||
log("➕", "Cgi", "POST constructor called");
|
||||
_initEnvp();
|
||||
_res_id = id;
|
||||
_cgi_path = _conf->getCgiPath(req->getTarget());
|
||||
if (_cgi_path == "") {
|
||||
throw;
|
||||
// TODO: need to make something probably will be checked before by
|
||||
// client
|
||||
}
|
||||
if (_cgi_path == "")
|
||||
throw std::runtime_error("");
|
||||
if (!access(_cgi_path.c_str(), X_OK))
|
||||
throw std::runtime_error("Executable is not executable " + _cgi_path);
|
||||
_prep();
|
||||
CgiIn *in = new CgiIn(_request->getBody(), _stdin_pipe[STDOUT_FILENO]);
|
||||
ResourceManager::append(in);
|
||||
_fd->fd = _stdout_pipe[STDIN_FILENO];
|
||||
_fd->events = POLLIN;
|
||||
}
|
||||
|
||||
Cgi::~Cgi(void) {
|
||||
Cgi::~Cgi(void) { log("➖", "Cgi", "destructor called"); }
|
||||
|
||||
void Cgi::_prep(void) {
|
||||
if (_is_post)
|
||||
if (pipe(_stdin_pipe) == -1)
|
||||
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:");
|
||||
}
|
||||
|
||||
void Cgi::_initEnvp(void) {
|
||||
@ -116,16 +127,6 @@ char **Cgi::_genEnv(void) {
|
||||
return newEnv;
|
||||
}
|
||||
|
||||
void Cgi::prepare(void) {
|
||||
if (pipe(_stdin_pipe) == -1 && pipe(_stdout_pipe) == -1) {
|
||||
throw;
|
||||
// TODO: need to make a better throw
|
||||
}
|
||||
_fd->fd = _stdin_pipe[1];
|
||||
_fd->events = POLLOUT;
|
||||
_prepared = true;
|
||||
}
|
||||
|
||||
void Cgi::process(void) {
|
||||
pid_t forkPid;
|
||||
|
||||
|
Reference in New Issue
Block a user