use cvar for dprint
This commit is contained in:
parent
f8eb3e9567
commit
e1753f26cb
16
replacer.py
16
replacer.py
|
@ -32,12 +32,13 @@ def load_config():
|
|||
return config
|
||||
|
||||
config = load_config()
|
||||
dprint = build_dprint_factory(config.get('debug'))
|
||||
dprint_factory = build_dprint_factory(config.get('debug'))
|
||||
dprint = ContextVar('dprint')
|
||||
http = ContextVar('http')
|
||||
|
||||
async def handler(request):
|
||||
hconfig = config['hosts'][request.host]
|
||||
print = dprint()
|
||||
dprint.set(dprint_factory())
|
||||
async with http().request(
|
||||
request.method,
|
||||
# TODO support upstream unix sockets
|
||||
|
@ -48,7 +49,7 @@ async def handler(request):
|
|||
# proxy redirects as-is
|
||||
allow_redirects=False,
|
||||
) as upstream_resp:
|
||||
print(request.method, request.host, request.path, '→', hconfig['upstream'])
|
||||
dprint()(request.method, request.host, request.path, '→', hconfig['upstream'])
|
||||
headers = upstream_resp.headers.copy()
|
||||
# we're not using gzip here so don't confuse our client
|
||||
with contextlib.suppress(KeyError): del headers['Content-Encoding']
|
||||
|
@ -56,12 +57,12 @@ async def handler(request):
|
|||
await resp.prepare(request)
|
||||
|
||||
if not hconfig['replace']:
|
||||
print('Not replacing for this host')
|
||||
dprint()('Not replacing for this host')
|
||||
return await proxy_passthrough(upstream_resp, resp)
|
||||
|
||||
for excluded_route in hconfig['excluded_routes']:
|
||||
if request.path.startswith(excluded_route):
|
||||
print('Not replacing for', excluded_route)
|
||||
dprint()('Not replacing for', excluded_route)
|
||||
return await proxy_passthrough(upstream_resp, resp)
|
||||
|
||||
type, slash, subtype = upstream_resp.content_type.partition('/')
|
||||
|
@ -71,16 +72,17 @@ async def handler(request):
|
|||
base_mime_type = type + slash + subtype.partition('+')[-1]
|
||||
|
||||
if hconfig['mime_types'] and base_mime_type not in hconfig['mime_types']:
|
||||
print('Not configured to replace for MIME type', upstream_resp.content_type)
|
||||
dprint()('Not configured to replace for MIME type', upstream_resp.content_type)
|
||||
return await proxy_passthrough(upstream_resp, resp)
|
||||
|
||||
print('replacing', repr(hconfig['pattern_decoded']), 'with', repr(hconfig['repl_decoded']))
|
||||
dprint()('replacing', repr(hconfig['pattern_decoded']), 'with', repr(hconfig['repl_decoded']))
|
||||
return await proxy_replace(hconfig, upstream_resp, resp)
|
||||
|
||||
async def proxy_replace(hconfig, upstream_resp, resp):
|
||||
# iter_lines when
|
||||
while (line := await upstream_resp.content.readline()):
|
||||
await resp.write(hconfig['pattern'].sub(hconfig['repl'], line))
|
||||
dprint()(repr(await upstream_resp.content.read()))
|
||||
|
||||
return await finalize_resp(resp)
|
||||
|
||||
|
|
Loading…
Reference in New Issue