From 5b91bef8968cae8be8d8d083b7f2d498c0c8052c Mon Sep 17 00:00:00 2001 From: Maks Snegov Date: Sun, 22 Jun 2014 11:47:21 +0400 Subject: [PATCH] add infinite redirects blocking --- nevernote.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/nevernote.py b/nevernote.py index 28e9905..87e99c8 100755 --- a/nevernote.py +++ b/nevernote.py @@ -10,6 +10,9 @@ from urllib.parse import urlparse import zlib +class InfiniteRedirects(Exception): pass + + class TitleParser(html.parser.HTMLParser): def __init__(self, *args, **kwargs): html.parser.HTMLParser.__init__(self, *args, **kwargs) @@ -26,8 +29,11 @@ class TitleParser(html.parser.HTMLParser): self.title = self.rawdata[title_start:title_end] -def download_content(url): +def download_content(url, depth=0): '''download page and decode it to utf-8''' + if depth > 10: + raise InfiniteRedirects('too much redirects: %s' % url) + up = urlparse(url) if not up.scheme: up = urlparse('//' + url) @@ -53,7 +59,7 @@ def download_content(url): or (response.status == http.client.FOUND)): new_url = response.getheader('Location') print('Redirecting to ' + new_url) - return download_content(new_url) + return download_content(new_url, depth+1) return response @@ -99,7 +105,7 @@ def embed_pictures(page, pict_urls): print('New picture: %s' % url) try: page = page.replace(url, embedded_image(url)) - except (ValueError): + except (ValueError, InfiniteRedirects): pass return page