From 6aae5c3082063c35eadc7548c9373dbdc62df201 Mon Sep 17 00:00:00 2001 From: Moshe Zadka Date: Sat, 31 Mar 2001 14:58:20 +0000 Subject: [PATCH] - #227562 - urllib.py - call URLopener.http_error_default when an invalid 401 request is being handled. - urllib.py - provide simple recovery/escape from apparent redirect recursion - #129288 - urllib.py - chanign %02x to %02X in quoting - urllib.py - HTTPS now works with string URLs --- Lib/urllib.py | 26 ++++++++++++++++++++++---- Misc/NEWS | 9 +++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/Lib/urllib.py b/Lib/urllib.py index 8b3c9240dbef..1946a971ab89 100644 --- a/Lib/urllib.py +++ b/Lib/urllib.py @@ -512,6 +512,8 @@ class FancyURLopener(URLopener): def __init__(self, *args): apply(URLopener.__init__, (self,) + args) self.auth_cache = {} + self.tries = 0 + self.maxtries = 10 def http_error_default(self, url, fp, errcode, errmsg, headers): """Default error handling -- don't raise an exception.""" @@ -519,7 +521,21 @@ class FancyURLopener(URLopener): def http_error_302(self, url, fp, errcode, errmsg, headers, data=None): """Error 302 -- relocated (temporarily).""" - # XXX The server can force infinite recursion here! + self.tries += 1 + if self.maxtries and self.tries >= self.maxtries: + if hasattr(self, "http_error_500"): + meth = self.http_error_500 + else: + meth = self.http_error_default + self.tries = 0 + return meth(url, fp, 500, + "Internal Server Error: Redirect Recursion", headers) + result = self.redirect_internal(url, fp, errcode, errmsg, headers, + data) + self.tries = 0 + return result + + def redirect_internal(self, url, fp, errcode, errmsg, headers, data): if headers.has_key('location'): newurl = headers['location'] elif headers.has_key('uri'): @@ -555,6 +571,8 @@ class FancyURLopener(URLopener): return getattr(self,name)(url, realm) else: return getattr(self,name)(url, realm, data) + return URLopener.http_error_default(self, url, fp, + errcode, errmsg, headers) def retry_http_basic_auth(self, url, realm, data=None): host, selector = splithost(url) @@ -689,7 +707,7 @@ class ftpwrapper: cmd = 'RETR ' + file conn = self.ftp.ntransfercmd(cmd) except ftplib.error_perm, reason: - if reason[:3] != '550': + if str(reason)[:3] != '550': raise IOError, ('ftp error', reason), sys.exc_info()[2] if not conn: # Set transfer mode to ASCII! @@ -1036,7 +1054,7 @@ def _fast_quote(s): for i in range(len(res)): c = res[i] if not _fast_safe.has_key(c): - res[i] = '%%%02x' % ord(c) + res[i] = '%%%02X' % ord(c) return string.join(res, '') def quote(s, safe = '/'): @@ -1067,7 +1085,7 @@ def quote(s, safe = '/'): for i in range(len(res)): c = res[i] if c not in safe: - res[i] = '%%%02x' % ord(c) + res[i] = '%%%02X' % ord(c) return string.join(res, '') def quote_plus(s, safe = ''): diff --git a/Misc/NEWS b/Misc/NEWS index a4e68f51e9ea..6b3450e70b4f 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -141,6 +141,15 @@ http://sourceforge.net/tracker/index.php?func=detail&aid=&group_id=5470&atid - #117606 - configure.in, configure - use gcc -shared and gcc -fPIC +- #227562 - urllib.py - call URLopener.http_error_default when + an invalid 401 request is being handled. + +- urllib.py - provide simple recovery/escape from apparent redirect recursion + +- #129288 - urllib.py - chanign %02x to %02X in quoting + +- urllib.py - HTTPS now works with string URLs + What's New in Python 2.0? ========================= -- 2.47.3