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

[Major] A semicolon enclosed in quotes is treated as the end of a line. #15

Open
Flithor opened this issue Feb 25, 2025 · 3 comments
Open
Labels
bug Something isn't working

Comments

@Flithor
Copy link

Flithor commented Feb 25, 2025

Original:

    add_header X-Xss-Protection "1;mode=block";

Saves:

    add_header X-Xss-Protection "1;
@jxnkwlp jxnkwlp added the bug Something isn't working label Feb 25, 2025
@Flithor
Copy link
Author

Flithor commented Feb 26, 2025

@jxnkwlp here I fixed for:

private void ParseLine(string text, int lineIndex)

private void ParseLine(string text, int lineIndex)
{
    if (text.Length == 0)
    {
        return;
    }
    // comment row
    if (text[0] == '#')
    {
        var commentToken = new CommentToken(text.Trim().TrimStart('#').Trim());
        AddToken(commentToken);
        return;
    }
    // group ending
    if (text[0] == '}')
    {
        _currentGroupToken = _currentGroupToken?.Parent;

        _currentToken = null;

        return;
    }

    var key = string.Empty;
    var value = string.Empty;
    var comment = string.Empty;
    int keyEndSymbol = -1, groupStartSymbol = -1, commendSymbol = -1, endSymbol = -1;
    bool matchKey = false,
         matchValue = false,
         matchEnd = false,
         isBetweenSingleQuote = false,
         isBetweenDoubleQuote = false;
    for (var i = 0; i < text.Length; i++)
    {
        switch (text[i])
        {
            // match single quote
            case '\'':
                isBetweenSingleQuote = !isBetweenSingleQuote;
                break;
            // continue if in single quote
            case not '\'' when isBetweenSingleQuote:
                break;
            // match double quote
            case '"':
                isBetweenDoubleQuote = !isBetweenDoubleQuote;
                break;
            // continue if in double quote
            case not '"' when isBetweenDoubleQuote:
                break;
            // match the token ending
            case ';':
                endSymbol = i;
                value = text.Substring(keyEndSymbol + 1, endSymbol - keyEndSymbol - 1).Trim();
                matchEnd = true;
                matchValue = true;
                break;
            // match the group beginning
            case '{':
                groupStartSymbol = i;
                value = text.Substring(keyEndSymbol + 1, groupStartSymbol - keyEndSymbol - 1).Trim();
                matchValue = true;
                break;
            // won't match group ending here
            // match comment beginning
            case '#':
                commendSymbol = i;
                comment = text.Substring(commendSymbol).Trim().TrimStart('#').Trim();
                goto CommentBreakCheck;
            // match the first space to match token key
            case ' ' when !matchKey:
                keyEndSymbol = i;
                matchKey = true;
                key = text.Substring(0, keyEndSymbol).Trim();
                break;
        }
    }

    CommentBreakCheck:
    // WARNING: Here you need to consider whether to support cross-line quote mark pairs
    if (isBetweenSingleQuote || isBetweenDoubleQuote)
        throw new Exception($"Unpaired quote detected at line {lineIndex+1}!");
    if (groupStartSymbol > -1)
    {
        var groupToken = new GroupToken(_currentGroupToken, key, value, comment);
        AddToken(groupToken);
        _currentGroupToken = groupToken;
        return;
    }

    if (groupStartSymbol == -1 && endSymbol == -1)
    {
        _currentToken = new ValueToken(_currentGroupToken, key, value, comment);
        return;
    }

    if (endSymbol > -1)
    {
        AddToken(new ValueToken(_currentGroupToken, key, value?.Trim(), comment));
        return;
    }

    throw new Exception("Some thing wrong");
}

@jxnkwlp
Copy link
Owner

jxnkwlp commented Feb 26, 2025

Thanks. Can you create an pull request ?

@Flithor
Copy link
Author

Flithor commented Feb 26, 2025

@jxnkwlp No, I don't have time to write a full test case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants