support regexes
This commit is contained in:
parent
00c30d2af0
commit
44f956c1a3
5
app.py
5
app.py
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
import io
|
import io
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
import sys
|
import sys
|
||||||
import utils
|
import utils
|
||||||
import random
|
import random
|
||||||
|
@ -21,7 +22,7 @@ with open(sys.argv[1] if len(sys.argv) > 1 else 'config.toml') as f:
|
||||||
for host in config['hosts'].values():
|
for host in config['hosts'].values():
|
||||||
if not host.get('replace', True): continue
|
if not host.get('replace', True): continue
|
||||||
host['pattern_decoded'] = host['pattern']
|
host['pattern_decoded'] = host['pattern']
|
||||||
host['pattern'] = host['pattern'].encode()
|
host['pattern'] = re.compile(host['pattern'].encode())
|
||||||
host['repl_decoded'] = host['repl']
|
host['repl_decoded'] = host['repl']
|
||||||
host['repl'] = host['repl'].encode()
|
host['repl'] = host['repl'].encode()
|
||||||
|
|
||||||
|
@ -51,7 +52,7 @@ async def handler(request):
|
||||||
# iter_lines when
|
# iter_lines when
|
||||||
print('replacing', repr(hconfig['pattern_decoded']), 'with', repr(hconfig['repl_decoded']))
|
print('replacing', repr(hconfig['pattern_decoded']), 'with', repr(hconfig['repl_decoded']))
|
||||||
while (line := await upstream_resp.content.readline()):
|
while (line := await upstream_resp.content.readline()):
|
||||||
await resp.write(line.replace(hconfig['pattern'], hconfig['repl']))
|
await resp.write(hconfig['pattern'].sub(hconfig['repl'], line))
|
||||||
else:
|
else:
|
||||||
print('not replacing')
|
print('not replacing')
|
||||||
async for chunk in upstream_resp.content.iter_chunked(io.DEFAULT_BUFFER_SIZE):
|
async for chunk in upstream_resp.content.iter_chunked(io.DEFAULT_BUFFER_SIZE):
|
||||||
|
|
|
@ -10,6 +10,7 @@ debug = True
|
||||||
# replace foo with bar for site1.example
|
# replace foo with bar for site1.example
|
||||||
[hosts."site1.example"]
|
[hosts."site1.example"]
|
||||||
upstream = 'http://localhost:3001'
|
upstream = 'http://localhost:3001'
|
||||||
|
# these can be regexes
|
||||||
pattern = 'foo'
|
pattern = 'foo'
|
||||||
repl = 'bar'
|
repl = 'bar'
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue