提交 45631645 编写于 作者: M Mislav Marohnić

split http_request into smaller methods

上级 d789e803
......@@ -937,6 +937,20 @@ help
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 @@ help
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 @@ help
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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册