Skip to content

Commit

Permalink
Merge pull request #49 from saladuit/mergeFix
Browse files Browse the repository at this point in the history
Merge fix
  • Loading branch information
lucienvb authored Jul 4, 2024
2 parents 4265b35 + ff422bc commit 9247852
Show file tree
Hide file tree
Showing 16 changed files with 197 additions and 118 deletions.
52 changes: 52 additions & 0 deletions config/2_serv.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Server Configuration
#
server {
listen f1r3s10.codam.nl:8080;
server_name f1r3s10;
error_dir /data/server1/errors/;
client_max_body_size 3M;

location / {
alias /data/server1/www/;
index landing_page.html;
allowed_methods GET;
autoindex on;
}
}


server {
listen f1r3s10.codam.nl:8080;
server_name f1r3s10.codam.nl;
error_dir /data/server1/errors/;
client_max_body_size 3M;

location / {
alias /data/server1/www/;
index landing_page.html;
allowed_methods GET;
autoindex on;
}

location /python/ {
allowed_methods GET POST;
alias /data/server1/python/;
cgi on;
}

location /images/ {
alias /data/server1/images/;
allowed_methods GET POST;
autoindex on;
}

location /upload/ {
alias /data/server1/upload/;
allowed_methods POST DELETE;
}

location /removed_folder/ {
allowed_methods GET;
return /images/coffee-resized.jpeg;
}
}
41 changes: 8 additions & 33 deletions config/default.conf
Original file line number Diff line number Diff line change
@@ -1,52 +1,27 @@
# Server Configuration
#
server {
listen f1r3s10.codam.nl:8080;
server_name f1r3s10;
error_dir /data/server1/errors/;
client_max_body_size 3M;

location / {
alias /data/server1/www/;
index landing_page.html;
allowed_methods GET;
autoindex on;
}
}


server {
listen f1r3s10.codam.nl:8080;
server_name f1r3s10.codam.nl;
listen localhost:8080;
server_name localhost;
root /data/server1;
error_dir /data/server1/errors/;
client_max_body_size 3M;

location / {
alias /data/server1/www/;
index landing_page.html;
alias /www/;
index landing_no_img.html;
allowed_methods GET;
autoindex on;
}

location /python/ {
allowed_methods GET POST;
alias /data/server1/python/;
cgi on;
}

location /images/ {
alias /data/server1/images/;
allowed_methods GET POST;
allowed_methods GET;
autoindex on;
}

location /upload/ {
alias /data/server1/upload/;
alias /upload/;
allowed_methods POST DELETE;
}

location /removed_folder/ {
allowed_methods GET;
return /images/coffee-resized.jpeg;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ <h1 align="center">This is just to show you what we have here</h1>
<!-- IMAGES -->
<p >with even an image:</p>
<img width="500" src="/images/Factorio_Wallpaper.jpg" alt="Factorio_Wallpaper via HTTP-request" />
<!--
--!>
<!-- text/plain -->
</div>

Expand Down
10 changes: 10 additions & 0 deletions data/server1/www/landing_no_img.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<title>This is Server 1</title>
</head>
<body>
<h1 align="center">This is just to show you what we have here</h1>
<p align="center">and we have some plain text underneath <br> with more text under it.</p>
</body>
</html>
5 changes: 2 additions & 3 deletions include/Client.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef CLIENT_HPP
#define CLIENT_HPP

#include "AutoIndexGenerator.hpp"
#include "CGI.hpp"
#include "FileManager.hpp"
#include "HTTPRequest.hpp"
Expand Down Expand Up @@ -50,8 +49,8 @@ class Client
ClientState _state;
int _serverToCgiFd[2];
int _cgiToServerFd[2];

void checkCGI(const std::string &request_target, HTTPMethod method);
};

const std::string MethodToString(HTTPMethod num);

#endif
2 changes: 1 addition & 1 deletion include/FileManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class FileManager
bool _autoindex;
size_t _bytes_sent;

std::string applyLocationSettings(const std::string &request_target);
std::string resolveRequestTarget(const std::string &request_target);

public:
FileManager();
Expand Down
6 changes: 3 additions & 3 deletions include/HTTPRequest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <string>
#include <unordered_map>

#define BUFFER_SIZE 64
#define BUFFER_SIZE 256

// DEFINES

Expand Down Expand Up @@ -59,8 +59,8 @@ class HTTPRequest
bool getHeaderEnd() const;

const std::string &getExecutable(void) const;
void setCGIToTrue(void);
const bool &CGITrue(void) const;
void setCGI(bool b);
const bool &getCGI(void) const;
const size_t &getBodyLength(void) const;

private:
Expand Down
5 changes: 5 additions & 0 deletions include/ServerSettings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class ServerSettings

const std::string &getListen() const;
const std::string &getServerName() const;
const std::string &getRoot() const;
const std::string &getErrorDir() const;
const std::string &getClientMaxBodySize() const;

Expand All @@ -30,6 +31,7 @@ class ServerSettings
private:
std::string _listen;
std::string _server_name;
std::string _root;
std::string _error_dir;
std::string _client_max_body_size;
std::vector<LocationSettings> _location_settings;
Expand All @@ -41,8 +43,11 @@ class ServerSettings
std::vector<Token>::iterator &value);
void parseListen(const Token value);
void parseServerName(const Token value);
void parseRoot(const Token value);
void parseErrorDir(const Token value);
void parseClientMaxBodySize(const Token value);

