/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/transport/http/_urllib2_wrappers.py

merge bzr.dev r4054

Show diffs side-by-side

added added

removed removed

Lines of Context:
47
47
# ensure that.
48
48
 
49
49
import httplib
 
50
try:
 
51
    import kerberos
 
52
except ImportError:
 
53
    have_kerberos = False
 
54
else:
 
55
    have_kerberos = True
50
56
import socket
51
57
import urllib
52
58
import urllib2
79
85
        self._report_activity(len(s), 'read')
80
86
        return s
81
87
 
82
 
    def readline(self, size=-1):
83
 
        s = self.filesock.readline(size)
 
88
    def readline(self):
 
89
        # This should be readline(self, size=-1), but httplib in python 2.4 and
 
90
        #  2.5 defines a SSLFile wrapper whose readline method lacks the size
 
91
        #  parameter.  So until we drop support for 2.4 and 2.5 and since we
 
92
        #  don't *need* the size parameter we'll stay with readline(self)
 
93
        #  --  vila 20090209
 
94
        s = self.filesock.readline()
84
95
        self._report_activity(len(s), 'read')
85
96
        return s
86
97
 
246
257
        # Preserve our preciousss
247
258
        sock = self.sock
248
259
        self.sock = None
249
 
        # Let httplib.HTTPConnection do its housekeeping 
 
260
        # Let httplib.HTTPConnection do its housekeeping
250
261
        self.close()
251
262
        # Restore our preciousss
252
263
        self.sock = sock
360
371
 
361
372
    def __init__(self, request):
362
373
        """Constructor
363
 
        
 
374
 
364
375
        :param request: the first request sent to the proxied host, already
365
376
            processed by the opener (i.e. proxied_host is already set).
366
377
        """
687
698
                        connect.proxied_host, self.host))
688
699
            # Housekeeping
689
700
            connection.cleanup_pipe()
690
 
            # Establish the connection encryption 
 
701
            # Establish the connection encryption
691
702
            connection.connect_to_origin()
692
703
            # Propagate the connection to the original request
693
704
            request.connection = connection
938
949
    preventively set authentication headers after the first
939
950
    successful authentication.
940
951
 
941
 
    This can be used for http and proxy, as well as for basic and
 
952
    This can be used for http and proxy, as well as for basic, negotiate and
942
953
    digest authentications.
943
954
 
944
955
    This provides an unified interface for all authentication handlers
1058
1069
        (digest's nonce is an example, digest's nonce_count is a
1059
1070
        *counter-example*). Such parameters must be updated by
1060
1071
        using the update_auth() method.
1061
 
        
 
1072
 
1062
1073
        :param header: The authentication header sent by the server.
1063
1074
        :param auth: The auth parameters already known. They may be
1064
1075
             updated.
1138
1149
    https_request = http_request # FIXME: Need test
1139
1150
 
1140
1151
 
 
1152
class NegotiateAuthHandler(AbstractAuthHandler):
 
1153
    """A authentication handler that handles WWW-Authenticate: Negotiate.
 
1154
 
 
1155
    At the moment this handler supports just Kerberos. In the future,
 
1156
    NTLM support may also be added.
 
1157
    """
 
1158
 
 
1159
    handler_order = 480
 
1160
 
 
1161
    def auth_match(self, header, auth):
 
1162
        scheme = header.lower()
 
1163
        if scheme != 'negotiate':
 
1164
            return False
 
1165
        self.update_auth(auth, 'scheme', scheme)
 
1166
        resp = self._auth_match_kerberos(auth)
 
1167
        if resp is None:
 
1168
            return False
 
1169
        # Optionally should try to authenticate using NTLM here
 
1170
        self.update_auth(auth, 'negotiate_response', resp)
 
1171
        return True
 
1172
 
 
1173
    def _auth_match_kerberos(self, auth):
 
1174
        """Try to create a GSSAPI response for authenticating against a host."""
 
1175
        if not have_kerberos:
 
1176
            return None
 
1177
        ret, vc = kerberos.authGSSClientInit("HTTP@%(host)s" % auth)
 
1178
        if ret < 1:
 
1179
            trace.warning('Unable to create GSSAPI context for %s: %d',
 
1180
                auth['host'], ret)
 
1181
            return None
 
1182
        ret = kerberos.authGSSClientStep(vc, "")
 
1183
        if ret < 0:
 
1184
            trace.mutter('authGSSClientStep failed: %d', ret)
 
1185
            return None
 
1186
        return kerberos.authGSSClientResponse(vc)
 
1187
 
 
1188
    def build_auth_header(self, auth, request):
 
1189
        return "Negotiate %s" % auth['negotiate_response']
 
1190
 
 
1191
    def auth_params_reusable(self, auth):
 
1192
        # If the auth scheme is known, it means a previous
 
1193
        # authentication was successful, all information is
 
1194
        # available, no further checks are needed.
 
1195
        return (auth.get('scheme', None) == 'negotiate' and
 
1196
                auth.get('negotiate_response', None) is not None)
 
1197
 
 
1198
 
1141
1199
class BasicAuthHandler(AbstractAuthHandler):
1142
1200
    """A custom basic authentication handler."""
1143
1201
 
1363
1421
    """Custom proxy basic authentication handler"""
1364
1422
 
1365
1423
 
 
1424
class HTTPNegotiateAuthHandler(NegotiateAuthHandler, HTTPAuthHandler):
 
1425
    """Custom http negotiate authentication handler"""
 
1426
 
 
1427
 
 
1428
class ProxyNegotiateAuthHandler(NegotiateAuthHandler, ProxyAuthHandler):
 
1429
    """Custom proxy negotiate authentication handler"""
 
1430
 
 
1431
 
1366
1432
class HTTPErrorProcessor(urllib2.HTTPErrorProcessor):
1367
1433
    """Process HTTP error responses.
1368
1434
 
1427
1493
            ProxyHandler(),
1428
1494
            HTTPBasicAuthHandler(),
1429
1495
            HTTPDigestAuthHandler(),
 
1496
            HTTPNegotiateAuthHandler(),
1430
1497
            ProxyBasicAuthHandler(),
1431
1498
            ProxyDigestAuthHandler(),
 
1499
            ProxyNegotiateAuthHandler(),
1432
1500
            HTTPHandler,
1433
1501
            HTTPSHandler,
1434
1502
            HTTPDefaultErrorHandler,