Feature #6617
closedNet::HTTP: Bind to a specific local IP/port
Added by mksm (Ricardo Amorim) over 13 years ago. Updated over 13 years ago.
Description
Despite having several solutions floating around the net, this feature is currently not present in Net::HTTP. I searched the issues but didn't find any discussion on this. How does the dev team feel about this? Can I submit a patch for revision?
Files
net-http_bind_to_local_ip.patch (1.25 KB) net-http_bind_to_local_ip.patch | mksm (Ricardo Amorim), 06/26/2012 03:44 AM | ||
net-http_bind_to_local_ip-tests.patch (1.46 KB) net-http_bind_to_local_ip-tests.patch | mksm (Ricardo Amorim), 07/05/2012 02:19 PM |
Updated by drbrain (Eric Hodel) over 13 years ago
Actions
#1
[ruby-core:45764]
- Category set to lib
Bind to a local port like TCPSocket.new does? I'm sure this feature could be added.
I think the easiest way to add it would result in an API like:
http = Net::HTTP.new remote_host
http.local_host = local_host
http.start do
…
end
Instead of passing the local host and port as parameters to Net::HTTP.new or Net::HTTP.start.
Updated by mksm (Ricardo Amorim) over 13 years ago
Actions
#2
[ruby-core:45773]
I agree. You can pass both local_ip and local_port to TCPSocket.open in Net::HTTP#connect.
Having the setter exposed would allow the user to change the local IP address between connections. Any ideas if this could cause problems with Net::HTTP or remote servers? There's also proxy and ipv6 support, both which I have never tested using this approach.
Updated by mksm (Ricardo Amorim) over 13 years ago
Actions
#3
[ruby-core:45839]
The patch adds accessors for @local_host and @local_port and uses both in TCPSocket.open call.
Updated by drbrain (Eric Hodel) over 13 years ago
Actions
#4
[ruby-core:45863]
- Assignee set to naruse (Yui NARUSE)
- Target version set to 2.0.0
Updated by naruse (Yui NARUSE) over 13 years ago
Actions
#5
[ruby-core:46126]
It looks good.
Could you provide a test for it?
It should be a patch for test/net/http/test_http.rb
Updated by madeofcode (Mark Dodwell) over 13 years ago
Actions
#6
[ruby-core:46174]
Forgive me if this is a silly question, but what is the point of initializing the instance variables to nil in the initializer?
Updated by jballanc (Joshua Ballanco) over 13 years ago
Actions
#7
[ruby-core:46185]
=begin
Not sure, but it may be related to this:
>> defined?(@foo)
=> nil
>> @foo = nil
=> nil
>> defined?(@foo)
=> "instance-variable"
=end
Updated by mksm (Ricardo Amorim) over 13 years ago
Actions
#8
[ruby-core:46195]
@naruse (Yui NARUSE): tests attached! It seems that local_host cannot be nil when setting local_port in TCPSocket.open.
@madeofcode: I also do not know, just followed the pattern. jballanc may have a point, but i can't see why there would be #defined? checks on ivars.
Updated by drbrain (Eric Hodel) over 13 years ago
Actions
#9
[ruby-core:46206]
=begin
You need to initialize instance variables to prevent warnings on uninitialized access:
$ ruby -we 'p @x'
-e:1: warning: instance variable @x not initialized
nil
=end
Updated by naruse (Yui NARUSE) over 13 years ago
Actions
#10
- Status changed from Open to Closed
- % Done changed from 0 to 100
This issue was solved with changeset r36367.
Ricardo, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.
- lib/net/http.rb (Net::HTTP#connect): use local_host and local_port
if specified. patched by Ricardo Amorim [Feature #6617]