Skip to content

Commit

Permalink
Merge pull request #36 from saladuit/configparser
Browse files Browse the repository at this point in the history
Configparser
  • Loading branch information
saladuit authored Nov 15, 2023
2 parents 582c930 + 44a38d3 commit 0656c86
Show file tree
Hide file tree
Showing 9 changed files with 279 additions and 252 deletions.
61 changes: 40 additions & 21 deletions config/default.conf
Original file line number Diff line number Diff line change
@@ -1,28 +1,47 @@
# https://nginx.org/en/docs/beginners_guide.html
# Global Server Configuration
#global {
# threads 4;
# read_size 1;
# write_size 1;
# default_error_pages /path/to/error_pages/;
#}
#
# Understanding The Comments:
# {nbr} : Limitied to {nbr} Option(s);
# M : Multiple Options;
# B : Binary, aka either On or Off;
#
# R : Required;
# O : Optional;

# Server Configuration

server {
port 9696;
host localhost; # Choose the port and host; default for this host:port
server_name lucy_saus; # Setup the server_names or not
client_max_body_size 3M; # Limit client body size
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]
client_max_body_size 3M; # [1 - O] Limit client body size ; [\d]{1,3}[KkMm]? ; Default: 1 mB

location / {
root /data/www; # [1 - O] Set up the root directory for the server ; [/]{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 {
root /cgi_bin/python; # already defined;
cgi_path /bin/python3; # [1 - O] Set a Path to where the CGI can find the Binary; [A-Za-z_/.]* ; Default: [EMPTY]
}

location / {
root /data/www; # Define a directory where the file should be searched
index index.html; # Set a default file to answer if the request is a directory
allow_methods GET POST; # Define a list of accepted HTTP methods for the route
directory_listing off; # Turn on or off directory listing
}
location /images {
root /data/images; # already defined;
allow_methods GET POST; # already defined;
autoindex off; # already defined;
}

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

location /removed_folder {
return localhost; # [1 - O] Reroute a directory to another URL; URL ; Default: []
}

location /images/ {
root /data; # if url /kapouet is rooted to /tmp/www
directory_listing off; # Turn on or off directory listing
}
return {URL};
}
2 changes: 2 additions & 0 deletions include/ConfigParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <LocationSettings.hpp>
#include <ServerSettings.hpp>

#include <stdexcept>
#include <string>
#include <unordered_map>
#include <vector>
Expand All @@ -15,6 +16,7 @@ class ConfigParser
std::vector<ServerSettings> _server_settings;

std::stringstream OpenFile();
void syntaxCheck(std::vector<Token>::iterator);

public:
ConfigParser(const std::string &file_path);
Expand Down
48 changes: 25 additions & 23 deletions include/LocationSettings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,6 @@
#include <unordered_map>
#include <vector>

enum class LocationSettingOption
{
Prefix,
Root,
Index,
DirectoryListing,
AllowedMethods,
CgiPass,
};

class LocationSettings
{
public:
Expand All @@ -26,23 +16,35 @@ class LocationSettings
LocationSettings(const LocationSettings &rhs);
LocationSettings &operator=(const LocationSettings &rhs) = delete;

const std::vector<std::string> &
getValues(LocationSettingOption setting) const;
void addValue(LocationSettingOption key, const std::string &value);
// Functionality:
// getters:
const std::string &getDir() const;
const std::string &getAlias() const;
const std::string &getIndex() const;
const std::string &getAllowedMethods() const;
const std::string &getReturn() const;
bool getAutoIndex() const;

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

// Printing:
void printLocationSettings() const;

std::string keyToString(LocationSettingOption Key) const;
std::string valuesToString(LocationSettingOption Key) const;

private:
std::unordered_map<LocationSettingOption, std::vector<std::string>>
_setting;
std::string _path;

LocationSettingOption identifyLocationSetting(const std::string &token);
std::string _directory;

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

void parseAlias(const Token token);
void parseIndex(const Token token);
void parseAllowedMethods(const Token token);
void parseCgiPath(const Token token);
void parseReturn(const Token token);
};
#endif // !LOCATIONSETTING_HPP
40 changes: 21 additions & 19 deletions include/ServerSettings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,10 @@
#include <LocationSettings.hpp>
#include <Token.hpp>

#include <regex>
#include <string>
#include <unordered_map>
#include <vector>

enum class ServerSettingOption
{
Port,
Host,
ServerName,
ClientMaxBodySize,
ErrorPages,
Location,
Count,
};

class ServerSettings
{
public:
Expand All @@ -29,19 +18,32 @@ class ServerSettings
ServerSettings(const ServerSettings &rhs);
ServerSettings &operator=(const ServerSettings &rhs) = delete;

const std::string &getValue(ServerSettingOption setting) const;
void setValue(ServerSettingOption key, const std::string &value);

ServerSettingOption identifyServerSetting(const std::string &token);
// TODO: void addLocationSetting(LocationSettings settings);
bool resolveLocation(const std::string &path, HTTPMethod method);
// Functionality:
const LocationSettings *resolveLocation(const std::string &request_target,
HTTPMethod input_method);
const std::string &getListen() const;
const std::string &getServerName() const;
const std::string &getErrorDir() const;
const std::string &getClientMaxBodySize() const;

// Printing:
void printServerSettings() const;

private:
std::unordered_map<ServerSettingOption, std::string> _server_setting;
std::string _listen;
std::string _server_name;
std::string _error_dir;
std::string _client_max_body_size;
std::vector<LocationSettings> _location_settings;

// Parsing:
void addValueToServerSettings(const Token &key,
std::vector<Token>::iterator &value);
void parseListen(const Token value);
void parseServerName(const Token value);
void parseErrorDir(const Token value);
void parseClientMaxBodySize(const Token value);

// TODO: methods fucntion that can resolve if a read/write/delete can be
// done on a certain location in the LocationSettings
};
Expand Down
2 changes: 1 addition & 1 deletion src/ConfigParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <filesystem>
#include <ios>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>

Expand Down Expand Up @@ -48,6 +47,7 @@ void ConfigParser::ParseConfig()
std::vector<Token> tokenlist;

tokenizeStream(OpenFile(), tokenlist);
syntaxCheck(tokenlist.begin());

for (std::vector<Token>::iterator it = tokenlist.begin();
it != tokenlist.end(); it++)
Expand Down
9 changes: 9 additions & 0 deletions src/ConfigSyntax.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

#include <ConfigParser.hpp>
#include <Logger.hpp>
#include <Token.hpp>

void ConfigParser::syntaxCheck(std::vector<Token>::iterator token)
{
(void)token;
}
Loading

0 comments on commit 0656c86

Please sign in to comment.