Project

General

Profile

Actions

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.

Status:
Closed
Target version:
[ruby-core:<unknown>]

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

Related issues 1 (0 open1 closed)

Related to Ruby master - Bug #6746: test_bind_to_local_host(TestNetHTTPLocalBind) fails.Closedshyouhei (Shyouhei Urabe)07/17/2012Actions

Updated by drbrain (Eric Hodel) over 12 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.

Updated by mksm (Ricardo Amorim) over 12 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.

Updated by mksm (Ricardo Amorim) over 12 years ago

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

Updated by drbrain (Eric Hodel) over 12 years ago

  • Assignee set to naruse (Yui NARUSE)
  • Target version set to 2.0.0

Updated by naruse (Yui NARUSE) over 12 years ago

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 12 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?

Updated by jballanc (Joshua Ballanco) over 12 years ago

=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 12 years ago

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

Updated by drbrain (Eric Hodel) over 12 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

Actions #10

Updated by naruse (Yui NARUSE) over 12 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]
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0