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

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

Overview

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,
  :verify => nil,
  :redirect_limit => 10
}
@@openssl_initialized =
false

Instance Method Summary collapse

Methods included from Authentication

#warn_if_near_expiration

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

Creates a new HTTP client connection to host:port.

Parameters:

  • host (String)

    the host to which this client will connect to

  • port (Fixnum)

    the port to which this client will connect to

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

    options influencing the properties of the created connection, the following options are recognized:

    :use_ssl [Boolean] true to connect with SSL, false otherwise, defaults to true
    :verify [#setup_connection] An object that will configure any verification to do on the connection
    :redirect_limit [Fixnum] the number of allowed redirections, defaults to 10
    

    passing any other option in the options hash results in a Puppet::Error exception

Raises:



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/puppet/network/http/connection.rb', line 44

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]
end

Instance Method Details

#addressObject

end of Net::HTTP#request_* proxies



114
115
116
# File 'lib/puppet/network/http/connection.rb', line 114

def address
  connection.address
end

#delete(*args) ⇒ Object



69
70
71
# File 'lib/puppet/network/http/connection.rb', line 69

def delete(*args)
  request(:delete, *args)
end

#get(*args) ⇒ Object



57
58
59
# File 'lib/puppet/network/http/connection.rb', line 57

def get(*args)
  request(:get, *args)
end

#head(*args) ⇒ Object



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

def head(*args)
  request(:head, *args)
end

#portObject



118
119
120
# File 'lib/puppet/network/http/connection.rb', line 118

def port
  connection.port
end

#post(*args) ⇒ Object



61
62
63
# File 'lib/puppet/network/http/connection.rb', line 61

def post(*args)
  request(:post, *args)
end

#put(*args) ⇒ Object



73
74
75
# File 'lib/puppet/network/http/connection.rb', line 73

def put(*args)
  request(:put, *args)
end

#request(method, *args) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/puppet/network/http/connection.rb', line 77

def request(method, *args)
  current_args = args.dup
  @redirect_limit.times do |redirection|
    response = execute_request(method, *args)
    return response unless [301, 302, 307].include?(response.code.to_i)

    # handle the redirection
    location = URI.parse(response['location'])
    @connection = initialize_connection(location.host, location.port, location.scheme == 'https')

    # update to the current request path
    current_args = [location.path] + current_args.drop(1)
    # and try again...
  end
  raise RedirectionLimitExceededException, "Too many HTTP redirections for #{@host}:#{@port}"
end

#request_get(*args, &block) ⇒ Object

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. 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.



101
102
103
# File 'lib/puppet/network/http/connection.rb', line 101

def request_get(*args, &block)
  connection.request_get(*args, &block)
end

#request_head(*args, &block) ⇒ Object



105
106
107
# File 'lib/puppet/network/http/connection.rb', line 105

def request_head(*args, &block)
  connection.request_head(*args, &block)
end

#request_post(*args, &block) ⇒ Object



109
110
111
# File 'lib/puppet/network/http/connection.rb', line 109

def request_post(*args, &block)
  connection.request_post(*args, &block)
end

#use_ssl?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/puppet/network/http/connection.rb', line 122

def use_ssl?
  connection.use_ssl?
end