Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configimplementation #42

Merged
merged 35 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
ab82a1a
added TODO
Tentanus Dec 6, 2023
6db314b
added TODO; minor refactor
Tentanus Dec 8, 2023
c865cce
minor change
Tentanus Dec 10, 2023
3226060
Merge branch 'main' of github.com:saladuit/BuzingaServ into configsyntax
Tentanus Dec 13, 2023
acf7559
working in Listen, reworked resolveLocation
Tentanus Dec 15, 2023
aed2e3c
restructured data
Tentanus Dec 17, 2023
e4e2d0b
webserver runs again. Gives 500, due to it trying to open a directory
Tentanus Dec 17, 2023
89d4312
cleanup
Tentanus Dec 17, 2023
377d472
MaxC:wlientBodySize is implemented and working.
Tentanus Dec 23, 2023
7f9929e
Errordirectory is implemented. resolveAlias now works propperly. stil…
Tentanus Dec 26, 2023
c695bcf
added resolveAlias and ResolveMethod
Tentanus Dec 27, 2023
a7c3fb4
minor changes
Tentanus Dec 27, 2023
1251bb3
added comment
Tentanus Dec 27, 2023
f0605e4
webserver returns files, some formatting
Tentanus Dec 29, 2023
56f2bec
Logger segfaults when moving image, images still don't transport prop…
Tentanus Dec 30, 2023
09dc0d3
filemanager now is capable of sending binary data
Tentanus Jan 4, 2024
86e947f
Filemanager contains the Serversetting instead of HTTPRequest; Found …
Tentanus Jan 4, 2024
6ab6505
adding start AutoIndexGenerator;
Tentanus Jan 5, 2024
beb4374
changing includes
Tentanus Jan 5, 2024
48e39e2
just added a parameter, that's it
Tentanus Jan 7, 2024
13c8999
AutoIndexGenerator almost finished
Tentanus Jan 10, 2024
b7a5280
AutoIndexGenerator working
Tentanus Jan 11, 2024
51a5c0c
formatting on AutoIndexGenerator
Tentanus Jan 12, 2024
1c8e074
implemented and fixed error_dir
Tentanus Jan 12, 2024
01a3629
added files for autoindexgenerator testing
Tentanus Feb 19, 2024
1e88041
ReturnException: If Location contains a Return, Filemanager will thro…
Tentanus Feb 19, 2024
3088811
redirection is working
Tentanus Feb 28, 2024
19ccaec
changed Statuscode 301 -> 302
Tentanus Mar 28, 2024
4509959
requested changes for pull request #42
Tentanus Apr 1, 2024
e491e83
start of Listen implementation
Tentanus Apr 18, 2024
637755d
converting HOST in config file & sorting ServerSetting blocks
Tentanus Apr 25, 2024
d7c4fc3
changed return of sortServersettings to vector, also changed Server c…
Tentanus Apr 25, 2024
251476c
Vectored serversettings has been implemented
Tentanus Apr 26, 2024
7e03a0b
Bugfix: was looking for ; (not :) in Host; BUG: Can't load images not…
Tentanus Apr 26, 2024
9c6e59e
Bugfix: Images can be loaded but I'm not sure how it was fixed...
Tentanus Apr 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 44 additions & 12 deletions config/default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,66 @@

