2017-08-15 14:42:29 +00:00
|
|
|
"""HTTP proxy that injects our webmention endpoint.
|
|
|
|
"""
|
2019-04-16 15:02:19 +00:00
|
|
|
import datetime
|
2019-12-26 06:20:57 +00:00
|
|
|
import urllib.parse
|
2017-08-15 14:42:29 +00:00
|
|
|
|
2021-07-08 14:25:44 +00:00
|
|
|
import flask
|
|
|
|
from flask import request
|
2021-07-18 04:22:13 +00:00
|
|
|
from oauth_dropins.webutil import flask_util
|
2021-08-06 17:29:25 +00:00
|
|
|
from oauth_dropins.webutil.flask_util import error
|
2017-08-15 14:42:29 +00:00
|
|
|
import requests
|
|
|
|
|
2021-07-08 14:25:44 +00:00
|
|
|
from app import app, cache
|
2017-08-15 14:42:29 +00:00
|
|
|
import common
|
|
|
|
|
2017-08-15 15:04:18 +00:00
|
|
|
LINK_HEADER = '<%s>; rel="webmention"'
|
2019-04-16 15:02:19 +00:00
|
|
|
CACHE_TIME = datetime.timedelta(seconds=15)
|
|
|
|
|
2017-08-15 14:42:29 +00:00
|
|
|
|
2021-07-11 23:30:14 +00:00
|
|
|
@app.get(r'/wm/<path:url>')
|
2021-08-28 14:18:46 +00:00
|
|
|
@flask_util.cached(cache, CACHE_TIME)
|
2021-07-08 14:25:44 +00:00
|
|
|
def add_wm(url=None):
|
2017-08-15 14:42:29 +00:00
|
|
|
"""Proxies HTTP requests and adds Link header to our webmention endpoint."""
|
2021-07-08 14:25:44 +00:00
|
|
|
url = urllib.parse.unquote(url)
|
|
|
|
if not url.startswith('http://') and not url.startswith('https://'):
|
2021-08-06 17:29:25 +00:00
|
|
|
error('URL must start with http:// or https://')
|
2021-07-08 14:25:44 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
got = common.requests_get(url)
|
|
|
|
except requests.exceptions.Timeout as e:
|
2021-08-06 17:29:25 +00:00
|
|
|
error(str(e), status=504, exc_info=True)
|
2021-07-08 14:25:44 +00:00
|
|
|
except requests.exceptions.RequestException as e:
|
2021-08-06 17:29:25 +00:00
|
|
|
error(str(e), status=502, exc_info=True)
|
2021-07-08 14:25:44 +00:00
|
|
|
|
|
|
|
resp = flask.make_response(got.content, got.status_code, dict(got.headers))
|
|
|
|
resp.headers.add('Link', LINK_HEADER % (request.args.get('endpoint') or
|
|
|
|
request.host_url + 'webmention'))
|
|
|
|
return resp
|