2018-09-17 00:27:00 +00:00
|
|
|
from __future__ import absolute_import
|
2022-01-24 04:07:52 +00:00
|
|
|
|
|
|
|
from .packages.six.moves.http_client import IncompleteRead as httplib_IncompleteRead
|
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
# Base Exceptions
|
|
|
|
|
|
|
|
|
|
|
|
class HTTPError(Exception):
|
2022-01-24 04:07:52 +00:00
|
|
|
"""Base exception used by this module."""
|
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class HTTPWarning(Warning):
|
2022-01-24 04:07:52 +00:00
|
|
|
"""Base warning used by this module."""
|
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class PoolError(HTTPError):
|
2022-01-24 04:07:52 +00:00
|
|
|
"""Base exception for errors caused within a pool."""
|
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
def __init__(self, pool, message):
|
|
|
|
self.pool = pool
|
|
|
|
HTTPError.__init__(self, "%s: %s" % (pool, message))
|
|
|
|
|
|
|
|
def __reduce__(self):
|
|
|
|
# For pickling purposes.
|
|
|
|
return self.__class__, (None, None)
|
|
|
|
|
|
|
|
|
|
|
|
class RequestError(PoolError):
|
2022-01-24 04:07:52 +00:00
|
|
|
"""Base exception for PoolErrors that have associated URLs."""
|
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
def __init__(self, pool, url, message):
|
|
|
|
self.url = url
|
|
|
|
PoolError.__init__(self, pool, message)
|
|
|
|
|
|
|
|
def __reduce__(self):
|
|
|
|
# For pickling purposes.
|
|
|
|
return self.__class__, (None, self.url, None)
|
|
|
|
|
|
|
|
|
|
|
|
class SSLError(HTTPError):
|
2022-01-24 04:07:52 +00:00
|
|
|
"""Raised when SSL certificate fails in an HTTPS connection."""
|
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class ProxyError(HTTPError):
|
2022-01-24 04:07:52 +00:00
|
|
|
"""Raised when the connection to a proxy fails."""
|
|
|
|
|
|
|
|
def __init__(self, message, error, *args):
|
|
|
|
super(ProxyError, self).__init__(message, error, *args)
|
|
|
|
self.original_error = error
|
2018-09-17 00:27:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DecodeError(HTTPError):
|
2022-01-24 04:07:52 +00:00
|
|
|
"""Raised when automatic decoding based on Content-Type fails."""
|
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class ProtocolError(HTTPError):
|
2022-01-24 04:07:52 +00:00
|
|
|
"""Raised when something unexpected happens mid-request/response."""
|
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
#: Renamed to ProtocolError but aliased for backwards compatibility.
|
|
|
|
ConnectionError = ProtocolError
|
|
|
|
|
|
|
|
|
|
|
|
# Leaf Exceptions
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
class MaxRetryError(RequestError):
|
|
|
|
"""Raised when the maximum number of retries is exceeded.
|
|
|
|
|
|
|
|
:param pool: The connection pool
|
|
|
|
:type pool: :class:`~urllib3.connectionpool.HTTPConnectionPool`
|
|
|
|
:param string url: The requested Url
|
|
|
|
:param exceptions.Exception reason: The underlying error
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, pool, url, reason=None):
|
|
|
|
self.reason = reason
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
message = "Max retries exceeded with url: %s (Caused by %r)" % (url, reason)
|
2018-09-17 00:27:00 +00:00
|
|
|
|
|
|
|
RequestError.__init__(self, pool, url, message)
|
|
|
|
|
|
|
|
|
|
|
|
class HostChangedError(RequestError):
|
2022-01-24 04:07:52 +00:00
|
|
|
"""Raised when an existing pool gets a request for a foreign host."""
|
2018-09-17 00:27:00 +00:00
|
|
|
|
|
|
|
def __init__(self, pool, url, retries=3):
|
|
|
|
message = "Tried to open a foreign host with url: %s" % url
|
|
|
|
RequestError.__init__(self, pool, url, message)
|
|
|
|
self.retries = retries
|
|
|
|
|
|
|
|
|
|
|
|
class TimeoutStateError(HTTPError):
|
2022-01-24 04:07:52 +00:00
|
|
|
"""Raised when passing an invalid state to a timeout"""
|
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class TimeoutError(HTTPError):
|
2022-01-24 04:07:52 +00:00
|
|
|
"""Raised when a socket timeout error occurs.
|
2018-09-17 00:27:00 +00:00
|
|
|
|
|
|
|
Catching this error will catch both :exc:`ReadTimeoutErrors
|
|
|
|
<ReadTimeoutError>` and :exc:`ConnectTimeoutErrors <ConnectTimeoutError>`.
|
|
|
|
"""
|
2022-01-24 04:07:52 +00:00
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class ReadTimeoutError(TimeoutError, RequestError):
|
2022-01-24 04:07:52 +00:00
|
|
|
"""Raised when a socket timeout occurs while receiving data from a server"""
|
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
# This timeout error does not have a URL attached and needs to inherit from the
|
|
|
|
# base HTTPError
|
|
|
|
class ConnectTimeoutError(TimeoutError):
|
2022-01-24 04:07:52 +00:00
|
|
|
"""Raised when a socket timeout occurs while connecting to a server"""
|
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class NewConnectionError(ConnectTimeoutError, PoolError):
|
2022-01-24 04:07:52 +00:00
|
|
|
"""Raised when we fail to establish a new connection. Usually ECONNREFUSED."""
|
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class EmptyPoolError(PoolError):
|
2022-01-24 04:07:52 +00:00
|
|
|
"""Raised when a pool runs out of connections and no more are allowed."""
|
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class ClosedPoolError(PoolError):
|
2022-01-24 04:07:52 +00:00
|
|
|
"""Raised when a request enters a pool after the pool has been closed."""
|
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class LocationValueError(ValueError, HTTPError):
|
2022-01-24 04:07:52 +00:00
|
|
|
"""Raised when there is something wrong with a given URL input."""
|
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class LocationParseError(LocationValueError):
|
2022-01-24 04:07:52 +00:00
|
|
|
"""Raised when get_host or similar fails to parse the URL input."""
|
2018-09-17 00:27:00 +00:00
|
|
|
|
|
|
|
def __init__(self, location):
|
|
|
|
message = "Failed to parse: %s" % location
|
|
|
|
HTTPError.__init__(self, message)
|
|
|
|
|
|
|
|
self.location = location
|
|
|
|
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
class URLSchemeUnknown(LocationValueError):
|
|
|
|
"""Raised when a URL input has an unsupported scheme."""
|
|
|
|
|
|
|
|
def __init__(self, scheme):
|
|
|
|
message = "Not supported URL scheme %s" % scheme
|
|
|
|
super(URLSchemeUnknown, self).__init__(message)
|
|
|
|
|
|
|
|
self.scheme = scheme
|
|
|
|
|
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
class ResponseError(HTTPError):
|
2022-01-24 04:07:52 +00:00
|
|
|
"""Used as a container for an error reason supplied in a MaxRetryError."""
|
|
|
|
|
|
|
|
GENERIC_ERROR = "too many error responses"
|
|
|
|
SPECIFIC_ERROR = "too many {status_code} error responses"
|
2018-09-17 00:27:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SecurityWarning(HTTPWarning):
|
2022-01-24 04:07:52 +00:00
|
|
|
"""Warned when performing security reducing actions"""
|
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class SubjectAltNameWarning(SecurityWarning):
|
2022-01-24 04:07:52 +00:00
|
|
|
"""Warned when connecting to a host with a certificate missing a SAN."""
|
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class InsecureRequestWarning(SecurityWarning):
|
2022-01-24 04:07:52 +00:00
|
|
|
"""Warned when making an unverified HTTPS request."""
|
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class SystemTimeWarning(SecurityWarning):
|
2022-01-24 04:07:52 +00:00
|
|
|
"""Warned when system time is suspected to be wrong"""
|
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class InsecurePlatformWarning(SecurityWarning):
|
2022-01-24 04:07:52 +00:00
|
|
|
"""Warned when certain TLS/SSL configuration is not available on a platform."""
|
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class SNIMissingWarning(HTTPWarning):
|
2022-01-24 04:07:52 +00:00
|
|
|
"""Warned when making a HTTPS request without SNI available."""
|
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class DependencyWarning(HTTPWarning):
|
|
|
|
"""
|
|
|
|
Warned when an attempt is made to import a module with missing optional
|
|
|
|
dependencies.
|
|
|
|
"""
|
2022-01-24 04:07:52 +00:00
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class ResponseNotChunked(ProtocolError, ValueError):
|
2022-01-24 04:07:52 +00:00
|
|
|
"""Response needs to be chunked in order to read it as chunks."""
|
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class BodyNotHttplibCompatible(HTTPError):
|
|
|
|
"""
|
2022-01-24 04:07:52 +00:00
|
|
|
Body should be :class:`http.client.HTTPResponse` like
|
|
|
|
(have an fp attribute which returns raw chunks) for read_chunked().
|
2018-09-17 00:27:00 +00:00
|
|
|
"""
|
2022-01-24 04:07:52 +00:00
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class IncompleteRead(HTTPError, httplib_IncompleteRead):
|
|
|
|
"""
|
|
|
|
Response length doesn't match expected Content-Length
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
Subclass of :class:`http.client.IncompleteRead` to allow int value
|
|
|
|
for ``partial`` to avoid creating large objects on streamed reads.
|
2018-09-17 00:27:00 +00:00
|
|
|
"""
|
2022-01-24 04:07:52 +00:00
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
def __init__(self, partial, expected):
|
|
|
|
super(IncompleteRead, self).__init__(partial, expected)
|
|
|
|
|
|
|
|
def __repr__(self):
|
2022-01-24 04:07:52 +00:00
|
|
|
return "IncompleteRead(%i bytes read, %i more expected)" % (
|
|
|
|
self.partial,
|
|
|
|
self.expected,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class InvalidChunkLength(HTTPError, httplib_IncompleteRead):
|
|
|
|
"""Invalid chunk length in a chunked response."""
|
|
|
|
|
|
|
|
def __init__(self, response, length):
|
|
|
|
super(InvalidChunkLength, self).__init__(
|
|
|
|
response.tell(), response.length_remaining
|
|
|
|
)
|
|
|
|
self.response = response
|
|
|
|
self.length = length
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "InvalidChunkLength(got length %r, %i bytes read)" % (
|
|
|
|
self.length,
|
|
|
|
self.partial,
|
|
|
|
)
|
2018-09-17 00:27:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
class InvalidHeader(HTTPError):
|
2022-01-24 04:07:52 +00:00
|
|
|
"""The header provided was somehow invalid."""
|
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
class ProxySchemeUnknown(AssertionError, URLSchemeUnknown):
|
|
|
|
"""ProxyManager does not support the supplied scheme"""
|
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
# TODO(t-8ch): Stop inheriting from AssertionError in v2.0.
|
|
|
|
|
|
|
|
def __init__(self, scheme):
|
2022-01-24 04:07:52 +00:00
|
|
|
# 'localhost' is here because our URL parser parses
|
|
|
|
# localhost:8080 -> scheme=localhost, remove if we fix this.
|
|
|
|
if scheme == "localhost":
|
|
|
|
scheme = None
|
|
|
|
if scheme is None:
|
|
|
|
message = "Proxy URL had no scheme, should start with http:// or https://"
|
|
|
|
else:
|
|
|
|
message = (
|
|
|
|
"Proxy URL had unsupported scheme %s, should use http:// or https://"
|
|
|
|
% scheme
|
|
|
|
)
|
2018-09-17 00:27:00 +00:00
|
|
|
super(ProxySchemeUnknown, self).__init__(message)
|
|
|
|
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
class ProxySchemeUnsupported(ValueError):
|
|
|
|
"""Fetching HTTPS resources through HTTPS proxies is unsupported"""
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
class HeaderParsingError(HTTPError):
|
2022-01-24 04:07:52 +00:00
|
|
|
"""Raised by assert_header_parsing, but we convert it to a log.warning statement."""
|
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
def __init__(self, defects, unparsed_data):
|
2022-01-24 04:07:52 +00:00
|
|
|
message = "%s, unparsed data: %r" % (defects or "Unknown", unparsed_data)
|
2018-09-17 00:27:00 +00:00
|
|
|
super(HeaderParsingError, self).__init__(message)
|
|
|
|
|
|
|
|
|
|
|
|
class UnrewindableBodyError(HTTPError):
|
2022-01-24 04:07:52 +00:00
|
|
|
"""urllib3 encountered an error when trying to rewind a body"""
|
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
pass
|