mirror of
https://github.com/KeyZox71/webserv.git
synced 2025-06-25 09:33:36 +02:00
「🏗️」 wip: cgi workinggg
This commit is contained in:
@ -16,6 +16,7 @@ root = "/home/adjoly"
|
||||
dirlist = true
|
||||
client_max_body_size = "10M"
|
||||
index = "index.html"
|
||||
cgi = { ".py", ".go" }
|
||||
|
||||
[server.location./api]
|
||||
methods = { "GET", "POST" }
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/14 12:20:06 by adjoly #+# #+# */
|
||||
/* Updated: 2025/05/03 09:42:35 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/27 19:22:15 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/03/19 14:59:41 by adjoly #+# #+# */
|
||||
/* Updated: 2025/05/27 09:37:23 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/27 20:05:20 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -40,6 +40,9 @@ class Route {
|
||||
bool * getMethods(void) { return _methods; }
|
||||
bool isCgi(std::string target) {
|
||||
std::string target_ext = target.substr(target.find('.'));
|
||||
if (_cgi == not_nullptr)
|
||||
return false;
|
||||
|
||||
for (auto it = prange(_cgi)) {
|
||||
if (target_ext == *it)
|
||||
return true;
|
||||
|
@ -6,12 +6,13 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/22 12:17:48 by adjoly #+# #+# */
|
||||
/* Updated: 2025/05/04 12:21:03 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/27 19:46:54 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "log.hpp"
|
||||
#include <cstring>
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
@ -29,7 +30,7 @@ class URL {
|
||||
}
|
||||
|
||||
int countMatchingSegments(const URL &url) const {
|
||||
if (_path_segments.size() == 0 || url._path_segments.size() == 0)
|
||||
if (_path_segments.size() == 0 || url._path_segments.size() == 0)
|
||||
return 0;
|
||||
int i = 0;
|
||||
|
||||
@ -80,7 +81,7 @@ class URL {
|
||||
}
|
||||
}
|
||||
|
||||
void _splitPath(const std::string &path,
|
||||
void _splitPath(const std::string & path,
|
||||
std::vector<std::string> &segments) const {
|
||||
std::stringstream ss(path);
|
||||
std::string segment;
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/03/19 14:15:51 by adjoly #+# #+# */
|
||||
/* Updated: 2025/04/22 15:28:31 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/27 19:29:59 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -55,5 +55,7 @@ static inline void *accessValue(const std::string &name, toml::nodeType type,
|
||||
}
|
||||
}
|
||||
|
||||
extern config::Config *_conf;
|
||||
|
||||
}; // namespace config
|
||||
}; // namespace webserv
|
||||
|
@ -6,14 +6,16 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/03 17:23:00 by mmoussou #+# #+# */
|
||||
/* Updated: 2025/05/15 12:12:47 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/27 22:22:56 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <config/URL.hpp>
|
||||
#include "config/Server.hpp"
|
||||
#include "cppeleven.hpp"
|
||||
#include <config/Route.hpp>
|
||||
#include <config/URL.hpp>
|
||||
#include <ctime>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
@ -26,13 +28,17 @@
|
||||
#include <config/default.hpp>
|
||||
|
||||
namespace webserv {
|
||||
namespace server {
|
||||
class Cgi;
|
||||
}
|
||||
namespace http {
|
||||
|
||||
class ARequest : public http::IMessage {
|
||||
public:
|
||||
virtual ~ARequest(void) {
|
||||
log("➖", "ARequest", "destructor called");
|
||||
delete _url;
|
||||
if (_url != not_nullptr)
|
||||
delete _url;
|
||||
}
|
||||
|
||||
virtual void parse(std::string const &data) = 0;
|
||||
@ -40,24 +46,27 @@ class ARequest : public http::IMessage {
|
||||
|
||||
std::string str(void) const;
|
||||
|
||||
std::string getMethod(void) const;
|
||||
std::string getTarget(void) const;
|
||||
std::string getProtocol(void) const;
|
||||
webserv::config::Route *getRoute(void) const;
|
||||
URL getUrl() const;
|
||||
std::string getMethod(void) const;
|
||||
std::string getTarget(void) const;
|
||||
std::string getProtocol(void) const;
|
||||
webserv::config::Route *getRoute(void) const;
|
||||
URL getUrl() const;
|
||||
virtual server::Cgi * getCgi() const { return not_nullptr; }
|
||||
|
||||
void setMethod(std::string const method);
|
||||
void setTarget(std::string const target);
|
||||
void setProtocol(std::string const protocol);
|
||||
void setServer(std::string const protocol);
|
||||
void setRoute(config::Route *route);
|
||||
void setSrv(config::Server *srv);
|
||||
|
||||
protected:
|
||||
std::string _method;
|
||||
std::string _target;
|
||||
std::string _protocol;
|
||||
webserv::config::Route *_route;
|
||||
URL *_url;
|
||||
std::string _method;
|
||||
std::string _target;
|
||||
std::string _protocol;
|
||||
webserv::config::Route *_route;
|
||||
config::Server * _srv;
|
||||
URL * _url;
|
||||
|
||||
std::string _sanitizeStr(std::string &str) {
|
||||
std::string newStr = str;
|
||||
|
@ -6,10 +6,11 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/30 09:30:15 by adjoly #+# #+# */
|
||||
/* Updated: 2025/05/27 16:49:00 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/27 22:23:15 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <cstddef>
|
||||
#include <requests/ARequest.hpp>
|
||||
|
||||
namespace webserv {
|
||||
@ -22,16 +23,19 @@ namespace http {
|
||||
class Get : public ARequest {
|
||||
public:
|
||||
Get(void) {}
|
||||
Get(std::string &data);
|
||||
Get(std::string &data, config::Server *srv);
|
||||
|
||||
void parse(std::string const &data);
|
||||
|
||||
Response execute(void);
|
||||
|
||||
// private:
|
||||
// server::Cgi *_cgi;
|
||||
server::Cgi *getCgi() const { return _cgi; }
|
||||
|
||||
private:
|
||||
server::Cgi *_cgi;
|
||||
};
|
||||
|
||||
// TODO: pass _srv to other
|
||||
class Post : public ARequest {
|
||||
public:
|
||||
Post(void) {}
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/29 14:20:09 by mmoussou #+# #+# */
|
||||
/* Updated: 2025/05/27 13:12:03 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/27 18:54:38 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -35,6 +35,7 @@ class AClientResource {
|
||||
|
||||
virtual void process(void) = 0;
|
||||
bool isProcessed(void) const { return _processed; }
|
||||
virtual bool isReady(void) const = 0;
|
||||
virtual short event(void) const = 0;
|
||||
|
||||
protected:
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: gadelbes <gadelbes@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/24 13:46:34 by gadelbes #+# #+# */
|
||||
/* Updated: 2025/05/27 16:16:08 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/27 21:32:43 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -16,7 +16,9 @@
|
||||
#include <config/default.hpp>
|
||||
#include <cstdio>
|
||||
|
||||
#include <exception>
|
||||
#include <map>
|
||||
#include <server/ResourceManager.hpp>
|
||||
#include <string>
|
||||
#include <unistd.h>
|
||||
|
||||
@ -52,6 +54,13 @@ class Cgi : public server::AClientResource {
|
||||
std::string str(void);
|
||||
|
||||
short event(void) const { return POLLIN; }
|
||||
bool isReady(void) const {
|
||||
if (_is_post == false)
|
||||
return true;
|
||||
if (ResourceManager::get(_stdin_pipe[PIPE_WRITE])->isProcessed())
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
protected:
|
||||
private:
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/05/13 18:14:45 by adjoly #+# #+# */
|
||||
/* Updated: 2025/05/27 13:08:08 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/27 18:59:29 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -37,8 +37,8 @@ class CgiIn : public AClientResource {
|
||||
// TODO: send the body
|
||||
}
|
||||
clientResType type(void) const { return CGI_IN; }
|
||||
|
||||
short event(void) const { return POLLIN; }
|
||||
short event(void) const { return POLLIN; }
|
||||
bool isReady(void) const { return true; }
|
||||
|
||||
protected:
|
||||
private:
|
||||
|
@ -6,13 +6,14 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/14 14:14:39 by adjoly #+# #+# */
|
||||
/* Updated: 2025/05/27 16:47:20 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/27 22:24:25 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <config/default.hpp>
|
||||
#include <cstddef>
|
||||
#include <cstdio>
|
||||
#include <netinet/in.h>
|
||||
#include <requests/default.hpp>
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/05/13 18:14:45 by adjoly #+# #+# */
|
||||
/* Updated: 2025/05/27 13:07:50 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/27 18:57:27 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -17,19 +17,19 @@
|
||||
namespace webserv {
|
||||
namespace server {
|
||||
|
||||
class FileUpload : public AClientResource {
|
||||
public:
|
||||
FileUpload(int id) {
|
||||
_fd = id;
|
||||
_pfd_event = POLLOUT;
|
||||
}
|
||||
~FileUpload(void) {}
|
||||
|
||||
clientResType type(void) const { return UP_FILE; }
|
||||
|
||||
protected:
|
||||
private:
|
||||
};
|
||||
// class FileUpload : public AClientResource {
|
||||
// public:
|
||||
// FileUpload(int id) {
|
||||
// _fd = id;
|
||||
// _pfd_event = POLLOUT;
|
||||
// }
|
||||
// ~FileUpload(void) {}
|
||||
//
|
||||
// clientResType type(void) const { return UP_FILE; }
|
||||
//
|
||||
// protected:
|
||||
// private:
|
||||
// };
|
||||
|
||||
} // 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/27 17:24:51 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/27 18:40:54 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -81,13 +81,6 @@ 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/05/27 18:38:04 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/27 19:32:38 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -29,7 +29,7 @@ class Client;
|
||||
|
||||
class Server {
|
||||
public:
|
||||
Server(config::Config *);
|
||||
Server(void);
|
||||
~Server(void);
|
||||
|
||||
protected:
|
||||
@ -74,23 +74,14 @@ class Server {
|
||||
}
|
||||
|
||||
Client *_getClient(int);
|
||||
|
||||
config::Config
|
||||
* _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<Client *> _client_data; // vector of all the client sockaddr_in
|
||||
|
||||
/**
|
||||
* @brief Can be used to handle a pollfd that is a server
|
||||
*/
|
||||
void _handle_srv(size_t i);
|
||||
|
||||
/**
|
||||
* @brief Can be used to handle pollfd taht is a client
|
||||
*/
|
||||
void _handle_client(size_t *i);
|
||||
void _handle_resource(size_t i);
|
||||
|
||||
};
|
||||
|
||||
}; // namespace server
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/11 13:29:21 by mmoussou #+# #+# */
|
||||
/* Updated: 2025/05/26 10:16:08 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/27 19:25:07 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -14,19 +14,18 @@
|
||||
|
||||
#define auto __auto_type
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <csignal>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <netinet/in.h>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <vector>
|
||||
#include <sstream>
|
||||
|
||||
|
||||
#define range(x) \
|
||||
x.begin(); \
|
||||
@ -39,7 +38,4 @@
|
||||
|
||||
#define BUFFER_SIZE 4096
|
||||
|
||||
namespace webserv {
|
||||
|
||||
} // namespace webserv
|
||||
|
||||
namespace webserv {} // namespace webserv
|
||||
|
15
src/main.cpp
15
src/main.cpp
@ -6,12 +6,11 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/03 15:45:07 by mmoussou #+# #+# */
|
||||
/* Updated: 2025/05/24 11:20:28 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/27 19:36:27 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "cppeleven.hpp"
|
||||
#include <config/Config.hpp>
|
||||
#include <config/default.hpp>
|
||||
#include <csignal>
|
||||
#include <cstdlib>
|
||||
@ -27,8 +26,9 @@
|
||||
#include <webserv.hpp>
|
||||
|
||||
namespace webserv {
|
||||
Logger *_log = not_nullptr;
|
||||
Logger * _log = not_nullptr;
|
||||
std::vector<server::AClientResource *> server::ResourceManager::_res;
|
||||
config::Config * config::_conf = not_nullptr;
|
||||
} // namespace webserv
|
||||
|
||||
int _sig = 0;
|
||||
@ -62,7 +62,6 @@ int main(int ac, char **av) {
|
||||
}
|
||||
|
||||
_log = not_nullptr;
|
||||
config::Config *conf;
|
||||
try {
|
||||
std::string str;
|
||||
if (ac < 2) {
|
||||
@ -70,20 +69,20 @@ int main(int ac, char **av) {
|
||||
} else {
|
||||
str = av[1];
|
||||
}
|
||||
conf = new config::Config(str);
|
||||
config::_conf = new config::Config(str);
|
||||
} catch (std::exception &) {
|
||||
if (_log != not_nullptr)
|
||||
delete _log;
|
||||
return 1;
|
||||
}
|
||||
if (signal(SIGINT, &ft_sig) == SIG_ERR) {
|
||||
conf->getLogger()->error("could not bind sigint :(");
|
||||
config::_conf->getLogger()->error("could not bind sigint :(");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
webserv::server::Server *serv = new webserv::server::Server(conf);
|
||||
webserv::server::Server *serv = new webserv::server::Server();
|
||||
|
||||
delete serv;
|
||||
delete _log;
|
||||
delete conf;
|
||||
delete config::_conf;
|
||||
}
|
||||
|
@ -6,17 +6,17 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/03 16:07:01 by mmoussou #+# #+# */
|
||||
/* Updated: 2025/05/02 13:58:35 by mmoussou ### ########.fr */
|
||||
/* Updated: 2025/05/27 19:52:11 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <vector>
|
||||
#include <dirent.h>
|
||||
#include <algorithm>
|
||||
#include <dirent.h>
|
||||
#include <sys/stat.h>
|
||||
#include <vector>
|
||||
|
||||
#include <log.hpp>
|
||||
#include <config/URL.hpp>
|
||||
#include <log.hpp>
|
||||
#include <requests/default.hpp>
|
||||
|
||||
using namespace webserv;
|
||||
@ -56,20 +56,15 @@ void ARequest::setProtocol(std::string const protocol) {
|
||||
this->_protocol = protocol;
|
||||
}
|
||||
|
||||
URL ARequest::getUrl() const
|
||||
{
|
||||
URL ARequest::getUrl() const {
|
||||
if (this->_url)
|
||||
return *(this->_url);
|
||||
else
|
||||
return URL("");
|
||||
}
|
||||
|
||||
config::Route *ARequest::getRoute(void) const
|
||||
{
|
||||
return (_route);
|
||||
}
|
||||
config::Route *ARequest::getRoute(void) const { return (_route); }
|
||||
|
||||
void ARequest::setRoute(config::Route *route)
|
||||
{
|
||||
this->_route = route;
|
||||
}
|
||||
void ARequest::setRoute(config::Route *route) { this->_route = route; }
|
||||
|
||||
void ARequest::setSrv(config::Server *srv) { _srv = srv; }
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: gadelbes <gadelbes@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/24 13:46:34 by gadelbes #+# #+# */
|
||||
/* Updated: 2025/05/27 13:08:36 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/27 22:26:52 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -45,7 +45,8 @@ Cgi::Cgi(http::Post *req, config::Route *conf)
|
||||
log("➕", "Cgi", "POST constructor called");
|
||||
_initEnvp();
|
||||
_prep();
|
||||
AClientResource *in = new CgiIn(_request->getBody(), _stdin_pipe[PIPE_WRITE]);
|
||||
AClientResource *in =
|
||||
new CgiIn(_request->getBody(), _stdin_pipe[PIPE_WRITE]);
|
||||
ResourceManager::append(in);
|
||||
}
|
||||
|
||||
@ -57,8 +58,12 @@ 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= _stdout_pipe[STDIN_FILENO];
|
||||
_script_path = _conf->getRootDir() + _request->getTarget();
|
||||
_fd = _stdout_pipe[STDIN_FILENO];
|
||||
_pfd_event = POLLIN;
|
||||
if (access(_script_path.c_str(), X_OK))
|
||||
throw std::runtime_error(
|
||||
"script is not executable please run : chmod +x " + _script_path);
|
||||
}
|
||||
|
||||
void Cgi::_initEnvp(void) {
|
||||
@ -117,6 +122,7 @@ char **Cgi::_genEnv(void) {
|
||||
std::strcpy(tmp, str.c_str());
|
||||
newEnv[i] = tmp;
|
||||
}
|
||||
newEnv[i] = NULL;
|
||||
|
||||
return newEnv;
|
||||
}
|
||||
@ -125,16 +131,15 @@ 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[PIPE_READ], STDIN_FILENO);
|
||||
close(_stdin_pipe[PIPE_READ]);
|
||||
close(_stdin_pipe[PIPE_WRITE]);
|
||||
if (_is_post) {
|
||||
dup2(_stdin_pipe[PIPE_READ], STDIN_FILENO);
|
||||
close(_stdin_pipe[PIPE_READ]);
|
||||
close(_stdin_pipe[PIPE_WRITE]);
|
||||
}
|
||||
|
||||
dup2(_stdout_pipe[PIPE_WRITE], STDOUT_FILENO);
|
||||
close(_stdout_pipe[PIPE_READ]);
|
||||
@ -153,25 +158,31 @@ void Cgi::process(void) {
|
||||
delete env;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
} else {
|
||||
if (_is_post)
|
||||
close(_stdin_pipe[PIPE_READ]);
|
||||
close(_stdout_pipe[PIPE_WRITE]);
|
||||
waitpid(forkPid, NULL, 0);
|
||||
}
|
||||
close(_stdin_pipe[PIPE_READ]);
|
||||
close(_stdout_pipe[PIPE_WRITE]);
|
||||
waitpid(forkPid, NULL, 0);
|
||||
}
|
||||
|
||||
std::string Cgi::str(void) {
|
||||
std::string str;
|
||||
int max = _conf->getMaxBody();
|
||||
char buffer[1024];
|
||||
int max = _conf->getMaxBody();
|
||||
char buffer[1024];
|
||||
std::ostringstream str;
|
||||
|
||||
while (max) {
|
||||
ssize_t count = read(_stdout_pipe[0], buffer, sizeof(buffer));
|
||||
if (count > 0)
|
||||
str.append(buffer);
|
||||
if (count > 0) {
|
||||
str.write(buffer, count);
|
||||
max -= count;
|
||||
} else if (count == 0) {
|
||||
break;
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
str.append("\0");
|
||||
ResourceManager::remove(_stdin_pipe[PIPE_WRITE]);
|
||||
return str;
|
||||
if (_is_post)
|
||||
ResourceManager::remove(_stdin_pipe[PIPE_WRITE]);
|
||||
return str.str();
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/30 09:42:18 by adjoly #+# #+# */
|
||||
/* Updated: 2025/05/02 14:53:08 by mmoussou ### ########.fr */
|
||||
/* Updated: 2025/05/27 22:23:22 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -6,25 +6,32 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/30 09:40:16 by adjoly #+# #+# */
|
||||
/* Updated: 2025/05/27 16:49:05 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/27 22:32:40 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "cppeleven.hpp"
|
||||
#include "requests/Response.hpp"
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <dirent.h>
|
||||
#include <exception>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <vector>
|
||||
|
||||
#include <server/default.hpp>
|
||||
#include <requests/default.hpp>
|
||||
#include <server/default.hpp>
|
||||
|
||||
using namespace webserv::http;
|
||||
|
||||
Get::Get(std::string &data) {
|
||||
// _cgi = not_nullptr;
|
||||
this->parse(data);
|
||||
Get::Get(std::string &data, config::Server *srv) {
|
||||
_cgi = not_nullptr;
|
||||
_srv = srv;
|
||||
_url = not_nullptr;
|
||||
this->parse(data);
|
||||
}
|
||||
|
||||
void Get::parse(std::string const &data) {
|
||||
@ -48,6 +55,8 @@ void Get::parse(std::string const &data) {
|
||||
}
|
||||
}
|
||||
|
||||
_route = _srv->whatRoute(URL(_target));
|
||||
|
||||
std::ostringstream body_stream;
|
||||
while (std::getline(stream, line))
|
||||
body_stream << line << "\n";
|
||||
@ -55,10 +64,28 @@ void Get::parse(std::string const &data) {
|
||||
|
||||
_url = new URL("http://" + _headers["Host"] + _target);
|
||||
|
||||
// if (_route->isCgi(_target)) {
|
||||
// _cgi = new server::Cgi(this, _route);
|
||||
// server::ResourceManager::append(_cgi);
|
||||
// }
|
||||
std::string targ = _target;
|
||||
|
||||
if (targ[targ.length() - 1] == '/') {
|
||||
targ += _route->getIndex();
|
||||
}
|
||||
|
||||
if (_route->isCgi(targ)) {
|
||||
_log->info("cgi added");
|
||||
try {
|
||||
_cgi = new server::Cgi(this, _route);
|
||||
} catch (std::exception &e) {
|
||||
_log->error(e.what());
|
||||
_method = "500";
|
||||
return;
|
||||
}
|
||||
server::ResourceManager::append(_cgi);
|
||||
struct pollfd pfd;
|
||||
pfd.events = _cgi->event();
|
||||
pfd.revents = 0;
|
||||
pfd.fd = _cgi->getId();
|
||||
server::PfdManager::append(pfd, server::RES);
|
||||
}
|
||||
|
||||
/*
|
||||
std::cout << "-- start-line --" << std::endl;
|
||||
@ -92,9 +119,56 @@ char isDirectory(const std::string &path) {
|
||||
return S_ISDIR(file_stat.st_mode);
|
||||
}
|
||||
|
||||
Response parseCgiOut(std::string cgi_str) {
|
||||
Response response;
|
||||
std::istringstream stream(cgi_str);
|
||||
std::string line;
|
||||
|
||||
response.setStatusCode(200);
|
||||
while (std::getline(stream, line) && line != "") {
|
||||
size_t delimiter_index = line.find(':');
|
||||
if (delimiter_index != std::string::npos) {
|
||||
std::string key = line.substr(0, delimiter_index);
|
||||
std::string value = line.substr(delimiter_index + 2);
|
||||
response.addHeader(key, value);
|
||||
}
|
||||
}
|
||||
std::ostringstream body_stream;
|
||||
while (std::getline(stream, line))
|
||||
body_stream << line << "\n";
|
||||
response.setBody(body_stream.str());
|
||||
|
||||
if (response.getHeader("Content-Length") == "") {
|
||||
std::stringstream length;
|
||||
length << response.getBody().length();
|
||||
response.addHeader("Content-Length", length.str());
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
Response Get::execute(void) {
|
||||
http::Response response;
|
||||
|
||||
if (_cgi != not_nullptr) {
|
||||
if (_method == "500") {
|
||||
response.setStatusCode(500);
|
||||
response.addHeader("Content-Type", "text/html");
|
||||
response.setBody(
|
||||
http::Errors::getResponseBody(response.getStatusCode()));
|
||||
server::PfdManager::remove(_cgi->getId());
|
||||
server::ResourceManager::remove(_cgi->getId());
|
||||
_cgi = not_nullptr;
|
||||
return response;
|
||||
}
|
||||
std::string str = static_cast<server::Cgi *>(_cgi)->str();
|
||||
response = parseCgiOut(str);
|
||||
response.setProtocol(_protocol);
|
||||
server::PfdManager::remove(_cgi->getId());
|
||||
server::ResourceManager::remove(_cgi->getId());
|
||||
_cgi = not_nullptr;
|
||||
return response;
|
||||
}
|
||||
|
||||
this->_target = this->_route->getRootDir() + this->_target;
|
||||
|
||||
try {
|
||||
@ -117,8 +191,8 @@ Response Get::execute(void) {
|
||||
response.addHeader("Content-Type",
|
||||
http::Mime::getType(this->_target));
|
||||
} else if (this->_route->getDirList()) {
|
||||
DIR *dir;
|
||||
struct dirent *entry;
|
||||
DIR * dir;
|
||||
struct dirent * entry;
|
||||
struct stat file_stat;
|
||||
std::vector<std::string> files;
|
||||
|
||||
@ -192,10 +266,6 @@ body {\n\
|
||||
response.setStatusCode(200);
|
||||
response.addHeader("Content-Type",
|
||||
http::Mime::getType(this->_target));
|
||||
|
||||
#ifdef VERBOSE
|
||||
//_log->debug(response.str().c_str());
|
||||
#endif
|
||||
}
|
||||
} catch (...) {
|
||||
// TODO: replace with a predefined array of error pages
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/30 09:50:20 by adjoly #+# #+# */
|
||||
/* Updated: 2025/05/27 09:23:54 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/27 22:23:36 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -6,21 +6,23 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/17 11:12:41 by mmoussou #+# #+# */
|
||||
/* Updated: 2025/05/27 16:48:09 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/27 22:26:09 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "cppeleven.hpp"
|
||||
#include "requests/RedirectResp.hpp"
|
||||
#include "requests/default.hpp"
|
||||
#include <cstddef>
|
||||
#include <log.hpp>
|
||||
#include <server/Cgi.hpp>
|
||||
#include <server/Client.hpp>
|
||||
#include <sstream>
|
||||
#include <sys/poll.h>
|
||||
|
||||
using namespace webserv::server;
|
||||
|
||||
Client::Client(int fd, config::Server *conf)
|
||||
: _fd(fd), _conf(conf) {
|
||||
Client::Client(int fd, config::Server *conf) : _fd(fd), _conf(conf) {
|
||||
_request = not_nullptr;
|
||||
log("➕", "Client", "constructor called");
|
||||
_response_done = false;
|
||||
@ -50,16 +52,15 @@ void Client::parse(void) {
|
||||
_getRequest(received_data);
|
||||
|
||||
_route = _conf->whatRoute(URL(this->_request->getTarget()));
|
||||
this->_request->setRoute(_route);
|
||||
|
||||
if (_conf->getServerNames() != not_nullptr) {
|
||||
std::string host = _request->getHeader("Host");
|
||||
bool ret = _conf->isServerName(host.substr(0, host.find(':')));
|
||||
bool ret = _conf->isServerName(host.substr(0, host.find(':')));
|
||||
if (ret == false) {
|
||||
throw std::runtime_error("serverName not correcponding");
|
||||
throw std::runtime_error("serverName not corresponding");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!this->_route || this->_route == not_nullptr) {
|
||||
this->_request->setMethod("404");
|
||||
return;
|
||||
@ -79,6 +80,8 @@ void Client::parse(void) {
|
||||
}
|
||||
|
||||
bool Client::requestParsed(void) {
|
||||
if (_request->getCgi() != not_nullptr && !_request->getCgi()->isProcessed())
|
||||
return false;
|
||||
if (_request == not_nullptr)
|
||||
return false;
|
||||
return true;
|
||||
@ -89,7 +92,7 @@ void Client::_getRequest(std::string request_str) {
|
||||
0, request_str.substr(0, 4).find_last_not_of(" ") + 1);
|
||||
|
||||
if (method == "GET") {
|
||||
this->_request = new http::Get(request_str);
|
||||
this->_request = new http::Get(request_str, _conf);
|
||||
std::stringstream str;
|
||||
str << "get request received on port : ";
|
||||
str << _conf->getPort();
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/11 16:11:40 by adjoly #+# #+# */
|
||||
/* Updated: 2025/05/27 18:39:00 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/27 21:19:49 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -14,6 +14,7 @@
|
||||
#include <algorithm>
|
||||
#include <cerrno>
|
||||
#include <cmath>
|
||||
#include <config/default.hpp>
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
#include <exception>
|
||||
@ -68,7 +69,7 @@ std::string getMethod(std::string &data) {
|
||||
|
||||
int Server::_fillHostsPorts(std::vector<std::string> &hosts,
|
||||
std::vector<int> & ports) {
|
||||
std::vector<config::Server *> config = _conf->getServers();
|
||||
std::vector<config::Server *> config = config::_conf->getServers();
|
||||
|
||||
for (auto it = range(config)) {
|
||||
hosts.push_back((*it)->getHost());
|
||||
@ -133,16 +134,15 @@ void Server::_run(void) {
|
||||
_handle_client(&i);
|
||||
break;
|
||||
case RES:
|
||||
_log->warn("not handling resource for now");
|
||||
_handle_resource(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Server::Server(config::Config *conf) : _conf(conf) {
|
||||
Server::Server() {
|
||||
log("➕", "Server::Server", "config constructor called");
|
||||
_log = conf->getLogger();
|
||||
try {
|
||||
_setup();
|
||||
_run();
|
||||
|
@ -6,13 +6,15 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/05/27 18:22:48 by adjoly #+# #+# */
|
||||
/* Updated: 2025/05/27 18:38:28 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/05/27 22:24:35 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "cppeleven.hpp"
|
||||
#include <cstddef>
|
||||
#include <iterator>
|
||||
#include <server/default.hpp>
|
||||
#include <sys/poll.h>
|
||||
|
||||
using namespace webserv::server;
|
||||
|
||||
@ -33,7 +35,7 @@ void Server::_handle_srv(size_t i) {
|
||||
_log->error(str.str());
|
||||
return;
|
||||
}
|
||||
config::Server *conf_srv = _conf->getServer(i);
|
||||
config::Server *conf_srv = config::_conf->getServer(i);
|
||||
if (conf_srv == not_nullptr) {
|
||||
_log->warn("where the f does he come from"); // does can't
|
||||
// actually happen
|
||||
@ -69,7 +71,7 @@ void Server::_handle_client(size_t *i) {
|
||||
Client *client = _getClient(PfdManager::at(*i).fd);
|
||||
if (client == not_nullptr) {
|
||||
_log->error("client does not exist");
|
||||
return ;
|
||||
return;
|
||||
}
|
||||
try {
|
||||
client->parse();
|
||||
@ -110,3 +112,22 @@ void Server::_handle_client(size_t *i) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Server::_handle_resource(size_t i) {
|
||||
struct pollfd pfd = PfdManager::at(i);
|
||||
AClientResource *res = ResourceManager::get(pfd.fd);
|
||||
if (res == not_nullptr) {
|
||||
std::cout << "wtff" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!res->isProcessed() && res->isReady()) {
|
||||
if (res->type() == CGI) {
|
||||
res->process();
|
||||
_log->info("processingggg");
|
||||
} else if (pfd.revents & res->event()) {
|
||||
res->process();
|
||||
_log->info("processingggg");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
6
test.py
Normal file → Executable file
6
test.py
Normal file → Executable file
@ -1,4 +1,4 @@
|
||||
#!/usr/bin/env python3
|
||||
#!/nix/store/kjvgj2n3yn70hmjifg6y0bk9m4rf7jba-python3-3.12.10/bin/python3
|
||||
|
||||
# Import modules for CGI handling
|
||||
import cgi
|
||||
@ -11,7 +11,9 @@ cgitb.enable()
|
||||
form = cgi.FieldStorage()
|
||||
|
||||
# Set the content type to HTML
|
||||
print("Content-Type: text/html\n")
|
||||
print("Content-Type: text/html")
|
||||
|
||||
print("")
|
||||
|
||||
# Output a simple HTML page
|
||||
print("<html>")
|
||||
|
Reference in New Issue
Block a user