Update server configuration and improve utility functions: change host to 127.0.0.1, modify root path, enhance trimSemi function, and adjust directive parsing logic.

This commit is contained in:
Quinten Mennen 2025-09-16 15:35:04 +02:00
parent e8f1c9a929
commit 0a26702457
5 changed files with 19 additions and 6 deletions

View File

@ -42,6 +42,7 @@ server {
asdfasdfasdf
server {
listen 80;
host 127.0.0.1;
server_name mylocal;
root /var/www/html2;

View File

@ -43,7 +43,7 @@ void removeEmptyLines(std::string &str)
{
if (!trim(line).empty())
{
result += line + '\n';
result += trimSemi(trim(line)) + '\n';
}
}
str = result;

View File

@ -59,7 +59,7 @@ void ServerConfig::parseDirectives(const std::string &declarations)
while (std::getline(stream, line))
{
std::string directive;
std::istringstream lineStream{trim(line)};
std::istringstream lineStream{line};
lineStream >> directive;
if (!directive.empty())
{
@ -80,10 +80,10 @@ void ServerConfig::parseDirectives(const std::string &declarations)
root = value;
std::cout << "Set root to " << root << '\n';
}
else if (directive == "server_name")
else if (directive == "host")
{
host = value;
std::cout << "Set server_name to " << host << '\n';
std::cout << "Set host to " << host << '\n';
}
else if (directive == "cgi_pass")
{
@ -123,7 +123,7 @@ void ServerConfig::parseDirectives(const std::string &declarations)
const LocationConfig &ServerConfig::getLocation(const std::string &path) const
{
if (locations.count(path) > 0 ) // NOLINT
if (locations.count(path) > 0) // NOLINT
{
return locations.at(path);
}

View File

@ -13,6 +13,17 @@ std::string trim(const std::string &str)
return str.substr(first, last - first + 1);
}
std::string trimSemi(const std::string &str)
{
size_t semi = str.find(';');
if (semi == str.length() - 1)
{
return str.substr(0, semi);
}
return str;
}
size_t findCorrespondingClosingBrace(const std::string &str, size_t openPos)
{
int braceCount = 1;

View File

@ -2,5 +2,6 @@
#include <string>
std::string trimSemi(const std::string &str);
std::string trim(const std::string &str);
size_t findCorrespondingClosingBrace(const std::string &str, size_t openPos);
size_t findCorrespondingClosingBrace(const std::string &str, size_t openPos);