Feature #6617
closed
Net::HTTP: Bind to a specific local IP/port
Added by mksm (Ricardo Amorim) over 12 years ago.
Updated over 12 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
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.
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.
The patch adds accessors for @local_host and @local_port and uses both in TCPSocket.open call.
- Assignee set to naruse (Yui NARUSE)
- Target version set to 2.0.0
It looks good.
Could you provide a test for it?
It should be a patch for test/net/http/test_http.rb
Forgive me if this is a silly question, but what is the point of initializing the instance variables to nil in the initializer?
=begin
Not sure, but it may be related to this:
>> defined?(@foo)
=> nil
>> @foo = nil
=> nil
>> defined?(@foo)
=> "instance-variable"
=end
@naruse (Yui NARUSE): tests attached! It seems that local_host cannot be nil when setting local_port in TCPSocket.open.
@madeofcode (Mark Dodwell): 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.
=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
- 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]
Also available in: Atom
PDF
Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0