」 feat(Requests): now use the config :D

This commit is contained in:
y-syo
2025-05-02 15:04:41 +02:00
parent 2352a865bf
commit bfef8ed76e
7 changed files with 105 additions and 64 deletions

View File

@ -12,13 +12,14 @@ port = 8080
[server.location./]
methods = { "GET" }
root = "/var/www/html"
root = "/sgoinfre/goinfre/Perso/mmoussou"
dirlist = true
client_max_body_size = "10M"
index = "banger.html"
[server.location./api]
methods = { "GET", "POST" }
root = "/var/www/api"
root = "/nfs/homes/mmoussou"
upload_path = "/etc/webserv/up"
cgi.go = "/bin/go"

View File

@ -6,13 +6,14 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/03 17:23:00 by mmoussou #+# #+# */
/* Updated: 2025/04/30 15:19:52 by mmoussou ### ########.fr */
/* Updated: 2025/05/02 13:58:52 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include "config/URL.hpp"
#include <config/URL.hpp>
#include <config/Route.hpp>
#include <ctime>
#include <fstream>
#include <iostream>
@ -42,19 +43,20 @@ class ARequest : public http::IMessage {
std::string getMethod(void) const;
std::string getTarget(void) const;
std::string getProtocol(void) const;
config::Server *getConfig(void) const;
webserv::config::Route *getRoute(void) const;
URL getUrl() 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);
void setRoute(config::Route *route);
protected:
std::string _method;
std::string _target;
std::string _protocol;
config::Server *_conf;
webserv::config::Route *_route;
URL *_url;
std::string _sanitizeStr(std::string &str) {

View File

@ -6,20 +6,21 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/03 16:07:01 by mmoussou #+# #+# */
/* Updated: 2025/04/30 15:21:54 by mmoussou ### ########.fr */
/* Updated: 2025/05/02 13:58:35 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include <algorithm>
#include <dirent.h>
#include <sys/stat.h>
#include <vector>
#include <dirent.h>
#include <algorithm>
#include <sys/stat.h>
#include <config/URL.hpp>
#include <log.hpp>
#include <config/URL.hpp>
#include <requests/default.hpp>
using namespace webserv::http;
using namespace webserv;
using namespace http;
std::string ARequest::str(void) const {
std::ostringstream response;
@ -62,3 +63,13 @@ URL ARequest::getUrl() const
else
return URL("");
}
config::Route *ARequest::getRoute(void) const
{
return (_route);
}
void ARequest::setRoute(config::Route *route)
{
this->_route = route;
}

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/30 09:42:18 by adjoly #+# #+# */
/* Updated: 2025/04/30 15:17:15 by mmoussou ### ########.fr */
/* Updated: 2025/05/02 14:53:08 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
@ -68,6 +68,8 @@ void Delete::parse(std::string const &data) {
Response Delete::execute(void) {
http::Response response;
this->_target = this->_route->getRootDir() + this->_target;
try {
if (std::remove(this->_target.c_str()))
throw std::runtime_error("can't remove file, FF");

View File

@ -6,14 +6,15 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/30 09:40:16 by adjoly #+# #+# */
/* Updated: 2025/04/30 15:30:08 by mmoussou ### ########.fr */
/* Updated: 2025/05/02 14:52:24 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include <algorithm>
#include <dirent.h>
#include <sys/stat.h>
#include <vector>
#include <dirent.h>
#include <unistd.h>
#include <algorithm>
#include <sys/stat.h>
#include <requests/default.hpp>
@ -87,8 +88,30 @@ char isDirectory(const std::string &path) {
Response Get::execute(void) {
http::Response response;
this->_target = this->_route->getRootDir() + this->_target;
try {
if (isDirectory(this->_target)) {
if (isDirectory(this->_target))
{
if (!access((this->_target + this->_route->getIndex()).c_str(), R_OK))
{
this->_target = this->_target + this->_route->getIndex();
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;
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));
}
else if (this->_route->getDirList())
{
DIR *dir;
struct dirent *entry;
struct stat file_stat;
@ -108,6 +131,7 @@ Response Get::execute(void) {
files.push_back(file_name);
}
}
closedir(dir);
std::sort(files.begin(), files.end());
@ -148,8 +172,12 @@ body {\n\
response.addHeader("Content-Length", length.str());
response.addHeader("Content-Type", "text/html");
response.setBody(body);
} else {
}
else
throw std::runtime_error("dir but no dirlist");
}
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)),

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/30 09:50:20 by adjoly #+# #+# */
/* Updated: 2025/04/30 15:18:01 by mmoussou ### ########.fr */
/* Updated: 2025/05/02 15:02:56 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
@ -70,7 +70,7 @@ void Post::parse(std::string const &data) {
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);
return this->_route->getUpRoot() + header.substr(start, end - start);
}
void handleMultipartData(const std::string &body, const std::string &boundary) {

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 11:12:41 by mmoussou #+# #+# */
/* Updated: 2025/05/02 13:33:07 by mmoussou ### ########.fr */
/* Updated: 2025/05/02 14:26:32 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
@ -49,12 +49,8 @@ void Client::parse(void) {
_getRequest(received_data);
_route = _conf->whatRoute(URL(this->_request->getTarget()));
if (_route == not_nullptr) {
_log->info("euuh");
return;
}
/* std::cout << "_route is " << (_route ? "not null" : "NULL") << std::endl;
*/
this->_request->setRoute(_route);
if (!this->_route || this->_route == not_nullptr) {
this->_request->setMethod("404");
return;
@ -67,6 +63,7 @@ void Client::parse(void) {
if (received_data.length() > (unsigned long)(_route->getMaxBody()))
this->_request->setMethod("413");
}
bool Client::requestParsed(void) {