lib/omnibus/fetchers/net_fetcher.rb in omnibus-1.2.0 vs lib/omnibus/fetchers/net_fetcher.rb in omnibus-1.3.0

- old
+ new

@@ -86,11 +86,16 @@ if !url.kind_of?(URI) url = URI.parse(url) end req = Net::HTTP::Get.new(url.request_uri, headers) - http_client = Net::HTTP.new(url.host, url.port) + + http_client = if http_proxy && !excluded_from_proxy?(url.host) + Net::HTTP::Proxy(http_proxy.host, http_proxy.port, http_proxy.user, http_proxy.password).new(url.host, url.port) + else + Net::HTTP.new(url.host, url.port) + end http_client.use_ssl = (url.scheme == "https") response = http_client.start { |http| http.request(req) } case response when Net::HTTPSuccess @@ -102,10 +107,35 @@ else response.error! end end + # search environment variable as given, all lowercase and all upper case + def get_env(name) + ENV[name] || ENV[name.downcase] || ENV[name.upcase] || nil + end + + # constructs a http_proxy uri from HTTP_PROXY* env vars + def http_proxy + @http_proxy ||= begin + proxy = get_env('HTTP_PROXY') or return + proxy = "http://#{proxy}" unless proxy =~ /^https?:/ + uri = URI.parse(proxy) + uri.user ||= get_env('HTTP_PROXY_USER') + uri.password ||= get_env('HTTP_PROXY_PASS') + uri + end + end + + # return true if the host is excluded from proxying via the no_proxy directive. + # the 'no_proxy' variable contains a list of host suffixes separated by comma + # example: example.com,www.examle.org,localhost + def excluded_from_proxy?(host) + no_proxy = get_env('no_proxy') || "" + no_proxy.split(/\s*,\s*/).any? { |pattern| host.end_with? pattern } + end + def download tries = 5 begin log "\033[1;31m#{source[:warning]}\033[0m" if source.has_key?(:warning) log "fetching #{project_file} from #{source_uri}" @@ -177,9 +207,11 @@ "bzip2 -dc #{project_file} | ( cd #{source_dir} && tar -xf - )" elsif project_file.end_with?(".7z") "7z.exe x #{project_file} -o#{source_dir} -r -y" elsif project_file.end_with?(".zip") "unzip #{project_file} -d #{source_dir}" + elsif project_file.end_with?(".xz") || project_file.end_with?(".txz") + "xz -dc #{project_file} | ( cd #{source_dir} && tar -xf - )" else #if we don't recognize the extension, simply copy over the file Proc.new do log "#{project_file} not an archive. Copying to #{project_dir}" # hack hack hack, no project dir yet