add /r/ redirect endpoint

for #16, #32. cc @swentel. more to come.
pull/36/head
Ryan Barrett 2018-10-14 07:42:28 -07:00
rodzic 118f8380c7
commit e720e543d8
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 6BE31FDF4776E9D4
3 zmienionych plików z 58 dodań i 0 usunięć

Wyświetl plik

@ -67,6 +67,10 @@ handlers:
script: logs.app
secure: always
- url: /r/.+
script: redirect.app
secure: always
- url: /wm/.+
script: add_webmention.app
secure: always

34
redirect.py 100644
Wyświetl plik

@ -0,0 +1,34 @@
"""Simple endpoint that redirects to the embedded fully qualified URL.
Used to wrap ActivityPub ids with the fed.brid.gy domain so that Mastodon
accepts them. Background:
https://github.com/snarfed/bridgy-fed/issues/16#issuecomment-424799599
https://github.com/tootsuite/mastodon/pull/6219#issuecomment-429142747
"""
import logging
import webapp2
import appengine_config
import common
class RedirectHandler(webapp2.RequestHandler):
"""301 redirects to the embedded fully qualified URL.
e.g. redirects /r/https://foo.com/bar?baz to https://foo.com/bar?baz
"""
def get(self):
assert self.request.path_qs.startswith('/r/')
to = self.request.path_qs[3:]
if not to.startswith('http://') and not to.startswith('https://'):
common.error(self, 'Expected fully qualified URL; got %s' % to)
logging.info('redirecting to %s', to)
self.redirect(to)
app = webapp2.WSGIApplication([
(r'/r/.+', RedirectHandler),
], debug=appengine_config.DEBUG)

Wyświetl plik

@ -0,0 +1,20 @@
"""Unit tests for redirect.py.
"""
from redirect import app
import testutil
class ActivityPubTest(testutil.TestCase):
def test_redirect(self):
got = app.get_response('/r/https://foo.com/bar?baz=baj&biff')
self.assertEqual(302, got.status_int)
self.assertEqual('https://foo.com/bar?baz=baj&biff', got.headers['Location'])
def test_redirect_scheme_missing(self):
got = app.get_response('/r/asdf.com')
self.assertEqual(400, got.status_int)
def test_redirect_url_missing(self):
got = app.get_response('/r/')
self.assertEqual(404, got.status_int)