mirror of
https://github.com/elastic/logstash.git
synced 2025-04-25 23:27:19 -04:00
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
48 lines
1.4 KiB
Ruby
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
|