kopia lustrzana https://github.com/snarfed/bridgy-fed
add /wm/... URL proxy that injects our webmention endpoint
rodzic
04e65d612d
commit
23180c7db0
|
@ -0,0 +1,41 @@
|
|||
"""HTTP proxy that injects our webmention endpoint.
|
||||
"""
|
||||
import logging
|
||||
|
||||
import appengine_config
|
||||
|
||||
import requests
|
||||
import webapp2
|
||||
|
||||
import common
|
||||
|
||||
LINK_HEADER = '<%s/webmention>; rel="webmention"'
|
||||
|
||||
|
||||
class AddWebmentionHandler(webapp2.RequestHandler):
|
||||
"""Proxies HTTP requests and adds Link header to our webmention endpoint."""
|
||||
|
||||
def get(self, url):
|
||||
if not url.startswith('http://') and not url.startswith('https://'):
|
||||
self.abort(400, 'URL must start with http:// or https://')
|
||||
|
||||
try:
|
||||
resp = common.requests_get(url)
|
||||
except requests.exceptions.Timeout as e:
|
||||
logging.info('Returning 504 due to', exc_info=True)
|
||||
self.abort(504, unicode(e))
|
||||
except requests.exceptions.RequestException as e:
|
||||
logging.info('Returning 502 due to', exc_info=True)
|
||||
self.abort(502, unicode(e))
|
||||
|
||||
self.response.status_int = resp.status_code
|
||||
self.response.write(resp.content)
|
||||
|
||||
self.response.headers.clear()
|
||||
self.response.headers.update(resp.headers)
|
||||
self.response.headers.add('Link', LINK_HEADER % self.request.host_url)
|
||||
|
||||
|
||||
app = webapp2.WSGIApplication([
|
||||
('/wm/(.+)', AddWebmentionHandler),
|
||||
], debug=appengine_config.DEBUG)
|
13
app.yaml
13
app.yaml
|
@ -35,14 +35,17 @@ handlers:
|
|||
upload: static/index.html
|
||||
|
||||
# dynamic
|
||||
- url: /[^/]+/?(inbox)?
|
||||
script: activitypub.app
|
||||
# TODO
|
||||
# secure: always
|
||||
- url: /wm/.+
|
||||
script: add_webmention.app
|
||||
secure: always
|
||||
|
||||
- url: /webmention
|
||||
script: webmention.app
|
||||
# secure: always
|
||||
secure: always
|
||||
|
||||
- url: /[^/]+/?(inbox)?
|
||||
script: activitypub.app
|
||||
secure: always
|
||||
|
||||
skip_files:
|
||||
- ^(.*/)?.*\.py[co]
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
# coding=utf-8
|
||||
"""Unit tests for add_webmention.py.
|
||||
"""
|
||||
import unittest
|
||||
|
||||
import mock
|
||||
import requests
|
||||
|
||||
from add_webmention import app
|
||||
|
||||
|
||||
@mock.patch('requests.get')
|
||||
class AddWebmentionTest(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.resp = requests.Response()
|
||||
self.resp._content = 'asdf ☕ qwert'
|
||||
self.resp.headers = {
|
||||
'Link': 'first',
|
||||
'Foo': 'bar',
|
||||
}
|
||||
|
||||
def test_get(self, mock_get):
|
||||
self.resp.status_code = 202
|
||||
mock_get.return_value = self.resp
|
||||
|
||||
got = app.get_response('/wm/http://url')
|
||||
self.assertEqual(202, got.status_int)
|
||||
self.assertEqual(self.resp._content, got.body)
|
||||
self.assertEqual(['bar'], got.headers.getall('Foo'))
|
||||
self.assertEqual(['first', '<http://localhost/webmention>; rel="webmention"'],
|
||||
got.headers.getall('Link'))
|
|
@ -10,6 +10,7 @@ from granary import microformats2
|
|||
import mf2py
|
||||
import mf2util
|
||||
from oauth_dropins.webutil import util
|
||||
import requests
|
||||
import webapp2
|
||||
|
||||
import activitypub
|
||||
|
|
Ładowanie…
Reference in New Issue