Feature #6617

Net::HTTP: Bind to a specific local IP/port

Added by Ricardo Amorim almost 3 years ago. Updated over 2 years ago.

[ruby-core:<unknown>]
Status:Closed
Priority:Normal
Assignee:Yui NARUSE

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?

net-http_bind_to_local_ip.patch Magnifier (1.25 KB) Ricardo Amorim, 06/26/2012 03:44 AM

net-http_bind_to_local_ip-tests.patch Magnifier (1.46 KB) Ricardo Amorim, 07/05/2012 02:19 PM


Related issues

Related to Ruby trunk - Bug #6746: test_bind_to_local_host(TestNetHTTPLocalBind) fails. Closed 07/17/2012

Associated revisions

Revision 36367
Added by Yui NARUSE over 2 years ago

  • lib/net/http.rb (Net::HTTP#connect): use local_host and local_port if specified. patched by Ricardo Amorim [Feature #6617]

Revision 36367
Added by Yui NARUSE over 2 years ago

  • lib/net/http.rb (Net::HTTP#connect): use local_host and local_port if specified. patched by Ricardo Amorim [Feature #6617]

History

#1 Updated by Eric Hodel almost 3 years ago

  • 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.

#2 Updated by Ricardo Amorim almost 3 years ago

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.

#3 Updated by Ricardo Amorim almost 3 years ago

The patch adds accessors for @local_host and @local_port and uses both in TCPSocket.open call.

#4 Updated by Eric Hodel almost 3 years ago

  • Assignee set to Yui NARUSE
  • Target version set to 2.0.0

#5 Updated by Yui NARUSE over 2 years ago

It looks good.
Could you provide a test for it?
It should be a patch for test/net/http/test_http.rb

#6 Updated by Mark Dodwell over 2 years ago

Forgive me if this is a silly question, but what is the point of initializing the instance variables to nil in the initializer?

#7 Updated by Joshua Ballanco over 2 years ago

=begin
Not sure, but it may be related to this:

>> defined?(@foo)
=> nil 
>> @foo = nil
=> nil 
>> defined?(@foo)
=> "instance-variable" 

=end

#8 Updated by Ricardo Amorim over 2 years ago

@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.

#9 Updated by Eric Hodel over 2 years ago

=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

#10 Updated by Yui NARUSE over 2 years ago

  • 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