Class: Puppet::Network::HTTP::Connection Deprecated

Inherits:
Object
  • Object
show all
Includes:
HTTP::ResponseConverter
Defined in:
lib/puppet/network/http/connection.rb

Overview

Deprecated.

Use :http

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.

Constant Summary collapse

OPTION_DEFAULTS =
{
  :use_ssl => true,
  :verifier => nil,
  :redirect_limit => 10,
}

Instance Method Summary collapse

Methods included from HTTP::ResponseConverter

to_ruby_response

Constructor Details

#initialize(host, port, options = {}) ⇒ Connection

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Creates a new HTTP client connection to ‘host`:`port`.

Parameters:

  • host (String)

    the host to which this client will connect to

  • port (Integer)

    the port to which this client will connect to

  • options (Hash) (defaults to: {})

    options influencing the properties of the created connection,

Options Hash (options):

  • :use_ssl (Boolean)

    true to connect with SSL, false otherwise, defaults to true

  • :verifier (Puppet::SSL::Verifier)

    An object that will configure any verification to do on the connection

  • :redirect_limit (Integer)

    the number of allowed redirections, defaults to 10 passing any other option in the options hash results in a Puppet::Error exception

Raises:



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/puppet/network/http/connection.rb', line 47

def initialize(host, port, options = {})
  unknown_options = options.keys - OPTION_DEFAULTS.keys
  raise Puppet::Error, _("Unrecognized option(s): %{opts}") % { opts: unknown_options.map(&:inspect).sort.join(', ') } unless unknown_options.empty?

  options = OPTION_DEFAULTS.merge(options)
  @use_ssl = options[:use_ssl]
  if @use_ssl
    unless options[:verifier].is_a?(Puppet::SSL::Verifier)
      raise ArgumentError, _("Expected an instance of Puppet::SSL::Verifier but was passed a %{klass}") % { klass: options[:verifier].class }
    end

    @verifier = options[:verifier]
  end
  @redirect_limit = options[:redirect_limit]
  @site = Puppet::HTTP::Site.new(@use_ssl ? 'https' : 'http', host, port)
  @client = Puppet.runtime[:http]
end

Instance Method Details

#addressObject

The address to connect to.



66
67
68
# File 'lib/puppet/network/http/connection.rb', line 66

def address
  @site.host
end

#delete(path, headers = { 'Depth' => 'Infinity' }, options = {}) ⇒ Object

options not recognized by this class will be ignored - no error will be thrown.

Parameters:

  • path (String)
  • headers (Hash{String => String}) (defaults to: { 'Depth' => 'Infinity' })
  • options (Hash) (defaults to: {})

    options influencing the request made. Any

Options Hash (options):

  • :basic_auth (Hash{Symbol => String})

    The basic auth :username and :password to use for the request, :metric_id Ignored by this class - used by Puppet Server only. The metric id by which to track metrics on requests.



143
144
145
146
147
148
149
150
151
# File 'lib/puppet/network/http/connection.rb', line 143

def delete(path, headers = { 'Depth' => 'Infinity' }, options = {})
  headers ||= {}
  options[:ssl_context] ||= resolve_ssl_context
  options[:redirect_limit] ||= @redirect_limit

  with_error_handling do
    to_ruby_response(@client.delete(to_url(path), headers: headers, options: options))
  end
end

#get(path, headers = {}, options = {}) ⇒ Object

options not recognized by this class will be ignored - no error will be thrown.

Parameters:

  • path (String)
  • headers (Hash{String => String}) (defaults to: {})
  • options (Hash) (defaults to: {})

    options influencing the request made. Any

Options Hash (options):

  • :basic_auth (Hash{Symbol => String})

    The basic auth :username and :password to use for the request, :metric_id Ignored by this class - used by Puppet Server only. The metric id by which to track metrics on requests.



98
99
100
101
102
103
104
105
106
# File 'lib/puppet/network/http/connection.rb', line 98

def get(path, headers = {}, options = {})
  headers ||= {}
  options[:ssl_context] ||= resolve_ssl_context
  options[:redirect_limit] ||= @redirect_limit

  with_error_handling do
    to_ruby_response(@client.get(to_url(path), headers: headers, options: options))
  end
end

#head(path, headers = {}, options = {}) ⇒ Object

options not recognized by this class will be ignored - no error will be thrown.

Parameters:

  • path (String)
  • headers (Hash{String => String}) (defaults to: {})
  • options (Hash) (defaults to: {})

    options influencing the request made. Any

Options Hash (options):

  • :basic_auth (Hash{Symbol => String})

    The basic auth :username and :password to use for the request, :metric_id Ignored by this class - used by Puppet Server only. The metric id by which to track metrics on requests.



129
130
131
132
133
134
135
136
137
# File 'lib/puppet/network/http/connection.rb', line 129

def head(path, headers = {}, options = {})
  headers ||= {}
  options[:ssl_context] ||= resolve_ssl_context
  options[:redirect_limit] ||= @redirect_limit

  with_error_handling do
    to_ruby_response(@client.head(to_url(path), headers: headers, options: options))
  end
end

#portObject

The port to connect to.



71
72
73
# File 'lib/puppet/network/http/connection.rb', line 71

def port
  @site.port
end

#post(path, data, headers = nil, options = {}) ⇒ Object

options not recognized by this class will be ignored - no error will be thrown.

Parameters:

  • path (String)
  • data (String)
  • headers (Hash{String => String}) (defaults to: nil)
  • options (Hash) (defaults to: {})

    options influencing the request made. Any

Options Hash (options):

  • :basic_auth (Hash{Symbol => String})

    The basic auth :username and :password to use for the request, :metric_id Ignored by this class - used by Puppet Server only. The metric id by which to track metrics on requests.



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/puppet/network/http/connection.rb', line 113

def post(path, data, headers = nil, options = {})
  headers ||= {}
  headers['Content-Type'] ||= "application/x-www-form-urlencoded"
  data ||= ''
  options[:ssl_context] ||= resolve_ssl_context
  options[:redirect_limit] ||= @redirect_limit

  with_error_handling do
    to_ruby_response(@client.post(to_url(path), data, headers: headers, options: options))
  end
end

#put(path, data, headers = nil, options = {}) ⇒ Object

options not recognized by this class will be ignored - no error will be thrown.

Parameters:

  • path (String)
  • data (String)
  • headers (Hash{String => String}) (defaults to: nil)
  • options (Hash) (defaults to: {})

    options influencing the request made. Any

Options Hash (options):

  • :basic_auth (Hash{Symbol => String})

    The basic auth :username and :password to use for the request, :metric_id Ignored by this class - used by Puppet Server only. The metric id by which to track metrics on requests.



158
159
160
161
162
163
164
165
166
167
168
# File 'lib/puppet/network/http/connection.rb', line 158

def put(path, data, headers = nil, options = {})
  headers ||= {}
  headers['Content-Type'] ||= "application/x-www-form-urlencoded"
  data ||= ''
  options[:ssl_context] ||= resolve_ssl_context
  options[:redirect_limit] ||= @redirect_limit

  with_error_handling do
    to_ruby_response(@client.put(to_url(path), data, headers: headers, options: options))
  end
end

#request_get(*args, &block) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/puppet/network/http/connection.rb', line 170

def request_get(*args, &block)
  path, headers = *args
  headers ||= {}
  options = {
    ssl_context: resolve_ssl_context,
    redirect_limit: @redirect_limit
  }

  ruby_response = nil
  @client.get(to_url(path), headers: headers, options: options) do |response|
    ruby_response = to_ruby_response(response)
    yield ruby_response if block_given?
  end
  ruby_response
end

#request_head(*args) {|ruby_response| ... } ⇒ Object

Yields:

  • (ruby_response)


186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/puppet/network/http/connection.rb', line 186

def request_head(*args, &block)
  path, headers = *args
  headers ||= {}
  options = {
    ssl_context: resolve_ssl_context,
    redirect_limit: @redirect_limit
  }

  response = @client.head(to_url(path), headers: headers, options: options)
  ruby_response = to_ruby_response(response)
  yield ruby_response if block_given?
  ruby_response
end

#request_post(*args, &block) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/puppet/network/http/connection.rb', line 200

def request_post(*args, &block)
  path, data, headers = *args
  headers ||= {}
  headers['Content-Type'] ||= "application/x-www-form-urlencoded"
  options = {
    ssl_context: resolve_ssl_context,
    redirect_limit: @redirect_limit
  }

  ruby_response = nil
  @client.post(to_url(path), data, headers: headers, options: options) do |response|
    ruby_response = to_ruby_response(response)
    yield ruby_response if block_given?
  end
  ruby_response
end

#use_ssl?Boolean

Whether to use ssl

Returns:

  • (Boolean)


76
77
78
# File 'lib/puppet/network/http/connection.rb', line 76

def use_ssl?
  @site.use_ssl?
end

#verifierObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



81
82
83
# File 'lib/puppet/network/http/connection.rb', line 81

def verifier
  @verifier
end