server {
listen localhost:8080; # [M - O] choose a Host:Port combination ; IPv4:{1 - 16bit} ; default: INADDR_ANY:8080
server_name localhost; # [M - O] Setup the server_names ; [A-Za-z_]* ; default: [EMPTY]
error_dir /error; # [1 - O] Set a directory that has error page ; [/]{1}[A-Za-z_/:w]* ; Default: [EMPTY]
server_name localhost; # [M - O] retup the server_names ; [A-Za-z_]* ; default: [EMPTY]
error_dir /data/errors/; # [1 - O] Set a directory that has error page ; [/]{1}[A-Za-z_/:w]* ; Default: [EMPTY]
client_max_body_size 3M; # [1 - O] Limit client body size ; [\d]{1,3}[KkMm]? ; Default: 1 mB

location / { # [1 - O] Request Target is the Target of the HTTP request; starts with a /
alias /data/www; # [1 - O] links the stated Direcotry as the RequestTarget Location ; [/]{1}[A-Za-z_/:w]* ; Default: /data/www
index index.html; # [1 - O] Set a default file to answer if the request is a directory ; [A-Za-z_/.]* ; Default: index.html
alias /data/www/; # [1 - O] links the stated Direcotry as the RequestTarget Location ; [/]{1}[A-Za-z_/:w]* ; Default: /data/www
#index index.html; # [1 - O] Set a default file to answer if the request is a directory ; [A-Za-z_/.]* ; Default: index.html
allowed_methods GET; # [M - O] Define a list of accepted HTTP methods for the route; GET/POST/DELETE ; Default: GET
autoindex on; # [B - O] Turn on or off directory listing; on; Default: off
}

location /python/ {
alias /cgi/python; # already defined;
cgi .py /bin/python3; # [1 - O] Set a Path to where the CGI can find the Binary; [A-Za-z_/.]* ; Default: [EMPTY]
alias /cgi/python/; # already defined;
cgi /bin/python3; # [1 - O] Set a Path to where the CGI can find the Binary; [A-Za-z_/.]* ; Default: [EMPTY]
}

location /images/ {
root /data/images; # already defined;
allow_methods GET POST; # already defined;
autoindex off; # already defined;
alias /data/images/;
allowed_methods GET POST;
autoindex off;
}

location /png/ {
alias /data/images/png/;
allowed_methods GET POST;
autoindex off;
}

location /images/jpg/ {
alias /data/upload/images/jpg/;
allowed_methods GET POST;
autoindex off;
}

location /upload/ {
root /data/upload;
allowed_moteds POST DELETE;
alias /data/upload/;
allowed_methods POST DELETE;
}

location /removed_folder/ {
return localhost; # [1 - O] Reroute a directory to another URL; URL ; Default: []
alias /data/www/;
allowed_methods GET;
return /images/coffee-resized.jpeg ; # [1 - O] Reroute a directory to another URL; URL ; Default: []
}
}

#TODO: all Settings still need implementation into the Webserver
# [ ] SetDefaultValues for configfile.
# [ ] ServerBlock:
# [ ] listen; //
# [ ] server_name; // should go over the Clients/HTTPServers after
# // the request is recieved and see which serverSettings block is relevant to the request;
# [x] error_dir; // should search this direcotry to find the relevant error file [e.g. 404.html];
# [x] client_max_body_size; // limits the size of the client Request, returning 413 when it exceeds this value;
# [ ] Location Block:
# [x] alias; // should do a fitted preppend on the path in URI;
# [x] index; // file return of the request just specifies this directory ;
# [x] autoindex; // Automaticly should create a Index file that shows an overview of the direcotry;
# [x] allowed_methods; // should check if the HTTP Method is allowed in this location;
# [ ] cgi; // is a boolean that shows if the directory is a CGI and the value is the path to the executable;
# [x] return; //
# [x] HTTPerrorpagegenerator // method that can generate a simple ERROR page to the _response.
#
10 changes: 10 additions & 0 deletions config/test.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Server Configuration

server {
location / {
alias /data/www;
}
}



5 changes: 4 additions & 1 deletion data/errors/404.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
</head>
<body>
<h1>404 Not Found</h1>
<p>The requested page could not be found.</p>
<hr>
<p>The requested page could not be found.
<br>
<br>This was loaded using our error_dir</p>
</body>
</html>
12 changes: 12 additions & 0 deletions data/errors/500.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<title>500 Not Found</title>
</head>
<body>
<h1>500 Not Found</h1>
<p>The server has encountered an internal error.
<br>
<br>File: /data/error/500.html</p>
</body>
</html>
Binary file added data/images/coffee-resized.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
File renamed without changes.
29 changes: 29 additions & 0 deletions data/www/autoindexresult.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<html>
<head>
<title> Index of /</title>
</head>
<body>
<h1> Index of /</h1>
<table style = "width:80%;font-size:15px">
<tbody>
<tr>
<th style = "text-align:left"> File Name</th>
<th style = "text-align:left"> File Size</th>
<th style = "text-align:left"> Last Modification</th>
</tr>
<tr>
<th style ="text-align:left" >index.html</th>
<th style ="text-align:left" >3899</th>
<th style ="text-align:left" >03 01 24</th>
</tr> <tr>
<th style ="text-align:left" >test.file</th>
<th style ="text-align:left" >0</th>
<th style ="text-align:left" >10 01 24</th>
</tr> <tr>
<th style ="text-align:left" >test.txt</th>
<th style ="text-align:left" >0</th>
<th style ="text-align:left" >10 01 24</th>
</tr> </tbody>
</table>
</body>
5 changes: 4 additions & 1 deletion data/www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ <h1 align="center">Do not go gentle into that good night</h1>
<!-- lvanbus: supersu -->
</P>
<!-- IMAGES -->

