diff --git a/lib/httparty.rb b/lib/httparty.rb index c36e2769..4db00076 100644 --- a/lib/httparty.rb +++ b/lib/httparty.rb @@ -47,6 +47,8 @@ def self.included(base) # [:+limit+:] Maximum number of redirects to follow. Takes precedences over :+no_follow+. # [:+query+:] Query string, or a Hash representing it. Normalized according to the same rules as :+body+. If you specify this on a POST, you must use a Hash. See also HTTParty::ClassMethods.default_params. # [:+timeout+:] Timeout for opening connection and reading data. + # [:+local_host:] Local address to bind to before connecting. + # [:+local_port:] Local port to bind to before connecting. # # There are also another set of options with names corresponding to various class methods. The methods in question are those that let you set a class-wide default, and the options override the defaults on a request-by-request basis. Those options are: # * :+base_uri+: see HTTParty::ClassMethods.base_uri. diff --git a/lib/httparty/connection_adapter.rb b/lib/httparty/connection_adapter.rb index 8b1a6196..679d8070 100644 --- a/lib/httparty/connection_adapter.rb +++ b/lib/httparty/connection_adapter.rb @@ -89,6 +89,25 @@ def connection http.ciphers = options[:ciphers] end + # Bind to a specific local address or port + # + # @see https://bugs.ruby-lang.org/issues/6617 + if options[:local_host] + if RUBY_VERSION >= "2.0.0" + http.local_host = options[:local_host] + else + Kernel.warn("Warning: option :local_host requires Ruby version 2.0 or later") + end + end + + if options[:local_port] + if RUBY_VERSION >= "2.0.0" + http.local_port = options[:local_port] + else + Kernel.warn("Warning: option :local_port requires Ruby version 2.0 or later") + end + end + return http end diff --git a/spec/httparty/connection_adapter_spec.rb b/spec/httparty/connection_adapter_spec.rb index 93d54e7e..684d1177 100644 --- a/spec/httparty/connection_adapter_spec.rb +++ b/spec/httparty/connection_adapter_spec.rb @@ -202,6 +202,13 @@ end end + context 'when providing a local bind address and port' do + let(:options) { {:local_host => "127.0.0.1", :local_port => 12345 } } + + its(:local_host) { should == '127.0.0.1' } + its(:local_port) { should == 12345 } + end if RUBY_VERSION >= '2.0' + context "when providing PEM certificates" do let(:pem) { :pem_contents } let(:options) { {:pem => pem, :pem_password => "password"} }