Permalink
Browse files

Followup fix for leading dot in no_proxy

 * '.foo.com' in no_proxy should match with 'www.foo.com'
 * '.foo.com' in no_proxy should not match with 'foo.com'

cf. e2bf6a0
  • Loading branch information...
1 parent b4572a1 commit 265edd5cfb5db173e61bf2284371bcbc91e634c0 @nahi committed Sep 22, 2012
Showing with 47 additions and 7 deletions.
  1. +17 −7 lib/httpclient.rb
  2. +30 −0 test/test_httpclient.rb
View
@@ -378,6 +378,7 @@ def initialize(*args)
proxy, agent_name, from = keyword_argument(args, :proxy, :agent_name, :from)
@proxy = nil # assigned later.
@no_proxy = nil
+ @no_proxy_regexps = []
@www_auth = WWWAuth.new
@proxy_auth = ProxyAuth.new
@request_filter = [@proxy_auth, @www_auth]
@@ -478,6 +479,17 @@ def no_proxy
# Calling this method resets all existing sessions.
def no_proxy=(no_proxy)
@no_proxy = no_proxy
+ @no_proxy_regexps.clear
+ if @no_proxy
+ @no_proxy.scan(/([^:,]+)(?::(\d+))?/) do |host, port|
+ if host[0] == ?.
+ regexp = /#{Regexp.quote(host)}\z/i
+ else
+ regexp = /(\A|\.)#{Regexp.quote(host)}\z/i
+ end
+ @no_proxy_regexps << [regexp, port]
+ end
+ end
reset_all
end
@@ -1041,13 +1053,11 @@ def no_proxy?(uri)
if !@proxy or NO_PROXY_HOSTS.include?(uri.host)
return true
end
- unless @no_proxy
- return false
- end
- @no_proxy.scan(/([^:,]+)(?::(\d+))?/) do |host, port|
- if /(\A|\.)#{Regexp.quote(host.gsub(/^\./,''))}\z/i =~ uri.host &&
- (!port || uri.port == port.to_i)
- return true
+ @no_proxy_regexps.each do |regexp, port|
+ if !port || uri.port == port.to_i
+ if regexp =~ uri.host
+ return true
+ end
end
end
false
View
@@ -298,6 +298,36 @@ def test_no_proxy
end
end
+ def test_no_proxy_with_initial_dot
+ @client.debug_dev = str = ""
+ @client.test_loopback_http_response << "HTTP/1.0 200 OK\r\n\r\n"
+ @client.no_proxy = ''
+ @client.proxy = proxyurl
+ @client.head('http://www.foo.com')
+ assert(/CONNECT TO localhost/ =~ str, 'via proxy')
+ #
+ @client.debug_dev = str = ""
+ @client.test_loopback_http_response << "HTTP/1.0 200 OK\r\n\r\n"
+ @client.no_proxy = '.foo.com'
+ @client.proxy = proxyurl
+ @client.head('http://www.foo.com')
+ assert(/CONNECT TO www.foo.com/ =~ str, 'no proxy because .foo.com matches with www.foo.com')
+ #
+ @client.debug_dev = str = ""
+ @client.test_loopback_http_response << "HTTP/1.0 200 OK\r\n\r\n"
+ @client.no_proxy = '.foo.com'
+ @client.proxy = proxyurl
+ @client.head('http://foo.com')
+ assert(/CONNECT TO localhost/ =~ str, 'via proxy because .foo.com does not matche with foo.com')
+ #
+ @client.debug_dev = str = ""
+ @client.test_loopback_http_response << "HTTP/1.0 200 OK\r\n\r\n"
+ @client.no_proxy = 'foo.com'
+ @client.proxy = proxyurl
+ @client.head('http://foo.com')
+ assert(/CONNECT TO foo.com/ =~ str, 'no proxy because foo.com matches with foo.com')
+ end
+
def test_cookie_update_while_authentication
escape_noproxy do
@client.test_loopback_http_response << <<EOS

0 comments on commit 265edd5

Please sign in to comment.