Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 51b697b

Browse files
committed
Issue #26864: Merge no_proxy fixes from 3.5
2 parents 0a5bd51 + aa27982 commit 51b697b

3 files changed

Lines changed: 24 additions & 2 deletions

File tree

Lib/test/test_urllib.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,19 @@ def test_getproxies_environment_keep_no_proxies(self):
231231
self.assertTrue(urllib.request.proxy_bypass_environment('anotherdomain.com:8888'))
232232
self.assertTrue(urllib.request.proxy_bypass_environment('newdomain.com:1234'))
233233

234+
def test_proxy_bypass_environment_host_match(self):
235+
bypass = urllib.request.proxy_bypass_environment
236+
self.env.set('NO_PROXY',
237+
'localhost, anotherdomain.com, newdomain.com:1234')
238+
self.assertTrue(bypass('localhost'))
239+
self.assertTrue(bypass('LocalHost')) # MixedCase
240+
self.assertTrue(bypass('LOCALHOST')) # UPPERCASE
241+
self.assertTrue(bypass('newdomain.com:1234'))
242+
self.assertTrue(bypass('anotherdomain.com:8888'))
243+
self.assertTrue(bypass('www.newdomain.com:1234'))
244+
self.assertFalse(bypass('prelocalhost'))
245+
self.assertFalse(bypass('newdomain.com')) # no port
246+
self.assertFalse(bypass('newdomain.com:1235')) # wrong port
234247

235248
class ProxyTests_withOrderedEnv(unittest.TestCase):
236249

Lib/urllib/request.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2492,8 +2492,12 @@ def proxy_bypass_environment(host, proxies=None):
24922492
# check if the host ends with any of the DNS suffixes
24932493
no_proxy_list = [proxy.strip() for proxy in no_proxy.split(',')]
24942494
for name in no_proxy_list:
2495-
if name and (hostonly.endswith(name) or host.endswith(name)):
2496-
return 1
2495+
if name:
2496+
name = re.escape(name)
2497+
pattern = r'(.+\.)?%s$' % name
2498+
if (re.match(pattern, hostonly, re.I)
2499+
or re.match(pattern, host, re.I)):
2500+
return 1
24972501
# otherwise, don't bypass
24982502
return 0
24992503

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,11 @@ Core and Builtins
256256
Library
257257
-------
258258

259+
- Issue #26864: In urllib.request, change the proxy bypass host checking
260+
against no_proxy to be case-insensitive, and to not match unrelated host
261+
names that happen to have a bypassed hostname as a suffix. Patch by Xiang
262+
Zhang.
263+
259264
- Issue #24902: Print server URL on http.server startup. Initial patch by
260265
Felix Kaiser.
261266

0 commit comments

Comments
 (0)