~ubuntu-branches/debian/stretch/electrum/stretch

« back to all changes in this revision

Viewing changes to packages/requests/packages/urllib3/connectionpool.py

  • Committer: Package Import Robot
  • Author(s): Tristan Seligmann
  • Date: 2015-10-23 21:00:40 UTC
  • mfrom: (1.1.6)
  • Revision ID: package-import@ubuntu.com-20151023210040-4ej97tguu585zwlc
Tags: 2.5.1-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
    ClosedPoolError,
18
18
    ProtocolError,
19
19
    EmptyPoolError,
 
20
    HeaderParsingError,
20
21
    HostChangedError,
21
22
    LocationValueError,
22
23
    MaxRetryError,
23
24
    ProxyError,
 
25
    ConnectTimeoutError,
24
26
    ReadTimeoutError,
25
27
    SSLError,
26
28
    TimeoutError,
27
29
    InsecureRequestWarning,
 
30
    NewConnectionError,
28
31
)
29
32
from .packages.ssl_match_hostname import CertificateError
30
33
from .packages import six
38
41
from .response import HTTPResponse
39
42
 
40
43
from .util.connection import is_connection_dropped
 
44
from .util.response import assert_header_parsing
41
45
from .util.retry import Retry
42
46
from .util.timeout import Timeout
43
 
from .util.url import get_host
 
47
from .util.url import get_host, Url
44
48
 
45
49
 
46
50
xrange = six.moves.xrange
72
76
        return '%s(host=%r, port=%r)' % (type(self).__name__,
73
77
                                         self.host, self.port)
74
78
 
 
79
    def __enter__(self):
 
80
        return self
 
81
 
 
82
    def __exit__(self, exc_type, exc_val, exc_tb):
 
83
        self.close()
 
84
        # Return False to re-raise any potential exceptions
 
85
        return False
 
86
 
 
87
    def close():
 
88
        """
 
89
        Close all pooled connections and disable the pool.
 
90
        """
 
91
        pass
 
92
 
 
93
 
75
94
# This is taken from http://hg.python.org/cpython/file/7aaba721ebc0/Lib/socket.py#l252
76
95
_blocking_errnos = set([errno.EAGAIN, errno.EWOULDBLOCK])
77
96
 
105
124
 
106
125
    :param maxsize:
107
126
        Number of connections to save that can be reused. More than 1 is useful
108
 
        in multithreaded situations. If ``block`` is set to false, more
 
127
        in multithreaded situations. If ``block`` is set to False, more
109
128
        connections will be created but they will not be saved once they've
110
129
        been used.
111
130
 
266
285
        """
267
286
        pass
268
287
 
 
288
    def _prepare_proxy(self, conn):
 
289
        # Nothing to do for HTTP connections.
 
290
        pass
 
291
 
269
292
    def _get_timeout(self, timeout):
270
293
        """ Helper that always returns a :class:`urllib3.util.Timeout` """
271
294
        if timeout is _Default:
349
372
 
350
373
        # Receive the response from the server
351
374
        try:
352
 
            try:  # Python 2.7+, use buffering of HTTP responses
 
375
            try:  # Python 2.7, use buffering of HTTP responses
353
376
                httplib_response = conn.getresponse(buffering=True)
354
377
            except TypeError:  # Python 2.6 and older
355
378
                httplib_response = conn.getresponse()
362
385
        log.debug("\"%s %s %s\" %s %s" % (method, url, http_version,
363
386
                                          httplib_response.status,
364
387
                                          httplib_response.length))
 
388
 
 
389
        try:
 
390
            assert_header_parsing(httplib_response.msg)
 
391
        except HeaderParsingError as hpe:  # Platform-specific: Python 3
 
392
            log.warning(
 
393
                'Failed to parse headers (url=%s): %s',
 
394
                self._absolute_url(url), hpe, exc_info=True)
 
395
 
365
396
        return httplib_response
366
397
 
 
398
    def _absolute_url(self, path):
 
399
        return Url(scheme=self.scheme, host=self.host, port=self.port, path=path).url
 
400
 
367
401
    def close(self):
368
402
        """
