Net::HTTP: Bind to a specific local IP/port
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?
#1 [ruby-core:45764] Updated by Eric Hodel about 4 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
Instead of passing the local host and port as parameters to Net::HTTP.new or Net::HTTP.start.
#2 [ruby-core:45773] Updated by Ricardo Amorim about 4 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.
#8 [ruby-core:46195] Updated by Ricardo Amorim about 4 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.
#10 Updated by Yui NARUSE about 4 years ago
- Status changed from Open to Closed
- % Done changed from 0 to 100