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
|
2017-08-15 14:42:29 +00:00
|
|
|
import logging
|
2017-10-01 14:00:32 +00:00
|
|
|
import urllib
|
2017-08-15 14:42:29 +00:00
|
|
|
|
|
|
|
import appengine_config
|
|
|
|
|
2019-04-16 15:02:19 +00:00
|
|
|
from oauth_dropins.webutil.handlers import memcache_response
|
2017-08-15 14:42:29 +00:00
|
|
|
import requests
|
|
|
|
import webapp2
|
|
|
|
|
|
|
|
import common
|
|
|
|
|
2017-08-15 15:04:18 +00:00
|
|
|
LINK_HEADER = '<%s>; rel="webmention"'
|
2017-08-15 14:42:29 +00:00
|
|
|
|
2019-04-16 15:02:19 +00:00
|
|
|
CACHE_TIME = datetime.timedelta(seconds=15)
|
|
|
|
|
2017-08-15 14:42:29 +00:00
|
|
|
|
|
|
|
class AddWebmentionHandler(webapp2.RequestHandler):
|
|
|
|
"""Proxies HTTP requests and adds Link header to our webmention endpoint."""
|
|
|
|
|
2019-04-16 15:02:19 +00:00
|
|
|
@memcache_response(CACHE_TIME)
|
2017-08-15 14:42:29 +00:00
|
|
|
def get(self, url):
|
2017-10-01 14:00:32 +00:00
|
|
|
url = urllib.unquote(url)
|
2017-08-15 14:42:29 +00:00
|
|
|
if not url.startswith('http://') and not url.startswith('https://'):
|
2017-10-15 23:57:33 +00:00
|
|
|
common.error(self, 'URL must start with http:// or https://')
|
2017-08-15 14:42:29 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
resp = common.requests_get(url)
|
|
|
|
except requests.exceptions.Timeout as e:
|
2017-10-17 14:46:42 +00:00
|
|
|
common.error(self, unicode(e), status=504, exc_info=True)
|
2017-08-15 14:42:29 +00:00
|
|
|
except requests.exceptions.RequestException as e:
|
2017-10-17 14:46:42 +00:00
|
|
|
common.error(self, unicode(e), status=502, exc_info=True)
|
2017-08-15 14:42:29 +00:00
|
|
|
|
|
|
|
self.response.status_int = resp.status_code
|
|
|
|
self.response.write(resp.content)
|
|
|
|
|
2017-08-15 15:04:18 +00:00
|
|
|
endpoint = LINK_HEADER % (str(self.request.get('endpoint')) or
|
2017-09-30 14:56:40 +00:00
|
|
|
appengine_config.HOST_URL + '/webmention')
|
2017-08-15 14:42:29 +00:00
|
|
|
self.response.headers.clear()
|
|
|
|
self.response.headers.update(resp.headers)
|
2017-08-15 15:04:18 +00:00
|
|
|
self.response.headers.add('Link', endpoint)
|
2017-08-15 14:42:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
app = webapp2.WSGIApplication([
|
|
|
|
('/wm/(.+)', AddWebmentionHandler),
|
|
|
|
], debug=appengine_config.DEBUG)
|