This class provides simple methods for issuing various types of HTTP requests. It’s interface is intended to mirror Ruby’s Net::HTTP object, but it provides a few important bits of additional functionality. Notably:
Any HTTPS requests made using this class will use Puppet’s SSL certificate configuration for their authentication, and
Provides some useful error handling for any SSL errors that occur during a request.
@api public
The address to connect to.
# File lib/puppet/network/http/connection.rb, line 149 def address @site.host end
@param path [String] @param headers [Hash{String => String}] @!macro common_options @api public
# File lib/puppet/network/http/connection.rb, line 102 def delete(path, headers = {'Depth' => 'Infinity'}, options = {}) request_with_redirects(Net::HTTP::Delete.new(path, headers), options) end
@param path [String] @param headers [Hash{String => String}] @!macro common_options @api public
# File lib/puppet/network/http/connection.rb, line 75 def get(path, headers = {}, options = {}) request_with_redirects(Net::HTTP::Get.new(path, headers), options) end
@param path [String] @param headers [Hash{String => String}] @!macro common_options @api public
# File lib/puppet/network/http/connection.rb, line 94 def head(path, headers = {}, options = {}) request_with_redirects(Net::HTTP::Head.new(path, headers), options) end
The port to connect to.
# File lib/puppet/network/http/connection.rb, line 154 def port @site.port end
@param path [String] @param data [String] @param headers [Hash{String => String}] @!macro common_options @api public
# File lib/puppet/network/http/connection.rb, line 84 def post(path, data, headers = nil, options = {}) request = Net::HTTP::Post.new(path, headers) request.body = data request_with_redirects(request, options) end
@param path [String] @param data [String] @param headers [Hash{String => String}] @!macro common_options @api public
# File lib/puppet/network/http/connection.rb, line 111 def put(path, data, headers = nil, options = {}) request = Net::HTTP::Put.new(path, headers) request.body = data request_with_redirects(request, options) end
# File lib/puppet/network/http/connection.rb, line 117 def request(method, *args) self.send(method, *args) end
TODO: These are proxies for the Net::HTTP#request_* methods, which are almost the same as the “get”, “post”, etc. methods that we’ve ported above, but they are able to accept a code block and will yield to it, which is necessary to stream responses, e.g. file content. For now we’re not funneling these proxy implementations through our request method above, so they will not inherit the same error handling. In the future we may want to refactor these so that they are funneled through that method and do inherit the error handling.
# File lib/puppet/network/http/connection.rb, line 129 def request_get(*args, &block) with_connection(@site) do |connection| connection.request_get(*args, &block) end end
# File lib/puppet/network/http/connection.rb, line 135 def request_head(*args, &block) with_connection(@site) do |connection| connection.request_head(*args, &block) end end
# File lib/puppet/network/http/connection.rb, line 141 def request_post(*args, &block) with_connection(@site) do |connection| connection.request_post(*args, &block) end end
Whether to use ssl
# File lib/puppet/network/http/connection.rb, line 159 def use_ssl? @site.use_ssl? end
Creates a new HTTP client connection to `host`:`port`. @param host [String] the host to which this client will connect to @param port [Fixnum] the port to which this client will connect to @param options [Hash] options influencing the properties of the created
connection,
@option options [Boolean] :use_ssl true to connect with SSL, false
otherwise, defaults to true
@option options [setup_connection] :verify An object that will configure
any verification to do on the connection
@option options [Fixnum] :redirect_limit the number of allowed
redirections, defaults to 10 passing any other option in the options hash results in a Puppet::Error exception
@note the HTTP connection itself happens lazily only when {request}, or
one of the {#get}, {#post}, {#delete}, {#head} or {#put} is called
@note The correct way to obtain a connection is to use one of the factory
methods on {Puppet::Network::HttpPool}
@api private
# File lib/puppet/network/http/connection.rb, line 51 def initialize(host, port, options = {}) @host = host @port = port unknown_options = options.keys - OPTION_DEFAULTS.keys raise Puppet::Error, "Unrecognized option(s): #{unknown_options.map(&:inspect).sort.join(', ')}" unless unknown_options.empty? options = OPTION_DEFAULTS.merge(options) @use_ssl = options[:use_ssl] @verify = options[:verify] @redirect_limit = options[:redirect_limit] @site = Puppet::Network::HTTP::Site.new(@use_ssl ? 'https' : 'http', host, port) @pool = Puppet.lookup(:http_pool) end