Skip to content

Commit 6aae5c3

Browse files
author
Moshe Zadka
committed
- #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 - python#129288 - urllib.py - chanign %02x to %02X in quoting - urllib.py - HTTPS now works with string URLs
1 parent 9b80782 commit 6aae5c3

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

Lib/urllib.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -512,14 +512,30 @@ class FancyURLopener(URLopener):
512512
def __init__(self, *args):
513513
apply(URLopener.__init__, (self,) + args)
514514
self.auth_cache = {}
515+
self.tries = 0
516+
self.maxtries = 10
515517

516518
def http_error_default(self, url, fp, errcode, errmsg, headers):
517519
"""Default error handling -- don't raise an exception."""
518520
return addinfourl(fp, headers, "http:" + url)
519521

520522
def http_error_302(self, url, fp, errcode, errmsg, headers, data=None):
521523
"""Error 302 -- relocated (temporarily)."""
522-
# XXX The server can force infinite recursion here!
524+
self.tries += 1
525+
if self.maxtries and self.tries >= self.maxtries:
526+
if hasattr(self, "http_error_500"):
527+
meth = self.http_error_500
528+
else:
529+
meth = self.http_error_default
530+
self.tries = 0
531+
return meth(url, fp, 500,
532+
"Internal Server Error: Redirect Recursion", headers)
533+
result = self.redirect_internal(url, fp, errcode, errmsg, headers,
534+
data)
535+
self.tries = 0
536+
return result
537+
538+
def redirect_internal(self, url, fp, errcode, errmsg, headers, data):
523539
if headers.has_key('location'):
524540
newurl = headers['location']
525541
elif headers.has_key('uri'):
@@ -555,6 +571,8 @@ def http_error_401(self, url, fp, errcode, errmsg, headers, data=None):
555571
return getattr(self,name)(url, realm)
556572
else:
557573
return getattr(self,name)(url, realm, data)
574+
return URLopener.http_error_default(self, url, fp,
575+
errcode, errmsg, headers)
558576

559577
def retry_http_basic_auth(self, url, realm, data=None):
560578
host, selector = splithost(url)
@@ -689,7 +707,7 @@ def retrfile(self, file, type):
689707
cmd = 'RETR ' + file
690708
conn = self.ftp.ntransfercmd(cmd)
691709
except ftplib.error_perm, reason:
692-
if reason[:3] != '550':
710+
if str(reason)[:3] != '550':
693711
raise IOError, ('ftp error', reason), sys.exc_info()[2]
694712
if not conn:
695713
# Set transfer mode to ASCII!
@@ -1036,7 +1054,7 @@ def _fast_quote(s):
10361054
for i in range(len(res)):
10371055
c = res[i]
10381056
if not _fast_safe.has_key(c):
1039-
res[i] = '%%%02x' % ord(c)
1057+
res[i] = '%%%02X' % ord(c)
10401058
return string.join(res, '')
10411059

10421060
def quote(s, safe = '/'):
@@ -1067,7 +1085,7 @@ def quote(s, safe = '/'):
10671085
for i in range(len(res)):
10681086
c = res[i]
10691087
if c not in safe:
1070-
res[i] = '%%%02x' % ord(c)
1088+
res[i] = '%%%02X' % ord(c)
10711089
return string.join(res, '')
10721090

10731091
def quote_plus(s, safe = ''):

Misc/NEWS

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,15 @@ http://sourceforge.net/tracker/index.php?func=detail&aid=<id>&group_id=5470&atid
141141

142142
- #117606 - configure.in, configure - use gcc -shared and gcc -fPIC
143143

144+
- #227562 - urllib.py - call URLopener.http_error_default when
145+
an invalid 401 request is being handled.
146+
147+
- urllib.py - provide simple recovery/escape from apparent redirect recursion
148+
149+
- #129288 - urllib.py - chanign %02x to %02X in quoting
150+
151+
- urllib.py - HTTPS now works with string URLs
152+
144153
What's New in Python 2.0?
145154
=========================
146155

0 commit comments

Comments
 (0)