/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

  • Committer: Vincent Ladeuil
  • Date: 2011-07-06 09:22:00 UTC
  • mfrom: (6008 +trunk)
  • mto: (6012.1.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 6013.
  • Revision ID: v.ladeuil+lp@free.fr-20110706092200-7iai2mwzc0sqdsvf
MergingĀ inĀ trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
110
110
        self.report_activity(len(s), 'read')
111
111
        return s
112
112
 
113
 
    # httplib in python 2.4 and 2.5 defines a SSLFile wrapper whose readline
114
 
    # method lacks the size parameter. python2.6 provides a proper ssl socket
115
 
    # and added it. python2.7 uses it, forcing us to provide it.
116
 
    if sys.version_info < (2, 6):
117
 
        def readline(self):
118
 
            s = self.filesock.readline()
119
 
            self.report_activity(len(s), 'read')
120
 
            return s
121
 
    else:
122
 
        def readline(self, size=-1):
123
 
            s = self.filesock.readline(size)
124
 
            self.report_activity(len(s), 'read')
125
 
            return s
 
113
    def readline(self, size=-1):
 
114
        s = self.filesock.readline(size)
 
115
        self.report_activity(len(s), 'read')
 
116
        return s
126
117
 
127
118
    def __getattr__(self, name):
128
119
        return getattr(self.filesock, name)
657
648
                                     headers)
658
649
            if 'http' in debug.debug_flags:
659
650
                trace.mutter('> %s %s' % (method, url))
660
 
                hdrs = ['%s: %s' % (k, v) for k,v in headers.items()]
 
651
                hdrs = []
 
652
                for k,v in headers.iteritems():
 
653
                    # People are often told to paste -Dhttp output to help
 
654
                    # debug. Don't compromise credentials.
 
655
                    if k in ('Authorization', 'Proxy-Authorization'):
 
656
                        v = '<masked>'
 
657
                    hdrs.append('%s: %s' % (k, v))
661
658
                trace.mutter('> ' + '\n> '.join(hdrs) + '\n')
662
659
            if self._debuglevel >= 1:
663
660
                print 'Request sent: [%r] from (%s)' \
942
939
        return None
943
940
 
944
941
    def proxy_bypass(self, host):
945
 
        """Check if host should be proxied or not"""
 
942
        """Check if host should be proxied or not.
 
943
 
 
944
        :returns: True to skip the proxy, False otherwise.
 
945
        """
946
946
        no_proxy = self.get_proxy_env_var('no', default_to=None)
 
947
        bypass = self.evaluate_proxy_bypass(host, no_proxy)
 
948
        if bypass is None:
 
949
            # Nevertheless, there are platform-specific ways to
 
950
            # ignore proxies...
 
951
            return urllib.proxy_bypass(host)
 
952
        else:
 
953
            return bypass
 
954
 
 
955
    def evaluate_proxy_bypass(self, host, no_proxy):
 
956
        """Check the host against a comma-separated no_proxy list as a string.
 
957
 
 
958
        :param host: ``host:port`` being requested
 
959
 
 
960
        :param no_proxy: comma-separated list of hosts to access directly.
 
961
 
 
962
        :returns: True to skip the proxy, False not to, or None to
 
963
            leave it to urllib.
 
964
        """
947
965
        if no_proxy is None:
 
966
            # All hosts are proxied
948
967
            return False
949
968
        hhost, hport = urllib.splitport(host)
950
969
        # Does host match any of the domains mentioned in
952
971
        # are fuzzy (to say the least). We try to allow most
953
972
        # commonly seen values.
954
973
        for domain in no_proxy.split(','):
 
974
            domain = domain.strip()
 
975
            if domain == '':
 
976
                continue
955
977
            dhost, dport = urllib.splitport(domain)
956
978
            if hport == dport or dport is None:
957
979
                # Protect glob chars
960
982
                dhost = dhost.replace("?", r".")
961
983
                if re.match(dhost, hhost, re.IGNORECASE):
962
984
                    return True
963
 
        # Nevertheless, there are platform-specific ways to
964
 
        # ignore proxies...
965
 
        return urllib.proxy_bypass(host)
 
985
        # Nothing explicitly avoid the host
 
986
        return None
966
987
 
967
988
    def set_proxy(self, request, type):
968
989
        if self.proxy_bypass(request.get_host()):
1245
1266
        user. The daughter classes should implements a public
1246
1267
        build_password_prompt using this method.
1247
1268
        """
1248
 
        prompt = '%s' % auth['protocol'].upper() + ' %(user)s@%(host)s'
 
1269
        prompt = u'%s' % auth['protocol'].upper() + u' %(user)s@%(host)s'
1249
1270
        realm = auth['realm']
1250
1271
        if realm is not None:
1251
 
            prompt += ", Realm: '%s'" % realm
1252
 
        prompt += ' password'
 
1272
            prompt += u", Realm: '%s'" % realm.decode('utf8')
 
1273
        prompt += u' password'
1253
1274
        return prompt
1254
1275
 
1255
1276
    def _build_username_prompt(self, auth):
1263
1284
        user. The daughter classes should implements a public
1264
1285
        build_username_prompt using this method.
1265
1286
        """
1266
 
        prompt = '%s' % auth['protocol'].upper() + ' %(host)s'
 
1287
        prompt = u'%s' % auth['protocol'].upper() + u' %(host)s'
1267
1288
        realm = auth['realm']
1268
1289
        if realm is not None:
1269
 
            prompt += ", Realm: '%s'" % realm
1270
 
        prompt += ' username'
 
1290
            prompt += u", Realm: '%s'" % realm.decode('utf8')
 
1291
        prompt += u' username'
1271
1292
        return prompt
1272
1293
 
1273
1294
    def http_request(self, request):
1378
1399
    if algorithm == 'MD5':
1379
1400
        H = lambda x: osutils.md5(x).hexdigest()
1380
1401
    elif algorithm == 'SHA':
1381
 
        H = lambda x: osutils.sha(x).hexdigest()
 
1402
        H = osutils.sha_string
1382
1403
    if H is not None:
1383
1404
        KD = lambda secret, data: H("%s:%s" % (secret, data))
1384
1405
    return H, KD
1387
1408
def get_new_cnonce(nonce, nonce_count):
1388
1409
    raw = '%s:%d:%s:%s' % (nonce, nonce_count, time.ctime(),
1389
1410
                           urllib2.randombytes(8))
1390
 
    return osutils.sha(raw).hexdigest()[:16]
 
1411
    return osutils.sha_string(raw)[:16]
1391
1412
 
1392
1413
 
1393
1414
class DigestAuthHandler(AbstractAuthHandler):
1537
1558
 
1538
1559
    def build_password_prompt(self, auth):
1539
1560
        prompt = self._build_password_prompt(auth)
1540
 
        prompt = 'Proxy ' + prompt
 
1561
        prompt = u'Proxy ' + prompt
1541
1562
        return prompt
1542
1563
 
1543
1564
    def build_username_prompt(self, auth):
1544
1565
        prompt = self._build_username_prompt(auth)
1545
 
        prompt = 'Proxy ' + prompt
 
1566
        prompt = u'Proxy ' + prompt
1546
1567
        return prompt
1547
1568
 
1548
1569
    def http_error_407(self, req, fp, code, msg, headers):