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 asdfasdfasdf
server { server {
listen 80; listen 80;
host 127.0.0.1;
server_name mylocal; server_name mylocal;
root /var/www/html2; root /var/www/html2;

View File

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

View File

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

View File

@ -13,6 +13,17 @@ std::string trim(const std::string &str)
return str.substr(first, last - first + 1); 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) size_t findCorrespondingClosingBrace(const std::string &str, size_t openPos)
{ {
int braceCount = 1; int braceCount = 1;

View File

@ -2,5 +2,6 @@
#include <string> #include <string>
std::string trimSemi(const std::string &str);
std::string trim(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);