lists.arthurdejong.org
RSS feed

Patch: report redirects to self

[Date Prev][Date Next] [Thread Prev][Thread Next]

Patch: report redirects to self



Hi! The following patch reports when a link redirects to itself, even if that 
redirection take a few steps to get back to the source:

Cheers,
Devin

Index: webcheck/db.py
===================================================================
--- webcheck/db.py      (revision 460)
+++ webcheck/db.py      (working copy)
@@ -96,7 +96,7 @@
 
     @staticmethod
     def clean_url(url):
-        # normalise the URL, removing the fragment from the URL
+        """normalise the URL, removing the fragment from the URL"""
         return urlparse.urldefrag(normalizeurl(url))[0]
 
     def _get_link(self, url):
@@ -135,18 +135,21 @@
 
     def add_redirect(self, url):
         """Indicate that this link redirects to the specified url."""
+        session = object_session(self)
         url = self.clean_url(url)
-        # figure out depth
+        # check for (possibly indirect) redirects to self
+        for link in session.query(Link).filter_by(url=url):
+            if link.follow_link() == self:
+                link.add_linkproblem('redirects back to source: %s' % self.url)
+                self.add_linkproblem('redirects back to source: %s' % link.url)
+                return
+        # figure out depth (how can [self.redirectdepth] ever by non-zero?)
         self.redirectdepth = max([self.redirectdepth] +
                                  [x.redirectdepth for x in self.parents]) + 1
         # check depth
         if self.redirectdepth >= config.REDIRECT_DEPTH:
             self.add_linkproblem('too many redirects (%d)' % 
self.redirectdepth)
             return
-        # check for redirect to self
-        if url == self.url:
-            self.add_linkproblem('redirect same as source: %s' % url)
-            return
         # add child
         self.add_child(url)
 

-- 
To unsubscribe send an email to
webcheck-users-unsubscribe@lists.arthurdejong.org or see
http://lists.arthurdejong.org/webcheck-users/