lib/cfoundry/rest_client.rb in cfoundry-0.7.0.rc3 vs lib/cfoundry/rest_client.rb in cfoundry-0.7.0.rc4
- old
+ new
@@ -4,10 +4,33 @@
require "multi_json"
require "fileutils"
module CFoundry
class RestClient
+ class HTTPFactory
+ def self.create(uri, http_proxy, https_proxy)
+ scheme = uri.scheme
+ proxy_to_use = (scheme == "http" ? http_proxy : https_proxy)
+
+ if proxy_to_use
+ proxy_uri = URI.parse(proxy_to_use)
+ proxy_user, proxy_pass = proxy_uri.userinfo.split(/:/) if proxy_uri.userinfo
+ http = Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port, proxy_user, proxy_pass).
+ new(uri.host, uri.port)
+ else
+ http = Net::HTTP.new(uri.host, uri.port)
+ end
+
+ if scheme == "https"
+ http.use_ssl = true
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
+ end
+
+ return http
+ end
+ end
+
include CFoundry::TraceHelpers
LOG_LENGTH = 10
HTTP_METHODS = {
@@ -22,11 +45,13 @@
: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,
+ :http_proxy, :https_proxy
def initialize(target, token = nil)
@target = target
@token = token
@trace = false
@@ -47,11 +72,10 @@
headers["Content-Length"] = 0
end
headers["X-Request-Id"] = @request_id if @request_id
headers["Authorization"] = @token.auth_header if @token
- headers["Proxy-User"] = @proxy if @proxy
if accept_type = mimetype(options[:accept])
headers["Accept"] = accept_type
end
@@ -116,19 +140,13 @@
print_request(request_hash) if @trace
add_headers(request, headers)
- # TODO: test http proxies
- http = Net::HTTP.new(uri.host, uri.port)
+ http = HTTPFactory.create(uri, http_proxy, https_proxy)
# 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