request.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # flake8: noqa: F405
  2. from urllib.request import * # noqa: F403
  3. from ..compat_utils import passthrough_module
  4. passthrough_module(__name__, 'urllib.request')
  5. del passthrough_module
  6. import os
  7. if os.name == 'nt':
  8. # On older Python versions, proxies are extracted from Windows registry erroneously. [1]
  9. # If the https proxy in the registry does not have a scheme, urllib will incorrectly add https:// to it. [2]
  10. # It is unlikely that the user has actually set it to be https, so we should be fine to safely downgrade
  11. # it to http on these older Python versions to avoid issues
  12. # This also applies for ftp proxy type, as ftp:// proxy scheme is not supported.
  13. # 1: https://github.com/python/cpython/issues/86793
  14. # 2: https://github.com/python/cpython/blob/51f1ae5ceb0673316c4e4b0175384e892e33cc6e/Lib/urllib/request.py#L2683-L2698
  15. import sys
  16. from urllib.request import getproxies_environment, getproxies_registry
  17. def getproxies_registry_patched():
  18. proxies = getproxies_registry()
  19. if (
  20. sys.version_info >= (3, 10, 5) # https://docs.python.org/3.10/whatsnew/changelog.html#python-3-10-5-final
  21. or (3, 9, 13) <= sys.version_info < (3, 10) # https://docs.python.org/3.9/whatsnew/changelog.html#python-3-9-13-final
  22. ):
  23. return proxies
  24. for scheme in ('https', 'ftp'):
  25. if scheme in proxies and proxies[scheme].startswith(f'{scheme}://'):
  26. proxies[scheme] = 'http' + proxies[scheme][len(scheme):]
  27. return proxies
  28. def getproxies():
  29. return getproxies_environment() or getproxies_registry_patched()
  30. del os