bridgy-fed/add_webmention.py

38 wiersze
1.2 KiB
Python
Czysty Zwykły widok Historia

"""HTTP proxy that injects our webmention endpoint.
"""
import datetime
import urllib.parse
2021-07-08 14:25:44 +00:00
import flask
from flask import request
import requests
2021-07-08 14:25:44 +00:00
from app import app, cache
import common
2021-07-10 15:07:40 +00:00
from common import error
LINK_HEADER = '<%s>; rel="webmention"'
CACHE_TIME = datetime.timedelta(seconds=15)
@app.get(r'/wm/<path:url>')
2021-07-08 14:25:44 +00:00
@cache.cached(timeout=CACHE_TIME.total_seconds(), query_string=True,
response_filter=common.not_5xx)
def add_wm(url=None):
"""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-07-10 15:07:40 +00:00
return 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-07-10 15:07:40 +00:00
return error(str(e), status=504, exc_info=True)
2021-07-08 14:25:44 +00:00
except requests.exceptions.RequestException as e:
2021-07-10 15:07:40 +00:00
return 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