ruby-changes:24317
From: naruse <ko1@a...>
Date: Thu, 12 Jul 2012 03:37:31 +0900 (JST)
Subject: [ruby-changes:24317] naruse:r36367 (trunk): * lib/net/http.rb (Net::HTTP#connect): use local_host and local_port
naruse 2012-07-12 03:37:18 +0900 (Thu, 12 Jul 2012) New Revision: 36367 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=36367 Log: * lib/net/http.rb (Net::HTTP#connect): use local_host and local_port if specified. patched by Ricardo Amorim [Feature #6617] Modified files: trunk/ChangeLog trunk/NEWS trunk/lib/net/http.rb trunk/test/net/http/test_http.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 36366) +++ ChangeLog (revision 36367) @@ -1,3 +1,8 @@ +Thu Jul 12 01:49:07 2012 NARUSE, Yui <naruse@r...> + + * lib/net/http.rb (Net::HTTP#connect): use local_host and local_port + if specified. patched by Ricardo Amorim [Feature #6617] + Wed Jul 11 17:36:39 2012 Nobuyoshi Nakada <nobu@r...> * ext/extmk.rb: append ENCOBJS to DLDOBJS but not EXTSOLIBS which is Index: lib/net/http.rb =================================================================== --- lib/net/http.rb (revision 36366) +++ lib/net/http.rb (revision 36367) @@ -587,6 +587,8 @@ def initialize(address, port = nil) @address = address @port = (port || HTTP.default_port) + @local_host = nil + @local_port = nil @curr_http_version = HTTPVersion @keep_alive_timeout = 2 @last_communicated = nil @@ -631,6 +633,12 @@ # The port number to connect to. attr_reader :port + # The local host used to estabilish the connection. + attr_accessor :local_host + + # The local port used to estabilish the connection. + attr_accessor :local_port + # Number of seconds to wait for the connection to open. Any number # may be used, including Floats for fractional seconds. If the HTTP # object cannot open a connection in this many seconds, it raises a @@ -799,7 +807,7 @@ def connect D "opening connection to #{conn_address()}..." s = Timeout.timeout(@open_timeout, Net::OpenTimeout) { - TCPSocket.open(conn_address(), conn_port()) + TCPSocket.open(conn_address(), conn_port(), @local_host, @local_port) } D "opened" if use_ssl? Index: NEWS =================================================================== --- NEWS (revision 36366) +++ NEWS (revision 36367) @@ -83,6 +83,15 @@ * incompatible changes: * Fiber#resume cannot resume a fiber which invokes "Fiber#transfer". +* net/http + * new methods: + * Net::HTTP#local_host + * Net::HTTP#local_host= + * Net::HTTP#local_port + * Net::HTTP#local_port= + * extended method: + * Net::HTTP#connect uses local_host and local_port if specified. + * net/imap * new methods: * Net::IMAP.default_port Index: test/net/http/test_http.rb =================================================================== --- test/net/http/test_http.rb (revision 36366) +++ test/net/http/test_http.rb (revision 36367) @@ -616,3 +616,45 @@ } end end + +class TestNetHTTPLocalBind < Test::Unit::TestCase + CONFIG = { + 'host' => '127.0.0.1', + 'port' => 10081, + 'proxy_host' => nil, + 'proxy_port' => nil, + } + + include TestNetHTTPUtils + + def test_bind_to_local_host + @server.mount_proc('/show_ip') { |req, res| res.body = req.remote_ip } + + http = Net::HTTP.new(config('host'), config('port')) + http.local_host = _select_local_ip_address + assert_not_nil(http.local_host) + assert_nil(http.local_port) + + res = http.get('/show_ip') + assert_equal(http.local_host, res.body) + end + + def test_bind_to_local_port + @server.mount_proc('/show_port') { |req, res| res.body = req.peeraddr[1].to_s } + + http = Net::HTTP.new(config('host'), config('port')) + http.local_host = _select_local_ip_address + http.local_port = [*10000..20000].shuffle.first.to_s + assert_not_nil(http.local_host) + assert_not_nil(http.local_port) + + res = http.get('/show_port') + assert_equal(http.local_port, res.body) + end + + def _select_local_ip_address + Socket.ip_address_list.find { |addr| + not addr.ipv4_loopback? and not addr.ipv4_multicast? + }.ip_address + end +end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/