void validateBlock();
};

#endif
7 changes: 4 additions & 3 deletions src/AutoIndexGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@ std::string AutoIndexGenerator::AutoIndexGenerator(const std::string dir,
<table style = \"width:80%;font-size:15px\">\n\
<tbody>\n\
<tr>\n\
\t<th style = \"text-align:left\"> File Name</th>\n\
\t<th style = \"text-align:left\"> File Size</th>\n\
\t<th style = \"text-align:left\"> Last Modification</th>\n\
\t<th style = \"text-align:left\"> <u>File Name</u></th>\n\
\t<th style = \"text-align:left\"> <u>File Size</u></th>\n\
\t<th style = \"text-align:left\"> <u>Last Modification</u></th>\n\
<hr>\n\
</tr>\n";

getTableLines(response, dir);
Expand Down
40 changes: 13 additions & 27 deletions src/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,26 +106,6 @@ HTTPResponse &Client::getResponse()
return (_response);
}

void Client::checkCGI(const std::string &request_target, HTTPMethod method)
{
const LocationSettings &loc =
_serversetting.resolveLocation(request_target);

if (loc.resolveMethod(method) == false)
throw ClientException(StatusCode::MethodNotAllowed);
if (loc.getCGI())
_request.setCGIToTrue();
}

// TODO:
// [x] ServerName isn't working propperly, requires testing
// [x] ResolveAlias needs a rework should copy and replace.
// [x] Contentlenght Isnt working, requires testing
// [x] Post Doesn't write the body of the request to the file
// [x] Delete doens't work, maybe resolve Location
// [ ] CGI Hangs when the child process gets stuck.
// [x] I'm still sometimes getting FATAL ERROR getClientPipe

