@@ -5,47 +5,56 @@ httpjail provides three different rule engines for evaluating HTTP requests. Eac
55## Available Engines
66
77### JavaScript (V8)
8+
89Fast, secure JavaScript evaluation using the V8 engine. Best for most use cases.
910
1011** Pros:**
12+
1113- ⚡ Fastest performance (compiled and cached)
1214- 🔒 Sandboxed execution environment
1315- 📝 Familiar JavaScript syntax
1416- 🎯 Direct access to request properties
1517
1618** Cons:**
19+
1720- Limited to JavaScript expressions
1821- No external tool integration
1922- No stateful operations between requests
2023
2124** Use when:** You need high-performance filtering with simple to moderate logic.
2225
2326### Shell Scripts
27+
2428Execute shell scripts for each request with full system access.
2529
2630** Pros:**
31+
2732- 🔧 Full system access and tool integration
2833- 📊 Can maintain state between requests
2934- 🔗 Integrate with existing scripts and tools
3035- 💾 Can query databases or external services
3136
3237** Cons:**
38+
3339- Slow (process spawn per request)
3440- Security considerations with shell execution
3541- Platform-specific behavior
3642
3743** Use when:** You need to integrate with existing tools or require complex system interactions.
3844
3945### Line Processor
46+
4047Stream-based processing with line-by-line evaluation.
4148
4249** Pros:**
50+
4351- 🚀 Long-running process (no spawn overhead)
4452- 📈 Can maintain state across requests
4553- 🐍 Language agnostic (Python, Ruby, etc.)
4654- 🔄 Bi-directional communication
4755
4856** Cons:**
57+
4958- More complex to implement
5059- Requires handling the protocol correctly
5160- Process management considerations
@@ -54,31 +63,37 @@ Stream-based processing with line-by-line evaluation.
5463
5564## Performance Comparison
5665
57- | Engine | Startup Time | Per-Request Overhead | Memory Usage | Stateful |
58- | --------| -------------| ---------------------| --------------| -- --------|
59- | JavaScript | Fast | ~ 0.1ms | Low | No |
60- | Shell Script | None | ~ 10-50ms | Variable | No* |
61- | Line Processor | Slow | ~ 0.5ms | Variable | Yes |
66+ | Engine | Startup Time | Per-Request Overhead | Memory Usage | Stateful |
67+ | -------------- | ------------ | -------------------- | ------------ | -------- |
68+ | JavaScript | Fast | ~ 0.1ms | Low | No |
69+ | Shell Script | None | ~ 10-50ms | Variable | No\* |
70+ | Line Processor | Slow | ~ 0.5ms | Variable | Yes |
6271
63- * Shell scripts can use files for state but each invocation is independent
72+ \ * Shell scripts can use files for state but each invocation is independent
6473
6574## Choosing a Rule Engine
6675
6776### For Production Use
77+
6878** JavaScript** is recommended for production environments due to:
79+
6980- Excellent performance
7081- Predictable resource usage
7182- Security isolation
7283- Simple deployment
7384
7485### For Development/Testing
86+
7587** Shell scripts** work well for:
88+
7689- Rapid prototyping
7790- Integration testing
7891- Debugging complex scenarios
7992
8093### For Complex Filtering
94+
8195** Line Processor** excels at:
96+
8297- Machine learning models
8398- Statistical analysis
8499- Complex stateful logic
@@ -87,20 +102,24 @@ Stream-based processing with line-by-line evaluation.
87102## Examples
88103
89104### Simple Host Filtering
105+
90106All three engines can handle basic filtering:
91107
92108** JavaScript:**
109+
93110``` bash
94111httpjail --js " r.host === 'github.com'" -- command
95112```
96113
97114** Shell Script:**
115+
98116``` bash
99117#! /bin/bash
100118[[ " $HTTPJAIL_HOST " == " github.com" ]] && exit 0 || exit 1
101119```
102120
103121** Line Processor:**
122+
104123``` python
105124# !/usr/bin/env python3
106125import sys, json
@@ -110,23 +129,27 @@ for line in sys.stdin:
110129```
111130
112131### Complex Logic
132+
113133For complex scenarios, consider the implementation complexity:
114134
115135** JavaScript** - Limited to expression evaluation:
136+
116137``` javascript
117138// Complex but still performant
118- const allowed = [' api.example.com' , ' cdn.example.com' ];
119- allowed .includes (r .host ) && r .method === ' GET' && r .path .startsWith (' /v1/' )
139+ const allowed = [" api.example.com" , " cdn.example.com" ];
140+ allowed .includes (r .host ) && r .method === " GET" && r .path .startsWith (" /v1/" );
120141```
121142
122143** Shell Script** - Can use any tool but slow:
144+
123145``` bash
124146#! /bin/bash
125147# Check against database, but spawns process per request
126148psql -c " SELECT allowed FROM rules WHERE host='$HTTPJAIL_HOST '" | grep -q true
127149```
128150
129151** Line Processor** - Best for complex stateful logic:
152+
130153``` python
131154# !/usr/bin/env python3
132155# Maintains state, handles thousands of requests efficiently
@@ -138,11 +161,11 @@ rate_limits = defaultdict(lambda: {"count": 0, "reset": time.time() + 60})
138161for line in sys.stdin:
139162 req = json.loads(line)
140163 host_limit = rate_limits[req[" host" ]]
141-
164+
142165 if time.time() > host_limit[" reset" ]:
143166 host_limit[" count" ] = 0
144167 host_limit[" reset" ] = time.time() + 60
145-
168+
146169 if host_limit[" count" ] < 100 : # 100 requests per minute
147170 host_limit[" count" ] += 1
148171 print (" allow" )
@@ -151,20 +174,6 @@ for line in sys.stdin:
151174 sys.stdout.flush()
152175```
153176
154- ## Migration Path
155-
156- Starting with JavaScript and need more features? Here's the typical migration path:
157-
158- 1 . ** Start with JavaScript** for simple rules
159- 2 . ** Move to Shell Scripts** when you need external tools
160- 3 . ** Upgrade to Line Processor** for high-volume or stateful filtering
161-
162- ## Security Considerations
163-
164- - ** JavaScript** : Fully sandboxed, safe for untrusted rules
165- - ** Shell Scripts** : Run with httpjail's privileges, validate all inputs
166- - ** Line Processor** : Depends on implementation language, consider using containers
167-
168177## Next Steps
169178
170179- [ JavaScript Rules] ( ./javascript-rules.md ) - Learn the JavaScript API
0 commit comments