Class: Chef::HTTP::BasicClient

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/http/basic_client.rb

Constant Summary collapse

HTTPS =
"https".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, opts = {}) ⇒ BasicClient

Instantiate a BasicClient.

Arguments:

url

An URI for the remote server.

Options:

ssl_policy

The SSL Policy to use, defaults to DefaultSSLPolicy



43
44
45
46
47
# File 'lib/chef/http/basic_client.rb', line 43

def initialize(url, opts={})
  @url = url
  @ssl_policy = opts[:ssl_policy] || DefaultSSLPolicy
  @http_client = build_http_client
end

Instance Attribute Details

#http_clientObject (readonly)

Returns the value of attribute http_client.



35
36
37
# File 'lib/chef/http/basic_client.rb', line 35

def http_client
  @http_client
end

#ssl_policyObject (readonly)

Returns the value of attribute ssl_policy.



36
37
38
# File 'lib/chef/http/basic_client.rb', line 36

def ssl_policy
  @ssl_policy
end

#urlObject (readonly)

Returns the value of attribute url.



34
35
36
# File 'lib/chef/http/basic_client.rb', line 34

def url
  @url
end

Instance Method Details

#build_http_clientObject



84
85
86
87
88
89
90
91
92
93
# File 'lib/chef/http/basic_client.rb', line 84

def build_http_client
  http_client = http_client_builder.new(host, port)

  if url.scheme == HTTPS
    configure_ssl(http_client)
  end

  http_client.read_timeout = config[:rest_timeout]
  http_client
end

#configObject



95
96
97
# File 'lib/chef/http/basic_client.rb', line 95

def config
  Chef::Config
end

#configure_ssl(http_client) ⇒ Object



111
112
113
114
# File 'lib/chef/http/basic_client.rb', line 111

def configure_ssl(http_client)
  http_client.use_ssl = true
  ssl_policy.apply_to(http_client)
end

#hostObject



49
50
51
# File 'lib/chef/http/basic_client.rb', line 49

def host
  @url.host
end

#http_client_builderObject



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/chef/http/basic_client.rb', line 99

def http_client_builder
  http_proxy = proxy_uri
  if http_proxy.nil?
    Net::HTTP
  else
    Chef::Log.debug("Using #{http_proxy.host}:#{http_proxy.port} for proxy")
    user = Chef::Config["#{url.scheme}_proxy_user"]
    pass = Chef::Config["#{url.scheme}_proxy_pass"]
    Net::HTTP.Proxy(http_proxy.host, http_proxy.port, user, pass)
  end
end

#portObject



53
54
55
# File 'lib/chef/http/basic_client.rb', line 53

def port
  @url.port
end

#proxy_uriObject

adapted from buildr/lib/buildr/core/transports.rb



76
77
78
79
80
81
82
# File 'lib/chef/http/basic_client.rb', line 76

def proxy_uri
  proxy = Chef::Config["#{url.scheme}_proxy"]
  proxy = URI.parse(proxy) if String === proxy
  excludes = Chef::Config[:no_proxy].to_s.split(/\s*,\s*/).compact
  excludes = excludes.map { |exclude| exclude =~ /:\d+$/ ? exclude : "#{exclude}:*" }
  return proxy unless excludes.any? { |exclude| File.fnmatch(exclude, "#{host}:#{port}") }
end

#request(method, url, req_body, base_headers = {}) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/chef/http/basic_client.rb', line 57

def request(method, url, req_body, base_headers={})
  http_request = HTTPRequest.new(method, url, req_body, base_headers).http_request
  Chef::Log.debug("Initiating #{method} to #{url}")
  Chef::Log.debug("---- HTTP Request Header Data: ----")
  base_headers.each do |name, value|
    Chef::Log.debug("#{name}: #{value}")
  end
  http_client.request(http_request) do |response|
    yield response if block_given?
    # http_client.request may not have the return signature we want, so
    # force the issue:
    return [http_request, response]
  end
rescue OpenSSL::SSL::SSLError => e
  Chef::Log.error("SSL Validation failure connecting to host: #{host} - #{e.message}")
  raise
end