ClientState Client::handleConnection(
short events, Poll &poll, Client &client,
std::unordered_map<int, std::shared_ptr<int>> &active_pipes)
Expand All @@ -142,11 +122,17 @@ ClientState Client::handleConnection(
if (_request.getHeaderEnd())
{
resolveServerSetting();
const LocationSettings &loc =
_serversetting.resolveLocation(_request.getRequestTarget());

if (loc.resolveMethod(_request.getMethodType()) == false)
throw ClientException(StatusCode::MethodNotAllowed);
if (loc.getCGI() == true)
_request.setCGI(true);
if (_request.getBody().size() > _request.getMaxBodySize())
throw ClientException(StatusCode::RequestBodyTooLarge);
if (_request.getBody().size() >= _request.getBodyLength())
return (ClientState::Loading);
checkCGI(_request.getRequestTarget(), _request.getMethodType());
}
return (_state);
}
Expand Down Expand Up @@ -180,7 +166,7 @@ ClientState Client::handleConnection(
else if (events & POLLOUT && _state == ClientState::Loading)
{
logger.log(DEBUG, "ClientState::Loading");
if (_request.CGITrue() == true &&
if (_request.getCGI() == true &&
_request.getMethodType() != HTTPMethod::DELETE)
{
_state = _cgi.parseURIForCGI(
Expand Down Expand Up @@ -221,18 +207,18 @@ ClientState Client::handleConnection(
catch (ClientException &e)
{
logger.log(ERROR, "Client exception: " + std::string(e.what()));
if (_request.CGITrue() == true &&
if (_request.getCGI() == true &&
_request.getMethodType() != HTTPMethod::DELETE)
_exit(1);
_response.clear();
_file_manager.setResponse(e.what());

const std::string errdir = _serversetting.getErrorDir();
if (errdir.empty())
const std::string path = _serversetting.getErrorDir();
if (path.empty())
_state = _file_manager.openErrorPage("", e.getStatusCode());
else
_state = _file_manager.openErrorPage(errdir.substr(1),
e.getStatusCode());
_state =
_file_manager.openErrorPage(path.substr(1), e.getStatusCode());
return (_state);
}
catch (ReturnException &e)
Expand Down
42 changes: 23 additions & 19 deletions src/FileManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,33 @@ FileManager::~FileManager()
{
}

std::string
FileManager::applyLocationSettings(const std::string &request_target)
std::string FileManager::resolveRequestTarget(const std::string &request_target)
{
// substr(1) is required to remove starting '/'
Logger &logger = Logger::getInstance();

const LocationSettings &loc =
_serversetting.resolveLocation(request_target);
const std::string root = _serversetting.getRoot().substr(1);

logger.log(DEBUG, "resolveRequestTarget:\t" + root + " " + request_target);
if (!loc.getRedirect().empty())
throw ReturnException(StatusCode::Found, loc);

if (request_target.find_last_of('/') == request_target.length() - 1)
{
if (!loc.getIndex().empty())
return (loc.resolveAlias(request_target).substr(1) +
loc.getIndex());
else if (loc.getAutoIndex() == false)
return (loc.resolveAlias(request_target).substr(1) + "index.html");
_request_target = AutoIndexGenerator::OpenAutoIndex(
loc.resolveAlias(request_target).substr(1), request_target);
_autoindex = true;
}

return (loc.resolveAlias(request_target).substr(1));
if (request_target.back() != '/')
return (root + loc.resolveAlias(request_target));
// is a directory
if (loc.getIndex().empty() == false)
return (root + loc.resolveAlias(request_target) + loc.getIndex());
logger.log(DEBUG, "resolveRequestTarget:\tAutoIndex:\t" +
(loc.getAutoIndex() ? std::string(" ON")
: std::string(" OFF")));
if (loc.getAutoIndex() == false)
throw ClientException(StatusCode::UnAuthorized);
const std::string AI_target = root + loc.resolveAlias(request_target);
_request_target =
AutoIndexGenerator::OpenAutoIndex(AI_target, request_target);
_autoindex = true;
return (AI_target);
}

void FileManager::openGetFile(const std::string &request_target_path)
Expand All @@ -49,7 +53,7 @@ void FileManager::openGetFile(const std::string &request_target_path)

logger.log(DEBUG, "request_target:\t" + request_target_path);
const std::string resolved_target =
applyLocationSettings(request_target_path);
resolveRequestTarget(request_target_path);
logger.log(DEBUG, "resolved_target:\t" + resolved_target);

if (_autoindex == true)
Expand All @@ -74,7 +78,7 @@ void FileManager::openPostFile(const std::string &request_target_path)

logger.log(DEBUG, "request_target:\t" + request_target_path);
const std::string resolved_target =
applyLocationSettings(request_target_path);
resolveRequestTarget(request_target_path);
logger.log(DEBUG, "resolved_target:\t" + resolved_target);

if (!std::filesystem::exists(resolved_target))
Expand Down Expand Up @@ -179,7 +183,7 @@ ClientState FileManager::manageDelete(const std::string &request_target_path)

logger.log(DEBUG, "request_target:\t" + request_target_path);
const std::string resolved_target =
applyLocationSettings(request_target_path);
resolveRequestTarget(request_target_path);
logger.log(DEBUG, "resolved_target:\t" + resolved_target);

logger.log(DEBUG, "manageDelete method is called");
Expand Down
Loading

0 comments on commit 9247852

Please sign in to comment.