/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 breezy/transport/http/_urllib2_wrappers.py

  • Committer: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2018-11-16 18:59:44 UTC
  • mfrom: (7143.15.15 more-cleanups)
  • Revision ID: breezy.the.bot@gmail.com-20181116185944-biefv1sub37qfybm
Sprinkle some PEP8iness.

Merged from https://code.launchpad.net/~jelmer/brz/more-cleanups/+merge/358611

Show diffs side-by-side

added added

removed removed

Lines of Context:
156
156
    else:
157
157
        return u'required'
158
158
 
 
159
 
159
160
opt_ssl_ca_certs = config.Option('ssl.ca_certs',
160
161
                                 from_unicode=ca_certs_from_store,
161
162
                                 default=default_ca_certs,
386
387
                pending = self._response.finish()
387
388
                # Warn the user (once)
388
389
                if (self._ranges_received_whole_file is None
389
 
                    and self._response.status == 200
390
 
                    and pending and pending > self._range_warning_thresold
 
390
                        and self._response.status == 200
 
391
                        and pending and pending > self._range_warning_thresold
391
392
                    ):
392
393
                    self._ranges_received_whole_file = True
393
394
                    trace.warning(
400
401
                # cleaning anymore, so no need to fail, we just get rid of the
401
402
                # socket and let callers reconnect
402
403
                if (len(e.args) == 0
403
 
                    or e.args[0] not in (errno.ECONNRESET, errno.ECONNABORTED)):
 
404
                        or e.args[0] not in (errno.ECONNRESET, errno.ECONNABORTED)):
404
405
                    raise
405
406
                self.close()
406
407
            self._response = None
446
447
        AbstractHTTPConnection.__init__(self, report_activity=report_activity)
447
448
        if PY3:
448
449
            http_client.HTTPSConnection.__init__(
449
 
                    self, host, port, key_file, cert_file)
 
450
                self, host, port, key_file, cert_file)
450
451
        else:
451
452
            # Use strict=True since we don't support HTTP/0.9
452
453
            http_client.HTTPSConnection.__init__(self, host, port,
453
 
                                             key_file, cert_file, strict=True)
 
454
                                                 key_file, cert_file, strict=True)
454
455
        self.proxied_host = proxied_host
455
456
        self.ca_certs = ca_certs
456
457
 
523
524
                 connection=None, parent=None,
524
525
                 accepted_errors=None):
525
526
        urllib_request.Request.__init__(
526
 
                self, url, data, headers,
527
 
                origin_req_host, unverifiable)
 
527
            self, url, data, headers,
 
528
            origin_req_host, unverifiable)
528
529
        self.method = method
529
530
        self.connection = connection
530
531
        self.accepted_errors = accepted_errors
613
614
    http[s] requests in AbstractHTTPHandler.
614
615
    """
615
616
 
616
 
    handler_order = 1000 # after all pre-processings
 
617
    handler_order = 1000  # after all pre-processings
617
618
 
618
619
    def __init__(self, report_activity=None, ca_certs=None):
619
620
        self._report_activity = report_activity
636
637
        except http_client.InvalidURL as exception:
637
638
            # There is only one occurrence of InvalidURL in http_client
638
639
            raise urlutils.InvalidURL(request.get_full_url(),
639
 
                                    extra='nonnumeric port')
 
640
                                      extra='nonnumeric port')
640
641
 
641
642
        return connection
642
643
 
775
776
                    else:
776
777
                        selector = request.get_selector()
777
778
                    my_exception = errors.ConnectionError(
778
 
                        msg= 'while sending %s %s:' % (request.get_method(),
779
 
                                                       selector),
 
779
                        msg='while sending %s %s:' % (request.get_method(),
 
780
                                                      selector),
780
781
                        orig_error=exc_val)
781
782
 
782
783
                if self._debuglevel >= 2:
819
820
            if sys.version_info[:2] >= (3, 6):
820
821
                connection._send_request(method, url,
821
822
                                         # FIXME: implements 100-continue
822
 
                                         #None, # We don't send the body yet
 
823
                                         # None, # We don't send the body yet
823
824
                                         request.data,
824
825
                                         headers, encode_chunked=False)
825
826
            else:
826
827
                connection._send_request(method, url,
827
828
                                         # FIXME: implements 100-continue
828
 
                                         #None, # We don't send the body yet
 
829
                                         # None, # We don't send the body yet
829
830
                                         request.data,
830
831
                                         headers)
831
832
            if 'http' in debug.debug_flags:
839
840
                    hdrs.append('%s: %s' % (k, v))
840
841
                trace.mutter('> ' + '\n> '.join(hdrs) + '\n')
841
842
            if self._debuglevel >= 1:
842
 
                print('Request sent: [%r] from (%s)' \
843
 
                    % (request, request.connection.sock.getsockname()))
 
843
                print('Request sent: [%r] from (%s)'
 
844
                      % (request, request.connection.sock.getsockname()))
844
845
            response = connection.getresponse()
845
846
            convert_to_addinfourl = True
846
847
        except (ssl.SSLError, ssl.CertificateError):
921
922
        connection = request.connection
922
923
        if connection.sock is None and \
923
924
                connection.proxied_host is not None and \
924
 
                request.get_method() != 'CONNECT' : # Don't loop
 
925
                request.get_method() != 'CONNECT':  # Don't loop
925
926
            # FIXME: We need a gazillion connection tests here, but we still
926
927
            # miss a https server :-( :
927
928
            # - with and without proxy
940
941
            response = self.parent.open(connect)
941
942
            if response.code != 200:
942
943
                raise errors.ConnectionError("Can't connect to %s via proxy %s" % (
943
 
                        connect.proxied_host, self.host))
 
944
                    connect.proxied_host, self.host))
944
945
            # Housekeeping
945
946
            connection.cleanup_pipe()
946
947
            # Establish the connection encryption
949
950
            request.connection = connection
950
951
        return self.do_open(HTTPSConnection, request)
951
952
 
 
953
 
952
954
class HTTPRedirectHandler(urllib_request.HTTPRedirectHandler):
953
955
    """Handles redirect requests.
