logstash/lib/pluginmanager/utils/http_client.rb
Pier-Hugues Pellerin 4344e4b613 Allow Logstash to accept a PROXY configuration
Logstash's plugin manager will now follow proxy configuration from the environment.
If you configure `http_proxy` and `https_proxy`, the manager will now use theses information for all the ruby http
connection and will also pass that information down to maven.

Fixes: #6619, #6528

Fixes #6825
2017-03-31 14:20:38 -04:00

48 lines
1.4 KiB
Ruby

# encoding: utf-8
module LogStash module PluginManager module Utils
class HttpClient
class RedirectionLimit < RuntimeError; end
HTTPS_SCHEME = "https"
REDIRECTION_LIMIT = 5
# Proxies should be handled by the library
# https://ruby-doc.org/stdlib-2.3.1/libdoc/net/http/rdoc/Net/HTTP.html#class-Net::HTTP-label-Proxies
def self.start(uri)
uri = URI(uri)
Net::HTTP.start(uri.host, uri.port, http_options(uri)) { |http| yield http }
end
def self.http_options(uri)
ssl_enabled = uri.scheme == HTTPS_SCHEME
{
:use_ssl => ssl_enabled
}
end
# Do a HEAD request on the file to see if it exist before downloading it
def self.remote_file_exist?(uri, redirect_count = 0)
uri = URI(uri)
# This is defensive programming, but in the real world we do create redirects all the time
raise RedirectionLimit, "Too many redirection, tried #{REDIRECTION_LIMIT} times" if redirect_count >= REDIRECTION_LIMIT
start(uri) do |http|
return false if uri.path.empty?
request = Net::HTTP::Head.new(uri.path)
response = http.request(request)
if response.code == "302"
new_uri = response.headers["location"]
remote_file_exist?(new_uri, redirect_count + 1)
elsif response.code == "200"
true
else
false
end
end
end
end
end end end