lib/cfoundry/rest_client.rb in cfoundry-0.5.3.rc6 vs lib/cfoundry/rest_client.rb in cfoundry-0.5.3.rc7

- old
+ new

@@ -22,11 +22,11 @@ :follow_redirects => true } attr_reader :target - attr_accessor :trace, :backtrace, :log, :request_id, :token, :target, :proxy + attr_accessor :trace, :backtrace, :log, :request_id, :token, :target, :proxy, :http_proxy, :https_proxy def initialize(target, token = nil) @target = target @token = token @trace = false @@ -113,21 +113,15 @@ print_request(request_hash) if @trace add_headers(request, headers) - # TODO: test http proxies - http = Net::HTTP.new(uri.host, uri.port) + http = create_http(uri) # TODO remove this when staging returns streaming responses http.read_timeout = 300 - if uri.is_a?(URI::HTTPS) - http.use_ssl = true - http.verify_mode = OpenSSL::SSL::VERIFY_NONE - end - before = Time.now http.start do response = http.request(request) time = Time.now - before @@ -152,9 +146,30 @@ raise Timeout.new(method, uri, e) rescue SocketError, Errno::ECONNREFUSED => e raise TargetRefused, e.message rescue URI::InvalidURIError raise InvalidTarget.new(@target) + end + + def create_http(uri) + if (uri.instance_of?(URI::HTTP) && http_proxy) || (uri.instance_of?(URI::HTTPS) && https_proxy) + if uri.instance_of?(URI::HTTP) + http_proxy_uri = URI.parse(http_proxy) + else + http_proxy_uri = URI.parse(https_proxy) + end + http_proxy_user, http_proxy_pass = http_proxy_uri.userinfo.split(/:/) if http_proxy_uri.userinfo + http = Net::HTTP::Proxy(http_proxy_uri.host, http_proxy_uri.port, http_proxy_user, http_proxy_pass).new(uri.host, uri.port) + else + http = Net::HTTP.new(uri.host, uri.port) + end + + if uri.is_a?(URI::HTTPS) + http.use_ssl = true + http.verify_mode = OpenSSL::SSL::VERIFY_NONE + end + + return http end def construct_url(path) uri = URI.parse(path) return path if uri.scheme