From ad6135d0fd5c3f92fa763b0fbac70f4c86f718fe Mon Sep 17 00:00:00 2001 From: Mikkel Kroman Date: Tue, 24 Sep 2013 19:42:26 +0200 Subject: [PATCH 1/3] Add option :local_host and :local_port --- lib/httparty/connection_adapter.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) 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 From 9bc8b251fca063973159a6916ae1fefa0e3a8597 Mon Sep 17 00:00:00 2001 From: Mikkel Kroman Date: Tue, 24 Sep 2013 19:43:17 +0200 Subject: [PATCH 2/3] Add spec for local_host and local_port options --- spec/httparty/connection_adapter_spec.rb | 7 +++++++ 1 file changed, 7 insertions(+) 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"} } From 9c7723b519866e8a14da6b450efebb59b7bdcf8c Mon Sep 17 00:00:00 2001 From: Mikkel Kroman Date: Tue, 24 Sep 2013 19:44:36 +0200 Subject: [PATCH 3/3] Add documentation for :local_host and :local_port options --- lib/httparty.rb | 2 ++ 1 file changed, 2 insertions(+) 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.