mirror of
https://github.com/KeyZox71/webserv.git
synced 2025-05-10 18:28:45 +02:00
「🏗️」 wip: reformated request include folder
This commit is contained in:
@ -6,13 +6,14 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/22 12:17:48 by adjoly #+# #+# */
|
||||
/* Updated: 2025/04/24 14:01:15 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/04/29 15:48:19 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstring>
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
@ -26,17 +27,21 @@ class URL {
|
||||
return comparePathSegments(other);
|
||||
}
|
||||
|
||||
bool operator<(const URL &other) const { return _full_url < other._full_url; }
|
||||
bool operator<(const URL &other) const {
|
||||
return _full_url < other._full_url;
|
||||
}
|
||||
|
||||
std::vector<std::string> getSegments(void) { return _path_segments; }
|
||||
std::string getFullUrl(void) { return _full_url; }
|
||||
std::string getQueryString(void) const { return _query_string; }
|
||||
std::string getFullUrl(void) const { return _full_url; }
|
||||
std::string getQueryString(void) const { return _query_string; }
|
||||
|
||||
private:
|
||||
void parse() {
|
||||
size_t scheme_pos = _full_url.find("://");
|
||||
size_t path_start = 0;
|
||||
size_t query_start = _full_url.find('?');
|
||||
size_t port_start = _full_url.find(':', scheme_pos + 3);
|
||||
size_t port_end = _full_url.find('/', port_start);
|
||||
|
||||
if (scheme_pos != std::string::npos) {
|
||||
path_start = _full_url.find('/', scheme_pos + 3);
|
||||
@ -47,7 +52,8 @@ class URL {
|
||||
if (query_start != std::string::npos) {
|
||||
_query_string = _full_url.substr(query_start + 1);
|
||||
if (path_start != std::string::npos) {
|
||||
std::string path = _full_url.substr(path_start, query_start - path_start);
|
||||
std::string path =
|
||||
_full_url.substr(path_start, query_start - path_start);
|
||||
splitPath(path, _path_segments);
|
||||
}
|
||||
} else {
|
||||
@ -56,6 +62,10 @@ class URL {
|
||||
splitPath(path, _path_segments);
|
||||
}
|
||||
}
|
||||
|
||||
if (port_start != std::string::npos && port_end != std::string::npos) {
|
||||
_port = _full_url.substr(port_start + 1, port_end - port_start - 1);
|
||||
}
|
||||
}
|
||||
|
||||
void splitPath(const std::string &path,
|
||||
@ -83,4 +93,11 @@ class URL {
|
||||
std::string _full_url;
|
||||
std::vector<std::string> _path_segments;
|
||||
std::string _query_string;
|
||||
std::string _port;
|
||||
};
|
||||
|
||||
inline std::ostream &operator<<(std::ostream &os, const URL &URL) {
|
||||
|
||||
os << URL.getFullUrl();
|
||||
return os;
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/03/20 09:28:27 by adjoly #+# #+# */
|
||||
/* Updated: 2025/04/28 14:29:54 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/04/29 17:28:36 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -40,10 +40,10 @@ inline void log(std::string emoji, std::string who, std::string str) {
|
||||
class Logger {
|
||||
public:
|
||||
Logger(void) : _ttyOnly(true) {
|
||||
//log("➕", "Logger", "default constructor called");
|
||||
log("➕", "Logger", "default constructor called");
|
||||
}
|
||||
Logger(const std::string &fileName) : _fileName(fileName) {
|
||||
//log("➕", "Logger", "filename constructor called");
|
||||
log("➕", "Logger", "filename constructor called");
|
||||
_file.open(fileName.c_str(), std::ios::app);
|
||||
if (!_file.is_open() && !_ttyOnly) {
|
||||
_ttyOnly = true;
|
||||
@ -53,7 +53,7 @@ class Logger {
|
||||
}
|
||||
|
||||
Logger(const Logger &other) : _ttyOnly(other._ttyOnly) {
|
||||
//log("➕", "Logger", "copy constructor called");
|
||||
log("➕", "Logger", "copy constructor called");
|
||||
if (!other._ttyOnly) {
|
||||
_file.open(other._fileName.c_str(), std::ios::app);
|
||||
if (!_file.is_open()) {
|
||||
|
61
includes/requests/ARequest.hpp
Normal file
61
includes/requests/ARequest.hpp
Normal file
@ -0,0 +1,61 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ARequest.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/03 17:23:00 by mmoussou #+# #+# */
|
||||
/* Updated: 2025/04/30 09:34:49 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "config/URL.hpp"
|
||||
#include <ctime>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
#include <requests/IMessage.hpp>
|
||||
#include <requests/Mime.hpp>
|
||||
#include <requests/Response.hpp>
|
||||
|
||||
#include <config/default.hpp>
|
||||
|
||||
namespace webserv {
|
||||
namespace http {
|
||||
|
||||
class ARequest : public http::IMessage {
|
||||
public:
|
||||
virtual ~ARequest(void) {
|
||||
log("➖", "ARequest", "destructor called");
|
||||
delete _url;
|
||||
}
|
||||
|
||||
virtual void parse(std::string const &data) = 0;
|
||||
virtual Response execute(void) = 0;
|
||||
|
||||
std::string str(void) const;
|
||||
|
||||
std::string getMethod(void) const;
|
||||
std::string getTarget(void) const;
|
||||
std::string getProtocol(void) const;
|
||||
config::Server *getConfig(void) const;
|
||||
|
||||
void setMethod(std::string const method);
|
||||
void setTarget(std::string const target);
|
||||
void setProtocol(std::string const protocol);
|
||||
void setServer(std::string const protocol);
|
||||
|
||||
protected:
|
||||
std::string _method;
|
||||
std::string _target;
|
||||
std::string _protocol;
|
||||
config::Server *_conf;
|
||||
URL *_url;
|
||||
};
|
||||
|
||||
} // namespace http
|
||||
} // namespace webserv
|
@ -6,14 +6,13 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/03/24 14:17:34 by adjoly #+# #+# */
|
||||
/* Updated: 2025/04/25 13:40:10 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/04/30 09:36:02 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "requests/HttpIMessage.hpp"
|
||||
#include "requests/HttpRequest.hpp"
|
||||
#include <requests/ARequest.hpp>
|
||||
#include <config/default.hpp>
|
||||
#include <map>
|
||||
#include <string>
|
||||
@ -22,7 +21,7 @@ namespace webserv {
|
||||
|
||||
class Cgi {
|
||||
public:
|
||||
Cgi(http::IRequest *, config::Server *);
|
||||
Cgi(http::ARequest *, config::Server *);
|
||||
~Cgi(void);
|
||||
|
||||
std::string getEnv(std::string &);
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/03/16 17:51:46 by mmoussou #+# #+# */
|
||||
/* Updated: 2025/04/24 14:56:08 by mmoussou ### ########.fr */
|
||||
/* Updated: 2025/04/30 09:35:21 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -15,7 +15,7 @@
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
#include <requests/HttpResponse.hpp>
|
||||
#include <requests/Response.hpp>
|
||||
|
||||
namespace webserv {
|
||||
namespace http {
|
||||
|
@ -1,93 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* HttpRequest.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/03 17:23:00 by mmoussou #+# #+# */
|
||||
/* Updated: 2025/04/24 15:09:52 by mmoussou ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <ctime>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
|
||||
#include <requests/HttpIMessage.hpp>
|
||||
#include <requests/HttpResponse.hpp>
|
||||
#include <requests/Mime.hpp>
|
||||
|
||||
#include <config/default.hpp>
|
||||
|
||||
namespace webserv {
|
||||
namespace http {
|
||||
|
||||
class IRequest: public http::IMessage {
|
||||
public:
|
||||
virtual ~IRequest(void);
|
||||
|
||||
virtual void parse(std::string const &data) = 0;
|
||||
virtual http::Response execute(void) = 0;
|
||||
|
||||
std::string str(void) const;
|
||||
|
||||
std::string getMethod(void) const;
|
||||
std::string getTarget(void) const;
|
||||
std::string getProtocol(void) const;
|
||||
config::Server *getConfig(void) const;
|
||||
|
||||
void setMethod(std::string const method);
|
||||
void setTarget(std::string const target);
|
||||
void setProtocol(std::string const protocol);
|
||||
void setServer(std::string const protocol);
|
||||
|
||||
protected:
|
||||
std::string _method;
|
||||
std::string _target;
|
||||
std::string _protocol;
|
||||
config::Server *_conf;
|
||||
|
||||
};
|
||||
|
||||
class Get: public http::IRequest {
|
||||
public:
|
||||
Get(void);
|
||||
~Get(void);
|
||||
Get(std::string &data);
|
||||
|
||||
void parse(std::string const &data);
|
||||
|
||||
http::Response execute(void);
|
||||
|
||||
};
|
||||
|
||||
class Post: public http::IRequest {
|
||||
public:
|
||||
Post(void);
|
||||
~Post(void);
|
||||
Post(std::string &data);
|
||||
|
||||
void parse(std::string const &data);
|
||||
|
||||
http::Response execute(void);
|
||||
|
||||
};
|
||||
|
||||
class Delete: public http::IRequest {
|
||||
public:
|
||||
Delete(void);
|
||||
~Delete(void);
|
||||
Delete(std::string &data);
|
||||
|
||||
void parse(std::string const &data);
|
||||
|
||||
http::Response execute(void);
|
||||
|
||||
};
|
||||
|
||||
} // -namespace http
|
||||
} // -namespace webserv
|
49
includes/requests/RequestImplement.hpp
Normal file
49
includes/requests/RequestImplement.hpp
Normal file
@ -0,0 +1,49 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* RequestImplement.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/30 09:30:15 by adjoly #+# #+# */
|
||||
/* Updated: 2025/04/30 09:34:17 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <requests/ARequest.hpp>
|
||||
|
||||
namespace webserv {
|
||||
namespace http {
|
||||
|
||||
class Get : public ARequest {
|
||||
public:
|
||||
Get(void) {}
|
||||
Get(std::string &data);
|
||||
|
||||
void parse(std::string const &data);
|
||||
|
||||
Response execute(void);
|
||||
};
|
||||
|
||||
class Post : public ARequest {
|
||||
public:
|
||||
Post(void) {}
|
||||
Post(std::string &data);
|
||||
|
||||
void parse(std::string const &data);
|
||||
|
||||
Response execute(void);
|
||||
};
|
||||
|
||||
class Delete : public http::ARequest {
|
||||
public:
|
||||
Delete(void) {}
|
||||
Delete(std::string &data);
|
||||
|
||||
void parse(std::string const &data);
|
||||
|
||||
Response execute(void);
|
||||
};
|
||||
|
||||
}; // namespace http
|
||||
}; // namespace webserv
|
@ -1,12 +1,12 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* HttpResponse.hpp :+: :+: :+: */
|
||||
/* Response.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/03 17:21:20 by mmoussou #+# #+# */
|
||||
/* Updated: 2025/04/23 14:36:47 by mmoussou ### ########.fr */
|
||||
/* Updated: 2025/04/30 09:33:47 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -14,33 +14,32 @@
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include <requests/HttpIMessage.hpp>
|
||||
#include <requests/IMessage.hpp>
|
||||
|
||||
typedef unsigned int uint;
|
||||
|
||||
namespace webserv {
|
||||
namespace http {
|
||||
|
||||
class Response: public http::IMessage {
|
||||
public:
|
||||
class Response : public http::IMessage {
|
||||
public:
|
||||
Response(void);
|
||||
~Response(void);
|
||||
|
||||
std::string getProtocol(void) const;
|
||||
std::string getProtocol(void) const;
|
||||
uint getStatusCode(void) const;
|
||||
std::string getStatusText(void) const;
|
||||
std::string getStatusText(void) const;
|
||||
|
||||
void setProtocol(std::string const protocol);
|
||||
void setStatusCode(uint const status_code);
|
||||
void setProtocol(std::string const protocol);
|
||||
void setStatusCode(uint const status_code);
|
||||
|
||||
std::string str(void) const;
|
||||
std::string str(void) const;
|
||||
|
||||
private:
|
||||
std::string _protocol;
|
||||
private:
|
||||
std::string _protocol;
|
||||
uint _status_code;
|
||||
std::string _status_text;
|
||||
|
||||
std::string _status_text;
|
||||
};
|
||||
|
||||
} // -namespace http
|
||||
} // -namespace webserv
|
||||
} // namespace http
|
||||
} // namespace webserv
|
@ -6,15 +6,21 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/03 15:48:22 by mmoussou #+# #+# */
|
||||
/* Updated: 2025/04/24 13:48:09 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/04/30 09:36:57 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <requests/Errors.hpp>
|
||||
#include <requests/HttpRequest.hpp>
|
||||
#include <requests/HttpResponse.hpp>
|
||||
#include <requests/ARequest.hpp>
|
||||
#include <requests/Cgi.hpp>
|
||||
#include <requests/Errors.hpp>
|
||||
#include <requests/IMessage.hpp>
|
||||
#include <requests/Mime.hpp>
|
||||
#include <requests/RequestImplement.hpp>
|
||||
#include <requests/Response.hpp>
|
||||
|
||||
namespace webserv {
|
||||
|
||||
};
|
||||
|
||||
using namespace webserv;
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/29 14:20:09 by mmoussou #+# #+# */
|
||||
/* Updated: 2025/04/29 14:58:45 by mmoussou ### ########.fr */
|
||||
/* Updated: 2025/04/29 15:44:50 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -18,9 +18,9 @@
|
||||
namespace webserv {
|
||||
namespace server {
|
||||
|
||||
class ClientResource {
|
||||
class AClientResource {
|
||||
public:
|
||||
virtual ~ClientResource() {}
|
||||
virtual ~AClientResource() {}
|
||||
|
||||
void addFileDescriptor(struct pollfd fd);
|
||||
struct pollfd getFileDescriptor();
|
@ -6,34 +6,32 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/03 16:07:01 by mmoussou #+# #+# */
|
||||
/* Updated: 2025/04/28 14:32:33 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/04/29 15:50:52 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
|
||||
#include <requests/HttpRequest.hpp>
|
||||
#include <requests/Errors.hpp>
|
||||
#include <sys/stat.h>
|
||||
#include <config/URL.hpp>
|
||||
#include <dirent.h>
|
||||
#include <requests/Errors.hpp>
|
||||
#include <requests/HttpRequest.hpp>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <log.hpp>
|
||||
|
||||
using namespace webserv;
|
||||
|
||||
http::IRequest::~IRequest(void) {
|
||||
|
||||
}
|
||||
|
||||
std::string http::IRequest::str(void) const
|
||||
{
|
||||
std::string http::IRequest::str(void) const {
|
||||
std::ostringstream response;
|
||||
|
||||
response << this->_method << " " << this->_target << " " << this->_protocol;
|
||||
response << "\r\n";
|
||||
|
||||
for (std::map<std::string, std::string>::const_iterator it = this->_headers.begin(); it != this->_headers.end(); ++it)
|
||||
for (std::map<std::string, std::string>::const_iterator it =
|
||||
this->_headers.begin();
|
||||
it != this->_headers.end(); ++it)
|
||||
response << it->first << ": " << it->second << "\r\n";
|
||||
|
||||
response << "\r\n";
|
||||
@ -42,71 +40,45 @@ std::string http::IRequest::str(void) const
|
||||
return (response.str());
|
||||
}
|
||||
|
||||
void parse(std::string const &data) { (void) data; }
|
||||
http::Response execute(void) { return (http::Response()); }
|
||||
void parse(std::string const &data) { (void)data; }
|
||||
http::Response execute(void) { return (http::Response()); }
|
||||
|
||||
std::string http::IRequest::getMethod(void) const
|
||||
{
|
||||
return (this->_method);
|
||||
}
|
||||
std::string http::IRequest::getMethod(void) const { return (this->_method); }
|
||||
|
||||
std::string http::IRequest::getTarget(void) const
|
||||
{
|
||||
return (this->_target);
|
||||
}
|
||||
std::string http::IRequest::getTarget(void) const { return (this->_target); }
|
||||
|
||||
std::string http::IRequest::getProtocol(void) const
|
||||
{
|
||||
std::string http::IRequest::getProtocol(void) const {
|
||||
return (this->_protocol);
|
||||
}
|
||||
|
||||
void http::IRequest::setMethod(std::string const method)
|
||||
{
|
||||
void http::IRequest::setMethod(std::string const method) {
|
||||
this->_method = method;
|
||||
}
|
||||
|
||||
void http::IRequest::setTarget(std::string const target)
|
||||
{
|
||||
void http::IRequest::setTarget(std::string const target) {
|
||||
this->_target = target;
|
||||
}
|
||||
|
||||
void http::IRequest::setProtocol(std::string const protocol)
|
||||
{
|
||||
void http::IRequest::setProtocol(std::string const protocol) {
|
||||
this->_protocol = protocol;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
http::Get::Get(std::string &data) { this->parse(data); }
|
||||
|
||||
http::Get::Get(void)
|
||||
{
|
||||
}
|
||||
void http::Get::parse(std::string const &data) {
|
||||
std::istringstream stream(data);
|
||||
std::string line;
|
||||
|
||||
http::Get::~Get(void)
|
||||
{
|
||||
}
|
||||
|
||||
http::Get::Get(std::string &data)
|
||||
{
|
||||
this->parse(data);
|
||||
}
|
||||
|
||||
void http::Get::parse(std::string const &data)
|
||||
{
|
||||
std::istringstream stream(data);
|
||||
std::string line;
|
||||
|
||||
if (std::getline(stream, line))
|
||||
{
|
||||
if (std::getline(stream, line)) {
|
||||
std::istringstream line_stream(line);
|
||||
line_stream >> this->_method >> this->_target >> this->_protocol;
|
||||
this->_target.insert(this->_target.begin(), '.');
|
||||
/* this->_target.insert(this->_target.begin(), '.'); */
|
||||
}
|
||||
|
||||
while (std::getline(stream, line) && line != "\r")
|
||||
{
|
||||
while (std::getline(stream, line) && line != "\r") {
|
||||
size_t delimiter_index = line.find(':');
|
||||
if (delimiter_index != std::string::npos)
|
||||
{
|
||||
if (delimiter_index != std::string::npos) {
|
||||
std::string key = line.substr(0, delimiter_index);
|
||||
std::string value = line.substr(delimiter_index + 2);
|
||||
this->_headers.insert(std::make_pair(key, value));
|
||||
@ -118,54 +90,59 @@ void http::Get::parse(std::string const &data)
|
||||
body_stream << line << "\n";
|
||||
this->_body = body_stream.str();
|
||||
|
||||
_url = new URL("http://" + _headers["Host"] + _target);
|
||||
std::cout << *_url << std::endl;
|
||||
|
||||
/*
|
||||
std::cout << "-- start-line --" << std::endl;
|
||||
std::cout << "method: " << this->_method << std::endl;
|
||||
std::cout << "target: " << this->_target << std::endl;
|
||||
std::cout << "target: " << this->_target << std::You can use every macro and
|
||||
define like FD_SET, FD_CLR, FD_ISSET and, FD_ZERO (understanding what they do
|
||||
and how they work is very useful). • A request to your server should never hang
|
||||
indefinitely. • Your server must be compatible with standard web browsers of
|
||||
your choice. • We will consider that NGINX is HTTP 1.1 compliant and may be used
|
||||
to compare headers and answer behaviors. • Your HTTP response status codes must
|
||||
be accurate. • Your server must have default error pages if none are provided.
|
||||
• You can’t use fork for anything other than CGI (like PHP, or Python, and so
|
||||
forth). • You must be able to serve a fully static website. • Clients must be
|
||||
able to upload files. • You need at least the GET, POST, and DELETE methodendl;
|
||||
std::cout << "protocol: " << this->_protocol << std::endl;
|
||||
std::cout << std::endl;
|
||||
std::cout << "-- headers --" << std::endl;
|
||||
for (std::map<std::string, std::string>::const_iterator it = this->_headers.begin(); it != this->_headers.end(); ++it)
|
||||
std::cout << it->first << ": " << it->second << std::endl;
|
||||
std::cout << std::endl;
|
||||
std::cout << "-- body --" << std::endl << this->_body << std::endl;
|
||||
for (std::map<std::string, std::string>::const_iterator it =
|
||||
this->_headers.begin(); it != this->_headers.end(); ++it) std::cout << it->first
|
||||
<< ": " << it->second << std::endl; std::cout << std::endl; std::cout << "--
|
||||
body --" << std::endl << this->_body << std::endl;
|
||||
*/
|
||||
}
|
||||
|
||||
char isDirectory(const std::string& path)
|
||||
{
|
||||
char isDirectory(const std::string &path) {
|
||||
struct stat file_stat;
|
||||
|
||||
if (stat(path.c_str(), &file_stat) != 0)
|
||||
{
|
||||
if (stat(path.c_str(), &file_stat) != 0) {
|
||||
throw std::runtime_error("can't open file (non-existant ?)");
|
||||
}
|
||||
return S_ISDIR(file_stat.st_mode);
|
||||
}
|
||||
|
||||
http::Response http::Get::execute(void)
|
||||
{
|
||||
http::Response response;
|
||||
http::Response http::Get::execute(void) {
|
||||
http::Response response;
|
||||
|
||||
try
|
||||
{
|
||||
if (isDirectory(this->_target))
|
||||
{
|
||||
DIR *dir;
|
||||
struct dirent *entry;
|
||||
struct stat file_stat;
|
||||
try {
|
||||
if (isDirectory(this->_target)) {
|
||||
DIR *dir;
|
||||
struct dirent *entry;
|
||||
struct stat file_stat;
|
||||
std::vector<std::string> files;
|
||||
|
||||
|
||||
if ((dir = opendir(this->_target.c_str())) == NULL)
|
||||
throw;
|
||||
while ((entry = readdir(dir)) != NULL)
|
||||
{
|
||||
while ((entry = readdir(dir)) != NULL) {
|
||||
std::string file_name = entry->d_name;
|
||||
if (file_name == ".")
|
||||
continue;
|
||||
std::string file_path = this->_target + "/" + file_name;
|
||||
if (stat(file_path.c_str(), &file_stat) == 0)
|
||||
{
|
||||
if (stat(file_path.c_str(), &file_stat) == 0) {
|
||||
if (S_ISDIR(file_stat.st_mode))
|
||||
files.push_back(file_name + "/");
|
||||
else
|
||||
@ -201,43 +178,43 @@ body {\n\
|
||||
|
||||
body += "<body><ul>\n";
|
||||
for (size_t i = 0; i < files.size(); i++)
|
||||
body += "<li><a href=\"" + files[i] + "\">" + files[i] + "</a></li>\n";
|
||||
body += "<li><a href=\"" + files[i] + "\">" + files[i] +
|
||||
"</a></li>\n";
|
||||
body += "</ul></body></html>";
|
||||
|
||||
response.setProtocol(this->_protocol);
|
||||
response.setStatusCode(200);
|
||||
std::stringstream length;
|
||||
std::stringstream length;
|
||||
length << body.length();
|
||||
response.addHeader("Content-Length", length.str());
|
||||
response.addHeader("Content-Type", "text/html");
|
||||
response.setBody(body);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
std::ifstream file(this->_target.c_str(), std::ios::binary);
|
||||
|
||||
} else {
|
||||
std::ifstream file(this->_target.c_str(), std::ios::binary);
|
||||
std::streampos file_start = file.tellg();
|
||||
response.setBody(std::string((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>()));
|
||||
std::stringstream length;
|
||||
response.setBody(std::string((std::istreambuf_iterator<char>(file)),
|
||||
std::istreambuf_iterator<char>()));
|
||||
std::stringstream length;
|
||||
length << (file.tellg() - file_start);
|
||||
response.addHeader("Content-Length", length.str());
|
||||
|
||||
response.setProtocol(this->_protocol);
|
||||
response.setStatusCode(200);
|
||||
response.addHeader("Content-Type", http::Mime::getType(this->_target));
|
||||
response.addHeader("Content-Type",
|
||||
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
|
||||
response.setProtocol(this->_protocol);
|
||||
response.setStatusCode(404);
|
||||
response.addHeader("Content-Type", "text/html");
|
||||
response.setBody(http::Errors::getResponseBody(response.getStatusCode()));
|
||||
response.setBody(
|
||||
http::Errors::getResponseBody(response.getStatusCode()));
|
||||
}
|
||||
|
||||
return (response);
|
||||
@ -245,36 +222,22 @@ body {\n\
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
http::Delete::Delete(void)
|
||||
{
|
||||
}
|
||||
|
||||
http::Delete::~Delete(void)
|
||||
{
|
||||
}
|
||||
http::Delete::Delete(std::string &data) { this->parse(data); }
|
||||
|
||||
http::Delete::Delete(std::string &data)
|
||||
{
|
||||
this->parse(data);
|
||||
}
|
||||
void http::Delete::parse(std::string const &data) {
|
||||
std::istringstream stream(data);
|
||||
std::string line;
|
||||
|
||||
void http::Delete::parse(std::string const &data)
|
||||
{
|
||||
std::istringstream stream(data);
|
||||
std::string line;
|
||||
|
||||
if (std::getline(stream, line))
|
||||
{
|
||||
if (std::getline(stream, line)) {
|
||||
std::istringstream line_stream(line);
|
||||
line_stream >> this->_method >> this->_target >> this->_protocol;
|
||||
this->_target.insert(this->_target.begin(), '.');
|
||||
}
|
||||
|
||||
while (std::getline(stream, line) && line != "\r")
|
||||
{
|
||||
while (std::getline(stream, line) && line != "\r") {
|
||||
size_t delimiter_index = line.find(':');
|
||||
if (delimiter_index != std::string::npos)
|
||||
{
|
||||
if (delimiter_index != std::string::npos) {
|
||||
std::string key = line.substr(0, delimiter_index);
|
||||
std::string value = line.substr(delimiter_index + 2);
|
||||
this->_headers.insert(std::make_pair(key, value));
|
||||
@ -286,6 +249,9 @@ void http::Delete::parse(std::string const &data)
|
||||
body_stream << line << "\n";
|
||||
this->_body = body_stream.str();
|
||||
|
||||
_url = new URL(_target);
|
||||
std::cout << *_url << std::endl;
|
||||
|
||||
/*
|
||||
std::cout << "-- start-line --" << std::endl;
|
||||
std::cout << "method: " << this->_method << std::endl;
|
||||
@ -293,34 +259,33 @@ void http::Delete::parse(std::string const &data)
|
||||
std::cout << "protocol: " << this->_protocol << std::endl;
|
||||
std::cout << std::endl;
|
||||
std::cout << "-- headers --" << std::endl;
|
||||
for (std::map<std::string, std::string>::const_iterator it = this->_headers.begin(); it != this->_headers.end(); ++it)
|
||||
std::cout << it->first << ": " << it->second << std::endl;
|
||||
std::cout << std::endl;
|
||||
for (std::map<std::string, std::string>::const_iterator it =
|
||||
this->_headers.begin(); it != this->_headers.end(); ++it) std::cout <<
|
||||
it->first << ": " << it->second << std::endl; std::cout << std::endl;
|
||||
std::cout << "-- body --" << std::endl << this->_body << std::endl;
|
||||
*/
|
||||
}
|
||||
|
||||
http::Response http::Delete::execute(void)
|
||||
{
|
||||
http::Response response;
|
||||
http::Response http::Delete::execute(void) {
|
||||
http::Response response;
|
||||
|
||||
try
|
||||
{
|
||||
try {
|
||||
if (std::remove(this->_target.c_str()))
|
||||
throw std::runtime_error("can't remove file, FF");
|
||||
response.setProtocol(this->_protocol);
|
||||
response.setStatusCode(204);
|
||||
time_t now = std::time(NULL);
|
||||
response.addHeader("Date", std::string(std::ctime(&now)));
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
// TODO: check errno value and get corresponding error page, check for corresponding error code : https://cdn.discordapp.com/attachments/784779058407014403/1350841524778307586/image.png?ex=67d8dd74&is=67d78bf4&hm=c030468d3862627d6402bf200960d1a15249ba2f8dac772af3283b368a77f2f5&
|
||||
} catch (...) {
|
||||
// TODO: check errno value and get corresponding error page, check for
|
||||
// corresponding error code :
|
||||
// https://cdn.discordapp.com/attachments/784779058407014403/1350841524778307586/image.png?ex=67d8dd74&is=67d78bf4&hm=c030468d3862627d6402bf200960d1a15249ba2f8dac772af3283b368a77f2f5&
|
||||
|
||||
response.setProtocol(this->_protocol);
|
||||
response.setStatusCode(404);
|
||||
response.addHeader("Content-Type", "text/html");
|
||||
response.setBody(http::Errors::getResponseBody(response.getStatusCode()));
|
||||
response.setBody(
|
||||
http::Errors::getResponseBody(response.getStatusCode()));
|
||||
}
|
||||
|
||||
return (response);
|
||||
@ -328,36 +293,21 @@ http::Response http::Delete::execute(void)
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
http::Post::Post(void)
|
||||
{
|
||||
}
|
||||
http::Post::Post(std::string &data) { this->parse(data); }
|
||||
|
||||
http::Post::~Post(void)
|
||||
{
|
||||
}
|
||||
void http::Post::parse(std::string const &data) {
|
||||
std::istringstream stream(data);
|
||||
std::string line;
|
||||
|
||||
http::Post::Post(std::string &data)
|
||||
{
|
||||
this->parse(data);
|
||||
}
|
||||
|
||||
void http::Post::parse(std::string const &data)
|
||||
{
|
||||
std::istringstream stream(data);
|
||||
std::string line;
|
||||
|
||||
if (std::getline(stream, line))
|
||||
{
|
||||
if (std::getline(stream, line)) {
|
||||
std::istringstream line_stream(line);
|
||||
line_stream >> this->_method >> this->_target >> this->_protocol;
|
||||
this->_target.insert(this->_target.begin(), '.');
|
||||
}
|
||||
|
||||
while (std::getline(stream, line) && line != "\r")
|
||||
{
|
||||
while (std::getline(stream, line) && line != "\r") {
|
||||
size_t delimiter_index = line.find(':');
|
||||
if (delimiter_index != std::string::npos)
|
||||
{
|
||||
if (delimiter_index != std::string::npos) {
|
||||
std::string key = line.substr(0, delimiter_index);
|
||||
std::string value = line.substr(delimiter_index + 2);
|
||||
this->_headers.insert(std::make_pair(key, value));
|
||||
@ -369,6 +319,9 @@ void http::Post::parse(std::string const &data)
|
||||
body_stream << line << "\n";
|
||||
this->_body = body_stream.str();
|
||||
|
||||
_url = new URL(_target);
|
||||
std::cout << *_url << std::endl;
|
||||
|
||||
/*
|
||||
std::cout << "-- start-line --" << std::endl;
|
||||
std::cout << "method: " << this->_method << std::endl;
|
||||
@ -376,45 +329,40 @@ void http::Post::parse(std::string const &data)
|
||||
std::cout << "protocol: " << this->_protocol << std::endl;
|
||||
std::cout << std::endl;
|
||||
std::cout << "-- headers --" << std::endl;
|
||||
for (std::map<std::string, std::string>::const_iterator it = this->_headers.begin(); it != this->_headers.end(); ++it)
|
||||
std::cout << it->first << ": " << it->second << std::endl;
|
||||
std::cout << std::endl;
|
||||
for (std::map<std::string, std::string>::const_iterator it =
|
||||
this->_headers.begin(); it != this->_headers.end(); ++it) std::cout <<
|
||||
it->first << ": " << it->second << std::endl; std::cout << std::endl;
|
||||
//std::cout << "-- body --" << std::endl << this->_body << std::endl;
|
||||
*/
|
||||
}
|
||||
|
||||
std::string extractFilename(const std::string &header)
|
||||
{
|
||||
std::string extractFilename(const std::string &header) {
|
||||
size_t start = header.find("filename=\"") + 10;
|
||||
size_t end = header.find("\"", start);
|
||||
return header.substr(start, end - start);
|
||||
}
|
||||
|
||||
void handleMultipartData(const std::string &body, const std::string &boundary)
|
||||
{
|
||||
void handleMultipartData(const std::string &body, const std::string &boundary) {
|
||||
size_t i = 0;
|
||||
std::string delim = "--" + boundary;
|
||||
std::string delim = "--" + boundary;
|
||||
delim.erase(delim.size() - 1);
|
||||
|
||||
while ((i = body.find(delim, i)) != std::string::npos)
|
||||
{
|
||||
size_t start = i + delim.length();
|
||||
while ((i = body.find(delim, i)) != std::string::npos) {
|
||||
size_t start = i + delim.length();
|
||||
size_t end = body.find("\r\n\r\n", start);
|
||||
|
||||
if (end != std::string::npos)
|
||||
{
|
||||
if (end != std::string::npos) {
|
||||
std::string part_header = body.substr(start, end - start);
|
||||
//std::cout << std::endl << std::endl << std::endl << std::endl;
|
||||
std::string part_content = body.substr(end + 4, body.find(delim, end) - end - 4);
|
||||
// std::cout << std::endl << std::endl << std::endl << std::endl;
|
||||
std::string part_content =
|
||||
body.substr(end + 4, body.find(delim, end) - end - 4);
|
||||
|
||||
std::ofstream outfile(extractFilename(part_header).c_str(), std::ios::binary);
|
||||
if (outfile.is_open())
|
||||
{
|
||||
std::ofstream outfile(extractFilename(part_header).c_str(),
|
||||
std::ios::binary);
|
||||
if (outfile.is_open()) {
|
||||
outfile.write(part_content.c_str(), part_content.length());
|
||||
outfile.close();
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
std::cerr << "open failed" << std::endl;
|
||||
}
|
||||
}
|
||||
@ -423,25 +371,28 @@ void handleMultipartData(const std::string &body, const std::string &boundary)
|
||||
}
|
||||
}
|
||||
|
||||
http::Response http::Post::execute(void)
|
||||
{
|
||||
http::Response response;
|
||||
http::Response http::Post::execute(void) {
|
||||
http::Response response;
|
||||
|
||||
try
|
||||
{
|
||||
handleMultipartData(this->_body, this->getHeaders()["Content-Type"].substr(this->getHeaders()["Content-Type"].find("=", this->getHeaders()["Content-Type"].find(";")) + 1));
|
||||
try {
|
||||
handleMultipartData(
|
||||
this->_body,
|
||||
this->getHeaders()["Content-Type"].substr(
|
||||
this->getHeaders()["Content-Type"].find(
|
||||
"=", this->getHeaders()["Content-Type"].find(";")) +
|
||||
1));
|
||||
|
||||
response.setProtocol(this->_protocol);
|
||||
response.setStatusCode(200);
|
||||
response.addHeader("Content-Type", "text/html");
|
||||
response.setBody(http::Errors::getResponseBody(response.getStatusCode()));
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
response.setBody(
|
||||
http::Errors::getResponseBody(response.getStatusCode()));
|
||||
} catch (...) {
|
||||
response.setProtocol(this->_protocol);
|
||||
response.setStatusCode(500);
|
||||
response.addHeader("Content-Type", "text/html");
|
||||
response.setBody(http::Errors::getResponseBody(response.getStatusCode()));
|
||||
response.setBody(
|
||||
http::Errors::getResponseBody(response.getStatusCode()));
|
||||
}
|
||||
return (response);
|
||||
}
|
||||
|
@ -1,42 +1,27 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Resource.cpp :+: :+: :+: */
|
||||
/* AResource.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/29 14:42:01 by mmoussou #+# #+# */
|
||||
/* Updated: 2025/04/29 14:56:53 by mmoussou ### ########.fr */
|
||||
/* Updated: 2025/04/29 15:45:19 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <server/Resource.hpp>
|
||||
|
||||
/*
|
||||
class ClientResource {
|
||||
public:
|
||||
virtual void handleResource() = 0;
|
||||
virtual ~HttpClientResource() {}
|
||||
|
||||
void addFileDescriptor(int fd);
|
||||
void getFileDescriptors();
|
||||
|
||||
private:
|
||||
struct pollfd fd;
|
||||
|
||||
}
|
||||
*/
|
||||
#include <server/AResource.hpp>
|
||||
|
||||
using namespace webserv;
|
||||
using namespace server;
|
||||
|
||||
void ClientResource::addFileDescriptor(struct pollfd fd)
|
||||
void AClientResource::addFileDescriptor(struct pollfd fd)
|
||||
{
|
||||
this->_fd = fd;
|
||||
}
|
||||
|
||||
|
||||
struct pollfd ClientResource::getFileDescriptor()
|
||||
struct pollfd AClientResource::getFileDescriptor()
|
||||
{
|
||||
return (this->_fd);
|
||||
}
|
@ -6,7 +6,7 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/17 11:12:41 by mmoussou #+# #+# */
|
||||
/* Updated: 2025/04/29 14:24:31 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/04/29 17:27:49 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -20,7 +20,7 @@ using namespace webserv::server;
|
||||
Client::Client(struct pollfd *pfd, sockaddr_in socket, config::Config *conf)
|
||||
: _pfd(pfd), _client_addr(socket), _Gconf(conf) {
|
||||
_request = not_nullptr;
|
||||
//log("➕", "Client", "constructor called");
|
||||
log("➕", "Client", "constructor called");
|
||||
}
|
||||
|
||||
Client::Client(const Client &cpy) {
|
||||
@ -34,7 +34,6 @@ void Client::parse(void) {
|
||||
char buffer[BUFFER_SIZE];
|
||||
ssize_t bytes_received;
|
||||
|
||||
|
||||
do {
|
||||
std::memset(buffer, 0, BUFFER_SIZE);
|
||||
bytes_received = recv(_pfd->fd, buffer, BUFFER_SIZE - 1, 0);
|
||||
@ -102,6 +101,6 @@ void Client::answer(void) {
|
||||
}
|
||||
|
||||
Client::~Client(void) {
|
||||
//log("➖", "Client", "destructor called");
|
||||
log("➖", "Client", "destructor called");
|
||||
delete (http::Get *)(this->_request);
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/11 16:11:40 by adjoly #+# #+# */
|
||||
/* Updated: 2025/04/29 14:27:42 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/04/29 17:28:17 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -102,7 +102,7 @@ void Server::_run(void) {
|
||||
fd.fd = *it;
|
||||
fd.events = POLLIN;
|
||||
_client_fds.push_back(fd);
|
||||
//_log->debug("new socket in poll");
|
||||
_log->debug("new socket in poll");
|
||||
}
|
||||
|
||||
// to add signal instead of 727
|
||||
@ -172,10 +172,9 @@ void Server::_run(void) {
|
||||
_log->error("client does not exist");
|
||||
continue;
|
||||
}
|
||||
if (client->requestParsed() == false) {
|
||||
continue;
|
||||
if (client->requestParsed() == true) {
|
||||
client->answer();
|
||||
}
|
||||
client->answer();
|
||||
_client_data.erase(std::find(_client_data.begin(),
|
||||
_client_data.end(), client));
|
||||
delete client;
|
||||
@ -194,7 +193,7 @@ void Server::_run(void) {
|
||||
}
|
||||
|
||||
Server::Server(config::Config *conf) : _conf(conf) {
|
||||
// log("➕", "Server::Server", "config constructor called");
|
||||
log("➕", "Server::Server", "config constructor called");
|
||||
_log = conf->getLogger();
|
||||
try {
|
||||
_setup();
|
||||
@ -205,7 +204,7 @@ Server::Server(config::Config *conf) : _conf(conf) {
|
||||
}
|
||||
|
||||
Server::~Server(void) {
|
||||
// log("➖", "Server::Server", "destructor called");
|
||||
log("➖", "Server::Server", "destructor called");
|
||||
for (std::vector<struct pollfd>::iterator it = _client_fds.begin();
|
||||
it != _client_fds.end(); it++)
|
||||
close(it->fd);
|
||||
|
Reference in New Issue
Block a user