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

Resolve Location #35

Merged
merged 6 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 3 additions & 3 deletions include/LocationSettings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ enum class LocationSettingOption
Root,
Index,
DirectoryListing,
AllowMethods,
AllowedMethods,
CgiPass,
Count,
};

class LocationSettings
Expand All @@ -32,9 +31,10 @@ class LocationSettings
void addValue(LocationSettingOption key, const std::string &value);

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

void printLocationSettings() const;

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

Expand Down
2 changes: 2 additions & 0 deletions include/ServerSettings.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef SERVERSETTING_HPP
#define SERVERSETTING_HPP

#include <HTTPRequest.hpp>
#include <LocationSettings.hpp>
#include <Token.hpp>

Expand Down Expand Up @@ -33,6 +34,7 @@ class ServerSettings

ServerSettingOption identifyServerSetting(const std::string &token);
// TODO: void addLocationSetting(LocationSettings settings);
bool resolveLocation(const std::string &path, HTTPMethod method);

void printServerSettings() const;

Expand Down
21 changes: 16 additions & 5 deletions src/LocationSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ LocationSettings::identifyLocationSetting(const std::string &token)
else if (token == "directory_listing")
return (LocationSettingOption::DirectoryListing);
else if (token == "allow_methods")
return (LocationSettingOption::AllowMethods);
return (LocationSettingOption::AllowedMethods);
else if (token == "cgipass")
return (LocationSettingOption::CgiPass);
else
Expand All @@ -88,6 +88,16 @@ void LocationSettings::addValue(LocationSettingOption key,
vect.emplace_back(value);
}

const std::string &LocationSettings::getPath() const
{
return (_path);
}

void LocationSettings::setPath(const std::string &path)
{
_path = path;
}

// THIS IS PRINTING FUNCTION

std::string LocationSettings::keyToString(LocationSettingOption Key) const
Expand All @@ -102,8 +112,8 @@ std::string LocationSettings::keyToString(LocationSettingOption Key) const
return ("Index");
case (LocationSettingOption::DirectoryListing):
return ("DirectoryListing");
case (LocationSettingOption::AllowMethods):
return ("AllowMethods");
case (LocationSettingOption::AllowedMethods):
return ("AllowedMethods");
case (LocationSettingOption::CgiPass):
return ("CgiPass");
default:
Expand All @@ -123,10 +133,11 @@ std::string LocationSettings::valuesToString(LocationSettingOption Key) const
void LocationSettings::printLocationSettings() const
{
Logger &logger = Logger::getInstance();
size_t enum_size = sizeof(LocationSettingOption) /
sizeof(std::underlying_type<LocationSettingOption>);

logger.log(DEBUG, "Path:\t" + _path);
for (int i = static_cast<int>(LocationSettingOption::Prefix);
i < static_cast<int>(LocationSettingOption::Count); i++)
for (size_t i = 0; i <= enum_size + 1; i++)
{
logger.log(DEBUG,
"LocationSetting: Key:\t" +
Expand Down
62 changes: 47 additions & 15 deletions src/ServerSettings.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

#include <HTTPRequest.hpp>
#include <LocationSettings.hpp>
#include <Logger.hpp>
#include <ServerSettings.hpp>
Expand Down Expand Up @@ -70,7 +71,36 @@ ServerSettings::identifyServerSetting(const std::string &token)
// TODO: this might need a costum exception
}

// TODO: void addLocationSetting(LocationSettings settings);
const std::string
methodToString(HTTPMethod method) // TODO: change data_types in function
Tentanus marked this conversation as resolved.
Show resolved Hide resolved
{
switch (method)
{
case (HTTPMethod::GET):
return ("GET");
case (HTTPMethod::POST):
return ("POST");
case (HTTPMethod::DELETE):
return ("DELETE");
default:
throw std::runtime_error("Unknown HTTPMethod");
}
}

bool ServerSettings::resolveLocation(const std::string &path,
HTTPMethod input_method)
{
for (auto &location_instance : _location_settings)
{
if (location_instance.getPath() != path)
continue;
for (auto &methods :
location_instance.getValues(LocationSettingOption::AllowedMethods))
if (methods == methodToString(input_method))
return (true);
}
return (false);
}

const std::string &ServerSettings::getValue(ServerSettingOption setting) const
{
Expand All @@ -84,22 +114,24 @@ void ServerSettings::setValue(ServerSettingOption key, const std::string &value)

// THIS IS PRINTING FUNCTION

std::string keyToString(ServerSettingOption Key)
std::string keyToString(size_t Key)
{
switch (Key)
ServerSettingOption ss_key = static_cast<ServerSettingOption>(Key);

switch (ss_key)
{
case (ServerSettingOption::Port):
return ("Port");
return ("Port\t\t");
case (ServerSettingOption::Host):
return ("Host");
return ("Host\t\t");
case (ServerSettingOption::ServerName):
return ("ServerName");
return ("ServerName\t");
case (ServerSettingOption::ClientMaxBodySize):
return ("ClientMaxBodySize");
case (ServerSettingOption::ErrorPages):
return ("ErrorPages");
return ("ErrorPages\t");
case (ServerSettingOption::Location):
return ("Location");
return ("Location\t");
default:
return ("OUT OF BOUND KEY");
}
Expand All @@ -108,22 +140,22 @@ std::string keyToString(ServerSettingOption Key)
void ServerSettings::printServerSettings() const
{
Logger &logger = Logger::getInstance();
size_t enum_size = sizeof(ServerSettingOption) /
sizeof(std::underlying_type<ServerSettingOption>);

for (int i = static_cast<int>(ServerSettingOption::Port);
i < static_cast<int>(ServerSettingOption::Count); i++)
for (size_t i = 0; i <= enum_size + 1; i++)
{
logger.log(DEBUG, "ServerSetting: Key:\t" +
keyToString(static_cast<ServerSettingOption>(i)));
try
{
logger.log(DEBUG,
"ServerSetting: Value:\t" +
"\tServerSetting: Key:\t" + keyToString(i) +
"\tValue:\t" +
getValue(static_cast<ServerSettingOption>(i)));
}
catch (std::out_of_range &e)
{
logger.log(WARNING, "ServerSettings : Missing option: " +
std::string(e.what()));
logger.log(WARNING, "\tServerSetting: Key:\t" + keyToString(i) +
" Missed option: " + std::string(e.what()));
}
}

Expand Down