Skip to content

Commit

Permalink
/convert: handle missing protocols
Browse files Browse the repository at this point in the history
  • Loading branch information
snarfed committed Jan 8, 2024
1 parent 4572f7b commit 178b5cc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
10 changes: 8 additions & 2 deletions convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ def convert(dest, _, src=None):
:func:`convert_source_path_redirect`
"""
if src:
src_cls = PROTOCOLS.get(src)
if not src_cls:
error(f'No protocol found for {src}', status=404)
logger.info(f'Overriding any domain protocol with {src}')
src_cls = PROTOCOLS[src]
else:
src_cls = Protocol.for_request(fed=Protocol)
if not src_cls:
Expand Down Expand Up @@ -108,4 +110,8 @@ def convert_source_path_redirect(src, dest, _):
request.url = request.url.replace(f'/{src}/', '/')
return convert(dest, None, src)

return redirect(subdomain_wrap(PROTOCOLS[src], new_path), code=301)
proto = PROTOCOLS.get(src)
if not proto:
error(f'No protocol found for {src}', status=404)

return redirect(subdomain_wrap(proto, new_path), code=301)
7 changes: 7 additions & 0 deletions tests/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,10 @@ def test_fed_subdomain(self):
self.assert_multiline_equals(HTML, resp.get_data(as_text=True),
ignore_blanks=True)

def test_missing_protocols(self):
resp = self.client.get('/convert/https:/user.com/post')
self.assertEqual(404, resp.status_code)

resp = self.client.get('/convert/https:/user.com/post',
base_url='https://fed.brid.gy/')
self.assertEqual(404, resp.status_code)

0 comments on commit 178b5cc

Please sign in to comment.