diff --git a/AUTHORS.rst b/AUTHORS.rst index 2f7de0e1..a4b2411b 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -97,3 +97,4 @@ Patches and Suggestions - Jonty Wareing - Shivaram Lingamneni - Miguel Turner +- Rohan Jain (crodjer) diff --git a/requests/models.py b/requests/models.py index abee9a62..e14c851b 100644 --- a/requests/models.py +++ b/requests/models.py @@ -28,7 +28,7 @@ from .exceptions import ( URLRequired, SSLError, MissingSchema, InvalidSchema, InvalidURL) from .utils import ( get_encoding_from_headers, stream_untransfer, guess_filename, requote_uri, - stream_decode_response_unicode, get_netrc_auth, + stream_decode_response_unicode, get_netrc_auth, get_environ_proxies, DEFAULT_CA_BUNDLE_PATH) from .compat import ( cookielib, urlparse, urlunparse, urljoin, urlsplit, urlencode, str, bytes, @@ -112,10 +112,7 @@ class Request(object): # If no proxies are given, allow configuration by environment variables # HTTP_PROXY and HTTPS_PROXY. if not self.proxies and self.config.get('trust_env'): - if 'HTTP_PROXY' in os.environ: - self.proxies['http'] = os.environ['HTTP_PROXY'] - if 'HTTPS_PROXY' in os.environ: - self.proxies['https'] = os.environ['HTTPS_PROXY'] + self.proxies = get_environ_proxies() self.data = data self.params = params diff --git a/requests/utils.py b/requests/utils.py index ecfee47b..8365cc36 100644 --- a/requests/utils.py +++ b/requests/utils.py @@ -446,3 +446,19 @@ def requote_uri(uri): # Then quote only illegal characters (do not quote reserved, unreserved, # or '%') return quote(unquote_unreserved(uri), safe="!#$%&'()*+,/:;=?@[]~") + +def get_environ_proxies(): + """Return a dict of environment proxies.""" + + proxy_keys = [ + 'all', + 'http', + 'https', + 'ftp', + 'socks', + 'no' + ] + + get_proxy = lambda k: os.environ.get(k) or os.environ.get(k.upper()) + proxies = [(key, get_proxy(key + '_proxy')) for key in proxy_keys] + return dict([(key, val) for (key, val) in proxies if val])