Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

split http_request into smaller methods

  • Loading branch information...
commit 4563164563b4edabca5fe4ae5cdc45e03431815e 1 parent d789e80
@mislav mislav authored
Showing with 27 additions and 27 deletions.
  1. +27 −27 lib/hub/commands.rb
View
54 lib/hub/commands.rb
@@ -937,6 +937,20 @@ def http_request(url, type = :Get)
req = Net::HTTP.const_get(type).new(url.request_uri)
req.basic_auth "#{user}/token", token if user and token
+ http = setup_http(url)
+
+ yield req if block_given?
+ http.start { http.request(req) }
+ end
+
+ def http_post(url, params = nil)
+ http_request(url, :Post) do |req|
+ req.set_form_data params if params
+ req['Content-Length'] = req.body ? req.body.length : 0
+ end
+ end
+
+ def setup_http(url)
port = url.port
if use_ssl = 'https' == url.scheme and not use_ssl?
# ruby compiled without openssl
@@ -944,40 +958,18 @@ def http_request(url, type = :Get)
port = 80
end
- # sniff out proxy settings
- if use_ssl
- proxy = ENV['HTTPS_PROXY'] || ENV['https_proxy']
- else
- proxy = ENV['HTTP_PROXY'] || ENV['http_proxy']
+ http_args = [url.host, port]
+ if proxy = proxy_url(use_ssl)
+ http_args.concat proxy.select(:host, :port, :user, :password)
end
- if proxy
- # use an HTTP(S) proxy
-
- unless /^[^:]+:\/\// =~ proxy
- proxy = "http://#{proxy}"
- end
-
- proxy = URI.parse(proxy)
- http = Net::HTTP.new(url.host, port, proxy.host, proxy.port, proxy.user, proxy.password)
- else
- http = Net::HTTP.new(url.host, port)
- end
+ http = Net::HTTP.new(*http_args)
if http.use_ssl = use_ssl
# TODO: SSL peer verification
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
-
- yield req if block_given?
- http.start { http.request(req) }
- end
-
- def http_post(url, params = nil)
- http_request(url, :Post) do |req|
- req.set_form_data params if params
- req['Content-Length'] = req.body ? req.body.length : 0
- end
+ return http
end
def load_net_http
@@ -990,6 +982,14 @@ def use_ssl?
defined? ::OpenSSL
end
+ def proxy_url(use_ssl)
+ env_name = "HTTP#{use_ssl ? 'S' : ''}_PROXY"
+ if proxy = ENV[env_name] || ENV[env_name.downcase]
+ proxy = "http://#{proxy}" unless proxy.include? '://'
+ URI.parse(proxy)
+ end
+ end
+
# Fake exception type for net/http exception handling.
# Necessary because net/http may or may not be loaded at the time.
module HTTPExceptions
Please sign in to comment.
Something went wrong with that request. Please try again.