kopia lustrzana https://github.com/snarfed/bridgy-fed
fetching AS2: handle Content-Type with charset suffix
rodzic
c76cb22569
commit
b7007a4521
13
common.py
13
common.py
|
@ -68,7 +68,7 @@ def _requests_fn(fn, url, parse_json=False, **kwargs):
|
||||||
resp = fn(url, **kwargs)
|
resp = fn(url, **kwargs)
|
||||||
|
|
||||||
logging.info('Got %s headers:%s', resp.status_code, resp.headers)
|
logging.info('Got %s headers:%s', resp.status_code, resp.headers)
|
||||||
type = resp.headers.get('Content-Type')
|
type = content_type(resp)
|
||||||
if type and type.startswith('text/') and type != 'text/json':
|
if type and type.startswith('text/') and type != 'text/json':
|
||||||
logging.info(resp.text)
|
logging.info(resp.text)
|
||||||
|
|
||||||
|
@ -114,7 +114,7 @@ def get_as2(url):
|
||||||
raise err
|
raise err
|
||||||
|
|
||||||
resp = requests_get(url, headers=CONNEG_HEADERS_AS2_HTML)
|
resp = requests_get(url, headers=CONNEG_HEADERS_AS2_HTML)
|
||||||
if resp.headers.get('Content-Type') in (CONTENT_TYPE_AS2, CONTENT_TYPE_AS2_LD):
|
if content_type(resp) in (CONTENT_TYPE_AS2, CONTENT_TYPE_AS2_LD):
|
||||||
return resp
|
return resp
|
||||||
|
|
||||||
parsed = BeautifulSoup(resp.content, from_encoding=resp.encoding)
|
parsed = BeautifulSoup(resp.content, from_encoding=resp.encoding)
|
||||||
|
@ -125,12 +125,19 @@ def get_as2(url):
|
||||||
|
|
||||||
resp = requests_get(urlparse.urljoin(resp.url, as2['href']),
|
resp = requests_get(urlparse.urljoin(resp.url, as2['href']),
|
||||||
headers=CONNEG_HEADERS_AS2)
|
headers=CONNEG_HEADERS_AS2)
|
||||||
if resp.headers.get('Content-Type') in (CONTENT_TYPE_AS2, CONTENT_TYPE_AS2_LD):
|
if content_type(resp) in (CONTENT_TYPE_AS2, CONTENT_TYPE_AS2_LD):
|
||||||
return resp
|
return resp
|
||||||
|
|
||||||
_error(resp)
|
_error(resp)
|
||||||
|
|
||||||
|
|
||||||
|
def content_type(resp):
|
||||||
|
"""Returns a requests.Response's Content-Type, without charset suffix."""
|
||||||
|
type = resp.headers.get('Content-Type')
|
||||||
|
if type:
|
||||||
|
return type.split(';')[0]
|
||||||
|
|
||||||
|
|
||||||
def error(handler, msg, status=None, exc_info=False):
|
def error(handler, msg, status=None, exc_info=False):
|
||||||
if not status:
|
if not status:
|
||||||
status = 400
|
status = 400
|
||||||
|
|
|
@ -74,7 +74,7 @@ class WebmentionTest(testutil.TestCase):
|
||||||
'actor': {
|
'actor': {
|
||||||
'url': 'http://orig/author',
|
'url': 'http://orig/author',
|
||||||
},
|
},
|
||||||
}, url='http://orig/as2', content_type=CONTENT_TYPE_AS2)
|
}, url='http://orig/as2', content_type=CONTENT_TYPE_AS2 + '; charset=utf-8')
|
||||||
|
|
||||||
self.reply_html = """\
|
self.reply_html = """\
|
||||||
<html>
|
<html>
|
||||||
|
|
|
@ -74,7 +74,7 @@ class WebmentionHandler(webapp2.RequestHandler):
|
||||||
target_resp = common.get_as2(target)
|
target_resp = common.get_as2(target)
|
||||||
except (requests.HTTPError, exc.HTTPBadGateway) as e:
|
except (requests.HTTPError, exc.HTTPBadGateway) as e:
|
||||||
if (e.response.status_code // 100 == 2 and
|
if (e.response.status_code // 100 == 2 and
|
||||||
e.response.headers.get('Content-Type').startswith('text/html')):
|
common.content_type(e.response).startswith('text/html')):
|
||||||
return self.send_salmon(source_obj, source_mf2, target_resp=e.response)
|
return self.send_salmon(source_obj, source_mf2, target_resp=e.response)
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
|
Ładowanie…
Reference in New Issue