369
403
        Close all pooled connections and disable the pool.
510
544
 
511
545
        try:
512
546
            # Request a connection from the queue.
 
547
            timeout_obj = self._get_timeout(timeout)
513
548
            conn = self._get_conn(timeout=pool_timeout)
514
549
 
 
550
            conn.timeout = timeout_obj.connect_timeout
 
551
 
 
552
            is_new_proxy_conn = self.proxy is not None and not getattr(conn, 'sock', None)
 
553
            if is_new_proxy_conn:
 
554
                self._prepare_proxy(conn)
 
555
 
515
556
            # Make the request on the httplib connection object.
516
557
            httplib_response = self._make_request(conn, method, url,
517
 
                                                  timeout=timeout,
 
558
                                                  timeout=timeout_obj,
518
559
                                                  body=body, headers=headers)
519
560
 
520
561
            # If we're going to release the connection in ``finally:``, then
542
583
            # Close the connection. If a connection is reused on which there
543
584
            # was a Certificate error, the next request will certainly raise
544
585
            # another Certificate error.
545
 
            if conn:
546
 
                conn.close()
547
 
                conn = None
 
586
            conn = conn and conn.close()
 
587
            release_conn = True
548
588
            raise SSLError(e)
549
589
 
550
 
        except (TimeoutError, HTTPException, SocketError, ConnectionError) as e:
551
 
            if conn:
552
 
                # Discard the connection for these exceptions. It will be
553
 
                # be replaced during the next _get_conn() call.
554
 
                conn.close()
555
 
                conn = None
556
 
 
557
 
            stacktrace = sys.exc_info()[2]
558
 
            if isinstance(e, SocketError) and self.proxy:
 
590
        except SSLError:
 
591
            # Treat SSLError separately from BaseSSLError to preserve
 
592
            # traceback.
 
593
            conn = conn and conn.close()
 
594
            release_conn = True
 
595
            raise
 
596
 
 
597
        except (TimeoutError, HTTPException, SocketError, ProtocolError) as e:
 
598
            # Discard the connection for these exceptions. It will be
 
599
            # be replaced during the next _get_conn() call.
 
600
            conn = conn and conn.close()
 
601
            release_conn = True
 
602
 
 
603
            if isinstance(e, (SocketError, NewConnectionError)) and self.proxy:
559
604
                e = ProxyError('Cannot connect to proxy.', e)
560
605
            elif isinstance(e, (SocketError, HTTPException)):
561
606
                e = ProtocolError('Connection aborted.', e)
562
607
 
563
 
            retries = retries.increment(method, url, error=e,
564
 
                                        _pool=self, _stacktrace=stacktrace)
 
608
            retries = retries.increment(method, url, error=e, _pool=self,
 
609
                                        _stacktrace=sys.exc_info()[2])
565
610
            retries.sleep()
566
611
 
567
612
            # Keep track of the error for the retry warning.
593
638
                retries = retries.increment(method, url, response=response, _pool=self)
594
639
            except MaxRetryError:
595
640
                if retries.raise_on_redirect:
 
641
                    # Release the connection for this response, since we're not
 
642
                    # returning it to be released manually.
 
643
                    response.release_conn()
596
644
                    raise
597
645
                return response
598
646
 
629
677
    ``assert_hostname`` and ``host`` in this order to verify connections.
630
678
    If ``assert_hostname`` is False, no verification is done.
631
679
 
632
 
    The ``key_file``, ``cert_file``, ``cert_reqs``, ``ca_certs`` and
633
 
    ``ssl_version`` are only used if :mod:`ssl` is available and are fed into
634
 
    :meth:`urllib3.util.ssl_wrap_socket` to upgrade the connection socket
635
 
    into an SSL socket.
 