<!-- <img width="200" src="localhost:8080/images/nose_monkey.webp" alt="nose_monkey.webp test via HTTP-request" />
<br>
<img width="200" src="../images/nose_monkey.webp" alt="nose_monkey.webp test via directory" />
--!>

<!-- text/plain -->
<P ALIGN="LEFT">hallo</P>
Expand Down
Empty file added data/www/test.file
Empty file.
2 changes: 0 additions & 2 deletions in.txt

This file was deleted.

17 changes: 17 additions & 0 deletions include/AutoIndexGenerator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef AUTOINDEXGENERATOR_HPP
#define AUTOINDEXGENERATOR_HPP

#include "Logger.hpp"

#include <dirent.h>
#include <fstream>
#include <string>

namespace AutoIndexGenerator
{
std::fstream OpenAutoIndex(std::string directory, const std::string uri);
std::string AutoIndexGenerator(const std::string dir, const std::string uri);

} // namespace AutoIndexGenerator

#endif // !AUTOINDEXGENERATOR_HPP
14 changes: 8 additions & 6 deletions include/Client.hpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
#ifndef CLIENT_HPP
#define CLIENT_HPP

#include <CGI.hpp>
#include <FileManager.hpp>
#include <HTTPRequest.hpp>
#include <HTTPResponse.hpp>
#include <Socket.hpp>
#include "CGI.hpp"
#include "FileManager.hpp"
#include "HTTPRequest.hpp"
#include "HTTPResponse.hpp"
#include "ServerSettings.hpp"
#include "Socket.hpp"

class Client
{
public:
Client(const int &server_fd);
Client(const int &server_fd, const ServerSettings &serversettings);
Client() = delete;
Client(const Client &other) = delete;
const Client &operator=(const Client &other) = delete;
Expand All @@ -25,6 +26,7 @@ class Client
FileManager _file_manager;
CGI _cgi;
Socket _socket;
ServerSettings _serversetting; // TODO: make multiple serverblocks work;
Tentanus marked this conversation as resolved.
Show resolved Hide resolved
ClientState _state;
};

Expand Down
16 changes: 13 additions & 3 deletions include/FileManager.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#ifndef FILE_MANAGER_HPP
#define FILE_MANAGER_HPP

#include <HTTPRequest.hpp>
#include <HTTPStatus.hpp>
#include "HTTPRequest.hpp"
#include "HTTPStatus.hpp"
#include "LocationSettings.hpp"
#include "ServerSettings.hpp"

#include <fstream>
#include <string>

Expand All @@ -11,9 +14,15 @@ class FileManager
private:
std::string _response;
std::fstream _request_target;
const ServerSettings &_serversetting;
bool _autoindex;

std::string applyLocationSettings(const std::string &request_target,
HTTPMethod method);

public:
FileManager();
FileManager(const ServerSettings &ServerSettings);
FileManager(const FileManager &other) = delete;
void operator=(const FileManager &other) = delete;
~FileManager();
Expand All @@ -22,7 +31,6 @@ class FileManager
void openPostFile(const std::string &request_target_path);
ClientState openErrorPage(const std::string &error_pages_path,
const StatusCode &status_code);
ClientState setErrorResponse(const StatusCode &status_code);
ClientState loadErrorPage(void);
ClientState manage(HTTPMethod method, const std::string &filename,
const std::string &body);
Expand All @@ -31,6 +39,8 @@ class FileManager
ClientState manageDelete(const std::string &reqest_target_path);

