From: Cory Benfield Date: Sun, 18 Nov 2012 12:06:33 +0000 (+0000) Subject: Respect the no_proxy environment variable. X-Git-Tag: v1.0.0~104^2~6^2~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=8da100f652793f95f7dfdad588c925dfac9e71a1;p=services%2Fpython-requests.git Respect the no_proxy environment variable. This change is in response to issue #879. --- diff --git a/requests/models.py b/requests/models.py index 58e7f9a..9c0666a 100644 --- a/requests/models.py +++ b/requests/models.py @@ -119,7 +119,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'): - self.proxies = get_environ_proxies() + self.proxies = get_environ_proxies(self.url) self.data = data self.params = params diff --git a/requests/utils.py b/requests/utils.py index b3d33f4..fef2d83 100644 --- a/requests/utils.py +++ b/requests/utils.py @@ -502,7 +502,7 @@ def requote_uri(uri): return quote(unquote_unreserved(uri), safe="!#$%&'()*+,/:;=?@[]~") -def get_environ_proxies(): +def get_environ_proxies(url): """Return a dict of environment proxies.""" proxy_keys = [ @@ -510,11 +510,29 @@ def get_environ_proxies(): 'http', 'https', 'ftp', - 'socks', - 'no' + 'socks' ] get_proxy = lambda k: os.environ.get(k) or os.environ.get(k.upper()) + + # First check whether no_proxy is defined. If it is, check that the URL + # we're getting isn't in the no_proxy list. + no_proxy = get_proxy('no_proxy') + + if no_proxy: + # We need to check whether we match here. We need to see if we match + # the end of the netloc, both with and without the port. + no_proxy = no_proxy.split(',') + netloc = urlparse(url).netloc + + for host in no_proxy: + if netloc.endswith(host) or netloc.split(':')[0].endswith(host): + # The URL does match something in no_proxy, so we don't want + # to apply the proxies on this URL. + return {} + + # If we get here, we either didn't have no_proxy set or we're not going + # anywhere that no_proxy applies to. proxies = [(key, get_proxy(key + '_proxy')) for key in proxy_keys] return dict([(key, val) for (key, val) in proxies if val]) diff --git a/tests/test_utils.py b/tests/test_utils.py index 5cd0684..7febc57 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -9,6 +9,7 @@ import random # Path hack. sys.path.insert(0, os.path.abspath('..')) +from requests.utils import get_environ_proxies import requests.utils from requests.compat import is_py3, bytes @@ -20,7 +21,7 @@ else: byteschr = chr -class GuessJSONUTFTests(unittest.TestCase): +class UtilityTests(unittest.TestCase): """Tests for the JSON UTF encoding guessing code.""" codecs = ( @@ -73,5 +74,28 @@ class GuessJSONUTFTests(unittest.TestCase): continue raise + def test_get_environ_proxies_respects_no_proxy(self): + '''This test confirms that the no_proxy environment setting is + respected by get_environ_proxies().''' + + # Set up some example environment settings. + os.environ['http_proxy'] = 'http://www.example.com/' + os.environ['no_proxy'] = r'localhost,.0.0.1:8080' + + # Set up expected proxy return values. + proxy_yes = {'http': 'http://www.example.com/'} + proxy_no = {} + + # Check that we get the right things back. + self.assertEqual(proxy_yes, + get_environ_proxies('http://www.google.com/')) + self.assertEqual(proxy_no, + get_environ_proxies('http://localhost/test')) + self.assertEqual(proxy_no, + get_environ_proxies('http://127.0.0.1:8080/')) + self.assertEqual(proxy_yes, + get_environ_proxies('http://127.0.0.1:8081/')) + + if __name__ == '__main__': unittest.main()