680
    The ``key_file``, ``cert_file``, ``cert_reqs``, ``ca_certs``,
 
681
    ``ca_cert_dir``, and ``ssl_version`` are only used if :mod:`ssl` is
 
682
    available and are fed into :meth:`urllib3.util.ssl_wrap_socket` to upgrade
 
683
    the connection socket into an SSL socket.
636
684
    """
637
685
 
638
686
    scheme = 'https'
645
693
                 key_file=None, cert_file=None, cert_reqs=None,
646
694
                 ca_certs=None, ssl_version=None,
647
695
                 assert_hostname=None, assert_fingerprint=None,
648
 
                 **conn_kw):
 
696
                 ca_cert_dir=None, **conn_kw):
649
697
 
650
698
        HTTPConnectionPool.__init__(self, host, port, strict, timeout, maxsize,
651
699
                                    block, headers, retries, _proxy, _proxy_headers,
652
700
                                    **conn_kw)
 
701
 
 
702
        if ca_certs and cert_reqs is None:
 
703
            cert_reqs = 'CERT_REQUIRED'
 
704
 
653
705
        self.key_file = key_file
654
706
        self.cert_file = cert_file
655
707
        self.cert_reqs = cert_reqs
656
708
        self.ca_certs = ca_certs
 
709
        self.ca_cert_dir = ca_cert_dir
657
710
        self.ssl_version = ssl_version
658
711
        self.assert_hostname = assert_hostname
659
712
        self.assert_fingerprint = assert_fingerprint
669
722
                          cert_file=self.cert_file,
670
723
                          cert_reqs=self.cert_reqs,
671
724
                          ca_certs=self.ca_certs,
 
725
                          ca_cert_dir=self.ca_cert_dir,
672
726
                          assert_hostname=self.assert_hostname,
673
727
                          assert_fingerprint=self.assert_fingerprint)
674
728
            conn.ssl_version = self.ssl_version
675
729
 
676
 
        if self.proxy is not None:
677
 
            # Python 2.7+
678
 
            try:
679
 
                set_tunnel = conn.set_tunnel
680
 
            except AttributeError:  # Platform-specific: Python 2.6
681
 
                set_tunnel = conn._set_tunnel
682
 
 
683
 
            if sys.version_info <= (2, 6, 4) and not self.proxy_headers:   # Python 2.6.4 and older
684
 
                set_tunnel(self.host, self.port)
685
 
            else:
686
 
                set_tunnel(self.host, self.port, self.proxy_headers)
687
 
 
688
 
            # Establish tunnel connection early, because otherwise httplib
689
 
            # would improperly set Host: header to proxy's IP:port.
690
 
            conn.connect()
691
 
 
692
730
        return conn
693
731
 
 
732
    def _prepare_proxy(self, conn):
 
733
        """
 
734
        Establish tunnel connection early, because otherwise httplib
 
735
        would improperly set Host: header to proxy's IP:port.
 
736
        """
 
737
        # Python 2.7+
 
738
        try:
 
739
            set_tunnel = conn.set_tunnel
 
740
        except AttributeError:  # Platform-specific: Python 2.6
 
741
            set_tunnel = conn._set_tunnel
 
742
 
 
743
        if sys.version_info <= (2, 6, 4) and not self.proxy_headers:   # Python 2.6.4 and older
 
744
            set_tunnel(self.host, self.port)
 
745
        else:
 
746
            set_tunnel(self.host, self.port, self.proxy_headers)
 
747
 
 
748
        conn.connect()
 
749
 
694
750
    def _new_conn(self):
695
751
        """
696
752
        Return a fresh :class:`httplib.HTTPSConnection`.
700
756
                 % (self.num_connections, self.host))
701
757
 
702
758
        if not self.ConnectionCls or self.ConnectionCls is DummyConnection:
703
 
            # Platform-specific: Python without ssl
704
759
            raise SSLError("Can't connect to HTTPS URL because the SSL "
705
760
                           "module is not available.")
706
761