diff options
author | Marshall Huss <mwhuss@gmail.com> | 2009-05-18 18:34:44 -0400 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2009-08-08 15:33:55 -0700 |
commit | 4d1552810f631898c3d7f758454c92ca35a8cb26 (patch) | |
tree | f76b0e9ecbf5aa46263d6515bd603ba7d90adf66 /activeresource/lib | |
parent | 5786395760f1e1906c878df4023cac3741e66e87 (diff) | |
download | rails-4d1552810f631898c3d7f758454c92ca35a8cb26.tar.gz rails-4d1552810f631898c3d7f758454c92ca35a8cb26.tar.bz2 rails-4d1552810f631898c3d7f758454c92ca35a8cb26.zip |
HTTP proxy support
[#2133 state:committed]
Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
Diffstat (limited to 'activeresource/lib')
-rw-r--r-- | activeresource/lib/active_resource/base.rb | 22 | ||||
-rw-r--r-- | activeresource/lib/active_resource/connection.rb | 16 |
2 files changed, 35 insertions, 3 deletions
diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index bc82139dac..9db35881b8 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -257,6 +257,22 @@ module ActiveResource end end + # Gets the \proxy variable if a proxy is required + def proxy + # Not using superclass_delegating_reader. See +site+ for explanation + if defined?(@proxy) + @proxy + elsif superclass != Object && superclass.proxy + superclass.proxy.dup.freeze + end + end + + # Sets the URI of the http proxy to the value in the +proxy+ argument. + def proxy=(proxy) + @connection = nil + @proxy = proxy.nil? ? nil : create_proxy_uri_from(proxy) + end + # Gets the \user for REST HTTP authentication. def user # Not using superclass_delegating_reader. See +site+ for explanation @@ -332,6 +348,7 @@ module ActiveResource def connection(refresh = false) if defined?(@connection) || superclass == Object @connection = Connection.new(site, format) if refresh || @connection.nil? + @connection.proxy = proxy if proxy @connection.user = user if user @connection.password = password if password @connection.timeout = timeout if timeout @@ -622,6 +639,11 @@ module ActiveResource site.is_a?(URI) ? site.dup : URI.parse(site) end + # Accepts a URI and creates the proxy URI from that. + def create_proxy_uri_from(proxy) + proxy.is_a?(URI) ? proxy.dup : URI.parse(proxy) + end + # contains a set of the current prefix parameters. def prefix_parameters @prefix_parameters ||= prefix_source.scan(/:\w+/).map { |key| key[1..-1].to_sym }.to_set diff --git a/activeresource/lib/active_resource/connection.rb b/activeresource/lib/active_resource/connection.rb index 99d4b8f2ca..b29e379c53 100644 --- a/activeresource/lib/active_resource/connection.rb +++ b/activeresource/lib/active_resource/connection.rb @@ -16,7 +16,7 @@ module ActiveResource :delete => 'Accept' } - attr_reader :site, :user, :password, :timeout + attr_reader :site, :user, :password, :timeout, :proxy attr_accessor :format class << self @@ -41,6 +41,11 @@ module ActiveResource @password = URI.decode(@site.password) if @site.password end + # Set the proxy for remote service. + def proxy=(proxy) + @proxy = proxy.is_a?(URI) ? proxy : URI.parse(proxy) + end + # Sets the user for remote service. def user=(user) @user = user @@ -132,8 +137,13 @@ module ActiveResource # Creates new Net::HTTP instance for communication with the # remote service and resources. def http - http = Net::HTTP.new(@site.host, @site.port) - http.use_ssl = @site.is_a?(URI::HTTPS) + http = + if @proxy + Net::HTTP.new(@site.host, @site.port, @proxy.host, @proxy.port, @proxy.user, @proxy.password) + else + Net::HTTP.new(@site.host, @site.port) + end + http.use_ssl = @site.is_a?(URI::HTTPS) http.verify_mode = OpenSSL::SSL::VERIFY_NONE if http.use_ssl? http.read_timeout = @timeout if @timeout # If timeout is not set, the default Net::HTTP timeout (60s) is used. http |