forked from snarfed/bridgy-fed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_webmention.py
45 lines (33 loc) · 1.33 KB
/
add_webmention.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""HTTP proxy that injects our webmention endpoint.
"""
import datetime
import urllib.parse
from oauth_dropins.webutil.handlers import cache_response
import requests
import webapp2
import common
LINK_HEADER = '<%s>; rel="webmention"'
CACHE_TIME = datetime.timedelta(seconds=15)
class AddWebmentionHandler(common.Handler):
"""Proxies HTTP requests and adds Link header to our webmention endpoint."""
@cache_response(CACHE_TIME)
def get(self, url):
url = urllib.parse.unquote(url)
if not url.startswith('http://') and not url.startswith('https://'):
self.error('URL must start with http:// or https://')
try:
resp = common.requests_get(url)
except requests.exceptions.Timeout as e:
self.error(str(e), status=504, exc_info=True)
except requests.exceptions.RequestException as e:
self.error(str(e), status=502, exc_info=True)
self.response.status_int = resp.status_code
self.response.write(resp.content)
endpoint = LINK_HEADER % (str(self.request.get('endpoint')) or
self.request.host_url + '/webmention')
self.response.headers.clear()
self.response.headers.update(resp.headers)
self.response.headers.add('Link', endpoint)
ROUTES = [
('/wm/(.+)', AddWebmentionHandler),
]