From e366fe36932d9bb674a50832cc141c19e017df8a Mon Sep 17 00:00:00 2001 From: David Kerkeslager Date: Fri, 14 Jan 2022 13:43:20 -0500 Subject: [PATCH] Add something that shows the redirect sequence --- src/bigly/templates/bigly/link_info.html | 20 ++++++++++++++++++++ src/bigly/views.py | 11 ++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/bigly/templates/bigly/link_info.html b/src/bigly/templates/bigly/link_info.html index 4eb7d1f..7bce71d 100644 --- a/src/bigly/templates/bigly/link_info.html +++ b/src/bigly/templates/bigly/link_info.html @@ -12,9 +12,29 @@ {{ status }} {% endif %} + + Content Type: + {{ content_type }} + Link: {{ link }} + + Redirects: + +
    + {% for r in redirect_sequence %} +
  1. + {% comment "Add something that communicates when utm parameters were removed." %} + {% endcomment %} + + {{ r }} + +
  2. + {% endfor %} +
+ + {% endblock %} diff --git a/src/bigly/views.py b/src/bigly/views.py index c74bc28..b037d6b 100644 --- a/src/bigly/views.py +++ b/src/bigly/views.py @@ -31,11 +31,16 @@ def _remove_utm(link): )) def _follow_redirects(link, remove_utm): + redirect_sequence = [] + while True: + redirect_sequence.append(link) + if remove_utm: link = _remove_utm(link) - response = requests.head(link) + # TODO Do this in an async call so it doesn't block the main thread + response = requests.head(link, timeout=10) # TODO Handle timeouts @@ -53,6 +58,10 @@ def _follow_redirects(link, remove_utm): return { 'link': link, 'status': response.status_code, + + # TODO Handle different capitalizations of "Content-Type" + 'content_type': response.headers.get('Content-Type'), + 'redirect_sequence': redirect_sequence, } class IndexView(TemplateView): -- 2.20.1