954
956
 
1002
1004
 
1003
1005
        if code in (301, 302, 303, 307):
1004
1006
            return Request(req.get_method(), newurl,
1005
 
                           headers = req.headers,
1006
 
                           origin_req_host = origin_req_host,
1007
 
                           unverifiable = True,
 
1007
                           headers=req.headers,
 
1008
                           origin_req_host=origin_req_host,
 
1009
                           unverifiable=True,
1008
1010
                           # TODO: It will be nice to be able to
1009
1011
                           # detect virtual hosts sharing the same
1010
1012
                           # IP address, that will allow us to
1011
1013
                           # share the same connection...
1012
 
                           connection = None,
1013
 
                           parent = req,
 
1014
                           connection=None,
 
1015
                           parent=req,
1014
1016
                           )
1015
1017
        else:
1016
 
            raise urllib_request.HTTPError(req.get_full_url(), code, msg, headers, fp)
 
1018
            raise urllib_request.HTTPError(
 
1019
                req.get_full_url(), code, msg, headers, fp)
1017
1020
 
1018
1021
    def http_error_302(self, req, fp, code, msg, headers):
1019
1022
        """Requests the redirected to URI.
1054
1057
        if hasattr(req, 'redirect_dict'):
1055
1058
            visited = redirected_req.redirect_dict = req.redirect_dict
1056
1059
            if (visited.get(newurl, 0) >= self.max_repeats or
1057
 
                len(visited) >= self.max_redirections):
 
1060
                    len(visited) >= self.max_redirections):
1058
1061
                raise urllib_request.HTTPError(req.get_full_url(), code,
1059
 
                                        self.inf_msg + msg, headers, fp)
 
1062
                                               self.inf_msg + msg, headers, fp)
1060
1063
        else:
1061
1064
            visited = redirected_req.redirect_dict = req.redirect_dict = {}
1062
1065
        visited[newurl] = visited.get(newurl, 0) + 1
1108
1111
            if self._debuglevel >= 3:
1109
1112
                print('Will bind %s for %r' % (scheme_request, proxy))
1110
1113
            setattr(self, scheme_request,
1111
 
                lambda request: self.set_proxy(request, scheme))
 
1114
                    lambda request: self.set_proxy(request, scheme))
1112
1115
        # We are interested only by the http[s] proxies
1113
1116
        http_proxy = self.get_proxy_env_var('http')
1114
1117
        bind_scheme_request(http_proxy, 'http')
1204
1207
            # proxied request, intialize.  scheme (the authentication scheme)
1205
1208
            # and realm will be set by the AuthHandler
1206
1209
            request.proxy_auth = {
1207
 
                                  'host': parsed_url.host,
1208
 
                                  'port': parsed_url.port,
1209
 
                                  'user': parsed_url.user,
1210
 
                                  'password': parsed_url.password,
1211
 
                                  'protocol': parsed_url.scheme,
1212
 
                                   # We ignore path since we connect to a proxy
1213
 
                                  'path': None}
 
1210
                'host': parsed_url.host,
 
1211
                'port': parsed_url.port,
 
1212
                'user': parsed_url.user,
 
1213
                'password': parsed_url.password,
 
1214
                'protocol': parsed_url.scheme,
 
1215
                # We ignore path since we connect to a proxy
 
1216
                'path': None}
1214
1217
        if parsed_url.port is None:
1215
1218
            phost = parsed_url.host
1216
1219
        else:
1348
1351
                # auth_match may have modified auth (by adding the
1349
1352
                # password or changing the realm, for example)
1350
1353
                if (request.get_header(self.auth_header, None) is not None
1351
 
                    and not auth['modified']):
 
1354
                        and not auth['modified']):
1352
1355
                    # We already tried that, give up
1353
1356
                    return None
1354
1357
 
1363
1366
                    # the credentials are wrong (or incomplete), but we know
1364
1367
                    # that the associated scheme should be used.
1365
1368
                    best_scheme = auth['best_scheme'] = self.scheme
1366
 
                if  best_scheme != self.scheme:
 
1369
                if best_scheme != self.scheme:
1367
1370
                    continue
1368
1371
 
1369
1372
                if self.requires_username and auth.get('user', None) is None:
1497
1500
        """Insert an authentication header if information is available"""
1498
1501
        auth = self.get_auth(request)
1499
1502
        if self.auth_params_reusable(auth):
1500
 
            self.add_auth_header(request, self.build_auth_header(auth, request))
 
1503
            self.add_auth_header(
 
1504
                request, self.build_auth_header(auth, request))
1501
1505
        return request
1502
1506
 
1503
 
    https_request = http_request # FIXME: Need test
 
1507
    https_request = http_request  # FIXME: Need test
1504
1508
 
1505
1509
 
1506
1510
class NegotiateAuthHandler(AbstractAuthHandler):
1540
1544
        ret, vc = kerberos.authGSSClientInit("HTTP@%(host)s" % auth)
1541
1545
        if ret < 1:
1542
1546
            trace.warning('Unable to create GSSAPI context for %s: %d',
1543
 
                auth['host'], ret)
 
1547
                          auth['host'], ret)
1544
1548
            return None
1545
1549
        ret = kerberos.authGSSClientStep(vc, "")
1546
1550
        if ret < 0:
1568
1572
 
1569
1573
    def build_auth_header(self, auth, request):
1570
1574
        raw = '%s:%s' % (auth['user'], auth['password'])
1571
 
        auth_header = 'Basic ' + base64.b64encode(raw.encode('utf-8')).decode('ascii')
 
1575
        auth_header = 'Basic ' + \
 
1576
            base64.b64encode(raw.encode('utf-8')).decode('ascii')
1572
1577
        return auth_header
1573
1578
 
1574
1579
    def extract_realm(self, header_value):
1589
1594
            self.update_auth(auth, 'scheme', scheme)
1590
1595
            self.update_auth(auth, 'realm', realm)
1591
1596
            if (auth.get('user', None) is None
1592
 
                or auth.get('password', None) is None):
 
1597
                    or auth.get('password', None) is None):
1593
1598
                user, password = self.get_user_password(auth)
1594
1599
                self.update_auth(auth, 'user', user)
1595
1600
                self.update_auth(auth, 'password', password)
1606
1611
    H = None
1607
1612
    KD = None
1608
1613
    if algorithm == 'MD5':
1609
 
        H = lambda x: osutils.md5(x).hexdigest()
 
1614
        def H(x): return osutils.md5(x).hexdigest()
1610
1615
    elif algorithm == 'SHA':
1611
1616
        H = osutils.sha_string
1612
1617
    if H is not None:
1613
 
        KD = lambda secret, data: H(("%s:%s" % (secret, data)).encode('utf-8'))
 
1618
        def KD(secret, data): return H(
 
1619
            ("%s:%s" % (secret, data)).encode('utf-8'))
1614
1620
    return H, KD
1615
1621
 
1616
1622
 
1639
1645
            return False
1640
1646
 
1641
1647
        # Put the requested authentication info into a dict
1642
 
        req_auth = urllib_request.parse_keqv_list(urllib_request.parse_http_list(raw_auth))
 
1648
        req_auth = urllib_request.parse_keqv_list(
 
1649
            urllib_request.parse_http_list(raw_auth))
1643
1650
 
1644
1651
        # Check that we can handle that authentication
1645
1652
        qop = req_auth.get('qop', None)
1646
 
        if qop != 'auth': # No auth-int so far
 
1653
        if qop != 'auth':  # No auth-int so far
1647
1654
            return False
1648
1655
 
1649
1656
        H, KD = get_digest_algorithm_impls(req_auth.get('algorithm', 'MD5'))
1683
1690
        url_scheme, url_selector = splittype(selector)
1684
1691
        sel_host, uri = splithost(url_selector)
1685
1692
 
1686
 
        A1 = ('%s:%s:%s' % (auth['user'], auth['realm'], auth['password'])).encode('utf-8')
 
1693
        A1 = ('%s:%s:%s' %
 
1694
              (auth['user'], auth['realm'], auth['password'])).encode('utf-8')
1687
1695
        A2 = ('%s:%s' % (request.get_method(), uri)).encode('utf-8')
1688
1696
 
1689
1697
        nonce = auth['nonce']
1814
1822
    instead, we leave our Transport handle them.
1815
1823
    """
1816
1824
 
1817
 
    accepted_errors = [200, # Ok
1818
 
                       206, # Partial content
1819
 
                       404, # Not found
 
1825
    accepted_errors = [200,  # Ok
 
1826
                       206,  # Partial content
 
1827
                       404,  # Not found
1820
1828
                       ]
1821
1829
    """The error codes the caller will handle.
1822
1830