/breezy/unstable

To get this branch, use:
bzr branch https://code.breezy-vcs.org/breezy/unstable

« back to all changes in this revision

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

  • Committer: Jelmer Vernooij
  • Date: 2017-05-24 01:39:33 UTC
  • mfrom: (3815.3776.6)
  • Revision ID: jelmer@jelmer.uk-20170524013933-ir4y4tqtrsiz2ka2
New upstream snapshot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2013, 2016 Canonical Ltd
 
1
# Copyright (C) 2006-2013, 2016, 2017 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
48
48
# actual code more or less do that, tests should be written to
49
49
# ensure that.
50
50
 
 
51
import base64
51
52
import errno
52
53
import httplib
53
54
import os
60
61
import sys
61
62
import time
62
63
 
63
 
from bzrlib import __version__ as bzrlib_version
64
 
from bzrlib import (
 
64
from ... import __version__ as breezy_version
 
65
from ... import (
65
66
    config,
66
67
    debug,
67
68
    errors,
72
73
    ui,
73
74
    urlutils,
74
75
)
 
76
from ...sixish import (
 
77
    reraise,
 
78
)
75
79
 
76
80
try:
77
81
    _ = (ssl.match_hostname, ssl.CertificateError)
273
277
        httplib.HTTPResponse.begin(self)
274
278
        if self.status in self._body_ignored_responses:
275
279
            if self.debuglevel >= 2:
276
 
                print "For status: [%s]," % self.status,
277
 
                print "will ready body, length: %s" % self.length
 
280
                print("For status: [%s], will ready body, length: %s" % (
 
281
                    self.status, self.length))
278
282
            if not (self.length is None or self.will_close):
279
283
                # In some cases, we just can't read the body not
280
284
                # even try or we may encounter a 104, 'Connection
285
289
                body = self.read(self.length)
286
290
                if self.debuglevel >= 9:
287
291
                    # This one can be huge and is generally not interesting
288
 
                    print "Consumed body: [%s]" % body
 
292
                    print("Consumed body: [%s]" % body)
289
293
            self.close()
290
294
        elif self.status == 200:
291
295
            # Whatever the request is, it went ok, so we surely don't want to
367
371
                        'Got a 200 response when asking for multiple ranges,'
368
372
                        ' does your server at %s:%s support range requests?',
369
373
                        self.host, self.port)
370
 
            except socket.error, e:
 
374
            except socket.error as e:
371
375
                # It's conceivable that the socket is in a bad state here
372
376
                # (including some test cases) and in this case, it doesn't need
373
377
                # cleaning anymore, so no need to fail, we just get rid of the
591
595
                host, proxied_host=request.proxied_host,
592
596
                report_activity=self._report_activity,
593
597
                ca_certs=self.ca_certs)
594
 
        except httplib.InvalidURL, exception:
 
598
        except httplib.InvalidURL as exception:
595
599
            # There is only one occurrence of InvalidURL in httplib
596
600
            raise errors.InvalidURL(request.get_full_url(),
597
601
                                    extra='nonnumeric port')
645
649
    _default_headers = {'Pragma': 'no-cache',
646
650
                        'Cache-control': 'max-age=0',
647
651
                        'Connection': 'Keep-Alive',
648
 
                        'User-agent': 'bzr/%s (urllib)' % bzrlib_version,
 
652
                        'User-agent': 'bzr/%s (urllib)' % breezy_version,
649
653
                        'Accept': '*/*',
650
654
                        }
651
655
 
683
687
        elif isinstance(exc_val, httplib.ImproperConnectionState):
684
688
            # The httplib pipeline is in incorrect state, it's a bug in our
685
689
            # implementation.
686
 
            raise exc_type, exc_val, exc_tb
 
690
            reraise(exc_type, exc_val, exc_tb)
687
691
        else:
688
692
            if first_try:
689
693
                if self._debuglevel >= 2:
690
 
                    print 'Received exception: [%r]' % exc_val
691
 
                    print '  On connection: [%r]' % request.connection
 
694
                    print('Received exception: [%r]' % exc_val)
 
695
                    print('  On connection: [%r]' % request.connection)
692
696
                    method = request.get_method()
693
697
                    url = request.get_full_url()
694
 
                    print '  Will retry, %s %r' % (method, url)
 
698
                    print('  Will retry, %s %r' % (method, url))
695
699
                request.connection.close()
696
700
                response = self.do_open(http_class, request, False)
697
701
            else:
698
702
                if self._debuglevel >= 2:
699
 
                    print 'Received second exception: [%r]' % exc_val
700
 
                    print '  On connection: [%r]' % request.connection
 
703
                    print('Received second exception: [%r]' % exc_val)
 
704
                    print('  On connection: [%r]' % request.connection)
701
705
                if exc_type in (httplib.BadStatusLine, httplib.UnknownProtocol):
702
706
                    # httplib.BadStatusLine and
703
707
                    # httplib.UnknownProtocol indicates that a
729
733
                        orig_error=exc_val)
730
734
 
731
735
                if self._debuglevel >= 2:
732
 
                    print 'On connection: [%r]' % request.connection
 
736
                    print('On connection: [%r]' % request.connection)
733
737
                    method = request.get_method()
734
738
                    url = request.get_full_url()
735
 
                    print '  Failed again, %s %r' % (method, url)
736
 
                    print '  Will raise: [%r]' % my_exception
737
 
                raise my_exception, None, exc_tb
 
739
                    print('  Failed again, %s %r' % (method, url))
 
740
                    print('  Will raise: [%r]' % my_exception)
 
741
                reraise(my_exception, None, exc_tb)
738
742
        return response
739
743
 
740
744
    def do_open(self, http_class, request, first_try=True):
778
782
                    hdrs.append('%s: %s' % (k, v))
779
783
                trace.mutter('> ' + '\n> '.join(hdrs) + '\n')
780
784
            if self._debuglevel >= 1:
781
 
                print 'Request sent: [%r] from (%s)' \
782
 
                    % (request, request.connection.sock.getsockname())
 
785
                print('Request sent: [%r] from (%s)' \
 
786
                    % (request, request.connection.sock.getsockname()))
783
787
            response = connection.getresponse()
784
788
            convert_to_addinfourl = True
785
789
        except (ssl.SSLError, ssl.CertificateError):
804
808
#            response = connection.getresponse()
805
809
 
806
810
        if self._debuglevel >= 2:
807
 
            print 'Receives response: %r' % response
808
 
            print '  For: %r(%r)' % (request.get_method(),
809
 
                                     request.get_full_url())
 
811
            print('Receives response: %r' % response)
 
812
            print('  For: %r(%r)' % (request.get_method(),
 
813
                                     request.get_full_url()))
810
814
 
811
815
        if convert_to_addinfourl:
812
816
            # Shamelessly copied from urllib2
819
823
            resp.msg = r.reason
820
824
            resp.version = r.version
821
825
            if self._debuglevel >= 2:
822
 
                print 'Create addinfourl: %r' % resp
823
 
                print '  For: %r(%r)' % (request.get_method(),
824
 
                                         request.get_full_url())
 
826
                print('Create addinfourl: %r' % resp)
 
827
                print('  For: %r(%r)' % (request.get_method(),
 
828
                                         request.get_full_url()))
825
829
            if 'http' in debug.debug_flags:
826
830
                version = 'HTTP/%d.%d'
827
831
                try:
965
969
        else:
966
970
            return
967
971
        if self._debuglevel >= 1:
968
 
            print 'Redirected to: %s (followed: %r)' % (newurl,
969
 
                                                        req.follow_redirections)
 
972
            print('Redirected to: %s (followed: %r)' % (newurl,
 
973
                                                        req.follow_redirections))
970
974
        if req.follow_redirections is False:
971
975
            req.redirected_to = newurl
972
976
            return fp
1028
1032
        # First, let's get rid of urllib2 implementation
1029
1033
        for type, proxy in self.proxies.items():
1030
1034
            if self._debuglevel >= 3:
1031
 
                print 'Will unbind %s_open for %r' % (type, proxy)
 
1035
                print('Will unbind %s_open for %r' % (type, proxy))
1032
1036
            delattr(self, '%s_open' % type)
1033
1037
 
1034
1038
        def bind_scheme_request(proxy, scheme):
1036
1040
                return
1037
1041
            scheme_request = scheme + '_request'
1038
1042
            if self._debuglevel >= 3:
1039
 
                print 'Will bind %s for %r' % (scheme_request, proxy)
 
1043
                print('Will bind %s for %r' % (scheme_request, proxy))
1040
1044
            setattr(self, scheme_request,
1041
1045
                lambda request: self.set_proxy(request, scheme))
1042
1046
        # We are interested only by the http[s] proxies
1116
1120
 
1117
1121
        proxy = self.get_proxy_env_var(type)
1118
1122
        if self._debuglevel >= 3:
1119
 
            print 'set_proxy %s_request for %r' % (type, proxy)
 
1123
            print('set_proxy %s_request for %r' % (type, proxy))
1120
1124
        # FIXME: python 2.5 urlparse provides a better _parse_proxy which can
1121
1125
        # grok user:password@host:port as well as
1122
1126
        # http://user:password@host:port
1143
1147
            phost = parsed_url.host + ':%d' % parsed_url.port
1144
1148
        request.set_proxy(phost, type)
1145
1149
        if self._debuglevel >= 3:
1146
 
            print 'set_proxy: proxy set to %s://%s' % (type, phost)
 
1150
            print('set_proxy: proxy set to %s://%s' % (type, phost))
1147
1151
        return request
1148
1152
 
1149
1153
 
1491
1495
 
1492
1496
    def build_auth_header(self, auth, request):
1493
1497
        raw = '%s:%s' % (auth['user'], auth['password'])
1494
 
        auth_header = 'Basic ' + raw.encode('base64').strip()
 
1498
        auth_header = 'Basic ' + base64.b64encode(raw)
1495
1499
        return auth_header
1496
1500
 
1497
1501
    def extract_realm(self, header_value):