support base mime filtering

This commit is contained in:
Kay Faraday 2021-09-08 12:42:33 +00:00
parent 551e21def6
commit f8eb3e9567
2 changed files with 11 additions and 3 deletions

View File

@ -13,8 +13,8 @@ upstream = 'http://localhost:3001'
# these can be regexes
pattern = 'foo'
repl = 'bar'
# which mime types to replace for. defaults to all mime types.
mime_types = ['text/html', 'application/json', 'application/activity+json', 'application/ld+json']
# which mime types to replace for. defaults to all mime types. `x/y` also matches `x/a+y`, `x/b+y`, `x/c+y` etc.
mime_types = ['text/html', 'application/json']
# path prefixes not to replace for. these are applied in descending order of length.
excluded_routes = ['/.well-known/', '/user/']

View File

@ -58,11 +58,19 @@ async def handler(request):
if not hconfig['replace']:
print('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)
return await proxy_passthrough(upstream_resp, resp)
if hconfig['mime_types'] and upstream_resp.content_type not in hconfig['mime_types']:
type, slash, subtype = upstream_resp.content_type.partition('/')
if slash != '/':
base_mime_type = upstream_resp.content_type
else:
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)
return await proxy_passthrough(upstream_resp, resp)