u413-284-si / webserv

This project is about setting up a http web server, providing a static website.
MIT License
0 stars 0 forks source link

58 handle special cases concerning curly brackets #61

Closed QCHR1581 closed 1 month ago

QCHR1581 commented 2 months ago

Implemented Fixes

Created implementation for handling following cases concerning curly brackets:

For details see #58.


Approach for curly brackets on the next line & whitespaces between server and bracket

Both cases are now getting handled by extracting the directive of the current line (in this case server) and checking if the the directive equals server

for (readAndTrimLine(); m_currentLine != "}"; readAndTrimLine()) {
+                 if (getDirective(m_currentLine) == "server") {
-                 if (m_currentLine == "server {") {
            ConfigServer server;
            m_configFile.servers.push_back(server);
            while (m_currentLine != "}") {
                readAndTrimLine();
                readServerConfigLine();
        }
        m_serverIndex++;
    }
}

Approach for a directive on the same line as a bracket

To handle this case, a rewrite of the read server config line loop and the read location config line loop was necessary. Furthermore the way how to process a remaining line also had to be changed.

Read server config line loop

while (m_currentLine != "}") {
    readAndTrimLine();
    readServerConfigLine();
    }

Read location config line loop

while (m_currentLine != "}") {
    readAndTrimLine();
    readLocationConfigLine();
    }
    m_locationIndex++;
    processRemainingLine(line);
    continue;

Process remaining line

void ConfigFileParser::processRemainingLine(std::string& line)
{
    size_t semicolonIndex = line.find(';');
    if (semicolonIndex == std::string::npos) {
        line = "";
        m_currentLine = line;
        return;
    }

    line.erase(0, semicolonIndex + 1);
    line = webutils::trimLeadingWhitespaces(line);
    webutils::trimTrailingWhiteSpaces(line);
    m_currentLine = line;
}

Reason to change the read loops

The reason to change the approach in this way is to stop the read loops if the current line equals }, even though a directive is on the same line as the }.

When a line like this appears:

root /var/www/hmtl; }

The directive is read, and the remaining line is processed. This results in the line being reduced to only a } character. Then the m_currentLine is updated, causing the loop to terminate.

Closes #58


Tests

There are tests for all of the mentioned cases to make sure that each of them is handled correctly

gwolf-011235 commented 2 months ago

Did some tests: