/usr/lib/ruby/vendor_ruby/influxdb/client/http.rb is in ruby-influxdb 0.2.3-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | require 'uri'
require 'cgi'
require 'net/http'
require 'net/https'
module InfluxDB
# rubocop:disable Metrics/MethodLength
# rubocop:disable Metrics/AbcSize
module HTTP # :nodoc:
def get(url, options = {})
connect_with_retry do |http|
response = do_request http, Net::HTTP::Get.new(url)
if response.is_a? Net::HTTPSuccess
handle_successful_response(response, options)
elsif response.is_a? Net::HTTPUnauthorized
fail InfluxDB::AuthenticationError, response.body
else
resolve_error(response.body)
end
end
end
def post(url, data)
headers = { "Content-Type" => "application/octet-stream" }
connect_with_retry do |http|
response = do_request http, Net::HTTP::Post.new(url, headers), data
if response.is_a? Net::HTTPSuccess
return response
elsif response.is_a? Net::HTTPUnauthorized
fail InfluxDB::AuthenticationError, response.body
else
resolve_error(response.body)
end
end
end
private
def connect_with_retry(&block)
hosts = config.hosts.dup
delay = config.initial_delay
retry_count = 0
begin
hosts.push(host = hosts.shift)
http = Net::HTTP.new(host, config.port)
http.open_timeout = config.open_timeout
http.read_timeout = config.read_timeout
http = setup_ssl(http)
block.call(http)
rescue Timeout::Error, *InfluxDB::NET_HTTP_EXCEPTIONS => e
retry_count += 1
if (config.retry == -1 || retry_count <= config.retry) && !stopped?
log :error, "Failed to contact host #{host}: #{e.inspect} - retrying in #{delay}s."
sleep delay
delay = [config.max_delay, delay * 2].min
retry
else
raise InfluxDB::ConnectionError, "Tried #{retry_count - 1} times to reconnect but failed."
end
ensure
http.finish if http.started?
end
end
def do_request(http, req, data = nil)
req.basic_auth config.username, config.password if basic_auth?
req.body = data if data
http.request(req)
end
def basic_auth?
config.auth_method == 'basic_auth'
end
def resolve_error(response)
if response =~ /Couldn\'t find series/
fail InfluxDB::SeriesNotFound, response
else
fail InfluxDB::Error, response
end
end
def handle_successful_response(response, options)
parsed_response = JSON.parse(response.body) if response.body
errors = errors_from_response(parsed_response) if parsed_response
fail InfluxDB::QueryError, errors if errors
options.fetch(:parse, false) ? parsed_response : response
end
def errors_from_response(parsed_resp)
parsed_resp.is_a?(Hash) && parsed_resp.fetch('results', [])
.fetch(0, {})
.fetch('error', nil)
end
def setup_ssl(http)
http.use_ssl = config.use_ssl
http.verify_mode = OpenSSL::SSL::VERIFY_NONE unless config.verify_ssl
return http unless config.use_ssl
http.cert_store = generate_cert_store
http
end
def generate_cert_store
store = OpenSSL::X509::Store.new
store.set_default_paths
if config.ssl_ca_cert
if File.directory?(config.ssl_ca_cert)
store.add_path(config.ssl_ca_cert)
else
store.add_file(config.ssl_ca_cert)
end
end
store
end
end
end
|