1 | # -*- coding: utf-8 -*- |
---|
2 | # |
---|
3 | # Copyright (C) 2011 Edgewall Software |
---|
4 | # All rights reserved. |
---|
5 | # |
---|
6 | # This software is licensed as described in the file COPYING, which |
---|
7 | # you should have received as part of this distribution. The terms |
---|
8 | # are also available at http://bitten.edgewall.org/wiki/License. |
---|
9 | |
---|
10 | """Compatibility fixes for external libraries and Python.""" |
---|
11 | |
---|
12 | import sys |
---|
13 | |
---|
14 | # Fix for issue http://bugs.python.org/issue8797 in Python 2.6 |
---|
15 | |
---|
16 | if sys.version_info[:2] == (2, 6): |
---|
17 | import urllib2 |
---|
18 | import base64 |
---|
19 | |
---|
20 | class HTTPBasicAuthHandler(urllib2.HTTPBasicAuthHandler): |
---|
21 | """Patched version of Python 2.6's HTTPBasicAuthHandler. |
---|
22 | |
---|
23 | The fix for [1]_ introduced an infinite recursion bug [2]_ into |
---|
24 | Python 2.6.x that is triggered by attempting to connect using |
---|
25 | Basic authentication with a bad username and/or password. This |
---|
26 | class fixes the problem using the simple solution outlined in [3]_. |
---|
27 | |
---|
28 | .. [1] http://bugs.python.org/issue3819 |
---|
29 | .. [2] http://bugs.python.org/issue8797 |
---|
30 | .. [3] http://bugs.python.org/issue8797#msg126657 |
---|
31 | """ |
---|
32 | |
---|
33 | def retry_http_basic_auth(self, host, req, realm): |
---|
34 | user, pw = self.passwd.find_user_password(realm, host) |
---|
35 | if pw is not None: |
---|
36 | raw = "%s:%s" % (user, pw) |
---|
37 | auth = 'Basic %s' % base64.b64encode(raw).strip() |
---|
38 | if req.get_header(self.auth_header, None) == auth: |
---|
39 | return None |
---|
40 | req.add_unredirected_header(self.auth_header, auth) |
---|
41 | return self.parent.open(req, timeout=req.timeout) |
---|
42 | else: |
---|
43 | return None |
---|
44 | |
---|
45 | else: |
---|
46 | from urllib2 import HTTPBasicAuthHandler |
---|