const std::string &getResponse(void) const;
void addToResponse(const std::string str);
void setResponse(const std::string str);
};

#endif
11 changes: 10 additions & 1 deletion include/HTTPRequest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define HTTP_REQUEST_HPP

#include <ClientState.hpp>
#include <ServerSettings.hpp>

#include <string>
#include <unordered_map>
Expand All @@ -15,21 +16,24 @@
#endif

// ENUM
#ifndef HTTP_METHOD_ENUM
lucienvb marked this conversation as resolved.
Show resolved Hide resolved
#define HTTP_METHOD_ENUM
enum class HTTPMethod
{
GET,
POST,
DELETE,
UNKNOWN,
};
#endif

// CLASS
class HTTPRequest
{
public:
HTTPRequest();
HTTPRequest(const HTTPRequest &rhs) = delete;
HTTPRequest &operator=(const HTTPRequest &other) = delete;
HTTPRequest &operator=(const HTTPRequest &rhs) = delete;
~HTTPRequest();

void setMethodType(const std::string &method_type);
Expand All @@ -38,6 +42,9 @@ class HTTPRequest
void setRequestTarget(const std::string &request_target);
const std::string &getRequestTarget(void) const;

void setMaxBodySize(std::string inp);
ssize_t getMaxBodySize(void) const;

void setHTTPVersion(const std::string &http_version);
const std::string &getHTTPVersion(void) const;

Expand All @@ -50,6 +57,7 @@ class HTTPRequest
private:
ssize_t _bytes_read;
size_t _content_length;
size_t _max_body_size;
HTTPMethod _methodType;
std::string _http_request;
std::string _request_target;
Expand All @@ -59,6 +67,7 @@ class HTTPRequest

size_t parseStartLine(size_t &i);
size_t parseHeaders(size_t &i);
void setLocationdependancies(std::string request_target);
};

#endif
2 changes: 1 addition & 1 deletion include/HTTPServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class HTTPServer

void setupServers(void);
void handleActivePollFDs();
void handleNewConnection(int fd);
void handleNewConnection(int fd, const ServerSettings &ServerBlock);
void handleExistingConnection(const pollfd &poll_fd);
};

Expand Down
1 change: 1 addition & 0 deletions include/HTTPStatus.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class HTTPStatus
~HTTPStatus();

std::string getStatusLine(const std::string &version) const;
std::string getStatusLineCRLF(const std::string &version) const;
std::string getHTMLStatus(void) const;
StatusCode getStatusCode() const;
};
Expand Down
29 changes: 22 additions & 7 deletions include/LocationSettings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,53 @@

#include <string>

// ENUM
#ifndef HTTP_METHOD_ENUM
Tentanus marked this conversation as resolved.
Show resolved Hide resolved
#define HTTP_METHOD_ENUM
enum class HTTPMethod
{
GET,
POST,
DELETE,
UNKNOWN,
};
#endif

class LocationSettings
{
public:
LocationSettings();
~LocationSettings();
LocationSettings(std::vector<Token>::iterator &token);
LocationSettings(const LocationSettings &rhs);
lucienvb marked this conversation as resolved.
Show resolved Hide resolved
LocationSettings &operator=(const LocationSettings &rhs) = delete;
LocationSettings &operator=(LocationSettings &rhs);

// Functionality:
// getters:
const std::string &getRequestTarget() const;

const std::string &getPath() const;
const std::string &getAlias() const;
const std::string &getIndex() const;
const std::string &getAllowedMethods() const;
const std::string &getReturn() const;
const std::string &getRedirect() const;
bool getAutoIndex() const;

// setters:
void setDir(const std::string &path);
// resolves:

const std::string resolveAlias(const std::string request_target) const;
bool resolveMethod(const HTTPMethod method) const;

// Printing:
void printLocationSettings() const;

private:
std::string _requesttarget;
saladuit marked this conversation as resolved.
Show resolved Hide resolved
std::string _path;

std::string _alias;
std::string _index;
std::string _allowed_methods;
std::string _cgi_path;
std::string _return;
std::string _redirect;
bool _auto_index;

void parseAlias(const Token token);
Expand Down
Loading
Loading