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