mirror of
https://github.com/KeyZox71/webserv.git
synced 2025-05-10 16:08:45 +02:00
「✨」 feat(Requests): now use the config :D
This commit is contained in:
@ -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"
|
||||
|
||||
|
@ -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) {
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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");
|
||||
|
@ -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,34 +88,57 @@ 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)) {
|
||||
DIR *dir;
|
||||
struct dirent *entry;
|
||||
struct stat file_stat;
|
||||
std::vector<std::string> files;
|
||||
if (isDirectory(this->_target))
|
||||
{
|
||||
if (!access((this->_target + this->_route->getIndex()).c_str(), R_OK))
|
||||
{
|
||||
this->_target = this->_target + this->_route->getIndex();
|
||||
|
||||
if ((dir = opendir(this->_target.c_str())) == NULL)
|
||||
throw;
|
||||
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 (S_ISDIR(file_stat.st_mode))
|
||||
files.push_back(file_name + "/");
|
||||
else
|
||||
files.push_back(file_name);
|
||||
}
|
||||
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));
|
||||
}
|
||||
closedir(dir);
|
||||
else if (this->_route->getDirList())
|
||||
{
|
||||
DIR *dir;
|
||||
struct dirent *entry;
|
||||
struct stat file_stat;
|
||||
std::vector<std::string> files;
|
||||
|
||||
std::sort(files.begin(), files.end());
|
||||
if ((dir = opendir(this->_target.c_str())) == NULL)
|
||||
throw;
|
||||
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 (S_ISDIR(file_stat.st_mode))
|
||||
files.push_back(file_name + "/");
|
||||
else
|
||||
files.push_back(file_name);
|
||||
}
|
||||
}
|
||||
|
||||
std::string body = "<html>";
|
||||
closedir(dir);
|
||||
|
||||
body += "<head><style>\n\
|
||||
std::sort(files.begin(), files.end());
|
||||
|
||||
std::string body = "<html>";
|
||||
|
||||
body += "<head><style>\n\
|
||||
:root {\n\
|
||||
background-color: -moz-dialog;\n\
|
||||
color: -moz-dialogtext;\n\
|
||||
@ -135,21 +159,25 @@ body {\n\
|
||||
}\n\
|
||||
</style></head>";
|
||||
|
||||
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 += "</ul></body></html>";
|
||||
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 += "</ul></body></html>";
|
||||
|
||||
response.setProtocol(this->_protocol);
|
||||
response.setStatusCode(200);
|
||||
std::stringstream length;
|
||||
length << body.length();
|
||||
response.addHeader("Content-Length", length.str());
|
||||
response.addHeader("Content-Type", "text/html");
|
||||
response.setBody(body);
|
||||
|
||||
} else {
|
||||
response.setProtocol(this->_protocol);
|
||||
response.setStatusCode(200);
|
||||
std::stringstream length;
|
||||
length << body.length();
|
||||
response.addHeader("Content-Length", length.str());
|
||||
response.addHeader("Content-Type", "text/html");
|
||||
response.setBody(body);
|
||||
}
|
||||
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)),
|
||||
@ -161,7 +189,7 @@ body {\n\
|
||||
response.setProtocol(this->_protocol);
|
||||
response.setStatusCode(200);
|
||||
response.addHeader("Content-Type",
|
||||
http::Mime::getType(this->_target));
|
||||
http::Mime::getType(this->_target));
|
||||
|
||||
#ifdef VERBOSE
|
||||
//_log->debug(response.str().c_str());
|
||||
|
@ -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) {
|
||||
@ -104,7 +104,7 @@ void handleMultipartData(const std::string &body, const std::string &boundary) {
|
||||
|
||||
Response Post::execute(void) {
|
||||
http::Response response;
|
||||
|
||||
|
||||
try {
|
||||
handleMultipartData(
|
||||
this->_body,
|
||||
|
@ -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) {
|
||||
|
Reference in New Issue
Block a user