HRW4U is a domain-specific language and compiler toolchain for Apache Traffic Server's header_rewrite plugin, providing a more readable and maintainable alternative to writing header rewrite rules directly.
hrw4u - Forward CompilerCompiles HRW4U source files into Apache Traffic Server header_rewrite configuration.
Input: Human-readable HRW4U syntax Output: Native header_rewrite plugin rules
u4wrh - Reverse CompilerDecompiles existing header_rewrite rules back into HRW4U source code.
Input: Native header_rewrite plugin rules Output: Human-readable HRW4U syntax
git clone https://github.com/apache/trafficserver.git cd trafficserver/tools/hrw4u # Create pyenv virtual environment pyenv virtualenv 3.11 hrw4u pyenv activate hrw4u pip install -r requirements.txt
# Generate parsers and build packages make # Create distributable wheel make package
# Install in development mode pip install -e . # Or install the built wheel pip install dist/hrw4u-*.whl
# Run all tests make test # Run specific test categories pytest -m examples # Documentation examples pytest -m conds # Condition tests pytest -m ops # Operator tests pytest -m reverse # Reverse compilation tests
# Compile HRW4U source to header_rewrite rules hrw4u input.hrw4u > output.conf # From stdin cat input.hrw4u | hrw4u > output.conf # Show parse tree (debugging) hrw4u --ast input.hrw4u # Enable debug output hrw4u --debug input.hrw4u
Options:
--hrw - Produce header_rewrite output (default)--ast - Show ANTLR parse tree--debug - Enable debug tracing# Decompile header_rewrite rules to HRW4U source u4wrh rules.conf > output.hrw4u # From stdin cat rules.conf | u4wrh > output.hrw4u # Show parse tree (debugging) u4wrh --ast rules.conf # Enable debug output u4wrh --debug rules.conf
Options:
--hrw4u - Produce HRW4U source output (default)--ast - Show ANTLR parse tree--debug - Enable debug tracingHRW4U Source (example.hrw4u):
REMAP {
if inbound.ip in {10.0.0.0/8, 192.168.0.0/16} {
inbound.req.X-IP = "{inbound.ip}";
}
}
Generated header_rewrite rules:
hrw4u example.hrw4u
cond %{REMAP_PSEUDO_HOOK} [AND]
cond %{IP:CLIENT} {10.0.0.0/8,192.168.0.0/16}
set-header X-IP "%{IP:CLIENT}"
Generated hrw4u script from header_rewrite rules:
hrw4u example.hrw4u | u4wrh
REMAP {
if inbound.ip in {10.0.0.0/8, 192.168.0.0/16} {
inbound.req.X-IP = "{inbound.ip}";
}
}
Passing the output from hrw4u back into u4wrh brings back the original script again!
The project uses a hybrid build system:
make # Build everything (default) make clean # Remove build artifacts make test # Run test suite make package # Create distributable wheel
tools/hrw4u/ ├── src/ # Python source code │ ├── common.py # Shared utilities and patterns │ ├── types.py # Type definitions and dataclasses │ ├── symbols.py # Symbol resolution engine │ └── ... # Additional modules ├── scripts/ # Command-line entry points │ ├── hrw4u # Forward compiler script │ └── u4wrh # Reverse compiler script ├── grammar/ # ANTLR4 grammar definitions ├── tests/ # Comprehensive test suite └── pyproject.toml # Modern Python package configuration
Licensed under the Apache License, Version 2.0. See LICENSE for details.
Part of the Apache Traffic Server project.