/usr/lib/python3/dist-packages/sphinx/util/requests.py is in python3-sphinx 1.6.7-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | # -*- coding: utf-8 -*-
"""
sphinx.util.requests
~~~~~~~~~~~~~~~~~~~~
Simple requests package loader
:copyright: Copyright 2007-2018 by the Sphinx team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
from __future__ import absolute_import
import warnings
from contextlib import contextmanager
import requests
import pkg_resources
from six import string_types
from six.moves.urllib.parse import urlsplit
try:
from requests.packages.urllib3.exceptions import SSLError
except ImportError:
# python-requests package in Debian jessie does not provide ``requests.packages.urllib3``.
# So try to import the exceptions from urllib3 package.
from urllib3.exceptions import SSLError # type: ignore
try:
from requests.packages.urllib3.exceptions import InsecureRequestWarning
except ImportError:
try:
# for Debian-jessie
from urllib3.exceptions import InsecureRequestWarning # type: ignore
except ImportError:
# for requests < 2.4.0
InsecureRequestWarning = None
try:
from requests.packages.urllib3.exceptions import InsecurePlatformWarning
except ImportError:
try:
# for Debian-jessie
from urllib3.exceptions import InsecurePlatformWarning # type: ignore
except ImportError:
# for requests < 2.4.0
InsecurePlatformWarning = None
# try to load requests[security] (but only if SSL is available)
try:
import ssl
except ImportError:
pass
else:
try:
pkg_resources.require(['requests[security]'])
except (pkg_resources.DistributionNotFound,
pkg_resources.VersionConflict):
if not getattr(ssl, 'HAS_SNI', False):
# don't complain on each url processed about the SSL issue
if InsecurePlatformWarning:
requests.packages.urllib3.disable_warnings(InsecurePlatformWarning)
warnings.warn(
'Some links may return broken results due to being unable to '
'check the Server Name Indication (SNI) in the returned SSL cert '
'against the hostname in the url requested. Recommended to '
'install "requests[security]" as a dependency or upgrade to '
'a python version with SNI support (Python 3 and Python 2.7.9+).'
)
except pkg_resources.UnknownExtra:
warnings.warn(
'Some links may return broken results due to being unable to '
'check the Server Name Indication (SNI) in the returned SSL cert '
'against the hostname in the url requested. Recommended to '
'install requests-2.4.1+.'
)
if False:
# For type annotation
from typing import Any, Generator, Union # NOQA
from sphinx.config import Config # NOQA
useragent_header = [('User-Agent',
'Mozilla/5.0 (X11; Linux x86_64; rv:25.0) Gecko/20100101 Firefox/25.0')]
def is_ssl_error(exc):
# type: (Exception) -> bool
"""Check an exception is SSLError."""
if isinstance(exc, SSLError):
return True
else:
args = getattr(exc, 'args', [])
if args and isinstance(args[0], SSLError):
return True
else:
return False
@contextmanager
def ignore_insecure_warning(**kwargs):
# type: (Any) -> Generator
with warnings.catch_warnings():
if not kwargs.get('verify') and InsecureRequestWarning:
# ignore InsecureRequestWarning if verify=False
warnings.filterwarnings("ignore", category=InsecureRequestWarning)
yield
def _get_tls_cacert(url, config):
# type: (unicode, Config) -> Union[str, bool]
"""Get addiotinal CA cert for a specific URL.
This also returns ``False`` if verification is disabled.
And returns ``True`` if additional CA cert not found.
"""
if not config.tls_verify:
return False
certs = getattr(config, 'tls_cacerts', None)
if not certs:
return True
elif isinstance(certs, (string_types, tuple)):
return certs # type: ignore
else:
hostname = urlsplit(url)[1]
if '@' in hostname:
hostname = hostname.split('@')[1]
return certs.get(hostname, True)
def get(url, **kwargs):
# type: (unicode, Any) -> requests.Response
"""Sends a GET request like requests.get().
This sets up User-Agent header and TLS verification automatically."""
kwargs.setdefault('headers', dict(useragent_header))
config = kwargs.pop('config', None)
if config:
kwargs.setdefault('verify', _get_tls_cacert(url, config))
with ignore_insecure_warning(**kwargs):
return requests.get(url, **kwargs)
def head(url, **kwargs):
# type: (unicode, Any) -> requests.Response
"""Sends a HEAD request like requests.head().
This sets up User-Agent header and TLS verification automatically."""
kwargs.setdefault('headers', dict(useragent_header))
config = kwargs.pop('config', None)
if config:
kwargs.setdefault('verify', _get_tls_cacert(url, config))
with ignore_insecure_warning(**kwargs):
return requests.get(url, **kwargs)
|