1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* lib/net/http.rb (Net::HTTP#connect): use local_host and local_port

if specified. patched by Ricardo Amorim [Feature #6617]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@36367 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
naruse 2012-07-11 18:37:18 +00:00
parent dea582458a
commit 4689853711
4 changed files with 65 additions and 1 deletions

View file

@ -1,3 +1,8 @@
Thu Jul 12 01:49:07 2012 NARUSE, Yui <naruse@ruby-lang.org>
* 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@ruby-lang.org>
* ext/extmk.rb: append ENCOBJS to DLDOBJS but not EXTSOLIBS which is

9
NEWS
View file

@ -83,6 +83,15 @@ with all sufficient information, see the ChangeLog file.
* 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

View file

@ -587,6 +587,8 @@ module Net #:nodoc:
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 @@ module Net #:nodoc:
# 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 @@ module Net #:nodoc:
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?

View file

@ -616,3 +616,45 @@ class TestNetHTTPKeepAlive < Test::Unit::TestCase
}
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