Skip to content

Conversation

@zwoop
Copy link
Contributor

@zwoop zwoop commented Oct 14, 2025

This refactors the parsing / evaluation, to put most of the exec / evaluation logic into
a new OperatorIf. This is not a real operator, that configurations would use, but allows
for the conditionals to be nested. The syntax in HRW is not awesome, but best we can
do within the constraints of backwards compatibility.

As such, this PR also includes changes and additions to hrw4u, where nested if's are
now supported and added to the existing if() / else semantics. E.g.

SEND_RESPONSE {
    if inbound.status == 200 {
        inbound.resp.X-When-200-Before = "Yes";
        if inbound.req.X-Foo == "foo" {
            inbound.resp.X-Foo = "Yes";
            if inbound.req.X-Bar == "bar" with NOCASE {
                inbound.resp.X-Foo-And-Bar = "Yes";
            } elif inbound.req.X-Fie == "fie" with NOCASE {
                inbound.resp.X-Foo-And-Fie = "Yes";
            }
        } elif inbound.req.X-Foo == "maybe" {
.
.
.

@zwoop zwoop added this to the 10.2.0 milestone Oct 14, 2025
@zwoop zwoop self-assigned this Oct 14, 2025
@zwoop zwoop force-pushed the HRWIfOperator branch 2 times, most recently from 0c7d56d to 5db0f6f Compare October 17, 2025 02:55
@zwoop zwoop requested a review from Copilot October 20, 2025 17:42
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for nested if/elif/else conditionals in the header rewrite system by refactoring the conditional logic into a new OperatorIf pseudo-operator. The changes enable arbitrary nesting depth for complex conditional logic while maintaining backward compatibility.

Key changes:

  • Introduced OperatorIf class to encapsulate conditional evaluation logic
  • Added if/endif keywords to enable nested conditional blocks
  • Implemented explicit slot assignment syntax for variables (e.g., @7) in hrw4u
  • Updated grammar files and parsers to support new syntax

Reviewed Changes

Copilot reviewed 43 out of 44 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
tools/hrw4u/grammar/hrw4u.g4 Added AT token and optional slot syntax to variable declarations
tools/hrw4u/grammar/u4wrh.g4 Added IF_OP and ENDIF_OP tokens and corresponding grammar rules
tools/hrw4u/src/visitor.py Updated to handle explicit slot assignments and nested conditionals
tools/hrw4u/src/symbols.py Refactored slot allocation to support explicit slot assignment
tools/hrw4u/src/types.py Renamed index to slot for clarity
tools/hrw4u/src/hrw_visitor.py Added support for if/endif operators and fixed nesting depth tracking
tools/hrw4u/scripts/testcase.py Added exceptions mechanism to skip direction-specific tests
plugins/header_rewrite/ruleset.h Refactored to delegate conditional logic to OperatorIf
plugins/header_rewrite/ruleset.cc Simplified execution to use OperatorIf::exec_and_return_mods
plugins/header_rewrite/operators.h Added OperatorIf class and OperatorAndMods struct
plugins/header_rewrite/operators.cc Implemented OperatorIf conditional evaluation logic
plugins/header_rewrite/operator.h Moved OperatorAndMods to operator.h for shared use
plugins/header_rewrite/parser.h Added IF and ENDIF clause types
plugins/header_rewrite/parser.cc Added parsing support for if/endif keywords
plugins/header_rewrite/header_rewrite.cc Implemented if/endif stack-based parsing with nesting validation
doc/admin-guide/plugins/header_rewrite.en.rst Added comprehensive documentation for nested conditionals
doc/admin-guide/configuration/hrw4u.en.rst Documented explicit slot assignment syntax
tests/gold_tests/pluginTest/header_rewrite/* Added test cases for nested conditionals
tools/hrw4u/tests/data/* Added test cases for explicit slots and updated error messages

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@zwoop
Copy link
Contributor Author

zwoop commented Oct 20, 2025

Ignored the nitpick comments, the current code is inline with other uses within the code base. Consistency is better.

Copy link
Contributor

@cmcfarlen cmcfarlen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Idk if I understand why If has to be an Operator. I guess sometimes it is called via the virtual exec function? The nested ones?

@zwoop
Copy link
Contributor Author

zwoop commented Oct 27, 2025

LGTM. Idk if I understand why If has to be an Operator. I guess sometimes it is called via the virtual exec function? The nested ones?

Does it have to be? No. But it simplified evaluations significantly, since the the operator list can now contain other OperatorIf's, giving us the nesting. This is similar to how we made groups ("nested conditions"), where every condition is a GROUP condition, which then contains other conditions and operators. Magic.

@zwoop zwoop merged commit e319f59 into apache:master Oct 27, 2025
15 checks passed
@zwoop zwoop deleted the HRWIfOperator branch October 27, 2025 16:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants