Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions tools/hrw4u/src/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ class RegexPatterns:
re.VERBOSE | re.DOTALL,
)

# Grammar IDENT rule from hrw4u.g4 (header names, qualifiers)
GRAMMAR_IDENT: Final = re.compile(r'^[a-zA-Z_][a-zA-Z0-9_@.-]*$')

# Additional performance patterns
IDENTIFIER: Final = re.compile(r'^[a-zA-Z_][a-zA-Z0-9_]*$')
WHITESPACE: Final = re.compile(r'\s+')
Expand Down
3 changes: 2 additions & 1 deletion tools/hrw4u/src/hrw_symbols.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ def repl(match: re.Match) -> str:

def _handle_set_rm_operation(
self, cmd: str, toks: list[str], prefix: str, qualifier: str, section: SectionType | None = None) -> str:
qualifier = Validator.unquote_if_ident(qualifier)
if cmd.startswith("rm-"):
return f'{prefix}{qualifier} = ""'
if len(toks) < 3:
Expand Down Expand Up @@ -289,7 +290,7 @@ def _handle_statement_function(self, name: str, args: list[str], section: Sectio
qargs = [status_code, self._rewrite_inline_percents(f'"{url_arg}"', section)]
elif name == "add-header" and args:
# Convert add-header command to += syntax for reverse mapping
header_name = args[0]
header_name = Validator.unquote_if_ident(args[0])
prefix = self.get_prefix_for_context("header_ops", section)
prefixed_header = f"{prefix}{header_name}"

Expand Down
12 changes: 12 additions & 0 deletions tools/hrw4u/src/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,18 @@ def needs_quotes(value: str) -> bool:
def quote_if_needed(value: str) -> str:
return f'"{value}"' if Validator.needs_quotes(value) else value

@staticmethod
def unquote_if_ident(value: str) -> str:
"""Strip surrounding quotes when the unquoted form is a valid grammar IDENT.

See grammar/hrw4u.g4:72 (IDENT rule); regex at common.py:52 must stay in sync.
"""
if len(value) >= 2 and value.startswith('"') and value.endswith('"'):
inner = value[1:-1]
if RegexPatterns.GRAMMAR_IDENT.fullmatch(inner):
return inner
return value

@staticmethod
def percent_block() -> Callable[[str], None]:

Expand Down
3 changes: 3 additions & 0 deletions tools/hrw4u/tests/data/vars/exceptions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
#
# Explicit slot assignment syntax cannot be reversed
explicit_slots.input: hrw4u

# HRW accepts quoted header names but hrw4u emits them bare when IDENT-safe
hyphen_header.input: u4wrh
5 changes: 5 additions & 0 deletions tools/hrw4u/tests/data/vars/hyphen_header.input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
REMAP {
if inbound.method == HEAD {
inbound.req.X-Blobstore-Authproxy-Head-Request = "true";
}
}
2 changes: 2 additions & 0 deletions tools/hrw4u/tests/data/vars/hyphen_header.output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cond %{METHOD} =HEAD [AND]
set-header "X-Blobstore-Authproxy-Head-Request" "true"
14 changes: 14 additions & 0 deletions tools/hrw4u/tests/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,20 @@ def test_quote_if_needed(self):
assert Validator.quote_if_needed("simple") == "simple"
assert Validator.quote_if_needed("has space") == '"has space"'

@pytest.mark.parametrize(
"value,expected",
[
('"X-Foo"', "X-Foo"),
('"X Foo"', '"X Foo"'),
("X-Foo", "X-Foo"),
('""', '""'),
('"@internal"', '"@internal"'),
('"1foo"', '"1foo"'),
],
)
def test_unquote_if_ident(self, value, expected):
assert Validator.unquote_if_ident(value) == expected


class TestPlainTextFormatterParity:
"""The plain formatter must preserve current CLI output byte-for-byte."""
Expand Down