fetching AS2: handle Content-Type with charset suffix

pull/27/head
Ryan Barrett 2017-10-20 20:35:07 -07:00
rodzic c76cb22569
commit b7007a4521
3 zmienionych plików z 12 dodań i 5 usunięć

Wyświetl plik

@ -68,7 +68,7 @@ def _requests_fn(fn, url, parse_json=False, **kwargs):
resp = fn(url, **kwargs)
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':
logging.info(resp.text)
@ -114,7 +114,7 @@ def get_as2(url):
raise err
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
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']),
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
_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):
if not status:
status = 400

Wyświetl plik

@ -74,7 +74,7 @@ class WebmentionTest(testutil.TestCase):
'actor': {
'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 = """\
<html>

Wyświetl plik

@ -74,7 +74,7 @@ class WebmentionHandler(webapp2.RequestHandler):
target_resp = common.get_as2(target)
except (requests.HTTPError, exc.HTTPBadGateway) as e:
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)
raise