」 feat: change mainloop implementent error pages but still some bug that need to be fixed overall working as expected

This commit is contained in:
Adam
2025-05-28 11:52:45 +02:00
committed by GitHub
33 changed files with 539 additions and 338 deletions

View File

@ -6,7 +6,7 @@
# By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ # # By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2024/10/25 16:09:27 by adjoly #+# #+# # # Created: 2024/10/25 16:09:27 by adjoly #+# #+# #
# Updated: 2025/04/28 14:57:54 by adjoly ### ########.fr # # Updated: 2025/05/28 09:42:17 by adjoly ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #

View File

@ -16,6 +16,7 @@ root = "/home/adjoly"
dirlist = true dirlist = true
client_max_body_size = "10M" client_max_body_size = "10M"
index = "index.html" index = "index.html"
cgi = { ".py", ".go" }
[server.location./api] [server.location./api]
methods = { "GET", "POST" } methods = { "GET", "POST" }

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/14 12:20:06 by adjoly #+# #+# */ /* 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/19 14:59:41 by adjoly #+# #+# */ /* Created: 2025/03/19 14:59:41 by adjoly #+# #+# */
/* Updated: 2025/05/27 09:37:23 by adjoly ### ########.fr */ /* Updated: 2025/05/28 09:52:21 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -40,6 +40,9 @@ class Route {
bool * getMethods(void) { return _methods; } bool * getMethods(void) { return _methods; }
bool isCgi(std::string target) { bool isCgi(std::string target) {
std::string target_ext = target.substr(target.find('.')); std::string target_ext = target.substr(target.find('.'));
if (_cgi == not_nullptr)
return false;
for (auto it = prange(_cgi)) { for (auto it = prange(_cgi)) {
if (target_ext == *it) if (target_ext == *it)
return true; return true;

View File

@ -6,12 +6,13 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/22 12:17:48 by adjoly #+# #+# */ /* 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 #pragma once
#include "log.hpp"
#include <cstring> #include <cstring>
#include <ostream> #include <ostream>
#include <sstream> #include <sstream>
@ -29,7 +30,7 @@ class URL {
} }
int countMatchingSegments(const URL &url) const { 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; return 0;
int i = 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::vector<std::string> &segments) const {
std::stringstream ss(path); std::stringstream ss(path);
std::string segment; std::string segment;

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/19 14:15:51 by adjoly #+# #+# */ /* 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 config
}; // namespace webserv }; // namespace webserv

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/20 09:28:27 by adjoly #+# #+# */ /* Created: 2025/03/20 09:28:27 by adjoly #+# #+# */
/* Updated: 2025/04/29 17:28:36 by adjoly ### ########.fr */ /* Updated: 2025/05/28 11:32:32 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -6,14 +6,16 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/03 17:23:00 by mmoussou #+# #+# */ /* Created: 2025/02/03 17:23:00 by mmoussou #+# #+# */
/* Updated: 2025/05/15 12:12:47 by adjoly ### ########.fr */ /* Updated: 2025/05/28 09:45:17 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#pragma once #pragma once
#include <config/URL.hpp> #include "config/Server.hpp"
#include "cppeleven.hpp"
#include <config/Route.hpp> #include <config/Route.hpp>
#include <config/URL.hpp>
#include <ctime> #include <ctime>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
@ -26,38 +28,41 @@
#include <config/default.hpp> #include <config/default.hpp>
namespace webserv { namespace webserv {
namespace server {
class Cgi;
}
namespace http { namespace http {
class ARequest : public http::IMessage { class ARequest : public http::IMessage {
public: public:
virtual ~ARequest(void) { virtual ~ARequest(void) { log("", "ARequest", "destructor called"); }
log("", "ARequest", "destructor called");
delete _url;
}
virtual void parse(std::string const &data) = 0; virtual void parse(std::string const &data) = 0;
virtual Response execute(void) = 0; virtual Response execute(void) = 0;
std::string str(void) const; std::string str(void) const;
std::string getMethod(void) const; std::string getMethod(void) const;
std::string getTarget(void) const; std::string getTarget(void) const;
std::string getProtocol(void) const; std::string getProtocol(void) const;
webserv::config::Route *getRoute(void) const; webserv::config::Route *getRoute(void) const;
URL getUrl() const; URL getUrl() const;
virtual server::Cgi * getCgi() const { return not_nullptr; }
void setMethod(std::string const method); void setMethod(std::string const method);
void setTarget(std::string const target); void setTarget(std::string const target);
void setProtocol(std::string const protocol); void setProtocol(std::string const protocol);
void setServer(std::string const protocol); void setServer(std::string const protocol);
void setRoute(config::Route *route); void setRoute(config::Route *route);
void setSrv(config::Server *srv);
protected: protected:
std::string _method; std::string _method;
std::string _target; std::string _target;
std::string _protocol; std::string _protocol;
webserv::config::Route *_route; webserv::config::Route *_route;
URL *_url; config::Server * _srv;
URL * _url;
std::string _sanitizeStr(std::string &str) { std::string _sanitizeStr(std::string &str) {
std::string newStr = str; std::string newStr = str;

View File

@ -1,3 +1,4 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
@ -6,7 +7,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/16 17:51:46 by mmoussou #+# #+# */ /* Created: 2025/03/16 17:51:46 by mmoussou #+# #+# */
/* Updated: 2025/04/30 14:30:00 by mmoussou ### ########.fr */ /* Updated: 2025/05/28 11:26:01 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -22,8 +23,8 @@ namespace http {
class Errors { class Errors {
public: public:
static std::string getResponseBody(int error_code); static std::string getResponseBody(int error_code, std::string err_file);
static void setEntries(const std::map<int, std::string>); // static void setEntries(const std::map<int, std::string>);
static std::map<int, std::string> message; static std::map<int, std::string> message;

View File

@ -6,10 +6,11 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/30 09:30:15 by adjoly #+# #+# */ /* Created: 2025/04/30 09:30:15 by adjoly #+# #+# */
/* Updated: 2025/05/27 16:49:00 by adjoly ### ########.fr */ /* Updated: 2025/05/28 11:29:38 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include <cstddef>
#include <requests/ARequest.hpp> #include <requests/ARequest.hpp>
namespace webserv { namespace webserv {
@ -22,20 +23,22 @@ namespace http {
class Get : public ARequest { class Get : public ARequest {
public: public:
Get(void) {} Get(void) {}
Get(std::string &data); Get(std::string &data, config::Server *srv);
~Get(void);
void parse(std::string const &data); void parse(std::string const &data);
Response execute(void); Response execute(void);
// private: server::Cgi *getCgi() const { return _cgi; }
// server::Cgi *_cgi;
private:
server::Cgi *_cgi;
}; };
class Post : public ARequest { class Post : public ARequest {
public: public:
Post(void) {} Post(std::string &data, config::Server *srv);
Post(std::string &data);
void parse(std::string const &data); void parse(std::string const &data);
@ -45,14 +48,16 @@ class Post : public ARequest {
Response execute(void); Response execute(void);
// private: server::Cgi *getCgi() const { return _cgi; }
// server::Cgi *_cgi;
private:
server::Cgi *_cgi;
}; };
class Delete : public ARequest { class Delete : public ARequest {
public: public:
Delete(void) {} Delete(void) {}
Delete(std::string &data); Delete(std::string &data, config::Server *srv);
void parse(std::string const &data); void parse(std::string const &data);

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/29 14:20:09 by mmoussou #+# #+# */ /* 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; virtual void process(void) = 0;
bool isProcessed(void) const { return _processed; } bool isProcessed(void) const { return _processed; }
virtual bool isReady(void) const = 0;
virtual short event(void) const = 0; virtual short event(void) const = 0;
protected: protected:

View File

@ -6,7 +6,7 @@
/* By: gadelbes <gadelbes@student.42.fr> +#+ +:+ +#+ */ /* By: gadelbes <gadelbes@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/24 13:46:34 by gadelbes #+# #+# */ /* 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 <config/default.hpp>
#include <cstdio> #include <cstdio>
#include <exception>
#include <map> #include <map>
#include <server/ResourceManager.hpp>
#include <string> #include <string>
#include <unistd.h> #include <unistd.h>
@ -52,6 +54,13 @@ class Cgi : public server::AClientResource {
std::string str(void); std::string str(void);
short event(void) const { return POLLIN; } 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: protected:
private: private:

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/13 18:14:45 by adjoly #+# #+# */ /* 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 // TODO: send the body
} }
clientResType type(void) const { return CGI_IN; } 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: protected:
private: private:

View File

@ -6,13 +6,14 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/14 14:14:39 by adjoly #+# #+# */ /* 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 #pragma once
#include <config/default.hpp> #include <config/default.hpp>
#include <cstddef>
#include <cstdio> #include <cstdio>
#include <netinet/in.h> #include <netinet/in.h>
#include <requests/default.hpp> #include <requests/default.hpp>

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/13 18:14:45 by adjoly #+# #+# */ /* 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 webserv {
namespace server { namespace server {
class FileUpload : public AClientResource { // class FileUpload : public AClientResource {
public: // public:
FileUpload(int id) { // FileUpload(int id) {
_fd = id; // _fd = id;
_pfd_event = POLLOUT; // _pfd_event = POLLOUT;
} // }
~FileUpload(void) {} // ~FileUpload(void) {}
//
clientResType type(void) const { return UP_FILE; } // clientResType type(void) const { return UP_FILE; }
//
protected: // protected:
private: // private:
}; // };
} // namespace server } // namespace server
} // namespace webserv } // namespace webserv

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/27 17:01:01 by adjoly #+# #+# */ /* Created: 2025/05/27 17:01:01 by adjoly #+# #+# */
/* Updated: 2025/05/27 17:59:37 by adjoly ### ########.fr */ /* Updated: 2025/05/27 18:12:13 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -82,8 +82,9 @@ class PfdManager {
} }
} }
static struct pollfd *data(void) { return _pfd_vec.data(); } static struct pollfd * data(void) { return _pfd_vec.data(); }
static size_t size(void) { return _pfd_vec.size(); } static size_t size(void) { return _pfd_vec.size(); }
static std::vector<struct pollfd> *vec(void) { return &_pfd_vec; }
static void clear(void) { static void clear(void) {
for (auto it = range(_pfd_vec)) { for (auto it = range(_pfd_vec)) {

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/12 17:13:39 by adjoly #+# #+# */ /* 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: protected:
private: private:
static std::vector<AClientResource *> _res; static std::vector<AClientResource *> _res;

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/11 17:45:43 by adjoly #+# #+# */ /* Created: 2025/04/11 17:45:43 by adjoly #+# #+# */
/* Updated: 2025/05/27 17:43:21 by adjoly ### ########.fr */ /* Updated: 2025/05/27 19:32:38 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,9 +16,11 @@
#include <fcntl.h> #include <fcntl.h>
#include <log.hpp> #include <log.hpp>
#include <netinet/in.h> #include <netinet/in.h>
#include <stdexcept>
#include <poll.h> #include <poll.h>
#include <server/PfdManager.hpp>
#include <stdexcept>
#include <vector> #include <vector>
#include <webserv.hpp>
namespace webserv { namespace webserv {
namespace server { namespace server {
@ -27,7 +29,7 @@ class Client;
class Server { class Server {
public: public:
Server(config::Config *); Server(void);
~Server(void); ~Server(void);
protected: protected:
@ -72,13 +74,14 @@ class Server {
} }
Client *_getClient(int); Client *_getClient(int);
std::vector<int> _fds_server; // The fds of the sockets
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<struct pollfd> _client_fds; // A vector of all the poll fd
std::vector<Client *> _client_data; // vector of all the client sockaddr_in std::vector<Client *> _client_data; // vector of all the client sockaddr_in
void _handle_srv(size_t i);
void _handle_client(size_t *i);
void _handle_resource(size_t i);
}; };
}; // namespace server }; // namespace server

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/11 13:29:21 by mmoussou #+# #+# */ /* 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 #define auto __auto_type
#include <sys/types.h>
#include <csignal> #include <csignal>
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <netinet/in.h> #include <netinet/in.h>
#include <sstream>
#include <string> #include <string>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#include <vector> #include <vector>
#include <sstream>
#define range(x) \ #define range(x) \
x.begin(); \ x.begin(); \
@ -39,7 +38,4 @@
#define BUFFER_SIZE 4096 #define BUFFER_SIZE 4096
namespace webserv { namespace webserv {} // namespace webserv
} // namespace webserv

13
not_found.html Normal file
View File

@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Not found D:</title>
</head>
<body>
<h1>The page you requested was not found. Here is an image of Kanel instead</h1>
<img src= "https://send.kanel.ovh/hotlink/xA0pJbJY6vuwjvGjpXHde6Y7WSVs7VF0qWp8y9YS.png" width="500" height="500">
</body>
</html>

View File

@ -23,8 +23,10 @@ std::vector<std::string> *Route::_parseCGI(toml::ANode *table) {
std::vector<std::string> *cgi = new std::vector<std::string>; std::vector<std::string> *cgi = new std::vector<std::string>;
for (auto it = prange(table->getArray())) { for (auto it = prange(table->getArray())) {
if ((*it)->type() == toml::STRING) if ((*it)->type() == toml::STRING) {
cgi->push_back(*static_cast<std::string *>((*it)->getValue())); std::string str = *static_cast<std::string *>((*it)->getValue());
cgi->push_back(str);
}
else { else {
std::stringstream str; std::stringstream str;
str << "Was expecting a: " << toml::nodeTypeToStr(toml::STRING); str << "Was expecting a: " << toml::nodeTypeToStr(toml::STRING);

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/24 15:10:07 by adjoly #+# #+# */ /* Created: 2025/03/24 15:10:07 by adjoly #+# #+# */
/* Updated: 2025/05/09 11:47:48 by adjoly ### ########.fr */ /* Updated: 2025/05/28 11:42:25 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,6 +15,7 @@
#include "log.hpp" #include "log.hpp"
#include <config/default.hpp> #include <config/default.hpp>
#include <cstddef> #include <cstddef>
#include <sstream>
#include <utility> #include <utility>
#include <webserv.hpp> #include <webserv.hpp>
@ -103,16 +104,15 @@ Server::~Server(void) {
std::map<int, std::string> * std::map<int, std::string> *
Server::_parseErrPages(std::map<std::string, toml::ANode *> *table) { Server::_parseErrPages(std::map<std::string, toml::ANode *> *table) {
std::map<int, std::string> *errPages = new std::map<int, std::string>; std::map<int, std::string> *errPages = new std::map<int, std::string>;
void *val;
int nb; int nb;
for (std::map<std::string, toml::ANode *>::iterator it = table->begin(); for (std::map<std::string, toml::ANode *>::iterator it = table->begin();
it != table->end(); it++) { it != table->end(); it++) {
val = accessValue(it->first, toml::STRING, _table, _log); if (it->second->type() == toml::STRING) {
if (val != not_nullptr) {
nb = std::atoi(it->first.c_str()); nb = std::atoi(it->first.c_str());
if (nb >= 400 && nb <= 599) if (nb >= 400 && nb <= 599)
(*errPages)[nb] = *static_cast<std::string *>(val); (*errPages)[nb] =
*static_cast<std::string *>(it->second->getValue());
else else
_log->warn("error page - " + it->first + " is not valid :("); _log->warn("error page - " + it->first + " is not valid :(");
} }
@ -133,11 +133,10 @@ Route *Server::whatRoute(const URL &url) {
std::map<URL, Route *>::iterator ret = _routes->end(); std::map<URL, Route *>::iterator ret = _routes->end();
int i = 0; int i = 0;
if (_routes == not_nullptr) if (_routes == not_nullptr)
return not_nullptr; return not_nullptr;
for (auto it = prange(_routes)) { for (auto it = prange(_routes)) {
if (i < it->first.countMatchingSegments(url)) { if (i < it->first.countMatchingSegments(url)) {
ret = it; ret = it;

View File

@ -6,12 +6,11 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/03 15:45:07 by mmoussou #+# #+# */ /* 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 "cppeleven.hpp"
#include <config/Config.hpp>
#include <config/default.hpp> #include <config/default.hpp>
#include <csignal> #include <csignal>
#include <cstdlib> #include <cstdlib>
@ -27,8 +26,9 @@
#include <webserv.hpp> #include <webserv.hpp>
namespace webserv { namespace webserv {
Logger *_log = not_nullptr; Logger * _log = not_nullptr;
std::vector<server::AClientResource *> server::ResourceManager::_res; std::vector<server::AClientResource *> server::ResourceManager::_res;
config::Config * config::_conf = not_nullptr;
} // namespace webserv } // namespace webserv
int _sig = 0; int _sig = 0;
@ -62,7 +62,6 @@ int main(int ac, char **av) {
} }
_log = not_nullptr; _log = not_nullptr;
config::Config *conf;
try { try {
std::string str; std::string str;
if (ac < 2) { if (ac < 2) {
@ -70,20 +69,20 @@ int main(int ac, char **av) {
} else { } else {
str = av[1]; str = av[1];
} }
conf = new config::Config(str); config::_conf = new config::Config(str);
} catch (std::exception &) { } catch (std::exception &) {
if (_log != not_nullptr) if (_log != not_nullptr)
delete _log; delete _log;
return 1; return 1;
} }
if (signal(SIGINT, &ft_sig) == SIG_ERR) { 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; return EXIT_FAILURE;
} }
webserv::server::Server *serv = new webserv::server::Server(conf); webserv::server::Server *serv = new webserv::server::Server();
delete serv; delete serv;
delete _log; delete _log;
delete conf; delete config::_conf;
} }

View File

@ -6,17 +6,17 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/03 16:07:01 by mmoussou #+# #+# */ /* 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 <algorithm>
#include <dirent.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <vector>
#include <log.hpp>
#include <config/URL.hpp> #include <config/URL.hpp>
#include <log.hpp>
#include <requests/default.hpp> #include <requests/default.hpp>
using namespace webserv; using namespace webserv;
@ -56,20 +56,15 @@ void ARequest::setProtocol(std::string const protocol) {
this->_protocol = protocol; this->_protocol = protocol;
} }
URL ARequest::getUrl() const URL ARequest::getUrl() const {
{
if (this->_url) if (this->_url)
return *(this->_url); return *(this->_url);
else else
return URL(""); return URL("");
} }
config::Route *ARequest::getRoute(void) const config::Route *ARequest::getRoute(void) const { return (_route); }
{
return (_route);
}
void ARequest::setRoute(config::Route *route) void ARequest::setRoute(config::Route *route) { this->_route = route; }
{
this->_route = route; void ARequest::setSrv(config::Server *srv) { _srv = srv; }
}

View File

@ -6,7 +6,7 @@
/* By: gadelbes <gadelbes@student.42.fr> +#+ +:+ +#+ */ /* By: gadelbes <gadelbes@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/24 13:46:34 by gadelbes #+# #+# */ /* 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"); log("", "Cgi", "POST constructor called");
_initEnvp(); _initEnvp();
_prep(); _prep();
AClientResource *in = new CgiIn(_request->getBody(), _stdin_pipe[PIPE_WRITE]); AClientResource *in =
new CgiIn(_request->getBody(), _stdin_pipe[PIPE_WRITE]);
ResourceManager::append(in); ResourceManager::append(in);
} }
@ -57,8 +58,12 @@ void Cgi::_prep(void) {
throw std::runtime_error("stdin pipe failed for cgi D:"); throw std::runtime_error("stdin pipe failed for cgi D:");
if (pipe(_stdout_pipe) == -1) if (pipe(_stdout_pipe) == -1)
throw std::runtime_error("stdout pipe failed for cgi D:"); 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; _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) { void Cgi::_initEnvp(void) {
@ -117,6 +122,7 @@ char **Cgi::_genEnv(void) {
std::strcpy(tmp, str.c_str()); std::strcpy(tmp, str.c_str());
newEnv[i] = tmp; newEnv[i] = tmp;
} }
newEnv[i] = NULL;
return newEnv; return newEnv;
} }
@ -125,16 +131,15 @@ void Cgi::process(void) {
_processed = true; _processed = true;
pid_t forkPid; 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(); forkPid = fork();
if (forkPid < 0) if (forkPid < 0)
throw std::runtime_error("fork failed D:"); throw std::runtime_error("fork failed D:");
else if (forkPid == 0) { else if (forkPid == 0) {
dup2(_stdin_pipe[PIPE_READ], STDIN_FILENO); if (_is_post) {
close(_stdin_pipe[PIPE_READ]); dup2(_stdin_pipe[PIPE_READ], STDIN_FILENO);
close(_stdin_pipe[PIPE_WRITE]); close(_stdin_pipe[PIPE_READ]);
close(_stdin_pipe[PIPE_WRITE]);
}
dup2(_stdout_pipe[PIPE_WRITE], STDOUT_FILENO); dup2(_stdout_pipe[PIPE_WRITE], STDOUT_FILENO);
close(_stdout_pipe[PIPE_READ]); close(_stdout_pipe[PIPE_READ]);
@ -153,25 +158,31 @@ void Cgi::process(void) {
delete env; delete env;
exit(EXIT_FAILURE); 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 Cgi::str(void) {
std::string str; int max = _conf->getMaxBody();
int max = _conf->getMaxBody(); char buffer[1024];
char buffer[1024]; std::ostringstream str;
while (max) { while (max) {
ssize_t count = read(_stdout_pipe[0], buffer, sizeof(buffer)); ssize_t count = read(_stdout_pipe[0], buffer, sizeof(buffer));
if (count > 0) if (count > 0) {
str.append(buffer); str.write(buffer, count);
max -= count;
} else if (count == 0) {
break;
}
else else
break; break;
} }
str.append("\0"); if (_is_post)
ResourceManager::remove(_stdin_pipe[PIPE_WRITE]); ResourceManager::remove(_stdin_pipe[PIPE_WRITE]);
return str; return str.str();
} }

View File

@ -6,39 +6,55 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/17 14:08:12 by mmoussou #+# #+# */ /* Created: 2025/03/17 14:08:12 by mmoussou #+# #+# */
/* Updated: 2025/04/30 14:35:48 by mmoussou ### ########.fr */ /* Updated: 2025/05/28 11:33:43 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include <fstream>
#include <iterator>
#include <log.hpp>
#include <requests/Errors.hpp> #include <requests/Errors.hpp>
#include <sstream>
#include <unistd.h>
using namespace webserv; using namespace webserv;
using namespace http; using namespace http;
void Errors::setEntries(const std::map<int, std::string> error_pages) // void Errors::setEntries(const std::map<int, std::string> error_pages)
{ // {
for (std::map<int, std::string>::const_iterator it = error_pages.begin(); it != error_pages.end(); ++it) // for (std::map<int, std::string>::const_iterator it = error_pages.begin(); it
{ // != error_pages.end(); ++it)
if (Errors::set_error_pages.find(it->first) == Errors::set_error_pages.end()) // only insert if key doesn't exist // {
Errors::set_error_pages[it->first] = it->second; // if (Errors::set_error_pages.find(it->first) ==
} // Errors::set_error_pages.end()) // only insert if key doesn't exist
// Errors::set_error_pages[it->first] = it->second;
// }
// }
std::string Errors::getResponseBody(int error_code, std::string err_file) {
std::string body;
if (err_file == "") {
_log->warn("no error file going default");
return ("<html><body><h1>" + Errors::message[error_code] +
"</h1></body></html>");
}
if (access(err_file.c_str(), R_OK) != -1) {
std::ifstream file(err_file.c_str(), std::ios::binary);
std::stringstream buf;
buf << file.rdbuf();
return buf.str();
} else {
_log->error("could not read file");
return ("<html><body><h1>" + Errors::message[error_code] +
"</h1></body></html>");
}
} }
std::string Errors::getResponseBody(int error_code) std::map<int, std::string> Errors::message = Errors::populateMessages();
{ std::map<int, std::string> Errors::set_error_pages;
std::string body;
if (Errors::set_error_pages.find(error_code) != Errors::set_error_pages.end()) std::map<int, std::string> Errors::populateMessages() {
return(Errors::set_error_pages[error_code]);
else
return("<html><body><h1>" + Errors::message[error_code] + "</h1></body></html>");
}
std::map<int, std::string> Errors::message = Errors::populateMessages();
std::map<int, std::string> Errors::set_error_pages;
std::map<int, std::string> Errors::populateMessages()
{
std::map<int, std::string> m; std::map<int, std::string> m;
m[100] = "Continue"; m[100] = "Continue";

View File

@ -6,10 +6,11 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/30 09:42:18 by adjoly #+# #+# */ /* Created: 2025/04/30 09:42:18 by adjoly #+# #+# */
/* Updated: 2025/05/02 14:53:08 by mmoussou ### ########.fr */ /* Updated: 2025/05/28 11:29:55 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "cppeleven.hpp"
#include <algorithm> #include <algorithm>
#include <dirent.h> #include <dirent.h>
#include <sys/stat.h> #include <sys/stat.h>
@ -19,7 +20,11 @@
using namespace webserv::http; using namespace webserv::http;
Delete::Delete(std::string &data) { this->parse(data); } Delete::Delete(std::string &data, config::Server *srv) {
_url = not_nullptr;
_srv = srv;
this->parse(data);
}
void Delete::parse(std::string const &data) { void Delete::parse(std::string const &data) {
std::istringstream stream(data); std::istringstream stream(data);
@ -85,10 +90,10 @@ Response Delete::execute(void) {
response.setProtocol(this->_protocol); response.setProtocol(this->_protocol);
response.setStatusCode(404); response.setStatusCode(404);
response.addHeader("Content-Type", "text/html"); response.addHeader("Content-Type", "text/html");
response.setBody( response.setBody(http::Errors::getResponseBody(
http::Errors::getResponseBody(response.getStatusCode())); response.getStatusCode(),
_srv->getErrorPage(response.getStatusCode())));
} }
return (response); return (response);
} }

View File

@ -6,25 +6,37 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/30 09:40:16 by adjoly #+# #+# */ /* Created: 2025/04/30 09:40:16 by adjoly #+# #+# */
/* Updated: 2025/05/27 16:49:05 by adjoly ### ########.fr */ /* Updated: 2025/05/28 11:28:01 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "cppeleven.hpp" #include "cppeleven.hpp"
#include "requests/Response.hpp"
#include <algorithm> #include <algorithm>
#include <cstddef>
#include <dirent.h> #include <dirent.h>
#include <exception>
#include <sstream>
#include <string>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include <vector> #include <vector>
#include <server/default.hpp>
#include <requests/default.hpp> #include <requests/default.hpp>
#include <server/default.hpp>
using namespace webserv::http; using namespace webserv::http;
Get::Get(std::string &data) { Get::Get(std::string &data, config::Server *srv) {
// _cgi = not_nullptr; _cgi = not_nullptr;
this->parse(data); _srv = srv;
_url = not_nullptr;
this->parse(data);
}
Get::~Get(void) {
// if (_url != not_nullptr)
// delete _url;
} }
void Get::parse(std::string const &data) { void Get::parse(std::string const &data) {
@ -48,6 +60,8 @@ void Get::parse(std::string const &data) {
} }
} }
_route = _srv->whatRoute(URL(_target));
std::ostringstream body_stream; std::ostringstream body_stream;
while (std::getline(stream, line)) while (std::getline(stream, line))
body_stream << line << "\n"; body_stream << line << "\n";
@ -55,10 +69,28 @@ void Get::parse(std::string const &data) {
_url = new URL("http://" + _headers["Host"] + _target); _url = new URL("http://" + _headers["Host"] + _target);
// if (_route->isCgi(_target)) { std::string targ = _target;
// _cgi = new server::Cgi(this, _route);
// server::ResourceManager::append(_cgi); 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; std::cout << "-- start-line --" << std::endl;
@ -92,9 +124,57 @@ char isDirectory(const std::string &path) {
return S_ISDIR(file_stat.st_mode); 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) { Response Get::execute(void) {
http::Response response; 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(),
_srv->getErrorPage(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; this->_target = this->_route->getRootDir() + this->_target;
try { try {
@ -117,8 +197,8 @@ Response Get::execute(void) {
response.addHeader("Content-Type", response.addHeader("Content-Type",
http::Mime::getType(this->_target)); http::Mime::getType(this->_target));
} else if (this->_route->getDirList()) { } else if (this->_route->getDirList()) {
DIR *dir; DIR * dir;
struct dirent *entry; struct dirent * entry;
struct stat file_stat; struct stat file_stat;
std::vector<std::string> files; std::vector<std::string> files;
@ -192,19 +272,17 @@ body {\n\
response.setStatusCode(200); response.setStatusCode(200);
response.addHeader("Content-Type", response.addHeader("Content-Type",
http::Mime::getType(this->_target)); http::Mime::getType(this->_target));
#ifdef VERBOSE
//_log->debug(response.str().c_str());
#endif
} }
} catch (...) { } catch (...) {
// TODO: replace with a predefined array of error pages // TODO: replace with a predefined array of error pages
response.setProtocol(this->_protocol); response.setProtocol(this->_protocol);
response.setStatusCode(404); response.setStatusCode(404);
response.addHeader("Content-Type", "text/html"); response.addHeader("Content-Type", "text/html");
response.setBody( response.setBody(http::Errors::getResponseBody(
http::Errors::getResponseBody(response.getStatusCode())); response.getStatusCode(),
_srv->getErrorPage(response.getStatusCode())));
} }
delete _url;
return (response); return (response);
} }

View File

@ -6,22 +6,28 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/30 09:50:20 by adjoly #+# #+# */ /* Created: 2025/04/30 09:50:20 by adjoly #+# #+# */
/* Updated: 2025/05/27 09:23:54 by adjoly ### ########.fr */ /* Updated: 2025/05/28 11:28:35 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "cppeleven.hpp"
#include <algorithm> #include <algorithm>
#include <dirent.h> #include <dirent.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <vector> #include <vector>
#include <config/URL.hpp> #include <config/URL.hpp>
#include <requests/default.hpp>
#include <log.hpp> #include <log.hpp>
#include <requests/default.hpp>
using namespace webserv::http; using namespace webserv::http;
Post::Post(std::string &data) { this->parse(data); } Post::Post(std::string &data, config::Server *srv) {
_url = not_nullptr;
_srv = srv;
_cgi = not_nullptr;
this->parse(data);
}
void Post::parse(std::string const &data) { void Post::parse(std::string const &data) {
std::istringstream stream(data); std::istringstream stream(data);
@ -73,7 +79,8 @@ std::string Post::extractFilename(const std::string &header) {
return this->_route->getUpRoot() + header.substr(start, end - start); return this->_route->getUpRoot() + header.substr(start, end - start);
} }
void Post::handleMultipartData(const std::string &body, const std::string &boundary) { void Post::handleMultipartData(const std::string &body,
const std::string &boundary) {
size_t i = 0; size_t i = 0;
std::string delim = "--" + boundary; std::string delim = "--" + boundary;
delim.erase(delim.size() - 1); delim.erase(delim.size() - 1);
@ -103,7 +110,7 @@ void Post::handleMultipartData(const std::string &body, const std::string &bound
Response Post::execute(void) { Response Post::execute(void) {
http::Response response; http::Response response;
try { try {
handleMultipartData( handleMultipartData(
this->_body, this->_body,
@ -115,15 +122,15 @@ Response Post::execute(void) {
response.setProtocol(this->_protocol); response.setProtocol(this->_protocol);
response.setStatusCode(200); response.setStatusCode(200);
response.addHeader("Content-Type", "text/html"); response.addHeader("Content-Type", "text/html");
response.setBody( response.setBody(http::Errors::getResponseBody(
http::Errors::getResponseBody(response.getStatusCode())); response.getStatusCode(),
_srv->getErrorPage(response.getStatusCode())));
} catch (...) { } catch (...) {
response.setProtocol(this->_protocol); response.setProtocol(this->_protocol);
response.setStatusCode(500); response.setStatusCode(500);
response.addHeader("Content-Type", "text/html"); response.setBody(http::Errors::getResponseBody(
response.setBody( response.getStatusCode(),
http::Errors::getResponseBody(response.getStatusCode())); _srv->getErrorPage(response.getStatusCode())));
} }
return (response); return (response);
} }

View File

@ -6,21 +6,23 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 11:12:41 by mmoussou #+# #+# */ /* Created: 2025/04/17 11:12:41 by mmoussou #+# #+# */
/* Updated: 2025/05/27 16:48:09 by adjoly ### ########.fr */ /* Updated: 2025/05/28 11:30:11 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "cppeleven.hpp" #include "cppeleven.hpp"
#include "requests/RedirectResp.hpp" #include "requests/RedirectResp.hpp"
#include "requests/default.hpp" #include "requests/default.hpp"
#include <cstddef>
#include <log.hpp> #include <log.hpp>
#include <server/Cgi.hpp>
#include <server/Client.hpp> #include <server/Client.hpp>
#include <sstream> #include <sstream>
#include <sys/poll.h>
using namespace webserv::server; using namespace webserv::server;
Client::Client(int fd, config::Server *conf) Client::Client(int fd, config::Server *conf) : _fd(fd), _conf(conf) {
: _fd(fd), _conf(conf) {
_request = not_nullptr; _request = not_nullptr;
log("", "Client", "constructor called"); log("", "Client", "constructor called");
_response_done = false; _response_done = false;
@ -49,47 +51,54 @@ void Client::parse(void) {
_getRequest(received_data); _getRequest(received_data);
_route = _conf->whatRoute(URL(this->_request->getTarget())); if (_request == not_nullptr)
this->_request->setRoute(_route); return;
if (_conf->getServerNames() != not_nullptr) { _route = _conf->whatRoute(URL(this->_request->getTarget()));
if (_request->getMethod() != "501" &&
_conf->getServerNames() != not_nullptr) {
std::string host = _request->getHeader("Host"); 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) { if (ret == false) {
throw std::runtime_error("serverName not correcponding"); throw std::runtime_error("serverName not corresponding");
} }
} }
if (!this->_route || this->_route == not_nullptr) { if (!_route || _route == not_nullptr) {
this->_request->setMethod("404"); _request->setMethod("404");
return; return;
} }
if (_route->getRedirect() == true) { if (_route->getRedirect() == true) {
} else if ((this->_request->getMethod() == "GET" && } else if ((_request->getMethod() == "GET" && !_route->getMethods()[0]) ||
!_route->getMethods()[0]) || (_request->getMethod() == "POST" && !_route->getMethods()[1]) ||
(this->_request->getMethod() == "POST" && (_request->getMethod() == "DELETE" && !_route->getMethods()[2]))
!_route->getMethods()[1]) ||
(this->_request->getMethod() == "DELETE" &&
!_route->getMethods()[2]))
this->_request->setMethod("405"); this->_request->setMethod("405");
if (received_data.length() > (unsigned long)(_route->getMaxBody())) if (received_data.length() > (unsigned long)(_route->getMaxBody()))
this->_request->setMethod("413"); _request->setMethod("413");
} }
bool Client::requestParsed(void) { bool Client::requestParsed(void) {
if (_request == not_nullptr) if (_request == not_nullptr)
return false; return false;
if (_request->getCgi() != not_nullptr)
if (!_request->getCgi()->isProcessed())
return false;
return true; return true;
} }
void Client::_getRequest(std::string request_str) { void Client::_getRequest(std::string request_str) {
if (request_str == "") {
_response_done = true;
return;
}
std::string method = request_str.substr( std::string method = request_str.substr(
0, request_str.substr(0, 4).find_last_not_of(" ") + 1); 0, request_str.substr(0, 4).find_last_not_of(" ") + 1);
if (method == "GET") { if (method == "GET") {
this->_request = new http::Get(request_str); _request = new http::Get(request_str, _conf);
std::stringstream str; std::stringstream str;
str << "get request received on port : "; str << "get request received on port : ";
str << _conf->getPort(); str << _conf->getPort();
@ -97,14 +106,14 @@ void Client::_getRequest(std::string request_str) {
str << _request->getTarget(); str << _request->getTarget();
_log->info(str.str()); _log->info(str.str());
} else if (method == "DELETE") { } else if (method == "DELETE") {
this->_request = new http::Delete(request_str); _request = new http::Delete(request_str, _conf);
_log->info("delete request received"); _log->info("delete request received");
} else if (method == "POST") { } else if (method == "POST") {
this->_request = new http::Post(request_str); _request = new http::Post(request_str, _conf);
_log->info("post request received"); _log->info("post request received");
} else { } else {
this->_request = new http::Get(); _request = new http::Get();
this->_request->setMethod("501"); _request->setMethod("501");
_log->info("unsupported request received"); _log->info("unsupported request received");
} }
// set target to correct target with the conf // set target to correct target with the conf
@ -157,18 +166,12 @@ void Client::answer(void) {
str << _response.getStatusCode(); str << _response.getStatusCode();
_log->info(str.str()); _log->info(str.str());
} }
/*std::stringstream str;
str << "response sent, for page : ";
str << _request->getTarget();
str << " with response code : ";
str << _response.getStatusCode();
_log->info(str.str());*/
} }
Client::~Client(void) { Client::~Client(void) {
log("", "Client", "destructor called"); log("", "Client", "destructor called");
delete (http::Get *)(this->_request); if (_request != not_nullptr)
delete _request;
} }
bool Client::isReadyToClose() const { bool Client::isReadyToClose() const {

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/11 16:11:40 by adjoly #+# #+# */ /* Created: 2025/04/11 16:11:40 by adjoly #+# #+# */
/* Updated: 2025/05/27 18:03:35 by adjoly ### ########.fr */ /* Updated: 2025/05/28 10:59:27 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,6 +14,8 @@
#include <algorithm> #include <algorithm>
#include <cerrno> #include <cerrno>
#include <cmath> #include <cmath>
#include <config/default.hpp>
#include <cstddef>
#include <cstring> #include <cstring>
#include <exception> #include <exception>
#include <fcntl.h> #include <fcntl.h>
@ -30,11 +32,10 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <webserv.hpp> #include <webserv.hpp>
using namespace webserv::server; using namespace webserv::server;
std::vector<struct pollfd> PfdManager::_pfd_vec; std::vector<struct pollfd> PfdManager::_pfd_vec;
std::vector<pfd_type> PfdManager::_pfd_type; std::vector<pfd_type> PfdManager::_pfd_type;
extern int _sig; extern int _sig;
@ -68,7 +69,7 @@ std::string getMethod(std::string &data) {
int Server::_fillHostsPorts(std::vector<std::string> &hosts, int Server::_fillHostsPorts(std::vector<std::string> &hosts,
std::vector<int> & ports) { std::vector<int> & ports) {
std::vector<config::Server *> config = _conf->getServers(); std::vector<config::Server *> config = config::_conf->getServers();
for (auto it = range(config)) { for (auto it = range(config)) {
hosts.push_back((*it)->getHost()); hosts.push_back((*it)->getHost());
@ -123,124 +124,27 @@ void Server::_run(void) {
continue; continue;
} }
size_t i = 0; for (size_t i = 0; i < PfdManager::size(); ++i) {
for (auto it = range(_fds_server), i++) { pfd_type type = PfdManager::getType(PfdManager::at(i).fd);
if (PfdManager::at(i).revents & POLLIN) { switch (type) {
struct sockaddr_in client_addr; case SRV:
socklen_t addrlen = sizeof(client_addr); _handle_srv(i);
int client_fd = break;
accept((*it), (struct sockaddr *)&client_addr, &addrlen); case CLIENT:
_handle_client(&i);
if (client_fd < 0) { break;
if (errno == EAGAIN || errno == EWOULDBLOCK) { case RES:
continue; _handle_resource(i);
} break;
std::stringstream str; default:
str << "Accept failed: "; break;
str << strerror(errno);
_log->error(str.str());
continue;
}
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;
pfd.events = POLLIN | POLLOUT;
pfd.revents = 0;
PfdManager::append(pfd, CLIENT);
Client *new_client = new Client(pfd.fd, conf_srv);
if (new_client == NULL) {
continue;
}
_client_data.push_back(new_client);
_log->debug("client pushed");
}
}
for (size_t i = _fds_server.size(); i < PfdManager::size(); ++i) {
if (PfdManager::at(i).revents & POLLERR) {
_log->debug("pollerr");
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 (PfdManager::at(i).revents & POLLIN) {
_log->debug("pollin");
Client *client = _getClient(PfdManager::at(i).fd);
if (client == not_nullptr) {
_log->error("client does not exist");
continue;
}
try {
client->parse();
} catch (std::exception &e) {
_log->error(e.what());
_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;
// }
// }
close(PfdManager::at(i).fd);
PfdManager::remove(PfdManager::at(i).fd);
_log->debug("client removed");
i--;
}
} else if (PfdManager::at(i).revents & POLLOUT) {
std::stringstream str;
str << PfdManager::at(i).fd;
_log->debug("pollout = " + str.str());
Client *client = _getClient(PfdManager::at(i).fd);
if (client == not_nullptr) {
_log->error("client does not exist");
continue;
}
if (client->requestParsed() == true &&
!client->isReadyToClose()) {
client->answer();
continue;
}
if (client->isReadyToClose()) {
_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;
// }
// }
close(PfdManager::at(i).fd);
PfdManager::remove(PfdManager::at(i).fd);
_log->debug("client removed");
i--;
}
} }
} }
} }
} }
Server::Server(config::Config *conf) : _conf(conf) { Server::Server() {
log("", "Server::Server", "config constructor called"); log("", "Server::Server", "config constructor called");
_log = conf->getLogger();
try { try {
_setup(); _setup();
_run(); _run();
@ -251,5 +155,8 @@ Server::Server(config::Config *conf) : _conf(conf) {
Server::~Server(void) { Server::~Server(void) {
log("", "Server::Server", "destructor called"); log("", "Server::Server", "destructor called");
for (auto it = range(_client_data)) {
delete *it;
}
PfdManager::clear(); PfdManager::clear();
} }

142
src/server/ServerHandle.cpp Normal file
View File

@ -0,0 +1,142 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ServerHandle.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/27 18:22:48 by adjoly #+# #+# */
/* Updated: 2025/05/28 10:49:47 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "cppeleven.hpp"
#include <cstddef>
#include <iterator>
#include <server/default.hpp>
#include <sys/poll.h>
using namespace webserv::server;
void Server::_handle_srv(size_t i) {
if (PfdManager::at(i).revents & POLLIN) {
struct sockaddr_in client_addr;
socklen_t addrlen = sizeof(client_addr);
int client_fd = accept(PfdManager::at(i).fd,
(struct sockaddr *)&client_addr, &addrlen);
if (client_fd < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
return;
}
std::stringstream str;
str << "Accept failed: ";
str << strerror(errno);
_log->error(str.str());
return;
}
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
// but just in case
close(client_fd);
return;
}
struct pollfd pfd;
pfd.fd = client_fd;
pfd.events = POLLIN | POLLOUT;
pfd.revents = 0;
PfdManager::append(pfd, CLIENT);
Client *new_client = new Client(pfd.fd, conf_srv);
if (new_client == NULL) {
return;
}
_client_data.push_back(new_client);
_log->debug("client pushed");
}
}
void Server::_handle_client(size_t *i) {
if (PfdManager::getType(PfdManager::at(*i).fd)) {
if (PfdManager::at(*i).revents & POLLERR) {
_log->debug("pollerr");
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 (PfdManager::at(*i).revents & POLLIN) {
_log->debug("pollin");
Client *client = _getClient(PfdManager::at(*i).fd);
if (client == not_nullptr) {
_log->error("client does not exist");
return;
}
try {
client->parse();
if (client->isReadyToClose()) {
_client_data.erase(std::find(_client_data.begin(),
_client_data.end(), client));
delete client;
close(PfdManager::at(*i).fd);
PfdManager::remove(PfdManager::at(*i).fd);
_log->debug("client removed");
i--;
}
} catch (std::exception &e) {
_log->error(e.what());
_client_data.erase(std::find(_client_data.begin(),
_client_data.end(), client));
delete client;
close(PfdManager::at(*i).fd);
PfdManager::remove(PfdManager::at(*i).fd);
_log->debug("client removed");
i--;
}
} else if (PfdManager::at(*i).revents & POLLOUT) {
std::stringstream str;
str << PfdManager::at(*i).fd;
_log->debug("pollout = " + str.str());
Client *client = _getClient(PfdManager::at(*i).fd);
if (client == not_nullptr) {
_log->error("client does not exist");
return;
}
if (client->requestParsed() == true && !client->isReadyToClose()) {
client->answer();
return;
}
if (client->isReadyToClose()) {
_client_data.erase(std::find(_client_data.begin(),
_client_data.end(), client));
delete client;
close(PfdManager::at(*i).fd);
PfdManager::remove(PfdManager::at(*i).fd);
_log->debug("client removed");
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
View File

@ -1,4 +1,4 @@
#!/usr/bin/env python3 #!/nix/store/kjvgj2n3yn70hmjifg6y0bk9m4rf7jba-python3-3.12.10/bin/python3
# Import modules for CGI handling # Import modules for CGI handling
import cgi import cgi
@ -11,7 +11,9 @@ cgitb.enable()
form = cgi.FieldStorage() form = cgi.FieldStorage()
# Set the content type to HTML # Set the content type to HTML
print("Content-Type: text/html\n") print("Content-Type: text/html")
print("")
# Output a simple HTML page # Output a simple HTML page
print("<html>") print("<html>")