~zulcss/ubuntu/precise/requests/requests-ca

« back to all changes in this revision

Viewing changes to requests/monkeys.py

  • Committer: Bazaar Package Importer
  • Author(s): Daniele Tricoli
  • Date: 2011-08-23 02:00:41 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20110823020041-5ki3i0z3u0pvo2sj
Tags: 0.6.1-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
"""
10
10
 
11
11
import urllib2
12
 
 
 
12
import re
13
13
 
14
14
class Request(urllib2.Request):
15
15
    """Hidden wrapper around the urllib2.Request object. Allows for manual
26
26
 
27
27
        return urllib2.Request.get_method(self)
28
28
 
 
29
 
29
30
class HTTPRedirectHandler(urllib2.HTTPRedirectHandler):
30
 
 
 
31
    """HTTP Redirect handler."""
31
32
    def http_error_301(self, req, fp, code, msg, headers):
32
33
        pass
33
34
 
36
37
 
37
38
 
38
39
class HTTPBasicAuthHandler(urllib2.HTTPBasicAuthHandler):
 
40
    """HTTP Basic Auth Handler with authentication loop fixes."""
39
41
 
40
42
    def __init__(self, *args, **kwargs):
41
43
        urllib2.HTTPBasicAuthHandler.__init__(self, *args, **kwargs)
42
44
        self.retried_req = None
 
45
        self.retried = 0
 
46
 
43
47
 
44
48
    def reset_retry_count(self):
45
49
        # Python 2.6.5 will call this on 401 or 407 errors and thus loop
47
51
        # http_error_auth_reqed instead.
48
52
        pass
49
53
 
 
54
 
50
55
    def http_error_auth_reqed(self, auth_header, host, req, headers):
51
56
        # Reset the retry counter once for each request.
52
57
        if req is not self.retried_req:
59
64
 
60
65
 
61
66
 
 
67
class HTTPForcedBasicAuthHandler(HTTPBasicAuthHandler):
 
68
    """HTTP Basic Auth Handler with forced Authentication."""
 
69
 
 
70
    auth_header = 'Authorization'
 
71
    rx = re.compile('(?:.*,)*[ \t]*([^ \t]+)[ \t]+'
 
72
                    'realm=(["\'])(.*?)\\2', re.I)
 
73
 
 
74
    def __init__(self,  *args, **kwargs):
 
75
        HTTPBasicAuthHandler.__init__(self, *args, **kwargs)
 
76
 
 
77
 
 
78
    def http_error_401(self, req, fp, code, msg, headers):
 
79
        url = req.get_full_url()
 
80
        response = self._http_error_auth_reqed('www-authenticate', url, req, headers)
 
81
        self.reset_retry_count()
 
82
        return response
 
83
 
 
84
    http_error_404 = http_error_401
 
85
 
 
86
 
 
87
    def _http_error_auth_reqed(self, authreq, host, req, headers):
 
88
 
 
89
        authreq = headers.get(authreq, None)
 
90
 
 
91
        if self.retried > 5:
 
92
            # retry sending the username:password 5 times before failing.
 
93
            raise urllib2.HTTPError(req.get_full_url(), 401, "basic auth failed",
 
94
                            headers, None)
 
95
        else:
 
96
            self.retried += 1
 
97
 
 
98
        if authreq:
 
99
 
 
100
            mo = self.rx.search(authreq)
 
101
 
 
102
            if mo:
 
103
                scheme, quote, realm = mo.groups()
 
104
 
 
105
                if scheme.lower() == 'basic':
 
106
                    response = self.retry_http_basic_auth(host, req, realm)
 
107
 
 
108
                    if response and response.code not in (401, 404):
 
109
                        self.retried = 0
 
110
                    return response
 
111
        else:
 
112
            response = self.retry_http_basic_auth(host, req, 'Realm')
 
113
 
 
114
            if response and response.code not in (401, 404):
 
115
                self.retried = 0
 
116
            return response
 
117
 
 
118
 
 
119
 
62
120
class HTTPDigestAuthHandler(urllib2.HTTPDigestAuthHandler):
63
121
 
64
122
    def __init__(self, *args, **kwargs):