Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add http proxy support for Omnibus::NetFetcher #77

Merged
merged 2 commits into from
Oct 22, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion lib/omnibus/fetchers/net_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,12 @@ def get_with_redirect(url, headers, limit = 10)
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) }
Expand All @@ -104,6 +109,31 @@ def get_with_redirect(url, headers, limit = 10)
end
end

#search environment variable as given, all lowercase and all upper case
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the benefit of #get_env? It looks like you're passing environment variables in uppercase, looking for them, looking for them again downcased, and then again upcased. I've never used environment variables that were anything other than uppercase. I think we're risking some confusion if someone sets a variable with different case by accident.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The proxy related environment variables are not required to by all uppercase in my experience. At least all the common cli tools (wget, git, curl) support proxy related variables in lowercase.
The systems I'm working with have proxy environment variables names in lowercase and it never caused any problems.
So the intended benefit of get_env is to read environment variables regardless if the are written uppercase or lowercase. Looking them up as is (ENV[name]) is not strictly necessary, I just wanted the method to be able to read variables in mixed case provided you pass in the exact string.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Matching behavior of curl is a good argument. I don't think this is overly complicated or confusing.

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
Expand Down