From 5bc655dfd32d4c185a9c512082091622b8c86e35 Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Wed, 7 Aug 2019 21:38:50 +0200 Subject: [PATCH] Tweak lookup --- utils/lookup.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/utils/lookup.py b/utils/lookup.py index 4f1e121..d5ddcff 100644 --- a/utils/lookup.py +++ b/utils/lookup.py @@ -4,6 +4,7 @@ import little_boxes.activitypub as ap import mf2py import requests from little_boxes.errors import NotAnActivityError +from little_boxes.errors import RemoteServerUnavailableError from little_boxes.webfinger import get_actor_url @@ -20,14 +21,20 @@ def lookup(url: str) -> ap.BaseActivity: # Some websites may returns 404, 503 or others when they don't support webfinger, and we're just taking a guess # when performing the lookup. pass + except requests.RequestException as err: + raise RemoteServerUnavailableError(f"failed to fetch {url}: {err!r}") backend = ap.get_backend() - resp = requests.get( - url, - timeout=15, - allow_redirects=False, - headers={"User-Agent": backend.user_agent()}, - ) + try: + resp = requests.head( + url, + timeout=10, + allow_redirects=True, + headers={"User-Agent": backend.user_agent()}, + ) + except requests.RequestException as err: + raise RemoteServerUnavailableError(f"failed to GET {url}: {err!r}") + resp.raise_for_status() # If the page is HTML, maybe it contains an alternate link pointing to an AP object